xref: /openbmc/linux/net/ipv6/addrconf.c (revision 0344338bd883e5e4a2f80409ed8260cd65d69e3b)
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,
1988013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
199c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
200930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
201930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
20252e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
20309c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
20409c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
20509c884d4SYOSHIFUJI Hideaki #endif
206930d6ff2SYOSHIFUJI Hideaki #endif
207fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2080bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
209778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2101b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
211b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
212c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2133d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2143d1bec99SHannes Frederic Sowa 		.initialized = false,
2153985e8a3SErik Kline 	},
2163985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
21735103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
2181da177e4SLinus Torvalds };
2191da177e4SLinus Torvalds 
220ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2211da177e4SLinus Torvalds 	.forwarding		= 0,
2221da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2231da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2241da177e4SLinus Torvalds 	.accept_ra		= 1,
2251da177e4SLinus Torvalds 	.accept_redirects	= 1,
2261da177e4SLinus Torvalds 	.autoconf		= 1,
227fc4eba58SHannes Frederic Sowa 	.force_mld_version	= 0,
228fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
229fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2301da177e4SLinus Torvalds 	.dad_transmits		= 1,
2311da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2321da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
2331da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2341da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2351da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2361da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2371da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2381da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2391da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
24065f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
241d9333196SBen Greear 	.accept_ra_from_local	= 0,
2428013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
243c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
244930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
245930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
24652e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
24709c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
24809c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
24909c884d4SYOSHIFUJI Hideaki #endif
250930d6ff2SYOSHIFUJI Hideaki #endif
251fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2520bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
253778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2541b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
255b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
256c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2573d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2583d1bec99SHannes Frederic Sowa 		.initialized = false,
2593d1bec99SHannes Frederic Sowa 	},
2603985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
26135103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
2621da177e4SLinus Torvalds };
2631da177e4SLinus Torvalds 
264f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
26505297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev)
266f24e3d65SMitsuru Chinen {
26705297949SDavid S. Miller 	return !qdisc_tx_is_noop(dev);
268f24e3d65SMitsuru Chinen }
269f24e3d65SMitsuru Chinen 
270b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev)
2711da177e4SLinus Torvalds {
272b7b1bfceSHannes Frederic Sowa 	if (del_timer(&idev->rs_timer))
273b7b1bfceSHannes Frederic Sowa 		__in6_dev_put(idev);
274b7b1bfceSHannes Frederic Sowa }
275b7b1bfceSHannes Frederic Sowa 
276c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
277b7b1bfceSHannes Frederic Sowa {
278c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
2791da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
282b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev,
2831da177e4SLinus Torvalds 				  unsigned long when)
2841da177e4SLinus Torvalds {
285b7b1bfceSHannes Frederic Sowa 	if (!timer_pending(&idev->rs_timer))
286b7b1bfceSHannes Frederic Sowa 		in6_dev_hold(idev);
287b7b1bfceSHannes Frederic Sowa 	mod_timer(&idev->rs_timer, jiffies + when);
2881da177e4SLinus Torvalds }
289b7b1bfceSHannes Frederic Sowa 
290c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
291c15b1ccaSHannes Frederic Sowa 				   unsigned long delay)
292b7b1bfceSHannes Frederic Sowa {
293c15b1ccaSHannes Frederic Sowa 	if (!delayed_work_pending(&ifp->dad_work))
294b7b1bfceSHannes Frederic Sowa 		in6_ifa_hold(ifp);
295c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2987f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
2997f7d9a6bSHerbert Xu {
300827da44cSJohn Stultz 	int i;
301827da44cSJohn Stultz 
302698365faSWANG Cong 	idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
303698365faSWANG Cong 	if (!idev->stats.ipv6)
3047f7d9a6bSHerbert Xu 		goto err_ip;
305827da44cSJohn Stultz 
306827da44cSJohn Stultz 	for_each_possible_cpu(i) {
307827da44cSJohn Stultz 		struct ipstats_mib *addrconf_stats;
308698365faSWANG Cong 		addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
309827da44cSJohn Stultz 		u64_stats_init(&addrconf_stats->syncp);
310827da44cSJohn Stultz 	}
311827da44cSJohn Stultz 
312827da44cSJohn Stultz 
313be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
314be281e55SEric Dumazet 					GFP_KERNEL);
315be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
3167f7d9a6bSHerbert Xu 		goto err_icmp;
317be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
318be281e55SEric Dumazet 					   GFP_KERNEL);
319be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
32014878f75SDavid L Stevens 		goto err_icmpmsg;
3217f7d9a6bSHerbert Xu 
3227f7d9a6bSHerbert Xu 	return 0;
3237f7d9a6bSHerbert Xu 
32414878f75SDavid L Stevens err_icmpmsg:
325be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3267f7d9a6bSHerbert Xu err_icmp:
327698365faSWANG Cong 	free_percpu(idev->stats.ipv6);
3287f7d9a6bSHerbert Xu err_ip:
329aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3307f7d9a6bSHerbert Xu }
3317f7d9a6bSHerbert Xu 
3321da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	struct inet6_dev *ndev;
335a317a2f1SWANG Cong 	int err = -ENOMEM;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	ASSERT_RTNL();
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
340a317a2f1SWANG Cong 		return ERR_PTR(-EINVAL);
3411da177e4SLinus Torvalds 
342322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
34363159f29SIan Morris 	if (!ndev)
344a317a2f1SWANG Cong 		return ERR_PTR(err);
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3471da177e4SLinus Torvalds 	ndev->dev = dev;
348502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
349b7b1bfceSHannes Frederic Sowa 	setup_timer(&ndev->rs_timer, addrconf_rs_timer,
350b7b1bfceSHannes Frederic Sowa 		    (unsigned long)ndev);
351c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3521da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3531da177e4SLinus Torvalds 	ndev->cnf.sysctl = NULL;
3541da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
35563159f29SIan Morris 	if (!ndev->nd_parms) {
3561da177e4SLinus Torvalds 		kfree(ndev);
357a317a2f1SWANG Cong 		return ERR_PTR(err);
3581da177e4SLinus Torvalds 	}
3590187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
3600187bdfbSBen Hutchings 		dev_disable_lro(dev);
3611da177e4SLinus Torvalds 	/* We refer to the device */
3621da177e4SLinus Torvalds 	dev_hold(dev);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
365ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
366f3213831SJoe Perches 			"%s: cannot allocate memory for statistics; dev=%s.\n",
367ba3542e1Sdingtianhong 			__func__, dev->name);
3681da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3698603e33dSRoy Li 		dev_put(dev);
3708603e33dSRoy Li 		kfree(ndev);
371a317a2f1SWANG Cong 		return ERR_PTR(err);
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
375ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
376f3213831SJoe Perches 			"%s: cannot create /proc/net/dev_snmp6/%s\n",
377ba3542e1Sdingtianhong 			__func__, dev->name);
378a317a2f1SWANG Cong 		goto err_release;
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3821da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3831da177e4SLinus Torvalds 	 */
3841da177e4SLinus Torvalds 	in6_dev_hold(ndev);
3851da177e4SLinus Torvalds 
3861b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
3871b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
3881b34be74SYOSHIFUJI Hideaki 
38907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
390c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
391f3213831SJoe Perches 		pr_info("%s: Disabled Multicast RS\n", dev->name);
392c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
393c7dc89c0SFred L. Templin 	}
394b077d7abSYOSHIFUJI Hideaki #endif
395b077d7abSYOSHIFUJI Hideaki 
396372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
3971da177e4SLinus Torvalds 	setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
3981da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
3991da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4009625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4011da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4021da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4030be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
4041da177e4SLinus Torvalds 	} else {
4051da177e4SLinus Torvalds 		in6_dev_hold(ndev);
4061da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
4071da177e4SLinus Torvalds 	}
4085d9efa7eSDavid S. Miller 
409914faa14SDaniel Borkmann 	ndev->token = in6addr_any;
4101da177e4SLinus Torvalds 
411f24e3d65SMitsuru Chinen 	if (netif_running(dev) && addrconf_qdisc_ok(dev))
41253aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
41353aadcc9SHerbert Xu 
4141da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4151da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
416a317a2f1SWANG Cong 	err = addrconf_sysctl_register(ndev);
417a317a2f1SWANG Cong 	if (err) {
418a317a2f1SWANG Cong 		ipv6_mc_destroy_dev(ndev);
419a317a2f1SWANG Cong 		del_timer(&ndev->regen_timer);
420a317a2f1SWANG Cong 		goto err_release;
421a317a2f1SWANG Cong 	}
42230c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
423cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip6_ptr, ndev);
424d88ae4ccSYOSHIFUJI Hideaki 
4252c5e8933SHannes Frederic Sowa 	/* Join interface-local all-node multicast group */
4262c5e8933SHannes Frederic Sowa 	ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4272c5e8933SHannes Frederic Sowa 
428d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
429f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
430d88ae4ccSYOSHIFUJI Hideaki 
431d6ddef9eSLi Wei 	/* Join all-router multicast group if forwarding is set */
4328b2aaedeSLi Wei 	if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
433d6ddef9eSLi Wei 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
434d6ddef9eSLi Wei 
4351da177e4SLinus Torvalds 	return ndev;
436a317a2f1SWANG Cong 
437a317a2f1SWANG Cong err_release:
438a317a2f1SWANG Cong 	neigh_parms_release(&nd_tbl, ndev->nd_parms);
439a317a2f1SWANG Cong 	ndev->dead = 1;
440a317a2f1SWANG Cong 	in6_dev_finish_destroy(ndev);
441a317a2f1SWANG Cong 	return ERR_PTR(err);
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
444cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	struct inet6_dev *idev;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	ASSERT_RTNL();
4491da177e4SLinus Torvalds 
450e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
451e21e8467SStephen Hemminger 	if (!idev) {
452e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
453a317a2f1SWANG Cong 		if (IS_ERR(idev))
4541da177e4SLinus Torvalds 			return NULL;
4551da177e4SLinus Torvalds 	}
456c5e33bddSYOSHIFUJI Hideaki 
4571da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4581da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4591da177e4SLinus Torvalds 	return idev;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
462f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
463f3a1bfb1SNicolas Dichtel {
464f3a1bfb1SNicolas Dichtel 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
465f3a1bfb1SNicolas Dichtel 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
466f3a1bfb1SNicolas Dichtel 
46776f8f6cbSNicolas Dichtel 	/* type -1 is used for ALL */
46876f8f6cbSNicolas Dichtel 	if (type == -1 || type == NETCONFA_FORWARDING)
469f3a1bfb1SNicolas Dichtel 		size += nla_total_size(4);
470b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
471d67b8c61SNicolas Dichtel 	if (type == -1 || type == NETCONFA_MC_FORWARDING)
472d67b8c61SNicolas Dichtel 		size += nla_total_size(4);
473b1afce95SDavid S. Miller #endif
474c92d5491Sstephen hemminger 	if (type == -1 || type == NETCONFA_PROXY_NEIGH)
475c92d5491Sstephen hemminger 		size += nla_total_size(4);
476f3a1bfb1SNicolas Dichtel 
47735103d11SAndy Gospodarek 	if (type == -1 || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
47835103d11SAndy Gospodarek 		size += nla_total_size(4);
47935103d11SAndy Gospodarek 
480f3a1bfb1SNicolas Dichtel 	return size;
481f3a1bfb1SNicolas Dichtel }
482f3a1bfb1SNicolas Dichtel 
483f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
484f3a1bfb1SNicolas Dichtel 				      struct ipv6_devconf *devconf, u32 portid,
485f3a1bfb1SNicolas Dichtel 				      u32 seq, int event, unsigned int flags,
486f3a1bfb1SNicolas Dichtel 				      int type)
487f3a1bfb1SNicolas Dichtel {
488f3a1bfb1SNicolas Dichtel 	struct nlmsghdr  *nlh;
489f3a1bfb1SNicolas Dichtel 	struct netconfmsg *ncm;
490f3a1bfb1SNicolas Dichtel 
491f3a1bfb1SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
492f3a1bfb1SNicolas Dichtel 			flags);
49363159f29SIan Morris 	if (!nlh)
494f3a1bfb1SNicolas Dichtel 		return -EMSGSIZE;
495f3a1bfb1SNicolas Dichtel 
496f3a1bfb1SNicolas Dichtel 	ncm = nlmsg_data(nlh);
497f3a1bfb1SNicolas Dichtel 	ncm->ncm_family = AF_INET6;
498f3a1bfb1SNicolas Dichtel 
499f3a1bfb1SNicolas Dichtel 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
500f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
501f3a1bfb1SNicolas Dichtel 
50276f8f6cbSNicolas Dichtel 	/* type -1 is used for ALL */
50376f8f6cbSNicolas Dichtel 	if ((type == -1 || type == NETCONFA_FORWARDING) &&
504f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
505f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
506b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
507d67b8c61SNicolas Dichtel 	if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
508d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
509d67b8c61SNicolas Dichtel 			devconf->mc_forwarding) < 0)
510d67b8c61SNicolas Dichtel 		goto nla_put_failure;
511b1afce95SDavid S. Miller #endif
512c92d5491Sstephen hemminger 	if ((type == -1 || type == NETCONFA_PROXY_NEIGH) &&
513c92d5491Sstephen hemminger 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
514c92d5491Sstephen hemminger 		goto nla_put_failure;
515c92d5491Sstephen hemminger 
51635103d11SAndy Gospodarek 	if ((type == -1 || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
51735103d11SAndy Gospodarek 	    nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
51835103d11SAndy Gospodarek 			devconf->ignore_routes_with_linkdown) < 0)
51935103d11SAndy Gospodarek 		goto nla_put_failure;
52035103d11SAndy Gospodarek 
521053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
522053c095aSJohannes Berg 	return 0;
523f3a1bfb1SNicolas Dichtel 
524f3a1bfb1SNicolas Dichtel nla_put_failure:
525f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
526f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
527f3a1bfb1SNicolas Dichtel }
528f3a1bfb1SNicolas Dichtel 
529d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
530f3a1bfb1SNicolas Dichtel 				  struct ipv6_devconf *devconf)
531f3a1bfb1SNicolas Dichtel {
532f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
533f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
534f3a1bfb1SNicolas Dichtel 
535f3a1bfb1SNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
53663159f29SIan Morris 	if (!skb)
537f3a1bfb1SNicolas Dichtel 		goto errout;
538f3a1bfb1SNicolas Dichtel 
539f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
540f3a1bfb1SNicolas Dichtel 					 RTM_NEWNETCONF, 0, type);
541f3a1bfb1SNicolas Dichtel 	if (err < 0) {
542f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
543f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
544f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
545f3a1bfb1SNicolas Dichtel 		goto errout;
546f3a1bfb1SNicolas Dichtel 	}
547f3a1bfb1SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
548f3a1bfb1SNicolas Dichtel 	return;
549f3a1bfb1SNicolas Dichtel errout:
550f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
551f3a1bfb1SNicolas Dichtel }
552f3a1bfb1SNicolas Dichtel 
55376f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
55476f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
55576f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
556c92d5491Sstephen hemminger 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
55735103d11SAndy Gospodarek 	[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]	= { .len = sizeof(int) },
55876f8f6cbSNicolas Dichtel };
55976f8f6cbSNicolas Dichtel 
56076f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
561661d2967SThomas Graf 				     struct nlmsghdr *nlh)
56276f8f6cbSNicolas Dichtel {
56376f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
56476f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
56576f8f6cbSNicolas Dichtel 	struct netconfmsg *ncm;
56676f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
56776f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
56876f8f6cbSNicolas Dichtel 	struct inet6_dev *in6_dev;
56976f8f6cbSNicolas Dichtel 	struct net_device *dev;
57076f8f6cbSNicolas Dichtel 	int ifindex;
57176f8f6cbSNicolas Dichtel 	int err;
57276f8f6cbSNicolas Dichtel 
57376f8f6cbSNicolas Dichtel 	err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
57476f8f6cbSNicolas Dichtel 			  devconf_ipv6_policy);
57576f8f6cbSNicolas Dichtel 	if (err < 0)
57676f8f6cbSNicolas Dichtel 		goto errout;
57776f8f6cbSNicolas Dichtel 
57876f8f6cbSNicolas Dichtel 	err = EINVAL;
57976f8f6cbSNicolas Dichtel 	if (!tb[NETCONFA_IFINDEX])
58076f8f6cbSNicolas Dichtel 		goto errout;
58176f8f6cbSNicolas Dichtel 
58276f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
58376f8f6cbSNicolas Dichtel 	switch (ifindex) {
58476f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
58576f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
58676f8f6cbSNicolas Dichtel 		break;
58776f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
58876f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
58976f8f6cbSNicolas Dichtel 		break;
59076f8f6cbSNicolas Dichtel 	default:
59176f8f6cbSNicolas Dichtel 		dev = __dev_get_by_index(net, ifindex);
59263159f29SIan Morris 		if (!dev)
59376f8f6cbSNicolas Dichtel 			goto errout;
59476f8f6cbSNicolas Dichtel 		in6_dev = __in6_dev_get(dev);
59563159f29SIan Morris 		if (!in6_dev)
59676f8f6cbSNicolas Dichtel 			goto errout;
59776f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
59876f8f6cbSNicolas Dichtel 		break;
59976f8f6cbSNicolas Dichtel 	}
60076f8f6cbSNicolas Dichtel 
60176f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
60276f8f6cbSNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
60363159f29SIan Morris 	if (!skb)
60476f8f6cbSNicolas Dichtel 		goto errout;
60576f8f6cbSNicolas Dichtel 
60676f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
60776f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
60876f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
60976f8f6cbSNicolas Dichtel 					 -1);
61076f8f6cbSNicolas Dichtel 	if (err < 0) {
61176f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
61276f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
61376f8f6cbSNicolas Dichtel 		kfree_skb(skb);
61476f8f6cbSNicolas Dichtel 		goto errout;
61576f8f6cbSNicolas Dichtel 	}
61676f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
61776f8f6cbSNicolas Dichtel errout:
61876f8f6cbSNicolas Dichtel 	return err;
61976f8f6cbSNicolas Dichtel }
62076f8f6cbSNicolas Dichtel 
6217a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
6227a674200SNicolas Dichtel 				      struct netlink_callback *cb)
6237a674200SNicolas Dichtel {
6247a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
6257a674200SNicolas Dichtel 	int h, s_h;
6267a674200SNicolas Dichtel 	int idx, s_idx;
6277a674200SNicolas Dichtel 	struct net_device *dev;
6287a674200SNicolas Dichtel 	struct inet6_dev *idev;
6297a674200SNicolas Dichtel 	struct hlist_head *head;
6307a674200SNicolas Dichtel 
6317a674200SNicolas Dichtel 	s_h = cb->args[0];
6327a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
6337a674200SNicolas Dichtel 
6347a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6357a674200SNicolas Dichtel 		idx = 0;
6367a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
6377a674200SNicolas Dichtel 		rcu_read_lock();
63863998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
63963998ac2SNicolas Dichtel 			  net->dev_base_seq;
6407a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
6417a674200SNicolas Dichtel 			if (idx < s_idx)
6427a674200SNicolas Dichtel 				goto cont;
6437a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
6447a674200SNicolas Dichtel 			if (!idev)
6457a674200SNicolas Dichtel 				goto cont;
6467a674200SNicolas Dichtel 
6477a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
6487a674200SNicolas Dichtel 						       &idev->cnf,
6497a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
6507a674200SNicolas Dichtel 						       cb->nlh->nlmsg_seq,
6517a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
6527a674200SNicolas Dichtel 						       NLM_F_MULTI,
653053c095aSJohannes Berg 						       -1) < 0) {
6547a674200SNicolas Dichtel 				rcu_read_unlock();
6557a674200SNicolas Dichtel 				goto done;
6567a674200SNicolas Dichtel 			}
65763998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
6587a674200SNicolas Dichtel cont:
6597a674200SNicolas Dichtel 			idx++;
6607a674200SNicolas Dichtel 		}
6617a674200SNicolas Dichtel 		rcu_read_unlock();
6627a674200SNicolas Dichtel 	}
6637a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
6647a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
6657a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
6667a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6677a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6687a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
669053c095aSJohannes Berg 					       -1) < 0)
6707a674200SNicolas Dichtel 			goto done;
6717a674200SNicolas Dichtel 		else
6727a674200SNicolas Dichtel 			h++;
6737a674200SNicolas Dichtel 	}
6747a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
6757a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
6767a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
6777a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6787a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6797a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
680053c095aSJohannes Berg 					       -1) < 0)
6817a674200SNicolas Dichtel 			goto done;
6827a674200SNicolas Dichtel 		else
6837a674200SNicolas Dichtel 			h++;
6847a674200SNicolas Dichtel 	}
6857a674200SNicolas Dichtel done:
6867a674200SNicolas Dichtel 	cb->args[0] = h;
6877a674200SNicolas Dichtel 	cb->args[1] = idx;
6887a674200SNicolas Dichtel 
6897a674200SNicolas Dichtel 	return skb->len;
6907a674200SNicolas Dichtel }
6917a674200SNicolas Dichtel 
6921da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
6931da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
6941da177e4SLinus Torvalds {
6951da177e4SLinus Torvalds 	struct net_device *dev;
6961da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	if (!idev)
6991da177e4SLinus Torvalds 		return;
7001da177e4SLinus Torvalds 	dev = idev->dev;
7010187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
7020187bdfbSBen Hutchings 		dev_disable_lro(dev);
7031a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
7042c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
705f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
7062c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
7072c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
7082c5e8933SHannes Frederic Sowa 		} else {
709f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
7102c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
7112c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
7122c5e8933SHannes Frederic Sowa 		}
7131da177e4SLinus Torvalds 	}
714502a2ffdSstephen hemminger 
715502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
7162c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
7172c12a74cSMichal Wrobel 			continue;
7181da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
7191da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
7201da177e4SLinus Torvalds 		else
7211da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
7221da177e4SLinus Torvalds 	}
723f3a1bfb1SNicolas Dichtel 	inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
724f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
7251da177e4SLinus Torvalds }
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 
728e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	struct net_device *dev;
7311da177e4SLinus Torvalds 	struct inet6_dev *idev;
7321da177e4SLinus Torvalds 
7334acd4945SBen Hutchings 	for_each_netdev(net, dev) {
7341da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
7351da177e4SLinus Torvalds 		if (idev) {
736e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
737e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
7381da177e4SLinus Torvalds 			if (changed)
7391da177e4SLinus Torvalds 				dev_forward_change(idev);
7401da177e4SLinus Torvalds 		}
7411da177e4SLinus Torvalds 	}
7421da177e4SLinus Torvalds }
743c8fecf22SPavel Emelyanov 
744013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
745c8fecf22SPavel Emelyanov {
746bff16c2fSPavel Emelyanov 	struct net *net;
747013d97e9SFrancesco Ruggeri 	int old;
748013d97e9SFrancesco Ruggeri 
749013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
750013d97e9SFrancesco Ruggeri 		return restart_syscall();
751bff16c2fSPavel Emelyanov 
752bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
753013d97e9SFrancesco Ruggeri 	old = *p;
754013d97e9SFrancesco Ruggeri 	*p = newf;
755c8fecf22SPavel Emelyanov 
756013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
757f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
758f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
759f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
760f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
761013d97e9SFrancesco Ruggeri 		rtnl_unlock();
762013d97e9SFrancesco Ruggeri 		return 0;
76388af182eSEric W. Biederman 	}
764b325fddbSStephen Hemminger 
765e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
766e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
767e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
768f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
769f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
770f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
771f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
772013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
773c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
7740187bdfbSBen Hutchings 	rtnl_unlock();
775c8fecf22SPavel Emelyanov 
776013d97e9SFrancesco Ruggeri 	if (newf)
7777b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
778b325fddbSStephen Hemminger 	return 1;
779c8fecf22SPavel Emelyanov }
78035103d11SAndy Gospodarek 
78135103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf)
78235103d11SAndy Gospodarek {
78335103d11SAndy Gospodarek 	struct net_device *dev;
78435103d11SAndy Gospodarek 	struct inet6_dev *idev;
78535103d11SAndy Gospodarek 
78635103d11SAndy Gospodarek 	for_each_netdev(net, dev) {
78735103d11SAndy Gospodarek 		idev = __in6_dev_get(dev);
78835103d11SAndy Gospodarek 		if (idev) {
78935103d11SAndy Gospodarek 			int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
79035103d11SAndy Gospodarek 
79135103d11SAndy Gospodarek 			idev->cnf.ignore_routes_with_linkdown = newf;
79235103d11SAndy Gospodarek 			if (changed)
79335103d11SAndy Gospodarek 				inet6_netconf_notify_devconf(dev_net(dev),
79435103d11SAndy Gospodarek 							     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
79535103d11SAndy Gospodarek 							     dev->ifindex,
79635103d11SAndy Gospodarek 							     &idev->cnf);
79735103d11SAndy Gospodarek 		}
79835103d11SAndy Gospodarek 	}
79935103d11SAndy Gospodarek }
80035103d11SAndy Gospodarek 
80135103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
80235103d11SAndy Gospodarek {
80335103d11SAndy Gospodarek 	struct net *net;
80435103d11SAndy Gospodarek 	int old;
80535103d11SAndy Gospodarek 
80635103d11SAndy Gospodarek 	if (!rtnl_trylock())
80735103d11SAndy Gospodarek 		return restart_syscall();
80835103d11SAndy Gospodarek 
80935103d11SAndy Gospodarek 	net = (struct net *)table->extra2;
81035103d11SAndy Gospodarek 	old = *p;
81135103d11SAndy Gospodarek 	*p = newf;
81235103d11SAndy Gospodarek 
81335103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
81435103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
81535103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
81635103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
81735103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_DEFAULT,
81835103d11SAndy Gospodarek 						     net->ipv6.devconf_dflt);
81935103d11SAndy Gospodarek 		rtnl_unlock();
82035103d11SAndy Gospodarek 		return 0;
82135103d11SAndy Gospodarek 	}
82235103d11SAndy Gospodarek 
82335103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
82435103d11SAndy Gospodarek 		net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
82535103d11SAndy Gospodarek 		addrconf_linkdown_change(net, newf);
82635103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
82735103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
82835103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
82935103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_ALL,
83035103d11SAndy Gospodarek 						     net->ipv6.devconf_all);
83135103d11SAndy Gospodarek 	}
83235103d11SAndy Gospodarek 	rtnl_unlock();
83335103d11SAndy Gospodarek 
83435103d11SAndy Gospodarek 	return 1;
83535103d11SAndy Gospodarek }
83635103d11SAndy Gospodarek 
8371da177e4SLinus Torvalds #endif
8381da177e4SLinus Torvalds 
8395c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
8401da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
8411da177e4SLinus Torvalds {
842c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
843547b792cSIlpo Järvinen 
8441da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
84591df42beSJoe Perches 	pr_debug("%s\n", __func__);
8461da177e4SLinus Torvalds #endif
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
8491da177e4SLinus Torvalds 
850c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
851c15b1ccaSHannes Frederic Sowa 		pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
852c15b1ccaSHannes Frederic Sowa 			  ifp);
8531da177e4SLinus Torvalds 
854e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
855f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
8561da177e4SLinus Torvalds 		return;
8571da177e4SLinus Torvalds 	}
85894e187c0SAmerigo Wang 	ip6_rt_put(ifp->rt);
8591da177e4SLinus Torvalds 
860e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
863e55ffac6SBrian Haley static void
864e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
865e55ffac6SBrian Haley {
866502a2ffdSstephen hemminger 	struct list_head *p;
8678a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
868e55ffac6SBrian Haley 
869e55ffac6SBrian Haley 	/*
870e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
871e55ffac6SBrian Haley 	 * global before linklocal.
872e55ffac6SBrian Haley 	 */
873502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
874502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
875502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
8768a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
877e55ffac6SBrian Haley 			break;
878e55ffac6SBrian Haley 	}
879e55ffac6SBrian Haley 
880b54c9b98SDavid S. Miller 	list_add_tail(&ifp->if_list, p);
881e55ffac6SBrian Haley }
882e55ffac6SBrian Haley 
883ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr)
8843eb84f49SYOSHIFUJI Hideaki {
885ddbe5032SEric Dumazet 	return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
8863eb84f49SYOSHIFUJI Hideaki }
8873eb84f49SYOSHIFUJI Hideaki 
8881da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds static struct inet6_ifaddr *
8913f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
8923f8f5298SJiri Pirko 	      const struct in6_addr *peer_addr, int pfxlen,
8938a226b2cSJiri Benc 	      int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
8941da177e4SLinus Torvalds {
8951da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
8961da177e4SLinus Torvalds 	struct rt6_info *rt;
8973a88a81dSstephen hemminger 	unsigned int hash;
8981da177e4SLinus Torvalds 	int err = 0;
899d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
900d68b8270SYOSHIFUJI Hideaki 
901d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
902d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
903d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
904d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
905d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
9061da177e4SLinus Torvalds 
9078814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
9081da177e4SLinus Torvalds 	if (idev->dead) {
9091da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
9101da177e4SLinus Torvalds 		goto out2;
9111da177e4SLinus Torvalds 	}
9121da177e4SLinus Torvalds 
91356d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
9149bdd8d40SBrian Haley 		err = -EACCES;
9159bdd8d40SBrian Haley 		goto out2;
9169bdd8d40SBrian Haley 	}
9179bdd8d40SBrian Haley 
9185c578aedSstephen hemminger 	spin_lock(&addrconf_hash_lock);
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
921c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
922ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: already assigned\n");
9231da177e4SLinus Torvalds 		err = -EEXIST;
9241da177e4SLinus Torvalds 		goto out;
9251da177e4SLinus Torvalds 	}
9261da177e4SLinus Torvalds 
927322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
9281da177e4SLinus Torvalds 
92963159f29SIan Morris 	if (!ifa) {
930ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: malloc failed\n");
9311da177e4SLinus Torvalds 		err = -ENOBUFS;
9321da177e4SLinus Torvalds 		goto out;
9331da177e4SLinus Torvalds 	}
9341da177e4SLinus Torvalds 
9358f031519SDavid S. Miller 	rt = addrconf_dst_alloc(idev, addr, false);
9361da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
9371da177e4SLinus Torvalds 		err = PTR_ERR(rt);
9381da177e4SLinus Torvalds 		goto out;
9391da177e4SLinus Torvalds 	}
9401da177e4SLinus Torvalds 
941bba24896SJiri Pirko 	neigh_parms_data_state_setall(idev->nd_parms);
942bba24896SJiri Pirko 
9434e3fd7a0SAlexey Dobriyan 	ifa->addr = *addr;
9443f8f5298SJiri Pirko 	if (peer_addr)
9453f8f5298SJiri Pirko 		ifa->peer_addr = *peer_addr;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
948c15b1ccaSHannes Frederic Sowa 	INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
949c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
9501da177e4SLinus Torvalds 	ifa->scope = scope;
9511da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
9521da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
9538a226b2cSJiri Benc 	ifa->valid_lft = valid_lft;
9548a226b2cSJiri Benc 	ifa->prefered_lft = prefered_lft;
9551da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
956617fe29dSDaniel Borkmann 	ifa->tokenized = false;
9571da177e4SLinus Torvalds 
95857f5f544SKeir Fraser 	ifa->rt = rt;
95957f5f544SKeir Fraser 
9601da177e4SLinus Torvalds 	ifa->idev = idev;
9611da177e4SLinus Torvalds 	in6_dev_hold(idev);
9621da177e4SLinus Torvalds 	/* For caller */
9631da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	/* Add to big hash table */
966ddbe5032SEric Dumazet 	hash = inet6_addr_hash(addr);
9671da177e4SLinus Torvalds 
9685c578aedSstephen hemminger 	hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
9695c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	write_lock(&idev->lock);
9721da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
973e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
976372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
9771da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
9781da177e4SLinus Torvalds 	}
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
9811da177e4SLinus Torvalds 	write_unlock(&idev->lock);
9821da177e4SLinus Torvalds out2:
9838814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
9841da177e4SLinus Torvalds 
985fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
986f88c91ddSCong Wang 		inet6addr_notifier_call_chain(NETDEV_UP, ifa);
9871da177e4SLinus Torvalds 	else {
9881da177e4SLinus Torvalds 		kfree(ifa);
9891da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
9901da177e4SLinus Torvalds 	}
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	return ifa;
9931da177e4SLinus Torvalds out:
9945c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
9951da177e4SLinus Torvalds 	goto out2;
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
9985b84efecSThomas Haller enum cleanup_prefix_rt_t {
9995b84efecSThomas Haller 	CLEANUP_PREFIX_RT_NOP,    /* no cleanup action for prefix route */
10005b84efecSThomas Haller 	CLEANUP_PREFIX_RT_DEL,    /* delete the prefix route */
10015b84efecSThomas Haller 	CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
10025b84efecSThomas Haller };
10035b84efecSThomas Haller 
10045b84efecSThomas Haller /*
10055b84efecSThomas Haller  * Check, whether the prefix for ifp would still need a prefix route
10065b84efecSThomas Haller  * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
10075b84efecSThomas Haller  * constants.
10085b84efecSThomas Haller  *
10095b84efecSThomas Haller  * 1) we don't purge prefix if address was not permanent.
10105b84efecSThomas Haller  *    prefix is managed by its own lifetime.
10115b84efecSThomas Haller  * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
10125b84efecSThomas Haller  * 3) if there are no addresses, delete prefix.
10135b84efecSThomas Haller  * 4) if there are still other permanent address(es),
10145b84efecSThomas Haller  *    corresponding prefix is still permanent.
10155b84efecSThomas Haller  * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
10165b84efecSThomas Haller  *    don't purge the prefix, assume user space is managing it.
10175b84efecSThomas Haller  * 6) otherwise, update prefix lifetime to the
10185b84efecSThomas Haller  *    longest valid lifetime among the corresponding
10195b84efecSThomas Haller  *    addresses on the device.
10205b84efecSThomas Haller  *    Note: subsequent RA will update lifetime.
10215b84efecSThomas Haller  **/
10225b84efecSThomas Haller static enum cleanup_prefix_rt_t
10235b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
10245b84efecSThomas Haller {
10255b84efecSThomas Haller 	struct inet6_ifaddr *ifa;
10265b84efecSThomas Haller 	struct inet6_dev *idev = ifp->idev;
10275b84efecSThomas Haller 	unsigned long lifetime;
10285b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
10295b84efecSThomas Haller 
10305b84efecSThomas Haller 	*expires = jiffies;
10315b84efecSThomas Haller 
10325b84efecSThomas Haller 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
10335b84efecSThomas Haller 		if (ifa == ifp)
10345b84efecSThomas Haller 			continue;
10355b84efecSThomas Haller 		if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
10365b84efecSThomas Haller 				       ifp->prefix_len))
10375b84efecSThomas Haller 			continue;
10385b84efecSThomas Haller 		if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
10395b84efecSThomas Haller 			return CLEANUP_PREFIX_RT_NOP;
10405b84efecSThomas Haller 
10415b84efecSThomas Haller 		action = CLEANUP_PREFIX_RT_EXPIRE;
10425b84efecSThomas Haller 
10435b84efecSThomas Haller 		spin_lock(&ifa->lock);
10445b84efecSThomas Haller 
10455b84efecSThomas Haller 		lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
10465b84efecSThomas Haller 		/*
10475b84efecSThomas Haller 		 * Note: Because this address is
10485b84efecSThomas Haller 		 * not permanent, lifetime <
10495b84efecSThomas Haller 		 * LONG_MAX / HZ here.
10505b84efecSThomas Haller 		 */
10515b84efecSThomas Haller 		if (time_before(*expires, ifa->tstamp + lifetime * HZ))
10525b84efecSThomas Haller 			*expires = ifa->tstamp + lifetime * HZ;
10535b84efecSThomas Haller 		spin_unlock(&ifa->lock);
10545b84efecSThomas Haller 	}
10555b84efecSThomas Haller 
10565b84efecSThomas Haller 	return action;
10575b84efecSThomas Haller }
10585b84efecSThomas Haller 
10595b84efecSThomas Haller static void
10605b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
10615b84efecSThomas Haller {
10625b84efecSThomas Haller 	struct rt6_info *rt;
10635b84efecSThomas Haller 
10645b84efecSThomas Haller 	rt = addrconf_get_prefix_route(&ifp->addr,
10655b84efecSThomas Haller 				       ifp->prefix_len,
10665b84efecSThomas Haller 				       ifp->idev->dev,
10675b84efecSThomas Haller 				       0, RTF_GATEWAY | RTF_DEFAULT);
10685b84efecSThomas Haller 	if (rt) {
10695b84efecSThomas Haller 		if (del_rt)
10705b84efecSThomas Haller 			ip6_del_rt(rt);
10715b84efecSThomas Haller 		else {
10725b84efecSThomas Haller 			if (!(rt->rt6i_flags & RTF_EXPIRES))
10735b84efecSThomas Haller 				rt6_set_expires(rt, expires);
10745b84efecSThomas Haller 			ip6_rt_put(rt);
10755b84efecSThomas Haller 		}
10765b84efecSThomas Haller 	}
10775b84efecSThomas Haller }
10785b84efecSThomas Haller 
10795b84efecSThomas Haller 
10801da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
10831da177e4SLinus Torvalds {
10844c5ff6a6SHerbert Xu 	int state;
10855b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
10865b84efecSThomas Haller 	unsigned long expires;
10871da177e4SLinus Torvalds 
1088c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
1089c15b1ccaSHannes Frederic Sowa 
10908e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
10914c5ff6a6SHerbert Xu 	state = ifp->state;
1092e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
10938e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
10944c5ff6a6SHerbert Xu 
10954c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
10964c5ff6a6SHerbert Xu 		goto out;
10971da177e4SLinus Torvalds 
10985c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
10995c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
11005c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
11011da177e4SLinus Torvalds 
11025b84efecSThomas Haller 	write_lock_bh(&ifp->idev->lock);
11035d9efa7eSDavid S. Miller 
11041da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
1105372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
11061da177e4SLinus Torvalds 		if (ifp->ifpub) {
11071da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
11081da177e4SLinus Torvalds 			ifp->ifpub = NULL;
11091da177e4SLinus Torvalds 		}
11101da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
11111da177e4SLinus Torvalds 	}
11121da177e4SLinus Torvalds 
11135b84efecSThomas Haller 	if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
11145b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &expires);
11155b84efecSThomas Haller 
1116502a2ffdSstephen hemminger 	list_del_init(&ifp->if_list);
11171da177e4SLinus Torvalds 	__in6_ifa_put(ifp);
1118502a2ffdSstephen hemminger 
11195b84efecSThomas Haller 	write_unlock_bh(&ifp->idev->lock);
11201da177e4SLinus Torvalds 
1121c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
1122b2238566SAndrey Vagin 
11231da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
11241da177e4SLinus Torvalds 
1125f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
11261da177e4SLinus Torvalds 
11275b84efecSThomas Haller 	if (action != CLEANUP_PREFIX_RT_NOP) {
11285b84efecSThomas Haller 		cleanup_prefix_route(ifp, expires,
11295b84efecSThomas Haller 			action == CLEANUP_PREFIX_RT_DEL);
11301da177e4SLinus Torvalds 	}
11311da177e4SLinus Torvalds 
1132c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1133c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
11344c5ff6a6SHerbert Xu out:
11351da177e4SLinus Torvalds 	in6_ifa_put(ifp);
11361da177e4SLinus Torvalds }
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
11391da177e4SLinus Torvalds {
11401da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
11411da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
114276f793e3SLorenzo Colitti 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1143eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
11441da177e4SLinus Torvalds 	int tmp_plen;
11451da177e4SLinus Torvalds 	int ret = 0;
114695c385b4SNeil Horman 	u32 addr_flags;
114776f793e3SLorenzo Colitti 	unsigned long now = jiffies;
11481da177e4SLinus Torvalds 
114953bd6749SJiri Pirko 	write_lock_bh(&idev->lock);
11501da177e4SLinus Torvalds 	if (ift) {
11511da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
11521da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
11531da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
11541da177e4SLinus Torvalds 		tmpaddr = &addr;
11551da177e4SLinus Torvalds 	} else {
11561da177e4SLinus Torvalds 		tmpaddr = NULL;
11571da177e4SLinus Torvalds 	}
11581da177e4SLinus Torvalds retry:
11591da177e4SLinus Torvalds 	in6_dev_hold(idev);
11601da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
116153bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1162f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
11631da177e4SLinus Torvalds 		in6_dev_put(idev);
11641da177e4SLinus Torvalds 		ret = -1;
11651da177e4SLinus Torvalds 		goto out;
11661da177e4SLinus Torvalds 	}
11671da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
11681da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
11691da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
11701da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
117153bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1172f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1173f3213831SJoe Perches 			__func__);
11741da177e4SLinus Torvalds 		in6_dev_put(idev);
11751da177e4SLinus Torvalds 		ret = -1;
11761da177e4SLinus Torvalds 		goto out;
11771da177e4SLinus Torvalds 	}
11781da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
11791da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1180eb7e0575SSorin Dumitru 	__ipv6_try_regen_rndid(idev, tmpaddr);
11811da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
118276f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
11831da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
11841da177e4SLinus Torvalds 			      ifp->valid_lft,
11857a876b0eSGlenn Wurster 			      idev->cnf.temp_valid_lft + age);
11861da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
11871da177e4SLinus Torvalds 				 ifp->prefered_lft,
11887a876b0eSGlenn Wurster 				 idev->cnf.temp_prefered_lft + age -
1189784e2710SBen Hutchings 				 idev->cnf.max_desync_factor);
11901da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
11911da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
11921da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
11931da177e4SLinus Torvalds 
1194eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1195eac55bf9SBenoit Boissinot 			idev->cnf.dad_transmits *
11961f9248e5SJiri Pirko 			NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
119753bd6749SJiri Pirko 	write_unlock_bh(&idev->lock);
119895c385b4SNeil Horman 
1199eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1200eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1201eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1202eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1203ecab6701SHeiner Kallweit 	 * Use age calculation as in addrconf_verify to avoid unnecessary
1204ecab6701SHeiner Kallweit 	 * temporary addresses being generated.
1205eac55bf9SBenoit Boissinot 	 */
1206ecab6701SHeiner Kallweit 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1207ecab6701SHeiner Kallweit 	if (tmp_prefered_lft <= regen_advance + age) {
1208eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1209eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1210eac55bf9SBenoit Boissinot 		ret = -1;
1211eac55bf9SBenoit Boissinot 		goto out;
1212eac55bf9SBenoit Boissinot 	}
1213eac55bf9SBenoit Boissinot 
121495c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
121595c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
121695c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
121795c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
121895c385b4SNeil Horman 
12194b08a8f1SHannes Frederic Sowa 	ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
12208a226b2cSJiri Benc 			    ipv6_addr_scope(&addr), addr_flags,
12214b08a8f1SHannes Frederic Sowa 			    tmp_valid_lft, tmp_prefered_lft);
12224b08a8f1SHannes Frederic Sowa 	if (IS_ERR(ift)) {
12231da177e4SLinus Torvalds 		in6_ifa_put(ifp);
12241da177e4SLinus Torvalds 		in6_dev_put(idev);
1225f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
12261da177e4SLinus Torvalds 		tmpaddr = &addr;
122753bd6749SJiri Pirko 		write_lock_bh(&idev->lock);
12281da177e4SLinus Torvalds 		goto retry;
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
12321da177e4SLinus Torvalds 	ift->ifpub = ifp;
123376f793e3SLorenzo Colitti 	ift->cstamp = now;
12341da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
12351da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
12361da177e4SLinus Torvalds 
1237cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
12381da177e4SLinus Torvalds 	in6_ifa_put(ift);
12391da177e4SLinus Torvalds 	in6_dev_put(idev);
12401da177e4SLinus Torvalds out:
12411da177e4SLinus Torvalds 	return ret;
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds /*
1245072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
12461da177e4SLinus Torvalds  */
1247a9b05723SYOSHIFUJI Hideaki enum {
1248a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1249a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1250a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1251a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1252a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1253a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1254a9b05723SYOSHIFUJI Hideaki #endif
1255a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1256a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1257a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1258a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1259a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
12607fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
12617fd2561eSErik Kline 	IPV6_SADDR_RULE_NOT_OPTIMISTIC,
12627fd2561eSErik Kline #endif
1263a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1264072047e4SYOSHIFUJI Hideaki };
1265072047e4SYOSHIFUJI Hideaki 
1266a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1267a9b05723SYOSHIFUJI Hideaki 	int			rule;
1268a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1269a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1270a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1271a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1272a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1273a9b05723SYOSHIFUJI Hideaki };
1274a9b05723SYOSHIFUJI Hideaki 
1275a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
12769acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1277a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1278a9b05723SYOSHIFUJI Hideaki 	int scope;
1279a9b05723SYOSHIFUJI Hideaki 	int label;
12807cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1281a9b05723SYOSHIFUJI Hideaki };
1282072047e4SYOSHIFUJI Hideaki 
1283b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
12841da177e4SLinus Torvalds {
128545bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1286072047e4SYOSHIFUJI Hideaki 		return 1;
1287072047e4SYOSHIFUJI Hideaki 	return 0;
12881da177e4SLinus Torvalds }
12891da177e4SLinus Torvalds 
12907fd2561eSErik Kline static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
12917fd2561eSErik Kline {
12927fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
12937fd2561eSErik Kline 	return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
12947fd2561eSErik Kline #else
12957fd2561eSErik Kline 	return false;
12967fd2561eSErik Kline #endif
12977fd2561eSErik Kline }
12987fd2561eSErik Kline 
12993de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
13003de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1301a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1302a9b05723SYOSHIFUJI Hideaki 			       int i)
1303a9b05723SYOSHIFUJI Hideaki {
1304a9b05723SYOSHIFUJI Hideaki 	int ret;
1305a9b05723SYOSHIFUJI Hideaki 
1306a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1307a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1308a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1309a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1310a9b05723SYOSHIFUJI Hideaki 			break;
1311a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1312a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1313a9b05723SYOSHIFUJI Hideaki 			break;
1314a9b05723SYOSHIFUJI Hideaki 		default:
1315a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1316a9b05723SYOSHIFUJI Hideaki 		}
1317a9b05723SYOSHIFUJI Hideaki 		goto out;
1318a9b05723SYOSHIFUJI Hideaki 	}
1319a9b05723SYOSHIFUJI Hideaki 
1320a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1321a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1322a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1323a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1324a9b05723SYOSHIFUJI Hideaki 		break;
1325a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1326a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1327a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1328a9b05723SYOSHIFUJI Hideaki 		break;
1329a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1330a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1331a9b05723SYOSHIFUJI Hideaki 		 *
1332a9b05723SYOSHIFUJI Hideaki 		 *      ret
1333a9b05723SYOSHIFUJI Hideaki 		 *       ^
1334a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1335a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1336a9b05723SYOSHIFUJI Hideaki 		 *       |
1337a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1338a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1339a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1340db9c7c39Sstephen hemminger 		 *  B-15 |    \        if scope is enough for destination.
1341a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1342a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1343a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1344a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1345a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1346a9b05723SYOSHIFUJI Hideaki 		 *
1347a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1348a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1349a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1350a9b05723SYOSHIFUJI Hideaki 		 */
1351a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1352a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1353a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1354a9b05723SYOSHIFUJI Hideaki 		else
1355a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1356a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1357a9b05723SYOSHIFUJI Hideaki 		break;
1358a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
13597fd2561eSErik Kline 	    {
1360a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
13617fd2561eSErik Kline 		u8 avoid = IFA_F_DEPRECATED;
13627fd2561eSErik Kline 
13637fd2561eSErik Kline 		if (!ipv6_use_optimistic_addr(score->ifa->idev))
13647fd2561eSErik Kline 			avoid |= IFA_F_OPTIMISTIC;
1365a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
13667fd2561eSErik Kline 		      !(score->ifa->flags & avoid);
1367a9b05723SYOSHIFUJI Hideaki 		break;
13687fd2561eSErik Kline 	    }
1369a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1370a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
13717cbca67cSYOSHIFUJI Hideaki 	    {
1372a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
13737cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
13747cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1375a9b05723SYOSHIFUJI Hideaki 		break;
13767cbca67cSYOSHIFUJI Hideaki 	    }
1377a9b05723SYOSHIFUJI Hideaki #endif
1378a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1379a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1380a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1381a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1382a9b05723SYOSHIFUJI Hideaki 		break;
1383a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1384a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
13853de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
13863de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1387a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1388a9b05723SYOSHIFUJI Hideaki 		break;
1389a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
13907cbca67cSYOSHIFUJI Hideaki 	    {
1391a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
139225985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1393a9b05723SYOSHIFUJI Hideaki 		 */
13947cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
13957cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
13967cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
13977cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1398a9b05723SYOSHIFUJI Hideaki 		break;
13997cbca67cSYOSHIFUJI Hideaki 	    }
1400a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1401a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1402a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1403a9b05723SYOSHIFUJI Hideaki 		 */
1404a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1405a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1406a9b05723SYOSHIFUJI Hideaki 		break;
1407a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1408a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
140991b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
141091b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
141191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
141291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1413a9b05723SYOSHIFUJI Hideaki 		break;
14147fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
14157fd2561eSErik Kline 	case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
14167fd2561eSErik Kline 		/* Optimistic addresses still have lower precedence than other
14177fd2561eSErik Kline 		 * preferred addresses.
14187fd2561eSErik Kline 		 */
14197fd2561eSErik Kline 		ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
14207fd2561eSErik Kline 		break;
14217fd2561eSErik Kline #endif
1422a9b05723SYOSHIFUJI Hideaki 	default:
1423a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1424a9b05723SYOSHIFUJI Hideaki 	}
1425a9b05723SYOSHIFUJI Hideaki 
1426a9b05723SYOSHIFUJI Hideaki 	if (ret)
1427a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1428a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1429a9b05723SYOSHIFUJI Hideaki out:
1430a9b05723SYOSHIFUJI Hideaki 	return ret;
1431a9b05723SYOSHIFUJI Hideaki }
1432a9b05723SYOSHIFUJI Hideaki 
1433c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net,
14349131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_dst *dst,
14359131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct inet6_dev *idev,
1436c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_score *scores,
1437c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				int hiscore_idx)
14381da177e4SLinus Torvalds {
1439c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1440072047e4SYOSHIFUJI Hideaki 
14411da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1442502a2ffdSstephen hemminger 	list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1443a9b05723SYOSHIFUJI Hideaki 		int i;
1444072047e4SYOSHIFUJI Hideaki 
1445a9b05723SYOSHIFUJI Hideaki 		/*
14466b3ae80aSYOSHIFUJI Hideaki 		 * - Tentative Address (RFC2462 section 5.4)
14476b3ae80aSYOSHIFUJI Hideaki 		 *  - A tentative address is not considered
14486b3ae80aSYOSHIFUJI Hideaki 		 *    "assigned to an interface" in the traditional
144995c385b4SNeil Horman 		 *    sense, unless it is also flagged as optimistic.
14506b3ae80aSYOSHIFUJI Hideaki 		 * - Candidate Source Address (section 4)
1451072047e4SYOSHIFUJI Hideaki 		 *  - In any case, anycast addresses, multicast
1452072047e4SYOSHIFUJI Hideaki 		 *    addresses, and the unspecified address MUST
1453072047e4SYOSHIFUJI Hideaki 		 *    NOT be included in a candidate set.
1454072047e4SYOSHIFUJI Hideaki 		 */
1455a9b05723SYOSHIFUJI Hideaki 		if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1456a9b05723SYOSHIFUJI Hideaki 		    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
14576b3ae80aSYOSHIFUJI Hideaki 			continue;
1458a9b05723SYOSHIFUJI Hideaki 
1459a9b05723SYOSHIFUJI Hideaki 		score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1460a9b05723SYOSHIFUJI Hideaki 
1461a9b05723SYOSHIFUJI Hideaki 		if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1462a9b05723SYOSHIFUJI Hideaki 			     score->addr_type & IPV6_ADDR_MULTICAST)) {
1463ba7a46f1SJoe Perches 			net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
14649131f3deSYOSHIFUJI Hideaki/吉藤英明 					    idev->dev->name);
14651da177e4SLinus Torvalds 			continue;
1466072047e4SYOSHIFUJI Hideaki 		}
1467072047e4SYOSHIFUJI Hideaki 
1468a9b05723SYOSHIFUJI Hideaki 		score->rule = -1;
1469a9b05723SYOSHIFUJI Hideaki 		bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1470072047e4SYOSHIFUJI Hideaki 
1471a9b05723SYOSHIFUJI Hideaki 		for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1472a9b05723SYOSHIFUJI Hideaki 			int minihiscore, miniscore;
1473072047e4SYOSHIFUJI Hideaki 
14749131f3deSYOSHIFUJI Hideaki/吉藤英明 			minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
14759131f3deSYOSHIFUJI Hideaki/吉藤英明 			miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1476072047e4SYOSHIFUJI Hideaki 
1477a9b05723SYOSHIFUJI Hideaki 			if (minihiscore > miniscore) {
1478a9b05723SYOSHIFUJI Hideaki 				if (i == IPV6_SADDR_RULE_SCOPE &&
1479a9b05723SYOSHIFUJI Hideaki 				    score->scopedist > 0) {
1480a9b05723SYOSHIFUJI Hideaki 					/*
1481a9b05723SYOSHIFUJI Hideaki 					 * special case:
1482a9b05723SYOSHIFUJI Hideaki 					 * each remaining entry
1483a9b05723SYOSHIFUJI Hideaki 					 * has too small (not enough)
1484a9b05723SYOSHIFUJI Hideaki 					 * scope, because ifa entries
1485a9b05723SYOSHIFUJI Hideaki 					 * are sorted by their scope
1486a9b05723SYOSHIFUJI Hideaki 					 * values.
1487072047e4SYOSHIFUJI Hideaki 					 */
14889131f3deSYOSHIFUJI Hideaki/吉藤英明 					goto out;
1489072047e4SYOSHIFUJI Hideaki 				}
1490a9b05723SYOSHIFUJI Hideaki 				break;
1491a9b05723SYOSHIFUJI Hideaki 			} else if (minihiscore < miniscore) {
1492a9b05723SYOSHIFUJI Hideaki 				if (hiscore->ifa)
1493a9b05723SYOSHIFUJI Hideaki 					in6_ifa_put(hiscore->ifa);
149499cd07a5SJuha-Matti Tapio 
1495a9b05723SYOSHIFUJI Hideaki 				in6_ifa_hold(score->ifa);
1496072047e4SYOSHIFUJI Hideaki 
1497a0bffffcSIlpo Järvinen 				swap(hiscore, score);
1498c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				hiscore_idx = 1 - hiscore_idx;
1499a9b05723SYOSHIFUJI Hideaki 
1500a9b05723SYOSHIFUJI Hideaki 				/* restore our iterator */
1501a9b05723SYOSHIFUJI Hideaki 				score->ifa = hiscore->ifa;
1502a9b05723SYOSHIFUJI Hideaki 
1503a9b05723SYOSHIFUJI Hideaki 				break;
15041da177e4SLinus Torvalds 			}
1505a9b05723SYOSHIFUJI Hideaki 		}
1506a9b05723SYOSHIFUJI Hideaki 	}
15079131f3deSYOSHIFUJI Hideaki/吉藤英明 out:
15081da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
1509c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	return hiscore_idx;
15101da177e4SLinus Torvalds }
15119131f3deSYOSHIFUJI Hideaki/吉藤英明 
15129131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
15139131f3deSYOSHIFUJI Hideaki/吉藤英明 		       const struct in6_addr *daddr, unsigned int prefs,
15149131f3deSYOSHIFUJI Hideaki/吉藤英明 		       struct in6_addr *saddr)
15159131f3deSYOSHIFUJI Hideaki/吉藤英明 {
1516c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score scores[2], *hiscore;
15179131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_dst dst;
15189131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct inet6_dev *idev;
15199131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct net_device *dev;
15209131f3deSYOSHIFUJI Hideaki/吉藤英明 	int dst_type;
15219131f3deSYOSHIFUJI Hideaki/吉藤英明 	bool use_oif_addr = false;
1522c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	int hiscore_idx = 0;
15239131f3deSYOSHIFUJI Hideaki/吉藤英明 
15249131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst_type = __ipv6_addr_type(daddr);
15259131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.addr = daddr;
15269131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
15279131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.scope = __ipv6_addr_src_scope(dst_type);
15289131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
15299131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.prefs = prefs;
15309131f3deSYOSHIFUJI Hideaki/吉藤英明 
1531c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].rule = -1;
1532c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].ifa = NULL;
15339131f3deSYOSHIFUJI Hideaki/吉藤英明 
15349131f3deSYOSHIFUJI Hideaki/吉藤英明 	rcu_read_lock();
15359131f3deSYOSHIFUJI Hideaki/吉藤英明 
15369131f3deSYOSHIFUJI Hideaki/吉藤英明 	/* Candidate Source Address (section 4)
15379131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *  - multicast and link-local destination address,
15389131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    the set of candidate source address MUST only
15399131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
15409131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same link as the outgoing
15419131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.
15429131f3deSYOSHIFUJI Hideaki/吉藤英明 	 * (- For site-local destination addresses, the
15439131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    set of candidate source addresses MUST only
15449131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
15459131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same site as the outgoing
15469131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.)
15473985e8a3SErik Kline 	 *  - "It is RECOMMENDED that the candidate source addresses
15483985e8a3SErik Kline 	 *    be the set of unicast addresses assigned to the
15493985e8a3SErik Kline 	 *    interface that will be used to send to the destination
15503985e8a3SErik Kline 	 *    (the 'outgoing' interface)." (RFC 6724)
15519131f3deSYOSHIFUJI Hideaki/吉藤英明 	 */
15529131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (dst_dev) {
15539131f3deSYOSHIFUJI Hideaki/吉藤英明 		idev = __in6_dev_get(dst_dev);
15543985e8a3SErik Kline 		if ((dst_type & IPV6_ADDR_MULTICAST) ||
15553985e8a3SErik Kline 		    dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
15563985e8a3SErik Kline 		    (idev && idev->cnf.use_oif_addrs_only)) {
15579131f3deSYOSHIFUJI Hideaki/吉藤英明 			use_oif_addr = true;
15589131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
15599131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
15609131f3deSYOSHIFUJI Hideaki/吉藤英明 
15619131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (use_oif_addr) {
1562c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 		if (idev)
1563c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
15649131f3deSYOSHIFUJI Hideaki/吉藤英明 	} else {
15659131f3deSYOSHIFUJI Hideaki/吉藤英明 		for_each_netdev_rcu(net, dev) {
15669131f3deSYOSHIFUJI Hideaki/吉藤英明 			idev = __in6_dev_get(dev);
15679131f3deSYOSHIFUJI Hideaki/吉藤英明 			if (!idev)
15689131f3deSYOSHIFUJI Hideaki/吉藤英明 				continue;
1569c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
15709131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
15719131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
15728814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
15731da177e4SLinus Torvalds 
1574c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	hiscore = &scores[hiscore_idx];
1575a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1576072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
15771da177e4SLinus Torvalds 
15784e3fd7a0SAlexey Dobriyan 	*saddr = hiscore->ifa->addr;
1579a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1580072047e4SYOSHIFUJI Hideaki 	return 0;
15811da177e4SLinus Torvalds }
15825e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
15831da177e4SLinus Torvalds 
15848965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1585479840ffSJiri Pirko 		      u32 banned_flags)
1586b7b1bfceSHannes Frederic Sowa {
1587b7b1bfceSHannes Frederic Sowa 	struct inet6_ifaddr *ifp;
1588b7b1bfceSHannes Frederic Sowa 	int err = -EADDRNOTAVAIL;
1589b7b1bfceSHannes Frederic Sowa 
1590602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1591602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1592602582caSHannes Frederic Sowa 			break;
1593b7b1bfceSHannes Frederic Sowa 		if (ifp->scope == IFA_LINK &&
1594b7b1bfceSHannes Frederic Sowa 		    !(ifp->flags & banned_flags)) {
1595b7b1bfceSHannes Frederic Sowa 			*addr = ifp->addr;
1596b7b1bfceSHannes Frederic Sowa 			err = 0;
1597b7b1bfceSHannes Frederic Sowa 			break;
1598b7b1bfceSHannes Frederic Sowa 		}
1599b7b1bfceSHannes Frederic Sowa 	}
1600b7b1bfceSHannes Frederic Sowa 	return err;
1601b7b1bfceSHannes Frederic Sowa }
1602b7b1bfceSHannes Frederic Sowa 
160395c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1604479840ffSJiri Pirko 		    u32 banned_flags)
16051da177e4SLinus Torvalds {
16061da177e4SLinus Torvalds 	struct inet6_dev *idev;
16071da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
16081da177e4SLinus Torvalds 
16098814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1610e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1611e21e8467SStephen Hemminger 	if (idev) {
16121da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1613b7b1bfceSHannes Frederic Sowa 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
16141da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
16151da177e4SLinus Torvalds 	}
16168814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
16171da177e4SLinus Torvalds 	return err;
16181da177e4SLinus Torvalds }
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
16211da177e4SLinus Torvalds {
16221da177e4SLinus Torvalds 	int cnt = 0;
16231da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1626502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list)
16271da177e4SLinus Torvalds 		cnt++;
16281da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
16291da177e4SLinus Torvalds 	return cnt;
16301da177e4SLinus Torvalds }
16311da177e4SLinus Torvalds 
1632b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
16332a7851bfSFlorian Westphal 		  const struct net_device *dev, int strict)
16341da177e4SLinus Torvalds {
1635c58da4c6SErik Kline 	return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1636c58da4c6SErik Kline }
1637c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr);
1638c58da4c6SErik Kline 
1639c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1640c58da4c6SErik Kline 			    const struct net_device *dev, int strict,
1641c58da4c6SErik Kline 			    u32 banned_flags)
1642c58da4c6SErik Kline {
1643eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1644ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
1645c58da4c6SErik Kline 	u32 ifp_flags;
16461da177e4SLinus Torvalds 
16475c578aedSstephen hemminger 	rcu_read_lock_bh();
1648b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1649878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1650bfeade08SDaniel Lezcano 			continue;
1651c58da4c6SErik Kline 		/* Decouple optimistic from tentative for evaluation here.
1652c58da4c6SErik Kline 		 * Ban optimistic addresses explicitly, when required.
1653c58da4c6SErik Kline 		 */
1654c58da4c6SErik Kline 		ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1655c58da4c6SErik Kline 			    ? (ifp->flags&~IFA_F_TENTATIVE)
1656c58da4c6SErik Kline 			    : ifp->flags;
16571da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1658c58da4c6SErik Kline 		    !(ifp_flags&banned_flags) &&
165963159f29SIan Morris 		    (!dev || ifp->idev->dev == dev ||
1660eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
16615c578aedSstephen hemminger 			rcu_read_unlock_bh();
1662eedf042aSStephen Hemminger 			return 1;
1663eedf042aSStephen Hemminger 		}
1664eedf042aSStephen Hemminger 	}
16655c578aedSstephen hemminger 
1666eedf042aSStephen Hemminger 	rcu_read_unlock_bh();
1667eedf042aSStephen Hemminger 	return 0;
16681da177e4SLinus Torvalds }
1669c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
16707159039aSYOSHIFUJI Hideaki 
16713e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
167206bfe655SDaniel Lezcano 			       struct net_device *dev)
16731da177e4SLinus Torvalds {
1674ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
16751da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
16761da177e4SLinus Torvalds 
1677b67bfe0dSSasha Levin 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1678878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
167906bfe655SDaniel Lezcano 			continue;
16801da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
168163159f29SIan Morris 			if (!dev || ifp->idev->dev == dev)
16823e81c6daSDavid S. Miller 				return true;
16831da177e4SLinus Torvalds 		}
16841da177e4SLinus Torvalds 	}
16853e81c6daSDavid S. Miller 	return false;
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds 
16887df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev.
16897df37ff3SCatalin\(ux\) M. BOIE  * If one is found it returns true.
16907df37ff3SCatalin\(ux\) M. BOIE  */
16917df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
16927df37ff3SCatalin\(ux\) M. BOIE 	const unsigned int prefix_len, struct net_device *dev)
16937df37ff3SCatalin\(ux\) M. BOIE {
16947df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_dev *idev;
16957df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_ifaddr *ifa;
16967df37ff3SCatalin\(ux\) M. BOIE 	bool ret = false;
16977df37ff3SCatalin\(ux\) M. BOIE 
16987df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_lock();
16997df37ff3SCatalin\(ux\) M. BOIE 	idev = __in6_dev_get(dev);
17007df37ff3SCatalin\(ux\) M. BOIE 	if (idev) {
17017df37ff3SCatalin\(ux\) M. BOIE 		read_lock_bh(&idev->lock);
17027df37ff3SCatalin\(ux\) M. BOIE 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
17037df37ff3SCatalin\(ux\) M. BOIE 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
17047df37ff3SCatalin\(ux\) M. BOIE 			if (ret)
17057df37ff3SCatalin\(ux\) M. BOIE 				break;
17067df37ff3SCatalin\(ux\) M. BOIE 		}
17077df37ff3SCatalin\(ux\) M. BOIE 		read_unlock_bh(&idev->lock);
17087df37ff3SCatalin\(ux\) M. BOIE 	}
17097df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_unlock();
17107df37ff3SCatalin\(ux\) M. BOIE 
17117df37ff3SCatalin\(ux\) M. BOIE 	return ret;
17127df37ff3SCatalin\(ux\) M. BOIE }
17137df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix);
17147df37ff3SCatalin\(ux\) M. BOIE 
1715b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
171652eeeb84SYOSHIFUJI Hideaki {
171752eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
171852eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
171952eeeb84SYOSHIFUJI Hideaki 	int	onlink;
172052eeeb84SYOSHIFUJI Hideaki 
172152eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
172252eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
172352eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
172452eeeb84SYOSHIFUJI Hideaki 	if (idev) {
172552eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
1726502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
172752eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
172852eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
172952eeeb84SYOSHIFUJI Hideaki 			if (onlink)
173052eeeb84SYOSHIFUJI Hideaki 				break;
173152eeeb84SYOSHIFUJI Hideaki 		}
173252eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
173352eeeb84SYOSHIFUJI Hideaki 	}
173452eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
173552eeeb84SYOSHIFUJI Hideaki 	return onlink;
173652eeeb84SYOSHIFUJI Hideaki }
173752eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
173852eeeb84SYOSHIFUJI Hideaki 
17399acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
17401cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
17411da177e4SLinus Torvalds {
1742b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
1743ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
17441da177e4SLinus Torvalds 
17455c578aedSstephen hemminger 	rcu_read_lock_bh();
1746b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1747878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
17481cab3da6SDaniel Lezcano 			continue;
17491da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
175063159f29SIan Morris 			if (!dev || ifp->idev->dev == dev ||
17511da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1752b79d1d54SDavid S. Miller 				result = ifp;
17531da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
17541da177e4SLinus Torvalds 				break;
17551da177e4SLinus Torvalds 			}
17561da177e4SLinus Torvalds 		}
17571da177e4SLinus Torvalds 	}
17585c578aedSstephen hemminger 	rcu_read_unlock_bh();
17591da177e4SLinus Torvalds 
1760b79d1d54SDavid S. Miller 	return result;
17611da177e4SLinus Torvalds }
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
17641da177e4SLinus Torvalds 
1765cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
17661da177e4SLinus Torvalds {
17671da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
17681da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
1769c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifp);
17701da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
1771cc411d0bSBrian Haley 		if (dad_failed)
1772cc411d0bSBrian Haley 			ifp->flags |= IFA_F_DADFAILED;
17731da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
1774e2577a06SHerbert Xu 		if (dad_failed)
1775e2577a06SHerbert Xu 			ipv6_ifa_notify(0, ifp);
17761da177e4SLinus Torvalds 		in6_ifa_put(ifp);
17771da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
17781da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
17791da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
17801da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
17811da177e4SLinus Torvalds 		if (ifpub) {
17821da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
17831da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
17841da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
17851da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
17861da177e4SLinus Torvalds 		} else {
17871da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
17881da177e4SLinus Torvalds 		}
17891da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
1790c15b1ccaSHannes Frederic Sowa 	} else {
17911da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
17921da177e4SLinus Torvalds 	}
1793c15b1ccaSHannes Frederic Sowa }
17941da177e4SLinus Torvalds 
1795f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1796f2344a13SHerbert Xu {
1797f2344a13SHerbert Xu 	int err = -ENOENT;
1798f2344a13SHerbert Xu 
17998e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
1800f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
1801f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
1802f2344a13SHerbert Xu 		err = 0;
1803f2344a13SHerbert Xu 	}
18048e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
1805f2344a13SHerbert Xu 
1806f2344a13SHerbert Xu 	return err;
1807f2344a13SHerbert Xu }
1808f2344a13SHerbert Xu 
18093c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
18103c21edbdSYOSHIFUJI Hideaki {
18115f40ef77SHannes Frederic Sowa 	struct in6_addr addr;
18121b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
18131855b7c3SHannes Frederic Sowa 	struct net *net = dev_net(ifp->idev->dev);
18149bdd8d40SBrian Haley 
1815853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
1816853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
1817f2344a13SHerbert Xu 		return;
1818853dc2e0SUrsula Braun 	}
1819f2344a13SHerbert Xu 
1820e87cc472SJoe Perches 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
18210522fea6SJens Rosenboom 			     ifp->idev->dev->name, &ifp->addr);
18229bdd8d40SBrian Haley 
18235f40ef77SHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
18241b34be74SYOSHIFUJI Hideaki 
18255f40ef77SHannes Frederic Sowa 	if (ifp->flags & IFA_F_STABLE_PRIVACY) {
18265f40ef77SHannes Frederic Sowa 		int scope = ifp->scope;
18275f40ef77SHannes Frederic Sowa 		u32 flags = ifp->flags;
18285f40ef77SHannes Frederic Sowa 		struct in6_addr new_addr;
18295f40ef77SHannes Frederic Sowa 		struct inet6_ifaddr *ifp2;
18305f40ef77SHannes Frederic Sowa 		u32 valid_lft, preferred_lft;
18315f40ef77SHannes Frederic Sowa 		int pfxlen = ifp->prefix_len;
18325f40ef77SHannes Frederic Sowa 		int retries = ifp->stable_privacy_retry + 1;
18335f40ef77SHannes Frederic Sowa 
18341855b7c3SHannes Frederic Sowa 		if (retries > net->ipv6.sysctl.idgen_retries) {
18355f40ef77SHannes Frederic Sowa 			net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
18365f40ef77SHannes Frederic Sowa 					     ifp->idev->dev->name);
18375f40ef77SHannes Frederic Sowa 			goto errdad;
18385f40ef77SHannes Frederic Sowa 		}
18395f40ef77SHannes Frederic Sowa 
18405f40ef77SHannes Frederic Sowa 		new_addr = ifp->addr;
18415f40ef77SHannes Frederic Sowa 		if (ipv6_generate_stable_address(&new_addr, retries,
18425f40ef77SHannes Frederic Sowa 						 idev))
18435f40ef77SHannes Frederic Sowa 			goto errdad;
18445f40ef77SHannes Frederic Sowa 
18455f40ef77SHannes Frederic Sowa 		valid_lft = ifp->valid_lft;
18465f40ef77SHannes Frederic Sowa 		preferred_lft = ifp->prefered_lft;
18475f40ef77SHannes Frederic Sowa 
18485f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp->lock);
18495f40ef77SHannes Frederic Sowa 
18505f40ef77SHannes Frederic Sowa 		if (idev->cnf.max_addresses &&
18515f40ef77SHannes Frederic Sowa 		    ipv6_count_addresses(idev) >=
18525f40ef77SHannes Frederic Sowa 		    idev->cnf.max_addresses)
18535f40ef77SHannes Frederic Sowa 			goto lock_errdad;
18545f40ef77SHannes Frederic Sowa 
18555f40ef77SHannes Frederic Sowa 		net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
18565f40ef77SHannes Frederic Sowa 				     ifp->idev->dev->name);
18575f40ef77SHannes Frederic Sowa 
18585f40ef77SHannes Frederic Sowa 		ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
18595f40ef77SHannes Frederic Sowa 				     scope, flags, valid_lft,
18605f40ef77SHannes Frederic Sowa 				     preferred_lft);
18615f40ef77SHannes Frederic Sowa 		if (IS_ERR(ifp2))
18625f40ef77SHannes Frederic Sowa 			goto lock_errdad;
18635f40ef77SHannes Frederic Sowa 
18645f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp2->lock);
18655f40ef77SHannes Frederic Sowa 		ifp2->stable_privacy_retry = retries;
18665f40ef77SHannes Frederic Sowa 		ifp2->state = INET6_IFADDR_STATE_PREDAD;
18675f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp2->lock);
18685f40ef77SHannes Frederic Sowa 
18691855b7c3SHannes Frederic Sowa 		addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
18705f40ef77SHannes Frederic Sowa 		in6_ifa_put(ifp2);
18715f40ef77SHannes Frederic Sowa lock_errdad:
18725f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp->lock);
18735f40ef77SHannes Frederic Sowa 	} else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
18741b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[0] = htonl(0xfe800000);
18751b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[1] = 0;
18761b34be74SYOSHIFUJI Hideaki 
18771b34be74SYOSHIFUJI Hideaki 		if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
18781b34be74SYOSHIFUJI Hideaki 		    ipv6_addr_equal(&ifp->addr, &addr)) {
18791b34be74SYOSHIFUJI Hideaki 			/* DAD failed for link-local based on MAC address */
18801b34be74SYOSHIFUJI Hideaki 			idev->cnf.disable_ipv6 = 1;
18819bdd8d40SBrian Haley 
1882f3213831SJoe Perches 			pr_info("%s: IPv6 being disabled!\n",
18839bdd8d40SBrian Haley 				ifp->idev->dev->name);
18841b34be74SYOSHIFUJI Hideaki 		}
18851b34be74SYOSHIFUJI Hideaki 	}
18861b34be74SYOSHIFUJI Hideaki 
18875f40ef77SHannes Frederic Sowa errdad:
1888c15b1ccaSHannes Frederic Sowa 	/* transition from _POSTDAD to _ERRDAD */
1889c15b1ccaSHannes Frederic Sowa 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
18908e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
1891c15b1ccaSHannes Frederic Sowa 
1892c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, 0);
18933c21edbdSYOSHIFUJI Hideaki }
18941da177e4SLinus Torvalds 
1895a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group.
1896a9ed4a29SSabrina Dubroca  * caller must hold RTNL */
1897b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
18981da177e4SLinus Torvalds {
18991da177e4SLinus Torvalds 	struct in6_addr maddr;
19001da177e4SLinus Torvalds 
19011da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
19021da177e4SLinus Torvalds 		return;
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
19051da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
19061da177e4SLinus Torvalds }
19071da177e4SLinus Torvalds 
1908a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
1909b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
19101da177e4SLinus Torvalds {
19111da177e4SLinus Torvalds 	struct in6_addr maddr;
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
19141da177e4SLinus Torvalds 		return;
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
19171da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
19181da177e4SLinus Torvalds }
19191da177e4SLinus Torvalds 
1920a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
192120380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
19221da177e4SLinus Torvalds {
19231da177e4SLinus Torvalds 	struct in6_addr addr;
1924c15b1ccaSHannes Frederic Sowa 
192588ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
19262bda8a0cSBjørn Mork 		return;
19271da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
19281da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
19291da177e4SLinus Torvalds 		return;
1930013b4d90SWANG Cong 	__ipv6_dev_ac_inc(ifp->idev, &addr);
19311da177e4SLinus Torvalds }
19321da177e4SLinus Torvalds 
1933a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
193420380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
19351da177e4SLinus Torvalds {
19361da177e4SLinus Torvalds 	struct in6_addr addr;
1937c15b1ccaSHannes Frederic Sowa 
193888ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
193932019e65SYOSHIFUJI Hideaki 		return;
19401da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
19411da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
19421da177e4SLinus Torvalds 		return;
19431da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
19441da177e4SLinus Torvalds }
19451da177e4SLinus Torvalds 
1946073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
19471da177e4SLinus Torvalds {
19481da177e4SLinus Torvalds 	if (dev->addr_len != ETH_ALEN)
19491da177e4SLinus Torvalds 		return -1;
19501da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr, 3);
19511da177e4SLinus Torvalds 	memcpy(eui + 5, dev->dev_addr + 3, 3);
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds 	/*
19541da177e4SLinus Torvalds 	 * The zSeries OSA network cards can be shared among various
19551da177e4SLinus Torvalds 	 * OS instances, but the OSA cards have only one MAC address.
19561da177e4SLinus Torvalds 	 * This leads to duplicate address conflicts in conjunction
19571da177e4SLinus Torvalds 	 * with IPv6 if more than one instance uses the same card.
19581da177e4SLinus Torvalds 	 *
19591da177e4SLinus Torvalds 	 * The driver for these cards can deliver a unique 16-bit
19601da177e4SLinus Torvalds 	 * identifier for each instance sharing the same card.  It is
19611da177e4SLinus Torvalds 	 * placed instead of 0xFFFE in the interface identifier.  The
19621da177e4SLinus Torvalds 	 * "u" bit of the interface identifier is not inverted in this
19631da177e4SLinus Torvalds 	 * case.  Hence the resulting interface identifier has local
19641da177e4SLinus Torvalds 	 * scope according to RFC2373.
19651da177e4SLinus Torvalds 	 */
19661da177e4SLinus Torvalds 	if (dev->dev_id) {
19671da177e4SLinus Torvalds 		eui[3] = (dev->dev_id >> 8) & 0xFF;
19681da177e4SLinus Torvalds 		eui[4] = dev->dev_id & 0xFF;
19691da177e4SLinus Torvalds 	} else {
19701da177e4SLinus Torvalds 		eui[3] = 0xFF;
19711da177e4SLinus Torvalds 		eui[4] = 0xFE;
19721da177e4SLinus Torvalds 		eui[0] ^= 2;
19731da177e4SLinus Torvalds 	}
19741da177e4SLinus Torvalds 	return 0;
1975073a8e0eSYOSHIFUJI Hideaki }
1976073a8e0eSYOSHIFUJI Hideaki 
197706a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
197806a4c1c5Salex.bluesman.smirnov@gmail.com {
197906a4c1c5Salex.bluesman.smirnov@gmail.com 	if (dev->addr_len != IEEE802154_ADDR_LEN)
198006a4c1c5Salex.bluesman.smirnov@gmail.com 		return -1;
198106a4c1c5Salex.bluesman.smirnov@gmail.com 	memcpy(eui, dev->dev_addr, 8);
19825e98a36eSYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
198306a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
198406a4c1c5Salex.bluesman.smirnov@gmail.com }
198506a4c1c5Salex.bluesman.smirnov@gmail.com 
1986cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1987cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
1988cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	union fwnet_hwaddr *ha;
1989cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1990cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
1991cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
1992cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1993cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	ha = (union fwnet_hwaddr *)dev->dev_addr;
1994cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1995cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1996cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
1997cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
1998cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
1999cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2000073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2001073a8e0eSYOSHIFUJI Hideaki {
20021da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
20031da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
20041da177e4SLinus Torvalds 		return -1;
20051da177e4SLinus Torvalds 	memset(eui, 0, 7);
20061da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
20071da177e4SLinus Torvalds 	return 0;
2008073a8e0eSYOSHIFUJI Hideaki }
2009073a8e0eSYOSHIFUJI Hideaki 
2010073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2011073a8e0eSYOSHIFUJI Hideaki {
20121da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
20131da177e4SLinus Torvalds 		return -1;
20141da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
20151da177e4SLinus Torvalds 	eui[0] |= 2;
20161da177e4SLinus Torvalds 	return 0;
20171da177e4SLinus Torvalds }
2018073a8e0eSYOSHIFUJI Hideaki 
2019c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2020dfd982baSYOSHIFUJI Hideaki {
20219af28511SSascha Hlusiak 	if (addr == 0)
20229af28511SSascha Hlusiak 		return -1;
2023dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2024dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2025dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2026dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2027dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2028dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2029dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
2030dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
2031dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
2032dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
2033dfd982baSYOSHIFUJI Hideaki 	return 0;
2034dfd982baSYOSHIFUJI Hideaki }
2035dfd982baSYOSHIFUJI Hideaki 
2036dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2037dfd982baSYOSHIFUJI Hideaki {
2038dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
2039dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2040dfd982baSYOSHIFUJI Hideaki 	return -1;
2041dfd982baSYOSHIFUJI Hideaki }
2042dfd982baSYOSHIFUJI Hideaki 
2043aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2044aee80b54Sstephen hemminger {
2045aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2046aee80b54Sstephen hemminger }
2047aee80b54Sstephen hemminger 
2048e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2049e837735eSNicolas Dichtel {
2050e837735eSNicolas Dichtel 	memcpy(eui, dev->perm_addr, 3);
2051e837735eSNicolas Dichtel 	memcpy(eui + 5, dev->perm_addr + 3, 3);
2052e837735eSNicolas Dichtel 	eui[3] = 0xFF;
2053e837735eSNicolas Dichtel 	eui[4] = 0xFE;
2054e837735eSNicolas Dichtel 	eui[0] ^= 2;
2055e837735eSNicolas Dichtel 	return 0;
2056e837735eSNicolas Dichtel }
2057e837735eSNicolas Dichtel 
2058073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2059073a8e0eSYOSHIFUJI Hideaki {
2060073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
2061073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
2062073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
2063073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
2064073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
2065073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
2066073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
2067073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
2068c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
2069dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
2070aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
2071aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
2072e74bccb8SJukka Rissanen 	case ARPHRD_6LOWPAN:
207306a4c1c5Salex.bluesman.smirnov@gmail.com 	case ARPHRD_IEEE802154:
207406a4c1c5Salex.bluesman.smirnov@gmail.com 		return addrconf_ifid_eui64(eui, dev);
2075cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
2076cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
2077e837735eSNicolas Dichtel 	case ARPHRD_TUNNEL6:
2078e837735eSNicolas Dichtel 		return addrconf_ifid_ip6tnl(eui, dev);
2079073a8e0eSYOSHIFUJI Hideaki 	}
20801da177e4SLinus Torvalds 	return -1;
20811da177e4SLinus Torvalds }
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
20841da177e4SLinus Torvalds {
20851da177e4SLinus Torvalds 	int err = -1;
20861da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2089602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2090602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
2091602582caSHannes Frederic Sowa 			break;
20921da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
20931da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
20941da177e4SLinus Torvalds 			err = 0;
20951da177e4SLinus Torvalds 			break;
20961da177e4SLinus Torvalds 		}
20971da177e4SLinus Torvalds 	}
20981da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
20991da177e4SLinus Torvalds 	return err;
21001da177e4SLinus Torvalds }
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2103eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev)
21041da177e4SLinus Torvalds {
21051da177e4SLinus Torvalds regen:
2106955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
21071da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 	/*
21101da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
21111da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
21121da177e4SLinus Torvalds 	 *
21131da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
21141da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
2115c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
21161da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
21171da177e4SLinus Torvalds 	 *  - value 0
21181da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
21191da177e4SLinus Torvalds 	 */
21201da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
21211da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
21221da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
21231da177e4SLinus Torvalds 		goto regen;
21241da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
21251da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
21261da177e4SLinus Torvalds 			goto regen;
21271da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
21281da177e4SLinus Torvalds 			goto regen;
21291da177e4SLinus Torvalds 	}
21301da177e4SLinus Torvalds }
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
21331da177e4SLinus Torvalds {
21341da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
21351da177e4SLinus Torvalds 	unsigned long expires;
21361da177e4SLinus Torvalds 
21378814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
21381da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	if (idev->dead)
21411da177e4SLinus Torvalds 		goto out;
21421da177e4SLinus Torvalds 
2143eb7e0575SSorin Dumitru 	__ipv6_regen_rndid(idev);
21441da177e4SLinus Torvalds 
21451da177e4SLinus Torvalds 	expires = jiffies +
21461da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
21471f9248e5SJiri Pirko 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
21481f9248e5SJiri Pirko 		NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
2149784e2710SBen Hutchings 		idev->cnf.max_desync_factor * HZ;
21501da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
2151f3213831SJoe Perches 		pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2152f3213831SJoe Perches 			__func__, idev->dev->name);
21531da177e4SLinus Torvalds 		goto out;
21541da177e4SLinus Torvalds 	}
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
21571da177e4SLinus Torvalds 		in6_dev_hold(idev);
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds out:
21601da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
21618814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
21621da177e4SLinus Torvalds 	in6_dev_put(idev);
21631da177e4SLinus Torvalds }
21641da177e4SLinus Torvalds 
2165eb7e0575SSorin Dumitru static void  __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
21668e5e8f30SEldad Zack {
21671da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2168eb7e0575SSorin Dumitru 		__ipv6_regen_rndid(idev);
21691da177e4SLinus Torvalds }
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds /*
21721da177e4SLinus Torvalds  *	Add prefix route.
21731da177e4SLinus Torvalds  */
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds static void
21761da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2177e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
21781da177e4SLinus Torvalds {
217986872cb5SThomas Graf 	struct fib6_config cfg = {
218086872cb5SThomas Graf 		.fc_table = RT6_TABLE_PREFIX,
218186872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
218286872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
218386872cb5SThomas Graf 		.fc_expires = expires,
218486872cb5SThomas Graf 		.fc_dst_len = plen,
218586872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
2186c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
2187f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
218886872cb5SThomas Graf 	};
21891da177e4SLinus Torvalds 
21904e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
21931da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
21941da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
21951da177e4SLinus Torvalds 	 */
219607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
21971da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
219886872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
21990be669bbSJoerg Roedel #endif
22001da177e4SLinus Torvalds 
220186872cb5SThomas Graf 	ip6_route_add(&cfg);
22021da177e4SLinus Torvalds }
22031da177e4SLinus Torvalds 
220414ef37b6SAndreas Hofmeister 
220514ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
220614ef37b6SAndreas Hofmeister 						  int plen,
220714ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
220814ef37b6SAndreas Hofmeister 						  u32 flags, u32 noflags)
220914ef37b6SAndreas Hofmeister {
221014ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
221114ef37b6SAndreas Hofmeister 	struct rt6_info *rt = NULL;
221214ef37b6SAndreas Hofmeister 	struct fib6_table *table;
221314ef37b6SAndreas Hofmeister 
221414ef37b6SAndreas Hofmeister 	table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
221563159f29SIan Morris 	if (!table)
221614ef37b6SAndreas Hofmeister 		return NULL;
221714ef37b6SAndreas Hofmeister 
22185744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
221914ef37b6SAndreas Hofmeister 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
222014ef37b6SAndreas Hofmeister 	if (!fn)
222114ef37b6SAndreas Hofmeister 		goto out;
22221f56a01fSMartin KaFai Lau 
22231f56a01fSMartin KaFai Lau 	noflags |= RTF_CACHE;
222414ef37b6SAndreas Hofmeister 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2225d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != dev->ifindex)
222614ef37b6SAndreas Hofmeister 			continue;
222714ef37b6SAndreas Hofmeister 		if ((rt->rt6i_flags & flags) != flags)
222814ef37b6SAndreas Hofmeister 			continue;
222985da53bfSRomain Kuntz 		if ((rt->rt6i_flags & noflags) != 0)
223014ef37b6SAndreas Hofmeister 			continue;
223114ef37b6SAndreas Hofmeister 		dst_hold(&rt->dst);
223214ef37b6SAndreas Hofmeister 		break;
223314ef37b6SAndreas Hofmeister 	}
223414ef37b6SAndreas Hofmeister out:
22355744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
223614ef37b6SAndreas Hofmeister 	return rt;
223714ef37b6SAndreas Hofmeister }
223814ef37b6SAndreas Hofmeister 
223914ef37b6SAndreas Hofmeister 
22401da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
22411da177e4SLinus Torvalds 
22421da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
22431da177e4SLinus Torvalds {
224486872cb5SThomas Graf 	struct fib6_config cfg = {
224586872cb5SThomas Graf 		.fc_table = RT6_TABLE_LOCAL,
224686872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
224786872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
224886872cb5SThomas Graf 		.fc_dst_len = 8,
224986872cb5SThomas Graf 		.fc_flags = RTF_UP,
2250c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
225186872cb5SThomas Graf 	};
22521da177e4SLinus Torvalds 
225386872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
225486872cb5SThomas Graf 
225586872cb5SThomas Graf 	ip6_route_add(&cfg);
22561da177e4SLinus Torvalds }
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
22591da177e4SLinus Torvalds {
22601da177e4SLinus Torvalds 	struct inet6_dev *idev;
22611da177e4SLinus Torvalds 
22621da177e4SLinus Torvalds 	ASSERT_RTNL();
22631da177e4SLinus Torvalds 
2264e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2265e21e8467SStephen Hemminger 	if (!idev)
226664e724f6SBrian Haley 		return ERR_PTR(-ENOBUFS);
226764e724f6SBrian Haley 
226864e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
226964e724f6SBrian Haley 		return ERR_PTR(-EACCES);
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds 	/* Add default multicast route */
22724af04abaSLi Wei 	if (!(dev->flags & IFF_LOOPBACK))
22731da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds 	return idev;
22761da177e4SLinus Torvalds }
22771da177e4SLinus Torvalds 
227853bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev,
227953bd6749SJiri Pirko 			     struct inet6_ifaddr *ifp,
228053bd6749SJiri Pirko 			     __u32 valid_lft, __u32 prefered_lft,
228153bd6749SJiri Pirko 			     bool create, unsigned long now)
228253bd6749SJiri Pirko {
228353bd6749SJiri Pirko 	u32 flags;
228453bd6749SJiri Pirko 	struct inet6_ifaddr *ift;
228553bd6749SJiri Pirko 
228653bd6749SJiri Pirko 	read_lock_bh(&idev->lock);
228753bd6749SJiri Pirko 	/* update all temporary addresses in the list */
228853bd6749SJiri Pirko 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
228953bd6749SJiri Pirko 		int age, max_valid, max_prefered;
229053bd6749SJiri Pirko 
229153bd6749SJiri Pirko 		if (ifp != ift->ifpub)
229253bd6749SJiri Pirko 			continue;
229353bd6749SJiri Pirko 
229453bd6749SJiri Pirko 		/* RFC 4941 section 3.3:
229553bd6749SJiri Pirko 		 * If a received option will extend the lifetime of a public
229653bd6749SJiri Pirko 		 * address, the lifetimes of temporary addresses should
229753bd6749SJiri Pirko 		 * be extended, subject to the overall constraint that no
229853bd6749SJiri Pirko 		 * temporary addresses should ever remain "valid" or "preferred"
229953bd6749SJiri Pirko 		 * for a time longer than (TEMP_VALID_LIFETIME) or
230053bd6749SJiri Pirko 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
230153bd6749SJiri Pirko 		 */
230253bd6749SJiri Pirko 		age = (now - ift->cstamp) / HZ;
230353bd6749SJiri Pirko 		max_valid = idev->cnf.temp_valid_lft - age;
230453bd6749SJiri Pirko 		if (max_valid < 0)
230553bd6749SJiri Pirko 			max_valid = 0;
230653bd6749SJiri Pirko 
230753bd6749SJiri Pirko 		max_prefered = idev->cnf.temp_prefered_lft -
230853bd6749SJiri Pirko 			       idev->cnf.max_desync_factor - age;
230953bd6749SJiri Pirko 		if (max_prefered < 0)
231053bd6749SJiri Pirko 			max_prefered = 0;
231153bd6749SJiri Pirko 
231253bd6749SJiri Pirko 		if (valid_lft > max_valid)
231353bd6749SJiri Pirko 			valid_lft = max_valid;
231453bd6749SJiri Pirko 
231553bd6749SJiri Pirko 		if (prefered_lft > max_prefered)
231653bd6749SJiri Pirko 			prefered_lft = max_prefered;
231753bd6749SJiri Pirko 
231853bd6749SJiri Pirko 		spin_lock(&ift->lock);
231953bd6749SJiri Pirko 		flags = ift->flags;
232053bd6749SJiri Pirko 		ift->valid_lft = valid_lft;
232153bd6749SJiri Pirko 		ift->prefered_lft = prefered_lft;
232253bd6749SJiri Pirko 		ift->tstamp = now;
232353bd6749SJiri Pirko 		if (prefered_lft > 0)
232453bd6749SJiri Pirko 			ift->flags &= ~IFA_F_DEPRECATED;
232553bd6749SJiri Pirko 
232653bd6749SJiri Pirko 		spin_unlock(&ift->lock);
232753bd6749SJiri Pirko 		if (!(flags&IFA_F_TENTATIVE))
232853bd6749SJiri Pirko 			ipv6_ifa_notify(0, ift);
232953bd6749SJiri Pirko 	}
233053bd6749SJiri Pirko 
233153bd6749SJiri Pirko 	if ((create || list_empty(&idev->tempaddr_list)) &&
233253bd6749SJiri Pirko 	    idev->cnf.use_tempaddr > 0) {
233353bd6749SJiri Pirko 		/* When a new public address is created as described
233453bd6749SJiri Pirko 		 * in [ADDRCONF], also create a new temporary address.
233553bd6749SJiri Pirko 		 * Also create a temporary address if it's enabled but
233653bd6749SJiri Pirko 		 * no temporary address currently exists.
233753bd6749SJiri Pirko 		 */
233853bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
233953bd6749SJiri Pirko 		ipv6_create_tempaddr(ifp, NULL);
234053bd6749SJiri Pirko 	} else {
234153bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
234253bd6749SJiri Pirko 	}
234353bd6749SJiri Pirko }
234453bd6749SJiri Pirko 
2345e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
23461da177e4SLinus Torvalds {
23471da177e4SLinus Torvalds 	struct prefix_info *pinfo;
23481da177e4SLinus Torvalds 	__u32 valid_lft;
23491da177e4SLinus Torvalds 	__u32 prefered_lft;
23501da177e4SLinus Torvalds 	int addr_type;
235164236f3fSHannes Frederic Sowa 	u32 addr_flags = 0;
23521da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
235356d417b1SBrian Haley 	struct net *net = dev_net(dev);
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
23561da177e4SLinus Torvalds 
23571da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
2358ba3542e1Sdingtianhong 		ADBG("addrconf: prefix option too short\n");
23591da177e4SLinus Torvalds 		return;
23601da177e4SLinus Torvalds 	}
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 	/*
23631da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
23641da177e4SLinus Torvalds 	 */
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
23671da177e4SLinus Torvalds 
23681da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
23691da177e4SLinus Torvalds 		return;
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
23721da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2375e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
23761da177e4SLinus Torvalds 		return;
23771da177e4SLinus Torvalds 	}
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
23801da177e4SLinus Torvalds 
238163159f29SIan Morris 	if (!in6_dev) {
2382e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2383e87cc472SJoe Perches 				    dev->name);
23841da177e4SLinus Torvalds 		return;
23851da177e4SLinus Torvalds 	}
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds 	/*
23881da177e4SLinus Torvalds 	 *	Two things going on here:
23891da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
23901da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
23911da177e4SLinus Torvalds 	 */
23921da177e4SLinus Torvalds 
23934bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
23944bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
23954bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
23964bed72e4SYOSHIFUJI Hideaki 
23971da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
23986f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
23996f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
24006f704992SYOSHIFUJI Hideaki 		 * not good.
24011da177e4SLinus Torvalds 		 */
24024bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
24034bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
24044bed72e4SYOSHIFUJI Hideaki 		else
24054bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
24063dd4bc68SYOSHIFUJI Hideaki 
24074bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
24084bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
24091da177e4SLinus Torvalds 
241014ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
241114ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
241214ef37b6SAndreas Hofmeister 					       dev,
241314ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
241414ef37b6SAndreas Hofmeister 					       RTF_GATEWAY | RTF_DEFAULT);
24151da177e4SLinus Torvalds 
241614ef37b6SAndreas Hofmeister 		if (rt) {
24176f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
24181da177e4SLinus Torvalds 			if (valid_lft == 0) {
2419e0a1ad73SThomas Graf 				ip6_del_rt(rt);
24201da177e4SLinus Torvalds 				rt = NULL;
24214bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
24226f704992SYOSHIFUJI Hideaki 				/* not infinity */
24231716a961SGao feng 				rt6_set_expires(rt, jiffies + rt_expires);
24246f704992SYOSHIFUJI Hideaki 			} else {
24251716a961SGao feng 				rt6_clean_expires(rt);
24261da177e4SLinus Torvalds 			}
24271da177e4SLinus Torvalds 		} else if (valid_lft) {
24286f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
24294bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
24304bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
24316f704992SYOSHIFUJI Hideaki 				/* not infinity */
24326f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
24336f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
24346f704992SYOSHIFUJI Hideaki 			}
24351da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
24366f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
24371da177e4SLinus Torvalds 		}
243894e187c0SAmerigo Wang 		ip6_rt_put(rt);
24391da177e4SLinus Torvalds 	}
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
24441da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
24451da177e4SLinus Torvalds 		struct in6_addr addr;
24461da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
2447617fe29dSDaniel Borkmann 		bool tokenized = false;
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
24501da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2451f53adae4SDaniel Borkmann 
2452f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2453f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2454f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2455f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2456f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
2457617fe29dSDaniel Borkmann 				tokenized = true;
2458622c81d5SHannes Frederic Sowa 			} else if (in6_dev->addr_gen_mode ==
2459622c81d5SHannes Frederic Sowa 				   IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
2460622c81d5SHannes Frederic Sowa 				   !ipv6_generate_stable_address(&addr, 0,
2461622c81d5SHannes Frederic Sowa 								 in6_dev)) {
246264236f3fSHannes Frederic Sowa 				addr_flags |= IFA_F_STABLE_PRIVACY;
2463622c81d5SHannes Frederic Sowa 				goto ok;
2464f53adae4SDaniel Borkmann 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
24651da177e4SLinus Torvalds 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
24661da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
24671da177e4SLinus Torvalds 				return;
24681da177e4SLinus Torvalds 			}
24691da177e4SLinus Torvalds 			goto ok;
24701da177e4SLinus Torvalds 		}
2471e87cc472SJoe Perches 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
24721da177e4SLinus Torvalds 				    pinfo->prefix_len);
24731da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
24741da177e4SLinus Torvalds 		return;
24751da177e4SLinus Torvalds 
24761da177e4SLinus Torvalds ok:
24771da177e4SLinus Torvalds 
247853b7997fSYOSHIFUJI Hideaki 		ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
24791da177e4SLinus Torvalds 
248063159f29SIan Morris 		if (!ifp && valid_lft) {
24811da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
248295c385b4SNeil Horman 
248395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
248495c385b4SNeil Horman 			if (in6_dev->cnf.optimistic_dad &&
2485e6bff995SNeil Horman 			    !net->ipv6.devconf_all->forwarding && sllao)
248695c385b4SNeil Horman 				addr_flags = IFA_F_OPTIMISTIC;
248795c385b4SNeil Horman #endif
24881da177e4SLinus Torvalds 
24891da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
24901da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
24911da177e4SLinus Torvalds 			 */
24921da177e4SLinus Torvalds 			if (!max_addresses ||
24931da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
24943f8f5298SJiri Pirko 				ifp = ipv6_add_addr(in6_dev, &addr, NULL,
24953f8f5298SJiri Pirko 						    pinfo->prefix_len,
249695c385b4SNeil Horman 						    addr_type&IPV6_ADDR_SCOPE_MASK,
24978a226b2cSJiri Benc 						    addr_flags, valid_lft,
24988a226b2cSJiri Benc 						    prefered_lft);
24991da177e4SLinus Torvalds 
25003f0d2ba0SYOSHIFUJI Hideaki / 吉藤英明 			if (IS_ERR_OR_NULL(ifp)) {
25011da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
25021da177e4SLinus Torvalds 				return;
25031da177e4SLinus Torvalds 			}
25041da177e4SLinus Torvalds 
25058a226b2cSJiri Benc 			update_lft = 0;
25068a226b2cSJiri Benc 			create = 1;
2507c15b1ccaSHannes Frederic Sowa 			spin_lock_bh(&ifp->lock);
2508c15b1ccaSHannes Frederic Sowa 			ifp->flags |= IFA_F_MANAGETEMPADDR;
25091da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
2510617fe29dSDaniel Borkmann 			ifp->tokenized = tokenized;
2511c15b1ccaSHannes Frederic Sowa 			spin_unlock_bh(&ifp->lock);
2512cf22f9a2SDavid S. Miller 			addrconf_dad_start(ifp);
25131da177e4SLinus Torvalds 		}
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds 		if (ifp) {
2516479840ffSJiri Pirko 			u32 flags;
25171da177e4SLinus Torvalds 			unsigned long now;
25181da177e4SLinus Torvalds 			u32 stored_lft;
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
25218e8e676dSHannes Frederic Sowa 			spin_lock_bh(&ifp->lock);
25221da177e4SLinus Torvalds 			now = jiffies;
25231da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
25241da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
25251da177e4SLinus Torvalds 			else
25261da177e4SLinus Torvalds 				stored_lft = 0;
25278a226b2cSJiri Benc 			if (!update_lft && !create && stored_lft) {
25289451a304SFabian Frederick 				const u32 minimum_lft = min_t(u32,
25299451a304SFabian Frederick 					stored_lft, MIN_VALID_LIFETIME);
2530c9d55d5bSPaul Marks 				valid_lft = max(valid_lft, minimum_lft);
2531c9d55d5bSPaul Marks 
2532c9d55d5bSPaul Marks 				/* RFC4862 Section 5.5.3e:
2533c9d55d5bSPaul Marks 				 * "Note that the preferred lifetime of the
2534c9d55d5bSPaul Marks 				 *  corresponding address is always reset to
2535c9d55d5bSPaul Marks 				 *  the Preferred Lifetime in the received
2536c9d55d5bSPaul Marks 				 *  Prefix Information option, regardless of
2537c9d55d5bSPaul Marks 				 *  whether the valid lifetime is also reset or
2538a1ed0526SBrian Haley 				 *  ignored."
2539a1ed0526SBrian Haley 				 *
2540c9d55d5bSPaul Marks 				 * So we should always update prefered_lft here.
2541a1ed0526SBrian Haley 				 */
2542a1ed0526SBrian Haley 				update_lft = 1;
2543a1ed0526SBrian Haley 			}
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 			if (update_lft) {
25461da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
25471da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
25481da177e4SLinus Torvalds 				ifp->tstamp = now;
25491da177e4SLinus Torvalds 				flags = ifp->flags;
25501da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
25518e8e676dSHannes Frederic Sowa 				spin_unlock_bh(&ifp->lock);
25521da177e4SLinus Torvalds 
25531da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
25541da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
25551da177e4SLinus Torvalds 			} else
25568e8e676dSHannes Frederic Sowa 				spin_unlock_bh(&ifp->lock);
25571da177e4SLinus Torvalds 
255853bd6749SJiri Pirko 			manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
255953bd6749SJiri Pirko 					 create, now);
25605d9efa7eSDavid S. Miller 
25611da177e4SLinus Torvalds 			in6_ifa_put(ifp);
2562c15b1ccaSHannes Frederic Sowa 			addrconf_verify();
25631da177e4SLinus Torvalds 		}
25641da177e4SLinus Torvalds 	}
25651da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
25661da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
25671da177e4SLinus Torvalds }
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds /*
25701da177e4SLinus Torvalds  *	Set destination address.
25711da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
25721da177e4SLinus Torvalds  *	device.
25731da177e4SLinus Torvalds  */
2574af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
25751da177e4SLinus Torvalds {
25761da177e4SLinus Torvalds 	struct in6_ifreq ireq;
25771da177e4SLinus Torvalds 	struct net_device *dev;
25781da177e4SLinus Torvalds 	int err = -EINVAL;
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds 	rtnl_lock();
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 	err = -EFAULT;
25831da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
25841da177e4SLinus Torvalds 		goto err_exit;
25851da177e4SLinus Torvalds 
2586af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds 	err = -ENODEV;
258963159f29SIan Morris 	if (!dev)
25901da177e4SLinus Torvalds 		goto err_exit;
25911da177e4SLinus Torvalds 
259207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
25931da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
25945bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
25951da177e4SLinus Torvalds 		struct ifreq ifr;
25961da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
25991da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
26001da177e4SLinus Torvalds 			goto err_exit;
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
26031da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
26041da177e4SLinus Torvalds 		p.iph.saddr = 0;
26051da177e4SLinus Torvalds 		p.iph.version = 4;
26061da177e4SLinus Torvalds 		p.iph.ihl = 5;
26071da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
26081da177e4SLinus Torvalds 		p.iph.ttl = 64;
2609d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
26101da177e4SLinus Torvalds 
26115bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
26125bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
26135bc3eb7eSStephen Hemminger 
26145bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
26155bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
26161da177e4SLinus Torvalds 			set_fs(oldfs);
26175bc3eb7eSStephen Hemminger 		} else
26185bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds 		if (err == 0) {
26211da177e4SLinus Torvalds 			err = -ENOBUFS;
2622af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2623af284937SDaniel Lezcano 			if (!dev)
26241da177e4SLinus Torvalds 				goto err_exit;
26251da177e4SLinus Torvalds 			err = dev_open(dev);
26261da177e4SLinus Torvalds 		}
26271da177e4SLinus Torvalds 	}
26280be669bbSJoerg Roedel #endif
26291da177e4SLinus Torvalds 
26301da177e4SLinus Torvalds err_exit:
26311da177e4SLinus Torvalds 	rtnl_unlock();
26321da177e4SLinus Torvalds 	return err;
26331da177e4SLinus Torvalds }
26341da177e4SLinus Torvalds 
263593a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join,
263693a714d6SMadhu Challa 			  const struct in6_addr *addr, int ifindex)
263793a714d6SMadhu Challa {
263893a714d6SMadhu Challa 	int ret;
263993a714d6SMadhu Challa 
264093a714d6SMadhu Challa 	ASSERT_RTNL();
264193a714d6SMadhu Challa 
264293a714d6SMadhu Challa 	lock_sock(sk);
264393a714d6SMadhu Challa 	if (join)
264454ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_join(sk, ifindex, addr);
264593a714d6SMadhu Challa 	else
264654ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_drop(sk, ifindex, addr);
264793a714d6SMadhu Challa 	release_sock(sk);
264893a714d6SMadhu Challa 
264993a714d6SMadhu Challa 	return ret;
265093a714d6SMadhu Challa }
265193a714d6SMadhu Challa 
26521da177e4SLinus Torvalds /*
26531da177e4SLinus Torvalds  *	Manual configuration of address on an interface
26541da177e4SLinus Torvalds  */
2655479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex,
2656479840ffSJiri Pirko 			  const struct in6_addr *pfx,
2657caeaba79SNicolas Dichtel 			  const struct in6_addr *peer_pfx,
2658479840ffSJiri Pirko 			  unsigned int plen, __u32 ifa_flags,
2659479840ffSJiri Pirko 			  __u32 prefered_lft, __u32 valid_lft)
26601da177e4SLinus Torvalds {
26611da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
26621da177e4SLinus Torvalds 	struct inet6_dev *idev;
26631da177e4SLinus Torvalds 	struct net_device *dev;
266493a714d6SMadhu Challa 	unsigned long timeout;
266593a714d6SMadhu Challa 	clock_t expires;
26661da177e4SLinus Torvalds 	int scope;
26676f704992SYOSHIFUJI Hideaki 	u32 flags;
26681da177e4SLinus Torvalds 
26691da177e4SLinus Torvalds 	ASSERT_RTNL();
26701da177e4SLinus Torvalds 
267124ef0da7SThomas Graf 	if (plen > 128)
267224ef0da7SThomas Graf 		return -EINVAL;
267324ef0da7SThomas Graf 
26740778769dSNoriaki TAKAMIYA 	/* check the lifetime */
26750778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
26760778769dSNoriaki TAKAMIYA 		return -EINVAL;
26770778769dSNoriaki TAKAMIYA 
267853bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
267953bd6749SJiri Pirko 		return -EINVAL;
268053bd6749SJiri Pirko 
2681af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2682af284937SDaniel Lezcano 	if (!dev)
26831da177e4SLinus Torvalds 		return -ENODEV;
26841da177e4SLinus Torvalds 
268564e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
268664e724f6SBrian Haley 	if (IS_ERR(idev))
268764e724f6SBrian Haley 		return PTR_ERR(idev);
26881da177e4SLinus Torvalds 
268993a714d6SMadhu Challa 	if (ifa_flags & IFA_F_MCAUTOJOIN) {
269093a714d6SMadhu Challa 		int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
269193a714d6SMadhu Challa 					 true, pfx, ifindex);
269293a714d6SMadhu Challa 
269393a714d6SMadhu Challa 		if (ret < 0)
269493a714d6SMadhu Challa 			return ret;
269593a714d6SMadhu Challa 	}
269693a714d6SMadhu Challa 
26971da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
26981da177e4SLinus Torvalds 
26994bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
27004bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
27014bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
27024bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
27036f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
27044bed72e4SYOSHIFUJI Hideaki 	} else {
27054bed72e4SYOSHIFUJI Hideaki 		expires = 0;
27064bed72e4SYOSHIFUJI Hideaki 		flags = 0;
27074bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
27086f704992SYOSHIFUJI Hideaki 	}
27090778769dSNoriaki TAKAMIYA 
27104bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
27114bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
27124bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
27130778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
27144bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
27154bed72e4SYOSHIFUJI Hideaki 	}
27160778769dSNoriaki TAKAMIYA 
27178a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
27188a226b2cSJiri Benc 			    valid_lft, prefered_lft);
27190778769dSNoriaki TAKAMIYA 
27201da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
2721761aac73SThomas Haller 		if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
272246d48046SYOSHIFUJI Hideaki 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
27236f704992SYOSHIFUJI Hideaki 					      expires, flags);
2724761aac73SThomas Haller 		}
2725761aac73SThomas Haller 
272695c385b4SNeil Horman 		/*
272795c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
272895c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
272995c385b4SNeil Horman 		 * manually configured addresses
273095c385b4SNeil Horman 		 */
2731cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
273253bd6749SJiri Pirko 		if (ifa_flags & IFA_F_MANAGETEMPADDR)
273353bd6749SJiri Pirko 			manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
273453bd6749SJiri Pirko 					 true, jiffies);
27351da177e4SLinus Torvalds 		in6_ifa_put(ifp);
2736c15b1ccaSHannes Frederic Sowa 		addrconf_verify_rtnl();
27371da177e4SLinus Torvalds 		return 0;
273893a714d6SMadhu Challa 	} else if (ifa_flags & IFA_F_MCAUTOJOIN) {
273993a714d6SMadhu Challa 		ipv6_mc_config(net->ipv6.mc_autojoin_sk,
274093a714d6SMadhu Challa 			       false, pfx, ifindex);
27411da177e4SLinus Torvalds 	}
27421da177e4SLinus Torvalds 
27431da177e4SLinus Torvalds 	return PTR_ERR(ifp);
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds 
27466046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
27476046d5b4SHeiner Kallweit 			  const struct in6_addr *pfx, unsigned int plen)
27481da177e4SLinus Torvalds {
27491da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
27501da177e4SLinus Torvalds 	struct inet6_dev *idev;
27511da177e4SLinus Torvalds 	struct net_device *dev;
27521da177e4SLinus Torvalds 
275324ef0da7SThomas Graf 	if (plen > 128)
275424ef0da7SThomas Graf 		return -EINVAL;
275524ef0da7SThomas Graf 
2756af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2757af284937SDaniel Lezcano 	if (!dev)
27581da177e4SLinus Torvalds 		return -ENODEV;
27591da177e4SLinus Torvalds 
2760e5d08d71SIan Morris 	idev = __in6_dev_get(dev);
276163159f29SIan Morris 	if (!idev)
27621da177e4SLinus Torvalds 		return -ENXIO;
27631da177e4SLinus Torvalds 
27641da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2765502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
27661da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
27671da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
27681da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
27691da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
27701da177e4SLinus Torvalds 
27716046d5b4SHeiner Kallweit 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
27726046d5b4SHeiner Kallweit 			    (ifa_flags & IFA_F_MANAGETEMPADDR))
27736046d5b4SHeiner Kallweit 				manage_tempaddrs(idev, ifp, 0, 0, false,
27746046d5b4SHeiner Kallweit 						 jiffies);
27751da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
27766046d5b4SHeiner Kallweit 			addrconf_verify_rtnl();
277793a714d6SMadhu Challa 			if (ipv6_addr_is_multicast(pfx)) {
277893a714d6SMadhu Challa 				ipv6_mc_config(net->ipv6.mc_autojoin_sk,
277993a714d6SMadhu Challa 					       false, pfx, dev->ifindex);
278093a714d6SMadhu Challa 			}
27811da177e4SLinus Torvalds 			return 0;
27821da177e4SLinus Torvalds 		}
27831da177e4SLinus Torvalds 	}
27841da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
27851da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
27861da177e4SLinus Torvalds }
27871da177e4SLinus Torvalds 
27881da177e4SLinus Torvalds 
2789af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
27901da177e4SLinus Torvalds {
27911da177e4SLinus Torvalds 	struct in6_ifreq ireq;
27921da177e4SLinus Torvalds 	int err;
27931da177e4SLinus Torvalds 
2794af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
27951da177e4SLinus Torvalds 		return -EPERM;
27961da177e4SLinus Torvalds 
27971da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
27981da177e4SLinus Torvalds 		return -EFAULT;
27991da177e4SLinus Torvalds 
28001da177e4SLinus Torvalds 	rtnl_lock();
2801caeaba79SNicolas Dichtel 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2802af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2803af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
28041da177e4SLinus Torvalds 	rtnl_unlock();
28051da177e4SLinus Torvalds 	return err;
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds 
2808af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
28091da177e4SLinus Torvalds {
28101da177e4SLinus Torvalds 	struct in6_ifreq ireq;
28111da177e4SLinus Torvalds 	int err;
28121da177e4SLinus Torvalds 
2813af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
28141da177e4SLinus Torvalds 		return -EPERM;
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
28171da177e4SLinus Torvalds 		return -EFAULT;
28181da177e4SLinus Torvalds 
28191da177e4SLinus Torvalds 	rtnl_lock();
28206046d5b4SHeiner Kallweit 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2821af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
28221da177e4SLinus Torvalds 	rtnl_unlock();
28231da177e4SLinus Torvalds 	return err;
28241da177e4SLinus Torvalds }
28251da177e4SLinus Torvalds 
2826b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2827b5f348e5SIlpo Järvinen 		     int plen, int scope)
2828b5f348e5SIlpo Järvinen {
2829b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
2830b5f348e5SIlpo Järvinen 
28318a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, addr, NULL, plen,
283207edd741SHannes Frederic Sowa 			    scope, IFA_F_PERMANENT,
283307edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2834b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
2835b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
2836b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
2837b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
2838b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2839b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
2840b5f348e5SIlpo Järvinen 	}
2841b5f348e5SIlpo Järvinen }
2842b5f348e5SIlpo Järvinen 
284307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
28441da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
28451da177e4SLinus Torvalds {
28461da177e4SLinus Torvalds 	struct in6_addr addr;
28471da177e4SLinus Torvalds 	struct net_device *dev;
2848c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
2849929c9cf3SNicolas Dichtel 	int scope, plen;
2850f0e2acfaSNicolas Dichtel 	u32 pflags = 0;
28511da177e4SLinus Torvalds 
28521da177e4SLinus Torvalds 	ASSERT_RTNL();
28531da177e4SLinus Torvalds 
28541da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
28551da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
28581da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
28591da177e4SLinus Torvalds 		scope = IFA_LINK;
2860929c9cf3SNicolas Dichtel 		plen = 64;
28611da177e4SLinus Torvalds 	} else {
28621da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
2863929c9cf3SNicolas Dichtel 		plen = 96;
2864f0e2acfaSNicolas Dichtel 		pflags |= RTF_NONEXTHOP;
28651da177e4SLinus Torvalds 	}
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
2868929c9cf3SNicolas Dichtel 		add_addr(idev, &addr, plen, scope);
2869f0e2acfaSNicolas Dichtel 		addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
28701da177e4SLinus Torvalds 		return;
28711da177e4SLinus Torvalds 	}
28721da177e4SLinus Torvalds 
28736fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
2874e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
28751da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
28761da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
28771da177e4SLinus Torvalds 
28781da177e4SLinus Torvalds 			int flag = scope;
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
28811da177e4SLinus Torvalds 
28821da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
28831da177e4SLinus Torvalds 
28841da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
28851da177e4SLinus Torvalds 					continue;
28861da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
28871da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
28881da177e4SLinus Torvalds 						continue;
28891da177e4SLinus Torvalds 					flag |= IFA_HOST;
28901da177e4SLinus Torvalds 				}
28911da177e4SLinus Torvalds 
2892b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
2893f0e2acfaSNicolas Dichtel 				addrconf_prefix_route(&addr, plen, idev->dev, 0,
2894f0e2acfaSNicolas Dichtel 						      pflags);
28951da177e4SLinus Torvalds 			}
28961da177e4SLinus Torvalds 		}
28971da177e4SLinus Torvalds 	}
28981da177e4SLinus Torvalds }
28990be669bbSJoerg Roedel #endif
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
29021da177e4SLinus Torvalds {
29031da177e4SLinus Torvalds 	struct inet6_dev  *idev;
290425fb6ca4SBalakumaran Kannan 	struct net_device *sp_dev;
290525fb6ca4SBalakumaran Kannan 	struct inet6_ifaddr *sp_ifa;
290625fb6ca4SBalakumaran Kannan 	struct rt6_info *sp_rt;
29071da177e4SLinus Torvalds 
29081da177e4SLinus Torvalds 	/* ::1 */
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds 	ASSERT_RTNL();
29111da177e4SLinus Torvalds 
2912e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
291363159f29SIan Morris 	if (!idev) {
291491df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
29151da177e4SLinus Torvalds 		return;
29161da177e4SLinus Torvalds 	}
29171da177e4SLinus Torvalds 
2918b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
291925fb6ca4SBalakumaran Kannan 
292025fb6ca4SBalakumaran Kannan 	/* Add routes to other interface's IPv6 addresses */
292125fb6ca4SBalakumaran Kannan 	for_each_netdev(dev_net(dev), sp_dev) {
292225fb6ca4SBalakumaran Kannan 		if (!strcmp(sp_dev->name, dev->name))
292325fb6ca4SBalakumaran Kannan 			continue;
292425fb6ca4SBalakumaran Kannan 
292525fb6ca4SBalakumaran Kannan 		idev = __in6_dev_get(sp_dev);
292625fb6ca4SBalakumaran Kannan 		if (!idev)
292725fb6ca4SBalakumaran Kannan 			continue;
292825fb6ca4SBalakumaran Kannan 
292925fb6ca4SBalakumaran Kannan 		read_lock_bh(&idev->lock);
293025fb6ca4SBalakumaran Kannan 		list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
293125fb6ca4SBalakumaran Kannan 
293225fb6ca4SBalakumaran Kannan 			if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
293325fb6ca4SBalakumaran Kannan 				continue;
293425fb6ca4SBalakumaran Kannan 
293533d99113SGao feng 			if (sp_ifa->rt) {
293633d99113SGao feng 				/* This dst has been added to garbage list when
293733d99113SGao feng 				 * lo device down, release this obsolete dst and
293833d99113SGao feng 				 * reallocate a new router for ifa.
293933d99113SGao feng 				 */
294033d99113SGao feng 				if (sp_ifa->rt->dst.obsolete > 0) {
294133d99113SGao feng 					ip6_rt_put(sp_ifa->rt);
294233d99113SGao feng 					sp_ifa->rt = NULL;
294333d99113SGao feng 				} else {
2944a881ae1fSGao feng 					continue;
294533d99113SGao feng 				}
294633d99113SGao feng 			}
2947a881ae1fSGao feng 
294857ec0afeSFrançois-Xavier Le Bail 			sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
294925fb6ca4SBalakumaran Kannan 
295025fb6ca4SBalakumaran Kannan 			/* Failure cases are ignored */
2951534c8779SGao feng 			if (!IS_ERR(sp_rt)) {
2952534c8779SGao feng 				sp_ifa->rt = sp_rt;
295325fb6ca4SBalakumaran Kannan 				ip6_ins_rt(sp_rt);
295425fb6ca4SBalakumaran Kannan 			}
2955534c8779SGao feng 		}
295625fb6ca4SBalakumaran Kannan 		read_unlock_bh(&idev->lock);
295725fb6ca4SBalakumaran Kannan 	}
29581da177e4SLinus Torvalds }
29591da177e4SLinus Torvalds 
296064236f3fSHannes Frederic Sowa static void addrconf_add_linklocal(struct inet6_dev *idev,
296164236f3fSHannes Frederic Sowa 				   const struct in6_addr *addr, u32 flags)
29621da177e4SLinus Torvalds {
29631da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
296464236f3fSHannes Frederic Sowa 	u32 addr_flags = flags | IFA_F_PERMANENT;
29651da177e4SLinus Torvalds 
296695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
296795c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
2968702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
296995c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
297095c385b4SNeil Horman #endif
297195c385b4SNeil Horman 
297207edd741SHannes Frederic Sowa 	ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
297307edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
29741da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
297546d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2976cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
29771da177e4SLinus Torvalds 		in6_ifa_put(ifp);
29781da177e4SLinus Torvalds 	}
29791da177e4SLinus Torvalds }
29801da177e4SLinus Torvalds 
2981622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address)
2982622c81d5SHannes Frederic Sowa {
2983622c81d5SHannes Frederic Sowa 	if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
2984622c81d5SHannes Frederic Sowa 		return true;
2985622c81d5SHannes Frederic Sowa 
2986622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0x02005eff) &&
2987622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
2988622c81d5SHannes Frederic Sowa 		return true;
2989622c81d5SHannes Frederic Sowa 
2990622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0xfdffffff) &&
2991622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
2992622c81d5SHannes Frederic Sowa 		return true;
2993622c81d5SHannes Frederic Sowa 
2994622c81d5SHannes Frederic Sowa 	return false;
2995622c81d5SHannes Frederic Sowa }
2996622c81d5SHannes Frederic Sowa 
2997622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address,
2998622c81d5SHannes Frederic Sowa 					u8 dad_count,
2999622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev)
3000622c81d5SHannes Frederic Sowa {
3001622c81d5SHannes Frederic Sowa 	static DEFINE_SPINLOCK(lock);
3002622c81d5SHannes Frederic Sowa 	static __u32 digest[SHA_DIGEST_WORDS];
3003622c81d5SHannes Frederic Sowa 	static __u32 workspace[SHA_WORKSPACE_WORDS];
3004622c81d5SHannes Frederic Sowa 
3005622c81d5SHannes Frederic Sowa 	static union {
3006622c81d5SHannes Frederic Sowa 		char __data[SHA_MESSAGE_BYTES];
3007622c81d5SHannes Frederic Sowa 		struct {
3008622c81d5SHannes Frederic Sowa 			struct in6_addr secret;
3009ff40217eSHannes Frederic Sowa 			__be32 prefix[2];
3010622c81d5SHannes Frederic Sowa 			unsigned char hwaddr[MAX_ADDR_LEN];
3011622c81d5SHannes Frederic Sowa 			u8 dad_count;
3012622c81d5SHannes Frederic Sowa 		} __packed;
3013622c81d5SHannes Frederic Sowa 	} data;
3014622c81d5SHannes Frederic Sowa 
3015622c81d5SHannes Frederic Sowa 	struct in6_addr secret;
3016622c81d5SHannes Frederic Sowa 	struct in6_addr temp;
3017622c81d5SHannes Frederic Sowa 	struct net *net = dev_net(idev->dev);
3018622c81d5SHannes Frederic Sowa 
3019622c81d5SHannes Frederic Sowa 	BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3020622c81d5SHannes Frederic Sowa 
3021622c81d5SHannes Frederic Sowa 	if (idev->cnf.stable_secret.initialized)
3022622c81d5SHannes Frederic Sowa 		secret = idev->cnf.stable_secret.secret;
3023622c81d5SHannes Frederic Sowa 	else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3024622c81d5SHannes Frederic Sowa 		secret = net->ipv6.devconf_dflt->stable_secret.secret;
3025622c81d5SHannes Frederic Sowa 	else
3026622c81d5SHannes Frederic Sowa 		return -1;
3027622c81d5SHannes Frederic Sowa 
3028622c81d5SHannes Frederic Sowa retry:
3029622c81d5SHannes Frederic Sowa 	spin_lock_bh(&lock);
3030622c81d5SHannes Frederic Sowa 
3031622c81d5SHannes Frederic Sowa 	sha_init(digest);
3032622c81d5SHannes Frederic Sowa 	memset(&data, 0, sizeof(data));
3033622c81d5SHannes Frederic Sowa 	memset(workspace, 0, sizeof(workspace));
3034622c81d5SHannes Frederic Sowa 	memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3035ff40217eSHannes Frederic Sowa 	data.prefix[0] = address->s6_addr32[0];
3036ff40217eSHannes Frederic Sowa 	data.prefix[1] = address->s6_addr32[1];
3037622c81d5SHannes Frederic Sowa 	data.secret = secret;
3038622c81d5SHannes Frederic Sowa 	data.dad_count = dad_count;
3039622c81d5SHannes Frederic Sowa 
3040622c81d5SHannes Frederic Sowa 	sha_transform(digest, data.__data, workspace);
3041622c81d5SHannes Frederic Sowa 
3042622c81d5SHannes Frederic Sowa 	temp = *address;
3043ff40217eSHannes Frederic Sowa 	temp.s6_addr32[2] = (__force __be32)digest[0];
3044ff40217eSHannes Frederic Sowa 	temp.s6_addr32[3] = (__force __be32)digest[1];
3045622c81d5SHannes Frederic Sowa 
3046622c81d5SHannes Frederic Sowa 	spin_unlock_bh(&lock);
3047622c81d5SHannes Frederic Sowa 
3048622c81d5SHannes Frederic Sowa 	if (ipv6_reserved_interfaceid(temp)) {
3049622c81d5SHannes Frederic Sowa 		dad_count++;
30501855b7c3SHannes Frederic Sowa 		if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3051622c81d5SHannes Frederic Sowa 			return -1;
3052622c81d5SHannes Frederic Sowa 		goto retry;
3053622c81d5SHannes Frederic Sowa 	}
3054622c81d5SHannes Frederic Sowa 
3055622c81d5SHannes Frederic Sowa 	*address = temp;
3056622c81d5SHannes Frederic Sowa 	return 0;
3057622c81d5SHannes Frederic Sowa }
3058622c81d5SHannes Frederic Sowa 
3059bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3060bc91b0f0SJiri Pirko {
3061bc91b0f0SJiri Pirko 	struct in6_addr addr;
3062bc91b0f0SJiri Pirko 
3063bc91b0f0SJiri Pirko 	ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3064622c81d5SHannes Frederic Sowa 
3065622c81d5SHannes Frederic Sowa 	if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY) {
3066622c81d5SHannes Frederic Sowa 		if (!ipv6_generate_stable_address(&addr, 0, idev))
306764236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr,
306864236f3fSHannes Frederic Sowa 					       IFA_F_STABLE_PRIVACY);
3069622c81d5SHannes Frederic Sowa 		else if (prefix_route)
3070622c81d5SHannes Frederic Sowa 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3071622c81d5SHannes Frederic Sowa 	} else if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) {
3072bc91b0f0SJiri Pirko 		/* addrconf_add_linklocal also adds a prefix_route and we
3073bc91b0f0SJiri Pirko 		 * only need to care about prefix routes if ipv6_generate_eui64
3074bc91b0f0SJiri Pirko 		 * couldn't generate one.
3075bc91b0f0SJiri Pirko 		 */
3076bc91b0f0SJiri Pirko 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
307764236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr, 0);
3078bc91b0f0SJiri Pirko 		else if (prefix_route)
3079bc91b0f0SJiri Pirko 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3080bc91b0f0SJiri Pirko 	}
3081bc91b0f0SJiri Pirko }
3082bc91b0f0SJiri Pirko 
30831da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
30841da177e4SLinus Torvalds {
30851da177e4SLinus Torvalds 	struct inet6_dev *idev;
30861da177e4SLinus Torvalds 
30871da177e4SLinus Torvalds 	ASSERT_RTNL();
30881da177e4SLinus Torvalds 
308974235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
309074235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
309174235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
309206a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
3093cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	    (dev->type != ARPHRD_IEEE802154) &&
3094e837735eSNicolas Dichtel 	    (dev->type != ARPHRD_IEEE1394) &&
3095e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_TUNNEL6) &&
3096e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_6LOWPAN)) {
309774235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
309874235a25SHerbert Xu 		return;
309974235a25SHerbert Xu 	}
310074235a25SHerbert Xu 
31011da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
310264e724f6SBrian Haley 	if (IS_ERR(idev))
31031da177e4SLinus Torvalds 		return;
31041da177e4SLinus Torvalds 
3105bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, false);
31061da177e4SLinus Torvalds }
31071da177e4SLinus Torvalds 
310807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
31091da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
31101da177e4SLinus Torvalds {
31111da177e4SLinus Torvalds 	struct inet6_dev *idev;
31121da177e4SLinus Torvalds 
31131da177e4SLinus Torvalds 	ASSERT_RTNL();
31141da177e4SLinus Torvalds 
31151da177e4SLinus Torvalds 	/*
31161da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
31171da177e4SLinus Torvalds 	 * addresses... we should configure all of
31181da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
31191da177e4SLinus Torvalds 	 */
31201da177e4SLinus Torvalds 
3121e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
312263159f29SIan Morris 	if (!idev) {
312391df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
31241da177e4SLinus Torvalds 		return;
31251da177e4SLinus Torvalds 	}
31261da177e4SLinus Torvalds 
3127c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
3128bc91b0f0SJiri Pirko 		addrconf_addr_gen(idev, false);
3129c7dc89c0SFred L. Templin 		return;
3130c7dc89c0SFred L. Templin 	}
3131c7dc89c0SFred L. Templin 
31321da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
31331da177e4SLinus Torvalds 
313462b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
31351da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
31361da177e4SLinus Torvalds }
31370be669bbSJoerg Roedel #endif
31381da177e4SLinus Torvalds 
313907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3140aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
3141aee80b54Sstephen hemminger {
3142aee80b54Sstephen hemminger 	struct inet6_dev *idev;
3143aee80b54Sstephen hemminger 
3144aee80b54Sstephen hemminger 	ASSERT_RTNL();
3145aee80b54Sstephen hemminger 
3146e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
314763159f29SIan Morris 	if (!idev) {
314891df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
3149aee80b54Sstephen hemminger 		return;
3150aee80b54Sstephen hemminger 	}
3151aee80b54Sstephen hemminger 
3152bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, true);
3153aee80b54Sstephen hemminger }
3154aee80b54Sstephen hemminger #endif
3155aee80b54Sstephen hemminger 
31561da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
3157351638e7SJiri Pirko 			   void *ptr)
31581da177e4SLinus Torvalds {
3159351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3160748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
3161c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
3162b217d616SHerbert Xu 	int err;
31631da177e4SLinus Torvalds 
31641da177e4SLinus Torvalds 	switch (event) {
316545ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
316674235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
316745ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
3168a317a2f1SWANG Cong 			if (IS_ERR(idev))
3169a317a2f1SWANG Cong 				return notifier_from_errno(PTR_ERR(idev));
317045ba9dd2SYOSHIFUJI Hideaki 		}
317145ba9dd2SYOSHIFUJI Hideaki 		break;
3172bcdd553fSStephen Hemminger 
31731da177e4SLinus Torvalds 	case NETDEV_UP:
31743c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
3175c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
3176c2edacf8SJay Vosburgh 			break;
3177c2edacf8SJay Vosburgh 
31783ce62a84SWANG Cong 		if (idev && idev->cnf.disable_ipv6)
31793ce62a84SWANG Cong 			break;
31803ce62a84SWANG Cong 
31813c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
3182f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
31833c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
3184f3213831SJoe Perches 				pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
31853c21edbdSYOSHIFUJI Hideaki 					dev->name);
31863c21edbdSYOSHIFUJI Hideaki 				break;
31873c21edbdSYOSHIFUJI Hideaki 			}
318899081049SKristian Slavov 
3189d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
3190d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
3191d31c7b8fSEvgeniy Polyakov 
3192a317a2f1SWANG Cong 			if (!IS_ERR_OR_NULL(idev)) {
319399081049SKristian Slavov 				idev->if_flags |= IF_READY;
3194e3ec6cfcSBenjamin Thery 				run_pending = 1;
3195e3ec6cfcSBenjamin Thery 			}
31963c21edbdSYOSHIFUJI Hideaki 		} else {
3197f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
31983c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
31993c21edbdSYOSHIFUJI Hideaki 				break;
32003c21edbdSYOSHIFUJI Hideaki 			}
32013c21edbdSYOSHIFUJI Hideaki 
32023c21edbdSYOSHIFUJI Hideaki 			if (idev) {
3203bcdd553fSStephen Hemminger 				if (idev->if_flags & IF_READY)
32043c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
32053c21edbdSYOSHIFUJI Hideaki 					break;
32063c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
32073c21edbdSYOSHIFUJI Hideaki 			}
32083c21edbdSYOSHIFUJI Hideaki 
3209f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
32103c21edbdSYOSHIFUJI Hideaki 				dev->name);
32113c21edbdSYOSHIFUJI Hideaki 
3212c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
32133c21edbdSYOSHIFUJI Hideaki 		}
32143c21edbdSYOSHIFUJI Hideaki 
32151da177e4SLinus Torvalds 		switch (dev->type) {
321607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
32171da177e4SLinus Torvalds 		case ARPHRD_SIT:
32181da177e4SLinus Torvalds 			addrconf_sit_config(dev);
32191da177e4SLinus Torvalds 			break;
32200be669bbSJoerg Roedel #endif
322107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3222aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
3223aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
3224aee80b54Sstephen hemminger 			break;
3225aee80b54Sstephen hemminger #endif
32261da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
32271da177e4SLinus Torvalds 			init_loopback(dev);
32281da177e4SLinus Torvalds 			break;
32291da177e4SLinus Torvalds 
32301da177e4SLinus Torvalds 		default:
32311da177e4SLinus Torvalds 			addrconf_dev_config(dev);
32321da177e4SLinus Torvalds 			break;
32333ff50b79SStephen Hemminger 		}
3234bcdd553fSStephen Hemminger 
3235a317a2f1SWANG Cong 		if (!IS_ERR_OR_NULL(idev)) {
3236c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
3237c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
3238c5e33bddSYOSHIFUJI Hideaki 
3239bcdd553fSStephen Hemminger 			/*
3240bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
3241bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
3242bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
32431da177e4SLinus Torvalds 			 */
3244bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
3245bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
32461da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
32471da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
32481da177e4SLinus Torvalds 			}
32491da177e4SLinus Torvalds 			idev->tstamp = jiffies;
32501da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
3251bcdd553fSStephen Hemminger 
3252bcdd553fSStephen Hemminger 			/*
3253bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
3254bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
32551da177e4SLinus Torvalds 			 */
32561da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
3257bcdd553fSStephen Hemminger 				addrconf_ifdown(dev, 1);
32581da177e4SLinus Torvalds 		}
32591da177e4SLinus Torvalds 		break;
32601da177e4SLinus Torvalds 
32611da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
32621da177e4SLinus Torvalds 		if (idev && dev->mtu >= IPV6_MIN_MTU) {
32631da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
32641da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
32651da177e4SLinus Torvalds 			break;
32661da177e4SLinus Torvalds 		}
32671da177e4SLinus Torvalds 
3268d31c7b8fSEvgeniy Polyakov 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3269d31c7b8fSEvgeniy Polyakov 			idev = ipv6_add_dev(dev);
3270a317a2f1SWANG Cong 			if (!IS_ERR(idev))
3271d31c7b8fSEvgeniy Polyakov 				break;
3272d31c7b8fSEvgeniy Polyakov 		}
3273d31c7b8fSEvgeniy Polyakov 
3274bcdd553fSStephen Hemminger 		/*
3275db9c7c39Sstephen hemminger 		 * if MTU under IPV6_MIN_MTU.
3276bcdd553fSStephen Hemminger 		 * Stop IPv6 on this interface.
3277bcdd553fSStephen Hemminger 		 */
32781da177e4SLinus Torvalds 
32791da177e4SLinus Torvalds 	case NETDEV_DOWN:
32801da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
32811da177e4SLinus Torvalds 		/*
32821da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
32831da177e4SLinus Torvalds 		 */
32841da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
32851da177e4SLinus Torvalds 		break;
32863c21edbdSYOSHIFUJI Hideaki 
32871da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
32881da177e4SLinus Torvalds 		if (idev) {
32895632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
3290408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
3291a317a2f1SWANG Cong 			err = addrconf_sysctl_register(idev);
3292b217d616SHerbert Xu 			if (err)
3293b217d616SHerbert Xu 				return notifier_from_errno(err);
3294a317a2f1SWANG Cong 			err = snmp6_register_dev(idev);
3295a317a2f1SWANG Cong 			if (err) {
3296a317a2f1SWANG Cong 				addrconf_sysctl_unregister(idev);
3297a317a2f1SWANG Cong 				return notifier_from_errno(err);
3298a317a2f1SWANG Cong 			}
32995632c515SStephen Hemminger 		}
33001da177e4SLinus Torvalds 		break;
3301bcdd553fSStephen Hemminger 
330293d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
330393d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
330493d9b7d7SJiri Pirko 		addrconf_type_change(dev, event);
330575c78500SMoni Shoua 		break;
33063ff50b79SStephen Hemminger 	}
33071da177e4SLinus Torvalds 
33081da177e4SLinus Torvalds 	return NOTIFY_OK;
33091da177e4SLinus Torvalds }
33101da177e4SLinus Torvalds 
33111da177e4SLinus Torvalds /*
33121da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
33131da177e4SLinus Torvalds  */
33141da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
33151da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
33161da177e4SLinus Torvalds };
33171da177e4SLinus Torvalds 
331893d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
331975c78500SMoni Shoua {
332075c78500SMoni Shoua 	struct inet6_dev *idev;
332175c78500SMoni Shoua 	ASSERT_RTNL();
332275c78500SMoni Shoua 
332375c78500SMoni Shoua 	idev = __in6_dev_get(dev);
332475c78500SMoni Shoua 
332593d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
332675c78500SMoni Shoua 		ipv6_mc_remap(idev);
332793d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
332875c78500SMoni Shoua 		ipv6_mc_unmap(idev);
332975c78500SMoni Shoua }
333075c78500SMoni Shoua 
33311da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
33321da177e4SLinus Torvalds {
3333c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
3334502a2ffdSstephen hemminger 	struct inet6_dev *idev;
3335502a2ffdSstephen hemminger 	struct inet6_ifaddr *ifa;
333673a8bd74SDavid S. Miller 	int state, i;
33371da177e4SLinus Torvalds 
33381da177e4SLinus Torvalds 	ASSERT_RTNL();
33391da177e4SLinus Torvalds 
3340f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
334173a8bd74SDavid S. Miller 	neigh_ifdown(&nd_tbl, dev);
33421da177e4SLinus Torvalds 
33431da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
334463159f29SIan Morris 	if (!idev)
33451da177e4SLinus Torvalds 		return -ENODEV;
33461da177e4SLinus Torvalds 
3347bcdd553fSStephen Hemminger 	/*
3348bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3349bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
33501da177e4SLinus Torvalds 	 */
3351439e2385SDenis V. Lunev 	if (how) {
33521da177e4SLinus Torvalds 		idev->dead = 1;
33538814c4b5SYOSHIFUJI Hideaki 
33548814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3355a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
33561da177e4SLinus Torvalds 
33571da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
33581da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
33591da177e4SLinus Torvalds 
33601da177e4SLinus Torvalds 	}
33611da177e4SLinus Torvalds 
336273a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
336373a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
336473a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
336573a8bd74SDavid S. Miller 
336673a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
336773a8bd74SDavid S. Miller restart:
3368b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
336973a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
337073a8bd74SDavid S. Miller 				hlist_del_init_rcu(&ifa->addr_lst);
3371c15b1ccaSHannes Frederic Sowa 				addrconf_del_dad_work(ifa);
337273a8bd74SDavid S. Miller 				goto restart;
337373a8bd74SDavid S. Miller 			}
337473a8bd74SDavid S. Miller 		}
337573a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
337673a8bd74SDavid S. Miller 	}
337773a8bd74SDavid S. Miller 
33781da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
33791da177e4SLinus Torvalds 
3380b7b1bfceSHannes Frederic Sowa 	addrconf_del_rs_timer(idev);
3381b7b1bfceSHannes Frederic Sowa 
3382bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3383439e2385SDenis V. Lunev 	if (!how)
33843c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
33851da177e4SLinus Torvalds 
3386439e2385SDenis V. Lunev 	if (how && del_timer(&idev->regen_timer))
33871da177e4SLinus Torvalds 		in6_dev_put(idev);
33881da177e4SLinus Torvalds 
3389bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3390372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3391372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3392372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3393372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
33941da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
33951da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
33961da177e4SLinus Torvalds 
33971da177e4SLinus Torvalds 		if (ifa->ifpub) {
33981da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
33991da177e4SLinus Torvalds 			ifa->ifpub = NULL;
34001da177e4SLinus Torvalds 		}
34011da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
34021da177e4SLinus Torvalds 		in6_ifa_put(ifa);
34031da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
34041da177e4SLinus Torvalds 	}
34058f37ada5Sstephen hemminger 
3406502a2ffdSstephen hemminger 	while (!list_empty(&idev->addr_list)) {
3407502a2ffdSstephen hemminger 		ifa = list_first_entry(&idev->addr_list,
3408502a2ffdSstephen hemminger 				       struct inet6_ifaddr, if_list);
3409c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifa);
341084e8b803Sstephen hemminger 
3411502a2ffdSstephen hemminger 		list_del(&ifa->if_list);
34121da177e4SLinus Torvalds 
34134c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
341473a8bd74SDavid S. Miller 
34158e8e676dSHannes Frederic Sowa 		spin_lock_bh(&ifa->lock);
34164c5ff6a6SHerbert Xu 		state = ifa->state;
34174c5ff6a6SHerbert Xu 		ifa->state = INET6_IFADDR_STATE_DEAD;
34188e8e676dSHannes Frederic Sowa 		spin_unlock_bh(&ifa->lock);
34194c5ff6a6SHerbert Xu 
34209d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
34211da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
3422f88c91ddSCong Wang 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
34232de79570SLorenzo Colitti 		}
34249d82ca98SJohn Fastabend 		in6_ifa_put(ifa);
342573a8bd74SDavid S. Miller 
34261da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
34271da177e4SLinus Torvalds 	}
34288f37ada5Sstephen hemminger 
34291da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
34301da177e4SLinus Torvalds 
3431381f4dcaSSabrina Dubroca 	/* Step 5: Discard anycast and multicast list */
3432381f4dcaSSabrina Dubroca 	if (how) {
3433381f4dcaSSabrina Dubroca 		ipv6_ac_destroy_dev(idev);
34341da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
3435381f4dcaSSabrina Dubroca 	} else {
34361da177e4SLinus Torvalds 		ipv6_mc_down(idev);
3437381f4dcaSSabrina Dubroca 	}
34381da177e4SLinus Torvalds 
34391da177e4SLinus Torvalds 	idev->tstamp = jiffies;
34401da177e4SLinus Torvalds 
3441bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3442439e2385SDenis V. Lunev 	if (how) {
3443408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
34441da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
34451da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
34461da177e4SLinus Torvalds 		in6_dev_put(idev);
34471da177e4SLinus Torvalds 	}
34481da177e4SLinus Torvalds 	return 0;
34491da177e4SLinus Torvalds }
34501da177e4SLinus Torvalds 
34511da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
34521da177e4SLinus Torvalds {
3453b7b1bfceSHannes Frederic Sowa 	struct inet6_dev *idev = (struct inet6_dev *)data;
3454caf92bc4SCong Wang 	struct net_device *dev = idev->dev;
3455b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
34561da177e4SLinus Torvalds 
3457b7b1bfceSHannes Frederic Sowa 	write_lock(&idev->lock);
34585b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
34591da177e4SLinus Torvalds 		goto out;
34601da177e4SLinus Torvalds 
34619ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
34621da177e4SLinus Torvalds 		goto out;
34635b2a1953Sstephen hemminger 
34645b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
34655b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
34665b2a1953Sstephen hemminger 		goto out;
34671da177e4SLinus Torvalds 
3468b7b1bfceSHannes Frederic Sowa 	if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
3469caf92bc4SCong Wang 		write_unlock(&idev->lock);
3470caf92bc4SCong Wang 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3471caf92bc4SCong Wang 			ndisc_send_rs(dev, &lladdr,
3472b7b1bfceSHannes Frederic Sowa 				      &in6addr_linklocal_allrouters);
3473b7b1bfceSHannes Frederic Sowa 		else
3474caf92bc4SCong Wang 			goto put;
3475b7b1bfceSHannes Frederic Sowa 
3476caf92bc4SCong Wang 		write_lock(&idev->lock);
34771da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
3478b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3479b7b1bfceSHannes Frederic Sowa 					     idev->cnf.rtr_solicits) ?
34805b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_delay :
34815b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_interval);
34821da177e4SLinus Torvalds 	} else {
34831da177e4SLinus Torvalds 		/*
34841da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
34851da177e4SLinus Torvalds 		 * assumption any longer.
34861da177e4SLinus Torvalds 		 */
348791df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
34881da177e4SLinus Torvalds 	}
34891da177e4SLinus Torvalds 
34901da177e4SLinus Torvalds out:
3491b7b1bfceSHannes Frederic Sowa 	write_unlock(&idev->lock);
3492caf92bc4SCong Wang put:
3493b7b1bfceSHannes Frederic Sowa 	in6_dev_put(idev);
34941da177e4SLinus Torvalds }
34951da177e4SLinus Torvalds 
34961da177e4SLinus Torvalds /*
34971da177e4SLinus Torvalds  *	Duplicate Address Detection
34981da177e4SLinus Torvalds  */
34993c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
35003c21edbdSYOSHIFUJI Hideaki {
35013c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
35023c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
35033c21edbdSYOSHIFUJI Hideaki 
350495c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
350595c385b4SNeil Horman 		rand_num = 0;
350695c385b4SNeil Horman 	else
350763862b5bSAruna-Hewapathirane 		rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
350895c385b4SNeil Horman 
3509b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes = idev->cnf.dad_transmits;
3510c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, rand_num);
35113c21edbdSYOSHIFUJI Hideaki }
35123c21edbdSYOSHIFUJI Hideaki 
3513c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
35141da177e4SLinus Torvalds {
35151da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
35161da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
35171da177e4SLinus Torvalds 
35181da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
35191da177e4SLinus Torvalds 
352063862b5bSAruna-Hewapathirane 	prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
35211da177e4SLinus Torvalds 
35221da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3523622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3524e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
35251da177e4SLinus Torvalds 		goto out;
35261da177e4SLinus Torvalds 
35271da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
35281b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
352955ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
353055ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3531cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
353221809fafSstephen hemminger 		spin_unlock(&ifp->lock);
35331da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
35341da177e4SLinus Torvalds 
35351da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
35361da177e4SLinus Torvalds 		return;
35371da177e4SLinus Torvalds 	}
35381da177e4SLinus Torvalds 
35396732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
354021809fafSstephen hemminger 		spin_unlock(&ifp->lock);
35416732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
35423c21edbdSYOSHIFUJI Hideaki 		/*
3543590a9887SMasatake YAMATO 		 * If the device is not ready:
35443c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
35453c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
35463c21edbdSYOSHIFUJI Hideaki 		 */
35473c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
3548cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
35496732badeSYOSHIFUJI Hideaki 		return;
35503c21edbdSYOSHIFUJI Hideaki 	}
355195c385b4SNeil Horman 
355295c385b4SNeil Horman 	/*
355395c385b4SNeil Horman 	 * Optimistic nodes can start receiving
355495c385b4SNeil Horman 	 * Frames right away
355595c385b4SNeil Horman 	 */
35567fd2561eSErik Kline 	if (ifp->flags & IFA_F_OPTIMISTIC) {
355795c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
35587fd2561eSErik Kline 		if (ipv6_use_optimistic_addr(idev)) {
35597fd2561eSErik Kline 			/* Because optimistic nodes can use this address,
35607fd2561eSErik Kline 			 * notify listeners. If DAD fails, RTM_DELADDR is sent.
35617fd2561eSErik Kline 			 */
35627fd2561eSErik Kline 			ipv6_ifa_notify(RTM_NEWADDR, ifp);
35637fd2561eSErik Kline 		}
35647fd2561eSErik Kline 	}
356595c385b4SNeil Horman 
35666732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
35671da177e4SLinus Torvalds out:
3568622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
35691da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
35701da177e4SLinus Torvalds }
35711da177e4SLinus Torvalds 
3572c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp)
35731da177e4SLinus Torvalds {
3574c15b1ccaSHannes Frederic Sowa 	bool begin_dad = false;
3575c15b1ccaSHannes Frederic Sowa 
35768e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3577c15b1ccaSHannes Frederic Sowa 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3578c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_PREDAD;
3579c15b1ccaSHannes Frederic Sowa 		begin_dad = true;
3580c15b1ccaSHannes Frederic Sowa 	}
35818e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3582c15b1ccaSHannes Frederic Sowa 
3583c15b1ccaSHannes Frederic Sowa 	if (begin_dad)
3584c15b1ccaSHannes Frederic Sowa 		addrconf_mod_dad_work(ifp, 0);
3585c15b1ccaSHannes Frederic Sowa }
3586c15b1ccaSHannes Frederic Sowa 
3587c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w)
3588c15b1ccaSHannes Frederic Sowa {
3589c15b1ccaSHannes Frederic Sowa 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3590c15b1ccaSHannes Frederic Sowa 						struct inet6_ifaddr,
3591c15b1ccaSHannes Frederic Sowa 						dad_work);
35921da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
35931da177e4SLinus Torvalds 	struct in6_addr mcaddr;
35941da177e4SLinus Torvalds 
3595c15b1ccaSHannes Frederic Sowa 	enum {
3596c15b1ccaSHannes Frederic Sowa 		DAD_PROCESS,
3597c15b1ccaSHannes Frederic Sowa 		DAD_BEGIN,
3598c15b1ccaSHannes Frederic Sowa 		DAD_ABORT,
3599c15b1ccaSHannes Frederic Sowa 	} action = DAD_PROCESS;
3600c15b1ccaSHannes Frederic Sowa 
3601c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
3602c15b1ccaSHannes Frederic Sowa 
36038e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3604c15b1ccaSHannes Frederic Sowa 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3605c15b1ccaSHannes Frederic Sowa 		action = DAD_BEGIN;
3606c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_DAD;
3607c15b1ccaSHannes Frederic Sowa 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3608c15b1ccaSHannes Frederic Sowa 		action = DAD_ABORT;
3609c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
3610c15b1ccaSHannes Frederic Sowa 	}
36118e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3612c15b1ccaSHannes Frederic Sowa 
3613c15b1ccaSHannes Frederic Sowa 	if (action == DAD_BEGIN) {
3614c15b1ccaSHannes Frederic Sowa 		addrconf_dad_begin(ifp);
3615c15b1ccaSHannes Frederic Sowa 		goto out;
3616c15b1ccaSHannes Frederic Sowa 	} else if (action == DAD_ABORT) {
3617c15b1ccaSHannes Frederic Sowa 		addrconf_dad_stop(ifp, 1);
3618c15b1ccaSHannes Frederic Sowa 		goto out;
3619c15b1ccaSHannes Frederic Sowa 	}
3620c15b1ccaSHannes Frederic Sowa 
3621b7b1bfceSHannes Frederic Sowa 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
3622f2344a13SHerbert Xu 		goto out;
3623f2344a13SHerbert Xu 
3624c15b1ccaSHannes Frederic Sowa 	write_lock_bh(&idev->lock);
3625122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
3626c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
36271da177e4SLinus Torvalds 		goto out;
36281da177e4SLinus Torvalds 	}
362921809fafSstephen hemminger 
363021809fafSstephen hemminger 	spin_lock(&ifp->lock);
3631622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3632622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
3633c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
3634622ccdf1SHerbert Xu 		goto out;
3635622ccdf1SHerbert Xu 	}
3636622ccdf1SHerbert Xu 
3637b7b1bfceSHannes Frederic Sowa 	if (ifp->dad_probes == 0) {
36381da177e4SLinus Torvalds 		/*
36391da177e4SLinus Torvalds 		 * DAD was successful
36401da177e4SLinus Torvalds 		 */
36411da177e4SLinus Torvalds 
3642cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
364321809fafSstephen hemminger 		spin_unlock(&ifp->lock);
3644c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
36451da177e4SLinus Torvalds 
36461da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
36471da177e4SLinus Torvalds 
36481da177e4SLinus Torvalds 		goto out;
36491da177e4SLinus Torvalds 	}
36501da177e4SLinus Torvalds 
3651b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes--;
3652c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp,
36531f9248e5SJiri Pirko 			      NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
365421809fafSstephen hemminger 	spin_unlock(&ifp->lock);
3655c15b1ccaSHannes Frederic Sowa 	write_unlock_bh(&idev->lock);
36561da177e4SLinus Torvalds 
36571da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
36581da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3659d7aabf22SYOSHIFUJI Hideaki 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
36601da177e4SLinus Torvalds out:
36611da177e4SLinus Torvalds 	in6_ifa_put(ifp);
3662c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
36631da177e4SLinus Torvalds }
36641da177e4SLinus Torvalds 
366511ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */
366611ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
366711ffff75SHannes Frederic Sowa {
366811ffff75SHannes Frederic Sowa 	struct inet6_ifaddr *ifpiter;
366911ffff75SHannes Frederic Sowa 	struct inet6_dev *idev = ifp->idev;
367011ffff75SHannes Frederic Sowa 
3671602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3672602582caSHannes Frederic Sowa 		if (ifpiter->scope > IFA_LINK)
3673602582caSHannes Frederic Sowa 			break;
367411ffff75SHannes Frederic Sowa 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
367511ffff75SHannes Frederic Sowa 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
367611ffff75SHannes Frederic Sowa 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
367711ffff75SHannes Frederic Sowa 		    IFA_F_PERMANENT)
367811ffff75SHannes Frederic Sowa 			return false;
367911ffff75SHannes Frederic Sowa 	}
368011ffff75SHannes Frederic Sowa 	return true;
368111ffff75SHannes Frederic Sowa }
368211ffff75SHannes Frederic Sowa 
36831da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
36841da177e4SLinus Torvalds {
36851da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
3686b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
3687b173ee48SHannes Frederic Sowa 	bool send_rs, send_mld;
3688b7b1bfceSHannes Frederic Sowa 
3689c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
36901da177e4SLinus Torvalds 
36911da177e4SLinus Torvalds 	/*
36921da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
36931da177e4SLinus Torvalds 	 */
36941da177e4SLinus Torvalds 
36951da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
36961da177e4SLinus Torvalds 
3697026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
3698026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
36991da177e4SLinus Torvalds 	 */
37001da177e4SLinus Torvalds 
37011ec047ebSHannes Frederic Sowa 	read_lock_bh(&ifp->idev->lock);
370211ffff75SHannes Frederic Sowa 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3703b173ee48SHannes Frederic Sowa 	send_rs = send_mld &&
3704b173ee48SHannes Frederic Sowa 		  ipv6_accept_ra(ifp->idev) &&
3705b173ee48SHannes Frederic Sowa 		  ifp->idev->cnf.rtr_solicits > 0 &&
3706b173ee48SHannes Frederic Sowa 		  (dev->flags&IFF_LOOPBACK) == 0;
37071ec047ebSHannes Frederic Sowa 	read_unlock_bh(&ifp->idev->lock);
37081ec047ebSHannes Frederic Sowa 
3709b173ee48SHannes Frederic Sowa 	/* While dad is in progress mld report's source address is in6_addrany.
3710b173ee48SHannes Frederic Sowa 	 * Resend with proper ll now.
3711b173ee48SHannes Frederic Sowa 	 */
3712b173ee48SHannes Frederic Sowa 	if (send_mld)
3713b173ee48SHannes Frederic Sowa 		ipv6_mc_dad_complete(ifp->idev);
3714b173ee48SHannes Frederic Sowa 
37151ec047ebSHannes Frederic Sowa 	if (send_rs) {
37161da177e4SLinus Torvalds 		/*
37171da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
37181da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
37191da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
37201da177e4SLinus Torvalds 		 */
37211ec047ebSHannes Frederic Sowa 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3722b7b1bfceSHannes Frederic Sowa 			return;
37231ec047ebSHannes Frederic Sowa 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
37241da177e4SLinus Torvalds 
3725b7b1bfceSHannes Frederic Sowa 		write_lock_bh(&ifp->idev->lock);
3726b7b1bfceSHannes Frederic Sowa 		spin_lock(&ifp->lock);
3727b7b1bfceSHannes Frederic Sowa 		ifp->idev->rs_probes = 1;
37281da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
3729b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(ifp->idev,
3730b7b1bfceSHannes Frederic Sowa 				      ifp->idev->cnf.rtr_solicit_interval);
3731b7b1bfceSHannes Frederic Sowa 		spin_unlock(&ifp->lock);
3732b7b1bfceSHannes Frederic Sowa 		write_unlock_bh(&ifp->idev->lock);
37331da177e4SLinus Torvalds 	}
37341da177e4SLinus Torvalds }
37351da177e4SLinus Torvalds 
3736e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev)
3737e21e8467SStephen Hemminger {
3738c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
3739c5e33bddSYOSHIFUJI Hideaki 
3740c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
3741502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
374221809fafSstephen hemminger 		spin_lock(&ifp->lock);
3743f2344a13SHerbert Xu 		if (ifp->flags & IFA_F_TENTATIVE &&
3744f2344a13SHerbert Xu 		    ifp->state == INET6_IFADDR_STATE_DAD)
3745c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
3746f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
3747c5e33bddSYOSHIFUJI Hideaki 	}
3748c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
3749c5e33bddSYOSHIFUJI Hideaki }
3750c5e33bddSYOSHIFUJI Hideaki 
37511da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
37521da177e4SLinus Torvalds struct if6_iter_state {
37533c40090aSDaniel Lezcano 	struct seq_net_private p;
37541da177e4SLinus Torvalds 	int bucket;
37551d578303SMihai Maruseac 	int offset;
37561da177e4SLinus Torvalds };
37571da177e4SLinus Torvalds 
37581d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
37591da177e4SLinus Torvalds {
37601da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
37611da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
37621218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
37631d578303SMihai Maruseac 	int p = 0;
37641da177e4SLinus Torvalds 
37651d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
37661d578303SMihai Maruseac 	if (pos == 0) {
37671d578303SMihai Maruseac 		state->bucket = 0;
37681d578303SMihai Maruseac 		state->offset = 0;
37691d578303SMihai Maruseac 	}
37701d578303SMihai Maruseac 
37711d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3772b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
37731d578303SMihai Maruseac 					 addr_lst) {
37749f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
37759f0d3c27SEric Dumazet 				continue;
37761d578303SMihai Maruseac 			/* sync with offset */
37771d578303SMihai Maruseac 			if (p < state->offset) {
37781d578303SMihai Maruseac 				p++;
37791d578303SMihai Maruseac 				continue;
37801d578303SMihai Maruseac 			}
37811d578303SMihai Maruseac 			state->offset++;
37821da177e4SLinus Torvalds 			return ifa;
37831da177e4SLinus Torvalds 		}
37841d578303SMihai Maruseac 
37851d578303SMihai Maruseac 		/* prepare for next bucket */
37861d578303SMihai Maruseac 		state->offset = 0;
37871d578303SMihai Maruseac 		p = 0;
37881d578303SMihai Maruseac 	}
3789c2e21293Sstephen hemminger 	return NULL;
3790c2e21293Sstephen hemminger }
37911da177e4SLinus Torvalds 
3792c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3793c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
37941da177e4SLinus Torvalds {
37951da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
37961218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
37971da177e4SLinus Torvalds 
3798b67bfe0dSSasha Levin 	hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
37999f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
38009f0d3c27SEric Dumazet 			continue;
38011d578303SMihai Maruseac 		state->offset++;
38021da177e4SLinus Torvalds 		return ifa;
38031d578303SMihai Maruseac 	}
38041da177e4SLinus Torvalds 
3805c2e21293Sstephen hemminger 	while (++state->bucket < IN6_ADDR_HSIZE) {
38061d578303SMihai Maruseac 		state->offset = 0;
3807b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa,
3808c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
38099f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
38109f0d3c27SEric Dumazet 				continue;
38111d578303SMihai Maruseac 			state->offset++;
3812c2e21293Sstephen hemminger 			return ifa;
3813c2e21293Sstephen hemminger 		}
3814c2e21293Sstephen hemminger 	}
3815c2e21293Sstephen hemminger 
3816c2e21293Sstephen hemminger 	return NULL;
3817c2e21293Sstephen hemminger }
3818c2e21293Sstephen hemminger 
38191da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
38204f70eccaSEric Dumazet 	__acquires(rcu_bh)
38211da177e4SLinus Torvalds {
38225c578aedSstephen hemminger 	rcu_read_lock_bh();
38231d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
38241da177e4SLinus Torvalds }
38251da177e4SLinus Torvalds 
38261da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
38271da177e4SLinus Torvalds {
38281da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
38291da177e4SLinus Torvalds 
38301da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
38311da177e4SLinus Torvalds 	++*pos;
38321da177e4SLinus Torvalds 	return ifa;
38331da177e4SLinus Torvalds }
38341da177e4SLinus Torvalds 
38351da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
38364f70eccaSEric Dumazet 	__releases(rcu_bh)
38371da177e4SLinus Torvalds {
38385c578aedSstephen hemminger 	rcu_read_unlock_bh();
38391da177e4SLinus Torvalds }
38401da177e4SLinus Torvalds 
38411da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
38421da177e4SLinus Torvalds {
38431da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3844971a351cSJiri Pirko 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3845b071195dSHarvey Harrison 		   &ifp->addr,
38461da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
38471da177e4SLinus Torvalds 		   ifp->prefix_len,
38481da177e4SLinus Torvalds 		   ifp->scope,
3849971a351cSJiri Pirko 		   (u8) ifp->flags,
38501da177e4SLinus Torvalds 		   ifp->idev->dev->name);
38511da177e4SLinus Torvalds 	return 0;
38521da177e4SLinus Torvalds }
38531da177e4SLinus Torvalds 
385456b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
38551da177e4SLinus Torvalds 	.start	= if6_seq_start,
38561da177e4SLinus Torvalds 	.next	= if6_seq_next,
38571da177e4SLinus Torvalds 	.show	= if6_seq_show,
38581da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
38591da177e4SLinus Torvalds };
38601da177e4SLinus Torvalds 
38611da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
38621da177e4SLinus Torvalds {
38633c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
3864cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
38651da177e4SLinus Torvalds }
38661da177e4SLinus Torvalds 
38679a32144eSArjan van de Ven static const struct file_operations if6_fops = {
38681da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
38691da177e4SLinus Torvalds 	.open		= if6_seq_open,
38701da177e4SLinus Torvalds 	.read		= seq_read,
38711da177e4SLinus Torvalds 	.llseek		= seq_lseek,
38723c40090aSDaniel Lezcano 	.release	= seq_release_net,
38731da177e4SLinus Torvalds };
38741da177e4SLinus Torvalds 
38752c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
38761da177e4SLinus Torvalds {
3877d4beaa66SGao feng 	if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
38781da177e4SLinus Torvalds 		return -ENOMEM;
38791da177e4SLinus Torvalds 	return 0;
38801da177e4SLinus Torvalds }
38811da177e4SLinus Torvalds 
38822c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
38833c40090aSDaniel Lezcano {
3884ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
38853c40090aSDaniel Lezcano }
38863c40090aSDaniel Lezcano 
38873c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
38883c40090aSDaniel Lezcano 	.init = if6_proc_net_init,
38893c40090aSDaniel Lezcano 	.exit = if6_proc_net_exit,
38903c40090aSDaniel Lezcano };
38913c40090aSDaniel Lezcano 
38923c40090aSDaniel Lezcano int __init if6_proc_init(void)
38933c40090aSDaniel Lezcano {
38943c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
38953c40090aSDaniel Lezcano }
38963c40090aSDaniel Lezcano 
38971da177e4SLinus Torvalds void if6_proc_exit(void)
38981da177e4SLinus Torvalds {
38993c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
39001da177e4SLinus Torvalds }
39011da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
39021da177e4SLinus Torvalds 
390307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
39043b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
3905b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
39063b9f9a1cSNoriaki TAKAMIYA {
39073b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
3908c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
3909ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
3910c2e21293Sstephen hemminger 
39115c578aedSstephen hemminger 	rcu_read_lock_bh();
3912b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3913878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
3914389f6612SDaniel Lezcano 			continue;
3915caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
39163b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
39173b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
39183b9f9a1cSNoriaki TAKAMIYA 			break;
39193b9f9a1cSNoriaki TAKAMIYA 		}
39203b9f9a1cSNoriaki TAKAMIYA 	}
39215c578aedSstephen hemminger 	rcu_read_unlock_bh();
39223b9f9a1cSNoriaki TAKAMIYA 	return ret;
39233b9f9a1cSNoriaki TAKAMIYA }
39243b9f9a1cSNoriaki TAKAMIYA #endif
39253b9f9a1cSNoriaki TAKAMIYA 
39261da177e4SLinus Torvalds /*
39271da177e4SLinus Torvalds  *	Periodic address status verification
39281da177e4SLinus Torvalds  */
39291da177e4SLinus Torvalds 
3930c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void)
39311da177e4SLinus Torvalds {
3932b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
39331da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
39341da177e4SLinus Torvalds 	int i;
39351da177e4SLinus Torvalds 
3936c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
3937c15b1ccaSHannes Frederic Sowa 
39385c578aedSstephen hemminger 	rcu_read_lock_bh();
39391da177e4SLinus Torvalds 	now = jiffies;
3940b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
39411da177e4SLinus Torvalds 
3942c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
39431da177e4SLinus Torvalds 
39441da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
39451da177e4SLinus Torvalds restart:
3946c15b1ccaSHannes Frederic Sowa 		hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
39471da177e4SLinus Torvalds 			unsigned long age;
39481da177e4SLinus Torvalds 
3949fad8da3eSYasushi Asano 			/* When setting preferred_lft to a value not zero or
3950fad8da3eSYasushi Asano 			 * infinity, while valid_lft is infinity
3951fad8da3eSYasushi Asano 			 * IFA_F_PERMANENT has a non-infinity life time.
3952fad8da3eSYasushi Asano 			 */
3953fad8da3eSYasushi Asano 			if ((ifp->flags & IFA_F_PERMANENT) &&
3954fad8da3eSYasushi Asano 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
39551da177e4SLinus Torvalds 				continue;
39561da177e4SLinus Torvalds 
39571da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
3958b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
3959b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
39601da177e4SLinus Torvalds 
39618f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
39628f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
39631da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
39641da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
39651da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
39661da177e4SLinus Torvalds 				goto restart;
39678f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
39688f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
39698f27ebb9SYOSHIFUJI Hideaki 				continue;
39701da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
3971a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
39721da177e4SLinus Torvalds 				int deprecate = 0;
39731da177e4SLinus Torvalds 
39741da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
39751da177e4SLinus Torvalds 					deprecate = 1;
39761da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
39771da177e4SLinus Torvalds 				}
39781da177e4SLinus Torvalds 
3979fad8da3eSYasushi Asano 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
3980fad8da3eSYasushi Asano 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
39811da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
39821da177e4SLinus Torvalds 
39831da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
39841da177e4SLinus Torvalds 
39851da177e4SLinus Torvalds 				if (deprecate) {
39861da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
39871da177e4SLinus Torvalds 
39881da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
39891da177e4SLinus Torvalds 					in6_ifa_put(ifp);
39901da177e4SLinus Torvalds 					goto restart;
39911da177e4SLinus Torvalds 				}
39921da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
39931da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
399488949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
399588949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
39961f9248e5SJiri Pirko 					NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
399788949cf4Sstephen hemminger 
39981da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
39991da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
40001da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
40011da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
40021da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
40031da177e4SLinus Torvalds 						ifp->regen_count++;
40041da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
40051da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
40061da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
40075c578aedSstephen hemminger 
4008291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
4009291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
4010291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
40111da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
40121da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
40131da177e4SLinus Torvalds 						in6_ifa_put(ifp);
40141da177e4SLinus Torvalds 						goto restart;
40151da177e4SLinus Torvalds 					}
40161da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
40171da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
40181da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
40191da177e4SLinus Torvalds 			} else {
40201da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
40211da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
40221da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
40231da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
40241da177e4SLinus Torvalds 			}
40251da177e4SLinus Torvalds 		}
40261da177e4SLinus Torvalds 	}
40271da177e4SLinus Torvalds 
4028b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
4029b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
4030b2db7564SYOSHIFUJI Hideaki 
4031b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
4032b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4033b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
4034b2db7564SYOSHIFUJI Hideaki 
4035b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4036b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4037b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4038b2db7564SYOSHIFUJI Hideaki 
4039ba3542e1Sdingtianhong 	ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4040ba3542e1Sdingtianhong 	      now, next, next_sec, next_sched);
4041c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
40425c578aedSstephen hemminger 	rcu_read_unlock_bh();
40431da177e4SLinus Torvalds }
40441da177e4SLinus Torvalds 
4045c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w)
4046c15b1ccaSHannes Frederic Sowa {
4047c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
4048c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4049c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
4050c15b1ccaSHannes Frederic Sowa }
4051c15b1ccaSHannes Frederic Sowa 
4052c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void)
4053c15b1ccaSHannes Frederic Sowa {
4054c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4055c15b1ccaSHannes Frederic Sowa }
4056c15b1ccaSHannes Frederic Sowa 
4057caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4058caeaba79SNicolas Dichtel 				     struct in6_addr **peer_pfx)
4059461d8837SThomas Graf {
4060461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
4061461d8837SThomas Graf 
4062caeaba79SNicolas Dichtel 	*peer_pfx = NULL;
4063caeaba79SNicolas Dichtel 
4064461d8837SThomas Graf 	if (addr)
4065461d8837SThomas Graf 		pfx = nla_data(addr);
4066461d8837SThomas Graf 
4067461d8837SThomas Graf 	if (local) {
4068461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4069caeaba79SNicolas Dichtel 			*peer_pfx = pfx;
4070461d8837SThomas Graf 		pfx = nla_data(local);
4071461d8837SThomas Graf 	}
4072461d8837SThomas Graf 
4073461d8837SThomas Graf 	return pfx;
4074461d8837SThomas Graf }
4075461d8837SThomas Graf 
4076ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4077461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
4078461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
4079461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
4080479840ffSJiri Pirko 	[IFA_FLAGS]		= { .len = sizeof(u32) },
4081461d8837SThomas Graf };
4082461d8837SThomas Graf 
40831da177e4SLinus Torvalds static int
4084661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
40851da177e4SLinus Torvalds {
40863b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4087b933f716SThomas Graf 	struct ifaddrmsg *ifm;
4088b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4089caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
40906046d5b4SHeiner Kallweit 	u32 ifa_flags;
4091b933f716SThomas Graf 	int err;
40921da177e4SLinus Torvalds 
4093b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4094b933f716SThomas Graf 	if (err < 0)
4095b933f716SThomas Graf 		return err;
4096b933f716SThomas Graf 
4097b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
4098caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
409963159f29SIan Morris 	if (!pfx)
41001da177e4SLinus Torvalds 		return -EINVAL;
41011da177e4SLinus Torvalds 
41026046d5b4SHeiner Kallweit 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
41036046d5b4SHeiner Kallweit 
41046046d5b4SHeiner Kallweit 	/* We ignore other flags so far. */
41056046d5b4SHeiner Kallweit 	ifa_flags &= IFA_F_MANAGETEMPADDR;
41066046d5b4SHeiner Kallweit 
41076046d5b4SHeiner Kallweit 	return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
41086046d5b4SHeiner Kallweit 			      ifm->ifa_prefixlen);
41091da177e4SLinus Torvalds }
41101da177e4SLinus Torvalds 
4111479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
411255ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
4113081bba5bSNoriaki TAKAMIYA {
41146f704992SYOSHIFUJI Hideaki 	u32 flags;
41156f704992SYOSHIFUJI Hideaki 	clock_t expires;
41164bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
411753bd6749SJiri Pirko 	bool was_managetempaddr;
41185b84efecSThomas Haller 	bool had_prefixroute;
411946d48046SYOSHIFUJI Hideaki 
4120c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4121c15b1ccaSHannes Frederic Sowa 
4122081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
4123081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
4124081bba5bSNoriaki TAKAMIYA 
412553bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR &&
412653bd6749SJiri Pirko 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
412753bd6749SJiri Pirko 		return -EINVAL;
412853bd6749SJiri Pirko 
41294bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
41304bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
41314bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
41324bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
41336f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
41344bed72e4SYOSHIFUJI Hideaki 	} else {
41354bed72e4SYOSHIFUJI Hideaki 		expires = 0;
41364bed72e4SYOSHIFUJI Hideaki 		flags = 0;
41374bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
41386f704992SYOSHIFUJI Hideaki 	}
4139081bba5bSNoriaki TAKAMIYA 
41404bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
41414bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
41424bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
414355ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
41444bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
41454bed72e4SYOSHIFUJI Hideaki 	}
4146081bba5bSNoriaki TAKAMIYA 
4147081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
414853bd6749SJiri Pirko 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
41495b84efecSThomas Haller 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
41505b84efecSThomas Haller 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
415153bd6749SJiri Pirko 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4152761aac73SThomas Haller 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4153761aac73SThomas Haller 			IFA_F_NOPREFIXROUTE);
415453bd6749SJiri Pirko 	ifp->flags |= ifa_flags;
4155081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
4156081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
4157081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
4158081bba5bSNoriaki TAKAMIYA 
4159081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
4160081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
4161081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
4162081bba5bSNoriaki TAKAMIYA 
4163761aac73SThomas Haller 	if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
416446d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
41656f704992SYOSHIFUJI Hideaki 				      expires, flags);
41665b84efecSThomas Haller 	} else if (had_prefixroute) {
41675b84efecSThomas Haller 		enum cleanup_prefix_rt_t action;
41685b84efecSThomas Haller 		unsigned long rt_expires;
41695b84efecSThomas Haller 
41705b84efecSThomas Haller 		write_lock_bh(&ifp->idev->lock);
41715b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &rt_expires);
41725b84efecSThomas Haller 		write_unlock_bh(&ifp->idev->lock);
41735b84efecSThomas Haller 
41745b84efecSThomas Haller 		if (action != CLEANUP_PREFIX_RT_NOP) {
41755b84efecSThomas Haller 			cleanup_prefix_route(ifp, rt_expires,
41765b84efecSThomas Haller 				action == CLEANUP_PREFIX_RT_DEL);
41775b84efecSThomas Haller 		}
4178761aac73SThomas Haller 	}
417953bd6749SJiri Pirko 
418053bd6749SJiri Pirko 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
418153bd6749SJiri Pirko 		if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
418253bd6749SJiri Pirko 			valid_lft = prefered_lft = 0;
418353bd6749SJiri Pirko 		manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
418453bd6749SJiri Pirko 				 !was_managetempaddr, jiffies);
418553bd6749SJiri Pirko 	}
418653bd6749SJiri Pirko 
4187c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4188081bba5bSNoriaki TAKAMIYA 
4189081bba5bSNoriaki TAKAMIYA 	return 0;
4190081bba5bSNoriaki TAKAMIYA }
4191081bba5bSNoriaki TAKAMIYA 
4192081bba5bSNoriaki TAKAMIYA static int
4193661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
41941da177e4SLinus Torvalds {
41953b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4196461d8837SThomas Graf 	struct ifaddrmsg *ifm;
4197461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4198caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
41997198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
42007198f8ceSThomas Graf 	struct net_device *dev;
420155ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4202479840ffSJiri Pirko 	u32 ifa_flags;
4203461d8837SThomas Graf 	int err;
42041da177e4SLinus Torvalds 
4205461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4206461d8837SThomas Graf 	if (err < 0)
4207461d8837SThomas Graf 		return err;
4208461d8837SThomas Graf 
4209461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
4210caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
421163159f29SIan Morris 	if (!pfx)
42121da177e4SLinus Torvalds 		return -EINVAL;
42131da177e4SLinus Torvalds 
4214461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
42150778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
4216461d8837SThomas Graf 
4217461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
42180778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
4219461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
4220461d8837SThomas Graf 	} else {
4221461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
4222461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
42230778769dSNoriaki TAKAMIYA 	}
42240778769dSNoriaki TAKAMIYA 
4225af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
422663159f29SIan Morris 	if (!dev)
42277198f8ceSThomas Graf 		return -ENODEV;
4228081bba5bSNoriaki TAKAMIYA 
4229479840ffSJiri Pirko 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4230479840ffSJiri Pirko 
423155ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
4232761aac73SThomas Haller 	ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
423393a714d6SMadhu Challa 		     IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
423455ebaef1SNoriaki TAKAMIYA 
42351cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
423663159f29SIan Morris 	if (!ifa) {
42377198f8ceSThomas Graf 		/*
42387198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
4239db9c7c39Sstephen hemminger 		 * userspace already relies on not having to provide this.
42407198f8ceSThomas Graf 		 */
4241caeaba79SNicolas Dichtel 		return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4242af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
4243af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
42441da177e4SLinus Torvalds 	}
42451da177e4SLinus Torvalds 
42467198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
42477198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
42487198f8ceSThomas Graf 		err = -EEXIST;
42497198f8ceSThomas Graf 	else
425055ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
42517198f8ceSThomas Graf 
42527198f8ceSThomas Graf 	in6_ifa_put(ifa);
42537198f8ceSThomas Graf 
42547198f8ceSThomas Graf 	return err;
42557198f8ceSThomas Graf }
42567198f8ceSThomas Graf 
4257479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4258101bb229SThomas Graf 			  u8 scope, int ifindex)
4259101bb229SThomas Graf {
4260101bb229SThomas Graf 	struct ifaddrmsg *ifm;
4261101bb229SThomas Graf 
4262101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
4263101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
4264101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
4265101bb229SThomas Graf 	ifm->ifa_flags = flags;
4266101bb229SThomas Graf 	ifm->ifa_scope = scope;
4267101bb229SThomas Graf 	ifm->ifa_index = ifindex;
4268101bb229SThomas Graf }
4269101bb229SThomas Graf 
427085486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
427185486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
427285486af0SThomas Graf {
427385486af0SThomas Graf 	struct ifa_cacheinfo ci;
427485486af0SThomas Graf 
427518a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
427618a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
427785486af0SThomas Graf 	ci.ifa_prefered = preferred;
427885486af0SThomas Graf 	ci.ifa_valid = valid;
427985486af0SThomas Graf 
428085486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
428185486af0SThomas Graf }
428285486af0SThomas Graf 
4283101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
4284101bb229SThomas Graf {
4285101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
4286101bb229SThomas Graf 		return RT_SCOPE_HOST;
4287101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
4288101bb229SThomas Graf 		return RT_SCOPE_LINK;
4289101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
4290101bb229SThomas Graf 		return RT_SCOPE_SITE;
4291101bb229SThomas Graf 	else
4292101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
4293101bb229SThomas Graf }
4294101bb229SThomas Graf 
42950ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
42960ab6803bSThomas Graf {
4297339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4298caeaba79SNicolas Dichtel 	       + nla_total_size(16) /* IFA_LOCAL */
4299339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
4300479840ffSJiri Pirko 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
4301479840ffSJiri Pirko 	       + nla_total_size(4)  /* IFA_FLAGS */;
43020ab6803bSThomas Graf }
4303c5396a31SYOSHIFUJI Hideaki 
43041da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
430515e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
43061da177e4SLinus Torvalds {
43071da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
430885486af0SThomas Graf 	u32 preferred, valid;
43091da177e4SLinus Torvalds 
431015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
431163159f29SIan Morris 	if (!nlh)
431226932566SPatrick McHardy 		return -EMSGSIZE;
43130ab6803bSThomas Graf 
4314101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4315101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
4316101bb229SThomas Graf 
4317fad8da3eSYasushi Asano 	if (!((ifa->flags&IFA_F_PERMANENT) &&
4318fad8da3eSYasushi Asano 	      (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
431985486af0SThomas Graf 		preferred = ifa->prefered_lft;
432085486af0SThomas Graf 		valid = ifa->valid_lft;
432185486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
43221da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
4323a1faa698SJens Rosenboom 			if (preferred > tval)
432485486af0SThomas Graf 				preferred -= tval;
4325a1faa698SJens Rosenboom 			else
4326a1faa698SJens Rosenboom 				preferred = 0;
4327f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
4328f56619fcSBen Hutchings 				if (valid > tval)
432985486af0SThomas Graf 					valid -= tval;
4330f56619fcSBen Hutchings 				else
4331f56619fcSBen Hutchings 					valid = 0;
4332f56619fcSBen Hutchings 			}
43331da177e4SLinus Torvalds 		}
43341da177e4SLinus Torvalds 	} else {
433585486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
433685486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
43371da177e4SLinus Torvalds 	}
433885486af0SThomas Graf 
43397996c799SCong Wang 	if (!ipv6_addr_any(&ifa->peer_addr)) {
4340930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4341930345eaSJiri Benc 		    nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4342caeaba79SNicolas Dichtel 			goto error;
4343caeaba79SNicolas Dichtel 	} else
4344930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4345caeaba79SNicolas Dichtel 			goto error;
4346caeaba79SNicolas Dichtel 
4347caeaba79SNicolas Dichtel 	if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4348caeaba79SNicolas Dichtel 		goto error;
434985486af0SThomas Graf 
4350479840ffSJiri Pirko 	if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4351479840ffSJiri Pirko 		goto error;
4352479840ffSJiri Pirko 
4353053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4354053c095aSJohannes Berg 	return 0;
4355caeaba79SNicolas Dichtel 
4356caeaba79SNicolas Dichtel error:
4357caeaba79SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
4358caeaba79SNicolas Dichtel 	return -EMSGSIZE;
43591da177e4SLinus Torvalds }
43601da177e4SLinus Torvalds 
43611da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
436215e47304SEric W. Biederman 				u32 portid, u32 seq, int event, u16 flags)
43631da177e4SLinus Torvalds {
43641da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4365101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4366101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
43671da177e4SLinus Torvalds 
43681da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4369101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4370101bb229SThomas Graf 
437115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
437263159f29SIan Morris 	if (!nlh)
437326932566SPatrick McHardy 		return -EMSGSIZE;
43740ab6803bSThomas Graf 
4375101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4376930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
43770ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
437826932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
437926932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
438026932566SPatrick McHardy 		return -EMSGSIZE;
438126932566SPatrick McHardy 	}
438285486af0SThomas Graf 
4383053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4384053c095aSJohannes Berg 	return 0;
43851da177e4SLinus Torvalds }
43861da177e4SLinus Torvalds 
43871da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
438815e47304SEric W. Biederman 				u32 portid, u32 seq, int event, unsigned int flags)
43891da177e4SLinus Torvalds {
43901da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4391101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4392101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
43931da177e4SLinus Torvalds 
43941da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4395101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4396101bb229SThomas Graf 
439715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
439863159f29SIan Morris 	if (!nlh)
439926932566SPatrick McHardy 		return -EMSGSIZE;
44000ab6803bSThomas Graf 
4401101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4402930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
44030ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
440426932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
440526932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
440626932566SPatrick McHardy 		return -EMSGSIZE;
440726932566SPatrick McHardy 	}
440885486af0SThomas Graf 
4409053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4410053c095aSJohannes Berg 	return 0;
44111da177e4SLinus Torvalds }
44121da177e4SLinus Torvalds 
4413e21e8467SStephen Hemminger enum addr_type_t {
44141da177e4SLinus Torvalds 	UNICAST_ADDR,
44151da177e4SLinus Torvalds 	MULTICAST_ADDR,
44161da177e4SLinus Torvalds 	ANYCAST_ADDR,
44171da177e4SLinus Torvalds };
44181da177e4SLinus Torvalds 
4419234b27c3SEric Dumazet /* called with rcu_read_lock() */
4420234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4421234b27c3SEric Dumazet 			  struct netlink_callback *cb, enum addr_type_t type,
4422234b27c3SEric Dumazet 			  int s_ip_idx, int *p_ip_idx)
44231da177e4SLinus Torvalds {
44241da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
44251da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
4426234b27c3SEric Dumazet 	int err = 1;
4427234b27c3SEric Dumazet 	int ip_idx = *p_ip_idx;
44281da177e4SLinus Torvalds 
44291da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
44301da177e4SLinus Torvalds 	switch (type) {
4431502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
4432502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
4433502a2ffdSstephen hemminger 
4434ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
4435502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
4436502a2ffdSstephen hemminger 			if (++ip_idx < s_ip_idx)
44371da177e4SLinus Torvalds 				continue;
44385d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifaddr(skb, ifa,
443915e47304SEric W. Biederman 						NETLINK_CB(cb->skb).portid,
44405d5619b4SYOSHIFUJI Hideaki 						cb->nlh->nlmsg_seq,
44415d5619b4SYOSHIFUJI Hideaki 						RTM_NEWADDR,
44425d5619b4SYOSHIFUJI Hideaki 						NLM_F_MULTI);
4443053c095aSJohannes Berg 			if (err < 0)
4444bcd32326SEric Dumazet 				break;
444563998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
44461da177e4SLinus Torvalds 		}
44471da177e4SLinus Torvalds 		break;
4448502a2ffdSstephen hemminger 	}
44491da177e4SLinus Torvalds 	case MULTICAST_ADDR:
44501da177e4SLinus Torvalds 		/* multicast address */
44511da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
44521da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
44531da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
44541da177e4SLinus Torvalds 				continue;
44555d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifmcaddr(skb, ifmca,
445615e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
44575d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
44585d5619b4SYOSHIFUJI Hideaki 						  RTM_GETMULTICAST,
44595d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4460053c095aSJohannes Berg 			if (err < 0)
4461bcd32326SEric Dumazet 				break;
44621da177e4SLinus Torvalds 		}
44631da177e4SLinus Torvalds 		break;
44641da177e4SLinus Torvalds 	case ANYCAST_ADDR:
44651da177e4SLinus Torvalds 		/* anycast address */
44661da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
44671da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
44681da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
44691da177e4SLinus Torvalds 				continue;
44705d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifacaddr(skb, ifaca,
447115e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
44725d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
44735d5619b4SYOSHIFUJI Hideaki 						  RTM_GETANYCAST,
44745d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4475053c095aSJohannes Berg 			if (err < 0)
4476bcd32326SEric Dumazet 				break;
44771da177e4SLinus Torvalds 		}
44781da177e4SLinus Torvalds 		break;
44791da177e4SLinus Torvalds 	default:
44801da177e4SLinus Torvalds 		break;
44811da177e4SLinus Torvalds 	}
44821da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
4483234b27c3SEric Dumazet 	*p_ip_idx = ip_idx;
4484234b27c3SEric Dumazet 	return err;
4485234b27c3SEric Dumazet }
44865d5619b4SYOSHIFUJI Hideaki 
4487234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4488234b27c3SEric Dumazet 			   enum addr_type_t type)
4489234b27c3SEric Dumazet {
4490234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
4491234b27c3SEric Dumazet 	int h, s_h;
4492234b27c3SEric Dumazet 	int idx, ip_idx;
4493234b27c3SEric Dumazet 	int s_idx, s_ip_idx;
4494234b27c3SEric Dumazet 	struct net_device *dev;
4495234b27c3SEric Dumazet 	struct inet6_dev *idev;
4496234b27c3SEric Dumazet 	struct hlist_head *head;
4497234b27c3SEric Dumazet 
4498234b27c3SEric Dumazet 	s_h = cb->args[0];
4499234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
4500234b27c3SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
4501234b27c3SEric Dumazet 
4502234b27c3SEric Dumazet 	rcu_read_lock();
450363998ac2SNicolas Dichtel 	cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4504234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4505234b27c3SEric Dumazet 		idx = 0;
4506234b27c3SEric Dumazet 		head = &net->dev_index_head[h];
4507b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
4508234b27c3SEric Dumazet 			if (idx < s_idx)
4509234b27c3SEric Dumazet 				goto cont;
45104b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
4511234b27c3SEric Dumazet 				s_ip_idx = 0;
4512234b27c3SEric Dumazet 			ip_idx = 0;
4513e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
4514e21e8467SStephen Hemminger 			if (!idev)
4515234b27c3SEric Dumazet 				goto cont;
4516234b27c3SEric Dumazet 
4517234b27c3SEric Dumazet 			if (in6_dump_addrs(idev, skb, cb, type,
45187b46a644SDavid S. Miller 					   s_ip_idx, &ip_idx) < 0)
4519234b27c3SEric Dumazet 				goto done;
45207562f876SPavel Emelianov cont:
45217562f876SPavel Emelianov 			idx++;
45221da177e4SLinus Torvalds 		}
4523234b27c3SEric Dumazet 	}
4524234b27c3SEric Dumazet done:
4525234b27c3SEric Dumazet 	rcu_read_unlock();
4526234b27c3SEric Dumazet 	cb->args[0] = h;
4527234b27c3SEric Dumazet 	cb->args[1] = idx;
4528234b27c3SEric Dumazet 	cb->args[2] = ip_idx;
4529234b27c3SEric Dumazet 
45301da177e4SLinus Torvalds 	return skb->len;
45311da177e4SLinus Torvalds }
45321da177e4SLinus Torvalds 
45331da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
45341da177e4SLinus Torvalds {
45351da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
4536b854272bSDenis V. Lunev 
45371da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
45381da177e4SLinus Torvalds }
45391da177e4SLinus Torvalds 
45401da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
45411da177e4SLinus Torvalds {
45421da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
4543b854272bSDenis V. Lunev 
45441da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
45451da177e4SLinus Torvalds }
45461da177e4SLinus Torvalds 
45471da177e4SLinus Torvalds 
45481da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
45491da177e4SLinus Torvalds {
45501da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
4551b854272bSDenis V. Lunev 
45521da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
45531da177e4SLinus Torvalds }
45541da177e4SLinus Torvalds 
4555661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
45566c223828SNoriaki TAKAMIYA {
45573b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
45581b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
45591b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4560caeaba79SNicolas Dichtel 	struct in6_addr *addr = NULL, *peer;
45616c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
45626c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
45636c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
45646c223828SNoriaki TAKAMIYA 	int err;
45656c223828SNoriaki TAKAMIYA 
45661b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
45671b29fc2cSThomas Graf 	if (err < 0)
45681b29fc2cSThomas Graf 		goto errout;
45696c223828SNoriaki TAKAMIYA 
4570caeaba79SNicolas Dichtel 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
457163159f29SIan Morris 	if (!addr) {
45721b29fc2cSThomas Graf 		err = -EINVAL;
45731b29fc2cSThomas Graf 		goto errout;
45741b29fc2cSThomas Graf 	}
45751b29fc2cSThomas Graf 
45761b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
45776c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
45786fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
45796c223828SNoriaki TAKAMIYA 
4580e21e8467SStephen Hemminger 	ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4581e21e8467SStephen Hemminger 	if (!ifa) {
45821b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
45831b29fc2cSThomas Graf 		goto errout;
45846c223828SNoriaki TAKAMIYA 	}
45856c223828SNoriaki TAKAMIYA 
4586e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4587e21e8467SStephen Hemminger 	if (!skb) {
45881b29fc2cSThomas Graf 		err = -ENOBUFS;
45891b29fc2cSThomas Graf 		goto errout_ifa;
45901b29fc2cSThomas Graf 	}
45911b29fc2cSThomas Graf 
459215e47304SEric W. Biederman 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
45936c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
459426932566SPatrick McHardy 	if (err < 0) {
459526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
459626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
459726932566SPatrick McHardy 		kfree_skb(skb);
459826932566SPatrick McHardy 		goto errout_ifa;
459926932566SPatrick McHardy 	}
460015e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
46011b29fc2cSThomas Graf errout_ifa:
46026c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
46031b29fc2cSThomas Graf errout:
46046c223828SNoriaki TAKAMIYA 	return err;
46056c223828SNoriaki TAKAMIYA }
46066c223828SNoriaki TAKAMIYA 
46071da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
46081da177e4SLinus Torvalds {
46091da177e4SLinus Torvalds 	struct sk_buff *skb;
4610c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
46115d620266SThomas Graf 	int err = -ENOBUFS;
46121da177e4SLinus Torvalds 
46130ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
461463159f29SIan Morris 	if (!skb)
46155d620266SThomas Graf 		goto errout;
46165d620266SThomas Graf 
46175d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
461826932566SPatrick McHardy 	if (err < 0) {
461926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
462026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
462126932566SPatrick McHardy 		kfree_skb(skb);
462226932566SPatrick McHardy 		goto errout;
462326932566SPatrick McHardy 	}
46241ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
46251ce85fe4SPablo Neira Ayuso 	return;
46265d620266SThomas Graf errout:
46275d620266SThomas Graf 	if (err < 0)
46286fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
46291da177e4SLinus Torvalds }
46301da177e4SLinus Torvalds 
4631b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
46321da177e4SLinus Torvalds 				__s32 *array, int bytes)
46331da177e4SLinus Torvalds {
463404561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
463504561c1fSThomas Graf 
46361da177e4SLinus Torvalds 	memset(array, 0, bytes);
46371da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
46381da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
46391da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
46401da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
46411da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
46421da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
46431da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
46441da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
464593908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
464693908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
464793908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
464893908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
46491da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4650fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4651fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4652fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4653fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
46541da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
46551da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
46561da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
46571da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
46581da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
46591da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
466065f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
46618013d1d7SHangbin Liu 	array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
4662c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4663930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4664930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
466593908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
466693908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
4667fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
466809c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
466909c884d4SYOSHIFUJI Hideaki #endif
4670930d6ff2SYOSHIFUJI Hideaki #endif
4671fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
46720bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
467395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
467495c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
46757fd2561eSErik Kline 	array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
467695c385b4SNeil Horman #endif
46777bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
46787bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
46797bc570c8SYOSHIFUJI Hideaki #endif
4680778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
46811b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4682c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
46835cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4684b800c3b9SHannes Frederic Sowa 	array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4685d9333196SBen Greear 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4686c2943f14SHarout Hedeshian 	array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
468735103d11SAndy Gospodarek 	array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
46883d1bec99SHannes Frederic Sowa 	/* we omit DEVCONF_STABLE_SECRET for now */
46893985e8a3SErik Kline 	array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
46901da177e4SLinus Torvalds }
46911da177e4SLinus Torvalds 
4692b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
4693b382b191SThomas Graf {
4694b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
4695b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
4696b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4697b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4698f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4699f53adae4SDaniel Borkmann 	     + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4700b382b191SThomas Graf }
4701b382b191SThomas Graf 
4702339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
4703339bf98fSThomas Graf {
4704339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4705339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4706339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4707339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
4708339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
4709*0344338bSAndy Gospodarek 	       + nla_total_size(1) /* IFLA_OPERSTATE */
4710b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4711339bf98fSThomas Graf }
4712c5396a31SYOSHIFUJI Hideaki 
4713be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
47147d720c3eSTejun Heo 				      int items, int bytes)
47157f7d9a6bSHerbert Xu {
47167f7d9a6bSHerbert Xu 	int i;
47177f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
47187f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
47197f7d9a6bSHerbert Xu 
47207f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
47217f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
47227f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
4723be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
47247f7d9a6bSHerbert Xu 
47257f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
47267f7d9a6bSHerbert Xu }
47277f7d9a6bSHerbert Xu 
4728698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
47294ce3c183SEric Dumazet 				      int items, int bytes, size_t syncpoff)
47304ce3c183SEric Dumazet {
47314ce3c183SEric Dumazet 	int i;
47324ce3c183SEric Dumazet 	int pad = bytes - sizeof(u64) * items;
47334ce3c183SEric Dumazet 	BUG_ON(pad < 0);
47344ce3c183SEric Dumazet 
47354ce3c183SEric Dumazet 	/* Use put_unaligned() because stats may not be aligned for u64. */
47364ce3c183SEric Dumazet 	put_unaligned(items, &stats[0]);
47374ce3c183SEric Dumazet 	for (i = 1; i < items; i++)
47384ce3c183SEric Dumazet 		put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
47394ce3c183SEric Dumazet 
47404ce3c183SEric Dumazet 	memset(&stats[items], 0, pad);
47414ce3c183SEric Dumazet }
47424ce3c183SEric Dumazet 
47437f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
47447f7d9a6bSHerbert Xu 			     int bytes)
47457f7d9a6bSHerbert Xu {
47467f7d9a6bSHerbert Xu 	switch (attrtype) {
47477f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
4748698365faSWANG Cong 		__snmp6_fill_stats64(stats, idev->stats.ipv6,
47494ce3c183SEric Dumazet 				     IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
47507f7d9a6bSHerbert Xu 		break;
47517f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
4752be281e55SEric Dumazet 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
47537f7d9a6bSHerbert Xu 		break;
47547f7d9a6bSHerbert Xu 	}
47557f7d9a6bSHerbert Xu }
47567f7d9a6bSHerbert Xu 
4757b382b191SThomas Graf static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
47581da177e4SLinus Torvalds {
4759bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
47601da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
47611da177e4SLinus Torvalds 
4762c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4763c78679e8SDavid S. Miller 		goto nla_put_failure;
47641da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
476524912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
476624912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
47671f9248e5SJiri Pirko 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
4768c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4769c78679e8SDavid S. Miller 		goto nla_put_failure;
4770bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
477163159f29SIan Morris 	if (!nla)
477204561c1fSThomas Graf 		goto nla_put_failure;
4773bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
47741da177e4SLinus Torvalds 
4775bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
4776bf99f1bdSYOSHIFUJI Hideaki 
4777bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
477863159f29SIan Morris 	if (!nla)
4779bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4780bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4781bf99f1bdSYOSHIFUJI Hideaki 
4782bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
478363159f29SIan Morris 	if (!nla)
4784bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4785bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
47861da177e4SLinus Torvalds 
4787f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
478863159f29SIan Morris 	if (!nla)
4789f53adae4SDaniel Borkmann 		goto nla_put_failure;
4790bc91b0f0SJiri Pirko 
4791bc91b0f0SJiri Pirko 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
4792bc91b0f0SJiri Pirko 		goto nla_put_failure;
4793bc91b0f0SJiri Pirko 
4794f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
4795f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4796f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
4797f53adae4SDaniel Borkmann 
4798b382b191SThomas Graf 	return 0;
4799b382b191SThomas Graf 
4800b382b191SThomas Graf nla_put_failure:
4801b382b191SThomas Graf 	return -EMSGSIZE;
4802b382b191SThomas Graf }
4803b382b191SThomas Graf 
4804b382b191SThomas Graf static size_t inet6_get_link_af_size(const struct net_device *dev)
4805b382b191SThomas Graf {
4806b382b191SThomas Graf 	if (!__in6_dev_get(dev))
4807b382b191SThomas Graf 		return 0;
4808b382b191SThomas Graf 
4809b382b191SThomas Graf 	return inet6_ifla6_size();
4810b382b191SThomas Graf }
4811b382b191SThomas Graf 
4812b382b191SThomas Graf static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4813b382b191SThomas Graf {
4814b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
4815b382b191SThomas Graf 
4816b382b191SThomas Graf 	if (!idev)
4817b382b191SThomas Graf 		return -ENODATA;
4818b382b191SThomas Graf 
4819b382b191SThomas Graf 	if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4820b382b191SThomas Graf 		return -EMSGSIZE;
4821b382b191SThomas Graf 
4822b382b191SThomas Graf 	return 0;
4823b382b191SThomas Graf }
4824b382b191SThomas Graf 
4825f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4826f53adae4SDaniel Borkmann {
4827f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
4828f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
4829fc403832SDaniel Borkmann 	bool update_rs = false;
4830dc848292SHannes Frederic Sowa 	struct in6_addr ll_addr;
4831f53adae4SDaniel Borkmann 
4832c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4833c15b1ccaSHannes Frederic Sowa 
483463159f29SIan Morris 	if (!token)
4835f53adae4SDaniel Borkmann 		return -EINVAL;
4836f53adae4SDaniel Borkmann 	if (ipv6_addr_any(token))
4837f53adae4SDaniel Borkmann 		return -EINVAL;
4838f53adae4SDaniel Borkmann 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4839f53adae4SDaniel Borkmann 		return -EINVAL;
4840f53adae4SDaniel Borkmann 	if (!ipv6_accept_ra(idev))
4841f53adae4SDaniel Borkmann 		return -EINVAL;
4842f53adae4SDaniel Borkmann 	if (idev->cnf.rtr_solicits <= 0)
4843f53adae4SDaniel Borkmann 		return -EINVAL;
4844f53adae4SDaniel Borkmann 
4845f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
4846f53adae4SDaniel Borkmann 
4847f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4848f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4849f53adae4SDaniel Borkmann 
4850f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
4851f53adae4SDaniel Borkmann 
4852dc848292SHannes Frederic Sowa 	if (!idev->dead && (idev->if_flags & IF_READY) &&
4853dc848292SHannes Frederic Sowa 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4854dc848292SHannes Frederic Sowa 			     IFA_F_OPTIMISTIC)) {
4855fc403832SDaniel Borkmann 
4856fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
4857fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
4858fc403832SDaniel Borkmann 		 */
4859f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4860fc403832SDaniel Borkmann 		update_rs = true;
4861fc403832SDaniel Borkmann 	}
4862f53adae4SDaniel Borkmann 
4863f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
4864fc403832SDaniel Borkmann 
486577ecaaceSHannes Frederic Sowa 	if (update_rs) {
4866f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
486777ecaaceSHannes Frederic Sowa 		idev->rs_probes = 1;
486877ecaaceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
486977ecaaceSHannes Frederic Sowa 	}
4870f53adae4SDaniel Borkmann 
4871f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
4872f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
4873f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
4874617fe29dSDaniel Borkmann 		if (ifp->tokenized) {
4875f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
4876f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
4877f53adae4SDaniel Borkmann 		}
4878f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
4879f53adae4SDaniel Borkmann 	}
4880f53adae4SDaniel Borkmann 
4881f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
4882b2ed64a9SLubomir Rintel 	inet6_ifinfo_notify(RTM_NEWLINK, idev);
4883c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4884f53adae4SDaniel Borkmann 	return 0;
4885f53adae4SDaniel Borkmann }
4886f53adae4SDaniel Borkmann 
488711b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
488811b1f828SDaniel Borkmann 	[IFLA_INET6_ADDR_GEN_MODE]	= { .type = NLA_U8 },
488911b1f828SDaniel Borkmann 	[IFLA_INET6_TOKEN]		= { .len = sizeof(struct in6_addr) },
489011b1f828SDaniel Borkmann };
489111b1f828SDaniel Borkmann 
489211b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev,
489311b1f828SDaniel Borkmann 				  const struct nlattr *nla)
489411b1f828SDaniel Borkmann {
489511b1f828SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
489611b1f828SDaniel Borkmann 
489711b1f828SDaniel Borkmann 	if (dev && !__in6_dev_get(dev))
489811b1f828SDaniel Borkmann 		return -EAFNOSUPPORT;
489911b1f828SDaniel Borkmann 
490011b1f828SDaniel Borkmann 	return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
490111b1f828SDaniel Borkmann }
490211b1f828SDaniel Borkmann 
4903f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4904f53adae4SDaniel Borkmann {
4905f53adae4SDaniel Borkmann 	int err = -EINVAL;
4906f53adae4SDaniel Borkmann 	struct inet6_dev *idev = __in6_dev_get(dev);
4907f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
4908f53adae4SDaniel Borkmann 
4909f53adae4SDaniel Borkmann 	if (!idev)
4910f53adae4SDaniel Borkmann 		return -EAFNOSUPPORT;
4911f53adae4SDaniel Borkmann 
4912f53adae4SDaniel Borkmann 	if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4913f53adae4SDaniel Borkmann 		BUG();
4914f53adae4SDaniel Borkmann 
4915bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_TOKEN]) {
4916f53adae4SDaniel Borkmann 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4917bc91b0f0SJiri Pirko 		if (err)
4918bc91b0f0SJiri Pirko 			return err;
4919bc91b0f0SJiri Pirko 	}
4920bc91b0f0SJiri Pirko 
4921bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
4922bc91b0f0SJiri Pirko 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
4923bc91b0f0SJiri Pirko 
4924bc91b0f0SJiri Pirko 		if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
4925622c81d5SHannes Frederic Sowa 		    mode != IN6_ADDR_GEN_MODE_NONE &&
4926622c81d5SHannes Frederic Sowa 		    mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY)
4927bc91b0f0SJiri Pirko 			return -EINVAL;
4928622c81d5SHannes Frederic Sowa 
4929622c81d5SHannes Frederic Sowa 		if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
4930622c81d5SHannes Frederic Sowa 		    !idev->cnf.stable_secret.initialized &&
4931622c81d5SHannes Frederic Sowa 		    !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
4932622c81d5SHannes Frederic Sowa 			return -EINVAL;
4933622c81d5SHannes Frederic Sowa 
4934bc91b0f0SJiri Pirko 		idev->addr_gen_mode = mode;
4935bc91b0f0SJiri Pirko 		err = 0;
4936bc91b0f0SJiri Pirko 	}
4937f53adae4SDaniel Borkmann 
4938f53adae4SDaniel Borkmann 	return err;
4939f53adae4SDaniel Borkmann }
4940f53adae4SDaniel Borkmann 
4941b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
494215e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
4943b382b191SThomas Graf {
4944b382b191SThomas Graf 	struct net_device *dev = idev->dev;
4945b382b191SThomas Graf 	struct ifinfomsg *hdr;
4946b382b191SThomas Graf 	struct nlmsghdr *nlh;
4947b382b191SThomas Graf 	void *protoinfo;
4948b382b191SThomas Graf 
494915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
495063159f29SIan Morris 	if (!nlh)
4951b382b191SThomas Graf 		return -EMSGSIZE;
4952b382b191SThomas Graf 
4953b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
4954b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
4955b382b191SThomas Graf 	hdr->__ifi_pad = 0;
4956b382b191SThomas Graf 	hdr->ifi_type = dev->type;
4957b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
4958b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
4959b382b191SThomas Graf 	hdr->ifi_change = 0;
4960b382b191SThomas Graf 
4961c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4962c78679e8SDavid S. Miller 	    (dev->addr_len &&
4963c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4964c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4965a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
4966*0344338bSAndy Gospodarek 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
4967*0344338bSAndy Gospodarek 	    nla_put_u8(skb, IFLA_OPERSTATE,
4968*0344338bSAndy Gospodarek 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
4969c78679e8SDavid S. Miller 		goto nla_put_failure;
4970b382b191SThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
497163159f29SIan Morris 	if (!protoinfo)
4972b382b191SThomas Graf 		goto nla_put_failure;
4973b382b191SThomas Graf 
4974b382b191SThomas Graf 	if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4975b382b191SThomas Graf 		goto nla_put_failure;
4976b382b191SThomas Graf 
497704561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
4978053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4979053c095aSJohannes Berg 	return 0;
49801da177e4SLinus Torvalds 
498104561c1fSThomas Graf nla_put_failure:
498226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
498326932566SPatrick McHardy 	return -EMSGSIZE;
49841da177e4SLinus Torvalds }
49851da177e4SLinus Torvalds 
49861da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
49871da177e4SLinus Torvalds {
49883b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
498984d2697dSEric Dumazet 	int h, s_h;
4990434a8a58SDavid S. Miller 	int idx = 0, s_idx;
49911da177e4SLinus Torvalds 	struct net_device *dev;
49921da177e4SLinus Torvalds 	struct inet6_dev *idev;
499384d2697dSEric Dumazet 	struct hlist_head *head;
49941da177e4SLinus Torvalds 
499584d2697dSEric Dumazet 	s_h = cb->args[0];
499684d2697dSEric Dumazet 	s_idx = cb->args[1];
499784d2697dSEric Dumazet 
499884d2697dSEric Dumazet 	rcu_read_lock();
499984d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
50007562f876SPavel Emelianov 		idx = 0;
500184d2697dSEric Dumazet 		head = &net->dev_index_head[h];
5002b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
50031da177e4SLinus Torvalds 			if (idx < s_idx)
50047562f876SPavel Emelianov 				goto cont;
500584d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
500684d2697dSEric Dumazet 			if (!idev)
50077562f876SPavel Emelianov 				goto cont;
500884d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
500915e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
501084d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
5011053c095aSJohannes Berg 					      RTM_NEWLINK, NLM_F_MULTI) < 0)
501284d2697dSEric Dumazet 				goto out;
50137562f876SPavel Emelianov cont:
50147562f876SPavel Emelianov 			idx++;
50151da177e4SLinus Torvalds 		}
501684d2697dSEric Dumazet 	}
501784d2697dSEric Dumazet out:
501884d2697dSEric Dumazet 	rcu_read_unlock();
501984d2697dSEric Dumazet 	cb->args[1] = idx;
502084d2697dSEric Dumazet 	cb->args[0] = h;
50211da177e4SLinus Torvalds 
50221da177e4SLinus Torvalds 	return skb->len;
50231da177e4SLinus Torvalds }
50241da177e4SLinus Torvalds 
50251da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
50261da177e4SLinus Torvalds {
50271da177e4SLinus Torvalds 	struct sk_buff *skb;
5028c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
50298d7a76c9SThomas Graf 	int err = -ENOBUFS;
50301da177e4SLinus Torvalds 
5031339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
503263159f29SIan Morris 	if (!skb)
50338d7a76c9SThomas Graf 		goto errout;
50348d7a76c9SThomas Graf 
50358d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
503626932566SPatrick McHardy 	if (err < 0) {
503726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
503826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
503926932566SPatrick McHardy 		kfree_skb(skb);
504026932566SPatrick McHardy 		goto errout;
504126932566SPatrick McHardy 	}
50425f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
50431ce85fe4SPablo Neira Ayuso 	return;
50448d7a76c9SThomas Graf errout:
50458d7a76c9SThomas Graf 	if (err < 0)
50465f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
50471da177e4SLinus Torvalds }
50481da177e4SLinus Torvalds 
5049339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
5050339bf98fSThomas Graf {
5051339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
5052339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
5053339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
5054339bf98fSThomas Graf }
5055c5396a31SYOSHIFUJI Hideaki 
50561da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
505715e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
5058b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
50591da177e4SLinus Torvalds {
50601da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
50611da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
50621da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
50631da177e4SLinus Torvalds 
506415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
506563159f29SIan Morris 	if (!nlh)
506626932566SPatrick McHardy 		return -EMSGSIZE;
50676051e2f4SThomas Graf 
50686051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
50691da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
50708a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
50718a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
50721da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
50731da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
50741da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
50758a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
50761da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
50771da177e4SLinus Torvalds 	if (pinfo->onlink)
50781da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
50791da177e4SLinus Torvalds 	if (pinfo->autoconf)
50801da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
50811da177e4SLinus Torvalds 
5082c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5083c78679e8SDavid S. Miller 		goto nla_put_failure;
50841da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
50851da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
5086c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5087c78679e8SDavid S. Miller 		goto nla_put_failure;
5088053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5089053c095aSJohannes Berg 	return 0;
50901da177e4SLinus Torvalds 
50916051e2f4SThomas Graf nla_put_failure:
509226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
509326932566SPatrick McHardy 	return -EMSGSIZE;
50941da177e4SLinus Torvalds }
50951da177e4SLinus Torvalds 
50961da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
50971da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
50981da177e4SLinus Torvalds {
50991da177e4SLinus Torvalds 	struct sk_buff *skb;
5100c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
51018c384bfaSThomas Graf 	int err = -ENOBUFS;
51021da177e4SLinus Torvalds 
5103339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
510463159f29SIan Morris 	if (!skb)
51058c384bfaSThomas Graf 		goto errout;
51068c384bfaSThomas Graf 
51078c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
510826932566SPatrick McHardy 	if (err < 0) {
510926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
511026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
511126932566SPatrick McHardy 		kfree_skb(skb);
511226932566SPatrick McHardy 		goto errout;
511326932566SPatrick McHardy 	}
51141ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
51151ce85fe4SPablo Neira Ayuso 	return;
51168c384bfaSThomas Graf errout:
51178c384bfaSThomas Graf 	if (err < 0)
51186fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
51191da177e4SLinus Torvalds }
51201da177e4SLinus Torvalds 
51211da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
51221da177e4SLinus Torvalds {
512363998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
512463998ac2SNicolas Dichtel 
5125c15b1ccaSHannes Frederic Sowa 	if (event)
5126c15b1ccaSHannes Frederic Sowa 		ASSERT_RTNL();
5127c15b1ccaSHannes Frederic Sowa 
51281da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
51291da177e4SLinus Torvalds 
51301da177e4SLinus Torvalds 	switch (event) {
51311da177e4SLinus Torvalds 	case RTM_NEWADDR:
513295c385b4SNeil Horman 		/*
513395c385b4SNeil Horman 		 * If the address was optimistic
513495c385b4SNeil Horman 		 * we inserted the route at the start of
513595c385b4SNeil Horman 		 * our DAD process, so we don't need
513695c385b4SNeil Horman 		 * to do it again
513795c385b4SNeil Horman 		 */
513895c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
513940e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
51401da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
51411da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
51427996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr))
5143caeaba79SNicolas Dichtel 			addrconf_prefix_route(&ifp->peer_addr, 128,
5144caeaba79SNicolas Dichtel 					      ifp->idev->dev, 0, 0);
51451da177e4SLinus Torvalds 		break;
51461da177e4SLinus Torvalds 	case RTM_DELADDR:
51471da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
51481da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
51491da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
51507996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr)) {
5151caeaba79SNicolas Dichtel 			struct rt6_info *rt;
5152caeaba79SNicolas Dichtel 
5153e7478dfcSNicolas Dichtel 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5154e7478dfcSNicolas Dichtel 						       ifp->idev->dev, 0, 0);
5155f24062b0SNicolas Dichtel 			if (rt && ip6_del_rt(rt))
5156caeaba79SNicolas Dichtel 				dst_free(&rt->dst);
5157caeaba79SNicolas Dichtel 		}
5158d8d1f30bSChangli Gao 		dst_hold(&ifp->rt->dst);
515993fa159aSstephen hemminger 
516073a8bd74SDavid S. Miller 		if (ip6_del_rt(ifp->rt))
5161d8d1f30bSChangli Gao 			dst_free(&ifp->rt->dst);
5162705f1c86SHannes Frederic Sowa 
5163705f1c86SHannes Frederic Sowa 		rt_genid_bump_ipv6(net);
51641da177e4SLinus Torvalds 		break;
51651da177e4SLinus Torvalds 	}
516663998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
51671da177e4SLinus Torvalds }
51681da177e4SLinus Torvalds 
51691da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
51701da177e4SLinus Torvalds {
51718814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
51721da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
51731da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
51748814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
51751da177e4SLinus Torvalds }
51761da177e4SLinus Torvalds 
51771da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
51781da177e4SLinus Torvalds 
51791da177e4SLinus Torvalds static
5180fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
51811da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
51821da177e4SLinus Torvalds {
51831da177e4SLinus Torvalds 	int *valp = ctl->data;
51841da177e4SLinus Torvalds 	int val = *valp;
518588af182eSEric W. Biederman 	loff_t pos = *ppos;
5186fe2c6338SJoe Perches 	struct ctl_table lctl;
51871da177e4SLinus Torvalds 	int ret;
51881da177e4SLinus Torvalds 
5189013d97e9SFrancesco Ruggeri 	/*
5190013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
5191013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5192013d97e9SFrancesco Ruggeri 	 */
5193013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5194013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5195013d97e9SFrancesco Ruggeri 
5196013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
51971da177e4SLinus Torvalds 
5198c8fecf22SPavel Emelyanov 	if (write)
5199b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
520088af182eSEric W. Biederman 	if (ret)
520188af182eSEric W. Biederman 		*ppos = pos;
52021da177e4SLinus Torvalds 	return ret;
52031da177e4SLinus Torvalds }
52041da177e4SLinus Torvalds 
520577751427SMarcelo Leitner static
520677751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
520777751427SMarcelo Leitner 			void __user *buffer, size_t *lenp, loff_t *ppos)
520877751427SMarcelo Leitner {
520977751427SMarcelo Leitner 	struct inet6_dev *idev = ctl->extra1;
521077751427SMarcelo Leitner 	int min_mtu = IPV6_MIN_MTU;
521177751427SMarcelo Leitner 	struct ctl_table lctl;
521277751427SMarcelo Leitner 
521377751427SMarcelo Leitner 	lctl = *ctl;
521477751427SMarcelo Leitner 	lctl.extra1 = &min_mtu;
521577751427SMarcelo Leitner 	lctl.extra2 = idev ? &idev->dev->mtu : NULL;
521677751427SMarcelo Leitner 
521777751427SMarcelo Leitner 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
521877751427SMarcelo Leitner }
521977751427SMarcelo Leitner 
522056d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
522156d417b1SBrian Haley {
522275538c2bSCong Wang 	struct netdev_notifier_info info;
522375538c2bSCong Wang 
522456d417b1SBrian Haley 	if (!idev || !idev->dev)
522556d417b1SBrian Haley 		return;
522656d417b1SBrian Haley 
522775538c2bSCong Wang 	netdev_notifier_info_init(&info, idev->dev);
522856d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
522975538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_DOWN, &info);
523056d417b1SBrian Haley 	else
523175538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_UP, &info);
523256d417b1SBrian Haley }
523356d417b1SBrian Haley 
523456d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
523556d417b1SBrian Haley {
523656d417b1SBrian Haley 	struct net_device *dev;
523756d417b1SBrian Haley 	struct inet6_dev *idev;
523856d417b1SBrian Haley 
523956d417b1SBrian Haley 	rcu_read_lock();
5240c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
524156d417b1SBrian Haley 		idev = __in6_dev_get(dev);
524256d417b1SBrian Haley 		if (idev) {
524356d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
524456d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
524556d417b1SBrian Haley 			if (changed)
524656d417b1SBrian Haley 				dev_disable_change(idev);
524756d417b1SBrian Haley 		}
524856d417b1SBrian Haley 	}
5249c6d14c84SEric Dumazet 	rcu_read_unlock();
525056d417b1SBrian Haley }
525156d417b1SBrian Haley 
5252013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
525356d417b1SBrian Haley {
525456d417b1SBrian Haley 	struct net *net;
5255013d97e9SFrancesco Ruggeri 	int old;
5256013d97e9SFrancesco Ruggeri 
5257013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
5258013d97e9SFrancesco Ruggeri 		return restart_syscall();
525956d417b1SBrian Haley 
526056d417b1SBrian Haley 	net = (struct net *)table->extra2;
5261013d97e9SFrancesco Ruggeri 	old = *p;
5262013d97e9SFrancesco Ruggeri 	*p = newf;
526356d417b1SBrian Haley 
5264013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5265013d97e9SFrancesco Ruggeri 		rtnl_unlock();
526656d417b1SBrian Haley 		return 0;
526788af182eSEric W. Biederman 	}
526856d417b1SBrian Haley 
526956d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
527056d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
527156d417b1SBrian Haley 		addrconf_disable_change(net, newf);
5272013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
527356d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
527456d417b1SBrian Haley 
527556d417b1SBrian Haley 	rtnl_unlock();
527656d417b1SBrian Haley 	return 0;
527756d417b1SBrian Haley }
527856d417b1SBrian Haley 
527956d417b1SBrian Haley static
5280fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
528156d417b1SBrian Haley 			    void __user *buffer, size_t *lenp, loff_t *ppos)
528256d417b1SBrian Haley {
528356d417b1SBrian Haley 	int *valp = ctl->data;
528456d417b1SBrian Haley 	int val = *valp;
528588af182eSEric W. Biederman 	loff_t pos = *ppos;
5286fe2c6338SJoe Perches 	struct ctl_table lctl;
528756d417b1SBrian Haley 	int ret;
528856d417b1SBrian Haley 
5289013d97e9SFrancesco Ruggeri 	/*
5290013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
5291013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5292013d97e9SFrancesco Ruggeri 	 */
5293013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5294013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5295013d97e9SFrancesco Ruggeri 
5296013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
529756d417b1SBrian Haley 
529856d417b1SBrian Haley 	if (write)
529956d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
530088af182eSEric W. Biederman 	if (ret)
530188af182eSEric W. Biederman 		*ppos = pos;
530256d417b1SBrian Haley 	return ret;
530356d417b1SBrian Haley }
530456d417b1SBrian Haley 
5305c92d5491Sstephen hemminger static
5306c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5307c92d5491Sstephen hemminger 			      void __user *buffer, size_t *lenp, loff_t *ppos)
5308c92d5491Sstephen hemminger {
5309c92d5491Sstephen hemminger 	int *valp = ctl->data;
5310c92d5491Sstephen hemminger 	int ret;
5311c92d5491Sstephen hemminger 	int old, new;
5312c92d5491Sstephen hemminger 
5313c92d5491Sstephen hemminger 	old = *valp;
5314c92d5491Sstephen hemminger 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5315c92d5491Sstephen hemminger 	new = *valp;
5316c92d5491Sstephen hemminger 
5317c92d5491Sstephen hemminger 	if (write && old != new) {
5318c92d5491Sstephen hemminger 		struct net *net = ctl->extra2;
5319c92d5491Sstephen hemminger 
5320c92d5491Sstephen hemminger 		if (!rtnl_trylock())
5321c92d5491Sstephen hemminger 			return restart_syscall();
5322c92d5491Sstephen hemminger 
5323c92d5491Sstephen hemminger 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5324c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5325c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_DEFAULT,
5326c92d5491Sstephen hemminger 						     net->ipv6.devconf_dflt);
5327c92d5491Sstephen hemminger 		else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5328c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5329c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_ALL,
5330c92d5491Sstephen hemminger 						     net->ipv6.devconf_all);
5331c92d5491Sstephen hemminger 		else {
5332c92d5491Sstephen hemminger 			struct inet6_dev *idev = ctl->extra1;
5333c92d5491Sstephen hemminger 
5334c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5335c92d5491Sstephen hemminger 						     idev->dev->ifindex,
5336c92d5491Sstephen hemminger 						     &idev->cnf);
5337c92d5491Sstephen hemminger 		}
5338c92d5491Sstephen hemminger 		rtnl_unlock();
5339c92d5491Sstephen hemminger 	}
5340c92d5491Sstephen hemminger 
5341c92d5491Sstephen hemminger 	return ret;
5342c92d5491Sstephen hemminger }
5343c92d5491Sstephen hemminger 
53443d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
53453d1bec99SHannes Frederic Sowa 					 void __user *buffer, size_t *lenp,
53463d1bec99SHannes Frederic Sowa 					 loff_t *ppos)
53473d1bec99SHannes Frederic Sowa {
53483d1bec99SHannes Frederic Sowa 	int err;
53493d1bec99SHannes Frederic Sowa 	struct in6_addr addr;
53503d1bec99SHannes Frederic Sowa 	char str[IPV6_MAX_STRLEN];
53513d1bec99SHannes Frederic Sowa 	struct ctl_table lctl = *ctl;
5352622c81d5SHannes Frederic Sowa 	struct net *net = ctl->extra2;
53533d1bec99SHannes Frederic Sowa 	struct ipv6_stable_secret *secret = ctl->data;
53543d1bec99SHannes Frederic Sowa 
5355622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5356622c81d5SHannes Frederic Sowa 		return -EIO;
5357622c81d5SHannes Frederic Sowa 
53583d1bec99SHannes Frederic Sowa 	lctl.maxlen = IPV6_MAX_STRLEN;
53593d1bec99SHannes Frederic Sowa 	lctl.data = str;
53603d1bec99SHannes Frederic Sowa 
53613d1bec99SHannes Frederic Sowa 	if (!rtnl_trylock())
53623d1bec99SHannes Frederic Sowa 		return restart_syscall();
53633d1bec99SHannes Frederic Sowa 
53643d1bec99SHannes Frederic Sowa 	if (!write && !secret->initialized) {
53653d1bec99SHannes Frederic Sowa 		err = -EIO;
53663d1bec99SHannes Frederic Sowa 		goto out;
53673d1bec99SHannes Frederic Sowa 	}
53683d1bec99SHannes Frederic Sowa 
53693d1bec99SHannes Frederic Sowa 	if (!write) {
53703d1bec99SHannes Frederic Sowa 		err = snprintf(str, sizeof(str), "%pI6",
53713d1bec99SHannes Frederic Sowa 			       &secret->secret);
53723d1bec99SHannes Frederic Sowa 		if (err >= sizeof(str)) {
53733d1bec99SHannes Frederic Sowa 			err = -EIO;
53743d1bec99SHannes Frederic Sowa 			goto out;
53753d1bec99SHannes Frederic Sowa 		}
53763d1bec99SHannes Frederic Sowa 	}
53773d1bec99SHannes Frederic Sowa 
53783d1bec99SHannes Frederic Sowa 	err = proc_dostring(&lctl, write, buffer, lenp, ppos);
53793d1bec99SHannes Frederic Sowa 	if (err || !write)
53803d1bec99SHannes Frederic Sowa 		goto out;
53813d1bec99SHannes Frederic Sowa 
53823d1bec99SHannes Frederic Sowa 	if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
53833d1bec99SHannes Frederic Sowa 		err = -EIO;
53843d1bec99SHannes Frederic Sowa 		goto out;
53853d1bec99SHannes Frederic Sowa 	}
53863d1bec99SHannes Frederic Sowa 
53873d1bec99SHannes Frederic Sowa 	secret->initialized = true;
53883d1bec99SHannes Frederic Sowa 	secret->secret = addr;
53893d1bec99SHannes Frederic Sowa 
5390622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5391622c81d5SHannes Frederic Sowa 		struct net_device *dev;
5392622c81d5SHannes Frederic Sowa 
5393622c81d5SHannes Frederic Sowa 		for_each_netdev(net, dev) {
5394622c81d5SHannes Frederic Sowa 			struct inet6_dev *idev = __in6_dev_get(dev);
5395622c81d5SHannes Frederic Sowa 
5396622c81d5SHannes Frederic Sowa 			if (idev) {
5397622c81d5SHannes Frederic Sowa 				idev->addr_gen_mode =
5398622c81d5SHannes Frederic Sowa 					IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5399622c81d5SHannes Frederic Sowa 			}
5400622c81d5SHannes Frederic Sowa 		}
5401622c81d5SHannes Frederic Sowa 	} else {
5402622c81d5SHannes Frederic Sowa 		struct inet6_dev *idev = ctl->extra1;
5403622c81d5SHannes Frederic Sowa 
5404622c81d5SHannes Frederic Sowa 		idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5405622c81d5SHannes Frederic Sowa 	}
5406622c81d5SHannes Frederic Sowa 
54073d1bec99SHannes Frederic Sowa out:
54083d1bec99SHannes Frederic Sowa 	rtnl_unlock();
54093d1bec99SHannes Frederic Sowa 
54103d1bec99SHannes Frederic Sowa 	return err;
54113d1bec99SHannes Frederic Sowa }
5412c92d5491Sstephen hemminger 
541335103d11SAndy Gospodarek static
541435103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
541535103d11SAndy Gospodarek 						int write,
541635103d11SAndy Gospodarek 						void __user *buffer,
541735103d11SAndy Gospodarek 						size_t *lenp,
541835103d11SAndy Gospodarek 						loff_t *ppos)
541935103d11SAndy Gospodarek {
542035103d11SAndy Gospodarek 	int *valp = ctl->data;
542135103d11SAndy Gospodarek 	int val = *valp;
542235103d11SAndy Gospodarek 	loff_t pos = *ppos;
542335103d11SAndy Gospodarek 	struct ctl_table lctl;
542435103d11SAndy Gospodarek 	int ret;
542535103d11SAndy Gospodarek 
542635103d11SAndy Gospodarek 	/* ctl->data points to idev->cnf.ignore_routes_when_linkdown
542735103d11SAndy Gospodarek 	 * we should not modify it until we get the rtnl lock.
542835103d11SAndy Gospodarek 	 */
542935103d11SAndy Gospodarek 	lctl = *ctl;
543035103d11SAndy Gospodarek 	lctl.data = &val;
543135103d11SAndy Gospodarek 
543235103d11SAndy Gospodarek 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
543335103d11SAndy Gospodarek 
543435103d11SAndy Gospodarek 	if (write)
543535103d11SAndy Gospodarek 		ret = addrconf_fixup_linkdown(ctl, valp, val);
543635103d11SAndy Gospodarek 	if (ret)
543735103d11SAndy Gospodarek 		*ppos = pos;
543835103d11SAndy Gospodarek 	return ret;
543935103d11SAndy Gospodarek }
544035103d11SAndy Gospodarek 
54411da177e4SLinus Torvalds static struct addrconf_sysctl_table
54421da177e4SLinus Torvalds {
54431da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
5444fe2c6338SJoe Perches 	struct ctl_table addrconf_vars[DEVCONF_MAX+1];
5445ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
54461da177e4SLinus Torvalds 	.sysctl_header = NULL,
54471da177e4SLinus Torvalds 	.addrconf_vars = {
54481da177e4SLinus Torvalds 		{
54491da177e4SLinus Torvalds 			.procname	= "forwarding",
54501da177e4SLinus Torvalds 			.data		= &ipv6_devconf.forwarding,
54511da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54521da177e4SLinus Torvalds 			.mode		= 0644,
54536d9f239aSAlexey Dobriyan 			.proc_handler	= addrconf_sysctl_forward,
54541da177e4SLinus Torvalds 		},
54551da177e4SLinus Torvalds 		{
54561da177e4SLinus Torvalds 			.procname	= "hop_limit",
54571da177e4SLinus Torvalds 			.data		= &ipv6_devconf.hop_limit,
54581da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54591da177e4SLinus Torvalds 			.mode		= 0644,
54601da177e4SLinus Torvalds 			.proc_handler	= proc_dointvec,
54611da177e4SLinus Torvalds 		},
54621da177e4SLinus Torvalds 		{
54631da177e4SLinus Torvalds 			.procname	= "mtu",
54641da177e4SLinus Torvalds 			.data		= &ipv6_devconf.mtu6,
54651da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54661da177e4SLinus Torvalds 			.mode		= 0644,
546777751427SMarcelo Leitner 			.proc_handler	= addrconf_sysctl_mtu,
54681da177e4SLinus Torvalds 		},
54691da177e4SLinus Torvalds 		{
54701da177e4SLinus Torvalds 			.procname	= "accept_ra",
54711da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_ra,
54721da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54731da177e4SLinus Torvalds 			.mode		= 0644,
54746d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
54751da177e4SLinus Torvalds 		},
54761da177e4SLinus Torvalds 		{
54771da177e4SLinus Torvalds 			.procname	= "accept_redirects",
54781da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_redirects,
54791da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54801da177e4SLinus Torvalds 			.mode		= 0644,
54816d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
54821da177e4SLinus Torvalds 		},
54831da177e4SLinus Torvalds 		{
54841da177e4SLinus Torvalds 			.procname	= "autoconf",
54851da177e4SLinus Torvalds 			.data		= &ipv6_devconf.autoconf,
54861da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54871da177e4SLinus Torvalds 			.mode		= 0644,
54886d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
54891da177e4SLinus Torvalds 		},
54901da177e4SLinus Torvalds 		{
54911da177e4SLinus Torvalds 			.procname	= "dad_transmits",
54921da177e4SLinus Torvalds 			.data		= &ipv6_devconf.dad_transmits,
54931da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
54941da177e4SLinus Torvalds 			.mode		= 0644,
54956d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
54961da177e4SLinus Torvalds 		},
54971da177e4SLinus Torvalds 		{
54981da177e4SLinus Torvalds 			.procname	= "router_solicitations",
54991da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicits,
55001da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55011da177e4SLinus Torvalds 			.mode		= 0644,
55026d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55031da177e4SLinus Torvalds 		},
55041da177e4SLinus Torvalds 		{
55051da177e4SLinus Torvalds 			.procname	= "router_solicitation_interval",
55061da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_interval,
55071da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55081da177e4SLinus Torvalds 			.mode		= 0644,
55096d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
55101da177e4SLinus Torvalds 		},
55111da177e4SLinus Torvalds 		{
55121da177e4SLinus Torvalds 			.procname	= "router_solicitation_delay",
55131da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_delay,
55141da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55151da177e4SLinus Torvalds 			.mode		= 0644,
55166d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
55171da177e4SLinus Torvalds 		},
55181da177e4SLinus Torvalds 		{
55191da177e4SLinus Torvalds 			.procname	= "force_mld_version",
55201da177e4SLinus Torvalds 			.data		= &ipv6_devconf.force_mld_version,
55211da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55221da177e4SLinus Torvalds 			.mode		= 0644,
55236d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55241da177e4SLinus Torvalds 		},
5525fc4eba58SHannes Frederic Sowa 		{
5526fc4eba58SHannes Frederic Sowa 			.procname	= "mldv1_unsolicited_report_interval",
5527fc4eba58SHannes Frederic Sowa 			.data		=
5528fc4eba58SHannes Frederic Sowa 				&ipv6_devconf.mldv1_unsolicited_report_interval,
5529fc4eba58SHannes Frederic Sowa 			.maxlen		= sizeof(int),
5530fc4eba58SHannes Frederic Sowa 			.mode		= 0644,
5531fc4eba58SHannes Frederic Sowa 			.proc_handler	= proc_dointvec_ms_jiffies,
5532fc4eba58SHannes Frederic Sowa 		},
5533fc4eba58SHannes Frederic Sowa 		{
5534fc4eba58SHannes Frederic Sowa 			.procname	= "mldv2_unsolicited_report_interval",
5535fc4eba58SHannes Frederic Sowa 			.data		=
5536fc4eba58SHannes Frederic Sowa 				&ipv6_devconf.mldv2_unsolicited_report_interval,
5537fc4eba58SHannes Frederic Sowa 			.maxlen		= sizeof(int),
5538fc4eba58SHannes Frederic Sowa 			.mode		= 0644,
5539fc4eba58SHannes Frederic Sowa 			.proc_handler	= proc_dointvec_ms_jiffies,
5540fc4eba58SHannes Frederic Sowa 		},
55411da177e4SLinus Torvalds 		{
55421da177e4SLinus Torvalds 			.procname	= "use_tempaddr",
55431da177e4SLinus Torvalds 			.data		= &ipv6_devconf.use_tempaddr,
55441da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55451da177e4SLinus Torvalds 			.mode		= 0644,
55466d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55471da177e4SLinus Torvalds 		},
55481da177e4SLinus Torvalds 		{
55491da177e4SLinus Torvalds 			.procname	= "temp_valid_lft",
55501da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_valid_lft,
55511da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55521da177e4SLinus Torvalds 			.mode		= 0644,
55536d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55541da177e4SLinus Torvalds 		},
55551da177e4SLinus Torvalds 		{
55561da177e4SLinus Torvalds 			.procname	= "temp_prefered_lft",
55571da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_prefered_lft,
55581da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55591da177e4SLinus Torvalds 			.mode		= 0644,
55606d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55611da177e4SLinus Torvalds 		},
55621da177e4SLinus Torvalds 		{
55631da177e4SLinus Torvalds 			.procname	= "regen_max_retry",
55641da177e4SLinus Torvalds 			.data		= &ipv6_devconf.regen_max_retry,
55651da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55661da177e4SLinus Torvalds 			.mode		= 0644,
55676d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55681da177e4SLinus Torvalds 		},
55691da177e4SLinus Torvalds 		{
55701da177e4SLinus Torvalds 			.procname	= "max_desync_factor",
55711da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_desync_factor,
55721da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55731da177e4SLinus Torvalds 			.mode		= 0644,
55746d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55751da177e4SLinus Torvalds 		},
55761da177e4SLinus Torvalds 		{
55771da177e4SLinus Torvalds 			.procname	= "max_addresses",
55781da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_addresses,
55791da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55801da177e4SLinus Torvalds 			.mode		= 0644,
55816d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55821da177e4SLinus Torvalds 		},
55831da177e4SLinus Torvalds 		{
558465f5c7c1SYOSHIFUJI Hideaki 			.procname	= "accept_ra_defrtr",
558565f5c7c1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_defrtr,
558665f5c7c1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
558765f5c7c1SYOSHIFUJI Hideaki 			.mode		= 0644,
55886d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
558965f5c7c1SYOSHIFUJI Hideaki 		},
559065f5c7c1SYOSHIFUJI Hideaki 		{
55918013d1d7SHangbin Liu 			.procname	= "accept_ra_min_hop_limit",
55928013d1d7SHangbin Liu 			.data		= &ipv6_devconf.accept_ra_min_hop_limit,
55938013d1d7SHangbin Liu 			.maxlen		= sizeof(int),
55948013d1d7SHangbin Liu 			.mode		= 0644,
55958013d1d7SHangbin Liu 			.proc_handler	= proc_dointvec,
55968013d1d7SHangbin Liu 		},
55978013d1d7SHangbin Liu 		{
5598c4fd30ebSYOSHIFUJI Hideaki 			.procname	= "accept_ra_pinfo",
5599c4fd30ebSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_pinfo,
5600c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5601c4fd30ebSYOSHIFUJI Hideaki 			.mode		= 0644,
56026d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
5603c4fd30ebSYOSHIFUJI Hideaki 		},
5604930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
5605930d6ff2SYOSHIFUJI Hideaki 		{
5606930d6ff2SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rtr_pref",
5607930d6ff2SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rtr_pref,
5608930d6ff2SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5609930d6ff2SYOSHIFUJI Hideaki 			.mode		= 0644,
56106d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
5611930d6ff2SYOSHIFUJI Hideaki 		},
561252e16356SYOSHIFUJI Hideaki 		{
561352e16356SYOSHIFUJI Hideaki 			.procname	= "router_probe_interval",
561452e16356SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.rtr_probe_interval,
561552e16356SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
561652e16356SYOSHIFUJI Hideaki 			.mode		= 0644,
56176d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
561852e16356SYOSHIFUJI Hideaki 		},
5619fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
562009c884d4SYOSHIFUJI Hideaki 		{
562109c884d4SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rt_info_max_plen",
562209c884d4SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
562309c884d4SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
562409c884d4SYOSHIFUJI Hideaki 			.mode		= 0644,
56256d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
562609c884d4SYOSHIFUJI Hideaki 		},
562709c884d4SYOSHIFUJI Hideaki #endif
5628930d6ff2SYOSHIFUJI Hideaki #endif
5629c4fd30ebSYOSHIFUJI Hideaki 		{
5630fbea49e1SYOSHIFUJI Hideaki 			.procname	= "proxy_ndp",
5631fbea49e1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.proxy_ndp,
5632fbea49e1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5633fbea49e1SYOSHIFUJI Hideaki 			.mode		= 0644,
5634c92d5491Sstephen hemminger 			.proc_handler	= addrconf_sysctl_proxy_ndp,
5635fbea49e1SYOSHIFUJI Hideaki 		},
5636fbea49e1SYOSHIFUJI Hideaki 		{
56370bcbc926SYOSHIFUJI Hideaki 			.procname	= "accept_source_route",
56380bcbc926SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_source_route,
56390bcbc926SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
56400bcbc926SYOSHIFUJI Hideaki 			.mode		= 0644,
56416d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
56420bcbc926SYOSHIFUJI Hideaki 		},
564395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
564495c385b4SNeil Horman 		{
564595c385b4SNeil Horman 			.procname       = "optimistic_dad",
564695c385b4SNeil Horman 			.data           = &ipv6_devconf.optimistic_dad,
564795c385b4SNeil Horman 			.maxlen         = sizeof(int),
564895c385b4SNeil Horman 			.mode           = 0644,
56496d9f239aSAlexey Dobriyan 			.proc_handler   = proc_dointvec,
565095c385b4SNeil Horman 
565195c385b4SNeil Horman 		},
56527fd2561eSErik Kline 		{
56537fd2561eSErik Kline 			.procname       = "use_optimistic",
56547fd2561eSErik Kline 			.data           = &ipv6_devconf.use_optimistic,
56557fd2561eSErik Kline 			.maxlen         = sizeof(int),
56567fd2561eSErik Kline 			.mode           = 0644,
56577fd2561eSErik Kline 			.proc_handler   = proc_dointvec,
56587fd2561eSErik Kline 
56597fd2561eSErik Kline 		},
566095c385b4SNeil Horman #endif
56617bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
56627bc570c8SYOSHIFUJI Hideaki 		{
56637bc570c8SYOSHIFUJI Hideaki 			.procname	= "mc_forwarding",
56647bc570c8SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.mc_forwarding,
56657bc570c8SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5666a4e6db07SDavid S. Miller 			.mode		= 0444,
56676d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
56687bc570c8SYOSHIFUJI Hideaki 		},
56697bc570c8SYOSHIFUJI Hideaki #endif
56700bcbc926SYOSHIFUJI Hideaki 		{
5671778d80beSYOSHIFUJI Hideaki 			.procname	= "disable_ipv6",
5672778d80beSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.disable_ipv6,
5673778d80beSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5674778d80beSYOSHIFUJI Hideaki 			.mode		= 0644,
567556d417b1SBrian Haley 			.proc_handler	= addrconf_sysctl_disable,
5676778d80beSYOSHIFUJI Hideaki 		},
5677778d80beSYOSHIFUJI Hideaki 		{
56781b34be74SYOSHIFUJI Hideaki 			.procname	= "accept_dad",
56791b34be74SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_dad,
56801b34be74SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
56811b34be74SYOSHIFUJI Hideaki 			.mode		= 0644,
56826d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
56831b34be74SYOSHIFUJI Hideaki 		},
56841b34be74SYOSHIFUJI Hideaki 		{
5685f7734fdfSOctavian Purdila 			.procname       = "force_tllao",
5686f7734fdfSOctavian Purdila 			.data           = &ipv6_devconf.force_tllao,
5687f7734fdfSOctavian Purdila 			.maxlen         = sizeof(int),
5688f7734fdfSOctavian Purdila 			.mode           = 0644,
5689f7734fdfSOctavian Purdila 			.proc_handler   = proc_dointvec
5690f7734fdfSOctavian Purdila 		},
5691f7734fdfSOctavian Purdila 		{
56925cb04436SHannes Frederic Sowa 			.procname       = "ndisc_notify",
56935cb04436SHannes Frederic Sowa 			.data           = &ipv6_devconf.ndisc_notify,
56945cb04436SHannes Frederic Sowa 			.maxlen         = sizeof(int),
56955cb04436SHannes Frederic Sowa 			.mode           = 0644,
56965cb04436SHannes Frederic Sowa 			.proc_handler   = proc_dointvec
56975cb04436SHannes Frederic Sowa 		},
56985cb04436SHannes Frederic Sowa 		{
5699b800c3b9SHannes Frederic Sowa 			.procname	= "suppress_frag_ndisc",
5700b800c3b9SHannes Frederic Sowa 			.data		= &ipv6_devconf.suppress_frag_ndisc,
5701b800c3b9SHannes Frederic Sowa 			.maxlen		= sizeof(int),
5702b800c3b9SHannes Frederic Sowa 			.mode		= 0644,
5703b800c3b9SHannes Frederic Sowa 			.proc_handler	= proc_dointvec
5704b800c3b9SHannes Frederic Sowa 		},
5705b800c3b9SHannes Frederic Sowa 		{
5706d9333196SBen Greear 			.procname	= "accept_ra_from_local",
5707d9333196SBen Greear 			.data		= &ipv6_devconf.accept_ra_from_local,
5708d9333196SBen Greear 			.maxlen		= sizeof(int),
5709d9333196SBen Greear 			.mode		= 0644,
5710d9333196SBen Greear 			.proc_handler	= proc_dointvec,
5711d9333196SBen Greear 		},
5712d9333196SBen Greear 		{
5713c2943f14SHarout Hedeshian 			.procname	= "accept_ra_mtu",
5714c2943f14SHarout Hedeshian 			.data		= &ipv6_devconf.accept_ra_mtu,
5715c2943f14SHarout Hedeshian 			.maxlen		= sizeof(int),
5716c2943f14SHarout Hedeshian 			.mode		= 0644,
5717c2943f14SHarout Hedeshian 			.proc_handler	= proc_dointvec,
5718c2943f14SHarout Hedeshian 		},
5719c2943f14SHarout Hedeshian 		{
57203d1bec99SHannes Frederic Sowa 			.procname	= "stable_secret",
57213d1bec99SHannes Frederic Sowa 			.data		= &ipv6_devconf.stable_secret,
57223d1bec99SHannes Frederic Sowa 			.maxlen		= IPV6_MAX_STRLEN,
57233d1bec99SHannes Frederic Sowa 			.mode		= 0600,
57243d1bec99SHannes Frederic Sowa 			.proc_handler	= addrconf_sysctl_stable_secret,
57253d1bec99SHannes Frederic Sowa 		},
57263d1bec99SHannes Frederic Sowa 		{
57273985e8a3SErik Kline 			.procname       = "use_oif_addrs_only",
57283985e8a3SErik Kline 			.data           = &ipv6_devconf.use_oif_addrs_only,
57293985e8a3SErik Kline 			.maxlen         = sizeof(int),
57303985e8a3SErik Kline 			.mode           = 0644,
57313985e8a3SErik Kline 			.proc_handler   = proc_dointvec,
573235103d11SAndy Gospodarek 		},
573335103d11SAndy Gospodarek 		{
573435103d11SAndy Gospodarek 			.procname	= "ignore_routes_with_linkdown",
573535103d11SAndy Gospodarek 			.data		= &ipv6_devconf.ignore_routes_with_linkdown,
573635103d11SAndy Gospodarek 			.maxlen		= sizeof(int),
573735103d11SAndy Gospodarek 			.mode		= 0644,
573835103d11SAndy Gospodarek 			.proc_handler	= addrconf_sysctl_ignore_routes_with_linkdown,
57393985e8a3SErik Kline 		},
57403985e8a3SErik Kline 		{
5741f8572d8fSEric W. Biederman 			/* sentinel */
57421da177e4SLinus Torvalds 		}
57431da177e4SLinus Torvalds 	},
57441da177e4SLinus Torvalds };
57451da177e4SLinus Torvalds 
5746bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5747f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
57481da177e4SLinus Torvalds {
57491da177e4SLinus Torvalds 	int i;
57501da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
57516105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
57521dab6222SPavel Emelyanov 
5753af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
575463159f29SIan Morris 	if (!t)
5755f68635e6SPavel Emelyanov 		goto out;
5756f68635e6SPavel Emelyanov 
57571da177e4SLinus Torvalds 	for (i = 0; t->addrconf_vars[i].data; i++) {
57581da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
57591da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
5760bff16c2fSPavel Emelyanov 		t->addrconf_vars[i].extra2 = net;
57611da177e4SLinus Torvalds 	}
57621da177e4SLinus Torvalds 
57636105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
57641da177e4SLinus Torvalds 
57656105e293SEric W. Biederman 	t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
576663159f29SIan Morris 	if (!t->sysctl_header)
57676105e293SEric W. Biederman 		goto free;
5768f68635e6SPavel Emelyanov 
57691da177e4SLinus Torvalds 	p->sysctl = t;
577095897312SPavel Emelyanov 	return 0;
57711da177e4SLinus Torvalds 
57721da177e4SLinus Torvalds free:
57731da177e4SLinus Torvalds 	kfree(t);
5774f68635e6SPavel Emelyanov out:
577595897312SPavel Emelyanov 	return -ENOBUFS;
57761da177e4SLinus Torvalds }
57771da177e4SLinus Torvalds 
5778408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5779f52295a9SPavel Emelyanov {
5780408c4768SPavel Emelyanov 	struct addrconf_sysctl_table *t;
5781f52295a9SPavel Emelyanov 
578263159f29SIan Morris 	if (!p->sysctl)
5783408c4768SPavel Emelyanov 		return;
5784408c4768SPavel Emelyanov 
5785408c4768SPavel Emelyanov 	t = p->sysctl;
57861da177e4SLinus Torvalds 	p->sysctl = NULL;
5787ff538818SLucian Adrian Grijincu 	unregister_net_sysctl_table(t->sysctl_header);
57881da177e4SLinus Torvalds 	kfree(t);
57891da177e4SLinus Torvalds }
5790408c4768SPavel Emelyanov 
5791a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev)
5792408c4768SPavel Emelyanov {
5793a317a2f1SWANG Cong 	int err;
5794a317a2f1SWANG Cong 
5795a317a2f1SWANG Cong 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
5796a317a2f1SWANG Cong 		return -EINVAL;
5797a317a2f1SWANG Cong 
5798a317a2f1SWANG Cong 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
5799f8572d8fSEric W. Biederman 				    &ndisc_ifinfo_sysctl_change);
5800a317a2f1SWANG Cong 	if (err)
5801a317a2f1SWANG Cong 		return err;
5802a317a2f1SWANG Cong 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5803f8572d8fSEric W. Biederman 					 idev, &idev->cnf);
5804a317a2f1SWANG Cong 	if (err)
5805a317a2f1SWANG Cong 		neigh_sysctl_unregister(idev->nd_parms);
5806a317a2f1SWANG Cong 
5807a317a2f1SWANG Cong 	return err;
5808408c4768SPavel Emelyanov }
5809408c4768SPavel Emelyanov 
5810408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
5811408c4768SPavel Emelyanov {
5812408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
5813408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
58141da177e4SLinus Torvalds }
58151da177e4SLinus Torvalds 
58161da177e4SLinus Torvalds 
58171da177e4SLinus Torvalds #endif
58181da177e4SLinus Torvalds 
58192c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
5820e0da5a48SPavel Emelyanov {
5821a79ca223SHong Zhiguo 	int err = -ENOMEM;
5822e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
5823e0da5a48SPavel Emelyanov 
5824a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
582563159f29SIan Morris 	if (!all)
5826e0da5a48SPavel Emelyanov 		goto err_alloc_all;
5827e0da5a48SPavel Emelyanov 
5828a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
582963159f29SIan Morris 	if (!dflt)
5830e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
5831a79ca223SHong Zhiguo 
583256d417b1SBrian Haley 	/* these will be inherited by all namespaces */
583356d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
583456d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5835e0da5a48SPavel Emelyanov 
58363d1bec99SHannes Frederic Sowa 	dflt->stable_secret.initialized = false;
58373d1bec99SHannes Frederic Sowa 	all->stable_secret.initialized = false;
58383d1bec99SHannes Frederic Sowa 
5839e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
5840e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
5841e0da5a48SPavel Emelyanov 
5842e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5843f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
5844e0da5a48SPavel Emelyanov 	if (err < 0)
5845e0da5a48SPavel Emelyanov 		goto err_reg_all;
5846e0da5a48SPavel Emelyanov 
5847f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5848e0da5a48SPavel Emelyanov 	if (err < 0)
5849e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
5850e0da5a48SPavel Emelyanov #endif
5851e0da5a48SPavel Emelyanov 	return 0;
5852e0da5a48SPavel Emelyanov 
5853e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5854e0da5a48SPavel Emelyanov err_reg_dflt:
5855e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
5856e0da5a48SPavel Emelyanov err_reg_all:
5857e0da5a48SPavel Emelyanov 	kfree(dflt);
5858e0da5a48SPavel Emelyanov #endif
5859e0da5a48SPavel Emelyanov err_alloc_dflt:
5860e0da5a48SPavel Emelyanov 	kfree(all);
5861e0da5a48SPavel Emelyanov err_alloc_all:
5862e0da5a48SPavel Emelyanov 	return err;
5863e0da5a48SPavel Emelyanov }
5864e0da5a48SPavel Emelyanov 
58652c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
5866e0da5a48SPavel Emelyanov {
5867e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5868e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5869e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
5870e0da5a48SPavel Emelyanov #endif
5871e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_dflt);
5872e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_all);
5873e0da5a48SPavel Emelyanov }
5874e0da5a48SPavel Emelyanov 
5875e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
5876e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
5877e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
5878e0da5a48SPavel Emelyanov };
5879e0da5a48SPavel Emelyanov 
5880207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = {
5881b382b191SThomas Graf 	.family		  = AF_INET6,
5882b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
5883b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
588411b1f828SDaniel Borkmann 	.validate_link_af = inet6_validate_link_af,
5885f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
5886b382b191SThomas Graf };
5887b382b191SThomas Graf 
58881da177e4SLinus Torvalds /*
58891da177e4SLinus Torvalds  *	Init / cleanup code
58901da177e4SLinus Torvalds  */
58911da177e4SLinus Torvalds 
58921da177e4SLinus Torvalds int __init addrconf_init(void)
58931da177e4SLinus Torvalds {
5894a317a2f1SWANG Cong 	struct inet6_dev *idev;
5895c2e21293Sstephen hemminger 	int i, err;
58962a8cc6c8SYOSHIFUJI Hideaki 
5897e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
5898e21e8467SStephen Hemminger 	if (err < 0) {
5899f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
5900f3213831SJoe Perches 			__func__, err);
59012cc6d2bfSNeil Horman 		goto out;
59022a8cc6c8SYOSHIFUJI Hideaki 	}
59031da177e4SLinus Torvalds 
59042cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
59052cc6d2bfSNeil Horman 	if (err < 0)
59062cc6d2bfSNeil Horman 		goto out_addrlabel;
5907e0da5a48SPavel Emelyanov 
5908c15b1ccaSHannes Frederic Sowa 	addrconf_wq = create_workqueue("ipv6_addrconf");
5909c15b1ccaSHannes Frederic Sowa 	if (!addrconf_wq) {
5910c15b1ccaSHannes Frederic Sowa 		err = -ENOMEM;
5911c15b1ccaSHannes Frederic Sowa 		goto out_nowq;
5912c15b1ccaSHannes Frederic Sowa 	}
5913c15b1ccaSHannes Frederic Sowa 
59141da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
59151da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
59161da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
59171da177e4SLinus Torvalds 	 * to other devices which are up.
59181da177e4SLinus Torvalds 	 *
59191da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
59201da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
59211da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
59221da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
59231da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
59241da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
59251da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
59261da177e4SLinus Torvalds 	 *
59271da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
59281da177e4SLinus Torvalds 	 * loopback_dev first by hand.
59291da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
59301da177e4SLinus Torvalds 	 * device and it being up should be removed.
59311da177e4SLinus Torvalds 	 */
59321da177e4SLinus Torvalds 	rtnl_lock();
5933a317a2f1SWANG Cong 	idev = ipv6_add_dev(init_net.loopback_dev);
59341da177e4SLinus Torvalds 	rtnl_unlock();
5935a317a2f1SWANG Cong 	if (IS_ERR(idev)) {
5936a317a2f1SWANG Cong 		err = PTR_ERR(idev);
5937e0da5a48SPavel Emelyanov 		goto errlo;
5938a317a2f1SWANG Cong 	}
59391da177e4SLinus Torvalds 
5940c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
5941c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5942c2e21293Sstephen hemminger 
59431da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
59441da177e4SLinus Torvalds 
5945c15b1ccaSHannes Frederic Sowa 	addrconf_verify();
5946c127ea2cSThomas Graf 
59473678a9d8Sstephen hemminger 	rtnl_af_register(&inet6_ops);
5948b382b191SThomas Graf 
5949c7ac8679SGreg Rose 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5950c7ac8679SGreg Rose 			      NULL);
5951c127ea2cSThomas Graf 	if (err < 0)
5952c127ea2cSThomas Graf 		goto errout;
5953c127ea2cSThomas Graf 
5954c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
5955c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5956c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5957c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5958c7ac8679SGreg Rose 			inet6_dump_ifaddr, NULL);
5959c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5960c7ac8679SGreg Rose 			inet6_dump_ifmcaddr, NULL);
5961c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5962c7ac8679SGreg Rose 			inet6_dump_ifacaddr, NULL);
596376f8f6cbSNicolas Dichtel 	__rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
59647a674200SNicolas Dichtel 			inet6_netconf_dump_devconf, NULL);
5965c127ea2cSThomas Graf 
59662a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
59672a8cc6c8SYOSHIFUJI Hideaki 
59681da177e4SLinus Torvalds 	return 0;
5969c127ea2cSThomas Graf errout:
5970b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
5971c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
5972e0da5a48SPavel Emelyanov errlo:
5973c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
5974c15b1ccaSHannes Frederic Sowa out_nowq:
5975e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
59762cc6d2bfSNeil Horman out_addrlabel:
59772cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
59782cc6d2bfSNeil Horman out:
5979c127ea2cSThomas Graf 	return err;
59801da177e4SLinus Torvalds }
59811da177e4SLinus Torvalds 
598209f7709fSDaniel Lezcano void addrconf_cleanup(void)
59831da177e4SLinus Torvalds {
5984176c39afSDaniel Lezcano 	struct net_device *dev;
59851da177e4SLinus Torvalds 	int i;
59861da177e4SLinus Torvalds 
59871da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
5988e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
59892cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
59901da177e4SLinus Torvalds 
59911da177e4SLinus Torvalds 	rtnl_lock();
59921da177e4SLinus Torvalds 
5993b382b191SThomas Graf 	__rtnl_af_unregister(&inet6_ops);
5994b382b191SThomas Graf 
5995176c39afSDaniel Lezcano 	/* clean dev list */
5996176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
5997176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
5998176c39afSDaniel Lezcano 			continue;
5999176c39afSDaniel Lezcano 		addrconf_ifdown(dev, 1);
6000176c39afSDaniel Lezcano 	}
6001176c39afSDaniel Lezcano 	addrconf_ifdown(init_net.loopback_dev, 2);
6002176c39afSDaniel Lezcano 
60031da177e4SLinus Torvalds 	/*
60041da177e4SLinus Torvalds 	 *	Check hash table.
60051da177e4SLinus Torvalds 	 */
60065c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
6007c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
6008c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
60095c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
6010c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
60111da177e4SLinus Torvalds 	rtnl_unlock();
6012c15b1ccaSHannes Frederic Sowa 
6013c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
60141da177e4SLinus Torvalds }
6015