xref: /openbmc/linux/net/ipv6/addrconf.c (revision 63159f29be1df7f93563a8a0f78c5e65fc844ed6)
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>
493d1bec99SHannes Frederic Sowa #include <linux/inet.h>
501da177e4SLinus Torvalds #include <linux/in6.h>
511da177e4SLinus Torvalds #include <linux/netdevice.h>
521823730fSThomas Graf #include <linux/if_addr.h>
531da177e4SLinus Torvalds #include <linux/if_arp.h>
541da177e4SLinus Torvalds #include <linux/if_arcnet.h>
551da177e4SLinus Torvalds #include <linux/if_infiniband.h>
561da177e4SLinus Torvalds #include <linux/route.h>
571da177e4SLinus Torvalds #include <linux/inetdevice.h>
581da177e4SLinus Torvalds #include <linux/init.h>
595a0e3ad6STejun Heo #include <linux/slab.h>
601da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
611da177e4SLinus Torvalds #include <linux/sysctl.h>
621da177e4SLinus Torvalds #endif
634fc268d2SRandy Dunlap #include <linux/capability.h>
641da177e4SLinus Torvalds #include <linux/delay.h>
651da177e4SLinus Torvalds #include <linux/notifier.h>
66543537bdSPaulo Marques #include <linux/string.h>
67ddbe5032SEric Dumazet #include <linux/hash.h>
681da177e4SLinus Torvalds 
69457c4cbcSEric W. Biederman #include <net/net_namespace.h>
701da177e4SLinus Torvalds #include <net/sock.h>
711da177e4SLinus Torvalds #include <net/snmp.h>
721da177e4SLinus Torvalds 
7306a4c1c5Salex.bluesman.smirnov@gmail.com #include <net/af_ieee802154.h>
74cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h>
751da177e4SLinus Torvalds #include <net/ipv6.h>
761da177e4SLinus Torvalds #include <net/protocol.h>
771da177e4SLinus Torvalds #include <net/ndisc.h>
781da177e4SLinus Torvalds #include <net/ip6_route.h>
791da177e4SLinus Torvalds #include <net/addrconf.h>
801da177e4SLinus Torvalds #include <net/tcp.h>
811da177e4SLinus Torvalds #include <net/ip.h>
825d620266SThomas Graf #include <net/netlink.h>
83f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
841da177e4SLinus Torvalds #include <linux/if_tunnel.h>
851da177e4SLinus Torvalds #include <linux/rtnetlink.h>
86f3a1bfb1SNicolas Dichtel #include <linux/netconf.h>
871da177e4SLinus Torvalds #include <linux/random.h>
88e21e8467SStephen Hemminger #include <linux/uaccess.h>
897f7d9a6bSHerbert Xu #include <asm/unaligned.h>
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #include <linux/proc_fs.h>
921da177e4SLinus Torvalds #include <linux/seq_file.h>
93bc3b2d7fSPaul Gortmaker #include <linux/export.h>
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds /* Set to 3 to get tracing... */
961da177e4SLinus Torvalds #define ACONF_DEBUG 2
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
99ba3542e1Sdingtianhong #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
1001da177e4SLinus Torvalds #else
101ba3542e1Sdingtianhong #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
1021da177e4SLinus Torvalds #endif
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
10518a31e1eSThomas Graf 
1063d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \
1073d1bec99SHannes Frederic Sowa 	sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
1083d1bec99SHannes Frederic Sowa 
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
115a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev);
116408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
117408c4768SPavel Emelyanov #else
118a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev)
119408c4768SPavel Emelyanov {
120a317a2f1SWANG Cong 	return 0;
121408c4768SPavel Emelyanov }
122408c4768SPavel Emelyanov 
123408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
124408c4768SPavel Emelyanov {
125408c4768SPavel Emelyanov }
1261da177e4SLinus Torvalds #endif
1271da177e4SLinus Torvalds 
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 
1321b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
1331da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev);
134622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr,
135622c81d5SHannes Frederic Sowa 					u8 dad_count,
136622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev);
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /*
1391da177e4SLinus Torvalds  *	Configured unicast address hash table
1401da177e4SLinus Torvalds  */
141c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
1425c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock);
1431da177e4SLinus Torvalds 
144c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void);
145c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void);
146c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *);
1471da177e4SLinus Torvalds 
148c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq;
149c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1521da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1531da177e4SLinus Torvalds 
15493d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev,
15575c78500SMoni Shoua 				 unsigned long event);
1561da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1571da177e4SLinus Torvalds 
15821caa662SRomain Kuntz static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
15921caa662SRomain Kuntz 						  int plen,
16021caa662SRomain Kuntz 						  const struct net_device *dev,
16121caa662SRomain Kuntz 						  u32 flags, u32 noflags);
16221caa662SRomain Kuntz 
163cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp);
164c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w);
1651da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
166c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1671da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1681da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1691da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1721da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1733e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
17406bfe655SDaniel Lezcano 			       struct net_device *dev);
1751da177e4SLinus Torvalds 
176888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1771da177e4SLinus Torvalds 	.forwarding		= 0,
1781da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1791da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1801da177e4SLinus Torvalds 	.accept_ra		= 1,
1811da177e4SLinus Torvalds 	.accept_redirects	= 1,
1821da177e4SLinus Torvalds 	.autoconf		= 1,
1831da177e4SLinus Torvalds 	.force_mld_version	= 0,
184fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
185fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
1861da177e4SLinus Torvalds 	.dad_transmits		= 1,
1871da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1881da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1891da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1901da177e4SLinus Torvalds 	.use_tempaddr		= 0,
1911da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1921da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1931da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1941da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1951da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
19665f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
197d9333196SBen Greear 	.accept_ra_from_local	= 0,
198c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
199930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
200930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
20152e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
20209c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
20309c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
20409c884d4SYOSHIFUJI Hideaki #endif
205930d6ff2SYOSHIFUJI Hideaki #endif
206fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2070bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
208778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2091b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
210b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
211c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2123d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2133d1bec99SHannes Frederic Sowa 		.initialized = false,
2143d1bec99SHannes Frederic Sowa 	}
2151da177e4SLinus Torvalds };
2161da177e4SLinus Torvalds 
217ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2181da177e4SLinus Torvalds 	.forwarding		= 0,
2191da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2201da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2211da177e4SLinus Torvalds 	.accept_ra		= 1,
2221da177e4SLinus Torvalds 	.accept_redirects	= 1,
2231da177e4SLinus Torvalds 	.autoconf		= 1,
224fc4eba58SHannes Frederic Sowa 	.force_mld_version	= 0,
225fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
226fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2271da177e4SLinus Torvalds 	.dad_transmits		= 1,
2281da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2291da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
2301da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2311da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2321da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2331da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2341da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2351da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2361da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
23765f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
238d9333196SBen Greear 	.accept_ra_from_local	= 0,
239c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
240930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
241930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
24252e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
24309c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
24409c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
24509c884d4SYOSHIFUJI Hideaki #endif
246930d6ff2SYOSHIFUJI Hideaki #endif
247fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2480bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
249778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2501b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
251b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
252c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2533d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2543d1bec99SHannes Frederic Sowa 		.initialized = false,
2553d1bec99SHannes Frederic Sowa 	},
2561da177e4SLinus Torvalds };
2571da177e4SLinus Torvalds 
258f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
25905297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev)
260f24e3d65SMitsuru Chinen {
26105297949SDavid S. Miller 	return !qdisc_tx_is_noop(dev);
262f24e3d65SMitsuru Chinen }
263f24e3d65SMitsuru Chinen 
264b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev)
2651da177e4SLinus Torvalds {
266b7b1bfceSHannes Frederic Sowa 	if (del_timer(&idev->rs_timer))
267b7b1bfceSHannes Frederic Sowa 		__in6_dev_put(idev);
268b7b1bfceSHannes Frederic Sowa }
269b7b1bfceSHannes Frederic Sowa 
270c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
271b7b1bfceSHannes Frederic Sowa {
272c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
2731da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
276b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev,
2771da177e4SLinus Torvalds 				  unsigned long when)
2781da177e4SLinus Torvalds {
279b7b1bfceSHannes Frederic Sowa 	if (!timer_pending(&idev->rs_timer))
280b7b1bfceSHannes Frederic Sowa 		in6_dev_hold(idev);
281b7b1bfceSHannes Frederic Sowa 	mod_timer(&idev->rs_timer, jiffies + when);
2821da177e4SLinus Torvalds }
283b7b1bfceSHannes Frederic Sowa 
284c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
285c15b1ccaSHannes Frederic Sowa 				   unsigned long delay)
286b7b1bfceSHannes Frederic Sowa {
287c15b1ccaSHannes Frederic Sowa 	if (!delayed_work_pending(&ifp->dad_work))
288b7b1bfceSHannes Frederic Sowa 		in6_ifa_hold(ifp);
289c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
2927f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
2937f7d9a6bSHerbert Xu {
294827da44cSJohn Stultz 	int i;
295827da44cSJohn Stultz 
296698365faSWANG Cong 	idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
297698365faSWANG Cong 	if (!idev->stats.ipv6)
2987f7d9a6bSHerbert Xu 		goto err_ip;
299827da44cSJohn Stultz 
300827da44cSJohn Stultz 	for_each_possible_cpu(i) {
301827da44cSJohn Stultz 		struct ipstats_mib *addrconf_stats;
302698365faSWANG Cong 		addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
303827da44cSJohn Stultz 		u64_stats_init(&addrconf_stats->syncp);
304827da44cSJohn Stultz 	}
305827da44cSJohn Stultz 
306827da44cSJohn Stultz 
307be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
308be281e55SEric Dumazet 					GFP_KERNEL);
309be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
3107f7d9a6bSHerbert Xu 		goto err_icmp;
311be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
312be281e55SEric Dumazet 					   GFP_KERNEL);
313be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
31414878f75SDavid L Stevens 		goto err_icmpmsg;
3157f7d9a6bSHerbert Xu 
3167f7d9a6bSHerbert Xu 	return 0;
3177f7d9a6bSHerbert Xu 
31814878f75SDavid L Stevens err_icmpmsg:
319be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3207f7d9a6bSHerbert Xu err_icmp:
321698365faSWANG Cong 	free_percpu(idev->stats.ipv6);
3227f7d9a6bSHerbert Xu err_ip:
323aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3247f7d9a6bSHerbert Xu }
3257f7d9a6bSHerbert Xu 
3261da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct inet6_dev *ndev;
329a317a2f1SWANG Cong 	int err = -ENOMEM;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	ASSERT_RTNL();
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
334a317a2f1SWANG Cong 		return ERR_PTR(-EINVAL);
3351da177e4SLinus Torvalds 
336322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
337*63159f29SIan Morris 	if (!ndev)
338a317a2f1SWANG Cong 		return ERR_PTR(err);
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3411da177e4SLinus Torvalds 	ndev->dev = dev;
342502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
343b7b1bfceSHannes Frederic Sowa 	setup_timer(&ndev->rs_timer, addrconf_rs_timer,
344b7b1bfceSHannes Frederic Sowa 		    (unsigned long)ndev);
345c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3461da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3471da177e4SLinus Torvalds 	ndev->cnf.sysctl = NULL;
3481da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
349*63159f29SIan Morris 	if (!ndev->nd_parms) {
3501da177e4SLinus Torvalds 		kfree(ndev);
351a317a2f1SWANG Cong 		return ERR_PTR(err);
3521da177e4SLinus Torvalds 	}
3530187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
3540187bdfbSBen Hutchings 		dev_disable_lro(dev);
3551da177e4SLinus Torvalds 	/* We refer to the device */
3561da177e4SLinus Torvalds 	dev_hold(dev);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
359ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
360f3213831SJoe Perches 			"%s: cannot allocate memory for statistics; dev=%s.\n",
361ba3542e1Sdingtianhong 			__func__, dev->name);
3621da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3638603e33dSRoy Li 		dev_put(dev);
3648603e33dSRoy Li 		kfree(ndev);
365a317a2f1SWANG Cong 		return ERR_PTR(err);
3661da177e4SLinus Torvalds 	}
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
369ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
370f3213831SJoe Perches 			"%s: cannot create /proc/net/dev_snmp6/%s\n",
371ba3542e1Sdingtianhong 			__func__, dev->name);
372a317a2f1SWANG Cong 		goto err_release;
3731da177e4SLinus Torvalds 	}
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3761da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3771da177e4SLinus Torvalds 	 */
3781da177e4SLinus Torvalds 	in6_dev_hold(ndev);
3791da177e4SLinus Torvalds 
3801b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
3811b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
3821b34be74SYOSHIFUJI Hideaki 
38307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
384c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
385f3213831SJoe Perches 		pr_info("%s: Disabled Multicast RS\n", dev->name);
386c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
387c7dc89c0SFred L. Templin 	}
388b077d7abSYOSHIFUJI Hideaki #endif
389b077d7abSYOSHIFUJI Hideaki 
390372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
3911da177e4SLinus Torvalds 	setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
3921da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
3931da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
3949625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
3951da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
3961da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
3970be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
3981da177e4SLinus Torvalds 	} else {
3991da177e4SLinus Torvalds 		in6_dev_hold(ndev);
4001da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
4011da177e4SLinus Torvalds 	}
4025d9efa7eSDavid S. Miller 
403914faa14SDaniel Borkmann 	ndev->token = in6addr_any;
4041da177e4SLinus Torvalds 
405f24e3d65SMitsuru Chinen 	if (netif_running(dev) && addrconf_qdisc_ok(dev))
40653aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
40753aadcc9SHerbert Xu 
4081da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4091da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
410a317a2f1SWANG Cong 	err = addrconf_sysctl_register(ndev);
411a317a2f1SWANG Cong 	if (err) {
412a317a2f1SWANG Cong 		ipv6_mc_destroy_dev(ndev);
413a317a2f1SWANG Cong 		del_timer(&ndev->regen_timer);
414a317a2f1SWANG Cong 		goto err_release;
415a317a2f1SWANG Cong 	}
41630c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
417cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip6_ptr, ndev);
418d88ae4ccSYOSHIFUJI Hideaki 
4192c5e8933SHannes Frederic Sowa 	/* Join interface-local all-node multicast group */
4202c5e8933SHannes Frederic Sowa 	ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4212c5e8933SHannes Frederic Sowa 
422d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
423f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
424d88ae4ccSYOSHIFUJI Hideaki 
425d6ddef9eSLi Wei 	/* Join all-router multicast group if forwarding is set */
4268b2aaedeSLi Wei 	if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
427d6ddef9eSLi Wei 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
428d6ddef9eSLi Wei 
4291da177e4SLinus Torvalds 	return ndev;
430a317a2f1SWANG Cong 
431a317a2f1SWANG Cong err_release:
432a317a2f1SWANG Cong 	neigh_parms_release(&nd_tbl, ndev->nd_parms);
433a317a2f1SWANG Cong 	ndev->dead = 1;
434a317a2f1SWANG Cong 	in6_dev_finish_destroy(ndev);
435a317a2f1SWANG Cong 	return ERR_PTR(err);
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
438cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	struct inet6_dev *idev;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	ASSERT_RTNL();
4431da177e4SLinus Torvalds 
444e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
445e21e8467SStephen Hemminger 	if (!idev) {
446e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
447a317a2f1SWANG Cong 		if (IS_ERR(idev))
4481da177e4SLinus Torvalds 			return NULL;
4491da177e4SLinus Torvalds 	}
450c5e33bddSYOSHIFUJI Hideaki 
4511da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4521da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4531da177e4SLinus Torvalds 	return idev;
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
456f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
457f3a1bfb1SNicolas Dichtel {
458f3a1bfb1SNicolas Dichtel 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
459f3a1bfb1SNicolas Dichtel 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
460f3a1bfb1SNicolas Dichtel 
46176f8f6cbSNicolas Dichtel 	/* type -1 is used for ALL */
46276f8f6cbSNicolas Dichtel 	if (type == -1 || type == NETCONFA_FORWARDING)
463f3a1bfb1SNicolas Dichtel 		size += nla_total_size(4);
464b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
465d67b8c61SNicolas Dichtel 	if (type == -1 || type == NETCONFA_MC_FORWARDING)
466d67b8c61SNicolas Dichtel 		size += nla_total_size(4);
467b1afce95SDavid S. Miller #endif
468c92d5491Sstephen hemminger 	if (type == -1 || type == NETCONFA_PROXY_NEIGH)
469c92d5491Sstephen hemminger 		size += nla_total_size(4);
470f3a1bfb1SNicolas Dichtel 
471f3a1bfb1SNicolas Dichtel 	return size;
472f3a1bfb1SNicolas Dichtel }
473f3a1bfb1SNicolas Dichtel 
474f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
475f3a1bfb1SNicolas Dichtel 				      struct ipv6_devconf *devconf, u32 portid,
476f3a1bfb1SNicolas Dichtel 				      u32 seq, int event, unsigned int flags,
477f3a1bfb1SNicolas Dichtel 				      int type)
478f3a1bfb1SNicolas Dichtel {
479f3a1bfb1SNicolas Dichtel 	struct nlmsghdr  *nlh;
480f3a1bfb1SNicolas Dichtel 	struct netconfmsg *ncm;
481f3a1bfb1SNicolas Dichtel 
482f3a1bfb1SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
483f3a1bfb1SNicolas Dichtel 			flags);
484*63159f29SIan Morris 	if (!nlh)
485f3a1bfb1SNicolas Dichtel 		return -EMSGSIZE;
486f3a1bfb1SNicolas Dichtel 
487f3a1bfb1SNicolas Dichtel 	ncm = nlmsg_data(nlh);
488f3a1bfb1SNicolas Dichtel 	ncm->ncm_family = AF_INET6;
489f3a1bfb1SNicolas Dichtel 
490f3a1bfb1SNicolas Dichtel 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
491f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
492f3a1bfb1SNicolas Dichtel 
49376f8f6cbSNicolas Dichtel 	/* type -1 is used for ALL */
49476f8f6cbSNicolas Dichtel 	if ((type == -1 || type == NETCONFA_FORWARDING) &&
495f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
496f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
497b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
498d67b8c61SNicolas Dichtel 	if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
499d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
500d67b8c61SNicolas Dichtel 			devconf->mc_forwarding) < 0)
501d67b8c61SNicolas Dichtel 		goto nla_put_failure;
502b1afce95SDavid S. Miller #endif
503c92d5491Sstephen hemminger 	if ((type == -1 || type == NETCONFA_PROXY_NEIGH) &&
504c92d5491Sstephen hemminger 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
505c92d5491Sstephen hemminger 		goto nla_put_failure;
506c92d5491Sstephen hemminger 
507053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
508053c095aSJohannes Berg 	return 0;
509f3a1bfb1SNicolas Dichtel 
510f3a1bfb1SNicolas Dichtel nla_put_failure:
511f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
512f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
513f3a1bfb1SNicolas Dichtel }
514f3a1bfb1SNicolas Dichtel 
515d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
516f3a1bfb1SNicolas Dichtel 				  struct ipv6_devconf *devconf)
517f3a1bfb1SNicolas Dichtel {
518f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
519f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
520f3a1bfb1SNicolas Dichtel 
521f3a1bfb1SNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
522*63159f29SIan Morris 	if (!skb)
523f3a1bfb1SNicolas Dichtel 		goto errout;
524f3a1bfb1SNicolas Dichtel 
525f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
526f3a1bfb1SNicolas Dichtel 					 RTM_NEWNETCONF, 0, type);
527f3a1bfb1SNicolas Dichtel 	if (err < 0) {
528f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
529f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
530f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
531f3a1bfb1SNicolas Dichtel 		goto errout;
532f3a1bfb1SNicolas Dichtel 	}
533f3a1bfb1SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
534f3a1bfb1SNicolas Dichtel 	return;
535f3a1bfb1SNicolas Dichtel errout:
536f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
537f3a1bfb1SNicolas Dichtel }
538f3a1bfb1SNicolas Dichtel 
53976f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
54076f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
54176f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
542c92d5491Sstephen hemminger 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
54376f8f6cbSNicolas Dichtel };
54476f8f6cbSNicolas Dichtel 
54576f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
546661d2967SThomas Graf 				     struct nlmsghdr *nlh)
54776f8f6cbSNicolas Dichtel {
54876f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
54976f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
55076f8f6cbSNicolas Dichtel 	struct netconfmsg *ncm;
55176f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
55276f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
55376f8f6cbSNicolas Dichtel 	struct inet6_dev *in6_dev;
55476f8f6cbSNicolas Dichtel 	struct net_device *dev;
55576f8f6cbSNicolas Dichtel 	int ifindex;
55676f8f6cbSNicolas Dichtel 	int err;
55776f8f6cbSNicolas Dichtel 
55876f8f6cbSNicolas Dichtel 	err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
55976f8f6cbSNicolas Dichtel 			  devconf_ipv6_policy);
56076f8f6cbSNicolas Dichtel 	if (err < 0)
56176f8f6cbSNicolas Dichtel 		goto errout;
56276f8f6cbSNicolas Dichtel 
56376f8f6cbSNicolas Dichtel 	err = EINVAL;
56476f8f6cbSNicolas Dichtel 	if (!tb[NETCONFA_IFINDEX])
56576f8f6cbSNicolas Dichtel 		goto errout;
56676f8f6cbSNicolas Dichtel 
56776f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
56876f8f6cbSNicolas Dichtel 	switch (ifindex) {
56976f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
57076f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
57176f8f6cbSNicolas Dichtel 		break;
57276f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
57376f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
57476f8f6cbSNicolas Dichtel 		break;
57576f8f6cbSNicolas Dichtel 	default:
57676f8f6cbSNicolas Dichtel 		dev = __dev_get_by_index(net, ifindex);
577*63159f29SIan Morris 		if (!dev)
57876f8f6cbSNicolas Dichtel 			goto errout;
57976f8f6cbSNicolas Dichtel 		in6_dev = __in6_dev_get(dev);
580*63159f29SIan Morris 		if (!in6_dev)
58176f8f6cbSNicolas Dichtel 			goto errout;
58276f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
58376f8f6cbSNicolas Dichtel 		break;
58476f8f6cbSNicolas Dichtel 	}
58576f8f6cbSNicolas Dichtel 
58676f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
58776f8f6cbSNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
588*63159f29SIan Morris 	if (!skb)
58976f8f6cbSNicolas Dichtel 		goto errout;
59076f8f6cbSNicolas Dichtel 
59176f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
59276f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
59376f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
59476f8f6cbSNicolas Dichtel 					 -1);
59576f8f6cbSNicolas Dichtel 	if (err < 0) {
59676f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
59776f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
59876f8f6cbSNicolas Dichtel 		kfree_skb(skb);
59976f8f6cbSNicolas Dichtel 		goto errout;
60076f8f6cbSNicolas Dichtel 	}
60176f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
60276f8f6cbSNicolas Dichtel errout:
60376f8f6cbSNicolas Dichtel 	return err;
60476f8f6cbSNicolas Dichtel }
60576f8f6cbSNicolas Dichtel 
6067a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
6077a674200SNicolas Dichtel 				      struct netlink_callback *cb)
6087a674200SNicolas Dichtel {
6097a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
6107a674200SNicolas Dichtel 	int h, s_h;
6117a674200SNicolas Dichtel 	int idx, s_idx;
6127a674200SNicolas Dichtel 	struct net_device *dev;
6137a674200SNicolas Dichtel 	struct inet6_dev *idev;
6147a674200SNicolas Dichtel 	struct hlist_head *head;
6157a674200SNicolas Dichtel 
6167a674200SNicolas Dichtel 	s_h = cb->args[0];
6177a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
6187a674200SNicolas Dichtel 
6197a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6207a674200SNicolas Dichtel 		idx = 0;
6217a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
6227a674200SNicolas Dichtel 		rcu_read_lock();
62363998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
62463998ac2SNicolas Dichtel 			  net->dev_base_seq;
6257a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
6267a674200SNicolas Dichtel 			if (idx < s_idx)
6277a674200SNicolas Dichtel 				goto cont;
6287a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
6297a674200SNicolas Dichtel 			if (!idev)
6307a674200SNicolas Dichtel 				goto cont;
6317a674200SNicolas Dichtel 
6327a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
6337a674200SNicolas Dichtel 						       &idev->cnf,
6347a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
6357a674200SNicolas Dichtel 						       cb->nlh->nlmsg_seq,
6367a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
6377a674200SNicolas Dichtel 						       NLM_F_MULTI,
638053c095aSJohannes Berg 						       -1) < 0) {
6397a674200SNicolas Dichtel 				rcu_read_unlock();
6407a674200SNicolas Dichtel 				goto done;
6417a674200SNicolas Dichtel 			}
64263998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
6437a674200SNicolas Dichtel cont:
6447a674200SNicolas Dichtel 			idx++;
6457a674200SNicolas Dichtel 		}
6467a674200SNicolas Dichtel 		rcu_read_unlock();
6477a674200SNicolas Dichtel 	}
6487a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
6497a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
6507a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
6517a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6527a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6537a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
654053c095aSJohannes Berg 					       -1) < 0)
6557a674200SNicolas Dichtel 			goto done;
6567a674200SNicolas Dichtel 		else
6577a674200SNicolas Dichtel 			h++;
6587a674200SNicolas Dichtel 	}
6597a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
6607a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
6617a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
6627a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6637a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6647a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
665053c095aSJohannes Berg 					       -1) < 0)
6667a674200SNicolas Dichtel 			goto done;
6677a674200SNicolas Dichtel 		else
6687a674200SNicolas Dichtel 			h++;
6697a674200SNicolas Dichtel 	}
6707a674200SNicolas Dichtel done:
6717a674200SNicolas Dichtel 	cb->args[0] = h;
6727a674200SNicolas Dichtel 	cb->args[1] = idx;
6737a674200SNicolas Dichtel 
6747a674200SNicolas Dichtel 	return skb->len;
6757a674200SNicolas Dichtel }
6767a674200SNicolas Dichtel 
6771da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
6781da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	struct net_device *dev;
6811da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	if (!idev)
6841da177e4SLinus Torvalds 		return;
6851da177e4SLinus Torvalds 	dev = idev->dev;
6860187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
6870187bdfbSBen Hutchings 		dev_disable_lro(dev);
6881a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
6892c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
690f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
6912c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
6922c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
6932c5e8933SHannes Frederic Sowa 		} else {
694f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
6952c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
6962c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
6972c5e8933SHannes Frederic Sowa 		}
6981da177e4SLinus Torvalds 	}
699502a2ffdSstephen hemminger 
700502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
7012c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
7022c12a74cSMichal Wrobel 			continue;
7031da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
7041da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
7051da177e4SLinus Torvalds 		else
7061da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
7071da177e4SLinus Torvalds 	}
708f3a1bfb1SNicolas Dichtel 	inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
709f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 
713e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	struct net_device *dev;
7161da177e4SLinus Torvalds 	struct inet6_dev *idev;
7171da177e4SLinus Torvalds 
7184acd4945SBen Hutchings 	for_each_netdev(net, dev) {
7191da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
7201da177e4SLinus Torvalds 		if (idev) {
721e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
722e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
7231da177e4SLinus Torvalds 			if (changed)
7241da177e4SLinus Torvalds 				dev_forward_change(idev);
7251da177e4SLinus Torvalds 		}
7261da177e4SLinus Torvalds 	}
7271da177e4SLinus Torvalds }
728c8fecf22SPavel Emelyanov 
729013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
730c8fecf22SPavel Emelyanov {
731bff16c2fSPavel Emelyanov 	struct net *net;
732013d97e9SFrancesco Ruggeri 	int old;
733013d97e9SFrancesco Ruggeri 
734013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
735013d97e9SFrancesco Ruggeri 		return restart_syscall();
736bff16c2fSPavel Emelyanov 
737bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
738013d97e9SFrancesco Ruggeri 	old = *p;
739013d97e9SFrancesco Ruggeri 	*p = newf;
740c8fecf22SPavel Emelyanov 
741013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
742f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
743f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
744f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
745f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
746013d97e9SFrancesco Ruggeri 		rtnl_unlock();
747013d97e9SFrancesco Ruggeri 		return 0;
74888af182eSEric W. Biederman 	}
749b325fddbSStephen Hemminger 
750e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
751e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
752e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
753f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
754f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
755f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
756f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
757013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
758c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
7590187bdfbSBen Hutchings 	rtnl_unlock();
760c8fecf22SPavel Emelyanov 
761013d97e9SFrancesco Ruggeri 	if (newf)
7627b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
763b325fddbSStephen Hemminger 	return 1;
764c8fecf22SPavel Emelyanov }
7651da177e4SLinus Torvalds #endif
7661da177e4SLinus Torvalds 
7675c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
7681da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
7691da177e4SLinus Torvalds {
770c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
771547b792cSIlpo Järvinen 
7721da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
77391df42beSJoe Perches 	pr_debug("%s\n", __func__);
7741da177e4SLinus Torvalds #endif
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
7771da177e4SLinus Torvalds 
778c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
779c15b1ccaSHannes Frederic Sowa 		pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
780c15b1ccaSHannes Frederic Sowa 			  ifp);
7811da177e4SLinus Torvalds 
782e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
783f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
7841da177e4SLinus Torvalds 		return;
7851da177e4SLinus Torvalds 	}
78694e187c0SAmerigo Wang 	ip6_rt_put(ifp->rt);
7871da177e4SLinus Torvalds 
788e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
7891da177e4SLinus Torvalds }
7901da177e4SLinus Torvalds 
791e55ffac6SBrian Haley static void
792e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
793e55ffac6SBrian Haley {
794502a2ffdSstephen hemminger 	struct list_head *p;
7958a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
796e55ffac6SBrian Haley 
797e55ffac6SBrian Haley 	/*
798e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
799e55ffac6SBrian Haley 	 * global before linklocal.
800e55ffac6SBrian Haley 	 */
801502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
802502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
803502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
8048a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
805e55ffac6SBrian Haley 			break;
806e55ffac6SBrian Haley 	}
807e55ffac6SBrian Haley 
808b54c9b98SDavid S. Miller 	list_add_tail(&ifp->if_list, p);
809e55ffac6SBrian Haley }
810e55ffac6SBrian Haley 
811ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr)
8123eb84f49SYOSHIFUJI Hideaki {
813ddbe5032SEric Dumazet 	return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
8143eb84f49SYOSHIFUJI Hideaki }
8153eb84f49SYOSHIFUJI Hideaki 
8161da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds static struct inet6_ifaddr *
8193f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
8203f8f5298SJiri Pirko 	      const struct in6_addr *peer_addr, int pfxlen,
8218a226b2cSJiri Benc 	      int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
8241da177e4SLinus Torvalds 	struct rt6_info *rt;
8253a88a81dSstephen hemminger 	unsigned int hash;
8261da177e4SLinus Torvalds 	int err = 0;
827d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
828d68b8270SYOSHIFUJI Hideaki 
829d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
830d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
831d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
832d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
833d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
8341da177e4SLinus Torvalds 
8358814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
8361da177e4SLinus Torvalds 	if (idev->dead) {
8371da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
8381da177e4SLinus Torvalds 		goto out2;
8391da177e4SLinus Torvalds 	}
8401da177e4SLinus Torvalds 
84156d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
8429bdd8d40SBrian Haley 		err = -EACCES;
8439bdd8d40SBrian Haley 		goto out2;
8449bdd8d40SBrian Haley 	}
8459bdd8d40SBrian Haley 
8465c578aedSstephen hemminger 	spin_lock(&addrconf_hash_lock);
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
849c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
850ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: already assigned\n");
8511da177e4SLinus Torvalds 		err = -EEXIST;
8521da177e4SLinus Torvalds 		goto out;
8531da177e4SLinus Torvalds 	}
8541da177e4SLinus Torvalds 
855322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
8561da177e4SLinus Torvalds 
857*63159f29SIan Morris 	if (!ifa) {
858ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: malloc failed\n");
8591da177e4SLinus Torvalds 		err = -ENOBUFS;
8601da177e4SLinus Torvalds 		goto out;
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 
8638f031519SDavid S. Miller 	rt = addrconf_dst_alloc(idev, addr, false);
8641da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
8651da177e4SLinus Torvalds 		err = PTR_ERR(rt);
8661da177e4SLinus Torvalds 		goto out;
8671da177e4SLinus Torvalds 	}
8681da177e4SLinus Torvalds 
869bba24896SJiri Pirko 	neigh_parms_data_state_setall(idev->nd_parms);
870bba24896SJiri Pirko 
8714e3fd7a0SAlexey Dobriyan 	ifa->addr = *addr;
8723f8f5298SJiri Pirko 	if (peer_addr)
8733f8f5298SJiri Pirko 		ifa->peer_addr = *peer_addr;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
876c15b1ccaSHannes Frederic Sowa 	INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
877c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
8781da177e4SLinus Torvalds 	ifa->scope = scope;
8791da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
8801da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
8818a226b2cSJiri Benc 	ifa->valid_lft = valid_lft;
8828a226b2cSJiri Benc 	ifa->prefered_lft = prefered_lft;
8831da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
884617fe29dSDaniel Borkmann 	ifa->tokenized = false;
8851da177e4SLinus Torvalds 
88657f5f544SKeir Fraser 	ifa->rt = rt;
88757f5f544SKeir Fraser 
8881da177e4SLinus Torvalds 	ifa->idev = idev;
8891da177e4SLinus Torvalds 	in6_dev_hold(idev);
8901da177e4SLinus Torvalds 	/* For caller */
8911da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	/* Add to big hash table */
894ddbe5032SEric Dumazet 	hash = inet6_addr_hash(addr);
8951da177e4SLinus Torvalds 
8965c578aedSstephen hemminger 	hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
8975c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	write_lock(&idev->lock);
9001da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
901e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
904372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
9051da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
9061da177e4SLinus Torvalds 	}
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
9091da177e4SLinus Torvalds 	write_unlock(&idev->lock);
9101da177e4SLinus Torvalds out2:
9118814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
9121da177e4SLinus Torvalds 
913fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
914f88c91ddSCong Wang 		inet6addr_notifier_call_chain(NETDEV_UP, ifa);
9151da177e4SLinus Torvalds 	else {
9161da177e4SLinus Torvalds 		kfree(ifa);
9171da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
9181da177e4SLinus Torvalds 	}
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	return ifa;
9211da177e4SLinus Torvalds out:
9225c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
9231da177e4SLinus Torvalds 	goto out2;
9241da177e4SLinus Torvalds }
9251da177e4SLinus Torvalds 
9265b84efecSThomas Haller enum cleanup_prefix_rt_t {
9275b84efecSThomas Haller 	CLEANUP_PREFIX_RT_NOP,    /* no cleanup action for prefix route */
9285b84efecSThomas Haller 	CLEANUP_PREFIX_RT_DEL,    /* delete the prefix route */
9295b84efecSThomas Haller 	CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
9305b84efecSThomas Haller };
9315b84efecSThomas Haller 
9325b84efecSThomas Haller /*
9335b84efecSThomas Haller  * Check, whether the prefix for ifp would still need a prefix route
9345b84efecSThomas Haller  * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
9355b84efecSThomas Haller  * constants.
9365b84efecSThomas Haller  *
9375b84efecSThomas Haller  * 1) we don't purge prefix if address was not permanent.
9385b84efecSThomas Haller  *    prefix is managed by its own lifetime.
9395b84efecSThomas Haller  * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
9405b84efecSThomas Haller  * 3) if there are no addresses, delete prefix.
9415b84efecSThomas Haller  * 4) if there are still other permanent address(es),
9425b84efecSThomas Haller  *    corresponding prefix is still permanent.
9435b84efecSThomas Haller  * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
9445b84efecSThomas Haller  *    don't purge the prefix, assume user space is managing it.
9455b84efecSThomas Haller  * 6) otherwise, update prefix lifetime to the
9465b84efecSThomas Haller  *    longest valid lifetime among the corresponding
9475b84efecSThomas Haller  *    addresses on the device.
9485b84efecSThomas Haller  *    Note: subsequent RA will update lifetime.
9495b84efecSThomas Haller  **/
9505b84efecSThomas Haller static enum cleanup_prefix_rt_t
9515b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
9525b84efecSThomas Haller {
9535b84efecSThomas Haller 	struct inet6_ifaddr *ifa;
9545b84efecSThomas Haller 	struct inet6_dev *idev = ifp->idev;
9555b84efecSThomas Haller 	unsigned long lifetime;
9565b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
9575b84efecSThomas Haller 
9585b84efecSThomas Haller 	*expires = jiffies;
9595b84efecSThomas Haller 
9605b84efecSThomas Haller 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
9615b84efecSThomas Haller 		if (ifa == ifp)
9625b84efecSThomas Haller 			continue;
9635b84efecSThomas Haller 		if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
9645b84efecSThomas Haller 				       ifp->prefix_len))
9655b84efecSThomas Haller 			continue;
9665b84efecSThomas Haller 		if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
9675b84efecSThomas Haller 			return CLEANUP_PREFIX_RT_NOP;
9685b84efecSThomas Haller 
9695b84efecSThomas Haller 		action = CLEANUP_PREFIX_RT_EXPIRE;
9705b84efecSThomas Haller 
9715b84efecSThomas Haller 		spin_lock(&ifa->lock);
9725b84efecSThomas Haller 
9735b84efecSThomas Haller 		lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
9745b84efecSThomas Haller 		/*
9755b84efecSThomas Haller 		 * Note: Because this address is
9765b84efecSThomas Haller 		 * not permanent, lifetime <
9775b84efecSThomas Haller 		 * LONG_MAX / HZ here.
9785b84efecSThomas Haller 		 */
9795b84efecSThomas Haller 		if (time_before(*expires, ifa->tstamp + lifetime * HZ))
9805b84efecSThomas Haller 			*expires = ifa->tstamp + lifetime * HZ;
9815b84efecSThomas Haller 		spin_unlock(&ifa->lock);
9825b84efecSThomas Haller 	}
9835b84efecSThomas Haller 
9845b84efecSThomas Haller 	return action;
9855b84efecSThomas Haller }
9865b84efecSThomas Haller 
9875b84efecSThomas Haller static void
9885b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
9895b84efecSThomas Haller {
9905b84efecSThomas Haller 	struct rt6_info *rt;
9915b84efecSThomas Haller 
9925b84efecSThomas Haller 	rt = addrconf_get_prefix_route(&ifp->addr,
9935b84efecSThomas Haller 				       ifp->prefix_len,
9945b84efecSThomas Haller 				       ifp->idev->dev,
9955b84efecSThomas Haller 				       0, RTF_GATEWAY | RTF_DEFAULT);
9965b84efecSThomas Haller 	if (rt) {
9975b84efecSThomas Haller 		if (del_rt)
9985b84efecSThomas Haller 			ip6_del_rt(rt);
9995b84efecSThomas Haller 		else {
10005b84efecSThomas Haller 			if (!(rt->rt6i_flags & RTF_EXPIRES))
10015b84efecSThomas Haller 				rt6_set_expires(rt, expires);
10025b84efecSThomas Haller 			ip6_rt_put(rt);
10035b84efecSThomas Haller 		}
10045b84efecSThomas Haller 	}
10055b84efecSThomas Haller }
10065b84efecSThomas Haller 
10075b84efecSThomas Haller 
10081da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
10111da177e4SLinus Torvalds {
10124c5ff6a6SHerbert Xu 	int state;
10135b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
10145b84efecSThomas Haller 	unsigned long expires;
10151da177e4SLinus Torvalds 
1016c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
1017c15b1ccaSHannes Frederic Sowa 
10188e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
10194c5ff6a6SHerbert Xu 	state = ifp->state;
1020e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
10218e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
10224c5ff6a6SHerbert Xu 
10234c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
10244c5ff6a6SHerbert Xu 		goto out;
10251da177e4SLinus Torvalds 
10265c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
10275c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
10285c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
10291da177e4SLinus Torvalds 
10305b84efecSThomas Haller 	write_lock_bh(&ifp->idev->lock);
10315d9efa7eSDavid S. Miller 
10321da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
1033372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
10341da177e4SLinus Torvalds 		if (ifp->ifpub) {
10351da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
10361da177e4SLinus Torvalds 			ifp->ifpub = NULL;
10371da177e4SLinus Torvalds 		}
10381da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
10391da177e4SLinus Torvalds 	}
10401da177e4SLinus Torvalds 
10415b84efecSThomas Haller 	if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
10425b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &expires);
10435b84efecSThomas Haller 
1044502a2ffdSstephen hemminger 	list_del_init(&ifp->if_list);
10451da177e4SLinus Torvalds 	__in6_ifa_put(ifp);
1046502a2ffdSstephen hemminger 
10475b84efecSThomas Haller 	write_unlock_bh(&ifp->idev->lock);
10481da177e4SLinus Torvalds 
1049c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
1050b2238566SAndrey Vagin 
10511da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
10521da177e4SLinus Torvalds 
1053f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
10541da177e4SLinus Torvalds 
10555b84efecSThomas Haller 	if (action != CLEANUP_PREFIX_RT_NOP) {
10565b84efecSThomas Haller 		cleanup_prefix_route(ifp, expires,
10575b84efecSThomas Haller 			action == CLEANUP_PREFIX_RT_DEL);
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 
1060c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1061c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
10624c5ff6a6SHerbert Xu out:
10631da177e4SLinus Torvalds 	in6_ifa_put(ifp);
10641da177e4SLinus Torvalds }
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
10671da177e4SLinus Torvalds {
10681da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
10691da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
107076f793e3SLorenzo Colitti 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1071eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
10721da177e4SLinus Torvalds 	int tmp_plen;
10731da177e4SLinus Torvalds 	int ret = 0;
107495c385b4SNeil Horman 	u32 addr_flags;
107576f793e3SLorenzo Colitti 	unsigned long now = jiffies;
10761da177e4SLinus Torvalds 
107753bd6749SJiri Pirko 	write_lock_bh(&idev->lock);
10781da177e4SLinus Torvalds 	if (ift) {
10791da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
10801da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
10811da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
10821da177e4SLinus Torvalds 		tmpaddr = &addr;
10831da177e4SLinus Torvalds 	} else {
10841da177e4SLinus Torvalds 		tmpaddr = NULL;
10851da177e4SLinus Torvalds 	}
10861da177e4SLinus Torvalds retry:
10871da177e4SLinus Torvalds 	in6_dev_hold(idev);
10881da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
108953bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1090f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
10911da177e4SLinus Torvalds 		in6_dev_put(idev);
10921da177e4SLinus Torvalds 		ret = -1;
10931da177e4SLinus Torvalds 		goto out;
10941da177e4SLinus Torvalds 	}
10951da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
10961da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
10971da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
10981da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
109953bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1100f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1101f3213831SJoe Perches 			__func__);
11021da177e4SLinus Torvalds 		in6_dev_put(idev);
11031da177e4SLinus Torvalds 		ret = -1;
11041da177e4SLinus Torvalds 		goto out;
11051da177e4SLinus Torvalds 	}
11061da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
11071da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1108eb7e0575SSorin Dumitru 	__ipv6_try_regen_rndid(idev, tmpaddr);
11091da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
111076f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
11111da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
11121da177e4SLinus Torvalds 			      ifp->valid_lft,
11137a876b0eSGlenn Wurster 			      idev->cnf.temp_valid_lft + age);
11141da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
11151da177e4SLinus Torvalds 				 ifp->prefered_lft,
11167a876b0eSGlenn Wurster 				 idev->cnf.temp_prefered_lft + age -
1117784e2710SBen Hutchings 				 idev->cnf.max_desync_factor);
11181da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
11191da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
11201da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
11211da177e4SLinus Torvalds 
1122eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1123eac55bf9SBenoit Boissinot 			idev->cnf.dad_transmits *
11241f9248e5SJiri Pirko 			NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
112553bd6749SJiri Pirko 	write_unlock_bh(&idev->lock);
112695c385b4SNeil Horman 
1127eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1128eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1129eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1130eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1131ecab6701SHeiner Kallweit 	 * Use age calculation as in addrconf_verify to avoid unnecessary
1132ecab6701SHeiner Kallweit 	 * temporary addresses being generated.
1133eac55bf9SBenoit Boissinot 	 */
1134ecab6701SHeiner Kallweit 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1135ecab6701SHeiner Kallweit 	if (tmp_prefered_lft <= regen_advance + age) {
1136eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1137eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1138eac55bf9SBenoit Boissinot 		ret = -1;
1139eac55bf9SBenoit Boissinot 		goto out;
1140eac55bf9SBenoit Boissinot 	}
1141eac55bf9SBenoit Boissinot 
114295c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
114395c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
114495c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
114595c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
114695c385b4SNeil Horman 
11474b08a8f1SHannes Frederic Sowa 	ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
11488a226b2cSJiri Benc 			    ipv6_addr_scope(&addr), addr_flags,
11494b08a8f1SHannes Frederic Sowa 			    tmp_valid_lft, tmp_prefered_lft);
11504b08a8f1SHannes Frederic Sowa 	if (IS_ERR(ift)) {
11511da177e4SLinus Torvalds 		in6_ifa_put(ifp);
11521da177e4SLinus Torvalds 		in6_dev_put(idev);
1153f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
11541da177e4SLinus Torvalds 		tmpaddr = &addr;
115553bd6749SJiri Pirko 		write_lock_bh(&idev->lock);
11561da177e4SLinus Torvalds 		goto retry;
11571da177e4SLinus Torvalds 	}
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
11601da177e4SLinus Torvalds 	ift->ifpub = ifp;
116176f793e3SLorenzo Colitti 	ift->cstamp = now;
11621da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
11631da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
11641da177e4SLinus Torvalds 
1165cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
11661da177e4SLinus Torvalds 	in6_ifa_put(ift);
11671da177e4SLinus Torvalds 	in6_dev_put(idev);
11681da177e4SLinus Torvalds out:
11691da177e4SLinus Torvalds 	return ret;
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds /*
1173072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
11741da177e4SLinus Torvalds  */
1175a9b05723SYOSHIFUJI Hideaki enum {
1176a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1177a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1178a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1179a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1180a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1181a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1182a9b05723SYOSHIFUJI Hideaki #endif
1183a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1184a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1185a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1186a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1187a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
11887fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
11897fd2561eSErik Kline 	IPV6_SADDR_RULE_NOT_OPTIMISTIC,
11907fd2561eSErik Kline #endif
1191a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1192072047e4SYOSHIFUJI Hideaki };
1193072047e4SYOSHIFUJI Hideaki 
1194a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1195a9b05723SYOSHIFUJI Hideaki 	int			rule;
1196a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1197a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1198a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1199a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1200a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1201a9b05723SYOSHIFUJI Hideaki };
1202a9b05723SYOSHIFUJI Hideaki 
1203a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
12049acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1205a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1206a9b05723SYOSHIFUJI Hideaki 	int scope;
1207a9b05723SYOSHIFUJI Hideaki 	int label;
12087cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1209a9b05723SYOSHIFUJI Hideaki };
1210072047e4SYOSHIFUJI Hideaki 
1211b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
12121da177e4SLinus Torvalds {
121345bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1214072047e4SYOSHIFUJI Hideaki 		return 1;
1215072047e4SYOSHIFUJI Hideaki 	return 0;
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
12187fd2561eSErik Kline static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
12197fd2561eSErik Kline {
12207fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
12217fd2561eSErik Kline 	return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
12227fd2561eSErik Kline #else
12237fd2561eSErik Kline 	return false;
12247fd2561eSErik Kline #endif
12257fd2561eSErik Kline }
12267fd2561eSErik Kline 
12273de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
12283de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1229a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1230a9b05723SYOSHIFUJI Hideaki 			       int i)
1231a9b05723SYOSHIFUJI Hideaki {
1232a9b05723SYOSHIFUJI Hideaki 	int ret;
1233a9b05723SYOSHIFUJI Hideaki 
1234a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1235a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1236a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1237a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1238a9b05723SYOSHIFUJI Hideaki 			break;
1239a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1240a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1241a9b05723SYOSHIFUJI Hideaki 			break;
1242a9b05723SYOSHIFUJI Hideaki 		default:
1243a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1244a9b05723SYOSHIFUJI Hideaki 		}
1245a9b05723SYOSHIFUJI Hideaki 		goto out;
1246a9b05723SYOSHIFUJI Hideaki 	}
1247a9b05723SYOSHIFUJI Hideaki 
1248a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1249a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1250a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1251a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1252a9b05723SYOSHIFUJI Hideaki 		break;
1253a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1254a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1255a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1256a9b05723SYOSHIFUJI Hideaki 		break;
1257a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1258a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1259a9b05723SYOSHIFUJI Hideaki 		 *
1260a9b05723SYOSHIFUJI Hideaki 		 *      ret
1261a9b05723SYOSHIFUJI Hideaki 		 *       ^
1262a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1263a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1264a9b05723SYOSHIFUJI Hideaki 		 *       |
1265a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1266a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1267a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1268db9c7c39Sstephen hemminger 		 *  B-15 |    \        if scope is enough for destination.
1269a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1270a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1271a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1272a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1273a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1274a9b05723SYOSHIFUJI Hideaki 		 *
1275a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1276a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1277a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1278a9b05723SYOSHIFUJI Hideaki 		 */
1279a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1280a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1281a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1282a9b05723SYOSHIFUJI Hideaki 		else
1283a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1284a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1285a9b05723SYOSHIFUJI Hideaki 		break;
1286a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
12877fd2561eSErik Kline 	    {
1288a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
12897fd2561eSErik Kline 		u8 avoid = IFA_F_DEPRECATED;
12907fd2561eSErik Kline 
12917fd2561eSErik Kline 		if (!ipv6_use_optimistic_addr(score->ifa->idev))
12927fd2561eSErik Kline 			avoid |= IFA_F_OPTIMISTIC;
1293a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
12947fd2561eSErik Kline 		      !(score->ifa->flags & avoid);
1295a9b05723SYOSHIFUJI Hideaki 		break;
12967fd2561eSErik Kline 	    }
1297a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1298a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
12997cbca67cSYOSHIFUJI Hideaki 	    {
1300a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
13017cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
13027cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1303a9b05723SYOSHIFUJI Hideaki 		break;
13047cbca67cSYOSHIFUJI Hideaki 	    }
1305a9b05723SYOSHIFUJI Hideaki #endif
1306a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1307a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1308a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1309a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1310a9b05723SYOSHIFUJI Hideaki 		break;
1311a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1312a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
13133de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
13143de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1315a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1316a9b05723SYOSHIFUJI Hideaki 		break;
1317a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
13187cbca67cSYOSHIFUJI Hideaki 	    {
1319a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
132025985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1321a9b05723SYOSHIFUJI Hideaki 		 */
13227cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
13237cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
13247cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
13257cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1326a9b05723SYOSHIFUJI Hideaki 		break;
13277cbca67cSYOSHIFUJI Hideaki 	    }
1328a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1329a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1330a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1331a9b05723SYOSHIFUJI Hideaki 		 */
1332a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1333a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1334a9b05723SYOSHIFUJI Hideaki 		break;
1335a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1336a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
133791b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
133891b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
133991b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
134091b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1341a9b05723SYOSHIFUJI Hideaki 		break;
13427fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
13437fd2561eSErik Kline 	case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
13447fd2561eSErik Kline 		/* Optimistic addresses still have lower precedence than other
13457fd2561eSErik Kline 		 * preferred addresses.
13467fd2561eSErik Kline 		 */
13477fd2561eSErik Kline 		ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
13487fd2561eSErik Kline 		break;
13497fd2561eSErik Kline #endif
1350a9b05723SYOSHIFUJI Hideaki 	default:
1351a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1352a9b05723SYOSHIFUJI Hideaki 	}
1353a9b05723SYOSHIFUJI Hideaki 
1354a9b05723SYOSHIFUJI Hideaki 	if (ret)
1355a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1356a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1357a9b05723SYOSHIFUJI Hideaki out:
1358a9b05723SYOSHIFUJI Hideaki 	return ret;
1359a9b05723SYOSHIFUJI Hideaki }
1360a9b05723SYOSHIFUJI Hideaki 
1361b3f644fcSPatrick McHardy int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
13629acd9f3aSYOSHIFUJI Hideaki 		       const struct in6_addr *daddr, unsigned int prefs,
13637cbca67cSYOSHIFUJI Hideaki 		       struct in6_addr *saddr)
13641da177e4SLinus Torvalds {
1365a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_score scores[2],
1366a9b05723SYOSHIFUJI Hideaki 				*score = &scores[0], *hiscore = &scores[1];
1367a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_dst dst;
1368072047e4SYOSHIFUJI Hideaki 	struct net_device *dev;
1369a9b05723SYOSHIFUJI Hideaki 	int dst_type;
13701da177e4SLinus Torvalds 
1371a9b05723SYOSHIFUJI Hideaki 	dst_type = __ipv6_addr_type(daddr);
1372a9b05723SYOSHIFUJI Hideaki 	dst.addr = daddr;
1373a9b05723SYOSHIFUJI Hideaki 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1374a9b05723SYOSHIFUJI Hideaki 	dst.scope = __ipv6_addr_src_scope(dst_type);
13753de23255SBenjamin Thery 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
13767cbca67cSYOSHIFUJI Hideaki 	dst.prefs = prefs;
1377a9b05723SYOSHIFUJI Hideaki 
1378a9b05723SYOSHIFUJI Hideaki 	hiscore->rule = -1;
1379a9b05723SYOSHIFUJI Hideaki 	hiscore->ifa = NULL;
13801da177e4SLinus Torvalds 
13818814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1382072047e4SYOSHIFUJI Hideaki 
1383c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
1384072047e4SYOSHIFUJI Hideaki 		struct inet6_dev *idev;
1385072047e4SYOSHIFUJI Hideaki 
1386a9b05723SYOSHIFUJI Hideaki 		/* Candidate Source Address (section 4)
1387072047e4SYOSHIFUJI Hideaki 		 *  - multicast and link-local destination address,
1388072047e4SYOSHIFUJI Hideaki 		 *    the set of candidate source address MUST only
1389072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1390072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same link as the outgoing
1391072047e4SYOSHIFUJI Hideaki 		 *    interface.
1392072047e4SYOSHIFUJI Hideaki 		 * (- For site-local destination addresses, the
1393072047e4SYOSHIFUJI Hideaki 		 *    set of candidate source addresses MUST only
1394072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1395072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same site as the outgoing
1396072047e4SYOSHIFUJI Hideaki 		 *    interface.)
1397072047e4SYOSHIFUJI Hideaki 		 */
1398a9b05723SYOSHIFUJI Hideaki 		if (((dst_type & IPV6_ADDR_MULTICAST) ||
1399a9b05723SYOSHIFUJI Hideaki 		     dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1400a9b05723SYOSHIFUJI Hideaki 		    dst.ifindex && dev->ifindex != dst.ifindex)
1401072047e4SYOSHIFUJI Hideaki 			continue;
1402072047e4SYOSHIFUJI Hideaki 
14031da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
1404072047e4SYOSHIFUJI Hideaki 		if (!idev)
1405072047e4SYOSHIFUJI Hideaki 			continue;
1406072047e4SYOSHIFUJI Hideaki 
14071da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1408502a2ffdSstephen hemminger 		list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1409a9b05723SYOSHIFUJI Hideaki 			int i;
1410072047e4SYOSHIFUJI Hideaki 
1411a9b05723SYOSHIFUJI Hideaki 			/*
14126b3ae80aSYOSHIFUJI Hideaki 			 * - Tentative Address (RFC2462 section 5.4)
14136b3ae80aSYOSHIFUJI Hideaki 			 *  - A tentative address is not considered
14146b3ae80aSYOSHIFUJI Hideaki 			 *    "assigned to an interface" in the traditional
141595c385b4SNeil Horman 			 *    sense, unless it is also flagged as optimistic.
14166b3ae80aSYOSHIFUJI Hideaki 			 * - Candidate Source Address (section 4)
1417072047e4SYOSHIFUJI Hideaki 			 *  - In any case, anycast addresses, multicast
1418072047e4SYOSHIFUJI Hideaki 			 *    addresses, and the unspecified address MUST
1419072047e4SYOSHIFUJI Hideaki 			 *    NOT be included in a candidate set.
1420072047e4SYOSHIFUJI Hideaki 			 */
1421a9b05723SYOSHIFUJI Hideaki 			if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1422a9b05723SYOSHIFUJI Hideaki 			    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
14236b3ae80aSYOSHIFUJI Hideaki 				continue;
1424a9b05723SYOSHIFUJI Hideaki 
1425a9b05723SYOSHIFUJI Hideaki 			score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1426a9b05723SYOSHIFUJI Hideaki 
1427a9b05723SYOSHIFUJI Hideaki 			if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1428a9b05723SYOSHIFUJI Hideaki 				     score->addr_type & IPV6_ADDR_MULTICAST)) {
1429ba7a46f1SJoe Perches 				net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1430072047e4SYOSHIFUJI Hideaki 						    dev->name);
14311da177e4SLinus Torvalds 				continue;
1432072047e4SYOSHIFUJI Hideaki 			}
1433072047e4SYOSHIFUJI Hideaki 
1434a9b05723SYOSHIFUJI Hideaki 			score->rule = -1;
1435a9b05723SYOSHIFUJI Hideaki 			bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1436072047e4SYOSHIFUJI Hideaki 
1437a9b05723SYOSHIFUJI Hideaki 			for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1438a9b05723SYOSHIFUJI Hideaki 				int minihiscore, miniscore;
1439072047e4SYOSHIFUJI Hideaki 
14403de23255SBenjamin Thery 				minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
14413de23255SBenjamin Thery 				miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1442072047e4SYOSHIFUJI Hideaki 
1443a9b05723SYOSHIFUJI Hideaki 				if (minihiscore > miniscore) {
1444a9b05723SYOSHIFUJI Hideaki 					if (i == IPV6_SADDR_RULE_SCOPE &&
1445a9b05723SYOSHIFUJI Hideaki 					    score->scopedist > 0) {
1446a9b05723SYOSHIFUJI Hideaki 						/*
1447a9b05723SYOSHIFUJI Hideaki 						 * special case:
1448a9b05723SYOSHIFUJI Hideaki 						 * each remaining entry
1449a9b05723SYOSHIFUJI Hideaki 						 * has too small (not enough)
1450a9b05723SYOSHIFUJI Hideaki 						 * scope, because ifa entries
1451a9b05723SYOSHIFUJI Hideaki 						 * are sorted by their scope
1452a9b05723SYOSHIFUJI Hideaki 						 * values.
1453072047e4SYOSHIFUJI Hideaki 						 */
1454a9b05723SYOSHIFUJI Hideaki 						goto try_nextdev;
1455072047e4SYOSHIFUJI Hideaki 					}
1456a9b05723SYOSHIFUJI Hideaki 					break;
1457a9b05723SYOSHIFUJI Hideaki 				} else if (minihiscore < miniscore) {
1458a9b05723SYOSHIFUJI Hideaki 					if (hiscore->ifa)
1459a9b05723SYOSHIFUJI Hideaki 						in6_ifa_put(hiscore->ifa);
146099cd07a5SJuha-Matti Tapio 
1461a9b05723SYOSHIFUJI Hideaki 					in6_ifa_hold(score->ifa);
1462072047e4SYOSHIFUJI Hideaki 
1463a0bffffcSIlpo Järvinen 					swap(hiscore, score);
1464a9b05723SYOSHIFUJI Hideaki 
1465a9b05723SYOSHIFUJI Hideaki 					/* restore our iterator */
1466a9b05723SYOSHIFUJI Hideaki 					score->ifa = hiscore->ifa;
1467a9b05723SYOSHIFUJI Hideaki 
1468a9b05723SYOSHIFUJI Hideaki 					break;
14691da177e4SLinus Torvalds 				}
1470a9b05723SYOSHIFUJI Hideaki 			}
1471a9b05723SYOSHIFUJI Hideaki 		}
1472a9b05723SYOSHIFUJI Hideaki try_nextdev:
14731da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
14741da177e4SLinus Torvalds 	}
14758814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
14761da177e4SLinus Torvalds 
1477a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1478072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
14791da177e4SLinus Torvalds 
14804e3fd7a0SAlexey Dobriyan 	*saddr = hiscore->ifa->addr;
1481a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1482072047e4SYOSHIFUJI Hideaki 	return 0;
14831da177e4SLinus Torvalds }
14845e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
14851da177e4SLinus Torvalds 
14868965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1487479840ffSJiri Pirko 		      u32 banned_flags)
1488b7b1bfceSHannes Frederic Sowa {
1489b7b1bfceSHannes Frederic Sowa 	struct inet6_ifaddr *ifp;
1490b7b1bfceSHannes Frederic Sowa 	int err = -EADDRNOTAVAIL;
1491b7b1bfceSHannes Frederic Sowa 
1492602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1493602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1494602582caSHannes Frederic Sowa 			break;
1495b7b1bfceSHannes Frederic Sowa 		if (ifp->scope == IFA_LINK &&
1496b7b1bfceSHannes Frederic Sowa 		    !(ifp->flags & banned_flags)) {
1497b7b1bfceSHannes Frederic Sowa 			*addr = ifp->addr;
1498b7b1bfceSHannes Frederic Sowa 			err = 0;
1499b7b1bfceSHannes Frederic Sowa 			break;
1500b7b1bfceSHannes Frederic Sowa 		}
1501b7b1bfceSHannes Frederic Sowa 	}
1502b7b1bfceSHannes Frederic Sowa 	return err;
1503b7b1bfceSHannes Frederic Sowa }
1504b7b1bfceSHannes Frederic Sowa 
150595c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1506479840ffSJiri Pirko 		    u32 banned_flags)
15071da177e4SLinus Torvalds {
15081da177e4SLinus Torvalds 	struct inet6_dev *idev;
15091da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
15101da177e4SLinus Torvalds 
15118814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1512e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1513e21e8467SStephen Hemminger 	if (idev) {
15141da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1515b7b1bfceSHannes Frederic Sowa 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
15161da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
15171da177e4SLinus Torvalds 	}
15188814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
15191da177e4SLinus Torvalds 	return err;
15201da177e4SLinus Torvalds }
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
15231da177e4SLinus Torvalds {
15241da177e4SLinus Torvalds 	int cnt = 0;
15251da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1528502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list)
15291da177e4SLinus Torvalds 		cnt++;
15301da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
15311da177e4SLinus Torvalds 	return cnt;
15321da177e4SLinus Torvalds }
15331da177e4SLinus Torvalds 
1534b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
15352a7851bfSFlorian Westphal 		  const struct net_device *dev, int strict)
15361da177e4SLinus Torvalds {
1537c58da4c6SErik Kline 	return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1538c58da4c6SErik Kline }
1539c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr);
1540c58da4c6SErik Kline 
1541c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1542c58da4c6SErik Kline 			    const struct net_device *dev, int strict,
1543c58da4c6SErik Kline 			    u32 banned_flags)
1544c58da4c6SErik Kline {
1545eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1546ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
1547c58da4c6SErik Kline 	u32 ifp_flags;
15481da177e4SLinus Torvalds 
15495c578aedSstephen hemminger 	rcu_read_lock_bh();
1550b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1551878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1552bfeade08SDaniel Lezcano 			continue;
1553c58da4c6SErik Kline 		/* Decouple optimistic from tentative for evaluation here.
1554c58da4c6SErik Kline 		 * Ban optimistic addresses explicitly, when required.
1555c58da4c6SErik Kline 		 */
1556c58da4c6SErik Kline 		ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1557c58da4c6SErik Kline 			    ? (ifp->flags&~IFA_F_TENTATIVE)
1558c58da4c6SErik Kline 			    : ifp->flags;
15591da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1560c58da4c6SErik Kline 		    !(ifp_flags&banned_flags) &&
1561*63159f29SIan Morris 		    (!dev || ifp->idev->dev == dev ||
1562eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
15635c578aedSstephen hemminger 			rcu_read_unlock_bh();
1564eedf042aSStephen Hemminger 			return 1;
1565eedf042aSStephen Hemminger 		}
1566eedf042aSStephen Hemminger 	}
15675c578aedSstephen hemminger 
1568eedf042aSStephen Hemminger 	rcu_read_unlock_bh();
1569eedf042aSStephen Hemminger 	return 0;
15701da177e4SLinus Torvalds }
1571c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
15727159039aSYOSHIFUJI Hideaki 
15733e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
157406bfe655SDaniel Lezcano 			       struct net_device *dev)
15751da177e4SLinus Torvalds {
1576ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
15771da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
15781da177e4SLinus Torvalds 
1579b67bfe0dSSasha Levin 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1580878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
158106bfe655SDaniel Lezcano 			continue;
15821da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
1583*63159f29SIan Morris 			if (!dev || ifp->idev->dev == dev)
15843e81c6daSDavid S. Miller 				return true;
15851da177e4SLinus Torvalds 		}
15861da177e4SLinus Torvalds 	}
15873e81c6daSDavid S. Miller 	return false;
15881da177e4SLinus Torvalds }
15891da177e4SLinus Torvalds 
15907df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev.
15917df37ff3SCatalin\(ux\) M. BOIE  * If one is found it returns true.
15927df37ff3SCatalin\(ux\) M. BOIE  */
15937df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
15947df37ff3SCatalin\(ux\) M. BOIE 	const unsigned int prefix_len, struct net_device *dev)
15957df37ff3SCatalin\(ux\) M. BOIE {
15967df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_dev *idev;
15977df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_ifaddr *ifa;
15987df37ff3SCatalin\(ux\) M. BOIE 	bool ret = false;
15997df37ff3SCatalin\(ux\) M. BOIE 
16007df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_lock();
16017df37ff3SCatalin\(ux\) M. BOIE 	idev = __in6_dev_get(dev);
16027df37ff3SCatalin\(ux\) M. BOIE 	if (idev) {
16037df37ff3SCatalin\(ux\) M. BOIE 		read_lock_bh(&idev->lock);
16047df37ff3SCatalin\(ux\) M. BOIE 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
16057df37ff3SCatalin\(ux\) M. BOIE 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
16067df37ff3SCatalin\(ux\) M. BOIE 			if (ret)
16077df37ff3SCatalin\(ux\) M. BOIE 				break;
16087df37ff3SCatalin\(ux\) M. BOIE 		}
16097df37ff3SCatalin\(ux\) M. BOIE 		read_unlock_bh(&idev->lock);
16107df37ff3SCatalin\(ux\) M. BOIE 	}
16117df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_unlock();
16127df37ff3SCatalin\(ux\) M. BOIE 
16137df37ff3SCatalin\(ux\) M. BOIE 	return ret;
16147df37ff3SCatalin\(ux\) M. BOIE }
16157df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix);
16167df37ff3SCatalin\(ux\) M. BOIE 
1617b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
161852eeeb84SYOSHIFUJI Hideaki {
161952eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
162052eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
162152eeeb84SYOSHIFUJI Hideaki 	int	onlink;
162252eeeb84SYOSHIFUJI Hideaki 
162352eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
162452eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
162552eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
162652eeeb84SYOSHIFUJI Hideaki 	if (idev) {
162752eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
1628502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
162952eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
163052eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
163152eeeb84SYOSHIFUJI Hideaki 			if (onlink)
163252eeeb84SYOSHIFUJI Hideaki 				break;
163352eeeb84SYOSHIFUJI Hideaki 		}
163452eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
163552eeeb84SYOSHIFUJI Hideaki 	}
163652eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
163752eeeb84SYOSHIFUJI Hideaki 	return onlink;
163852eeeb84SYOSHIFUJI Hideaki }
163952eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
164052eeeb84SYOSHIFUJI Hideaki 
16419acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
16421cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
16431da177e4SLinus Torvalds {
1644b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
1645ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
16461da177e4SLinus Torvalds 
16475c578aedSstephen hemminger 	rcu_read_lock_bh();
1648b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1649878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
16501cab3da6SDaniel Lezcano 			continue;
16511da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
1652*63159f29SIan Morris 			if (!dev || ifp->idev->dev == dev ||
16531da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1654b79d1d54SDavid S. Miller 				result = ifp;
16551da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
16561da177e4SLinus Torvalds 				break;
16571da177e4SLinus Torvalds 			}
16581da177e4SLinus Torvalds 		}
16591da177e4SLinus Torvalds 	}
16605c578aedSstephen hemminger 	rcu_read_unlock_bh();
16611da177e4SLinus Torvalds 
1662b79d1d54SDavid S. Miller 	return result;
16631da177e4SLinus Torvalds }
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
16661da177e4SLinus Torvalds 
1667cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
16681da177e4SLinus Torvalds {
16691da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
16701da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
1671c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifp);
16721da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
1673cc411d0bSBrian Haley 		if (dad_failed)
1674cc411d0bSBrian Haley 			ifp->flags |= IFA_F_DADFAILED;
16751da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
1676e2577a06SHerbert Xu 		if (dad_failed)
1677e2577a06SHerbert Xu 			ipv6_ifa_notify(0, ifp);
16781da177e4SLinus Torvalds 		in6_ifa_put(ifp);
16791da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
16801da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
16811da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
16821da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
16831da177e4SLinus Torvalds 		if (ifpub) {
16841da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
16851da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
16861da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
16871da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
16881da177e4SLinus Torvalds 		} else {
16891da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
16901da177e4SLinus Torvalds 		}
16911da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
1692c15b1ccaSHannes Frederic Sowa 	} else {
16931da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
16941da177e4SLinus Torvalds 	}
1695c15b1ccaSHannes Frederic Sowa }
16961da177e4SLinus Torvalds 
1697f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1698f2344a13SHerbert Xu {
1699f2344a13SHerbert Xu 	int err = -ENOENT;
1700f2344a13SHerbert Xu 
17018e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
1702f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
1703f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
1704f2344a13SHerbert Xu 		err = 0;
1705f2344a13SHerbert Xu 	}
17068e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
1707f2344a13SHerbert Xu 
1708f2344a13SHerbert Xu 	return err;
1709f2344a13SHerbert Xu }
1710f2344a13SHerbert Xu 
17113c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
17123c21edbdSYOSHIFUJI Hideaki {
17135f40ef77SHannes Frederic Sowa 	struct in6_addr addr;
17141b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
17151855b7c3SHannes Frederic Sowa 	struct net *net = dev_net(ifp->idev->dev);
17169bdd8d40SBrian Haley 
1717853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
1718853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
1719f2344a13SHerbert Xu 		return;
1720853dc2e0SUrsula Braun 	}
1721f2344a13SHerbert Xu 
1722e87cc472SJoe Perches 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
17230522fea6SJens Rosenboom 			     ifp->idev->dev->name, &ifp->addr);
17249bdd8d40SBrian Haley 
17255f40ef77SHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
17261b34be74SYOSHIFUJI Hideaki 
17275f40ef77SHannes Frederic Sowa 	if (ifp->flags & IFA_F_STABLE_PRIVACY) {
17285f40ef77SHannes Frederic Sowa 		int scope = ifp->scope;
17295f40ef77SHannes Frederic Sowa 		u32 flags = ifp->flags;
17305f40ef77SHannes Frederic Sowa 		struct in6_addr new_addr;
17315f40ef77SHannes Frederic Sowa 		struct inet6_ifaddr *ifp2;
17325f40ef77SHannes Frederic Sowa 		u32 valid_lft, preferred_lft;
17335f40ef77SHannes Frederic Sowa 		int pfxlen = ifp->prefix_len;
17345f40ef77SHannes Frederic Sowa 		int retries = ifp->stable_privacy_retry + 1;
17355f40ef77SHannes Frederic Sowa 
17361855b7c3SHannes Frederic Sowa 		if (retries > net->ipv6.sysctl.idgen_retries) {
17375f40ef77SHannes Frederic Sowa 			net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
17385f40ef77SHannes Frederic Sowa 					     ifp->idev->dev->name);
17395f40ef77SHannes Frederic Sowa 			goto errdad;
17405f40ef77SHannes Frederic Sowa 		}
17415f40ef77SHannes Frederic Sowa 
17425f40ef77SHannes Frederic Sowa 		new_addr = ifp->addr;
17435f40ef77SHannes Frederic Sowa 		if (ipv6_generate_stable_address(&new_addr, retries,
17445f40ef77SHannes Frederic Sowa 						 idev))
17455f40ef77SHannes Frederic Sowa 			goto errdad;
17465f40ef77SHannes Frederic Sowa 
17475f40ef77SHannes Frederic Sowa 		valid_lft = ifp->valid_lft;
17485f40ef77SHannes Frederic Sowa 		preferred_lft = ifp->prefered_lft;
17495f40ef77SHannes Frederic Sowa 
17505f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp->lock);
17515f40ef77SHannes Frederic Sowa 
17525f40ef77SHannes Frederic Sowa 		if (idev->cnf.max_addresses &&
17535f40ef77SHannes Frederic Sowa 		    ipv6_count_addresses(idev) >=
17545f40ef77SHannes Frederic Sowa 		    idev->cnf.max_addresses)
17555f40ef77SHannes Frederic Sowa 			goto lock_errdad;
17565f40ef77SHannes Frederic Sowa 
17575f40ef77SHannes Frederic Sowa 		net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
17585f40ef77SHannes Frederic Sowa 				     ifp->idev->dev->name);
17595f40ef77SHannes Frederic Sowa 
17605f40ef77SHannes Frederic Sowa 		ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
17615f40ef77SHannes Frederic Sowa 				     scope, flags, valid_lft,
17625f40ef77SHannes Frederic Sowa 				     preferred_lft);
17635f40ef77SHannes Frederic Sowa 		if (IS_ERR(ifp2))
17645f40ef77SHannes Frederic Sowa 			goto lock_errdad;
17655f40ef77SHannes Frederic Sowa 
17665f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp2->lock);
17675f40ef77SHannes Frederic Sowa 		ifp2->stable_privacy_retry = retries;
17685f40ef77SHannes Frederic Sowa 		ifp2->state = INET6_IFADDR_STATE_PREDAD;
17695f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp2->lock);
17705f40ef77SHannes Frederic Sowa 
17711855b7c3SHannes Frederic Sowa 		addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
17725f40ef77SHannes Frederic Sowa 		in6_ifa_put(ifp2);
17735f40ef77SHannes Frederic Sowa lock_errdad:
17745f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp->lock);
17755f40ef77SHannes Frederic Sowa 	} else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
17761b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[0] = htonl(0xfe800000);
17771b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[1] = 0;
17781b34be74SYOSHIFUJI Hideaki 
17791b34be74SYOSHIFUJI Hideaki 		if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
17801b34be74SYOSHIFUJI Hideaki 		    ipv6_addr_equal(&ifp->addr, &addr)) {
17811b34be74SYOSHIFUJI Hideaki 			/* DAD failed for link-local based on MAC address */
17821b34be74SYOSHIFUJI Hideaki 			idev->cnf.disable_ipv6 = 1;
17839bdd8d40SBrian Haley 
1784f3213831SJoe Perches 			pr_info("%s: IPv6 being disabled!\n",
17859bdd8d40SBrian Haley 				ifp->idev->dev->name);
17861b34be74SYOSHIFUJI Hideaki 		}
17871b34be74SYOSHIFUJI Hideaki 	}
17881b34be74SYOSHIFUJI Hideaki 
17895f40ef77SHannes Frederic Sowa errdad:
1790c15b1ccaSHannes Frederic Sowa 	/* transition from _POSTDAD to _ERRDAD */
1791c15b1ccaSHannes Frederic Sowa 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
17928e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
1793c15b1ccaSHannes Frederic Sowa 
1794c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, 0);
17953c21edbdSYOSHIFUJI Hideaki }
17961da177e4SLinus Torvalds 
1797a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group.
1798a9ed4a29SSabrina Dubroca  * caller must hold RTNL */
1799b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
18001da177e4SLinus Torvalds {
18011da177e4SLinus Torvalds 	struct in6_addr maddr;
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
18041da177e4SLinus Torvalds 		return;
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
18071da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
18081da177e4SLinus Torvalds }
18091da177e4SLinus Torvalds 
1810a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
1811b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
18121da177e4SLinus Torvalds {
18131da177e4SLinus Torvalds 	struct in6_addr maddr;
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
18161da177e4SLinus Torvalds 		return;
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
18191da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
18201da177e4SLinus Torvalds }
18211da177e4SLinus Torvalds 
1822a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
182320380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
18241da177e4SLinus Torvalds {
18251da177e4SLinus Torvalds 	struct in6_addr addr;
1826c15b1ccaSHannes Frederic Sowa 
182788ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
18282bda8a0cSBjørn Mork 		return;
18291da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
18301da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
18311da177e4SLinus Torvalds 		return;
1832013b4d90SWANG Cong 	__ipv6_dev_ac_inc(ifp->idev, &addr);
18331da177e4SLinus Torvalds }
18341da177e4SLinus Torvalds 
1835a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
183620380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
18371da177e4SLinus Torvalds {
18381da177e4SLinus Torvalds 	struct in6_addr addr;
1839c15b1ccaSHannes Frederic Sowa 
184088ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
184132019e65SYOSHIFUJI Hideaki 		return;
18421da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
18431da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
18441da177e4SLinus Torvalds 		return;
18451da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
18461da177e4SLinus Torvalds }
18471da177e4SLinus Torvalds 
1848073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
18491da177e4SLinus Torvalds {
18501da177e4SLinus Torvalds 	if (dev->addr_len != ETH_ALEN)
18511da177e4SLinus Torvalds 		return -1;
18521da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr, 3);
18531da177e4SLinus Torvalds 	memcpy(eui + 5, dev->dev_addr + 3, 3);
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds 	/*
18561da177e4SLinus Torvalds 	 * The zSeries OSA network cards can be shared among various
18571da177e4SLinus Torvalds 	 * OS instances, but the OSA cards have only one MAC address.
18581da177e4SLinus Torvalds 	 * This leads to duplicate address conflicts in conjunction
18591da177e4SLinus Torvalds 	 * with IPv6 if more than one instance uses the same card.
18601da177e4SLinus Torvalds 	 *
18611da177e4SLinus Torvalds 	 * The driver for these cards can deliver a unique 16-bit
18621da177e4SLinus Torvalds 	 * identifier for each instance sharing the same card.  It is
18631da177e4SLinus Torvalds 	 * placed instead of 0xFFFE in the interface identifier.  The
18641da177e4SLinus Torvalds 	 * "u" bit of the interface identifier is not inverted in this
18651da177e4SLinus Torvalds 	 * case.  Hence the resulting interface identifier has local
18661da177e4SLinus Torvalds 	 * scope according to RFC2373.
18671da177e4SLinus Torvalds 	 */
18681da177e4SLinus Torvalds 	if (dev->dev_id) {
18691da177e4SLinus Torvalds 		eui[3] = (dev->dev_id >> 8) & 0xFF;
18701da177e4SLinus Torvalds 		eui[4] = dev->dev_id & 0xFF;
18711da177e4SLinus Torvalds 	} else {
18721da177e4SLinus Torvalds 		eui[3] = 0xFF;
18731da177e4SLinus Torvalds 		eui[4] = 0xFE;
18741da177e4SLinus Torvalds 		eui[0] ^= 2;
18751da177e4SLinus Torvalds 	}
18761da177e4SLinus Torvalds 	return 0;
1877073a8e0eSYOSHIFUJI Hideaki }
1878073a8e0eSYOSHIFUJI Hideaki 
187906a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
188006a4c1c5Salex.bluesman.smirnov@gmail.com {
188106a4c1c5Salex.bluesman.smirnov@gmail.com 	if (dev->addr_len != IEEE802154_ADDR_LEN)
188206a4c1c5Salex.bluesman.smirnov@gmail.com 		return -1;
188306a4c1c5Salex.bluesman.smirnov@gmail.com 	memcpy(eui, dev->dev_addr, 8);
18845e98a36eSYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
188506a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
188606a4c1c5Salex.bluesman.smirnov@gmail.com }
188706a4c1c5Salex.bluesman.smirnov@gmail.com 
1888cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1889cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
1890cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	union fwnet_hwaddr *ha;
1891cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1892cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
1893cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
1894cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1895cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	ha = (union fwnet_hwaddr *)dev->dev_addr;
1896cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1897cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1898cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
1899cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
1900cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
1901cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1902073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1903073a8e0eSYOSHIFUJI Hideaki {
19041da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
19051da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
19061da177e4SLinus Torvalds 		return -1;
19071da177e4SLinus Torvalds 	memset(eui, 0, 7);
19081da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
19091da177e4SLinus Torvalds 	return 0;
1910073a8e0eSYOSHIFUJI Hideaki }
1911073a8e0eSYOSHIFUJI Hideaki 
1912073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1913073a8e0eSYOSHIFUJI Hideaki {
19141da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
19151da177e4SLinus Torvalds 		return -1;
19161da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
19171da177e4SLinus Torvalds 	eui[0] |= 2;
19181da177e4SLinus Torvalds 	return 0;
19191da177e4SLinus Torvalds }
1920073a8e0eSYOSHIFUJI Hideaki 
1921c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1922dfd982baSYOSHIFUJI Hideaki {
19239af28511SSascha Hlusiak 	if (addr == 0)
19249af28511SSascha Hlusiak 		return -1;
1925dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1926dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1927dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1928dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1929dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1930dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1931dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
1932dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
1933dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
1934dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
1935dfd982baSYOSHIFUJI Hideaki 	return 0;
1936dfd982baSYOSHIFUJI Hideaki }
1937dfd982baSYOSHIFUJI Hideaki 
1938dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1939dfd982baSYOSHIFUJI Hideaki {
1940dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
1941dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1942dfd982baSYOSHIFUJI Hideaki 	return -1;
1943dfd982baSYOSHIFUJI Hideaki }
1944dfd982baSYOSHIFUJI Hideaki 
1945aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1946aee80b54Sstephen hemminger {
1947aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1948aee80b54Sstephen hemminger }
1949aee80b54Sstephen hemminger 
1950e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
1951e837735eSNicolas Dichtel {
1952e837735eSNicolas Dichtel 	memcpy(eui, dev->perm_addr, 3);
1953e837735eSNicolas Dichtel 	memcpy(eui + 5, dev->perm_addr + 3, 3);
1954e837735eSNicolas Dichtel 	eui[3] = 0xFF;
1955e837735eSNicolas Dichtel 	eui[4] = 0xFE;
1956e837735eSNicolas Dichtel 	eui[0] ^= 2;
1957e837735eSNicolas Dichtel 	return 0;
1958e837735eSNicolas Dichtel }
1959e837735eSNicolas Dichtel 
1960073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1961073a8e0eSYOSHIFUJI Hideaki {
1962073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
1963073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
1964073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
1965073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
1966073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
1967073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
1968073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
1969073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
1970c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
1971dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
1972aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
1973aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
1974e74bccb8SJukka Rissanen 	case ARPHRD_6LOWPAN:
197506a4c1c5Salex.bluesman.smirnov@gmail.com 	case ARPHRD_IEEE802154:
197606a4c1c5Salex.bluesman.smirnov@gmail.com 		return addrconf_ifid_eui64(eui, dev);
1977cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
1978cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
1979e837735eSNicolas Dichtel 	case ARPHRD_TUNNEL6:
1980e837735eSNicolas Dichtel 		return addrconf_ifid_ip6tnl(eui, dev);
1981073a8e0eSYOSHIFUJI Hideaki 	}
19821da177e4SLinus Torvalds 	return -1;
19831da177e4SLinus Torvalds }
19841da177e4SLinus Torvalds 
19851da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
19861da177e4SLinus Torvalds {
19871da177e4SLinus Torvalds 	int err = -1;
19881da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1991602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1992602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1993602582caSHannes Frederic Sowa 			break;
19941da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
19951da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
19961da177e4SLinus Torvalds 			err = 0;
19971da177e4SLinus Torvalds 			break;
19981da177e4SLinus Torvalds 		}
19991da177e4SLinus Torvalds 	}
20001da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
20011da177e4SLinus Torvalds 	return err;
20021da177e4SLinus Torvalds }
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2005eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev)
20061da177e4SLinus Torvalds {
20071da177e4SLinus Torvalds regen:
2008955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
20091da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	/*
20121da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
20131da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
20141da177e4SLinus Torvalds 	 *
20151da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
20161da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
2017c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
20181da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
20191da177e4SLinus Torvalds 	 *  - value 0
20201da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
20211da177e4SLinus Torvalds 	 */
20221da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
20231da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
20241da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
20251da177e4SLinus Torvalds 		goto regen;
20261da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
20271da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
20281da177e4SLinus Torvalds 			goto regen;
20291da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
20301da177e4SLinus Torvalds 			goto regen;
20311da177e4SLinus Torvalds 	}
20321da177e4SLinus Torvalds }
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
20351da177e4SLinus Torvalds {
20361da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
20371da177e4SLinus Torvalds 	unsigned long expires;
20381da177e4SLinus Torvalds 
20398814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
20401da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 	if (idev->dead)
20431da177e4SLinus Torvalds 		goto out;
20441da177e4SLinus Torvalds 
2045eb7e0575SSorin Dumitru 	__ipv6_regen_rndid(idev);
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds 	expires = jiffies +
20481da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
20491f9248e5SJiri Pirko 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
20501f9248e5SJiri Pirko 		NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
2051784e2710SBen Hutchings 		idev->cnf.max_desync_factor * HZ;
20521da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
2053f3213831SJoe Perches 		pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2054f3213831SJoe Perches 			__func__, idev->dev->name);
20551da177e4SLinus Torvalds 		goto out;
20561da177e4SLinus Torvalds 	}
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
20591da177e4SLinus Torvalds 		in6_dev_hold(idev);
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds out:
20621da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
20638814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
20641da177e4SLinus Torvalds 	in6_dev_put(idev);
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds 
2067eb7e0575SSorin Dumitru static void  __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
20688e5e8f30SEldad Zack {
20691da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2070eb7e0575SSorin Dumitru 		__ipv6_regen_rndid(idev);
20711da177e4SLinus Torvalds }
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds /*
20741da177e4SLinus Torvalds  *	Add prefix route.
20751da177e4SLinus Torvalds  */
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds static void
20781da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2079e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
20801da177e4SLinus Torvalds {
208186872cb5SThomas Graf 	struct fib6_config cfg = {
208286872cb5SThomas Graf 		.fc_table = RT6_TABLE_PREFIX,
208386872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
208486872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
208586872cb5SThomas Graf 		.fc_expires = expires,
208686872cb5SThomas Graf 		.fc_dst_len = plen,
208786872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
2088c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
2089f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
209086872cb5SThomas Graf 	};
20911da177e4SLinus Torvalds 
20924e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
20951da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
20961da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
20971da177e4SLinus Torvalds 	 */
209807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
20991da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
210086872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
21010be669bbSJoerg Roedel #endif
21021da177e4SLinus Torvalds 
210386872cb5SThomas Graf 	ip6_route_add(&cfg);
21041da177e4SLinus Torvalds }
21051da177e4SLinus Torvalds 
210614ef37b6SAndreas Hofmeister 
210714ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
210814ef37b6SAndreas Hofmeister 						  int plen,
210914ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
211014ef37b6SAndreas Hofmeister 						  u32 flags, u32 noflags)
211114ef37b6SAndreas Hofmeister {
211214ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
211314ef37b6SAndreas Hofmeister 	struct rt6_info *rt = NULL;
211414ef37b6SAndreas Hofmeister 	struct fib6_table *table;
211514ef37b6SAndreas Hofmeister 
211614ef37b6SAndreas Hofmeister 	table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
2117*63159f29SIan Morris 	if (!table)
211814ef37b6SAndreas Hofmeister 		return NULL;
211914ef37b6SAndreas Hofmeister 
21205744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
212114ef37b6SAndreas Hofmeister 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
212214ef37b6SAndreas Hofmeister 	if (!fn)
212314ef37b6SAndreas Hofmeister 		goto out;
212414ef37b6SAndreas Hofmeister 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2125d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != dev->ifindex)
212614ef37b6SAndreas Hofmeister 			continue;
212714ef37b6SAndreas Hofmeister 		if ((rt->rt6i_flags & flags) != flags)
212814ef37b6SAndreas Hofmeister 			continue;
212985da53bfSRomain Kuntz 		if ((rt->rt6i_flags & noflags) != 0)
213014ef37b6SAndreas Hofmeister 			continue;
213114ef37b6SAndreas Hofmeister 		dst_hold(&rt->dst);
213214ef37b6SAndreas Hofmeister 		break;
213314ef37b6SAndreas Hofmeister 	}
213414ef37b6SAndreas Hofmeister out:
21355744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
213614ef37b6SAndreas Hofmeister 	return rt;
213714ef37b6SAndreas Hofmeister }
213814ef37b6SAndreas Hofmeister 
213914ef37b6SAndreas Hofmeister 
21401da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
21431da177e4SLinus Torvalds {
214486872cb5SThomas Graf 	struct fib6_config cfg = {
214586872cb5SThomas Graf 		.fc_table = RT6_TABLE_LOCAL,
214686872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
214786872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
214886872cb5SThomas Graf 		.fc_dst_len = 8,
214986872cb5SThomas Graf 		.fc_flags = RTF_UP,
2150c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
215186872cb5SThomas Graf 	};
21521da177e4SLinus Torvalds 
215386872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
215486872cb5SThomas Graf 
215586872cb5SThomas Graf 	ip6_route_add(&cfg);
21561da177e4SLinus Torvalds }
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
21591da177e4SLinus Torvalds {
21601da177e4SLinus Torvalds 	struct inet6_dev *idev;
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 	ASSERT_RTNL();
21631da177e4SLinus Torvalds 
2164e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2165e21e8467SStephen Hemminger 	if (!idev)
216664e724f6SBrian Haley 		return ERR_PTR(-ENOBUFS);
216764e724f6SBrian Haley 
216864e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
216964e724f6SBrian Haley 		return ERR_PTR(-EACCES);
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds 	/* Add default multicast route */
21724af04abaSLi Wei 	if (!(dev->flags & IFF_LOOPBACK))
21731da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds 	return idev;
21761da177e4SLinus Torvalds }
21771da177e4SLinus Torvalds 
217853bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev,
217953bd6749SJiri Pirko 			     struct inet6_ifaddr *ifp,
218053bd6749SJiri Pirko 			     __u32 valid_lft, __u32 prefered_lft,
218153bd6749SJiri Pirko 			     bool create, unsigned long now)
218253bd6749SJiri Pirko {
218353bd6749SJiri Pirko 	u32 flags;
218453bd6749SJiri Pirko 	struct inet6_ifaddr *ift;
218553bd6749SJiri Pirko 
218653bd6749SJiri Pirko 	read_lock_bh(&idev->lock);
218753bd6749SJiri Pirko 	/* update all temporary addresses in the list */
218853bd6749SJiri Pirko 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
218953bd6749SJiri Pirko 		int age, max_valid, max_prefered;
219053bd6749SJiri Pirko 
219153bd6749SJiri Pirko 		if (ifp != ift->ifpub)
219253bd6749SJiri Pirko 			continue;
219353bd6749SJiri Pirko 
219453bd6749SJiri Pirko 		/* RFC 4941 section 3.3:
219553bd6749SJiri Pirko 		 * If a received option will extend the lifetime of a public
219653bd6749SJiri Pirko 		 * address, the lifetimes of temporary addresses should
219753bd6749SJiri Pirko 		 * be extended, subject to the overall constraint that no
219853bd6749SJiri Pirko 		 * temporary addresses should ever remain "valid" or "preferred"
219953bd6749SJiri Pirko 		 * for a time longer than (TEMP_VALID_LIFETIME) or
220053bd6749SJiri Pirko 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
220153bd6749SJiri Pirko 		 */
220253bd6749SJiri Pirko 		age = (now - ift->cstamp) / HZ;
220353bd6749SJiri Pirko 		max_valid = idev->cnf.temp_valid_lft - age;
220453bd6749SJiri Pirko 		if (max_valid < 0)
220553bd6749SJiri Pirko 			max_valid = 0;
220653bd6749SJiri Pirko 
220753bd6749SJiri Pirko 		max_prefered = idev->cnf.temp_prefered_lft -
220853bd6749SJiri Pirko 			       idev->cnf.max_desync_factor - age;
220953bd6749SJiri Pirko 		if (max_prefered < 0)
221053bd6749SJiri Pirko 			max_prefered = 0;
221153bd6749SJiri Pirko 
221253bd6749SJiri Pirko 		if (valid_lft > max_valid)
221353bd6749SJiri Pirko 			valid_lft = max_valid;
221453bd6749SJiri Pirko 
221553bd6749SJiri Pirko 		if (prefered_lft > max_prefered)
221653bd6749SJiri Pirko 			prefered_lft = max_prefered;
221753bd6749SJiri Pirko 
221853bd6749SJiri Pirko 		spin_lock(&ift->lock);
221953bd6749SJiri Pirko 		flags = ift->flags;
222053bd6749SJiri Pirko 		ift->valid_lft = valid_lft;
222153bd6749SJiri Pirko 		ift->prefered_lft = prefered_lft;
222253bd6749SJiri Pirko 		ift->tstamp = now;
222353bd6749SJiri Pirko 		if (prefered_lft > 0)
222453bd6749SJiri Pirko 			ift->flags &= ~IFA_F_DEPRECATED;
222553bd6749SJiri Pirko 
222653bd6749SJiri Pirko 		spin_unlock(&ift->lock);
222753bd6749SJiri Pirko 		if (!(flags&IFA_F_TENTATIVE))
222853bd6749SJiri Pirko 			ipv6_ifa_notify(0, ift);
222953bd6749SJiri Pirko 	}
223053bd6749SJiri Pirko 
223153bd6749SJiri Pirko 	if ((create || list_empty(&idev->tempaddr_list)) &&
223253bd6749SJiri Pirko 	    idev->cnf.use_tempaddr > 0) {
223353bd6749SJiri Pirko 		/* When a new public address is created as described
223453bd6749SJiri Pirko 		 * in [ADDRCONF], also create a new temporary address.
223553bd6749SJiri Pirko 		 * Also create a temporary address if it's enabled but
223653bd6749SJiri Pirko 		 * no temporary address currently exists.
223753bd6749SJiri Pirko 		 */
223853bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
223953bd6749SJiri Pirko 		ipv6_create_tempaddr(ifp, NULL);
224053bd6749SJiri Pirko 	} else {
224153bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
224253bd6749SJiri Pirko 	}
224353bd6749SJiri Pirko }
224453bd6749SJiri Pirko 
2245e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
22461da177e4SLinus Torvalds {
22471da177e4SLinus Torvalds 	struct prefix_info *pinfo;
22481da177e4SLinus Torvalds 	__u32 valid_lft;
22491da177e4SLinus Torvalds 	__u32 prefered_lft;
22501da177e4SLinus Torvalds 	int addr_type;
225164236f3fSHannes Frederic Sowa 	u32 addr_flags = 0;
22521da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
225356d417b1SBrian Haley 	struct net *net = dev_net(dev);
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
22561da177e4SLinus Torvalds 
22571da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
2258ba3542e1Sdingtianhong 		ADBG("addrconf: prefix option too short\n");
22591da177e4SLinus Torvalds 		return;
22601da177e4SLinus Torvalds 	}
22611da177e4SLinus Torvalds 
22621da177e4SLinus Torvalds 	/*
22631da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
22641da177e4SLinus Torvalds 	 */
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
22691da177e4SLinus Torvalds 		return;
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
22721da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2275e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
22761da177e4SLinus Torvalds 		return;
22771da177e4SLinus Torvalds 	}
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
22801da177e4SLinus Torvalds 
2281*63159f29SIan Morris 	if (!in6_dev) {
2282e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2283e87cc472SJoe Perches 				    dev->name);
22841da177e4SLinus Torvalds 		return;
22851da177e4SLinus Torvalds 	}
22861da177e4SLinus Torvalds 
22871da177e4SLinus Torvalds 	/*
22881da177e4SLinus Torvalds 	 *	Two things going on here:
22891da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
22901da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
22911da177e4SLinus Torvalds 	 */
22921da177e4SLinus Torvalds 
22934bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
22944bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
22954bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
22964bed72e4SYOSHIFUJI Hideaki 
22971da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
22986f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
22996f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
23006f704992SYOSHIFUJI Hideaki 		 * not good.
23011da177e4SLinus Torvalds 		 */
23024bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
23034bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
23044bed72e4SYOSHIFUJI Hideaki 		else
23054bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
23063dd4bc68SYOSHIFUJI Hideaki 
23074bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
23084bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
23091da177e4SLinus Torvalds 
231014ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
231114ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
231214ef37b6SAndreas Hofmeister 					       dev,
231314ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
231414ef37b6SAndreas Hofmeister 					       RTF_GATEWAY | RTF_DEFAULT);
23151da177e4SLinus Torvalds 
231614ef37b6SAndreas Hofmeister 		if (rt) {
23176f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
23181da177e4SLinus Torvalds 			if (valid_lft == 0) {
2319e0a1ad73SThomas Graf 				ip6_del_rt(rt);
23201da177e4SLinus Torvalds 				rt = NULL;
23214bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
23226f704992SYOSHIFUJI Hideaki 				/* not infinity */
23231716a961SGao feng 				rt6_set_expires(rt, jiffies + rt_expires);
23246f704992SYOSHIFUJI Hideaki 			} else {
23251716a961SGao feng 				rt6_clean_expires(rt);
23261da177e4SLinus Torvalds 			}
23271da177e4SLinus Torvalds 		} else if (valid_lft) {
23286f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
23294bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
23304bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
23316f704992SYOSHIFUJI Hideaki 				/* not infinity */
23326f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
23336f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
23346f704992SYOSHIFUJI Hideaki 			}
23351da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
23366f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
23371da177e4SLinus Torvalds 		}
233894e187c0SAmerigo Wang 		ip6_rt_put(rt);
23391da177e4SLinus Torvalds 	}
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
23441da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
23451da177e4SLinus Torvalds 		struct in6_addr addr;
23461da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
2347617fe29dSDaniel Borkmann 		bool tokenized = false;
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
23501da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2351f53adae4SDaniel Borkmann 
2352f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2353f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2354f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2355f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2356f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
2357617fe29dSDaniel Borkmann 				tokenized = true;
2358622c81d5SHannes Frederic Sowa 			} else if (in6_dev->addr_gen_mode ==
2359622c81d5SHannes Frederic Sowa 				   IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
2360622c81d5SHannes Frederic Sowa 				   !ipv6_generate_stable_address(&addr, 0,
2361622c81d5SHannes Frederic Sowa 								 in6_dev)) {
236264236f3fSHannes Frederic Sowa 				addr_flags |= IFA_F_STABLE_PRIVACY;
2363622c81d5SHannes Frederic Sowa 				goto ok;
2364f53adae4SDaniel Borkmann 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
23651da177e4SLinus Torvalds 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
23661da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
23671da177e4SLinus Torvalds 				return;
23681da177e4SLinus Torvalds 			}
23691da177e4SLinus Torvalds 			goto ok;
23701da177e4SLinus Torvalds 		}
2371e87cc472SJoe Perches 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
23721da177e4SLinus Torvalds 				    pinfo->prefix_len);
23731da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
23741da177e4SLinus Torvalds 		return;
23751da177e4SLinus Torvalds 
23761da177e4SLinus Torvalds ok:
23771da177e4SLinus Torvalds 
237853b7997fSYOSHIFUJI Hideaki 		ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
23791da177e4SLinus Torvalds 
2380*63159f29SIan Morris 		if (!ifp && valid_lft) {
23811da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
238295c385b4SNeil Horman 
238395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
238495c385b4SNeil Horman 			if (in6_dev->cnf.optimistic_dad &&
2385e6bff995SNeil Horman 			    !net->ipv6.devconf_all->forwarding && sllao)
238695c385b4SNeil Horman 				addr_flags = IFA_F_OPTIMISTIC;
238795c385b4SNeil Horman #endif
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
23901da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
23911da177e4SLinus Torvalds 			 */
23921da177e4SLinus Torvalds 			if (!max_addresses ||
23931da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
23943f8f5298SJiri Pirko 				ifp = ipv6_add_addr(in6_dev, &addr, NULL,
23953f8f5298SJiri Pirko 						    pinfo->prefix_len,
239695c385b4SNeil Horman 						    addr_type&IPV6_ADDR_SCOPE_MASK,
23978a226b2cSJiri Benc 						    addr_flags, valid_lft,
23988a226b2cSJiri Benc 						    prefered_lft);
23991da177e4SLinus Torvalds 
24003f0d2ba0SYOSHIFUJI Hideaki / 吉藤英明 			if (IS_ERR_OR_NULL(ifp)) {
24011da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
24021da177e4SLinus Torvalds 				return;
24031da177e4SLinus Torvalds 			}
24041da177e4SLinus Torvalds 
24058a226b2cSJiri Benc 			update_lft = 0;
24068a226b2cSJiri Benc 			create = 1;
2407c15b1ccaSHannes Frederic Sowa 			spin_lock_bh(&ifp->lock);
2408c15b1ccaSHannes Frederic Sowa 			ifp->flags |= IFA_F_MANAGETEMPADDR;
24091da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
2410617fe29dSDaniel Borkmann 			ifp->tokenized = tokenized;
2411c15b1ccaSHannes Frederic Sowa 			spin_unlock_bh(&ifp->lock);
2412cf22f9a2SDavid S. Miller 			addrconf_dad_start(ifp);
24131da177e4SLinus Torvalds 		}
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds 		if (ifp) {
2416479840ffSJiri Pirko 			u32 flags;
24171da177e4SLinus Torvalds 			unsigned long now;
24181da177e4SLinus Torvalds 			u32 stored_lft;
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
24218e8e676dSHannes Frederic Sowa 			spin_lock_bh(&ifp->lock);
24221da177e4SLinus Torvalds 			now = jiffies;
24231da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
24241da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
24251da177e4SLinus Torvalds 			else
24261da177e4SLinus Torvalds 				stored_lft = 0;
24278a226b2cSJiri Benc 			if (!update_lft && !create && stored_lft) {
24289451a304SFabian Frederick 				const u32 minimum_lft = min_t(u32,
24299451a304SFabian Frederick 					stored_lft, MIN_VALID_LIFETIME);
2430c9d55d5bSPaul Marks 				valid_lft = max(valid_lft, minimum_lft);
2431c9d55d5bSPaul Marks 
2432c9d55d5bSPaul Marks 				/* RFC4862 Section 5.5.3e:
2433c9d55d5bSPaul Marks 				 * "Note that the preferred lifetime of the
2434c9d55d5bSPaul Marks 				 *  corresponding address is always reset to
2435c9d55d5bSPaul Marks 				 *  the Preferred Lifetime in the received
2436c9d55d5bSPaul Marks 				 *  Prefix Information option, regardless of
2437c9d55d5bSPaul Marks 				 *  whether the valid lifetime is also reset or
2438a1ed0526SBrian Haley 				 *  ignored."
2439a1ed0526SBrian Haley 				 *
2440c9d55d5bSPaul Marks 				 * So we should always update prefered_lft here.
2441a1ed0526SBrian Haley 				 */
2442a1ed0526SBrian Haley 				update_lft = 1;
2443a1ed0526SBrian Haley 			}
24441da177e4SLinus Torvalds 
24451da177e4SLinus Torvalds 			if (update_lft) {
24461da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
24471da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
24481da177e4SLinus Torvalds 				ifp->tstamp = now;
24491da177e4SLinus Torvalds 				flags = ifp->flags;
24501da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
24518e8e676dSHannes Frederic Sowa 				spin_unlock_bh(&ifp->lock);
24521da177e4SLinus Torvalds 
24531da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
24541da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
24551da177e4SLinus Torvalds 			} else
24568e8e676dSHannes Frederic Sowa 				spin_unlock_bh(&ifp->lock);
24571da177e4SLinus Torvalds 
245853bd6749SJiri Pirko 			manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
245953bd6749SJiri Pirko 					 create, now);
24605d9efa7eSDavid S. Miller 
24611da177e4SLinus Torvalds 			in6_ifa_put(ifp);
2462c15b1ccaSHannes Frederic Sowa 			addrconf_verify();
24631da177e4SLinus Torvalds 		}
24641da177e4SLinus Torvalds 	}
24651da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
24661da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
24671da177e4SLinus Torvalds }
24681da177e4SLinus Torvalds 
24691da177e4SLinus Torvalds /*
24701da177e4SLinus Torvalds  *	Set destination address.
24711da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
24721da177e4SLinus Torvalds  *	device.
24731da177e4SLinus Torvalds  */
2474af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
24751da177e4SLinus Torvalds {
24761da177e4SLinus Torvalds 	struct in6_ifreq ireq;
24771da177e4SLinus Torvalds 	struct net_device *dev;
24781da177e4SLinus Torvalds 	int err = -EINVAL;
24791da177e4SLinus Torvalds 
24801da177e4SLinus Torvalds 	rtnl_lock();
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds 	err = -EFAULT;
24831da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
24841da177e4SLinus Torvalds 		goto err_exit;
24851da177e4SLinus Torvalds 
2486af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 	err = -ENODEV;
2489*63159f29SIan Morris 	if (!dev)
24901da177e4SLinus Torvalds 		goto err_exit;
24911da177e4SLinus Torvalds 
249207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
24931da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
24945bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
24951da177e4SLinus Torvalds 		struct ifreq ifr;
24961da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
24971da177e4SLinus Torvalds 
24981da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
24991da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
25001da177e4SLinus Torvalds 			goto err_exit;
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
25031da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
25041da177e4SLinus Torvalds 		p.iph.saddr = 0;
25051da177e4SLinus Torvalds 		p.iph.version = 4;
25061da177e4SLinus Torvalds 		p.iph.ihl = 5;
25071da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
25081da177e4SLinus Torvalds 		p.iph.ttl = 64;
2509d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
25101da177e4SLinus Torvalds 
25115bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
25125bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
25135bc3eb7eSStephen Hemminger 
25145bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
25155bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
25161da177e4SLinus Torvalds 			set_fs(oldfs);
25175bc3eb7eSStephen Hemminger 		} else
25185bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 		if (err == 0) {
25211da177e4SLinus Torvalds 			err = -ENOBUFS;
2522af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2523af284937SDaniel Lezcano 			if (!dev)
25241da177e4SLinus Torvalds 				goto err_exit;
25251da177e4SLinus Torvalds 			err = dev_open(dev);
25261da177e4SLinus Torvalds 		}
25271da177e4SLinus Torvalds 	}
25280be669bbSJoerg Roedel #endif
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds err_exit:
25311da177e4SLinus Torvalds 	rtnl_unlock();
25321da177e4SLinus Torvalds 	return err;
25331da177e4SLinus Torvalds }
25341da177e4SLinus Torvalds 
253593a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join,
253693a714d6SMadhu Challa 			  const struct in6_addr *addr, int ifindex)
253793a714d6SMadhu Challa {
253893a714d6SMadhu Challa 	int ret;
253993a714d6SMadhu Challa 
254093a714d6SMadhu Challa 	ASSERT_RTNL();
254193a714d6SMadhu Challa 
254293a714d6SMadhu Challa 	lock_sock(sk);
254393a714d6SMadhu Challa 	if (join)
254454ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_join(sk, ifindex, addr);
254593a714d6SMadhu Challa 	else
254654ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_drop(sk, ifindex, addr);
254793a714d6SMadhu Challa 	release_sock(sk);
254893a714d6SMadhu Challa 
254993a714d6SMadhu Challa 	return ret;
255093a714d6SMadhu Challa }
255193a714d6SMadhu Challa 
25521da177e4SLinus Torvalds /*
25531da177e4SLinus Torvalds  *	Manual configuration of address on an interface
25541da177e4SLinus Torvalds  */
2555479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex,
2556479840ffSJiri Pirko 			  const struct in6_addr *pfx,
2557caeaba79SNicolas Dichtel 			  const struct in6_addr *peer_pfx,
2558479840ffSJiri Pirko 			  unsigned int plen, __u32 ifa_flags,
2559479840ffSJiri Pirko 			  __u32 prefered_lft, __u32 valid_lft)
25601da177e4SLinus Torvalds {
25611da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
25621da177e4SLinus Torvalds 	struct inet6_dev *idev;
25631da177e4SLinus Torvalds 	struct net_device *dev;
256493a714d6SMadhu Challa 	unsigned long timeout;
256593a714d6SMadhu Challa 	clock_t expires;
25661da177e4SLinus Torvalds 	int scope;
25676f704992SYOSHIFUJI Hideaki 	u32 flags;
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds 	ASSERT_RTNL();
25701da177e4SLinus Torvalds 
257124ef0da7SThomas Graf 	if (plen > 128)
257224ef0da7SThomas Graf 		return -EINVAL;
257324ef0da7SThomas Graf 
25740778769dSNoriaki TAKAMIYA 	/* check the lifetime */
25750778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
25760778769dSNoriaki TAKAMIYA 		return -EINVAL;
25770778769dSNoriaki TAKAMIYA 
257853bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
257953bd6749SJiri Pirko 		return -EINVAL;
258053bd6749SJiri Pirko 
2581af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2582af284937SDaniel Lezcano 	if (!dev)
25831da177e4SLinus Torvalds 		return -ENODEV;
25841da177e4SLinus Torvalds 
258564e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
258664e724f6SBrian Haley 	if (IS_ERR(idev))
258764e724f6SBrian Haley 		return PTR_ERR(idev);
25881da177e4SLinus Torvalds 
258993a714d6SMadhu Challa 	if (ifa_flags & IFA_F_MCAUTOJOIN) {
259093a714d6SMadhu Challa 		int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
259193a714d6SMadhu Challa 					 true, pfx, ifindex);
259293a714d6SMadhu Challa 
259393a714d6SMadhu Challa 		if (ret < 0)
259493a714d6SMadhu Challa 			return ret;
259593a714d6SMadhu Challa 	}
259693a714d6SMadhu Challa 
25971da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
25981da177e4SLinus Torvalds 
25994bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
26004bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
26014bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
26024bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
26036f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
26044bed72e4SYOSHIFUJI Hideaki 	} else {
26054bed72e4SYOSHIFUJI Hideaki 		expires = 0;
26064bed72e4SYOSHIFUJI Hideaki 		flags = 0;
26074bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
26086f704992SYOSHIFUJI Hideaki 	}
26090778769dSNoriaki TAKAMIYA 
26104bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
26114bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
26124bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
26130778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
26144bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
26154bed72e4SYOSHIFUJI Hideaki 	}
26160778769dSNoriaki TAKAMIYA 
26178a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
26188a226b2cSJiri Benc 			    valid_lft, prefered_lft);
26190778769dSNoriaki TAKAMIYA 
26201da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
2621761aac73SThomas Haller 		if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
262246d48046SYOSHIFUJI Hideaki 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
26236f704992SYOSHIFUJI Hideaki 					      expires, flags);
2624761aac73SThomas Haller 		}
2625761aac73SThomas Haller 
262695c385b4SNeil Horman 		/*
262795c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
262895c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
262995c385b4SNeil Horman 		 * manually configured addresses
263095c385b4SNeil Horman 		 */
2631cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
263253bd6749SJiri Pirko 		if (ifa_flags & IFA_F_MANAGETEMPADDR)
263353bd6749SJiri Pirko 			manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
263453bd6749SJiri Pirko 					 true, jiffies);
26351da177e4SLinus Torvalds 		in6_ifa_put(ifp);
2636c15b1ccaSHannes Frederic Sowa 		addrconf_verify_rtnl();
26371da177e4SLinus Torvalds 		return 0;
263893a714d6SMadhu Challa 	} else if (ifa_flags & IFA_F_MCAUTOJOIN) {
263993a714d6SMadhu Challa 		ipv6_mc_config(net->ipv6.mc_autojoin_sk,
264093a714d6SMadhu Challa 			       false, pfx, ifindex);
26411da177e4SLinus Torvalds 	}
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds 	return PTR_ERR(ifp);
26441da177e4SLinus Torvalds }
26451da177e4SLinus Torvalds 
26466046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
26476046d5b4SHeiner Kallweit 			  const struct in6_addr *pfx, unsigned int plen)
26481da177e4SLinus Torvalds {
26491da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
26501da177e4SLinus Torvalds 	struct inet6_dev *idev;
26511da177e4SLinus Torvalds 	struct net_device *dev;
26521da177e4SLinus Torvalds 
265324ef0da7SThomas Graf 	if (plen > 128)
265424ef0da7SThomas Graf 		return -EINVAL;
265524ef0da7SThomas Graf 
2656af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2657af284937SDaniel Lezcano 	if (!dev)
26581da177e4SLinus Torvalds 		return -ENODEV;
26591da177e4SLinus Torvalds 
2660e5d08d71SIan Morris 	idev = __in6_dev_get(dev);
2661*63159f29SIan Morris 	if (!idev)
26621da177e4SLinus Torvalds 		return -ENXIO;
26631da177e4SLinus Torvalds 
26641da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2665502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
26661da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
26671da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
26681da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
26691da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
26701da177e4SLinus Torvalds 
26716046d5b4SHeiner Kallweit 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
26726046d5b4SHeiner Kallweit 			    (ifa_flags & IFA_F_MANAGETEMPADDR))
26736046d5b4SHeiner Kallweit 				manage_tempaddrs(idev, ifp, 0, 0, false,
26746046d5b4SHeiner Kallweit 						 jiffies);
26751da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
26766046d5b4SHeiner Kallweit 			addrconf_verify_rtnl();
267793a714d6SMadhu Challa 			if (ipv6_addr_is_multicast(pfx)) {
267893a714d6SMadhu Challa 				ipv6_mc_config(net->ipv6.mc_autojoin_sk,
267993a714d6SMadhu Challa 					       false, pfx, dev->ifindex);
268093a714d6SMadhu Challa 			}
26811da177e4SLinus Torvalds 			return 0;
26821da177e4SLinus Torvalds 		}
26831da177e4SLinus Torvalds 	}
26841da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
26851da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
26861da177e4SLinus Torvalds }
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds 
2689af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
26901da177e4SLinus Torvalds {
26911da177e4SLinus Torvalds 	struct in6_ifreq ireq;
26921da177e4SLinus Torvalds 	int err;
26931da177e4SLinus Torvalds 
2694af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
26951da177e4SLinus Torvalds 		return -EPERM;
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
26981da177e4SLinus Torvalds 		return -EFAULT;
26991da177e4SLinus Torvalds 
27001da177e4SLinus Torvalds 	rtnl_lock();
2701caeaba79SNicolas Dichtel 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2702af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2703af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
27041da177e4SLinus Torvalds 	rtnl_unlock();
27051da177e4SLinus Torvalds 	return err;
27061da177e4SLinus Torvalds }
27071da177e4SLinus Torvalds 
2708af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
27091da177e4SLinus Torvalds {
27101da177e4SLinus Torvalds 	struct in6_ifreq ireq;
27111da177e4SLinus Torvalds 	int err;
27121da177e4SLinus Torvalds 
2713af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
27141da177e4SLinus Torvalds 		return -EPERM;
27151da177e4SLinus Torvalds 
27161da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
27171da177e4SLinus Torvalds 		return -EFAULT;
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds 	rtnl_lock();
27206046d5b4SHeiner Kallweit 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2721af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
27221da177e4SLinus Torvalds 	rtnl_unlock();
27231da177e4SLinus Torvalds 	return err;
27241da177e4SLinus Torvalds }
27251da177e4SLinus Torvalds 
2726b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2727b5f348e5SIlpo Järvinen 		     int plen, int scope)
2728b5f348e5SIlpo Järvinen {
2729b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
2730b5f348e5SIlpo Järvinen 
27318a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, addr, NULL, plen,
273207edd741SHannes Frederic Sowa 			    scope, IFA_F_PERMANENT,
273307edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2734b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
2735b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
2736b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
2737b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
2738b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2739b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
2740b5f348e5SIlpo Järvinen 	}
2741b5f348e5SIlpo Järvinen }
2742b5f348e5SIlpo Järvinen 
274307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
27441da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
27451da177e4SLinus Torvalds {
27461da177e4SLinus Torvalds 	struct in6_addr addr;
27471da177e4SLinus Torvalds 	struct net_device *dev;
2748c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
2749929c9cf3SNicolas Dichtel 	int scope, plen;
2750f0e2acfaSNicolas Dichtel 	u32 pflags = 0;
27511da177e4SLinus Torvalds 
27521da177e4SLinus Torvalds 	ASSERT_RTNL();
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
27551da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
27561da177e4SLinus Torvalds 
27571da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
27581da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
27591da177e4SLinus Torvalds 		scope = IFA_LINK;
2760929c9cf3SNicolas Dichtel 		plen = 64;
27611da177e4SLinus Torvalds 	} else {
27621da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
2763929c9cf3SNicolas Dichtel 		plen = 96;
2764f0e2acfaSNicolas Dichtel 		pflags |= RTF_NONEXTHOP;
27651da177e4SLinus Torvalds 	}
27661da177e4SLinus Torvalds 
27671da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
2768929c9cf3SNicolas Dichtel 		add_addr(idev, &addr, plen, scope);
2769f0e2acfaSNicolas Dichtel 		addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
27701da177e4SLinus Torvalds 		return;
27711da177e4SLinus Torvalds 	}
27721da177e4SLinus Torvalds 
27736fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
2774e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
27751da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
27761da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds 			int flag = scope;
27791da177e4SLinus Torvalds 
27801da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
27831da177e4SLinus Torvalds 
27841da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
27851da177e4SLinus Torvalds 					continue;
27861da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
27871da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
27881da177e4SLinus Torvalds 						continue;
27891da177e4SLinus Torvalds 					flag |= IFA_HOST;
27901da177e4SLinus Torvalds 				}
27911da177e4SLinus Torvalds 
2792b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
2793f0e2acfaSNicolas Dichtel 				addrconf_prefix_route(&addr, plen, idev->dev, 0,
2794f0e2acfaSNicolas Dichtel 						      pflags);
27951da177e4SLinus Torvalds 			}
27961da177e4SLinus Torvalds 		}
27971da177e4SLinus Torvalds 	}
27981da177e4SLinus Torvalds }
27990be669bbSJoerg Roedel #endif
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
28021da177e4SLinus Torvalds {
28031da177e4SLinus Torvalds 	struct inet6_dev  *idev;
280425fb6ca4SBalakumaran Kannan 	struct net_device *sp_dev;
280525fb6ca4SBalakumaran Kannan 	struct inet6_ifaddr *sp_ifa;
280625fb6ca4SBalakumaran Kannan 	struct rt6_info *sp_rt;
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds 	/* ::1 */
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds 	ASSERT_RTNL();
28111da177e4SLinus Torvalds 
2812e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
2813*63159f29SIan Morris 	if (!idev) {
281491df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
28151da177e4SLinus Torvalds 		return;
28161da177e4SLinus Torvalds 	}
28171da177e4SLinus Torvalds 
2818b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
281925fb6ca4SBalakumaran Kannan 
282025fb6ca4SBalakumaran Kannan 	/* Add routes to other interface's IPv6 addresses */
282125fb6ca4SBalakumaran Kannan 	for_each_netdev(dev_net(dev), sp_dev) {
282225fb6ca4SBalakumaran Kannan 		if (!strcmp(sp_dev->name, dev->name))
282325fb6ca4SBalakumaran Kannan 			continue;
282425fb6ca4SBalakumaran Kannan 
282525fb6ca4SBalakumaran Kannan 		idev = __in6_dev_get(sp_dev);
282625fb6ca4SBalakumaran Kannan 		if (!idev)
282725fb6ca4SBalakumaran Kannan 			continue;
282825fb6ca4SBalakumaran Kannan 
282925fb6ca4SBalakumaran Kannan 		read_lock_bh(&idev->lock);
283025fb6ca4SBalakumaran Kannan 		list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
283125fb6ca4SBalakumaran Kannan 
283225fb6ca4SBalakumaran Kannan 			if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
283325fb6ca4SBalakumaran Kannan 				continue;
283425fb6ca4SBalakumaran Kannan 
283533d99113SGao feng 			if (sp_ifa->rt) {
283633d99113SGao feng 				/* This dst has been added to garbage list when
283733d99113SGao feng 				 * lo device down, release this obsolete dst and
283833d99113SGao feng 				 * reallocate a new router for ifa.
283933d99113SGao feng 				 */
284033d99113SGao feng 				if (sp_ifa->rt->dst.obsolete > 0) {
284133d99113SGao feng 					ip6_rt_put(sp_ifa->rt);
284233d99113SGao feng 					sp_ifa->rt = NULL;
284333d99113SGao feng 				} else {
2844a881ae1fSGao feng 					continue;
284533d99113SGao feng 				}
284633d99113SGao feng 			}
2847a881ae1fSGao feng 
284857ec0afeSFrançois-Xavier Le Bail 			sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
284925fb6ca4SBalakumaran Kannan 
285025fb6ca4SBalakumaran Kannan 			/* Failure cases are ignored */
2851534c8779SGao feng 			if (!IS_ERR(sp_rt)) {
2852534c8779SGao feng 				sp_ifa->rt = sp_rt;
285325fb6ca4SBalakumaran Kannan 				ip6_ins_rt(sp_rt);
285425fb6ca4SBalakumaran Kannan 			}
2855534c8779SGao feng 		}
285625fb6ca4SBalakumaran Kannan 		read_unlock_bh(&idev->lock);
285725fb6ca4SBalakumaran Kannan 	}
28581da177e4SLinus Torvalds }
28591da177e4SLinus Torvalds 
286064236f3fSHannes Frederic Sowa static void addrconf_add_linklocal(struct inet6_dev *idev,
286164236f3fSHannes Frederic Sowa 				   const struct in6_addr *addr, u32 flags)
28621da177e4SLinus Torvalds {
28631da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
286464236f3fSHannes Frederic Sowa 	u32 addr_flags = flags | IFA_F_PERMANENT;
28651da177e4SLinus Torvalds 
286695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
286795c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
2868702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
286995c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
287095c385b4SNeil Horman #endif
287195c385b4SNeil Horman 
287207edd741SHannes Frederic Sowa 	ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
287307edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
28741da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
287546d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2876cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
28771da177e4SLinus Torvalds 		in6_ifa_put(ifp);
28781da177e4SLinus Torvalds 	}
28791da177e4SLinus Torvalds }
28801da177e4SLinus Torvalds 
2881622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address)
2882622c81d5SHannes Frederic Sowa {
2883622c81d5SHannes Frederic Sowa 	if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
2884622c81d5SHannes Frederic Sowa 		return true;
2885622c81d5SHannes Frederic Sowa 
2886622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0x02005eff) &&
2887622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
2888622c81d5SHannes Frederic Sowa 		return true;
2889622c81d5SHannes Frederic Sowa 
2890622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0xfdffffff) &&
2891622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
2892622c81d5SHannes Frederic Sowa 		return true;
2893622c81d5SHannes Frederic Sowa 
2894622c81d5SHannes Frederic Sowa 	return false;
2895622c81d5SHannes Frederic Sowa }
2896622c81d5SHannes Frederic Sowa 
2897622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address,
2898622c81d5SHannes Frederic Sowa 					u8 dad_count,
2899622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev)
2900622c81d5SHannes Frederic Sowa {
2901622c81d5SHannes Frederic Sowa 	static DEFINE_SPINLOCK(lock);
2902622c81d5SHannes Frederic Sowa 	static __u32 digest[SHA_DIGEST_WORDS];
2903622c81d5SHannes Frederic Sowa 	static __u32 workspace[SHA_WORKSPACE_WORDS];
2904622c81d5SHannes Frederic Sowa 
2905622c81d5SHannes Frederic Sowa 	static union {
2906622c81d5SHannes Frederic Sowa 		char __data[SHA_MESSAGE_BYTES];
2907622c81d5SHannes Frederic Sowa 		struct {
2908622c81d5SHannes Frederic Sowa 			struct in6_addr secret;
2909ff40217eSHannes Frederic Sowa 			__be32 prefix[2];
2910622c81d5SHannes Frederic Sowa 			unsigned char hwaddr[MAX_ADDR_LEN];
2911622c81d5SHannes Frederic Sowa 			u8 dad_count;
2912622c81d5SHannes Frederic Sowa 		} __packed;
2913622c81d5SHannes Frederic Sowa 	} data;
2914622c81d5SHannes Frederic Sowa 
2915622c81d5SHannes Frederic Sowa 	struct in6_addr secret;
2916622c81d5SHannes Frederic Sowa 	struct in6_addr temp;
2917622c81d5SHannes Frederic Sowa 	struct net *net = dev_net(idev->dev);
2918622c81d5SHannes Frederic Sowa 
2919622c81d5SHannes Frederic Sowa 	BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
2920622c81d5SHannes Frederic Sowa 
2921622c81d5SHannes Frederic Sowa 	if (idev->cnf.stable_secret.initialized)
2922622c81d5SHannes Frederic Sowa 		secret = idev->cnf.stable_secret.secret;
2923622c81d5SHannes Frederic Sowa 	else if (net->ipv6.devconf_dflt->stable_secret.initialized)
2924622c81d5SHannes Frederic Sowa 		secret = net->ipv6.devconf_dflt->stable_secret.secret;
2925622c81d5SHannes Frederic Sowa 	else
2926622c81d5SHannes Frederic Sowa 		return -1;
2927622c81d5SHannes Frederic Sowa 
2928622c81d5SHannes Frederic Sowa retry:
2929622c81d5SHannes Frederic Sowa 	spin_lock_bh(&lock);
2930622c81d5SHannes Frederic Sowa 
2931622c81d5SHannes Frederic Sowa 	sha_init(digest);
2932622c81d5SHannes Frederic Sowa 	memset(&data, 0, sizeof(data));
2933622c81d5SHannes Frederic Sowa 	memset(workspace, 0, sizeof(workspace));
2934622c81d5SHannes Frederic Sowa 	memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
2935ff40217eSHannes Frederic Sowa 	data.prefix[0] = address->s6_addr32[0];
2936ff40217eSHannes Frederic Sowa 	data.prefix[1] = address->s6_addr32[1];
2937622c81d5SHannes Frederic Sowa 	data.secret = secret;
2938622c81d5SHannes Frederic Sowa 	data.dad_count = dad_count;
2939622c81d5SHannes Frederic Sowa 
2940622c81d5SHannes Frederic Sowa 	sha_transform(digest, data.__data, workspace);
2941622c81d5SHannes Frederic Sowa 
2942622c81d5SHannes Frederic Sowa 	temp = *address;
2943ff40217eSHannes Frederic Sowa 	temp.s6_addr32[2] = (__force __be32)digest[0];
2944ff40217eSHannes Frederic Sowa 	temp.s6_addr32[3] = (__force __be32)digest[1];
2945622c81d5SHannes Frederic Sowa 
2946622c81d5SHannes Frederic Sowa 	spin_unlock_bh(&lock);
2947622c81d5SHannes Frederic Sowa 
2948622c81d5SHannes Frederic Sowa 	if (ipv6_reserved_interfaceid(temp)) {
2949622c81d5SHannes Frederic Sowa 		dad_count++;
29501855b7c3SHannes Frederic Sowa 		if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
2951622c81d5SHannes Frederic Sowa 			return -1;
2952622c81d5SHannes Frederic Sowa 		goto retry;
2953622c81d5SHannes Frederic Sowa 	}
2954622c81d5SHannes Frederic Sowa 
2955622c81d5SHannes Frederic Sowa 	*address = temp;
2956622c81d5SHannes Frederic Sowa 	return 0;
2957622c81d5SHannes Frederic Sowa }
2958622c81d5SHannes Frederic Sowa 
2959bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
2960bc91b0f0SJiri Pirko {
2961bc91b0f0SJiri Pirko 	struct in6_addr addr;
2962bc91b0f0SJiri Pirko 
2963bc91b0f0SJiri Pirko 	ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2964622c81d5SHannes Frederic Sowa 
2965622c81d5SHannes Frederic Sowa 	if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY) {
2966622c81d5SHannes Frederic Sowa 		if (!ipv6_generate_stable_address(&addr, 0, idev))
296764236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr,
296864236f3fSHannes Frederic Sowa 					       IFA_F_STABLE_PRIVACY);
2969622c81d5SHannes Frederic Sowa 		else if (prefix_route)
2970622c81d5SHannes Frederic Sowa 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
2971622c81d5SHannes Frederic Sowa 	} else if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) {
2972bc91b0f0SJiri Pirko 		/* addrconf_add_linklocal also adds a prefix_route and we
2973bc91b0f0SJiri Pirko 		 * only need to care about prefix routes if ipv6_generate_eui64
2974bc91b0f0SJiri Pirko 		 * couldn't generate one.
2975bc91b0f0SJiri Pirko 		 */
2976bc91b0f0SJiri Pirko 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
297764236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr, 0);
2978bc91b0f0SJiri Pirko 		else if (prefix_route)
2979bc91b0f0SJiri Pirko 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
2980bc91b0f0SJiri Pirko 	}
2981bc91b0f0SJiri Pirko }
2982bc91b0f0SJiri Pirko 
29831da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
29841da177e4SLinus Torvalds {
29851da177e4SLinus Torvalds 	struct inet6_dev *idev;
29861da177e4SLinus Torvalds 
29871da177e4SLinus Torvalds 	ASSERT_RTNL();
29881da177e4SLinus Torvalds 
298974235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
299074235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
299174235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
299206a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
2993cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	    (dev->type != ARPHRD_IEEE802154) &&
2994e837735eSNicolas Dichtel 	    (dev->type != ARPHRD_IEEE1394) &&
2995e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_TUNNEL6) &&
2996e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_6LOWPAN)) {
299774235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
299874235a25SHerbert Xu 		return;
299974235a25SHerbert Xu 	}
300074235a25SHerbert Xu 
30011da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
300264e724f6SBrian Haley 	if (IS_ERR(idev))
30031da177e4SLinus Torvalds 		return;
30041da177e4SLinus Torvalds 
3005bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, false);
30061da177e4SLinus Torvalds }
30071da177e4SLinus Torvalds 
300807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
30091da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
30101da177e4SLinus Torvalds {
30111da177e4SLinus Torvalds 	struct inet6_dev *idev;
30121da177e4SLinus Torvalds 
30131da177e4SLinus Torvalds 	ASSERT_RTNL();
30141da177e4SLinus Torvalds 
30151da177e4SLinus Torvalds 	/*
30161da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
30171da177e4SLinus Torvalds 	 * addresses... we should configure all of
30181da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
30191da177e4SLinus Torvalds 	 */
30201da177e4SLinus Torvalds 
3021e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
3022*63159f29SIan Morris 	if (!idev) {
302391df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
30241da177e4SLinus Torvalds 		return;
30251da177e4SLinus Torvalds 	}
30261da177e4SLinus Torvalds 
3027c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
3028bc91b0f0SJiri Pirko 		addrconf_addr_gen(idev, false);
3029c7dc89c0SFred L. Templin 		return;
3030c7dc89c0SFred L. Templin 	}
3031c7dc89c0SFred L. Templin 
30321da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
30331da177e4SLinus Torvalds 
303462b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
30351da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
30361da177e4SLinus Torvalds }
30370be669bbSJoerg Roedel #endif
30381da177e4SLinus Torvalds 
303907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3040aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
3041aee80b54Sstephen hemminger {
3042aee80b54Sstephen hemminger 	struct inet6_dev *idev;
3043aee80b54Sstephen hemminger 
3044aee80b54Sstephen hemminger 	ASSERT_RTNL();
3045aee80b54Sstephen hemminger 
3046e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
3047*63159f29SIan Morris 	if (!idev) {
304891df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
3049aee80b54Sstephen hemminger 		return;
3050aee80b54Sstephen hemminger 	}
3051aee80b54Sstephen hemminger 
3052bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, true);
3053aee80b54Sstephen hemminger }
3054aee80b54Sstephen hemminger #endif
3055aee80b54Sstephen hemminger 
30561da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
3057351638e7SJiri Pirko 			   void *ptr)
30581da177e4SLinus Torvalds {
3059351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3060748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
3061c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
3062b217d616SHerbert Xu 	int err;
30631da177e4SLinus Torvalds 
30641da177e4SLinus Torvalds 	switch (event) {
306545ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
306674235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
306745ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
3068a317a2f1SWANG Cong 			if (IS_ERR(idev))
3069a317a2f1SWANG Cong 				return notifier_from_errno(PTR_ERR(idev));
307045ba9dd2SYOSHIFUJI Hideaki 		}
307145ba9dd2SYOSHIFUJI Hideaki 		break;
3072bcdd553fSStephen Hemminger 
30731da177e4SLinus Torvalds 	case NETDEV_UP:
30743c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
3075c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
3076c2edacf8SJay Vosburgh 			break;
3077c2edacf8SJay Vosburgh 
30783ce62a84SWANG Cong 		if (idev && idev->cnf.disable_ipv6)
30793ce62a84SWANG Cong 			break;
30803ce62a84SWANG Cong 
30813c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
3082f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
30833c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
3084f3213831SJoe Perches 				pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
30853c21edbdSYOSHIFUJI Hideaki 					dev->name);
30863c21edbdSYOSHIFUJI Hideaki 				break;
30873c21edbdSYOSHIFUJI Hideaki 			}
308899081049SKristian Slavov 
3089d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
3090d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
3091d31c7b8fSEvgeniy Polyakov 
3092a317a2f1SWANG Cong 			if (!IS_ERR_OR_NULL(idev)) {
309399081049SKristian Slavov 				idev->if_flags |= IF_READY;
3094e3ec6cfcSBenjamin Thery 				run_pending = 1;
3095e3ec6cfcSBenjamin Thery 			}
30963c21edbdSYOSHIFUJI Hideaki 		} else {
3097f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
30983c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
30993c21edbdSYOSHIFUJI Hideaki 				break;
31003c21edbdSYOSHIFUJI Hideaki 			}
31013c21edbdSYOSHIFUJI Hideaki 
31023c21edbdSYOSHIFUJI Hideaki 			if (idev) {
3103bcdd553fSStephen Hemminger 				if (idev->if_flags & IF_READY)
31043c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
31053c21edbdSYOSHIFUJI Hideaki 					break;
31063c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
31073c21edbdSYOSHIFUJI Hideaki 			}
31083c21edbdSYOSHIFUJI Hideaki 
3109f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
31103c21edbdSYOSHIFUJI Hideaki 				dev->name);
31113c21edbdSYOSHIFUJI Hideaki 
3112c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
31133c21edbdSYOSHIFUJI Hideaki 		}
31143c21edbdSYOSHIFUJI Hideaki 
31151da177e4SLinus Torvalds 		switch (dev->type) {
311607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
31171da177e4SLinus Torvalds 		case ARPHRD_SIT:
31181da177e4SLinus Torvalds 			addrconf_sit_config(dev);
31191da177e4SLinus Torvalds 			break;
31200be669bbSJoerg Roedel #endif
312107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3122aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
3123aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
3124aee80b54Sstephen hemminger 			break;
3125aee80b54Sstephen hemminger #endif
31261da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
31271da177e4SLinus Torvalds 			init_loopback(dev);
31281da177e4SLinus Torvalds 			break;
31291da177e4SLinus Torvalds 
31301da177e4SLinus Torvalds 		default:
31311da177e4SLinus Torvalds 			addrconf_dev_config(dev);
31321da177e4SLinus Torvalds 			break;
31333ff50b79SStephen Hemminger 		}
3134bcdd553fSStephen Hemminger 
3135a317a2f1SWANG Cong 		if (!IS_ERR_OR_NULL(idev)) {
3136c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
3137c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
3138c5e33bddSYOSHIFUJI Hideaki 
3139bcdd553fSStephen Hemminger 			/*
3140bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
3141bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
3142bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
31431da177e4SLinus Torvalds 			 */
3144bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
3145bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
31461da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
31471da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
31481da177e4SLinus Torvalds 			}
31491da177e4SLinus Torvalds 			idev->tstamp = jiffies;
31501da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
3151bcdd553fSStephen Hemminger 
3152bcdd553fSStephen Hemminger 			/*
3153bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
3154bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
31551da177e4SLinus Torvalds 			 */
31561da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
3157bcdd553fSStephen Hemminger 				addrconf_ifdown(dev, 1);
31581da177e4SLinus Torvalds 		}
31591da177e4SLinus Torvalds 		break;
31601da177e4SLinus Torvalds 
31611da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
31621da177e4SLinus Torvalds 		if (idev && dev->mtu >= IPV6_MIN_MTU) {
31631da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
31641da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
31651da177e4SLinus Torvalds 			break;
31661da177e4SLinus Torvalds 		}
31671da177e4SLinus Torvalds 
3168d31c7b8fSEvgeniy Polyakov 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3169d31c7b8fSEvgeniy Polyakov 			idev = ipv6_add_dev(dev);
3170a317a2f1SWANG Cong 			if (!IS_ERR(idev))
3171d31c7b8fSEvgeniy Polyakov 				break;
3172d31c7b8fSEvgeniy Polyakov 		}
3173d31c7b8fSEvgeniy Polyakov 
3174bcdd553fSStephen Hemminger 		/*
3175db9c7c39Sstephen hemminger 		 * if MTU under IPV6_MIN_MTU.
3176bcdd553fSStephen Hemminger 		 * Stop IPv6 on this interface.
3177bcdd553fSStephen Hemminger 		 */
31781da177e4SLinus Torvalds 
31791da177e4SLinus Torvalds 	case NETDEV_DOWN:
31801da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
31811da177e4SLinus Torvalds 		/*
31821da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
31831da177e4SLinus Torvalds 		 */
31841da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
31851da177e4SLinus Torvalds 		break;
31863c21edbdSYOSHIFUJI Hideaki 
31871da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
31881da177e4SLinus Torvalds 		if (idev) {
31895632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
3190408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
3191a317a2f1SWANG Cong 			err = addrconf_sysctl_register(idev);
3192b217d616SHerbert Xu 			if (err)
3193b217d616SHerbert Xu 				return notifier_from_errno(err);
3194a317a2f1SWANG Cong 			err = snmp6_register_dev(idev);
3195a317a2f1SWANG Cong 			if (err) {
3196a317a2f1SWANG Cong 				addrconf_sysctl_unregister(idev);
3197a317a2f1SWANG Cong 				return notifier_from_errno(err);
3198a317a2f1SWANG Cong 			}
31995632c515SStephen Hemminger 		}
32001da177e4SLinus Torvalds 		break;
3201bcdd553fSStephen Hemminger 
320293d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
320393d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
320493d9b7d7SJiri Pirko 		addrconf_type_change(dev, event);
320575c78500SMoni Shoua 		break;
32063ff50b79SStephen Hemminger 	}
32071da177e4SLinus Torvalds 
32081da177e4SLinus Torvalds 	return NOTIFY_OK;
32091da177e4SLinus Torvalds }
32101da177e4SLinus Torvalds 
32111da177e4SLinus Torvalds /*
32121da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
32131da177e4SLinus Torvalds  */
32141da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
32151da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
32161da177e4SLinus Torvalds };
32171da177e4SLinus Torvalds 
321893d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
321975c78500SMoni Shoua {
322075c78500SMoni Shoua 	struct inet6_dev *idev;
322175c78500SMoni Shoua 	ASSERT_RTNL();
322275c78500SMoni Shoua 
322375c78500SMoni Shoua 	idev = __in6_dev_get(dev);
322475c78500SMoni Shoua 
322593d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
322675c78500SMoni Shoua 		ipv6_mc_remap(idev);
322793d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
322875c78500SMoni Shoua 		ipv6_mc_unmap(idev);
322975c78500SMoni Shoua }
323075c78500SMoni Shoua 
32311da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
32321da177e4SLinus Torvalds {
3233c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
3234502a2ffdSstephen hemminger 	struct inet6_dev *idev;
3235502a2ffdSstephen hemminger 	struct inet6_ifaddr *ifa;
323673a8bd74SDavid S. Miller 	int state, i;
32371da177e4SLinus Torvalds 
32381da177e4SLinus Torvalds 	ASSERT_RTNL();
32391da177e4SLinus Torvalds 
3240f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
324173a8bd74SDavid S. Miller 	neigh_ifdown(&nd_tbl, dev);
32421da177e4SLinus Torvalds 
32431da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
3244*63159f29SIan Morris 	if (!idev)
32451da177e4SLinus Torvalds 		return -ENODEV;
32461da177e4SLinus Torvalds 
3247bcdd553fSStephen Hemminger 	/*
3248bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3249bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
32501da177e4SLinus Torvalds 	 */
3251439e2385SDenis V. Lunev 	if (how) {
32521da177e4SLinus Torvalds 		idev->dead = 1;
32538814c4b5SYOSHIFUJI Hideaki 
32548814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3255a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
32561da177e4SLinus Torvalds 
32571da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
32581da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
32591da177e4SLinus Torvalds 
32601da177e4SLinus Torvalds 	}
32611da177e4SLinus Torvalds 
326273a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
326373a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
326473a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
326573a8bd74SDavid S. Miller 
326673a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
326773a8bd74SDavid S. Miller restart:
3268b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
326973a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
327073a8bd74SDavid S. Miller 				hlist_del_init_rcu(&ifa->addr_lst);
3271c15b1ccaSHannes Frederic Sowa 				addrconf_del_dad_work(ifa);
327273a8bd74SDavid S. Miller 				goto restart;
327373a8bd74SDavid S. Miller 			}
327473a8bd74SDavid S. Miller 		}
327573a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
327673a8bd74SDavid S. Miller 	}
327773a8bd74SDavid S. Miller 
32781da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
32791da177e4SLinus Torvalds 
3280b7b1bfceSHannes Frederic Sowa 	addrconf_del_rs_timer(idev);
3281b7b1bfceSHannes Frederic Sowa 
3282bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3283439e2385SDenis V. Lunev 	if (!how)
32843c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
32851da177e4SLinus Torvalds 
3286439e2385SDenis V. Lunev 	if (how && del_timer(&idev->regen_timer))
32871da177e4SLinus Torvalds 		in6_dev_put(idev);
32881da177e4SLinus Torvalds 
3289bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3290372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3291372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3292372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3293372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
32941da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
32951da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds 		if (ifa->ifpub) {
32981da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
32991da177e4SLinus Torvalds 			ifa->ifpub = NULL;
33001da177e4SLinus Torvalds 		}
33011da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
33021da177e4SLinus Torvalds 		in6_ifa_put(ifa);
33031da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
33041da177e4SLinus Torvalds 	}
33058f37ada5Sstephen hemminger 
3306502a2ffdSstephen hemminger 	while (!list_empty(&idev->addr_list)) {
3307502a2ffdSstephen hemminger 		ifa = list_first_entry(&idev->addr_list,
3308502a2ffdSstephen hemminger 				       struct inet6_ifaddr, if_list);
3309c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifa);
331084e8b803Sstephen hemminger 
3311502a2ffdSstephen hemminger 		list_del(&ifa->if_list);
33121da177e4SLinus Torvalds 
33134c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
331473a8bd74SDavid S. Miller 
33158e8e676dSHannes Frederic Sowa 		spin_lock_bh(&ifa->lock);
33164c5ff6a6SHerbert Xu 		state = ifa->state;
33174c5ff6a6SHerbert Xu 		ifa->state = INET6_IFADDR_STATE_DEAD;
33188e8e676dSHannes Frederic Sowa 		spin_unlock_bh(&ifa->lock);
33194c5ff6a6SHerbert Xu 
33209d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
33211da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
3322f88c91ddSCong Wang 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
33232de79570SLorenzo Colitti 		}
33249d82ca98SJohn Fastabend 		in6_ifa_put(ifa);
332573a8bd74SDavid S. Miller 
33261da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
33271da177e4SLinus Torvalds 	}
33288f37ada5Sstephen hemminger 
33291da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
33301da177e4SLinus Torvalds 
3331381f4dcaSSabrina Dubroca 	/* Step 5: Discard anycast and multicast list */
3332381f4dcaSSabrina Dubroca 	if (how) {
3333381f4dcaSSabrina Dubroca 		ipv6_ac_destroy_dev(idev);
33341da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
3335381f4dcaSSabrina Dubroca 	} else {
33361da177e4SLinus Torvalds 		ipv6_mc_down(idev);
3337381f4dcaSSabrina Dubroca 	}
33381da177e4SLinus Torvalds 
33391da177e4SLinus Torvalds 	idev->tstamp = jiffies;
33401da177e4SLinus Torvalds 
3341bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3342439e2385SDenis V. Lunev 	if (how) {
3343408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
33441da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
33451da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
33461da177e4SLinus Torvalds 		in6_dev_put(idev);
33471da177e4SLinus Torvalds 	}
33481da177e4SLinus Torvalds 	return 0;
33491da177e4SLinus Torvalds }
33501da177e4SLinus Torvalds 
33511da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
33521da177e4SLinus Torvalds {
3353b7b1bfceSHannes Frederic Sowa 	struct inet6_dev *idev = (struct inet6_dev *)data;
3354caf92bc4SCong Wang 	struct net_device *dev = idev->dev;
3355b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
33561da177e4SLinus Torvalds 
3357b7b1bfceSHannes Frederic Sowa 	write_lock(&idev->lock);
33585b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
33591da177e4SLinus Torvalds 		goto out;
33601da177e4SLinus Torvalds 
33619ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
33621da177e4SLinus Torvalds 		goto out;
33635b2a1953Sstephen hemminger 
33645b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
33655b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
33665b2a1953Sstephen hemminger 		goto out;
33671da177e4SLinus Torvalds 
3368b7b1bfceSHannes Frederic Sowa 	if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
3369caf92bc4SCong Wang 		write_unlock(&idev->lock);
3370caf92bc4SCong Wang 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3371caf92bc4SCong Wang 			ndisc_send_rs(dev, &lladdr,
3372b7b1bfceSHannes Frederic Sowa 				      &in6addr_linklocal_allrouters);
3373b7b1bfceSHannes Frederic Sowa 		else
3374caf92bc4SCong Wang 			goto put;
3375b7b1bfceSHannes Frederic Sowa 
3376caf92bc4SCong Wang 		write_lock(&idev->lock);
33771da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
3378b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3379b7b1bfceSHannes Frederic Sowa 					     idev->cnf.rtr_solicits) ?
33805b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_delay :
33815b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_interval);
33821da177e4SLinus Torvalds 	} else {
33831da177e4SLinus Torvalds 		/*
33841da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
33851da177e4SLinus Torvalds 		 * assumption any longer.
33861da177e4SLinus Torvalds 		 */
338791df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
33881da177e4SLinus Torvalds 	}
33891da177e4SLinus Torvalds 
33901da177e4SLinus Torvalds out:
3391b7b1bfceSHannes Frederic Sowa 	write_unlock(&idev->lock);
3392caf92bc4SCong Wang put:
3393b7b1bfceSHannes Frederic Sowa 	in6_dev_put(idev);
33941da177e4SLinus Torvalds }
33951da177e4SLinus Torvalds 
33961da177e4SLinus Torvalds /*
33971da177e4SLinus Torvalds  *	Duplicate Address Detection
33981da177e4SLinus Torvalds  */
33993c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
34003c21edbdSYOSHIFUJI Hideaki {
34013c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
34023c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
34033c21edbdSYOSHIFUJI Hideaki 
340495c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
340595c385b4SNeil Horman 		rand_num = 0;
340695c385b4SNeil Horman 	else
340763862b5bSAruna-Hewapathirane 		rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
340895c385b4SNeil Horman 
3409b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes = idev->cnf.dad_transmits;
3410c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, rand_num);
34113c21edbdSYOSHIFUJI Hideaki }
34123c21edbdSYOSHIFUJI Hideaki 
3413c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
34141da177e4SLinus Torvalds {
34151da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
34161da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
34171da177e4SLinus Torvalds 
34181da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
34191da177e4SLinus Torvalds 
342063862b5bSAruna-Hewapathirane 	prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
34211da177e4SLinus Torvalds 
34221da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3423622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3424e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
34251da177e4SLinus Torvalds 		goto out;
34261da177e4SLinus Torvalds 
34271da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
34281b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
342955ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
343055ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3431cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
343221809fafSstephen hemminger 		spin_unlock(&ifp->lock);
34331da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
34341da177e4SLinus Torvalds 
34351da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
34361da177e4SLinus Torvalds 		return;
34371da177e4SLinus Torvalds 	}
34381da177e4SLinus Torvalds 
34396732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
344021809fafSstephen hemminger 		spin_unlock(&ifp->lock);
34416732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
34423c21edbdSYOSHIFUJI Hideaki 		/*
3443590a9887SMasatake YAMATO 		 * If the device is not ready:
34443c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
34453c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
34463c21edbdSYOSHIFUJI Hideaki 		 */
34473c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
3448cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
34496732badeSYOSHIFUJI Hideaki 		return;
34503c21edbdSYOSHIFUJI Hideaki 	}
345195c385b4SNeil Horman 
345295c385b4SNeil Horman 	/*
345395c385b4SNeil Horman 	 * Optimistic nodes can start receiving
345495c385b4SNeil Horman 	 * Frames right away
345595c385b4SNeil Horman 	 */
34567fd2561eSErik Kline 	if (ifp->flags & IFA_F_OPTIMISTIC) {
345795c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
34587fd2561eSErik Kline 		if (ipv6_use_optimistic_addr(idev)) {
34597fd2561eSErik Kline 			/* Because optimistic nodes can use this address,
34607fd2561eSErik Kline 			 * notify listeners. If DAD fails, RTM_DELADDR is sent.
34617fd2561eSErik Kline 			 */
34627fd2561eSErik Kline 			ipv6_ifa_notify(RTM_NEWADDR, ifp);
34637fd2561eSErik Kline 		}
34647fd2561eSErik Kline 	}
346595c385b4SNeil Horman 
34666732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
34671da177e4SLinus Torvalds out:
3468622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
34691da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
34701da177e4SLinus Torvalds }
34711da177e4SLinus Torvalds 
3472c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp)
34731da177e4SLinus Torvalds {
3474c15b1ccaSHannes Frederic Sowa 	bool begin_dad = false;
3475c15b1ccaSHannes Frederic Sowa 
34768e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3477c15b1ccaSHannes Frederic Sowa 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3478c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_PREDAD;
3479c15b1ccaSHannes Frederic Sowa 		begin_dad = true;
3480c15b1ccaSHannes Frederic Sowa 	}
34818e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3482c15b1ccaSHannes Frederic Sowa 
3483c15b1ccaSHannes Frederic Sowa 	if (begin_dad)
3484c15b1ccaSHannes Frederic Sowa 		addrconf_mod_dad_work(ifp, 0);
3485c15b1ccaSHannes Frederic Sowa }
3486c15b1ccaSHannes Frederic Sowa 
3487c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w)
3488c15b1ccaSHannes Frederic Sowa {
3489c15b1ccaSHannes Frederic Sowa 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3490c15b1ccaSHannes Frederic Sowa 						struct inet6_ifaddr,
3491c15b1ccaSHannes Frederic Sowa 						dad_work);
34921da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
34931da177e4SLinus Torvalds 	struct in6_addr mcaddr;
34941da177e4SLinus Torvalds 
3495c15b1ccaSHannes Frederic Sowa 	enum {
3496c15b1ccaSHannes Frederic Sowa 		DAD_PROCESS,
3497c15b1ccaSHannes Frederic Sowa 		DAD_BEGIN,
3498c15b1ccaSHannes Frederic Sowa 		DAD_ABORT,
3499c15b1ccaSHannes Frederic Sowa 	} action = DAD_PROCESS;
3500c15b1ccaSHannes Frederic Sowa 
3501c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
3502c15b1ccaSHannes Frederic Sowa 
35038e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3504c15b1ccaSHannes Frederic Sowa 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3505c15b1ccaSHannes Frederic Sowa 		action = DAD_BEGIN;
3506c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_DAD;
3507c15b1ccaSHannes Frederic Sowa 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3508c15b1ccaSHannes Frederic Sowa 		action = DAD_ABORT;
3509c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
3510c15b1ccaSHannes Frederic Sowa 	}
35118e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3512c15b1ccaSHannes Frederic Sowa 
3513c15b1ccaSHannes Frederic Sowa 	if (action == DAD_BEGIN) {
3514c15b1ccaSHannes Frederic Sowa 		addrconf_dad_begin(ifp);
3515c15b1ccaSHannes Frederic Sowa 		goto out;
3516c15b1ccaSHannes Frederic Sowa 	} else if (action == DAD_ABORT) {
3517c15b1ccaSHannes Frederic Sowa 		addrconf_dad_stop(ifp, 1);
3518c15b1ccaSHannes Frederic Sowa 		goto out;
3519c15b1ccaSHannes Frederic Sowa 	}
3520c15b1ccaSHannes Frederic Sowa 
3521b7b1bfceSHannes Frederic Sowa 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
3522f2344a13SHerbert Xu 		goto out;
3523f2344a13SHerbert Xu 
3524c15b1ccaSHannes Frederic Sowa 	write_lock_bh(&idev->lock);
3525122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
3526c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
35271da177e4SLinus Torvalds 		goto out;
35281da177e4SLinus Torvalds 	}
352921809fafSstephen hemminger 
353021809fafSstephen hemminger 	spin_lock(&ifp->lock);
3531622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3532622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
3533c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
3534622ccdf1SHerbert Xu 		goto out;
3535622ccdf1SHerbert Xu 	}
3536622ccdf1SHerbert Xu 
3537b7b1bfceSHannes Frederic Sowa 	if (ifp->dad_probes == 0) {
35381da177e4SLinus Torvalds 		/*
35391da177e4SLinus Torvalds 		 * DAD was successful
35401da177e4SLinus Torvalds 		 */
35411da177e4SLinus Torvalds 
3542cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
354321809fafSstephen hemminger 		spin_unlock(&ifp->lock);
3544c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
35451da177e4SLinus Torvalds 
35461da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
35471da177e4SLinus Torvalds 
35481da177e4SLinus Torvalds 		goto out;
35491da177e4SLinus Torvalds 	}
35501da177e4SLinus Torvalds 
3551b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes--;
3552c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp,
35531f9248e5SJiri Pirko 			      NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
355421809fafSstephen hemminger 	spin_unlock(&ifp->lock);
3555c15b1ccaSHannes Frederic Sowa 	write_unlock_bh(&idev->lock);
35561da177e4SLinus Torvalds 
35571da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
35581da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3559d7aabf22SYOSHIFUJI Hideaki 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
35601da177e4SLinus Torvalds out:
35611da177e4SLinus Torvalds 	in6_ifa_put(ifp);
3562c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
35631da177e4SLinus Torvalds }
35641da177e4SLinus Torvalds 
356511ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */
356611ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
356711ffff75SHannes Frederic Sowa {
356811ffff75SHannes Frederic Sowa 	struct inet6_ifaddr *ifpiter;
356911ffff75SHannes Frederic Sowa 	struct inet6_dev *idev = ifp->idev;
357011ffff75SHannes Frederic Sowa 
3571602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3572602582caSHannes Frederic Sowa 		if (ifpiter->scope > IFA_LINK)
3573602582caSHannes Frederic Sowa 			break;
357411ffff75SHannes Frederic Sowa 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
357511ffff75SHannes Frederic Sowa 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
357611ffff75SHannes Frederic Sowa 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
357711ffff75SHannes Frederic Sowa 		    IFA_F_PERMANENT)
357811ffff75SHannes Frederic Sowa 			return false;
357911ffff75SHannes Frederic Sowa 	}
358011ffff75SHannes Frederic Sowa 	return true;
358111ffff75SHannes Frederic Sowa }
358211ffff75SHannes Frederic Sowa 
35831da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
35841da177e4SLinus Torvalds {
35851da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
3586b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
3587b173ee48SHannes Frederic Sowa 	bool send_rs, send_mld;
3588b7b1bfceSHannes Frederic Sowa 
3589c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
35901da177e4SLinus Torvalds 
35911da177e4SLinus Torvalds 	/*
35921da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
35931da177e4SLinus Torvalds 	 */
35941da177e4SLinus Torvalds 
35951da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
35961da177e4SLinus Torvalds 
3597026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
3598026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
35991da177e4SLinus Torvalds 	 */
36001da177e4SLinus Torvalds 
36011ec047ebSHannes Frederic Sowa 	read_lock_bh(&ifp->idev->lock);
360211ffff75SHannes Frederic Sowa 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3603b173ee48SHannes Frederic Sowa 	send_rs = send_mld &&
3604b173ee48SHannes Frederic Sowa 		  ipv6_accept_ra(ifp->idev) &&
3605b173ee48SHannes Frederic Sowa 		  ifp->idev->cnf.rtr_solicits > 0 &&
3606b173ee48SHannes Frederic Sowa 		  (dev->flags&IFF_LOOPBACK) == 0;
36071ec047ebSHannes Frederic Sowa 	read_unlock_bh(&ifp->idev->lock);
36081ec047ebSHannes Frederic Sowa 
3609b173ee48SHannes Frederic Sowa 	/* While dad is in progress mld report's source address is in6_addrany.
3610b173ee48SHannes Frederic Sowa 	 * Resend with proper ll now.
3611b173ee48SHannes Frederic Sowa 	 */
3612b173ee48SHannes Frederic Sowa 	if (send_mld)
3613b173ee48SHannes Frederic Sowa 		ipv6_mc_dad_complete(ifp->idev);
3614b173ee48SHannes Frederic Sowa 
36151ec047ebSHannes Frederic Sowa 	if (send_rs) {
36161da177e4SLinus Torvalds 		/*
36171da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
36181da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
36191da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
36201da177e4SLinus Torvalds 		 */
36211ec047ebSHannes Frederic Sowa 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3622b7b1bfceSHannes Frederic Sowa 			return;
36231ec047ebSHannes Frederic Sowa 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
36241da177e4SLinus Torvalds 
3625b7b1bfceSHannes Frederic Sowa 		write_lock_bh(&ifp->idev->lock);
3626b7b1bfceSHannes Frederic Sowa 		spin_lock(&ifp->lock);
3627b7b1bfceSHannes Frederic Sowa 		ifp->idev->rs_probes = 1;
36281da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
3629b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(ifp->idev,
3630b7b1bfceSHannes Frederic Sowa 				      ifp->idev->cnf.rtr_solicit_interval);
3631b7b1bfceSHannes Frederic Sowa 		spin_unlock(&ifp->lock);
3632b7b1bfceSHannes Frederic Sowa 		write_unlock_bh(&ifp->idev->lock);
36331da177e4SLinus Torvalds 	}
36341da177e4SLinus Torvalds }
36351da177e4SLinus Torvalds 
3636e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev)
3637e21e8467SStephen Hemminger {
3638c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
3639c5e33bddSYOSHIFUJI Hideaki 
3640c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
3641502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
364221809fafSstephen hemminger 		spin_lock(&ifp->lock);
3643f2344a13SHerbert Xu 		if (ifp->flags & IFA_F_TENTATIVE &&
3644f2344a13SHerbert Xu 		    ifp->state == INET6_IFADDR_STATE_DAD)
3645c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
3646f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
3647c5e33bddSYOSHIFUJI Hideaki 	}
3648c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
3649c5e33bddSYOSHIFUJI Hideaki }
3650c5e33bddSYOSHIFUJI Hideaki 
36511da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
36521da177e4SLinus Torvalds struct if6_iter_state {
36533c40090aSDaniel Lezcano 	struct seq_net_private p;
36541da177e4SLinus Torvalds 	int bucket;
36551d578303SMihai Maruseac 	int offset;
36561da177e4SLinus Torvalds };
36571da177e4SLinus Torvalds 
36581d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
36591da177e4SLinus Torvalds {
36601da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
36611da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
36621218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
36631d578303SMihai Maruseac 	int p = 0;
36641da177e4SLinus Torvalds 
36651d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
36661d578303SMihai Maruseac 	if (pos == 0) {
36671d578303SMihai Maruseac 		state->bucket = 0;
36681d578303SMihai Maruseac 		state->offset = 0;
36691d578303SMihai Maruseac 	}
36701d578303SMihai Maruseac 
36711d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3672b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
36731d578303SMihai Maruseac 					 addr_lst) {
36749f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
36759f0d3c27SEric Dumazet 				continue;
36761d578303SMihai Maruseac 			/* sync with offset */
36771d578303SMihai Maruseac 			if (p < state->offset) {
36781d578303SMihai Maruseac 				p++;
36791d578303SMihai Maruseac 				continue;
36801d578303SMihai Maruseac 			}
36811d578303SMihai Maruseac 			state->offset++;
36821da177e4SLinus Torvalds 			return ifa;
36831da177e4SLinus Torvalds 		}
36841d578303SMihai Maruseac 
36851d578303SMihai Maruseac 		/* prepare for next bucket */
36861d578303SMihai Maruseac 		state->offset = 0;
36871d578303SMihai Maruseac 		p = 0;
36881d578303SMihai Maruseac 	}
3689c2e21293Sstephen hemminger 	return NULL;
3690c2e21293Sstephen hemminger }
36911da177e4SLinus Torvalds 
3692c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3693c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
36941da177e4SLinus Torvalds {
36951da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
36961218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
36971da177e4SLinus Torvalds 
3698b67bfe0dSSasha Levin 	hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
36999f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
37009f0d3c27SEric Dumazet 			continue;
37011d578303SMihai Maruseac 		state->offset++;
37021da177e4SLinus Torvalds 		return ifa;
37031d578303SMihai Maruseac 	}
37041da177e4SLinus Torvalds 
3705c2e21293Sstephen hemminger 	while (++state->bucket < IN6_ADDR_HSIZE) {
37061d578303SMihai Maruseac 		state->offset = 0;
3707b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa,
3708c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
37099f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
37109f0d3c27SEric Dumazet 				continue;
37111d578303SMihai Maruseac 			state->offset++;
3712c2e21293Sstephen hemminger 			return ifa;
3713c2e21293Sstephen hemminger 		}
3714c2e21293Sstephen hemminger 	}
3715c2e21293Sstephen hemminger 
3716c2e21293Sstephen hemminger 	return NULL;
3717c2e21293Sstephen hemminger }
3718c2e21293Sstephen hemminger 
37191da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
37204f70eccaSEric Dumazet 	__acquires(rcu_bh)
37211da177e4SLinus Torvalds {
37225c578aedSstephen hemminger 	rcu_read_lock_bh();
37231d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
37241da177e4SLinus Torvalds }
37251da177e4SLinus Torvalds 
37261da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
37271da177e4SLinus Torvalds {
37281da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
37291da177e4SLinus Torvalds 
37301da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
37311da177e4SLinus Torvalds 	++*pos;
37321da177e4SLinus Torvalds 	return ifa;
37331da177e4SLinus Torvalds }
37341da177e4SLinus Torvalds 
37351da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
37364f70eccaSEric Dumazet 	__releases(rcu_bh)
37371da177e4SLinus Torvalds {
37385c578aedSstephen hemminger 	rcu_read_unlock_bh();
37391da177e4SLinus Torvalds }
37401da177e4SLinus Torvalds 
37411da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
37421da177e4SLinus Torvalds {
37431da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3744971a351cSJiri Pirko 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3745b071195dSHarvey Harrison 		   &ifp->addr,
37461da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
37471da177e4SLinus Torvalds 		   ifp->prefix_len,
37481da177e4SLinus Torvalds 		   ifp->scope,
3749971a351cSJiri Pirko 		   (u8) ifp->flags,
37501da177e4SLinus Torvalds 		   ifp->idev->dev->name);
37511da177e4SLinus Torvalds 	return 0;
37521da177e4SLinus Torvalds }
37531da177e4SLinus Torvalds 
375456b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
37551da177e4SLinus Torvalds 	.start	= if6_seq_start,
37561da177e4SLinus Torvalds 	.next	= if6_seq_next,
37571da177e4SLinus Torvalds 	.show	= if6_seq_show,
37581da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
37591da177e4SLinus Torvalds };
37601da177e4SLinus Torvalds 
37611da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
37621da177e4SLinus Torvalds {
37633c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
3764cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
37651da177e4SLinus Torvalds }
37661da177e4SLinus Torvalds 
37679a32144eSArjan van de Ven static const struct file_operations if6_fops = {
37681da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
37691da177e4SLinus Torvalds 	.open		= if6_seq_open,
37701da177e4SLinus Torvalds 	.read		= seq_read,
37711da177e4SLinus Torvalds 	.llseek		= seq_lseek,
37723c40090aSDaniel Lezcano 	.release	= seq_release_net,
37731da177e4SLinus Torvalds };
37741da177e4SLinus Torvalds 
37752c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
37761da177e4SLinus Torvalds {
3777d4beaa66SGao feng 	if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
37781da177e4SLinus Torvalds 		return -ENOMEM;
37791da177e4SLinus Torvalds 	return 0;
37801da177e4SLinus Torvalds }
37811da177e4SLinus Torvalds 
37822c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
37833c40090aSDaniel Lezcano {
3784ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
37853c40090aSDaniel Lezcano }
37863c40090aSDaniel Lezcano 
37873c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
37883c40090aSDaniel Lezcano 	.init = if6_proc_net_init,
37893c40090aSDaniel Lezcano 	.exit = if6_proc_net_exit,
37903c40090aSDaniel Lezcano };
37913c40090aSDaniel Lezcano 
37923c40090aSDaniel Lezcano int __init if6_proc_init(void)
37933c40090aSDaniel Lezcano {
37943c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
37953c40090aSDaniel Lezcano }
37963c40090aSDaniel Lezcano 
37971da177e4SLinus Torvalds void if6_proc_exit(void)
37981da177e4SLinus Torvalds {
37993c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
38001da177e4SLinus Torvalds }
38011da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
38021da177e4SLinus Torvalds 
380307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
38043b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
3805b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
38063b9f9a1cSNoriaki TAKAMIYA {
38073b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
3808c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
3809ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
3810c2e21293Sstephen hemminger 
38115c578aedSstephen hemminger 	rcu_read_lock_bh();
3812b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3813878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
3814389f6612SDaniel Lezcano 			continue;
3815caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
38163b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
38173b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
38183b9f9a1cSNoriaki TAKAMIYA 			break;
38193b9f9a1cSNoriaki TAKAMIYA 		}
38203b9f9a1cSNoriaki TAKAMIYA 	}
38215c578aedSstephen hemminger 	rcu_read_unlock_bh();
38223b9f9a1cSNoriaki TAKAMIYA 	return ret;
38233b9f9a1cSNoriaki TAKAMIYA }
38243b9f9a1cSNoriaki TAKAMIYA #endif
38253b9f9a1cSNoriaki TAKAMIYA 
38261da177e4SLinus Torvalds /*
38271da177e4SLinus Torvalds  *	Periodic address status verification
38281da177e4SLinus Torvalds  */
38291da177e4SLinus Torvalds 
3830c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void)
38311da177e4SLinus Torvalds {
3832b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
38331da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
38341da177e4SLinus Torvalds 	int i;
38351da177e4SLinus Torvalds 
3836c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
3837c15b1ccaSHannes Frederic Sowa 
38385c578aedSstephen hemminger 	rcu_read_lock_bh();
38391da177e4SLinus Torvalds 	now = jiffies;
3840b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
38411da177e4SLinus Torvalds 
3842c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
38431da177e4SLinus Torvalds 
38441da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
38451da177e4SLinus Torvalds restart:
3846c15b1ccaSHannes Frederic Sowa 		hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
38471da177e4SLinus Torvalds 			unsigned long age;
38481da177e4SLinus Torvalds 
3849fad8da3eSYasushi Asano 			/* When setting preferred_lft to a value not zero or
3850fad8da3eSYasushi Asano 			 * infinity, while valid_lft is infinity
3851fad8da3eSYasushi Asano 			 * IFA_F_PERMANENT has a non-infinity life time.
3852fad8da3eSYasushi Asano 			 */
3853fad8da3eSYasushi Asano 			if ((ifp->flags & IFA_F_PERMANENT) &&
3854fad8da3eSYasushi Asano 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
38551da177e4SLinus Torvalds 				continue;
38561da177e4SLinus Torvalds 
38571da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
3858b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
3859b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
38601da177e4SLinus Torvalds 
38618f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
38628f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
38631da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
38641da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
38651da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
38661da177e4SLinus Torvalds 				goto restart;
38678f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
38688f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
38698f27ebb9SYOSHIFUJI Hideaki 				continue;
38701da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
3871a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
38721da177e4SLinus Torvalds 				int deprecate = 0;
38731da177e4SLinus Torvalds 
38741da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
38751da177e4SLinus Torvalds 					deprecate = 1;
38761da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
38771da177e4SLinus Torvalds 				}
38781da177e4SLinus Torvalds 
3879fad8da3eSYasushi Asano 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
3880fad8da3eSYasushi Asano 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
38811da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
38821da177e4SLinus Torvalds 
38831da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
38841da177e4SLinus Torvalds 
38851da177e4SLinus Torvalds 				if (deprecate) {
38861da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
38871da177e4SLinus Torvalds 
38881da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
38891da177e4SLinus Torvalds 					in6_ifa_put(ifp);
38901da177e4SLinus Torvalds 					goto restart;
38911da177e4SLinus Torvalds 				}
38921da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
38931da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
389488949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
389588949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
38961f9248e5SJiri Pirko 					NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
389788949cf4Sstephen hemminger 
38981da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
38991da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
39001da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
39011da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
39021da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
39031da177e4SLinus Torvalds 						ifp->regen_count++;
39041da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
39051da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
39061da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
39075c578aedSstephen hemminger 
3908291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
3909291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
3910291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
39111da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
39121da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
39131da177e4SLinus Torvalds 						in6_ifa_put(ifp);
39141da177e4SLinus Torvalds 						goto restart;
39151da177e4SLinus Torvalds 					}
39161da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
39171da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
39181da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
39191da177e4SLinus Torvalds 			} else {
39201da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
39211da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
39221da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
39231da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
39241da177e4SLinus Torvalds 			}
39251da177e4SLinus Torvalds 		}
39261da177e4SLinus Torvalds 	}
39271da177e4SLinus Torvalds 
3928b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
3929b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
3930b2db7564SYOSHIFUJI Hideaki 
3931b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
3932b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3933b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
3934b2db7564SYOSHIFUJI Hideaki 
3935b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3936b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3937b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3938b2db7564SYOSHIFUJI Hideaki 
3939ba3542e1Sdingtianhong 	ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3940ba3542e1Sdingtianhong 	      now, next, next_sec, next_sched);
3941c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
39425c578aedSstephen hemminger 	rcu_read_unlock_bh();
39431da177e4SLinus Torvalds }
39441da177e4SLinus Torvalds 
3945c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w)
3946c15b1ccaSHannes Frederic Sowa {
3947c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
3948c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
3949c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
3950c15b1ccaSHannes Frederic Sowa }
3951c15b1ccaSHannes Frederic Sowa 
3952c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void)
3953c15b1ccaSHannes Frederic Sowa {
3954c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
3955c15b1ccaSHannes Frederic Sowa }
3956c15b1ccaSHannes Frederic Sowa 
3957caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
3958caeaba79SNicolas Dichtel 				     struct in6_addr **peer_pfx)
3959461d8837SThomas Graf {
3960461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
3961461d8837SThomas Graf 
3962caeaba79SNicolas Dichtel 	*peer_pfx = NULL;
3963caeaba79SNicolas Dichtel 
3964461d8837SThomas Graf 	if (addr)
3965461d8837SThomas Graf 		pfx = nla_data(addr);
3966461d8837SThomas Graf 
3967461d8837SThomas Graf 	if (local) {
3968461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3969caeaba79SNicolas Dichtel 			*peer_pfx = pfx;
3970461d8837SThomas Graf 		pfx = nla_data(local);
3971461d8837SThomas Graf 	}
3972461d8837SThomas Graf 
3973461d8837SThomas Graf 	return pfx;
3974461d8837SThomas Graf }
3975461d8837SThomas Graf 
3976ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3977461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
3978461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
3979461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
3980479840ffSJiri Pirko 	[IFA_FLAGS]		= { .len = sizeof(u32) },
3981461d8837SThomas Graf };
3982461d8837SThomas Graf 
39831da177e4SLinus Torvalds static int
3984661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
39851da177e4SLinus Torvalds {
39863b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3987b933f716SThomas Graf 	struct ifaddrmsg *ifm;
3988b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
3989caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
39906046d5b4SHeiner Kallweit 	u32 ifa_flags;
3991b933f716SThomas Graf 	int err;
39921da177e4SLinus Torvalds 
3993b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3994b933f716SThomas Graf 	if (err < 0)
3995b933f716SThomas Graf 		return err;
3996b933f716SThomas Graf 
3997b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
3998caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3999*63159f29SIan Morris 	if (!pfx)
40001da177e4SLinus Torvalds 		return -EINVAL;
40011da177e4SLinus Torvalds 
40026046d5b4SHeiner Kallweit 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
40036046d5b4SHeiner Kallweit 
40046046d5b4SHeiner Kallweit 	/* We ignore other flags so far. */
40056046d5b4SHeiner Kallweit 	ifa_flags &= IFA_F_MANAGETEMPADDR;
40066046d5b4SHeiner Kallweit 
40076046d5b4SHeiner Kallweit 	return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
40086046d5b4SHeiner Kallweit 			      ifm->ifa_prefixlen);
40091da177e4SLinus Torvalds }
40101da177e4SLinus Torvalds 
4011479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
401255ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
4013081bba5bSNoriaki TAKAMIYA {
40146f704992SYOSHIFUJI Hideaki 	u32 flags;
40156f704992SYOSHIFUJI Hideaki 	clock_t expires;
40164bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
401753bd6749SJiri Pirko 	bool was_managetempaddr;
40185b84efecSThomas Haller 	bool had_prefixroute;
401946d48046SYOSHIFUJI Hideaki 
4020c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4021c15b1ccaSHannes Frederic Sowa 
4022081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
4023081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
4024081bba5bSNoriaki TAKAMIYA 
402553bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR &&
402653bd6749SJiri Pirko 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
402753bd6749SJiri Pirko 		return -EINVAL;
402853bd6749SJiri Pirko 
40294bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
40304bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
40314bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
40324bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
40336f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
40344bed72e4SYOSHIFUJI Hideaki 	} else {
40354bed72e4SYOSHIFUJI Hideaki 		expires = 0;
40364bed72e4SYOSHIFUJI Hideaki 		flags = 0;
40374bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
40386f704992SYOSHIFUJI Hideaki 	}
4039081bba5bSNoriaki TAKAMIYA 
40404bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
40414bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
40424bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
404355ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
40444bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
40454bed72e4SYOSHIFUJI Hideaki 	}
4046081bba5bSNoriaki TAKAMIYA 
4047081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
404853bd6749SJiri Pirko 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
40495b84efecSThomas Haller 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
40505b84efecSThomas Haller 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
405153bd6749SJiri Pirko 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4052761aac73SThomas Haller 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4053761aac73SThomas Haller 			IFA_F_NOPREFIXROUTE);
405453bd6749SJiri Pirko 	ifp->flags |= ifa_flags;
4055081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
4056081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
4057081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
4058081bba5bSNoriaki TAKAMIYA 
4059081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
4060081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
4061081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
4062081bba5bSNoriaki TAKAMIYA 
4063761aac73SThomas Haller 	if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
406446d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
40656f704992SYOSHIFUJI Hideaki 				      expires, flags);
40665b84efecSThomas Haller 	} else if (had_prefixroute) {
40675b84efecSThomas Haller 		enum cleanup_prefix_rt_t action;
40685b84efecSThomas Haller 		unsigned long rt_expires;
40695b84efecSThomas Haller 
40705b84efecSThomas Haller 		write_lock_bh(&ifp->idev->lock);
40715b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &rt_expires);
40725b84efecSThomas Haller 		write_unlock_bh(&ifp->idev->lock);
40735b84efecSThomas Haller 
40745b84efecSThomas Haller 		if (action != CLEANUP_PREFIX_RT_NOP) {
40755b84efecSThomas Haller 			cleanup_prefix_route(ifp, rt_expires,
40765b84efecSThomas Haller 				action == CLEANUP_PREFIX_RT_DEL);
40775b84efecSThomas Haller 		}
4078761aac73SThomas Haller 	}
407953bd6749SJiri Pirko 
408053bd6749SJiri Pirko 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
408153bd6749SJiri Pirko 		if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
408253bd6749SJiri Pirko 			valid_lft = prefered_lft = 0;
408353bd6749SJiri Pirko 		manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
408453bd6749SJiri Pirko 				 !was_managetempaddr, jiffies);
408553bd6749SJiri Pirko 	}
408653bd6749SJiri Pirko 
4087c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4088081bba5bSNoriaki TAKAMIYA 
4089081bba5bSNoriaki TAKAMIYA 	return 0;
4090081bba5bSNoriaki TAKAMIYA }
4091081bba5bSNoriaki TAKAMIYA 
4092081bba5bSNoriaki TAKAMIYA static int
4093661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
40941da177e4SLinus Torvalds {
40953b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4096461d8837SThomas Graf 	struct ifaddrmsg *ifm;
4097461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4098caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
40997198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
41007198f8ceSThomas Graf 	struct net_device *dev;
410155ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4102479840ffSJiri Pirko 	u32 ifa_flags;
4103461d8837SThomas Graf 	int err;
41041da177e4SLinus Torvalds 
4105461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4106461d8837SThomas Graf 	if (err < 0)
4107461d8837SThomas Graf 		return err;
4108461d8837SThomas Graf 
4109461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
4110caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4111*63159f29SIan Morris 	if (!pfx)
41121da177e4SLinus Torvalds 		return -EINVAL;
41131da177e4SLinus Torvalds 
4114461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
41150778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
4116461d8837SThomas Graf 
4117461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
41180778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
4119461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
4120461d8837SThomas Graf 	} else {
4121461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
4122461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
41230778769dSNoriaki TAKAMIYA 	}
41240778769dSNoriaki TAKAMIYA 
4125af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
4126*63159f29SIan Morris 	if (!dev)
41277198f8ceSThomas Graf 		return -ENODEV;
4128081bba5bSNoriaki TAKAMIYA 
4129479840ffSJiri Pirko 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4130479840ffSJiri Pirko 
413155ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
4132761aac73SThomas Haller 	ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
413393a714d6SMadhu Challa 		     IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
413455ebaef1SNoriaki TAKAMIYA 
41351cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
4136*63159f29SIan Morris 	if (!ifa) {
41377198f8ceSThomas Graf 		/*
41387198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
4139db9c7c39Sstephen hemminger 		 * userspace already relies on not having to provide this.
41407198f8ceSThomas Graf 		 */
4141caeaba79SNicolas Dichtel 		return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4142af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
4143af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
41441da177e4SLinus Torvalds 	}
41451da177e4SLinus Torvalds 
41467198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
41477198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
41487198f8ceSThomas Graf 		err = -EEXIST;
41497198f8ceSThomas Graf 	else
415055ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
41517198f8ceSThomas Graf 
41527198f8ceSThomas Graf 	in6_ifa_put(ifa);
41537198f8ceSThomas Graf 
41547198f8ceSThomas Graf 	return err;
41557198f8ceSThomas Graf }
41567198f8ceSThomas Graf 
4157479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4158101bb229SThomas Graf 			  u8 scope, int ifindex)
4159101bb229SThomas Graf {
4160101bb229SThomas Graf 	struct ifaddrmsg *ifm;
4161101bb229SThomas Graf 
4162101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
4163101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
4164101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
4165101bb229SThomas Graf 	ifm->ifa_flags = flags;
4166101bb229SThomas Graf 	ifm->ifa_scope = scope;
4167101bb229SThomas Graf 	ifm->ifa_index = ifindex;
4168101bb229SThomas Graf }
4169101bb229SThomas Graf 
417085486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
417185486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
417285486af0SThomas Graf {
417385486af0SThomas Graf 	struct ifa_cacheinfo ci;
417485486af0SThomas Graf 
417518a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
417618a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
417785486af0SThomas Graf 	ci.ifa_prefered = preferred;
417885486af0SThomas Graf 	ci.ifa_valid = valid;
417985486af0SThomas Graf 
418085486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
418185486af0SThomas Graf }
418285486af0SThomas Graf 
4183101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
4184101bb229SThomas Graf {
4185101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
4186101bb229SThomas Graf 		return RT_SCOPE_HOST;
4187101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
4188101bb229SThomas Graf 		return RT_SCOPE_LINK;
4189101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
4190101bb229SThomas Graf 		return RT_SCOPE_SITE;
4191101bb229SThomas Graf 	else
4192101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
4193101bb229SThomas Graf }
4194101bb229SThomas Graf 
41950ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
41960ab6803bSThomas Graf {
4197339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4198caeaba79SNicolas Dichtel 	       + nla_total_size(16) /* IFA_LOCAL */
4199339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
4200479840ffSJiri Pirko 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
4201479840ffSJiri Pirko 	       + nla_total_size(4)  /* IFA_FLAGS */;
42020ab6803bSThomas Graf }
4203c5396a31SYOSHIFUJI Hideaki 
42041da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
420515e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
42061da177e4SLinus Torvalds {
42071da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
420885486af0SThomas Graf 	u32 preferred, valid;
42091da177e4SLinus Torvalds 
421015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4211*63159f29SIan Morris 	if (!nlh)
421226932566SPatrick McHardy 		return -EMSGSIZE;
42130ab6803bSThomas Graf 
4214101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4215101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
4216101bb229SThomas Graf 
4217fad8da3eSYasushi Asano 	if (!((ifa->flags&IFA_F_PERMANENT) &&
4218fad8da3eSYasushi Asano 	      (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
421985486af0SThomas Graf 		preferred = ifa->prefered_lft;
422085486af0SThomas Graf 		valid = ifa->valid_lft;
422185486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
42221da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
4223a1faa698SJens Rosenboom 			if (preferred > tval)
422485486af0SThomas Graf 				preferred -= tval;
4225a1faa698SJens Rosenboom 			else
4226a1faa698SJens Rosenboom 				preferred = 0;
4227f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
4228f56619fcSBen Hutchings 				if (valid > tval)
422985486af0SThomas Graf 					valid -= tval;
4230f56619fcSBen Hutchings 				else
4231f56619fcSBen Hutchings 					valid = 0;
4232f56619fcSBen Hutchings 			}
42331da177e4SLinus Torvalds 		}
42341da177e4SLinus Torvalds 	} else {
423585486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
423685486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
42371da177e4SLinus Torvalds 	}
423885486af0SThomas Graf 
42397996c799SCong Wang 	if (!ipv6_addr_any(&ifa->peer_addr)) {
4240caeaba79SNicolas Dichtel 		if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 ||
4241caeaba79SNicolas Dichtel 		    nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0)
4242caeaba79SNicolas Dichtel 			goto error;
4243caeaba79SNicolas Dichtel 	} else
4244caeaba79SNicolas Dichtel 		if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0)
4245caeaba79SNicolas Dichtel 			goto error;
4246caeaba79SNicolas Dichtel 
4247caeaba79SNicolas Dichtel 	if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4248caeaba79SNicolas Dichtel 		goto error;
424985486af0SThomas Graf 
4250479840ffSJiri Pirko 	if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4251479840ffSJiri Pirko 		goto error;
4252479840ffSJiri Pirko 
4253053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4254053c095aSJohannes Berg 	return 0;
4255caeaba79SNicolas Dichtel 
4256caeaba79SNicolas Dichtel error:
4257caeaba79SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
4258caeaba79SNicolas Dichtel 	return -EMSGSIZE;
42591da177e4SLinus Torvalds }
42601da177e4SLinus Torvalds 
42611da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
426215e47304SEric W. Biederman 				u32 portid, u32 seq, int event, u16 flags)
42631da177e4SLinus Torvalds {
42641da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4265101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4266101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
42671da177e4SLinus Torvalds 
42681da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4269101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4270101bb229SThomas Graf 
427115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4272*63159f29SIan Morris 	if (!nlh)
427326932566SPatrick McHardy 		return -EMSGSIZE;
42740ab6803bSThomas Graf 
4275101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
42760ab6803bSThomas Graf 	if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
42770ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
427826932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
427926932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
428026932566SPatrick McHardy 		return -EMSGSIZE;
428126932566SPatrick McHardy 	}
428285486af0SThomas Graf 
4283053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4284053c095aSJohannes Berg 	return 0;
42851da177e4SLinus Torvalds }
42861da177e4SLinus Torvalds 
42871da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
428815e47304SEric W. Biederman 				u32 portid, u32 seq, int event, unsigned int flags)
42891da177e4SLinus Torvalds {
42901da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4291101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4292101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
42931da177e4SLinus Torvalds 
42941da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4295101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4296101bb229SThomas Graf 
429715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4298*63159f29SIan Morris 	if (!nlh)
429926932566SPatrick McHardy 		return -EMSGSIZE;
43000ab6803bSThomas Graf 
4301101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
43020ab6803bSThomas Graf 	if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
43030ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
430426932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
430526932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
430626932566SPatrick McHardy 		return -EMSGSIZE;
430726932566SPatrick McHardy 	}
430885486af0SThomas Graf 
4309053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4310053c095aSJohannes Berg 	return 0;
43111da177e4SLinus Torvalds }
43121da177e4SLinus Torvalds 
4313e21e8467SStephen Hemminger enum addr_type_t {
43141da177e4SLinus Torvalds 	UNICAST_ADDR,
43151da177e4SLinus Torvalds 	MULTICAST_ADDR,
43161da177e4SLinus Torvalds 	ANYCAST_ADDR,
43171da177e4SLinus Torvalds };
43181da177e4SLinus Torvalds 
4319234b27c3SEric Dumazet /* called with rcu_read_lock() */
4320234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4321234b27c3SEric Dumazet 			  struct netlink_callback *cb, enum addr_type_t type,
4322234b27c3SEric Dumazet 			  int s_ip_idx, int *p_ip_idx)
43231da177e4SLinus Torvalds {
43241da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
43251da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
4326234b27c3SEric Dumazet 	int err = 1;
4327234b27c3SEric Dumazet 	int ip_idx = *p_ip_idx;
43281da177e4SLinus Torvalds 
43291da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
43301da177e4SLinus Torvalds 	switch (type) {
4331502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
4332502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
4333502a2ffdSstephen hemminger 
4334ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
4335502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
4336502a2ffdSstephen hemminger 			if (++ip_idx < s_ip_idx)
43371da177e4SLinus Torvalds 				continue;
43385d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifaddr(skb, ifa,
433915e47304SEric W. Biederman 						NETLINK_CB(cb->skb).portid,
43405d5619b4SYOSHIFUJI Hideaki 						cb->nlh->nlmsg_seq,
43415d5619b4SYOSHIFUJI Hideaki 						RTM_NEWADDR,
43425d5619b4SYOSHIFUJI Hideaki 						NLM_F_MULTI);
4343053c095aSJohannes Berg 			if (err < 0)
4344bcd32326SEric Dumazet 				break;
434563998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
43461da177e4SLinus Torvalds 		}
43471da177e4SLinus Torvalds 		break;
4348502a2ffdSstephen hemminger 	}
43491da177e4SLinus Torvalds 	case MULTICAST_ADDR:
43501da177e4SLinus Torvalds 		/* multicast address */
43511da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
43521da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
43531da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
43541da177e4SLinus Torvalds 				continue;
43555d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifmcaddr(skb, ifmca,
435615e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
43575d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
43585d5619b4SYOSHIFUJI Hideaki 						  RTM_GETMULTICAST,
43595d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4360053c095aSJohannes Berg 			if (err < 0)
4361bcd32326SEric Dumazet 				break;
43621da177e4SLinus Torvalds 		}
43631da177e4SLinus Torvalds 		break;
43641da177e4SLinus Torvalds 	case ANYCAST_ADDR:
43651da177e4SLinus Torvalds 		/* anycast address */
43661da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
43671da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
43681da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
43691da177e4SLinus Torvalds 				continue;
43705d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifacaddr(skb, ifaca,
437115e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
43725d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
43735d5619b4SYOSHIFUJI Hideaki 						  RTM_GETANYCAST,
43745d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4375053c095aSJohannes Berg 			if (err < 0)
4376bcd32326SEric Dumazet 				break;
43771da177e4SLinus Torvalds 		}
43781da177e4SLinus Torvalds 		break;
43791da177e4SLinus Torvalds 	default:
43801da177e4SLinus Torvalds 		break;
43811da177e4SLinus Torvalds 	}
43821da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
4383234b27c3SEric Dumazet 	*p_ip_idx = ip_idx;
4384234b27c3SEric Dumazet 	return err;
4385234b27c3SEric Dumazet }
43865d5619b4SYOSHIFUJI Hideaki 
4387234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4388234b27c3SEric Dumazet 			   enum addr_type_t type)
4389234b27c3SEric Dumazet {
4390234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
4391234b27c3SEric Dumazet 	int h, s_h;
4392234b27c3SEric Dumazet 	int idx, ip_idx;
4393234b27c3SEric Dumazet 	int s_idx, s_ip_idx;
4394234b27c3SEric Dumazet 	struct net_device *dev;
4395234b27c3SEric Dumazet 	struct inet6_dev *idev;
4396234b27c3SEric Dumazet 	struct hlist_head *head;
4397234b27c3SEric Dumazet 
4398234b27c3SEric Dumazet 	s_h = cb->args[0];
4399234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
4400234b27c3SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
4401234b27c3SEric Dumazet 
4402234b27c3SEric Dumazet 	rcu_read_lock();
440363998ac2SNicolas Dichtel 	cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4404234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4405234b27c3SEric Dumazet 		idx = 0;
4406234b27c3SEric Dumazet 		head = &net->dev_index_head[h];
4407b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
4408234b27c3SEric Dumazet 			if (idx < s_idx)
4409234b27c3SEric Dumazet 				goto cont;
44104b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
4411234b27c3SEric Dumazet 				s_ip_idx = 0;
4412234b27c3SEric Dumazet 			ip_idx = 0;
4413e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
4414e21e8467SStephen Hemminger 			if (!idev)
4415234b27c3SEric Dumazet 				goto cont;
4416234b27c3SEric Dumazet 
4417234b27c3SEric Dumazet 			if (in6_dump_addrs(idev, skb, cb, type,
44187b46a644SDavid S. Miller 					   s_ip_idx, &ip_idx) < 0)
4419234b27c3SEric Dumazet 				goto done;
44207562f876SPavel Emelianov cont:
44217562f876SPavel Emelianov 			idx++;
44221da177e4SLinus Torvalds 		}
4423234b27c3SEric Dumazet 	}
4424234b27c3SEric Dumazet done:
4425234b27c3SEric Dumazet 	rcu_read_unlock();
4426234b27c3SEric Dumazet 	cb->args[0] = h;
4427234b27c3SEric Dumazet 	cb->args[1] = idx;
4428234b27c3SEric Dumazet 	cb->args[2] = ip_idx;
4429234b27c3SEric Dumazet 
44301da177e4SLinus Torvalds 	return skb->len;
44311da177e4SLinus Torvalds }
44321da177e4SLinus Torvalds 
44331da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
44341da177e4SLinus Torvalds {
44351da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
4436b854272bSDenis V. Lunev 
44371da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
44381da177e4SLinus Torvalds }
44391da177e4SLinus Torvalds 
44401da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
44411da177e4SLinus Torvalds {
44421da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
4443b854272bSDenis V. Lunev 
44441da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
44451da177e4SLinus Torvalds }
44461da177e4SLinus Torvalds 
44471da177e4SLinus Torvalds 
44481da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
44491da177e4SLinus Torvalds {
44501da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
4451b854272bSDenis V. Lunev 
44521da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
44531da177e4SLinus Torvalds }
44541da177e4SLinus Torvalds 
4455661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
44566c223828SNoriaki TAKAMIYA {
44573b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
44581b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
44591b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4460caeaba79SNicolas Dichtel 	struct in6_addr *addr = NULL, *peer;
44616c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
44626c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
44636c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
44646c223828SNoriaki TAKAMIYA 	int err;
44656c223828SNoriaki TAKAMIYA 
44661b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
44671b29fc2cSThomas Graf 	if (err < 0)
44681b29fc2cSThomas Graf 		goto errout;
44696c223828SNoriaki TAKAMIYA 
4470caeaba79SNicolas Dichtel 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4471*63159f29SIan Morris 	if (!addr) {
44721b29fc2cSThomas Graf 		err = -EINVAL;
44731b29fc2cSThomas Graf 		goto errout;
44741b29fc2cSThomas Graf 	}
44751b29fc2cSThomas Graf 
44761b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
44776c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
44786fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
44796c223828SNoriaki TAKAMIYA 
4480e21e8467SStephen Hemminger 	ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4481e21e8467SStephen Hemminger 	if (!ifa) {
44821b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
44831b29fc2cSThomas Graf 		goto errout;
44846c223828SNoriaki TAKAMIYA 	}
44856c223828SNoriaki TAKAMIYA 
4486e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4487e21e8467SStephen Hemminger 	if (!skb) {
44881b29fc2cSThomas Graf 		err = -ENOBUFS;
44891b29fc2cSThomas Graf 		goto errout_ifa;
44901b29fc2cSThomas Graf 	}
44911b29fc2cSThomas Graf 
449215e47304SEric W. Biederman 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
44936c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
449426932566SPatrick McHardy 	if (err < 0) {
449526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
449626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
449726932566SPatrick McHardy 		kfree_skb(skb);
449826932566SPatrick McHardy 		goto errout_ifa;
449926932566SPatrick McHardy 	}
450015e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
45011b29fc2cSThomas Graf errout_ifa:
45026c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
45031b29fc2cSThomas Graf errout:
45046c223828SNoriaki TAKAMIYA 	return err;
45056c223828SNoriaki TAKAMIYA }
45066c223828SNoriaki TAKAMIYA 
45071da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
45081da177e4SLinus Torvalds {
45091da177e4SLinus Torvalds 	struct sk_buff *skb;
4510c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
45115d620266SThomas Graf 	int err = -ENOBUFS;
45121da177e4SLinus Torvalds 
45130ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4514*63159f29SIan Morris 	if (!skb)
45155d620266SThomas Graf 		goto errout;
45165d620266SThomas Graf 
45175d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
451826932566SPatrick McHardy 	if (err < 0) {
451926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
452026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
452126932566SPatrick McHardy 		kfree_skb(skb);
452226932566SPatrick McHardy 		goto errout;
452326932566SPatrick McHardy 	}
45241ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
45251ce85fe4SPablo Neira Ayuso 	return;
45265d620266SThomas Graf errout:
45275d620266SThomas Graf 	if (err < 0)
45286fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
45291da177e4SLinus Torvalds }
45301da177e4SLinus Torvalds 
4531b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
45321da177e4SLinus Torvalds 				__s32 *array, int bytes)
45331da177e4SLinus Torvalds {
453404561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
453504561c1fSThomas Graf 
45361da177e4SLinus Torvalds 	memset(array, 0, bytes);
45371da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
45381da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
45391da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
45401da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
45411da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
45421da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
45431da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
45441da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
454593908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
454693908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
454793908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
454893908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
45491da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4550fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4551fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4552fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4553fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
45541da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
45551da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
45561da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
45571da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
45581da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
45591da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
456065f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4561c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4562930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4563930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
456493908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
456593908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
4566fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
456709c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
456809c884d4SYOSHIFUJI Hideaki #endif
4569930d6ff2SYOSHIFUJI Hideaki #endif
4570fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
45710bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
457295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
457395c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
45747fd2561eSErik Kline 	array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
457595c385b4SNeil Horman #endif
45767bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
45777bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
45787bc570c8SYOSHIFUJI Hideaki #endif
4579778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
45801b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4581c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
45825cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4583b800c3b9SHannes Frederic Sowa 	array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4584d9333196SBen Greear 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4585c2943f14SHarout Hedeshian 	array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
45863d1bec99SHannes Frederic Sowa 	/* we omit DEVCONF_STABLE_SECRET for now */
45871da177e4SLinus Torvalds }
45881da177e4SLinus Torvalds 
4589b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
4590b382b191SThomas Graf {
4591b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
4592b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
4593b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4594b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4595f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4596f53adae4SDaniel Borkmann 	     + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4597b382b191SThomas Graf }
4598b382b191SThomas Graf 
4599339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
4600339bf98fSThomas Graf {
4601339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4602339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4603339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4604339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
4605339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
4606b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4607339bf98fSThomas Graf }
4608c5396a31SYOSHIFUJI Hideaki 
4609be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
46107d720c3eSTejun Heo 				      int items, int bytes)
46117f7d9a6bSHerbert Xu {
46127f7d9a6bSHerbert Xu 	int i;
46137f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
46147f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
46157f7d9a6bSHerbert Xu 
46167f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
46177f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
46187f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
4619be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
46207f7d9a6bSHerbert Xu 
46217f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
46227f7d9a6bSHerbert Xu }
46237f7d9a6bSHerbert Xu 
4624698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
46254ce3c183SEric Dumazet 				      int items, int bytes, size_t syncpoff)
46264ce3c183SEric Dumazet {
46274ce3c183SEric Dumazet 	int i;
46284ce3c183SEric Dumazet 	int pad = bytes - sizeof(u64) * items;
46294ce3c183SEric Dumazet 	BUG_ON(pad < 0);
46304ce3c183SEric Dumazet 
46314ce3c183SEric Dumazet 	/* Use put_unaligned() because stats may not be aligned for u64. */
46324ce3c183SEric Dumazet 	put_unaligned(items, &stats[0]);
46334ce3c183SEric Dumazet 	for (i = 1; i < items; i++)
46344ce3c183SEric Dumazet 		put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
46354ce3c183SEric Dumazet 
46364ce3c183SEric Dumazet 	memset(&stats[items], 0, pad);
46374ce3c183SEric Dumazet }
46384ce3c183SEric Dumazet 
46397f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
46407f7d9a6bSHerbert Xu 			     int bytes)
46417f7d9a6bSHerbert Xu {
46427f7d9a6bSHerbert Xu 	switch (attrtype) {
46437f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
4644698365faSWANG Cong 		__snmp6_fill_stats64(stats, idev->stats.ipv6,
46454ce3c183SEric Dumazet 				     IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
46467f7d9a6bSHerbert Xu 		break;
46477f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
4648be281e55SEric Dumazet 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
46497f7d9a6bSHerbert Xu 		break;
46507f7d9a6bSHerbert Xu 	}
46517f7d9a6bSHerbert Xu }
46527f7d9a6bSHerbert Xu 
4653b382b191SThomas Graf static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
46541da177e4SLinus Torvalds {
4655bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
46561da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
46571da177e4SLinus Torvalds 
4658c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4659c78679e8SDavid S. Miller 		goto nla_put_failure;
46601da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
466124912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
466224912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
46631f9248e5SJiri Pirko 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
4664c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4665c78679e8SDavid S. Miller 		goto nla_put_failure;
4666bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4667*63159f29SIan Morris 	if (!nla)
466804561c1fSThomas Graf 		goto nla_put_failure;
4669bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
46701da177e4SLinus Torvalds 
4671bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
4672bf99f1bdSYOSHIFUJI Hideaki 
4673bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4674*63159f29SIan Morris 	if (!nla)
4675bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4676bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4677bf99f1bdSYOSHIFUJI Hideaki 
4678bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4679*63159f29SIan Morris 	if (!nla)
4680bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4681bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
46821da177e4SLinus Torvalds 
4683f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4684*63159f29SIan Morris 	if (!nla)
4685f53adae4SDaniel Borkmann 		goto nla_put_failure;
4686bc91b0f0SJiri Pirko 
4687bc91b0f0SJiri Pirko 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
4688bc91b0f0SJiri Pirko 		goto nla_put_failure;
4689bc91b0f0SJiri Pirko 
4690f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
4691f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4692f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
4693f53adae4SDaniel Borkmann 
4694b382b191SThomas Graf 	return 0;
4695b382b191SThomas Graf 
4696b382b191SThomas Graf nla_put_failure:
4697b382b191SThomas Graf 	return -EMSGSIZE;
4698b382b191SThomas Graf }
4699b382b191SThomas Graf 
4700b382b191SThomas Graf static size_t inet6_get_link_af_size(const struct net_device *dev)
4701b382b191SThomas Graf {
4702b382b191SThomas Graf 	if (!__in6_dev_get(dev))
4703b382b191SThomas Graf 		return 0;
4704b382b191SThomas Graf 
4705b382b191SThomas Graf 	return inet6_ifla6_size();
4706b382b191SThomas Graf }
4707b382b191SThomas Graf 
4708b382b191SThomas Graf static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4709b382b191SThomas Graf {
4710b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
4711b382b191SThomas Graf 
4712b382b191SThomas Graf 	if (!idev)
4713b382b191SThomas Graf 		return -ENODATA;
4714b382b191SThomas Graf 
4715b382b191SThomas Graf 	if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4716b382b191SThomas Graf 		return -EMSGSIZE;
4717b382b191SThomas Graf 
4718b382b191SThomas Graf 	return 0;
4719b382b191SThomas Graf }
4720b382b191SThomas Graf 
4721f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4722f53adae4SDaniel Borkmann {
4723f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
4724f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
4725fc403832SDaniel Borkmann 	bool update_rs = false;
4726dc848292SHannes Frederic Sowa 	struct in6_addr ll_addr;
4727f53adae4SDaniel Borkmann 
4728c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4729c15b1ccaSHannes Frederic Sowa 
4730*63159f29SIan Morris 	if (!token)
4731f53adae4SDaniel Borkmann 		return -EINVAL;
4732f53adae4SDaniel Borkmann 	if (ipv6_addr_any(token))
4733f53adae4SDaniel Borkmann 		return -EINVAL;
4734f53adae4SDaniel Borkmann 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4735f53adae4SDaniel Borkmann 		return -EINVAL;
4736f53adae4SDaniel Borkmann 	if (!ipv6_accept_ra(idev))
4737f53adae4SDaniel Borkmann 		return -EINVAL;
4738f53adae4SDaniel Borkmann 	if (idev->cnf.rtr_solicits <= 0)
4739f53adae4SDaniel Borkmann 		return -EINVAL;
4740f53adae4SDaniel Borkmann 
4741f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
4742f53adae4SDaniel Borkmann 
4743f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4744f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4745f53adae4SDaniel Borkmann 
4746f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
4747f53adae4SDaniel Borkmann 
4748dc848292SHannes Frederic Sowa 	if (!idev->dead && (idev->if_flags & IF_READY) &&
4749dc848292SHannes Frederic Sowa 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4750dc848292SHannes Frederic Sowa 			     IFA_F_OPTIMISTIC)) {
4751fc403832SDaniel Borkmann 
4752fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
4753fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
4754fc403832SDaniel Borkmann 		 */
4755f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4756fc403832SDaniel Borkmann 		update_rs = true;
4757fc403832SDaniel Borkmann 	}
4758f53adae4SDaniel Borkmann 
4759f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
4760fc403832SDaniel Borkmann 
476177ecaaceSHannes Frederic Sowa 	if (update_rs) {
4762f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
476377ecaaceSHannes Frederic Sowa 		idev->rs_probes = 1;
476477ecaaceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
476577ecaaceSHannes Frederic Sowa 	}
4766f53adae4SDaniel Borkmann 
4767f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
4768f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
4769f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
4770617fe29dSDaniel Borkmann 		if (ifp->tokenized) {
4771f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
4772f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
4773f53adae4SDaniel Borkmann 		}
4774f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
4775f53adae4SDaniel Borkmann 	}
4776f53adae4SDaniel Borkmann 
4777f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
4778b2ed64a9SLubomir Rintel 	inet6_ifinfo_notify(RTM_NEWLINK, idev);
4779c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4780f53adae4SDaniel Borkmann 	return 0;
4781f53adae4SDaniel Borkmann }
4782f53adae4SDaniel Borkmann 
478311b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
478411b1f828SDaniel Borkmann 	[IFLA_INET6_ADDR_GEN_MODE]	= { .type = NLA_U8 },
478511b1f828SDaniel Borkmann 	[IFLA_INET6_TOKEN]		= { .len = sizeof(struct in6_addr) },
478611b1f828SDaniel Borkmann };
478711b1f828SDaniel Borkmann 
478811b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev,
478911b1f828SDaniel Borkmann 				  const struct nlattr *nla)
479011b1f828SDaniel Borkmann {
479111b1f828SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
479211b1f828SDaniel Borkmann 
479311b1f828SDaniel Borkmann 	if (dev && !__in6_dev_get(dev))
479411b1f828SDaniel Borkmann 		return -EAFNOSUPPORT;
479511b1f828SDaniel Borkmann 
479611b1f828SDaniel Borkmann 	return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
479711b1f828SDaniel Borkmann }
479811b1f828SDaniel Borkmann 
4799f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4800f53adae4SDaniel Borkmann {
4801f53adae4SDaniel Borkmann 	int err = -EINVAL;
4802f53adae4SDaniel Borkmann 	struct inet6_dev *idev = __in6_dev_get(dev);
4803f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
4804f53adae4SDaniel Borkmann 
4805f53adae4SDaniel Borkmann 	if (!idev)
4806f53adae4SDaniel Borkmann 		return -EAFNOSUPPORT;
4807f53adae4SDaniel Borkmann 
4808f53adae4SDaniel Borkmann 	if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4809f53adae4SDaniel Borkmann 		BUG();
4810f53adae4SDaniel Borkmann 
4811bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_TOKEN]) {
4812f53adae4SDaniel Borkmann 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4813bc91b0f0SJiri Pirko 		if (err)
4814bc91b0f0SJiri Pirko 			return err;
4815bc91b0f0SJiri Pirko 	}
4816bc91b0f0SJiri Pirko 
4817bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
4818bc91b0f0SJiri Pirko 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
4819bc91b0f0SJiri Pirko 
4820bc91b0f0SJiri Pirko 		if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
4821622c81d5SHannes Frederic Sowa 		    mode != IN6_ADDR_GEN_MODE_NONE &&
4822622c81d5SHannes Frederic Sowa 		    mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY)
4823bc91b0f0SJiri Pirko 			return -EINVAL;
4824622c81d5SHannes Frederic Sowa 
4825622c81d5SHannes Frederic Sowa 		if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
4826622c81d5SHannes Frederic Sowa 		    !idev->cnf.stable_secret.initialized &&
4827622c81d5SHannes Frederic Sowa 		    !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
4828622c81d5SHannes Frederic Sowa 			return -EINVAL;
4829622c81d5SHannes Frederic Sowa 
4830bc91b0f0SJiri Pirko 		idev->addr_gen_mode = mode;
4831bc91b0f0SJiri Pirko 		err = 0;
4832bc91b0f0SJiri Pirko 	}
4833f53adae4SDaniel Borkmann 
4834f53adae4SDaniel Borkmann 	return err;
4835f53adae4SDaniel Borkmann }
4836f53adae4SDaniel Borkmann 
4837b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
483815e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
4839b382b191SThomas Graf {
4840b382b191SThomas Graf 	struct net_device *dev = idev->dev;
4841b382b191SThomas Graf 	struct ifinfomsg *hdr;
4842b382b191SThomas Graf 	struct nlmsghdr *nlh;
4843b382b191SThomas Graf 	void *protoinfo;
4844b382b191SThomas Graf 
484515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4846*63159f29SIan Morris 	if (!nlh)
4847b382b191SThomas Graf 		return -EMSGSIZE;
4848b382b191SThomas Graf 
4849b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
4850b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
4851b382b191SThomas Graf 	hdr->__ifi_pad = 0;
4852b382b191SThomas Graf 	hdr->ifi_type = dev->type;
4853b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
4854b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
4855b382b191SThomas Graf 	hdr->ifi_change = 0;
4856b382b191SThomas Graf 
4857c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4858c78679e8SDavid S. Miller 	    (dev->addr_len &&
4859c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4860c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4861c78679e8SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
4862c78679e8SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4863c78679e8SDavid S. Miller 		goto nla_put_failure;
4864b382b191SThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4865*63159f29SIan Morris 	if (!protoinfo)
4866b382b191SThomas Graf 		goto nla_put_failure;
4867b382b191SThomas Graf 
4868b382b191SThomas Graf 	if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4869b382b191SThomas Graf 		goto nla_put_failure;
4870b382b191SThomas Graf 
487104561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
4872053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4873053c095aSJohannes Berg 	return 0;
48741da177e4SLinus Torvalds 
487504561c1fSThomas Graf nla_put_failure:
487626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
487726932566SPatrick McHardy 	return -EMSGSIZE;
48781da177e4SLinus Torvalds }
48791da177e4SLinus Torvalds 
48801da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
48811da177e4SLinus Torvalds {
48823b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
488384d2697dSEric Dumazet 	int h, s_h;
4884434a8a58SDavid S. Miller 	int idx = 0, s_idx;
48851da177e4SLinus Torvalds 	struct net_device *dev;
48861da177e4SLinus Torvalds 	struct inet6_dev *idev;
488784d2697dSEric Dumazet 	struct hlist_head *head;
48881da177e4SLinus Torvalds 
488984d2697dSEric Dumazet 	s_h = cb->args[0];
489084d2697dSEric Dumazet 	s_idx = cb->args[1];
489184d2697dSEric Dumazet 
489284d2697dSEric Dumazet 	rcu_read_lock();
489384d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
48947562f876SPavel Emelianov 		idx = 0;
489584d2697dSEric Dumazet 		head = &net->dev_index_head[h];
4896b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
48971da177e4SLinus Torvalds 			if (idx < s_idx)
48987562f876SPavel Emelianov 				goto cont;
489984d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
490084d2697dSEric Dumazet 			if (!idev)
49017562f876SPavel Emelianov 				goto cont;
490284d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
490315e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
490484d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
4905053c095aSJohannes Berg 					      RTM_NEWLINK, NLM_F_MULTI) < 0)
490684d2697dSEric Dumazet 				goto out;
49077562f876SPavel Emelianov cont:
49087562f876SPavel Emelianov 			idx++;
49091da177e4SLinus Torvalds 		}
491084d2697dSEric Dumazet 	}
491184d2697dSEric Dumazet out:
491284d2697dSEric Dumazet 	rcu_read_unlock();
491384d2697dSEric Dumazet 	cb->args[1] = idx;
491484d2697dSEric Dumazet 	cb->args[0] = h;
49151da177e4SLinus Torvalds 
49161da177e4SLinus Torvalds 	return skb->len;
49171da177e4SLinus Torvalds }
49181da177e4SLinus Torvalds 
49191da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
49201da177e4SLinus Torvalds {
49211da177e4SLinus Torvalds 	struct sk_buff *skb;
4922c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
49238d7a76c9SThomas Graf 	int err = -ENOBUFS;
49241da177e4SLinus Torvalds 
4925339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
4926*63159f29SIan Morris 	if (!skb)
49278d7a76c9SThomas Graf 		goto errout;
49288d7a76c9SThomas Graf 
49298d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
493026932566SPatrick McHardy 	if (err < 0) {
493126932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
493226932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
493326932566SPatrick McHardy 		kfree_skb(skb);
493426932566SPatrick McHardy 		goto errout;
493526932566SPatrick McHardy 	}
49365f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
49371ce85fe4SPablo Neira Ayuso 	return;
49388d7a76c9SThomas Graf errout:
49398d7a76c9SThomas Graf 	if (err < 0)
49405f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
49411da177e4SLinus Torvalds }
49421da177e4SLinus Torvalds 
4943339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
4944339bf98fSThomas Graf {
4945339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
4946339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
4947339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
4948339bf98fSThomas Graf }
4949c5396a31SYOSHIFUJI Hideaki 
49501da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
495115e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
4952b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
49531da177e4SLinus Torvalds {
49541da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
49551da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
49561da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
49571da177e4SLinus Torvalds 
495815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
4959*63159f29SIan Morris 	if (!nlh)
496026932566SPatrick McHardy 		return -EMSGSIZE;
49616051e2f4SThomas Graf 
49626051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
49631da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
49648a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
49658a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
49661da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
49671da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
49681da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
49698a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
49701da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
49711da177e4SLinus Torvalds 	if (pinfo->onlink)
49721da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
49731da177e4SLinus Torvalds 	if (pinfo->autoconf)
49741da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
49751da177e4SLinus Torvalds 
4976c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4977c78679e8SDavid S. Miller 		goto nla_put_failure;
49781da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
49791da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
4980c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4981c78679e8SDavid S. Miller 		goto nla_put_failure;
4982053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4983053c095aSJohannes Berg 	return 0;
49841da177e4SLinus Torvalds 
49856051e2f4SThomas Graf nla_put_failure:
498626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
498726932566SPatrick McHardy 	return -EMSGSIZE;
49881da177e4SLinus Torvalds }
49891da177e4SLinus Torvalds 
49901da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
49911da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
49921da177e4SLinus Torvalds {
49931da177e4SLinus Torvalds 	struct sk_buff *skb;
4994c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
49958c384bfaSThomas Graf 	int err = -ENOBUFS;
49961da177e4SLinus Torvalds 
4997339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
4998*63159f29SIan Morris 	if (!skb)
49998c384bfaSThomas Graf 		goto errout;
50008c384bfaSThomas Graf 
50018c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
500226932566SPatrick McHardy 	if (err < 0) {
500326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
500426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
500526932566SPatrick McHardy 		kfree_skb(skb);
500626932566SPatrick McHardy 		goto errout;
500726932566SPatrick McHardy 	}
50081ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
50091ce85fe4SPablo Neira Ayuso 	return;
50108c384bfaSThomas Graf errout:
50118c384bfaSThomas Graf 	if (err < 0)
50126fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
50131da177e4SLinus Torvalds }
50141da177e4SLinus Torvalds 
50151da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
50161da177e4SLinus Torvalds {
501763998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
501863998ac2SNicolas Dichtel 
5019c15b1ccaSHannes Frederic Sowa 	if (event)
5020c15b1ccaSHannes Frederic Sowa 		ASSERT_RTNL();
5021c15b1ccaSHannes Frederic Sowa 
50221da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
50231da177e4SLinus Torvalds 
50241da177e4SLinus Torvalds 	switch (event) {
50251da177e4SLinus Torvalds 	case RTM_NEWADDR:
502695c385b4SNeil Horman 		/*
502795c385b4SNeil Horman 		 * If the address was optimistic
502895c385b4SNeil Horman 		 * we inserted the route at the start of
502995c385b4SNeil Horman 		 * our DAD process, so we don't need
503095c385b4SNeil Horman 		 * to do it again
503195c385b4SNeil Horman 		 */
503295c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
503340e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
50341da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
50351da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
50367996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr))
5037caeaba79SNicolas Dichtel 			addrconf_prefix_route(&ifp->peer_addr, 128,
5038caeaba79SNicolas Dichtel 					      ifp->idev->dev, 0, 0);
50391da177e4SLinus Torvalds 		break;
50401da177e4SLinus Torvalds 	case RTM_DELADDR:
50411da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
50421da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
50431da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
50447996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr)) {
5045caeaba79SNicolas Dichtel 			struct rt6_info *rt;
5046caeaba79SNicolas Dichtel 
5047e7478dfcSNicolas Dichtel 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5048e7478dfcSNicolas Dichtel 						       ifp->idev->dev, 0, 0);
5049f24062b0SNicolas Dichtel 			if (rt && ip6_del_rt(rt))
5050caeaba79SNicolas Dichtel 				dst_free(&rt->dst);
5051caeaba79SNicolas Dichtel 		}
5052d8d1f30bSChangli Gao 		dst_hold(&ifp->rt->dst);
505393fa159aSstephen hemminger 
505473a8bd74SDavid S. Miller 		if (ip6_del_rt(ifp->rt))
5055d8d1f30bSChangli Gao 			dst_free(&ifp->rt->dst);
5056705f1c86SHannes Frederic Sowa 
5057705f1c86SHannes Frederic Sowa 		rt_genid_bump_ipv6(net);
50581da177e4SLinus Torvalds 		break;
50591da177e4SLinus Torvalds 	}
506063998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
50611da177e4SLinus Torvalds }
50621da177e4SLinus Torvalds 
50631da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
50641da177e4SLinus Torvalds {
50658814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
50661da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
50671da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
50688814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
50691da177e4SLinus Torvalds }
50701da177e4SLinus Torvalds 
50711da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
50721da177e4SLinus Torvalds 
50731da177e4SLinus Torvalds static
5074fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
50751da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
50761da177e4SLinus Torvalds {
50771da177e4SLinus Torvalds 	int *valp = ctl->data;
50781da177e4SLinus Torvalds 	int val = *valp;
507988af182eSEric W. Biederman 	loff_t pos = *ppos;
5080fe2c6338SJoe Perches 	struct ctl_table lctl;
50811da177e4SLinus Torvalds 	int ret;
50821da177e4SLinus Torvalds 
5083013d97e9SFrancesco Ruggeri 	/*
5084013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
5085013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5086013d97e9SFrancesco Ruggeri 	 */
5087013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5088013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5089013d97e9SFrancesco Ruggeri 
5090013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
50911da177e4SLinus Torvalds 
5092c8fecf22SPavel Emelyanov 	if (write)
5093b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
509488af182eSEric W. Biederman 	if (ret)
509588af182eSEric W. Biederman 		*ppos = pos;
50961da177e4SLinus Torvalds 	return ret;
50971da177e4SLinus Torvalds }
50981da177e4SLinus Torvalds 
509977751427SMarcelo Leitner static
510077751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
510177751427SMarcelo Leitner 			void __user *buffer, size_t *lenp, loff_t *ppos)
510277751427SMarcelo Leitner {
510377751427SMarcelo Leitner 	struct inet6_dev *idev = ctl->extra1;
510477751427SMarcelo Leitner 	int min_mtu = IPV6_MIN_MTU;
510577751427SMarcelo Leitner 	struct ctl_table lctl;
510677751427SMarcelo Leitner 
510777751427SMarcelo Leitner 	lctl = *ctl;
510877751427SMarcelo Leitner 	lctl.extra1 = &min_mtu;
510977751427SMarcelo Leitner 	lctl.extra2 = idev ? &idev->dev->mtu : NULL;
511077751427SMarcelo Leitner 
511177751427SMarcelo Leitner 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
511277751427SMarcelo Leitner }
511377751427SMarcelo Leitner 
511456d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
511556d417b1SBrian Haley {
511675538c2bSCong Wang 	struct netdev_notifier_info info;
511775538c2bSCong Wang 
511856d417b1SBrian Haley 	if (!idev || !idev->dev)
511956d417b1SBrian Haley 		return;
512056d417b1SBrian Haley 
512175538c2bSCong Wang 	netdev_notifier_info_init(&info, idev->dev);
512256d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
512375538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_DOWN, &info);
512456d417b1SBrian Haley 	else
512575538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_UP, &info);
512656d417b1SBrian Haley }
512756d417b1SBrian Haley 
512856d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
512956d417b1SBrian Haley {
513056d417b1SBrian Haley 	struct net_device *dev;
513156d417b1SBrian Haley 	struct inet6_dev *idev;
513256d417b1SBrian Haley 
513356d417b1SBrian Haley 	rcu_read_lock();
5134c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
513556d417b1SBrian Haley 		idev = __in6_dev_get(dev);
513656d417b1SBrian Haley 		if (idev) {
513756d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
513856d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
513956d417b1SBrian Haley 			if (changed)
514056d417b1SBrian Haley 				dev_disable_change(idev);
514156d417b1SBrian Haley 		}
514256d417b1SBrian Haley 	}
5143c6d14c84SEric Dumazet 	rcu_read_unlock();
514456d417b1SBrian Haley }
514556d417b1SBrian Haley 
5146013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
514756d417b1SBrian Haley {
514856d417b1SBrian Haley 	struct net *net;
5149013d97e9SFrancesco Ruggeri 	int old;
5150013d97e9SFrancesco Ruggeri 
5151013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
5152013d97e9SFrancesco Ruggeri 		return restart_syscall();
515356d417b1SBrian Haley 
515456d417b1SBrian Haley 	net = (struct net *)table->extra2;
5155013d97e9SFrancesco Ruggeri 	old = *p;
5156013d97e9SFrancesco Ruggeri 	*p = newf;
515756d417b1SBrian Haley 
5158013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5159013d97e9SFrancesco Ruggeri 		rtnl_unlock();
516056d417b1SBrian Haley 		return 0;
516188af182eSEric W. Biederman 	}
516256d417b1SBrian Haley 
516356d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
516456d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
516556d417b1SBrian Haley 		addrconf_disable_change(net, newf);
5166013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
516756d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
516856d417b1SBrian Haley 
516956d417b1SBrian Haley 	rtnl_unlock();
517056d417b1SBrian Haley 	return 0;
517156d417b1SBrian Haley }
517256d417b1SBrian Haley 
517356d417b1SBrian Haley static
5174fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
517556d417b1SBrian Haley 			    void __user *buffer, size_t *lenp, loff_t *ppos)
517656d417b1SBrian Haley {
517756d417b1SBrian Haley 	int *valp = ctl->data;
517856d417b1SBrian Haley 	int val = *valp;
517988af182eSEric W. Biederman 	loff_t pos = *ppos;
5180fe2c6338SJoe Perches 	struct ctl_table lctl;
518156d417b1SBrian Haley 	int ret;
518256d417b1SBrian Haley 
5183013d97e9SFrancesco Ruggeri 	/*
5184013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
5185013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5186013d97e9SFrancesco Ruggeri 	 */
5187013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5188013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5189013d97e9SFrancesco Ruggeri 
5190013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
519156d417b1SBrian Haley 
519256d417b1SBrian Haley 	if (write)
519356d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
519488af182eSEric W. Biederman 	if (ret)
519588af182eSEric W. Biederman 		*ppos = pos;
519656d417b1SBrian Haley 	return ret;
519756d417b1SBrian Haley }
519856d417b1SBrian Haley 
5199c92d5491Sstephen hemminger static
5200c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5201c92d5491Sstephen hemminger 			      void __user *buffer, size_t *lenp, loff_t *ppos)
5202c92d5491Sstephen hemminger {
5203c92d5491Sstephen hemminger 	int *valp = ctl->data;
5204c92d5491Sstephen hemminger 	int ret;
5205c92d5491Sstephen hemminger 	int old, new;
5206c92d5491Sstephen hemminger 
5207c92d5491Sstephen hemminger 	old = *valp;
5208c92d5491Sstephen hemminger 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5209c92d5491Sstephen hemminger 	new = *valp;
5210c92d5491Sstephen hemminger 
5211c92d5491Sstephen hemminger 	if (write && old != new) {
5212c92d5491Sstephen hemminger 		struct net *net = ctl->extra2;
5213c92d5491Sstephen hemminger 
5214c92d5491Sstephen hemminger 		if (!rtnl_trylock())
5215c92d5491Sstephen hemminger 			return restart_syscall();
5216c92d5491Sstephen hemminger 
5217c92d5491Sstephen hemminger 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5218c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5219c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_DEFAULT,
5220c92d5491Sstephen hemminger 						     net->ipv6.devconf_dflt);
5221c92d5491Sstephen hemminger 		else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5222c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5223c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_ALL,
5224c92d5491Sstephen hemminger 						     net->ipv6.devconf_all);
5225c92d5491Sstephen hemminger 		else {
5226c92d5491Sstephen hemminger 			struct inet6_dev *idev = ctl->extra1;
5227c92d5491Sstephen hemminger 
5228c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5229c92d5491Sstephen hemminger 						     idev->dev->ifindex,
5230c92d5491Sstephen hemminger 						     &idev->cnf);
5231c92d5491Sstephen hemminger 		}
5232c92d5491Sstephen hemminger 		rtnl_unlock();
5233c92d5491Sstephen hemminger 	}
5234c92d5491Sstephen hemminger 
5235c92d5491Sstephen hemminger 	return ret;
5236c92d5491Sstephen hemminger }
5237c92d5491Sstephen hemminger 
52383d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
52393d1bec99SHannes Frederic Sowa 					 void __user *buffer, size_t *lenp,
52403d1bec99SHannes Frederic Sowa 					 loff_t *ppos)
52413d1bec99SHannes Frederic Sowa {
52423d1bec99SHannes Frederic Sowa 	int err;
52433d1bec99SHannes Frederic Sowa 	struct in6_addr addr;
52443d1bec99SHannes Frederic Sowa 	char str[IPV6_MAX_STRLEN];
52453d1bec99SHannes Frederic Sowa 	struct ctl_table lctl = *ctl;
5246622c81d5SHannes Frederic Sowa 	struct net *net = ctl->extra2;
52473d1bec99SHannes Frederic Sowa 	struct ipv6_stable_secret *secret = ctl->data;
52483d1bec99SHannes Frederic Sowa 
5249622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5250622c81d5SHannes Frederic Sowa 		return -EIO;
5251622c81d5SHannes Frederic Sowa 
52523d1bec99SHannes Frederic Sowa 	lctl.maxlen = IPV6_MAX_STRLEN;
52533d1bec99SHannes Frederic Sowa 	lctl.data = str;
52543d1bec99SHannes Frederic Sowa 
52553d1bec99SHannes Frederic Sowa 	if (!rtnl_trylock())
52563d1bec99SHannes Frederic Sowa 		return restart_syscall();
52573d1bec99SHannes Frederic Sowa 
52583d1bec99SHannes Frederic Sowa 	if (!write && !secret->initialized) {
52593d1bec99SHannes Frederic Sowa 		err = -EIO;
52603d1bec99SHannes Frederic Sowa 		goto out;
52613d1bec99SHannes Frederic Sowa 	}
52623d1bec99SHannes Frederic Sowa 
52633d1bec99SHannes Frederic Sowa 	if (!write) {
52643d1bec99SHannes Frederic Sowa 		err = snprintf(str, sizeof(str), "%pI6",
52653d1bec99SHannes Frederic Sowa 			       &secret->secret);
52663d1bec99SHannes Frederic Sowa 		if (err >= sizeof(str)) {
52673d1bec99SHannes Frederic Sowa 			err = -EIO;
52683d1bec99SHannes Frederic Sowa 			goto out;
52693d1bec99SHannes Frederic Sowa 		}
52703d1bec99SHannes Frederic Sowa 	}
52713d1bec99SHannes Frederic Sowa 
52723d1bec99SHannes Frederic Sowa 	err = proc_dostring(&lctl, write, buffer, lenp, ppos);
52733d1bec99SHannes Frederic Sowa 	if (err || !write)
52743d1bec99SHannes Frederic Sowa 		goto out;
52753d1bec99SHannes Frederic Sowa 
52763d1bec99SHannes Frederic Sowa 	if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
52773d1bec99SHannes Frederic Sowa 		err = -EIO;
52783d1bec99SHannes Frederic Sowa 		goto out;
52793d1bec99SHannes Frederic Sowa 	}
52803d1bec99SHannes Frederic Sowa 
52813d1bec99SHannes Frederic Sowa 	secret->initialized = true;
52823d1bec99SHannes Frederic Sowa 	secret->secret = addr;
52833d1bec99SHannes Frederic Sowa 
5284622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5285622c81d5SHannes Frederic Sowa 		struct net_device *dev;
5286622c81d5SHannes Frederic Sowa 
5287622c81d5SHannes Frederic Sowa 		for_each_netdev(net, dev) {
5288622c81d5SHannes Frederic Sowa 			struct inet6_dev *idev = __in6_dev_get(dev);
5289622c81d5SHannes Frederic Sowa 
5290622c81d5SHannes Frederic Sowa 			if (idev) {
5291622c81d5SHannes Frederic Sowa 				idev->addr_gen_mode =
5292622c81d5SHannes Frederic Sowa 					IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5293622c81d5SHannes Frederic Sowa 			}
5294622c81d5SHannes Frederic Sowa 		}
5295622c81d5SHannes Frederic Sowa 	} else {
5296622c81d5SHannes Frederic Sowa 		struct inet6_dev *idev = ctl->extra1;
5297622c81d5SHannes Frederic Sowa 
5298622c81d5SHannes Frederic Sowa 		idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5299622c81d5SHannes Frederic Sowa 	}
5300622c81d5SHannes Frederic Sowa 
53013d1bec99SHannes Frederic Sowa out:
53023d1bec99SHannes Frederic Sowa 	rtnl_unlock();
53033d1bec99SHannes Frederic Sowa 
53043d1bec99SHannes Frederic Sowa 	return err;
53053d1bec99SHannes Frederic Sowa }
5306c92d5491Sstephen hemminger 
53071da177e4SLinus Torvalds static struct addrconf_sysctl_table
53081da177e4SLinus Torvalds {
53091da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
5310fe2c6338SJoe Perches 	struct ctl_table addrconf_vars[DEVCONF_MAX+1];
5311ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
53121da177e4SLinus Torvalds 	.sysctl_header = NULL,
53131da177e4SLinus Torvalds 	.addrconf_vars = {
53141da177e4SLinus Torvalds 		{
53151da177e4SLinus Torvalds 			.procname	= "forwarding",
53161da177e4SLinus Torvalds 			.data		= &ipv6_devconf.forwarding,
53171da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
53181da177e4SLinus Torvalds 			.mode		= 0644,
53196d9f239aSAlexey Dobriyan 			.proc_handler	= addrconf_sysctl_forward,
53201da177e4SLinus Torvalds 		},
53211da177e4SLinus Torvalds 		{
53221da177e4SLinus Torvalds 			.procname	= "hop_limit",
53231da177e4SLinus Torvalds 			.data		= &ipv6_devconf.hop_limit,
53241da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
53251da177e4SLinus Torvalds 			.mode		= 0644,
53261da177e4SLinus Torvalds 			.proc_handler	= proc_dointvec,
53271da177e4SLinus Torvalds 		},
53281da177e4SLinus Torvalds 		{
53291da177e4SLinus Torvalds 			.procname	= "mtu",
53301da177e4SLinus Torvalds 			.data		= &ipv6_devconf.mtu6,
53311da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
53321da177e4SLinus Torvalds 			.mode		= 0644,
533377751427SMarcelo Leitner 			.proc_handler	= addrconf_sysctl_mtu,
53341da177e4SLinus Torvalds 		},
53351da177e4SLinus Torvalds 		{
53361da177e4SLinus Torvalds 			.procname	= "accept_ra",
53371da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_ra,
53381da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
53391da177e4SLinus Torvalds 			.mode		= 0644,
53406d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
53411da177e4SLinus Torvalds 		},
53421da177e4SLinus Torvalds 		{
53431da177e4SLinus Torvalds 			.procname	= "accept_redirects",
53441da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_redirects,
53451da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
53461da177e4SLinus Torvalds 			.mode		= 0644,
53476d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
53481da177e4SLinus Torvalds 		},
53491da177e4SLinus Torvalds 		{
53501da177e4SLinus Torvalds 			.procname	= "autoconf",
53511da177e4SLinus Torvalds 			.data		= &ipv6_devconf.autoconf,
53521da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
53531da177e4SLinus Torvalds 			.mode		= 0644,
53546d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
53551da177e4SLinus Torvalds 		},
53561da177e4SLinus Torvalds 		{
53571da177e4SLinus Torvalds 			.procname	= "dad_transmits",
53581da177e4SLinus Torvalds 			.data		= &ipv6_devconf.dad_transmits,
53591da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
53601da177e4SLinus Torvalds 			.mode		= 0644,
53616d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
53621da177e4SLinus Torvalds 		},
53631da177e4SLinus Torvalds 		{
53641da177e4SLinus Torvalds 			.procname	= "router_solicitations",
53651da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicits,
53661da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
53671da177e4SLinus Torvalds 			.mode		= 0644,
53686d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
53691da177e4SLinus Torvalds 		},
53701da177e4SLinus Torvalds 		{
53711da177e4SLinus Torvalds 			.procname	= "router_solicitation_interval",
53721da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_interval,
53731da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
53741da177e4SLinus Torvalds 			.mode		= 0644,
53756d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
53761da177e4SLinus Torvalds 		},
53771da177e4SLinus Torvalds 		{
53781da177e4SLinus Torvalds 			.procname	= "router_solicitation_delay",
53791da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_delay,
53801da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
53811da177e4SLinus Torvalds 			.mode		= 0644,
53826d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
53831da177e4SLinus Torvalds 		},
53841da177e4SLinus Torvalds 		{
53851da177e4SLinus Torvalds 			.procname	= "force_mld_version",
53861da177e4SLinus Torvalds 			.data		= &ipv6_devconf.force_mld_version,
53871da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
53881da177e4SLinus Torvalds 			.mode		= 0644,
53896d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
53901da177e4SLinus Torvalds 		},
5391fc4eba58SHannes Frederic Sowa 		{
5392fc4eba58SHannes Frederic Sowa 			.procname	= "mldv1_unsolicited_report_interval",
5393fc4eba58SHannes Frederic Sowa 			.data		=
5394fc4eba58SHannes Frederic Sowa 				&ipv6_devconf.mldv1_unsolicited_report_interval,
5395fc4eba58SHannes Frederic Sowa 			.maxlen		= sizeof(int),
5396fc4eba58SHannes Frederic Sowa 			.mode		= 0644,
5397fc4eba58SHannes Frederic Sowa 			.proc_handler	= proc_dointvec_ms_jiffies,
5398fc4eba58SHannes Frederic Sowa 		},
5399fc4eba58SHannes Frederic Sowa 		{
5400fc4eba58SHannes Frederic Sowa 			.procname	= "mldv2_unsolicited_report_interval",
5401fc4eba58SHannes Frederic Sowa 			.data		=
5402fc4eba58SHannes Frederic Sowa 				&ipv6_devconf.mldv2_unsolicited_report_interval,
5403fc4eba58SHannes Frederic Sowa 			.maxlen		= sizeof(int),
5404fc4eba58SHannes Frederic Sowa 			.mode		= 0644,
5405fc4eba58SHannes Frederic Sowa 			.proc_handler	= proc_dointvec_ms_jiffies,
5406fc4eba58SHannes Frederic Sowa 		},
54071da177e4SLinus Torvalds 		{
54081da177e4SLinus Torvalds 			.procname	= "use_tempaddr",
54091da177e4SLinus Torvalds 			.data		= &ipv6_devconf.use_tempaddr,
54101da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54111da177e4SLinus Torvalds 			.mode		= 0644,
54126d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
54131da177e4SLinus Torvalds 		},
54141da177e4SLinus Torvalds 		{
54151da177e4SLinus Torvalds 			.procname	= "temp_valid_lft",
54161da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_valid_lft,
54171da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54181da177e4SLinus Torvalds 			.mode		= 0644,
54196d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
54201da177e4SLinus Torvalds 		},
54211da177e4SLinus Torvalds 		{
54221da177e4SLinus Torvalds 			.procname	= "temp_prefered_lft",
54231da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_prefered_lft,
54241da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54251da177e4SLinus Torvalds 			.mode		= 0644,
54266d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
54271da177e4SLinus Torvalds 		},
54281da177e4SLinus Torvalds 		{
54291da177e4SLinus Torvalds 			.procname	= "regen_max_retry",
54301da177e4SLinus Torvalds 			.data		= &ipv6_devconf.regen_max_retry,
54311da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54321da177e4SLinus Torvalds 			.mode		= 0644,
54336d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
54341da177e4SLinus Torvalds 		},
54351da177e4SLinus Torvalds 		{
54361da177e4SLinus Torvalds 			.procname	= "max_desync_factor",
54371da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_desync_factor,
54381da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54391da177e4SLinus Torvalds 			.mode		= 0644,
54406d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
54411da177e4SLinus Torvalds 		},
54421da177e4SLinus Torvalds 		{
54431da177e4SLinus Torvalds 			.procname	= "max_addresses",
54441da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_addresses,
54451da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54461da177e4SLinus Torvalds 			.mode		= 0644,
54476d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
54481da177e4SLinus Torvalds 		},
54491da177e4SLinus Torvalds 		{
545065f5c7c1SYOSHIFUJI Hideaki 			.procname	= "accept_ra_defrtr",
545165f5c7c1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_defrtr,
545265f5c7c1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
545365f5c7c1SYOSHIFUJI Hideaki 			.mode		= 0644,
54546d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
545565f5c7c1SYOSHIFUJI Hideaki 		},
545665f5c7c1SYOSHIFUJI Hideaki 		{
5457c4fd30ebSYOSHIFUJI Hideaki 			.procname	= "accept_ra_pinfo",
5458c4fd30ebSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_pinfo,
5459c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5460c4fd30ebSYOSHIFUJI Hideaki 			.mode		= 0644,
54616d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
5462c4fd30ebSYOSHIFUJI Hideaki 		},
5463930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
5464930d6ff2SYOSHIFUJI Hideaki 		{
5465930d6ff2SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rtr_pref",
5466930d6ff2SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rtr_pref,
5467930d6ff2SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5468930d6ff2SYOSHIFUJI Hideaki 			.mode		= 0644,
54696d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
5470930d6ff2SYOSHIFUJI Hideaki 		},
547152e16356SYOSHIFUJI Hideaki 		{
547252e16356SYOSHIFUJI Hideaki 			.procname	= "router_probe_interval",
547352e16356SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.rtr_probe_interval,
547452e16356SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
547552e16356SYOSHIFUJI Hideaki 			.mode		= 0644,
54766d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
547752e16356SYOSHIFUJI Hideaki 		},
5478fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
547909c884d4SYOSHIFUJI Hideaki 		{
548009c884d4SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rt_info_max_plen",
548109c884d4SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
548209c884d4SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
548309c884d4SYOSHIFUJI Hideaki 			.mode		= 0644,
54846d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
548509c884d4SYOSHIFUJI Hideaki 		},
548609c884d4SYOSHIFUJI Hideaki #endif
5487930d6ff2SYOSHIFUJI Hideaki #endif
5488c4fd30ebSYOSHIFUJI Hideaki 		{
5489fbea49e1SYOSHIFUJI Hideaki 			.procname	= "proxy_ndp",
5490fbea49e1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.proxy_ndp,
5491fbea49e1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5492fbea49e1SYOSHIFUJI Hideaki 			.mode		= 0644,
5493c92d5491Sstephen hemminger 			.proc_handler	= addrconf_sysctl_proxy_ndp,
5494fbea49e1SYOSHIFUJI Hideaki 		},
5495fbea49e1SYOSHIFUJI Hideaki 		{
54960bcbc926SYOSHIFUJI Hideaki 			.procname	= "accept_source_route",
54970bcbc926SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_source_route,
54980bcbc926SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
54990bcbc926SYOSHIFUJI Hideaki 			.mode		= 0644,
55006d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55010bcbc926SYOSHIFUJI Hideaki 		},
550295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
550395c385b4SNeil Horman 		{
550495c385b4SNeil Horman 			.procname       = "optimistic_dad",
550595c385b4SNeil Horman 			.data           = &ipv6_devconf.optimistic_dad,
550695c385b4SNeil Horman 			.maxlen         = sizeof(int),
550795c385b4SNeil Horman 			.mode           = 0644,
55086d9f239aSAlexey Dobriyan 			.proc_handler   = proc_dointvec,
550995c385b4SNeil Horman 
551095c385b4SNeil Horman 		},
55117fd2561eSErik Kline 		{
55127fd2561eSErik Kline 			.procname       = "use_optimistic",
55137fd2561eSErik Kline 			.data           = &ipv6_devconf.use_optimistic,
55147fd2561eSErik Kline 			.maxlen         = sizeof(int),
55157fd2561eSErik Kline 			.mode           = 0644,
55167fd2561eSErik Kline 			.proc_handler   = proc_dointvec,
55177fd2561eSErik Kline 
55187fd2561eSErik Kline 		},
551995c385b4SNeil Horman #endif
55207bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
55217bc570c8SYOSHIFUJI Hideaki 		{
55227bc570c8SYOSHIFUJI Hideaki 			.procname	= "mc_forwarding",
55237bc570c8SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.mc_forwarding,
55247bc570c8SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5525a4e6db07SDavid S. Miller 			.mode		= 0444,
55266d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55277bc570c8SYOSHIFUJI Hideaki 		},
55287bc570c8SYOSHIFUJI Hideaki #endif
55290bcbc926SYOSHIFUJI Hideaki 		{
5530778d80beSYOSHIFUJI Hideaki 			.procname	= "disable_ipv6",
5531778d80beSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.disable_ipv6,
5532778d80beSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5533778d80beSYOSHIFUJI Hideaki 			.mode		= 0644,
553456d417b1SBrian Haley 			.proc_handler	= addrconf_sysctl_disable,
5535778d80beSYOSHIFUJI Hideaki 		},
5536778d80beSYOSHIFUJI Hideaki 		{
55371b34be74SYOSHIFUJI Hideaki 			.procname	= "accept_dad",
55381b34be74SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_dad,
55391b34be74SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
55401b34be74SYOSHIFUJI Hideaki 			.mode		= 0644,
55416d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55421b34be74SYOSHIFUJI Hideaki 		},
55431b34be74SYOSHIFUJI Hideaki 		{
5544f7734fdfSOctavian Purdila 			.procname       = "force_tllao",
5545f7734fdfSOctavian Purdila 			.data           = &ipv6_devconf.force_tllao,
5546f7734fdfSOctavian Purdila 			.maxlen         = sizeof(int),
5547f7734fdfSOctavian Purdila 			.mode           = 0644,
5548f7734fdfSOctavian Purdila 			.proc_handler   = proc_dointvec
5549f7734fdfSOctavian Purdila 		},
5550f7734fdfSOctavian Purdila 		{
55515cb04436SHannes Frederic Sowa 			.procname       = "ndisc_notify",
55525cb04436SHannes Frederic Sowa 			.data           = &ipv6_devconf.ndisc_notify,
55535cb04436SHannes Frederic Sowa 			.maxlen         = sizeof(int),
55545cb04436SHannes Frederic Sowa 			.mode           = 0644,
55555cb04436SHannes Frederic Sowa 			.proc_handler   = proc_dointvec
55565cb04436SHannes Frederic Sowa 		},
55575cb04436SHannes Frederic Sowa 		{
5558b800c3b9SHannes Frederic Sowa 			.procname	= "suppress_frag_ndisc",
5559b800c3b9SHannes Frederic Sowa 			.data		= &ipv6_devconf.suppress_frag_ndisc,
5560b800c3b9SHannes Frederic Sowa 			.maxlen		= sizeof(int),
5561b800c3b9SHannes Frederic Sowa 			.mode		= 0644,
5562b800c3b9SHannes Frederic Sowa 			.proc_handler	= proc_dointvec
5563b800c3b9SHannes Frederic Sowa 		},
5564b800c3b9SHannes Frederic Sowa 		{
5565d9333196SBen Greear 			.procname	= "accept_ra_from_local",
5566d9333196SBen Greear 			.data		= &ipv6_devconf.accept_ra_from_local,
5567d9333196SBen Greear 			.maxlen		= sizeof(int),
5568d9333196SBen Greear 			.mode		= 0644,
5569d9333196SBen Greear 			.proc_handler	= proc_dointvec,
5570d9333196SBen Greear 		},
5571d9333196SBen Greear 		{
5572c2943f14SHarout Hedeshian 			.procname	= "accept_ra_mtu",
5573c2943f14SHarout Hedeshian 			.data		= &ipv6_devconf.accept_ra_mtu,
5574c2943f14SHarout Hedeshian 			.maxlen		= sizeof(int),
5575c2943f14SHarout Hedeshian 			.mode		= 0644,
5576c2943f14SHarout Hedeshian 			.proc_handler	= proc_dointvec,
5577c2943f14SHarout Hedeshian 		},
5578c2943f14SHarout Hedeshian 		{
55793d1bec99SHannes Frederic Sowa 			.procname	= "stable_secret",
55803d1bec99SHannes Frederic Sowa 			.data		= &ipv6_devconf.stable_secret,
55813d1bec99SHannes Frederic Sowa 			.maxlen		= IPV6_MAX_STRLEN,
55823d1bec99SHannes Frederic Sowa 			.mode		= 0600,
55833d1bec99SHannes Frederic Sowa 			.proc_handler	= addrconf_sysctl_stable_secret,
55843d1bec99SHannes Frederic Sowa 		},
55853d1bec99SHannes Frederic Sowa 		{
5586f8572d8fSEric W. Biederman 			/* sentinel */
55871da177e4SLinus Torvalds 		}
55881da177e4SLinus Torvalds 	},
55891da177e4SLinus Torvalds };
55901da177e4SLinus Torvalds 
5591bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5592f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
55931da177e4SLinus Torvalds {
55941da177e4SLinus Torvalds 	int i;
55951da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
55966105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
55971dab6222SPavel Emelyanov 
5598af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
5599*63159f29SIan Morris 	if (!t)
5600f68635e6SPavel Emelyanov 		goto out;
5601f68635e6SPavel Emelyanov 
56021da177e4SLinus Torvalds 	for (i = 0; t->addrconf_vars[i].data; i++) {
56031da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
56041da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
5605bff16c2fSPavel Emelyanov 		t->addrconf_vars[i].extra2 = net;
56061da177e4SLinus Torvalds 	}
56071da177e4SLinus Torvalds 
56086105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
56091da177e4SLinus Torvalds 
56106105e293SEric W. Biederman 	t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
5611*63159f29SIan Morris 	if (!t->sysctl_header)
56126105e293SEric W. Biederman 		goto free;
5613f68635e6SPavel Emelyanov 
56141da177e4SLinus Torvalds 	p->sysctl = t;
561595897312SPavel Emelyanov 	return 0;
56161da177e4SLinus Torvalds 
56171da177e4SLinus Torvalds free:
56181da177e4SLinus Torvalds 	kfree(t);
5619f68635e6SPavel Emelyanov out:
562095897312SPavel Emelyanov 	return -ENOBUFS;
56211da177e4SLinus Torvalds }
56221da177e4SLinus Torvalds 
5623408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5624f52295a9SPavel Emelyanov {
5625408c4768SPavel Emelyanov 	struct addrconf_sysctl_table *t;
5626f52295a9SPavel Emelyanov 
5627*63159f29SIan Morris 	if (!p->sysctl)
5628408c4768SPavel Emelyanov 		return;
5629408c4768SPavel Emelyanov 
5630408c4768SPavel Emelyanov 	t = p->sysctl;
56311da177e4SLinus Torvalds 	p->sysctl = NULL;
5632ff538818SLucian Adrian Grijincu 	unregister_net_sysctl_table(t->sysctl_header);
56331da177e4SLinus Torvalds 	kfree(t);
56341da177e4SLinus Torvalds }
5635408c4768SPavel Emelyanov 
5636a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev)
5637408c4768SPavel Emelyanov {
5638a317a2f1SWANG Cong 	int err;
5639a317a2f1SWANG Cong 
5640a317a2f1SWANG Cong 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
5641a317a2f1SWANG Cong 		return -EINVAL;
5642a317a2f1SWANG Cong 
5643a317a2f1SWANG Cong 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
5644f8572d8fSEric W. Biederman 				    &ndisc_ifinfo_sysctl_change);
5645a317a2f1SWANG Cong 	if (err)
5646a317a2f1SWANG Cong 		return err;
5647a317a2f1SWANG Cong 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5648f8572d8fSEric W. Biederman 					 idev, &idev->cnf);
5649a317a2f1SWANG Cong 	if (err)
5650a317a2f1SWANG Cong 		neigh_sysctl_unregister(idev->nd_parms);
5651a317a2f1SWANG Cong 
5652a317a2f1SWANG Cong 	return err;
5653408c4768SPavel Emelyanov }
5654408c4768SPavel Emelyanov 
5655408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
5656408c4768SPavel Emelyanov {
5657408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
5658408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
56591da177e4SLinus Torvalds }
56601da177e4SLinus Torvalds 
56611da177e4SLinus Torvalds 
56621da177e4SLinus Torvalds #endif
56631da177e4SLinus Torvalds 
56642c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
5665e0da5a48SPavel Emelyanov {
5666a79ca223SHong Zhiguo 	int err = -ENOMEM;
5667e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
5668e0da5a48SPavel Emelyanov 
5669a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5670*63159f29SIan Morris 	if (!all)
5671e0da5a48SPavel Emelyanov 		goto err_alloc_all;
5672e0da5a48SPavel Emelyanov 
5673a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5674*63159f29SIan Morris 	if (!dflt)
5675e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
5676a79ca223SHong Zhiguo 
567756d417b1SBrian Haley 	/* these will be inherited by all namespaces */
567856d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
567956d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5680e0da5a48SPavel Emelyanov 
56813d1bec99SHannes Frederic Sowa 	dflt->stable_secret.initialized = false;
56823d1bec99SHannes Frederic Sowa 	all->stable_secret.initialized = false;
56833d1bec99SHannes Frederic Sowa 
5684e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
5685e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
5686e0da5a48SPavel Emelyanov 
5687e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5688f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
5689e0da5a48SPavel Emelyanov 	if (err < 0)
5690e0da5a48SPavel Emelyanov 		goto err_reg_all;
5691e0da5a48SPavel Emelyanov 
5692f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5693e0da5a48SPavel Emelyanov 	if (err < 0)
5694e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
5695e0da5a48SPavel Emelyanov #endif
5696e0da5a48SPavel Emelyanov 	return 0;
5697e0da5a48SPavel Emelyanov 
5698e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5699e0da5a48SPavel Emelyanov err_reg_dflt:
5700e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
5701e0da5a48SPavel Emelyanov err_reg_all:
5702e0da5a48SPavel Emelyanov 	kfree(dflt);
5703e0da5a48SPavel Emelyanov #endif
5704e0da5a48SPavel Emelyanov err_alloc_dflt:
5705e0da5a48SPavel Emelyanov 	kfree(all);
5706e0da5a48SPavel Emelyanov err_alloc_all:
5707e0da5a48SPavel Emelyanov 	return err;
5708e0da5a48SPavel Emelyanov }
5709e0da5a48SPavel Emelyanov 
57102c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
5711e0da5a48SPavel Emelyanov {
5712e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5713e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5714e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
5715e0da5a48SPavel Emelyanov #endif
5716e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_dflt);
5717e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_all);
5718e0da5a48SPavel Emelyanov }
5719e0da5a48SPavel Emelyanov 
5720e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
5721e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
5722e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
5723e0da5a48SPavel Emelyanov };
5724e0da5a48SPavel Emelyanov 
5725207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = {
5726b382b191SThomas Graf 	.family		  = AF_INET6,
5727b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
5728b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
572911b1f828SDaniel Borkmann 	.validate_link_af = inet6_validate_link_af,
5730f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
5731b382b191SThomas Graf };
5732b382b191SThomas Graf 
57331da177e4SLinus Torvalds /*
57341da177e4SLinus Torvalds  *	Init / cleanup code
57351da177e4SLinus Torvalds  */
57361da177e4SLinus Torvalds 
57371da177e4SLinus Torvalds int __init addrconf_init(void)
57381da177e4SLinus Torvalds {
5739a317a2f1SWANG Cong 	struct inet6_dev *idev;
5740c2e21293Sstephen hemminger 	int i, err;
57412a8cc6c8SYOSHIFUJI Hideaki 
5742e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
5743e21e8467SStephen Hemminger 	if (err < 0) {
5744f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
5745f3213831SJoe Perches 			__func__, err);
57462cc6d2bfSNeil Horman 		goto out;
57472a8cc6c8SYOSHIFUJI Hideaki 	}
57481da177e4SLinus Torvalds 
57492cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
57502cc6d2bfSNeil Horman 	if (err < 0)
57512cc6d2bfSNeil Horman 		goto out_addrlabel;
5752e0da5a48SPavel Emelyanov 
5753c15b1ccaSHannes Frederic Sowa 	addrconf_wq = create_workqueue("ipv6_addrconf");
5754c15b1ccaSHannes Frederic Sowa 	if (!addrconf_wq) {
5755c15b1ccaSHannes Frederic Sowa 		err = -ENOMEM;
5756c15b1ccaSHannes Frederic Sowa 		goto out_nowq;
5757c15b1ccaSHannes Frederic Sowa 	}
5758c15b1ccaSHannes Frederic Sowa 
57591da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
57601da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
57611da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
57621da177e4SLinus Torvalds 	 * to other devices which are up.
57631da177e4SLinus Torvalds 	 *
57641da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
57651da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
57661da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
57671da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
57681da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
57691da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
57701da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
57711da177e4SLinus Torvalds 	 *
57721da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
57731da177e4SLinus Torvalds 	 * loopback_dev first by hand.
57741da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
57751da177e4SLinus Torvalds 	 * device and it being up should be removed.
57761da177e4SLinus Torvalds 	 */
57771da177e4SLinus Torvalds 	rtnl_lock();
5778a317a2f1SWANG Cong 	idev = ipv6_add_dev(init_net.loopback_dev);
57791da177e4SLinus Torvalds 	rtnl_unlock();
5780a317a2f1SWANG Cong 	if (IS_ERR(idev)) {
5781a317a2f1SWANG Cong 		err = PTR_ERR(idev);
5782e0da5a48SPavel Emelyanov 		goto errlo;
5783a317a2f1SWANG Cong 	}
57841da177e4SLinus Torvalds 
5785c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
5786c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5787c2e21293Sstephen hemminger 
57881da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
57891da177e4SLinus Torvalds 
5790c15b1ccaSHannes Frederic Sowa 	addrconf_verify();
5791c127ea2cSThomas Graf 
57923678a9d8Sstephen hemminger 	rtnl_af_register(&inet6_ops);
5793b382b191SThomas Graf 
5794c7ac8679SGreg Rose 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5795c7ac8679SGreg Rose 			      NULL);
5796c127ea2cSThomas Graf 	if (err < 0)
5797c127ea2cSThomas Graf 		goto errout;
5798c127ea2cSThomas Graf 
5799c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
5800c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5801c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5802c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5803c7ac8679SGreg Rose 			inet6_dump_ifaddr, NULL);
5804c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5805c7ac8679SGreg Rose 			inet6_dump_ifmcaddr, NULL);
5806c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5807c7ac8679SGreg Rose 			inet6_dump_ifacaddr, NULL);
580876f8f6cbSNicolas Dichtel 	__rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
58097a674200SNicolas Dichtel 			inet6_netconf_dump_devconf, NULL);
5810c127ea2cSThomas Graf 
58112a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
58122a8cc6c8SYOSHIFUJI Hideaki 
58131da177e4SLinus Torvalds 	return 0;
5814c127ea2cSThomas Graf errout:
5815b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
5816c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
5817e0da5a48SPavel Emelyanov errlo:
5818c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
5819c15b1ccaSHannes Frederic Sowa out_nowq:
5820e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
58212cc6d2bfSNeil Horman out_addrlabel:
58222cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
58232cc6d2bfSNeil Horman out:
5824c127ea2cSThomas Graf 	return err;
58251da177e4SLinus Torvalds }
58261da177e4SLinus Torvalds 
582709f7709fSDaniel Lezcano void addrconf_cleanup(void)
58281da177e4SLinus Torvalds {
5829176c39afSDaniel Lezcano 	struct net_device *dev;
58301da177e4SLinus Torvalds 	int i;
58311da177e4SLinus Torvalds 
58321da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
5833e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
58342cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
58351da177e4SLinus Torvalds 
58361da177e4SLinus Torvalds 	rtnl_lock();
58371da177e4SLinus Torvalds 
5838b382b191SThomas Graf 	__rtnl_af_unregister(&inet6_ops);
5839b382b191SThomas Graf 
5840176c39afSDaniel Lezcano 	/* clean dev list */
5841176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
5842176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
5843176c39afSDaniel Lezcano 			continue;
5844176c39afSDaniel Lezcano 		addrconf_ifdown(dev, 1);
5845176c39afSDaniel Lezcano 	}
5846176c39afSDaniel Lezcano 	addrconf_ifdown(init_net.loopback_dev, 2);
5847176c39afSDaniel Lezcano 
58481da177e4SLinus Torvalds 	/*
58491da177e4SLinus Torvalds 	 *	Check hash table.
58501da177e4SLinus Torvalds 	 */
58515c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
5852c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
5853c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
58545c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
5855c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
58561da177e4SLinus Torvalds 	rtnl_unlock();
5857c15b1ccaSHannes Frederic Sowa 
5858c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
58591da177e4SLinus Torvalds }
5860