xref: /openbmc/linux/net/ipv6/addrconf.c (revision 47e26941f78d6ec03c5dd547425409f1f7852ff5)
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>
46174cd4b1SIngo Molnar #include <linux/sched/signal.h>
471da177e4SLinus Torvalds #include <linux/socket.h>
481da177e4SLinus Torvalds #include <linux/sockios.h>
491da177e4SLinus Torvalds #include <linux/net.h>
503d1bec99SHannes Frederic Sowa #include <linux/inet.h>
511da177e4SLinus Torvalds #include <linux/in6.h>
521da177e4SLinus Torvalds #include <linux/netdevice.h>
531823730fSThomas Graf #include <linux/if_addr.h>
541da177e4SLinus Torvalds #include <linux/if_arp.h>
551da177e4SLinus Torvalds #include <linux/if_arcnet.h>
561da177e4SLinus Torvalds #include <linux/if_infiniband.h>
571da177e4SLinus Torvalds #include <linux/route.h>
581da177e4SLinus Torvalds #include <linux/inetdevice.h>
591da177e4SLinus Torvalds #include <linux/init.h>
605a0e3ad6STejun Heo #include <linux/slab.h>
611da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
621da177e4SLinus Torvalds #include <linux/sysctl.h>
631da177e4SLinus Torvalds #endif
644fc268d2SRandy Dunlap #include <linux/capability.h>
651da177e4SLinus Torvalds #include <linux/delay.h>
661da177e4SLinus Torvalds #include <linux/notifier.h>
67543537bdSPaulo Marques #include <linux/string.h>
68ddbe5032SEric Dumazet #include <linux/hash.h>
691da177e4SLinus Torvalds 
70457c4cbcSEric W. Biederman #include <net/net_namespace.h>
711da177e4SLinus Torvalds #include <net/sock.h>
721da177e4SLinus Torvalds #include <net/snmp.h>
731da177e4SLinus Torvalds 
745241c2d7SAlexander Aring #include <net/6lowpan.h>
75cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h>
761da177e4SLinus Torvalds #include <net/ipv6.h>
771da177e4SLinus Torvalds #include <net/protocol.h>
781da177e4SLinus Torvalds #include <net/ndisc.h>
791da177e4SLinus Torvalds #include <net/ip6_route.h>
801da177e4SLinus Torvalds #include <net/addrconf.h>
811da177e4SLinus Torvalds #include <net/tcp.h>
821da177e4SLinus Torvalds #include <net/ip.h>
835d620266SThomas Graf #include <net/netlink.h>
84f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
85ca254490SDavid Ahern #include <net/l3mdev.h>
861da177e4SLinus Torvalds #include <linux/if_tunnel.h>
871da177e4SLinus Torvalds #include <linux/rtnetlink.h>
88f3a1bfb1SNicolas Dichtel #include <linux/netconf.h>
891da177e4SLinus Torvalds #include <linux/random.h>
90e21e8467SStephen Hemminger #include <linux/uaccess.h>
917f7d9a6bSHerbert Xu #include <asm/unaligned.h>
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #include <linux/proc_fs.h>
941da177e4SLinus Torvalds #include <linux/seq_file.h>
95bc3b2d7fSPaul Gortmaker #include <linux/export.h>
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds /* Set to 3 to get tracing... */
981da177e4SLinus Torvalds #define ACONF_DEBUG 2
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
101ba3542e1Sdingtianhong #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
1021da177e4SLinus Torvalds #else
103ba3542e1Sdingtianhong #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
1041da177e4SLinus Torvalds #endif
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
10718a31e1eSThomas Graf 
1083d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \
1093d1bec99SHannes Frederic Sowa 	sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
1103d1bec99SHannes Frederic Sowa 
11118a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp)
11218a31e1eSThomas Graf {
11318a31e1eSThomas Graf 	return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
11418a31e1eSThomas Graf }
1151da177e4SLinus Torvalds 
116bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_init(s32 irt)
117bd11f074SMaciej Żenczykowski {
118bd11f074SMaciej Żenczykowski 	/* multiply 'initial retransmission time' by 0.9 .. 1.1 */
119bd11f074SMaciej Żenczykowski 	u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
120bd11f074SMaciej Żenczykowski 	do_div(tmp, 1000000);
121bd11f074SMaciej Żenczykowski 	return (s32)tmp;
122bd11f074SMaciej Żenczykowski }
123bd11f074SMaciej Żenczykowski 
124bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
125bd11f074SMaciej Żenczykowski {
126bd11f074SMaciej Żenczykowski 	/* multiply 'retransmission timeout' by 1.9 .. 2.1 */
127bd11f074SMaciej Żenczykowski 	u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
128bd11f074SMaciej Żenczykowski 	do_div(tmp, 1000000);
129bd11f074SMaciej Żenczykowski 	if ((s32)tmp > mrt) {
130bd11f074SMaciej Żenczykowski 		/* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
131bd11f074SMaciej Żenczykowski 		tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
132bd11f074SMaciej Żenczykowski 		do_div(tmp, 1000000);
133bd11f074SMaciej Żenczykowski 	}
134bd11f074SMaciej Żenczykowski 	return (s32)tmp;
135bd11f074SMaciej Żenczykowski }
136bd11f074SMaciej Żenczykowski 
1371da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
138a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev);
139408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
140408c4768SPavel Emelyanov #else
141a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev)
142408c4768SPavel Emelyanov {
143a317a2f1SWANG Cong 	return 0;
144408c4768SPavel Emelyanov }
145408c4768SPavel Emelyanov 
146408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
147408c4768SPavel Emelyanov {
148408c4768SPavel Emelyanov }
1491da177e4SLinus Torvalds #endif
1501da177e4SLinus Torvalds 
1519d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev);
1529d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1531da177e4SLinus Torvalds 
1541b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
155d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev);
156622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr,
157622c81d5SHannes Frederic Sowa 					u8 dad_count,
158622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds /*
1611da177e4SLinus Torvalds  *	Configured unicast address hash table
1621da177e4SLinus Torvalds  */
163c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
1645c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock);
1651da177e4SLinus Torvalds 
166c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void);
167c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void);
168c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *);
1691da177e4SLinus Torvalds 
170c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq;
171c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1741da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1751da177e4SLinus Torvalds 
17693d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev,
17775c78500SMoni Shoua 				 unsigned long event);
1781da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1791da177e4SLinus Torvalds 
18021caa662SRomain Kuntz static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
18121caa662SRomain Kuntz 						  int plen,
18221caa662SRomain Kuntz 						  const struct net_device *dev,
18321caa662SRomain Kuntz 						  u32 flags, u32 noflags);
18421caa662SRomain Kuntz 
185cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp);
186c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w);
187764d3be6SPaolo Abeni static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id);
188c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1891da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1901da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1911da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1941da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1953e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
19606bfe655SDaniel Lezcano 			       struct net_device *dev);
1971da177e4SLinus Torvalds 
198888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1991da177e4SLinus Torvalds 	.forwarding		= 0,
2001da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2011da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2021da177e4SLinus Torvalds 	.accept_ra		= 1,
2031da177e4SLinus Torvalds 	.accept_redirects	= 1,
2041da177e4SLinus Torvalds 	.autoconf		= 1,
2051da177e4SLinus Torvalds 	.force_mld_version	= 0,
206fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
207fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2081da177e4SLinus Torvalds 	.dad_transmits		= 1,
2091da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2101da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
211bd11f074SMaciej Żenczykowski 	.rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
2121da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2131da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2141da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2151da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2161da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2171da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2181da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
21965f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
220d9333196SBen Greear 	.accept_ra_from_local	= 0,
2218013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
222c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
223930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
224930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
22552e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
22609c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
227bbea124bSJoel Scherpelz 	.accept_ra_rt_info_min_plen = 0,
22809c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
22909c884d4SYOSHIFUJI Hideaki #endif
230930d6ff2SYOSHIFUJI Hideaki #endif
231fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2320bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
233778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2341b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
235b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
236c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2373d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2383d1bec99SHannes Frederic Sowa 		.initialized = false,
2393985e8a3SErik Kline 	},
2403985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
24135103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
242f1705ec1SDavid Ahern 	.keep_addr_on_down	= 0,
2431ababebaSDavid Lebrun 	.seg6_enabled		= 0,
244bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
245bf355b8dSDavid Lebrun 	.seg6_require_hmac	= 0,
246bf355b8dSDavid Lebrun #endif
247adc176c5SErik Nordmark 	.enhanced_dad           = 1,
248d35a00b8SFelix Jia 	.addr_gen_mode		= IN6_ADDR_GEN_MODE_EUI64,
249df789fe7SDavid Forster 	.disable_policy		= 0,
2501da177e4SLinus Torvalds };
2511da177e4SLinus Torvalds 
252ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2531da177e4SLinus Torvalds 	.forwarding		= 0,
2541da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2551da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2561da177e4SLinus Torvalds 	.accept_ra		= 1,
2571da177e4SLinus Torvalds 	.accept_redirects	= 1,
2581da177e4SLinus Torvalds 	.autoconf		= 1,
259fc4eba58SHannes Frederic Sowa 	.force_mld_version	= 0,
260fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
261fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2621da177e4SLinus Torvalds 	.dad_transmits		= 1,
2631da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2641da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
265bd11f074SMaciej Żenczykowski 	.rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
2661da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2671da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2681da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2691da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2701da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2711da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2721da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
27365f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
274d9333196SBen Greear 	.accept_ra_from_local	= 0,
2758013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
276c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
277930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
278930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
27952e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
28009c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
281bbea124bSJoel Scherpelz 	.accept_ra_rt_info_min_plen = 0,
28209c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
28309c884d4SYOSHIFUJI Hideaki #endif
284930d6ff2SYOSHIFUJI Hideaki #endif
285fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2860bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
287778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2881b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
289b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
290c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2913d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2923d1bec99SHannes Frederic Sowa 		.initialized = false,
2933d1bec99SHannes Frederic Sowa 	},
2943985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
29535103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
296f1705ec1SDavid Ahern 	.keep_addr_on_down	= 0,
2971ababebaSDavid Lebrun 	.seg6_enabled		= 0,
298bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
299bf355b8dSDavid Lebrun 	.seg6_require_hmac	= 0,
300bf355b8dSDavid Lebrun #endif
301adc176c5SErik Nordmark 	.enhanced_dad           = 1,
302d35a00b8SFelix Jia 	.addr_gen_mode		= IN6_ADDR_GEN_MODE_EUI64,
303df789fe7SDavid Forster 	.disable_policy		= 0,
3041da177e4SLinus Torvalds };
3051da177e4SLinus Torvalds 
3061f372c7bSMike Manning /* Check if link is ready: is it up and is a valid qdisc available */
3071f372c7bSMike Manning static inline bool addrconf_link_ready(const struct net_device *dev)
308f24e3d65SMitsuru Chinen {
3091f372c7bSMike Manning 	return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
310f24e3d65SMitsuru Chinen }
311f24e3d65SMitsuru Chinen 
312b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev)
3131da177e4SLinus Torvalds {
314b7b1bfceSHannes Frederic Sowa 	if (del_timer(&idev->rs_timer))
315b7b1bfceSHannes Frederic Sowa 		__in6_dev_put(idev);
316b7b1bfceSHannes Frederic Sowa }
317b7b1bfceSHannes Frederic Sowa 
318c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
319b7b1bfceSHannes Frederic Sowa {
320c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
3211da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
324b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev,
3251da177e4SLinus Torvalds 				  unsigned long when)
3261da177e4SLinus Torvalds {
327b7b1bfceSHannes Frederic Sowa 	if (!timer_pending(&idev->rs_timer))
328b7b1bfceSHannes Frederic Sowa 		in6_dev_hold(idev);
329b7b1bfceSHannes Frederic Sowa 	mod_timer(&idev->rs_timer, jiffies + when);
3301da177e4SLinus Torvalds }
331b7b1bfceSHannes Frederic Sowa 
332c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
333c15b1ccaSHannes Frederic Sowa 				   unsigned long delay)
334b7b1bfceSHannes Frederic Sowa {
335b7b1bfceSHannes Frederic Sowa 	in6_ifa_hold(ifp);
336f8a894b2SXin Long 	if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
337f8a894b2SXin Long 		in6_ifa_put(ifp);
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3407f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
3417f7d9a6bSHerbert Xu {
342827da44cSJohn Stultz 	int i;
343827da44cSJohn Stultz 
344698365faSWANG Cong 	idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
345698365faSWANG Cong 	if (!idev->stats.ipv6)
3467f7d9a6bSHerbert Xu 		goto err_ip;
347827da44cSJohn Stultz 
348827da44cSJohn Stultz 	for_each_possible_cpu(i) {
349827da44cSJohn Stultz 		struct ipstats_mib *addrconf_stats;
350698365faSWANG Cong 		addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
351827da44cSJohn Stultz 		u64_stats_init(&addrconf_stats->syncp);
352827da44cSJohn Stultz 	}
353827da44cSJohn Stultz 
354827da44cSJohn Stultz 
355be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
356be281e55SEric Dumazet 					GFP_KERNEL);
357be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
3587f7d9a6bSHerbert Xu 		goto err_icmp;
359be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
360be281e55SEric Dumazet 					   GFP_KERNEL);
361be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
36214878f75SDavid L Stevens 		goto err_icmpmsg;
3637f7d9a6bSHerbert Xu 
3647f7d9a6bSHerbert Xu 	return 0;
3657f7d9a6bSHerbert Xu 
36614878f75SDavid L Stevens err_icmpmsg:
367be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3687f7d9a6bSHerbert Xu err_icmp:
369698365faSWANG Cong 	free_percpu(idev->stats.ipv6);
3707f7d9a6bSHerbert Xu err_ip:
371aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3727f7d9a6bSHerbert Xu }
3737f7d9a6bSHerbert Xu 
3741da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	struct inet6_dev *ndev;
377a317a2f1SWANG Cong 	int err = -ENOMEM;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	ASSERT_RTNL();
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
382a317a2f1SWANG Cong 		return ERR_PTR(-EINVAL);
3831da177e4SLinus Torvalds 
384322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
38563159f29SIan Morris 	if (!ndev)
386a317a2f1SWANG Cong 		return ERR_PTR(err);
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3891da177e4SLinus Torvalds 	ndev->dev = dev;
390502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
391b7b1bfceSHannes Frederic Sowa 	setup_timer(&ndev->rs_timer, addrconf_rs_timer,
392b7b1bfceSHannes Frederic Sowa 		    (unsigned long)ndev);
393c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3949b29c696SHannes Frederic Sowa 
3959b29c696SHannes Frederic Sowa 	if (ndev->cnf.stable_secret.initialized)
396d35a00b8SFelix Jia 		ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
3979b29c696SHannes Frederic Sowa 	else
398d35a00b8SFelix Jia 		ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode;
3999b29c696SHannes Frederic Sowa 
4001da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
4011da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
40263159f29SIan Morris 	if (!ndev->nd_parms) {
4031da177e4SLinus Torvalds 		kfree(ndev);
404a317a2f1SWANG Cong 		return ERR_PTR(err);
4051da177e4SLinus Torvalds 	}
4060187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
4070187bdfbSBen Hutchings 		dev_disable_lro(dev);
4081da177e4SLinus Torvalds 	/* We refer to the device */
4091da177e4SLinus Torvalds 	dev_hold(dev);
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
412ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
413f3213831SJoe Perches 			"%s: cannot allocate memory for statistics; dev=%s.\n",
414ba3542e1Sdingtianhong 			__func__, dev->name);
4151da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
4168603e33dSRoy Li 		dev_put(dev);
4178603e33dSRoy Li 		kfree(ndev);
418a317a2f1SWANG Cong 		return ERR_PTR(err);
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
422ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
423f3213831SJoe Perches 			"%s: cannot create /proc/net/dev_snmp6/%s\n",
424ba3542e1Sdingtianhong 			__func__, dev->name);
425a317a2f1SWANG Cong 		goto err_release;
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 
4289d6280daSJiri Bohac 	/* One reference from device. */
4291be92460SReshetova, Elena 	refcount_set(&ndev->refcnt, 1);
4301da177e4SLinus Torvalds 
4311b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
4321b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
4331b34be74SYOSHIFUJI Hideaki 
43407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
435c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
436f3213831SJoe Perches 		pr_info("%s: Disabled Multicast RS\n", dev->name);
437c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
438c7dc89c0SFred L. Templin 	}
439b077d7abSYOSHIFUJI Hideaki #endif
440b077d7abSYOSHIFUJI Hideaki 
441372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
44276506a98SJiri Bohac 	ndev->desync_factor = U32_MAX;
4431da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
4441da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4459625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4461da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4471da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4480be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
4499d6280daSJiri Bohac 	} else
4509d6280daSJiri Bohac 		ipv6_regen_rndid(ndev);
4515d9efa7eSDavid S. Miller 
452914faa14SDaniel Borkmann 	ndev->token = in6addr_any;
4531da177e4SLinus Torvalds 
4541f372c7bSMike Manning 	if (netif_running(dev) && addrconf_link_ready(dev))
45553aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
45653aadcc9SHerbert Xu 
4571da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4581da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
459a317a2f1SWANG Cong 	err = addrconf_sysctl_register(ndev);
460a317a2f1SWANG Cong 	if (err) {
461a317a2f1SWANG Cong 		ipv6_mc_destroy_dev(ndev);
4622a189f9eSSabrina Dubroca 		snmp6_unregister_dev(ndev);
463a317a2f1SWANG Cong 		goto err_release;
464a317a2f1SWANG Cong 	}
46530c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
466cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip6_ptr, ndev);
467d88ae4ccSYOSHIFUJI Hideaki 
4682c5e8933SHannes Frederic Sowa 	/* Join interface-local all-node multicast group */
4692c5e8933SHannes Frederic Sowa 	ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4702c5e8933SHannes Frederic Sowa 
471d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
472f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
473d88ae4ccSYOSHIFUJI Hideaki 
474d6ddef9eSLi Wei 	/* Join all-router multicast group if forwarding is set */
4758b2aaedeSLi Wei 	if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
476d6ddef9eSLi Wei 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
477d6ddef9eSLi Wei 
4781da177e4SLinus Torvalds 	return ndev;
479a317a2f1SWANG Cong 
480a317a2f1SWANG Cong err_release:
481a317a2f1SWANG Cong 	neigh_parms_release(&nd_tbl, ndev->nd_parms);
482a317a2f1SWANG Cong 	ndev->dead = 1;
483a317a2f1SWANG Cong 	in6_dev_finish_destroy(ndev);
484a317a2f1SWANG Cong 	return ERR_PTR(err);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
487cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	struct inet6_dev *idev;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	ASSERT_RTNL();
4921da177e4SLinus Torvalds 
493e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
494e21e8467SStephen Hemminger 	if (!idev) {
495e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
496a317a2f1SWANG Cong 		if (IS_ERR(idev))
4971da177e4SLinus Torvalds 			return NULL;
4981da177e4SLinus Torvalds 	}
499c5e33bddSYOSHIFUJI Hideaki 
5001da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
5011da177e4SLinus Torvalds 		ipv6_mc_up(idev);
5021da177e4SLinus Torvalds 	return idev;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
505f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
506f3a1bfb1SNicolas Dichtel {
507f3a1bfb1SNicolas Dichtel 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
508f3a1bfb1SNicolas Dichtel 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
509136ba622SZhang Shengju 	bool all = false;
510f3a1bfb1SNicolas Dichtel 
511136ba622SZhang Shengju 	if (type == NETCONFA_ALL)
512136ba622SZhang Shengju 		all = true;
513136ba622SZhang Shengju 
514136ba622SZhang Shengju 	if (all || type == NETCONFA_FORWARDING)
515f3a1bfb1SNicolas Dichtel 		size += nla_total_size(4);
516b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
517136ba622SZhang Shengju 	if (all || type == NETCONFA_MC_FORWARDING)
518d67b8c61SNicolas Dichtel 		size += nla_total_size(4);
519b1afce95SDavid S. Miller #endif
520136ba622SZhang Shengju 	if (all || type == NETCONFA_PROXY_NEIGH)
521c92d5491Sstephen hemminger 		size += nla_total_size(4);
522f3a1bfb1SNicolas Dichtel 
523136ba622SZhang Shengju 	if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
52435103d11SAndy Gospodarek 		size += nla_total_size(4);
52535103d11SAndy Gospodarek 
526f3a1bfb1SNicolas Dichtel 	return size;
527f3a1bfb1SNicolas Dichtel }
528f3a1bfb1SNicolas Dichtel 
529f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
530f3a1bfb1SNicolas Dichtel 				      struct ipv6_devconf *devconf, u32 portid,
531f3a1bfb1SNicolas Dichtel 				      u32 seq, int event, unsigned int flags,
532f3a1bfb1SNicolas Dichtel 				      int type)
533f3a1bfb1SNicolas Dichtel {
534f3a1bfb1SNicolas Dichtel 	struct nlmsghdr  *nlh;
535f3a1bfb1SNicolas Dichtel 	struct netconfmsg *ncm;
536136ba622SZhang Shengju 	bool all = false;
537f3a1bfb1SNicolas Dichtel 
538f3a1bfb1SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
539f3a1bfb1SNicolas Dichtel 			flags);
54063159f29SIan Morris 	if (!nlh)
541f3a1bfb1SNicolas Dichtel 		return -EMSGSIZE;
542f3a1bfb1SNicolas Dichtel 
543136ba622SZhang Shengju 	if (type == NETCONFA_ALL)
544136ba622SZhang Shengju 		all = true;
545136ba622SZhang Shengju 
546f3a1bfb1SNicolas Dichtel 	ncm = nlmsg_data(nlh);
547f3a1bfb1SNicolas Dichtel 	ncm->ncm_family = AF_INET6;
548f3a1bfb1SNicolas Dichtel 
549f3a1bfb1SNicolas Dichtel 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
550f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
551f3a1bfb1SNicolas Dichtel 
55223452170SDavid Ahern 	if (!devconf)
55323452170SDavid Ahern 		goto out;
55423452170SDavid Ahern 
555136ba622SZhang Shengju 	if ((all || type == NETCONFA_FORWARDING) &&
556f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
557f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
558b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
559136ba622SZhang Shengju 	if ((all || type == NETCONFA_MC_FORWARDING) &&
560d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
561d67b8c61SNicolas Dichtel 			devconf->mc_forwarding) < 0)
562d67b8c61SNicolas Dichtel 		goto nla_put_failure;
563b1afce95SDavid S. Miller #endif
564136ba622SZhang Shengju 	if ((all || type == NETCONFA_PROXY_NEIGH) &&
565c92d5491Sstephen hemminger 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
566c92d5491Sstephen hemminger 		goto nla_put_failure;
567c92d5491Sstephen hemminger 
568136ba622SZhang Shengju 	if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
56935103d11SAndy Gospodarek 	    nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
57035103d11SAndy Gospodarek 			devconf->ignore_routes_with_linkdown) < 0)
57135103d11SAndy Gospodarek 		goto nla_put_failure;
57235103d11SAndy Gospodarek 
57323452170SDavid Ahern out:
574053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
575053c095aSJohannes Berg 	return 0;
576f3a1bfb1SNicolas Dichtel 
577f3a1bfb1SNicolas Dichtel nla_put_failure:
578f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
579f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
580f3a1bfb1SNicolas Dichtel }
581f3a1bfb1SNicolas Dichtel 
58285b3daadSDavid Ahern void inet6_netconf_notify_devconf(struct net *net, int event, int type,
58385b3daadSDavid Ahern 				  int ifindex, struct ipv6_devconf *devconf)
584f3a1bfb1SNicolas Dichtel {
585f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
586f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
587f3a1bfb1SNicolas Dichtel 
588927265bcSEric Dumazet 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
58963159f29SIan Morris 	if (!skb)
590f3a1bfb1SNicolas Dichtel 		goto errout;
591f3a1bfb1SNicolas Dichtel 
592f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
59385b3daadSDavid Ahern 					 event, 0, type);
594f3a1bfb1SNicolas Dichtel 	if (err < 0) {
595f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
596f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
597f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
598f3a1bfb1SNicolas Dichtel 		goto errout;
599f3a1bfb1SNicolas Dichtel 	}
600927265bcSEric Dumazet 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
601f3a1bfb1SNicolas Dichtel 	return;
602f3a1bfb1SNicolas Dichtel errout:
603f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
604f3a1bfb1SNicolas Dichtel }
605f3a1bfb1SNicolas Dichtel 
60676f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
60776f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
60876f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
609c92d5491Sstephen hemminger 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
61035103d11SAndy Gospodarek 	[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]	= { .len = sizeof(int) },
61176f8f6cbSNicolas Dichtel };
61276f8f6cbSNicolas Dichtel 
61376f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
614c21ef3e3SDavid Ahern 				     struct nlmsghdr *nlh,
615c21ef3e3SDavid Ahern 				     struct netlink_ext_ack *extack)
61676f8f6cbSNicolas Dichtel {
61776f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
61876f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
61976f8f6cbSNicolas Dichtel 	struct netconfmsg *ncm;
62076f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
62176f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
62276f8f6cbSNicolas Dichtel 	struct inet6_dev *in6_dev;
62376f8f6cbSNicolas Dichtel 	struct net_device *dev;
62476f8f6cbSNicolas Dichtel 	int ifindex;
62576f8f6cbSNicolas Dichtel 	int err;
62676f8f6cbSNicolas Dichtel 
62776f8f6cbSNicolas Dichtel 	err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
628c21ef3e3SDavid Ahern 			  devconf_ipv6_policy, extack);
62976f8f6cbSNicolas Dichtel 	if (err < 0)
63076f8f6cbSNicolas Dichtel 		goto errout;
63176f8f6cbSNicolas Dichtel 
632a97eb33fSAnton Protopopov 	err = -EINVAL;
63376f8f6cbSNicolas Dichtel 	if (!tb[NETCONFA_IFINDEX])
63476f8f6cbSNicolas Dichtel 		goto errout;
63576f8f6cbSNicolas Dichtel 
63676f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
63776f8f6cbSNicolas Dichtel 	switch (ifindex) {
63876f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
63976f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
64076f8f6cbSNicolas Dichtel 		break;
64176f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
64276f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
64376f8f6cbSNicolas Dichtel 		break;
64476f8f6cbSNicolas Dichtel 	default:
64576f8f6cbSNicolas Dichtel 		dev = __dev_get_by_index(net, ifindex);
64663159f29SIan Morris 		if (!dev)
64776f8f6cbSNicolas Dichtel 			goto errout;
64876f8f6cbSNicolas Dichtel 		in6_dev = __in6_dev_get(dev);
64963159f29SIan Morris 		if (!in6_dev)
65076f8f6cbSNicolas Dichtel 			goto errout;
65176f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
65276f8f6cbSNicolas Dichtel 		break;
65376f8f6cbSNicolas Dichtel 	}
65476f8f6cbSNicolas Dichtel 
65576f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
656136ba622SZhang Shengju 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
65763159f29SIan Morris 	if (!skb)
65876f8f6cbSNicolas Dichtel 		goto errout;
65976f8f6cbSNicolas Dichtel 
66076f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
66176f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
66276f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
663136ba622SZhang Shengju 					 NETCONFA_ALL);
66476f8f6cbSNicolas Dichtel 	if (err < 0) {
66576f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
66676f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
66776f8f6cbSNicolas Dichtel 		kfree_skb(skb);
66876f8f6cbSNicolas Dichtel 		goto errout;
66976f8f6cbSNicolas Dichtel 	}
67076f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
67176f8f6cbSNicolas Dichtel errout:
67276f8f6cbSNicolas Dichtel 	return err;
67376f8f6cbSNicolas Dichtel }
67476f8f6cbSNicolas Dichtel 
6757a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
6767a674200SNicolas Dichtel 				      struct netlink_callback *cb)
6777a674200SNicolas Dichtel {
6787a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
6797a674200SNicolas Dichtel 	int h, s_h;
6807a674200SNicolas Dichtel 	int idx, s_idx;
6817a674200SNicolas Dichtel 	struct net_device *dev;
6827a674200SNicolas Dichtel 	struct inet6_dev *idev;
6837a674200SNicolas Dichtel 	struct hlist_head *head;
6847a674200SNicolas Dichtel 
6857a674200SNicolas Dichtel 	s_h = cb->args[0];
6867a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
6877a674200SNicolas Dichtel 
6887a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6897a674200SNicolas Dichtel 		idx = 0;
6907a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
6917a674200SNicolas Dichtel 		rcu_read_lock();
69263998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
69363998ac2SNicolas Dichtel 			  net->dev_base_seq;
6947a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
6957a674200SNicolas Dichtel 			if (idx < s_idx)
6967a674200SNicolas Dichtel 				goto cont;
6977a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
6987a674200SNicolas Dichtel 			if (!idev)
6997a674200SNicolas Dichtel 				goto cont;
7007a674200SNicolas Dichtel 
7017a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
7027a674200SNicolas Dichtel 						       &idev->cnf,
7037a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
7047a674200SNicolas Dichtel 						       cb->nlh->nlmsg_seq,
7057a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
7067a674200SNicolas Dichtel 						       NLM_F_MULTI,
707136ba622SZhang Shengju 						       NETCONFA_ALL) < 0) {
7087a674200SNicolas Dichtel 				rcu_read_unlock();
7097a674200SNicolas Dichtel 				goto done;
7107a674200SNicolas Dichtel 			}
71163998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7127a674200SNicolas Dichtel cont:
7137a674200SNicolas Dichtel 			idx++;
7147a674200SNicolas Dichtel 		}
7157a674200SNicolas Dichtel 		rcu_read_unlock();
7167a674200SNicolas Dichtel 	}
7177a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
7187a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
7197a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
7207a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
7217a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
7227a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
723136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
7247a674200SNicolas Dichtel 			goto done;
7257a674200SNicolas Dichtel 		else
7267a674200SNicolas Dichtel 			h++;
7277a674200SNicolas Dichtel 	}
7287a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
7297a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
7307a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
7317a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
7327a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
7337a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
734136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
7357a674200SNicolas Dichtel 			goto done;
7367a674200SNicolas Dichtel 		else
7377a674200SNicolas Dichtel 			h++;
7387a674200SNicolas Dichtel 	}
7397a674200SNicolas Dichtel done:
7407a674200SNicolas Dichtel 	cb->args[0] = h;
7417a674200SNicolas Dichtel 	cb->args[1] = idx;
7427a674200SNicolas Dichtel 
7437a674200SNicolas Dichtel 	return skb->len;
7447a674200SNicolas Dichtel }
7457a674200SNicolas Dichtel 
7461da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
7471da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	struct net_device *dev;
7501da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	if (!idev)
7531da177e4SLinus Torvalds 		return;
7541da177e4SLinus Torvalds 	dev = idev->dev;
7550187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
7560187bdfbSBen Hutchings 		dev_disable_lro(dev);
7571a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
7582c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
759f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
7602c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
7612c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
7622c5e8933SHannes Frederic Sowa 		} else {
763f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
7642c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
7652c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
7662c5e8933SHannes Frederic Sowa 		}
7671da177e4SLinus Torvalds 	}
768502a2ffdSstephen hemminger 
769502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
7702c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
7712c12a74cSMichal Wrobel 			continue;
7721da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
7731da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
7741da177e4SLinus Torvalds 		else
7751da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
7761da177e4SLinus Torvalds 	}
77785b3daadSDavid Ahern 	inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
77885b3daadSDavid Ahern 				     NETCONFA_FORWARDING,
779f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
7801da177e4SLinus Torvalds }
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 
783e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	struct net_device *dev;
7861da177e4SLinus Torvalds 	struct inet6_dev *idev;
7871da177e4SLinus Torvalds 
7884acd4945SBen Hutchings 	for_each_netdev(net, dev) {
7891da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
7901da177e4SLinus Torvalds 		if (idev) {
791e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
792e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
7931da177e4SLinus Torvalds 			if (changed)
7941da177e4SLinus Torvalds 				dev_forward_change(idev);
7951da177e4SLinus Torvalds 		}
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds }
798c8fecf22SPavel Emelyanov 
799013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
800c8fecf22SPavel Emelyanov {
801bff16c2fSPavel Emelyanov 	struct net *net;
802013d97e9SFrancesco Ruggeri 	int old;
803013d97e9SFrancesco Ruggeri 
804013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
805013d97e9SFrancesco Ruggeri 		return restart_syscall();
806bff16c2fSPavel Emelyanov 
807bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
808013d97e9SFrancesco Ruggeri 	old = *p;
809013d97e9SFrancesco Ruggeri 	*p = newf;
810c8fecf22SPavel Emelyanov 
811013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
812f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
81385b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
81485b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
815f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
816f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
817013d97e9SFrancesco Ruggeri 		rtnl_unlock();
818013d97e9SFrancesco Ruggeri 		return 0;
81988af182eSEric W. Biederman 	}
820b325fddbSStephen Hemminger 
821e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
822d26c638cSNicolas Dichtel 		int old_dflt = net->ipv6.devconf_dflt->forwarding;
823d26c638cSNicolas Dichtel 
824e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
825d26c638cSNicolas Dichtel 		if ((!newf) ^ (!old_dflt))
82685b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
82785b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
828d26c638cSNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
829d26c638cSNicolas Dichtel 						     net->ipv6.devconf_dflt);
830d26c638cSNicolas Dichtel 
831e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
832f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
83385b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
83485b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
835f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
836f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
837013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
838c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
8390187bdfbSBen Hutchings 	rtnl_unlock();
840c8fecf22SPavel Emelyanov 
841013d97e9SFrancesco Ruggeri 	if (newf)
8427b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
843b325fddbSStephen Hemminger 	return 1;
844c8fecf22SPavel Emelyanov }
84535103d11SAndy Gospodarek 
84635103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf)
84735103d11SAndy Gospodarek {
84835103d11SAndy Gospodarek 	struct net_device *dev;
84935103d11SAndy Gospodarek 	struct inet6_dev *idev;
85035103d11SAndy Gospodarek 
85135103d11SAndy Gospodarek 	for_each_netdev(net, dev) {
85235103d11SAndy Gospodarek 		idev = __in6_dev_get(dev);
85335103d11SAndy Gospodarek 		if (idev) {
85435103d11SAndy Gospodarek 			int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
85535103d11SAndy Gospodarek 
85635103d11SAndy Gospodarek 			idev->cnf.ignore_routes_with_linkdown = newf;
85735103d11SAndy Gospodarek 			if (changed)
85835103d11SAndy Gospodarek 				inet6_netconf_notify_devconf(dev_net(dev),
85985b3daadSDavid Ahern 							     RTM_NEWNETCONF,
86035103d11SAndy Gospodarek 							     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
86135103d11SAndy Gospodarek 							     dev->ifindex,
86235103d11SAndy Gospodarek 							     &idev->cnf);
86335103d11SAndy Gospodarek 		}
86435103d11SAndy Gospodarek 	}
86535103d11SAndy Gospodarek }
86635103d11SAndy Gospodarek 
86735103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
86835103d11SAndy Gospodarek {
86935103d11SAndy Gospodarek 	struct net *net;
87035103d11SAndy Gospodarek 	int old;
87135103d11SAndy Gospodarek 
87235103d11SAndy Gospodarek 	if (!rtnl_trylock())
87335103d11SAndy Gospodarek 		return restart_syscall();
87435103d11SAndy Gospodarek 
87535103d11SAndy Gospodarek 	net = (struct net *)table->extra2;
87635103d11SAndy Gospodarek 	old = *p;
87735103d11SAndy Gospodarek 	*p = newf;
87835103d11SAndy Gospodarek 
87935103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
88035103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
88135103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
88285b3daadSDavid Ahern 						     RTM_NEWNETCONF,
88335103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
88435103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_DEFAULT,
88535103d11SAndy Gospodarek 						     net->ipv6.devconf_dflt);
88635103d11SAndy Gospodarek 		rtnl_unlock();
88735103d11SAndy Gospodarek 		return 0;
88835103d11SAndy Gospodarek 	}
88935103d11SAndy Gospodarek 
89035103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
89135103d11SAndy Gospodarek 		net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
89235103d11SAndy Gospodarek 		addrconf_linkdown_change(net, newf);
89335103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
89435103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
89585b3daadSDavid Ahern 						     RTM_NEWNETCONF,
89635103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
89735103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_ALL,
89835103d11SAndy Gospodarek 						     net->ipv6.devconf_all);
89935103d11SAndy Gospodarek 	}
90035103d11SAndy Gospodarek 	rtnl_unlock();
90135103d11SAndy Gospodarek 
90235103d11SAndy Gospodarek 	return 1;
90335103d11SAndy Gospodarek }
90435103d11SAndy Gospodarek 
9051da177e4SLinus Torvalds #endif
9061da177e4SLinus Torvalds 
9075c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
9081da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
9091da177e4SLinus Torvalds {
910c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
911547b792cSIlpo Järvinen 
9121da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
91391df42beSJoe Perches 	pr_debug("%s\n", __func__);
9141da177e4SLinus Torvalds #endif
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
9171da177e4SLinus Torvalds 
918c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
919c15b1ccaSHannes Frederic Sowa 		pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
920c15b1ccaSHannes Frederic Sowa 			  ifp);
9211da177e4SLinus Torvalds 
922e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
923f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
9241da177e4SLinus Torvalds 		return;
9251da177e4SLinus Torvalds 	}
92694e187c0SAmerigo Wang 	ip6_rt_put(ifp->rt);
9271da177e4SLinus Torvalds 
928e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
9291da177e4SLinus Torvalds }
9301da177e4SLinus Torvalds 
931e55ffac6SBrian Haley static void
932e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
933e55ffac6SBrian Haley {
934502a2ffdSstephen hemminger 	struct list_head *p;
9358a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
936e55ffac6SBrian Haley 
937e55ffac6SBrian Haley 	/*
938e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
939e55ffac6SBrian Haley 	 * global before linklocal.
940e55ffac6SBrian Haley 	 */
941502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
942502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
943502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
9448a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
945e55ffac6SBrian Haley 			break;
946e55ffac6SBrian Haley 	}
947e55ffac6SBrian Haley 
9488ef802aaSEric Dumazet 	list_add_tail_rcu(&ifp->if_list, p);
949e55ffac6SBrian Haley }
950e55ffac6SBrian Haley 
951ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr)
9523eb84f49SYOSHIFUJI Hideaki {
953ddbe5032SEric Dumazet 	return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
9543eb84f49SYOSHIFUJI Hideaki }
9553eb84f49SYOSHIFUJI Hideaki 
9561da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds static struct inet6_ifaddr *
9593f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
9603f8f5298SJiri Pirko 	      const struct in6_addr *peer_addr, int pfxlen,
9618a226b2cSJiri Benc 	      int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
9621da177e4SLinus Torvalds {
963df789fe7SDavid Forster 	struct net *net = dev_net(idev->dev);
9641da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
9651da177e4SLinus Torvalds 	struct rt6_info *rt;
9663ad7d246SKrister Johansen 	struct in6_validator_info i6vi;
9673a88a81dSstephen hemminger 	unsigned int hash;
9681da177e4SLinus Torvalds 	int err = 0;
969d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
970d68b8270SYOSHIFUJI Hideaki 
971d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
972d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
973d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
974d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
975d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
9761da177e4SLinus Torvalds 
9778814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
9783ad7d246SKrister Johansen 
9793ad7d246SKrister Johansen 	in6_dev_hold(idev);
9803ad7d246SKrister Johansen 
9811da177e4SLinus Torvalds 	if (idev->dead) {
9821da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
9831da177e4SLinus Torvalds 		goto out2;
9841da177e4SLinus Torvalds 	}
9851da177e4SLinus Torvalds 
98656d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
9879bdd8d40SBrian Haley 		err = -EACCES;
9889bdd8d40SBrian Haley 		goto out2;
9899bdd8d40SBrian Haley 	}
9909bdd8d40SBrian Haley 
9913ad7d246SKrister Johansen 	i6vi.i6vi_addr = *addr;
9923ad7d246SKrister Johansen 	i6vi.i6vi_dev = idev;
9933ad7d246SKrister Johansen 	rcu_read_unlock_bh();
9943ad7d246SKrister Johansen 
9953ad7d246SKrister Johansen 	err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
9963ad7d246SKrister Johansen 
9973ad7d246SKrister Johansen 	rcu_read_lock_bh();
9983ad7d246SKrister Johansen 	err = notifier_to_errno(err);
9993ad7d246SKrister Johansen 	if (err)
10003ad7d246SKrister Johansen 		goto out2;
10013ad7d246SKrister Johansen 
10025c578aedSstephen hemminger 	spin_lock(&addrconf_hash_lock);
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
1005c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
1006ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: already assigned\n");
10071da177e4SLinus Torvalds 		err = -EEXIST;
10081da177e4SLinus Torvalds 		goto out;
10091da177e4SLinus Torvalds 	}
10101da177e4SLinus Torvalds 
1011322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
10121da177e4SLinus Torvalds 
101363159f29SIan Morris 	if (!ifa) {
1014ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: malloc failed\n");
10151da177e4SLinus Torvalds 		err = -ENOBUFS;
10161da177e4SLinus Torvalds 		goto out;
10171da177e4SLinus Torvalds 	}
10181da177e4SLinus Torvalds 
10198f031519SDavid S. Miller 	rt = addrconf_dst_alloc(idev, addr, false);
10201da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
10211da177e4SLinus Torvalds 		err = PTR_ERR(rt);
10221da177e4SLinus Torvalds 		goto out;
10231da177e4SLinus Torvalds 	}
10241da177e4SLinus Torvalds 
1025df789fe7SDavid Forster 	if (net->ipv6.devconf_all->disable_policy ||
1026df789fe7SDavid Forster 	    idev->cnf.disable_policy)
1027df789fe7SDavid Forster 		rt->dst.flags |= DST_NOPOLICY;
1028df789fe7SDavid Forster 
1029bba24896SJiri Pirko 	neigh_parms_data_state_setall(idev->nd_parms);
1030bba24896SJiri Pirko 
10314e3fd7a0SAlexey Dobriyan 	ifa->addr = *addr;
10323f8f5298SJiri Pirko 	if (peer_addr)
10333f8f5298SJiri Pirko 		ifa->peer_addr = *peer_addr;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
1036c15b1ccaSHannes Frederic Sowa 	INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1037c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
10381da177e4SLinus Torvalds 	ifa->scope = scope;
10391da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
104066eb9f86SMahesh Bandewar 	ifa->flags = flags;
104166eb9f86SMahesh Bandewar 	/* No need to add the TENTATIVE flag for addresses with NODAD */
104266eb9f86SMahesh Bandewar 	if (!(flags & IFA_F_NODAD))
104366eb9f86SMahesh Bandewar 		ifa->flags |= IFA_F_TENTATIVE;
10448a226b2cSJiri Benc 	ifa->valid_lft = valid_lft;
10458a226b2cSJiri Benc 	ifa->prefered_lft = prefered_lft;
10461da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
1047617fe29dSDaniel Borkmann 	ifa->tokenized = false;
10481da177e4SLinus Torvalds 
104957f5f544SKeir Fraser 	ifa->rt = rt;
105057f5f544SKeir Fraser 
10511da177e4SLinus Torvalds 	ifa->idev = idev;
10521da177e4SLinus Torvalds 	/* For caller */
1053271201c0SReshetova, Elena 	refcount_set(&ifa->refcnt, 1);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	/* Add to big hash table */
1056ddbe5032SEric Dumazet 	hash = inet6_addr_hash(addr);
10571da177e4SLinus Torvalds 
10585c578aedSstephen hemminger 	hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
10595c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	write_lock(&idev->lock);
10621da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
1063e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
1066372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
10671da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
10681da177e4SLinus Torvalds 	}
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
10711da177e4SLinus Torvalds 	write_unlock(&idev->lock);
10721da177e4SLinus Torvalds out2:
10738814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
10741da177e4SLinus Torvalds 
1075fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
1076f88c91ddSCong Wang 		inet6addr_notifier_call_chain(NETDEV_UP, ifa);
10771da177e4SLinus Torvalds 	else {
10781da177e4SLinus Torvalds 		kfree(ifa);
10793ad7d246SKrister Johansen 		in6_dev_put(idev);
10801da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
10811da177e4SLinus Torvalds 	}
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	return ifa;
10841da177e4SLinus Torvalds out:
10855c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
10861da177e4SLinus Torvalds 	goto out2;
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
10895b84efecSThomas Haller enum cleanup_prefix_rt_t {
10905b84efecSThomas Haller 	CLEANUP_PREFIX_RT_NOP,    /* no cleanup action for prefix route */
10915b84efecSThomas Haller 	CLEANUP_PREFIX_RT_DEL,    /* delete the prefix route */
10925b84efecSThomas Haller 	CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
10935b84efecSThomas Haller };
10945b84efecSThomas Haller 
10955b84efecSThomas Haller /*
10965b84efecSThomas Haller  * Check, whether the prefix for ifp would still need a prefix route
10975b84efecSThomas Haller  * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
10985b84efecSThomas Haller  * constants.
10995b84efecSThomas Haller  *
11005b84efecSThomas Haller  * 1) we don't purge prefix if address was not permanent.
11015b84efecSThomas Haller  *    prefix is managed by its own lifetime.
11025b84efecSThomas Haller  * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
11035b84efecSThomas Haller  * 3) if there are no addresses, delete prefix.
11045b84efecSThomas Haller  * 4) if there are still other permanent address(es),
11055b84efecSThomas Haller  *    corresponding prefix is still permanent.
11065b84efecSThomas Haller  * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
11075b84efecSThomas Haller  *    don't purge the prefix, assume user space is managing it.
11085b84efecSThomas Haller  * 6) otherwise, update prefix lifetime to the
11095b84efecSThomas Haller  *    longest valid lifetime among the corresponding
11105b84efecSThomas Haller  *    addresses on the device.
11115b84efecSThomas Haller  *    Note: subsequent RA will update lifetime.
11125b84efecSThomas Haller  **/
11135b84efecSThomas Haller static enum cleanup_prefix_rt_t
11145b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
11155b84efecSThomas Haller {
11165b84efecSThomas Haller 	struct inet6_ifaddr *ifa;
11175b84efecSThomas Haller 	struct inet6_dev *idev = ifp->idev;
11185b84efecSThomas Haller 	unsigned long lifetime;
11195b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
11205b84efecSThomas Haller 
11215b84efecSThomas Haller 	*expires = jiffies;
11225b84efecSThomas Haller 
11235b84efecSThomas Haller 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
11245b84efecSThomas Haller 		if (ifa == ifp)
11255b84efecSThomas Haller 			continue;
11265b84efecSThomas Haller 		if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
11275b84efecSThomas Haller 				       ifp->prefix_len))
11285b84efecSThomas Haller 			continue;
11295b84efecSThomas Haller 		if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
11305b84efecSThomas Haller 			return CLEANUP_PREFIX_RT_NOP;
11315b84efecSThomas Haller 
11325b84efecSThomas Haller 		action = CLEANUP_PREFIX_RT_EXPIRE;
11335b84efecSThomas Haller 
11345b84efecSThomas Haller 		spin_lock(&ifa->lock);
11355b84efecSThomas Haller 
11365b84efecSThomas Haller 		lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
11375b84efecSThomas Haller 		/*
11385b84efecSThomas Haller 		 * Note: Because this address is
11395b84efecSThomas Haller 		 * not permanent, lifetime <
11405b84efecSThomas Haller 		 * LONG_MAX / HZ here.
11415b84efecSThomas Haller 		 */
11425b84efecSThomas Haller 		if (time_before(*expires, ifa->tstamp + lifetime * HZ))
11435b84efecSThomas Haller 			*expires = ifa->tstamp + lifetime * HZ;
11445b84efecSThomas Haller 		spin_unlock(&ifa->lock);
11455b84efecSThomas Haller 	}
11465b84efecSThomas Haller 
11475b84efecSThomas Haller 	return action;
11485b84efecSThomas Haller }
11495b84efecSThomas Haller 
11505b84efecSThomas Haller static void
11515b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
11525b84efecSThomas Haller {
11535b84efecSThomas Haller 	struct rt6_info *rt;
11545b84efecSThomas Haller 
11555b84efecSThomas Haller 	rt = addrconf_get_prefix_route(&ifp->addr,
11565b84efecSThomas Haller 				       ifp->prefix_len,
11575b84efecSThomas Haller 				       ifp->idev->dev,
11585b84efecSThomas Haller 				       0, RTF_GATEWAY | RTF_DEFAULT);
11595b84efecSThomas Haller 	if (rt) {
11605b84efecSThomas Haller 		if (del_rt)
11615b84efecSThomas Haller 			ip6_del_rt(rt);
11625b84efecSThomas Haller 		else {
11635b84efecSThomas Haller 			if (!(rt->rt6i_flags & RTF_EXPIRES))
11645b84efecSThomas Haller 				rt6_set_expires(rt, expires);
11655b84efecSThomas Haller 			ip6_rt_put(rt);
11665b84efecSThomas Haller 		}
11675b84efecSThomas Haller 	}
11685b84efecSThomas Haller }
11695b84efecSThomas Haller 
11705b84efecSThomas Haller 
11711da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
11741da177e4SLinus Torvalds {
11754c5ff6a6SHerbert Xu 	int state;
11765b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
11775b84efecSThomas Haller 	unsigned long expires;
11781da177e4SLinus Torvalds 
1179c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
1180c15b1ccaSHannes Frederic Sowa 
11818e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
11824c5ff6a6SHerbert Xu 	state = ifp->state;
1183e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
11848e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
11854c5ff6a6SHerbert Xu 
11864c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
11874c5ff6a6SHerbert Xu 		goto out;
11881da177e4SLinus Torvalds 
11895c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
11905c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
11915c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
11921da177e4SLinus Torvalds 
11935b84efecSThomas Haller 	write_lock_bh(&ifp->idev->lock);
11945d9efa7eSDavid S. Miller 
11951da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
1196372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
11971da177e4SLinus Torvalds 		if (ifp->ifpub) {
11981da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
11991da177e4SLinus Torvalds 			ifp->ifpub = NULL;
12001da177e4SLinus Torvalds 		}
12011da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
12021da177e4SLinus Torvalds 	}
12031da177e4SLinus Torvalds 
12045b84efecSThomas Haller 	if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
12055b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &expires);
12065b84efecSThomas Haller 
12078ef802aaSEric Dumazet 	list_del_rcu(&ifp->if_list);
12081da177e4SLinus Torvalds 	__in6_ifa_put(ifp);
1209502a2ffdSstephen hemminger 
12105b84efecSThomas Haller 	write_unlock_bh(&ifp->idev->lock);
12111da177e4SLinus Torvalds 
1212c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
1213b2238566SAndrey Vagin 
12141da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
12151da177e4SLinus Torvalds 
1216f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
12171da177e4SLinus Torvalds 
12185b84efecSThomas Haller 	if (action != CLEANUP_PREFIX_RT_NOP) {
12195b84efecSThomas Haller 		cleanup_prefix_route(ifp, expires,
12205b84efecSThomas Haller 			action == CLEANUP_PREFIX_RT_DEL);
12211da177e4SLinus Torvalds 	}
12221da177e4SLinus Torvalds 
1223c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1224c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
12254c5ff6a6SHerbert Xu out:
12261da177e4SLinus Torvalds 	in6_ifa_put(ifp);
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
12301da177e4SLinus Torvalds {
12311da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
12321da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
123376f793e3SLorenzo Colitti 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1234eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
12351da177e4SLinus Torvalds 	int tmp_plen;
12361da177e4SLinus Torvalds 	int ret = 0;
123795c385b4SNeil Horman 	u32 addr_flags;
123876f793e3SLorenzo Colitti 	unsigned long now = jiffies;
123976506a98SJiri Bohac 	long max_desync_factor;
12407aa8e63fSJiri Bohac 	s32 cnf_temp_preferred_lft;
12411da177e4SLinus Torvalds 
124253bd6749SJiri Pirko 	write_lock_bh(&idev->lock);
12431da177e4SLinus Torvalds 	if (ift) {
12441da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
12451da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
12461da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
12471da177e4SLinus Torvalds 		tmpaddr = &addr;
12481da177e4SLinus Torvalds 	} else {
12491da177e4SLinus Torvalds 		tmpaddr = NULL;
12501da177e4SLinus Torvalds 	}
12511da177e4SLinus Torvalds retry:
12521da177e4SLinus Torvalds 	in6_dev_hold(idev);
12531da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
125453bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1255f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
12561da177e4SLinus Torvalds 		in6_dev_put(idev);
12571da177e4SLinus Torvalds 		ret = -1;
12581da177e4SLinus Torvalds 		goto out;
12591da177e4SLinus Torvalds 	}
12601da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
12611da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
12621da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
12631da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
126453bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1265f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1266f3213831SJoe Perches 			__func__);
12671da177e4SLinus Torvalds 		in6_dev_put(idev);
12681da177e4SLinus Torvalds 		ret = -1;
12691da177e4SLinus Torvalds 		goto out;
12701da177e4SLinus Torvalds 	}
12711da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
12721da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
12739d6280daSJiri Bohac 	ipv6_try_regen_rndid(idev, tmpaddr);
12741da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
127576f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
12761da177e4SLinus Torvalds 
1277eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1278eac55bf9SBenoit Boissinot 			idev->cnf.dad_transmits *
12791f9248e5SJiri Pirko 			NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
128076506a98SJiri Bohac 
128176506a98SJiri Bohac 	/* recalculate max_desync_factor each time and update
128276506a98SJiri Bohac 	 * idev->desync_factor if it's larger
128376506a98SJiri Bohac 	 */
12847aa8e63fSJiri Bohac 	cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
128576506a98SJiri Bohac 	max_desync_factor = min_t(__u32,
128676506a98SJiri Bohac 				  idev->cnf.max_desync_factor,
12877aa8e63fSJiri Bohac 				  cnf_temp_preferred_lft - regen_advance);
128876506a98SJiri Bohac 
128976506a98SJiri Bohac 	if (unlikely(idev->desync_factor > max_desync_factor)) {
129076506a98SJiri Bohac 		if (max_desync_factor > 0) {
129176506a98SJiri Bohac 			get_random_bytes(&idev->desync_factor,
129276506a98SJiri Bohac 					 sizeof(idev->desync_factor));
129376506a98SJiri Bohac 			idev->desync_factor %= max_desync_factor;
129476506a98SJiri Bohac 		} else {
129576506a98SJiri Bohac 			idev->desync_factor = 0;
129676506a98SJiri Bohac 		}
129776506a98SJiri Bohac 	}
129876506a98SJiri Bohac 
129976506a98SJiri Bohac 	tmp_valid_lft = min_t(__u32,
130076506a98SJiri Bohac 			      ifp->valid_lft,
130176506a98SJiri Bohac 			      idev->cnf.temp_valid_lft + age);
13027aa8e63fSJiri Bohac 	tmp_prefered_lft = cnf_temp_preferred_lft + age -
130376506a98SJiri Bohac 			    idev->desync_factor;
130476506a98SJiri Bohac 	tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
130576506a98SJiri Bohac 	tmp_plen = ifp->prefix_len;
130676506a98SJiri Bohac 	tmp_tstamp = ifp->tstamp;
130776506a98SJiri Bohac 	spin_unlock_bh(&ifp->lock);
130876506a98SJiri Bohac 
130953bd6749SJiri Pirko 	write_unlock_bh(&idev->lock);
131095c385b4SNeil Horman 
1311eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1312eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1313eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1314eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1315ecab6701SHeiner Kallweit 	 * Use age calculation as in addrconf_verify to avoid unnecessary
1316ecab6701SHeiner Kallweit 	 * temporary addresses being generated.
1317eac55bf9SBenoit Boissinot 	 */
1318ecab6701SHeiner Kallweit 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1319ecab6701SHeiner Kallweit 	if (tmp_prefered_lft <= regen_advance + age) {
1320eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1321eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1322eac55bf9SBenoit Boissinot 		ret = -1;
1323eac55bf9SBenoit Boissinot 		goto out;
1324eac55bf9SBenoit Boissinot 	}
1325eac55bf9SBenoit Boissinot 
132695c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
132795c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
132895c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
132995c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
133095c385b4SNeil Horman 
13314b08a8f1SHannes Frederic Sowa 	ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
13328a226b2cSJiri Benc 			    ipv6_addr_scope(&addr), addr_flags,
13334b08a8f1SHannes Frederic Sowa 			    tmp_valid_lft, tmp_prefered_lft);
13344b08a8f1SHannes Frederic Sowa 	if (IS_ERR(ift)) {
13351da177e4SLinus Torvalds 		in6_ifa_put(ifp);
13361da177e4SLinus Torvalds 		in6_dev_put(idev);
1337f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
13381da177e4SLinus Torvalds 		tmpaddr = &addr;
133953bd6749SJiri Pirko 		write_lock_bh(&idev->lock);
13401da177e4SLinus Torvalds 		goto retry;
13411da177e4SLinus Torvalds 	}
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
13441da177e4SLinus Torvalds 	ift->ifpub = ifp;
134576f793e3SLorenzo Colitti 	ift->cstamp = now;
13461da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
13471da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
13481da177e4SLinus Torvalds 
1349cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
13501da177e4SLinus Torvalds 	in6_ifa_put(ift);
13511da177e4SLinus Torvalds 	in6_dev_put(idev);
13521da177e4SLinus Torvalds out:
13531da177e4SLinus Torvalds 	return ret;
13541da177e4SLinus Torvalds }
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds /*
1357072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
13581da177e4SLinus Torvalds  */
1359a9b05723SYOSHIFUJI Hideaki enum {
1360a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1361a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1362a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1363a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1364a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1365a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1366a9b05723SYOSHIFUJI Hideaki #endif
1367a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1368a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1369a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1370a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1371a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
13727fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
13737fd2561eSErik Kline 	IPV6_SADDR_RULE_NOT_OPTIMISTIC,
13747fd2561eSErik Kline #endif
1375a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1376072047e4SYOSHIFUJI Hideaki };
1377072047e4SYOSHIFUJI Hideaki 
1378a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1379a9b05723SYOSHIFUJI Hideaki 	int			rule;
1380a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1381a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1382a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1383a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1384a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1385a9b05723SYOSHIFUJI Hideaki };
1386a9b05723SYOSHIFUJI Hideaki 
1387a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
13889acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1389a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1390a9b05723SYOSHIFUJI Hideaki 	int scope;
1391a9b05723SYOSHIFUJI Hideaki 	int label;
13927cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1393a9b05723SYOSHIFUJI Hideaki };
1394072047e4SYOSHIFUJI Hideaki 
1395b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
13961da177e4SLinus Torvalds {
139745bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1398072047e4SYOSHIFUJI Hideaki 		return 1;
1399072047e4SYOSHIFUJI Hideaki 	return 0;
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
140235e015e1SMatteo Croce static bool ipv6_use_optimistic_addr(struct net *net,
140335e015e1SMatteo Croce 				     struct inet6_dev *idev)
14047fd2561eSErik Kline {
14057fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
140635e015e1SMatteo Croce 	if (!idev)
140735e015e1SMatteo Croce 		return false;
140835e015e1SMatteo Croce 	if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
140935e015e1SMatteo Croce 		return false;
141035e015e1SMatteo Croce 	if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
141135e015e1SMatteo Croce 		return false;
141235e015e1SMatteo Croce 
141335e015e1SMatteo Croce 	return true;
14147fd2561eSErik Kline #else
14157fd2561eSErik Kline 	return false;
14167fd2561eSErik Kline #endif
14177fd2561eSErik Kline }
14187fd2561eSErik Kline 
14193de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
14203de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1421a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1422a9b05723SYOSHIFUJI Hideaki 			       int i)
1423a9b05723SYOSHIFUJI Hideaki {
1424a9b05723SYOSHIFUJI Hideaki 	int ret;
1425a9b05723SYOSHIFUJI Hideaki 
1426a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1427a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1428a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1429a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1430a9b05723SYOSHIFUJI Hideaki 			break;
1431a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1432a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1433a9b05723SYOSHIFUJI Hideaki 			break;
1434a9b05723SYOSHIFUJI Hideaki 		default:
1435a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1436a9b05723SYOSHIFUJI Hideaki 		}
1437a9b05723SYOSHIFUJI Hideaki 		goto out;
1438a9b05723SYOSHIFUJI Hideaki 	}
1439a9b05723SYOSHIFUJI Hideaki 
1440a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1441a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1442a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1443a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1444a9b05723SYOSHIFUJI Hideaki 		break;
1445a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1446a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1447a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1448a9b05723SYOSHIFUJI Hideaki 		break;
1449a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1450a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1451a9b05723SYOSHIFUJI Hideaki 		 *
1452a9b05723SYOSHIFUJI Hideaki 		 *      ret
1453a9b05723SYOSHIFUJI Hideaki 		 *       ^
1454a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1455a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1456a9b05723SYOSHIFUJI Hideaki 		 *       |
1457a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1458a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1459a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1460db9c7c39Sstephen hemminger 		 *  B-15 |    \        if scope is enough for destination.
1461a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1462a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1463a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1464a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1465a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1466a9b05723SYOSHIFUJI Hideaki 		 *
1467a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1468a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1469a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1470a9b05723SYOSHIFUJI Hideaki 		 */
1471a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1472a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1473a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1474a9b05723SYOSHIFUJI Hideaki 		else
1475a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1476a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1477a9b05723SYOSHIFUJI Hideaki 		break;
1478a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
14797fd2561eSErik Kline 	    {
1480a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
14817fd2561eSErik Kline 		u8 avoid = IFA_F_DEPRECATED;
14827fd2561eSErik Kline 
148335e015e1SMatteo Croce 		if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
14847fd2561eSErik Kline 			avoid |= IFA_F_OPTIMISTIC;
1485a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
14867fd2561eSErik Kline 		      !(score->ifa->flags & avoid);
1487a9b05723SYOSHIFUJI Hideaki 		break;
14887fd2561eSErik Kline 	    }
1489a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1490a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
14917cbca67cSYOSHIFUJI Hideaki 	    {
1492a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
14937cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
14947cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1495a9b05723SYOSHIFUJI Hideaki 		break;
14967cbca67cSYOSHIFUJI Hideaki 	    }
1497a9b05723SYOSHIFUJI Hideaki #endif
1498a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1499a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1500a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1501a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1502a9b05723SYOSHIFUJI Hideaki 		break;
1503a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1504a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
15053de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
15063de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1507a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1508a9b05723SYOSHIFUJI Hideaki 		break;
1509a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
15107cbca67cSYOSHIFUJI Hideaki 	    {
1511a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
151225985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1513a9b05723SYOSHIFUJI Hideaki 		 */
15147cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
15157cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
15167cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
15177cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1518a9b05723SYOSHIFUJI Hideaki 		break;
15197cbca67cSYOSHIFUJI Hideaki 	    }
1520a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1521a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1522a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1523a9b05723SYOSHIFUJI Hideaki 		 */
1524a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1525a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1526a9b05723SYOSHIFUJI Hideaki 		break;
1527a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1528a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
152991b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
153091b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
153191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
153291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1533a9b05723SYOSHIFUJI Hideaki 		break;
15347fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
15357fd2561eSErik Kline 	case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
15367fd2561eSErik Kline 		/* Optimistic addresses still have lower precedence than other
15377fd2561eSErik Kline 		 * preferred addresses.
15387fd2561eSErik Kline 		 */
15397fd2561eSErik Kline 		ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
15407fd2561eSErik Kline 		break;
15417fd2561eSErik Kline #endif
1542a9b05723SYOSHIFUJI Hideaki 	default:
1543a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1544a9b05723SYOSHIFUJI Hideaki 	}
1545a9b05723SYOSHIFUJI Hideaki 
1546a9b05723SYOSHIFUJI Hideaki 	if (ret)
1547a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1548a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1549a9b05723SYOSHIFUJI Hideaki out:
1550a9b05723SYOSHIFUJI Hideaki 	return ret;
1551a9b05723SYOSHIFUJI Hideaki }
1552a9b05723SYOSHIFUJI Hideaki 
1553c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net,
15549131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_dst *dst,
15559131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct inet6_dev *idev,
1556c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_score *scores,
1557c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				int hiscore_idx)
15581da177e4SLinus Torvalds {
1559c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1560072047e4SYOSHIFUJI Hideaki 
15611da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1562502a2ffdSstephen hemminger 	list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1563a9b05723SYOSHIFUJI Hideaki 		int i;
1564072047e4SYOSHIFUJI Hideaki 
1565a9b05723SYOSHIFUJI Hideaki 		/*
15666b3ae80aSYOSHIFUJI Hideaki 		 * - Tentative Address (RFC2462 section 5.4)
15676b3ae80aSYOSHIFUJI Hideaki 		 *  - A tentative address is not considered
15686b3ae80aSYOSHIFUJI Hideaki 		 *    "assigned to an interface" in the traditional
156995c385b4SNeil Horman 		 *    sense, unless it is also flagged as optimistic.
15706b3ae80aSYOSHIFUJI Hideaki 		 * - Candidate Source Address (section 4)
1571072047e4SYOSHIFUJI Hideaki 		 *  - In any case, anycast addresses, multicast
1572072047e4SYOSHIFUJI Hideaki 		 *    addresses, and the unspecified address MUST
1573072047e4SYOSHIFUJI Hideaki 		 *    NOT be included in a candidate set.
1574072047e4SYOSHIFUJI Hideaki 		 */
1575a9b05723SYOSHIFUJI Hideaki 		if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1576a9b05723SYOSHIFUJI Hideaki 		    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
15776b3ae80aSYOSHIFUJI Hideaki 			continue;
1578a9b05723SYOSHIFUJI Hideaki 
1579a9b05723SYOSHIFUJI Hideaki 		score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1580a9b05723SYOSHIFUJI Hideaki 
1581a9b05723SYOSHIFUJI Hideaki 		if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1582a9b05723SYOSHIFUJI Hideaki 			     score->addr_type & IPV6_ADDR_MULTICAST)) {
1583ba7a46f1SJoe Perches 			net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
15849131f3deSYOSHIFUJI Hideaki/吉藤英明 					    idev->dev->name);
15851da177e4SLinus Torvalds 			continue;
1586072047e4SYOSHIFUJI Hideaki 		}
1587072047e4SYOSHIFUJI Hideaki 
1588a9b05723SYOSHIFUJI Hideaki 		score->rule = -1;
1589a9b05723SYOSHIFUJI Hideaki 		bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1590072047e4SYOSHIFUJI Hideaki 
1591a9b05723SYOSHIFUJI Hideaki 		for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1592a9b05723SYOSHIFUJI Hideaki 			int minihiscore, miniscore;
1593072047e4SYOSHIFUJI Hideaki 
15949131f3deSYOSHIFUJI Hideaki/吉藤英明 			minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
15959131f3deSYOSHIFUJI Hideaki/吉藤英明 			miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1596072047e4SYOSHIFUJI Hideaki 
1597a9b05723SYOSHIFUJI Hideaki 			if (minihiscore > miniscore) {
1598a9b05723SYOSHIFUJI Hideaki 				if (i == IPV6_SADDR_RULE_SCOPE &&
1599a9b05723SYOSHIFUJI Hideaki 				    score->scopedist > 0) {
1600a9b05723SYOSHIFUJI Hideaki 					/*
1601a9b05723SYOSHIFUJI Hideaki 					 * special case:
1602a9b05723SYOSHIFUJI Hideaki 					 * each remaining entry
1603a9b05723SYOSHIFUJI Hideaki 					 * has too small (not enough)
1604a9b05723SYOSHIFUJI Hideaki 					 * scope, because ifa entries
1605a9b05723SYOSHIFUJI Hideaki 					 * are sorted by their scope
1606a9b05723SYOSHIFUJI Hideaki 					 * values.
1607072047e4SYOSHIFUJI Hideaki 					 */
16089131f3deSYOSHIFUJI Hideaki/吉藤英明 					goto out;
1609072047e4SYOSHIFUJI Hideaki 				}
1610a9b05723SYOSHIFUJI Hideaki 				break;
1611a9b05723SYOSHIFUJI Hideaki 			} else if (minihiscore < miniscore) {
1612a9b05723SYOSHIFUJI Hideaki 				if (hiscore->ifa)
1613a9b05723SYOSHIFUJI Hideaki 					in6_ifa_put(hiscore->ifa);
161499cd07a5SJuha-Matti Tapio 
1615a9b05723SYOSHIFUJI Hideaki 				in6_ifa_hold(score->ifa);
1616072047e4SYOSHIFUJI Hideaki 
1617a0bffffcSIlpo Järvinen 				swap(hiscore, score);
1618c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				hiscore_idx = 1 - hiscore_idx;
1619a9b05723SYOSHIFUJI Hideaki 
1620a9b05723SYOSHIFUJI Hideaki 				/* restore our iterator */
1621a9b05723SYOSHIFUJI Hideaki 				score->ifa = hiscore->ifa;
1622a9b05723SYOSHIFUJI Hideaki 
1623a9b05723SYOSHIFUJI Hideaki 				break;
16241da177e4SLinus Torvalds 			}
1625a9b05723SYOSHIFUJI Hideaki 		}
1626a9b05723SYOSHIFUJI Hideaki 	}
16279131f3deSYOSHIFUJI Hideaki/吉藤英明 out:
16281da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
1629c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	return hiscore_idx;
16301da177e4SLinus Torvalds }
16319131f3deSYOSHIFUJI Hideaki/吉藤英明 
1632afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net,
1633afbac601SDavid Ahern 				 const struct net_device *dst_dev,
1634afbac601SDavid Ahern 				 const struct net_device *master,
1635afbac601SDavid Ahern 				 struct ipv6_saddr_dst *dst,
1636afbac601SDavid Ahern 				 struct ipv6_saddr_score *scores,
1637afbac601SDavid Ahern 				 int hiscore_idx)
1638afbac601SDavid Ahern {
1639afbac601SDavid Ahern 	struct inet6_dev *idev;
1640afbac601SDavid Ahern 
1641afbac601SDavid Ahern 	idev = __in6_dev_get(dst_dev);
1642afbac601SDavid Ahern 	if (idev)
1643afbac601SDavid Ahern 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1644afbac601SDavid Ahern 						   scores, hiscore_idx);
1645afbac601SDavid Ahern 
1646afbac601SDavid Ahern 	idev = __in6_dev_get(master);
1647afbac601SDavid Ahern 	if (idev)
1648afbac601SDavid Ahern 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1649afbac601SDavid Ahern 						   scores, hiscore_idx);
1650afbac601SDavid Ahern 
1651afbac601SDavid Ahern 	return hiscore_idx;
1652afbac601SDavid Ahern }
1653afbac601SDavid Ahern 
16549131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
16559131f3deSYOSHIFUJI Hideaki/吉藤英明 		       const struct in6_addr *daddr, unsigned int prefs,
16569131f3deSYOSHIFUJI Hideaki/吉藤英明 		       struct in6_addr *saddr)
16579131f3deSYOSHIFUJI Hideaki/吉藤英明 {
1658c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score scores[2], *hiscore;
16599131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_dst dst;
16609131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct inet6_dev *idev;
16619131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct net_device *dev;
16629131f3deSYOSHIFUJI Hideaki/吉藤英明 	int dst_type;
16639131f3deSYOSHIFUJI Hideaki/吉藤英明 	bool use_oif_addr = false;
1664c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	int hiscore_idx = 0;
16659131f3deSYOSHIFUJI Hideaki/吉藤英明 
16669131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst_type = __ipv6_addr_type(daddr);
16679131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.addr = daddr;
16689131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
16699131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.scope = __ipv6_addr_src_scope(dst_type);
16709131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
16719131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.prefs = prefs;
16729131f3deSYOSHIFUJI Hideaki/吉藤英明 
1673c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].rule = -1;
1674c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].ifa = NULL;
16759131f3deSYOSHIFUJI Hideaki/吉藤英明 
16769131f3deSYOSHIFUJI Hideaki/吉藤英明 	rcu_read_lock();
16779131f3deSYOSHIFUJI Hideaki/吉藤英明 
16789131f3deSYOSHIFUJI Hideaki/吉藤英明 	/* Candidate Source Address (section 4)
16799131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *  - multicast and link-local destination address,
16809131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    the set of candidate source address MUST only
16819131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
16829131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same link as the outgoing
16839131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.
16849131f3deSYOSHIFUJI Hideaki/吉藤英明 	 * (- For site-local destination addresses, the
16859131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    set of candidate source addresses MUST only
16869131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
16879131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same site as the outgoing
16889131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.)
16893985e8a3SErik Kline 	 *  - "It is RECOMMENDED that the candidate source addresses
16903985e8a3SErik Kline 	 *    be the set of unicast addresses assigned to the
16913985e8a3SErik Kline 	 *    interface that will be used to send to the destination
16923985e8a3SErik Kline 	 *    (the 'outgoing' interface)." (RFC 6724)
16939131f3deSYOSHIFUJI Hideaki/吉藤英明 	 */
16949131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (dst_dev) {
16959131f3deSYOSHIFUJI Hideaki/吉藤英明 		idev = __in6_dev_get(dst_dev);
16963985e8a3SErik Kline 		if ((dst_type & IPV6_ADDR_MULTICAST) ||
16973985e8a3SErik Kline 		    dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
16983985e8a3SErik Kline 		    (idev && idev->cnf.use_oif_addrs_only)) {
16999131f3deSYOSHIFUJI Hideaki/吉藤英明 			use_oif_addr = true;
17009131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
17019131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
17029131f3deSYOSHIFUJI Hideaki/吉藤英明 
17039131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (use_oif_addr) {
1704c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 		if (idev)
1705c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
17069131f3deSYOSHIFUJI Hideaki/吉藤英明 	} else {
1707afbac601SDavid Ahern 		const struct net_device *master;
1708afbac601SDavid Ahern 		int master_idx = 0;
1709afbac601SDavid Ahern 
1710afbac601SDavid Ahern 		/* if dst_dev exists and is enslaved to an L3 device, then
1711afbac601SDavid Ahern 		 * prefer addresses from dst_dev and then the master over
1712afbac601SDavid Ahern 		 * any other enslaved devices in the L3 domain.
1713afbac601SDavid Ahern 		 */
1714afbac601SDavid Ahern 		master = l3mdev_master_dev_rcu(dst_dev);
1715afbac601SDavid Ahern 		if (master) {
1716afbac601SDavid Ahern 			master_idx = master->ifindex;
1717afbac601SDavid Ahern 
1718afbac601SDavid Ahern 			hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1719afbac601SDavid Ahern 							    master, &dst,
1720afbac601SDavid Ahern 							    scores, hiscore_idx);
1721afbac601SDavid Ahern 
1722afbac601SDavid Ahern 			if (scores[hiscore_idx].ifa)
1723afbac601SDavid Ahern 				goto out;
1724afbac601SDavid Ahern 		}
1725afbac601SDavid Ahern 
17269131f3deSYOSHIFUJI Hideaki/吉藤英明 		for_each_netdev_rcu(net, dev) {
1727afbac601SDavid Ahern 			/* only consider addresses on devices in the
1728afbac601SDavid Ahern 			 * same L3 domain
1729afbac601SDavid Ahern 			 */
1730afbac601SDavid Ahern 			if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1731afbac601SDavid Ahern 				continue;
17329131f3deSYOSHIFUJI Hideaki/吉藤英明 			idev = __in6_dev_get(dev);
17339131f3deSYOSHIFUJI Hideaki/吉藤英明 			if (!idev)
17349131f3deSYOSHIFUJI Hideaki/吉藤英明 				continue;
1735c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
17369131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
17379131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
1738afbac601SDavid Ahern 
1739afbac601SDavid Ahern out:
17408814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
17411da177e4SLinus Torvalds 
1742c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	hiscore = &scores[hiscore_idx];
1743a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1744072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
17451da177e4SLinus Torvalds 
17464e3fd7a0SAlexey Dobriyan 	*saddr = hiscore->ifa->addr;
1747a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1748072047e4SYOSHIFUJI Hideaki 	return 0;
17491da177e4SLinus Torvalds }
17505e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
17511da177e4SLinus Torvalds 
17528965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1753479840ffSJiri Pirko 		      u32 banned_flags)
1754b7b1bfceSHannes Frederic Sowa {
1755b7b1bfceSHannes Frederic Sowa 	struct inet6_ifaddr *ifp;
1756b7b1bfceSHannes Frederic Sowa 	int err = -EADDRNOTAVAIL;
1757b7b1bfceSHannes Frederic Sowa 
1758602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1759602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1760602582caSHannes Frederic Sowa 			break;
1761b7b1bfceSHannes Frederic Sowa 		if (ifp->scope == IFA_LINK &&
1762b7b1bfceSHannes Frederic Sowa 		    !(ifp->flags & banned_flags)) {
1763b7b1bfceSHannes Frederic Sowa 			*addr = ifp->addr;
1764b7b1bfceSHannes Frederic Sowa 			err = 0;
1765b7b1bfceSHannes Frederic Sowa 			break;
1766b7b1bfceSHannes Frederic Sowa 		}
1767b7b1bfceSHannes Frederic Sowa 	}
1768b7b1bfceSHannes Frederic Sowa 	return err;
1769b7b1bfceSHannes Frederic Sowa }
1770b7b1bfceSHannes Frederic Sowa 
177195c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1772479840ffSJiri Pirko 		    u32 banned_flags)
17731da177e4SLinus Torvalds {
17741da177e4SLinus Torvalds 	struct inet6_dev *idev;
17751da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
17761da177e4SLinus Torvalds 
17778814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1778e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1779e21e8467SStephen Hemminger 	if (idev) {
17801da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1781b7b1bfceSHannes Frederic Sowa 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
17821da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
17831da177e4SLinus Torvalds 	}
17848814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
17851da177e4SLinus Torvalds 	return err;
17861da177e4SLinus Torvalds }
17871da177e4SLinus Torvalds 
1788d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev)
17891da177e4SLinus Torvalds {
1790d9bf82c2SEric Dumazet 	const struct inet6_ifaddr *ifp;
17911da177e4SLinus Torvalds 	int cnt = 0;
17921da177e4SLinus Torvalds 
1793d9bf82c2SEric Dumazet 	rcu_read_lock();
1794d9bf82c2SEric Dumazet 	list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
17951da177e4SLinus Torvalds 		cnt++;
1796d9bf82c2SEric Dumazet 	rcu_read_unlock();
17971da177e4SLinus Torvalds 	return cnt;
17981da177e4SLinus Torvalds }
17991da177e4SLinus Torvalds 
1800b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
18012a7851bfSFlorian Westphal 		  const struct net_device *dev, int strict)
18021da177e4SLinus Torvalds {
1803c58da4c6SErik Kline 	return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1804c58da4c6SErik Kline }
1805c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr);
1806c58da4c6SErik Kline 
1807c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1808c58da4c6SErik Kline 			    const struct net_device *dev, int strict,
1809c58da4c6SErik Kline 			    u32 banned_flags)
1810c58da4c6SErik Kline {
1811eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1812ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
1813c58da4c6SErik Kline 	u32 ifp_flags;
18141da177e4SLinus Torvalds 
18155c578aedSstephen hemminger 	rcu_read_lock_bh();
1816b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1817878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1818bfeade08SDaniel Lezcano 			continue;
1819c58da4c6SErik Kline 		/* Decouple optimistic from tentative for evaluation here.
1820c58da4c6SErik Kline 		 * Ban optimistic addresses explicitly, when required.
1821c58da4c6SErik Kline 		 */
1822c58da4c6SErik Kline 		ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1823c58da4c6SErik Kline 			    ? (ifp->flags&~IFA_F_TENTATIVE)
1824c58da4c6SErik Kline 			    : ifp->flags;
18251da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1826c58da4c6SErik Kline 		    !(ifp_flags&banned_flags) &&
182763159f29SIan Morris 		    (!dev || ifp->idev->dev == dev ||
1828eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
18295c578aedSstephen hemminger 			rcu_read_unlock_bh();
1830eedf042aSStephen Hemminger 			return 1;
1831eedf042aSStephen Hemminger 		}
1832eedf042aSStephen Hemminger 	}
18335c578aedSstephen hemminger 
1834eedf042aSStephen Hemminger 	rcu_read_unlock_bh();
1835eedf042aSStephen Hemminger 	return 0;
18361da177e4SLinus Torvalds }
1837c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
18387159039aSYOSHIFUJI Hideaki 
18393e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
184006bfe655SDaniel Lezcano 			       struct net_device *dev)
18411da177e4SLinus Torvalds {
1842ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
18431da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
18441da177e4SLinus Torvalds 
1845b67bfe0dSSasha Levin 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1846878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
184706bfe655SDaniel Lezcano 			continue;
18481da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
184963159f29SIan Morris 			if (!dev || ifp->idev->dev == dev)
18503e81c6daSDavid S. Miller 				return true;
18511da177e4SLinus Torvalds 		}
18521da177e4SLinus Torvalds 	}
18533e81c6daSDavid S. Miller 	return false;
18541da177e4SLinus Torvalds }
18551da177e4SLinus Torvalds 
18567df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev.
18577df37ff3SCatalin\(ux\) M. BOIE  * If one is found it returns true.
18587df37ff3SCatalin\(ux\) M. BOIE  */
18597df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
18607df37ff3SCatalin\(ux\) M. BOIE 	const unsigned int prefix_len, struct net_device *dev)
18617df37ff3SCatalin\(ux\) M. BOIE {
1862*47e26941SEric Dumazet 	const struct inet6_ifaddr *ifa;
1863*47e26941SEric Dumazet 	const struct inet6_dev *idev;
18647df37ff3SCatalin\(ux\) M. BOIE 	bool ret = false;
18657df37ff3SCatalin\(ux\) M. BOIE 
18667df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_lock();
18677df37ff3SCatalin\(ux\) M. BOIE 	idev = __in6_dev_get(dev);
18687df37ff3SCatalin\(ux\) M. BOIE 	if (idev) {
1869*47e26941SEric Dumazet 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
18707df37ff3SCatalin\(ux\) M. BOIE 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
18717df37ff3SCatalin\(ux\) M. BOIE 			if (ret)
18727df37ff3SCatalin\(ux\) M. BOIE 				break;
18737df37ff3SCatalin\(ux\) M. BOIE 		}
18747df37ff3SCatalin\(ux\) M. BOIE 	}
18757df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_unlock();
18767df37ff3SCatalin\(ux\) M. BOIE 
18777df37ff3SCatalin\(ux\) M. BOIE 	return ret;
18787df37ff3SCatalin\(ux\) M. BOIE }
18797df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix);
18807df37ff3SCatalin\(ux\) M. BOIE 
1881b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
188252eeeb84SYOSHIFUJI Hideaki {
188352eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
188452eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
188552eeeb84SYOSHIFUJI Hideaki 	int	onlink;
188652eeeb84SYOSHIFUJI Hideaki 
188752eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
188852eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
188952eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
189052eeeb84SYOSHIFUJI Hideaki 	if (idev) {
189152eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
1892502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
189352eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
189452eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
189552eeeb84SYOSHIFUJI Hideaki 			if (onlink)
189652eeeb84SYOSHIFUJI Hideaki 				break;
189752eeeb84SYOSHIFUJI Hideaki 		}
189852eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
189952eeeb84SYOSHIFUJI Hideaki 	}
190052eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
190152eeeb84SYOSHIFUJI Hideaki 	return onlink;
190252eeeb84SYOSHIFUJI Hideaki }
190352eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
190452eeeb84SYOSHIFUJI Hideaki 
19059acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
19061cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
19071da177e4SLinus Torvalds {
1908b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
1909ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
19101da177e4SLinus Torvalds 
19115c578aedSstephen hemminger 	rcu_read_lock_bh();
1912b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1913878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
19141cab3da6SDaniel Lezcano 			continue;
19151da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
191663159f29SIan Morris 			if (!dev || ifp->idev->dev == dev ||
19171da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1918b79d1d54SDavid S. Miller 				result = ifp;
19191da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
19201da177e4SLinus Torvalds 				break;
19211da177e4SLinus Torvalds 			}
19221da177e4SLinus Torvalds 		}
19231da177e4SLinus Torvalds 	}
19245c578aedSstephen hemminger 	rcu_read_unlock_bh();
19251da177e4SLinus Torvalds 
1926b79d1d54SDavid S. Miller 	return result;
19271da177e4SLinus Torvalds }
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
19301da177e4SLinus Torvalds 
1931cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
19321da177e4SLinus Torvalds {
19333d171f39SLubomir Rintel 	if (dad_failed)
19343d171f39SLubomir Rintel 		ifp->flags |= IFA_F_DADFAILED;
19353d171f39SLubomir Rintel 
1936ec8add2aSSabrina Dubroca 	if (ifp->flags&IFA_F_TEMPORARY) {
19371da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
19381da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
19391da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
19401da177e4SLinus Torvalds 		if (ifpub) {
19411da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
19421da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
19431da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
19441da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
19451da177e4SLinus Torvalds 		} else {
19461da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
19471da177e4SLinus Torvalds 		}
19481da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
1949ec8add2aSSabrina Dubroca 	} else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
1950ec8add2aSSabrina Dubroca 		spin_lock_bh(&ifp->lock);
1951ec8add2aSSabrina Dubroca 		addrconf_del_dad_work(ifp);
1952ec8add2aSSabrina Dubroca 		ifp->flags |= IFA_F_TENTATIVE;
1953ec8add2aSSabrina Dubroca 		spin_unlock_bh(&ifp->lock);
1954ec8add2aSSabrina Dubroca 		if (dad_failed)
1955ec8add2aSSabrina Dubroca 			ipv6_ifa_notify(0, ifp);
1956ec8add2aSSabrina Dubroca 		in6_ifa_put(ifp);
1957c15b1ccaSHannes Frederic Sowa 	} else {
19581da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
19591da177e4SLinus Torvalds 	}
1960c15b1ccaSHannes Frederic Sowa }
19611da177e4SLinus Torvalds 
1962f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1963f2344a13SHerbert Xu {
1964f2344a13SHerbert Xu 	int err = -ENOENT;
1965f2344a13SHerbert Xu 
19668e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
1967f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
1968f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
1969f2344a13SHerbert Xu 		err = 0;
1970f2344a13SHerbert Xu 	}
19718e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
1972f2344a13SHerbert Xu 
1973f2344a13SHerbert Xu 	return err;
1974f2344a13SHerbert Xu }
1975f2344a13SHerbert Xu 
19763c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
19773c21edbdSYOSHIFUJI Hideaki {
19781b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
19791855b7c3SHannes Frederic Sowa 	struct net *net = dev_net(ifp->idev->dev);
19809bdd8d40SBrian Haley 
1981853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
1982853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
1983f2344a13SHerbert Xu 		return;
1984853dc2e0SUrsula Braun 	}
1985f2344a13SHerbert Xu 
1986e87cc472SJoe Perches 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
19870522fea6SJens Rosenboom 			     ifp->idev->dev->name, &ifp->addr);
19889bdd8d40SBrian Haley 
19895f40ef77SHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
19901b34be74SYOSHIFUJI Hideaki 
19915f40ef77SHannes Frederic Sowa 	if (ifp->flags & IFA_F_STABLE_PRIVACY) {
19925f40ef77SHannes Frederic Sowa 		int scope = ifp->scope;
19935f40ef77SHannes Frederic Sowa 		u32 flags = ifp->flags;
19945f40ef77SHannes Frederic Sowa 		struct in6_addr new_addr;
19955f40ef77SHannes Frederic Sowa 		struct inet6_ifaddr *ifp2;
19965f40ef77SHannes Frederic Sowa 		u32 valid_lft, preferred_lft;
19975f40ef77SHannes Frederic Sowa 		int pfxlen = ifp->prefix_len;
19985f40ef77SHannes Frederic Sowa 		int retries = ifp->stable_privacy_retry + 1;
19995f40ef77SHannes Frederic Sowa 
20001855b7c3SHannes Frederic Sowa 		if (retries > net->ipv6.sysctl.idgen_retries) {
20015f40ef77SHannes Frederic Sowa 			net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
20025f40ef77SHannes Frederic Sowa 					     ifp->idev->dev->name);
20035f40ef77SHannes Frederic Sowa 			goto errdad;
20045f40ef77SHannes Frederic Sowa 		}
20055f40ef77SHannes Frederic Sowa 
20065f40ef77SHannes Frederic Sowa 		new_addr = ifp->addr;
20075f40ef77SHannes Frederic Sowa 		if (ipv6_generate_stable_address(&new_addr, retries,
20085f40ef77SHannes Frederic Sowa 						 idev))
20095f40ef77SHannes Frederic Sowa 			goto errdad;
20105f40ef77SHannes Frederic Sowa 
20115f40ef77SHannes Frederic Sowa 		valid_lft = ifp->valid_lft;
20125f40ef77SHannes Frederic Sowa 		preferred_lft = ifp->prefered_lft;
20135f40ef77SHannes Frederic Sowa 
20145f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp->lock);
20155f40ef77SHannes Frederic Sowa 
20165f40ef77SHannes Frederic Sowa 		if (idev->cnf.max_addresses &&
20175f40ef77SHannes Frederic Sowa 		    ipv6_count_addresses(idev) >=
20185f40ef77SHannes Frederic Sowa 		    idev->cnf.max_addresses)
20195f40ef77SHannes Frederic Sowa 			goto lock_errdad;
20205f40ef77SHannes Frederic Sowa 
20215f40ef77SHannes Frederic Sowa 		net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
20225f40ef77SHannes Frederic Sowa 				     ifp->idev->dev->name);
20235f40ef77SHannes Frederic Sowa 
20245f40ef77SHannes Frederic Sowa 		ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
20255f40ef77SHannes Frederic Sowa 				     scope, flags, valid_lft,
20265f40ef77SHannes Frederic Sowa 				     preferred_lft);
20275f40ef77SHannes Frederic Sowa 		if (IS_ERR(ifp2))
20285f40ef77SHannes Frederic Sowa 			goto lock_errdad;
20295f40ef77SHannes Frederic Sowa 
20305f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp2->lock);
20315f40ef77SHannes Frederic Sowa 		ifp2->stable_privacy_retry = retries;
20325f40ef77SHannes Frederic Sowa 		ifp2->state = INET6_IFADDR_STATE_PREDAD;
20335f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp2->lock);
20345f40ef77SHannes Frederic Sowa 
20351855b7c3SHannes Frederic Sowa 		addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
20365f40ef77SHannes Frederic Sowa 		in6_ifa_put(ifp2);
20375f40ef77SHannes Frederic Sowa lock_errdad:
20385f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp->lock);
20391b34be74SYOSHIFUJI Hideaki 	}
20401b34be74SYOSHIFUJI Hideaki 
20415f40ef77SHannes Frederic Sowa errdad:
2042c15b1ccaSHannes Frederic Sowa 	/* transition from _POSTDAD to _ERRDAD */
2043c15b1ccaSHannes Frederic Sowa 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
20448e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
2045c15b1ccaSHannes Frederic Sowa 
2046c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, 0);
2047751eb6b6SWei Yongjun 	in6_ifa_put(ifp);
20483c21edbdSYOSHIFUJI Hideaki }
20491da177e4SLinus Torvalds 
2050a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group.
2051a9ed4a29SSabrina Dubroca  * caller must hold RTNL */
2052b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
20531da177e4SLinus Torvalds {
20541da177e4SLinus Torvalds 	struct in6_addr maddr;
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
20571da177e4SLinus Torvalds 		return;
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
20601da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
20611da177e4SLinus Torvalds }
20621da177e4SLinus Torvalds 
2063a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
2064b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
20651da177e4SLinus Torvalds {
20661da177e4SLinus Torvalds 	struct in6_addr maddr;
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
20691da177e4SLinus Torvalds 		return;
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
20721da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
20731da177e4SLinus Torvalds }
20741da177e4SLinus Torvalds 
2075a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
207620380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
20771da177e4SLinus Torvalds {
20781da177e4SLinus Torvalds 	struct in6_addr addr;
2079c15b1ccaSHannes Frederic Sowa 
208088ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
20812bda8a0cSBjørn Mork 		return;
20821da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
20831da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
20841da177e4SLinus Torvalds 		return;
2085013b4d90SWANG Cong 	__ipv6_dev_ac_inc(ifp->idev, &addr);
20861da177e4SLinus Torvalds }
20871da177e4SLinus Torvalds 
2088a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
208920380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
20901da177e4SLinus Torvalds {
20911da177e4SLinus Torvalds 	struct in6_addr addr;
2092c15b1ccaSHannes Frederic Sowa 
209388ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
209432019e65SYOSHIFUJI Hideaki 		return;
20951da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
20961da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
20971da177e4SLinus Torvalds 		return;
20981da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds 
21018a7a4b47SAlexander Aring static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
210206a4c1c5Salex.bluesman.smirnov@gmail.com {
21038a7a4b47SAlexander Aring 	switch (dev->addr_len) {
21048a7a4b47SAlexander Aring 	case ETH_ALEN:
21059dae2e03SLuiz Augusto von Dentz 		memcpy(eui, dev->dev_addr, 3);
21069dae2e03SLuiz Augusto von Dentz 		eui[3] = 0xFF;
21079dae2e03SLuiz Augusto von Dentz 		eui[4] = 0xFE;
21089dae2e03SLuiz Augusto von Dentz 		memcpy(eui + 5, dev->dev_addr + 3, 3);
21099dae2e03SLuiz Augusto von Dentz 		break;
21108a7a4b47SAlexander Aring 	case EUI64_ADDR_LEN:
21115241c2d7SAlexander Aring 		memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
21125e98a36eSYOSHIFUJI Hideaki / 吉藤英明 		eui[0] ^= 2;
21138a7a4b47SAlexander Aring 		break;
21148a7a4b47SAlexander Aring 	default:
21158a7a4b47SAlexander Aring 		return -1;
21168a7a4b47SAlexander Aring 	}
21178a7a4b47SAlexander Aring 
211806a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
211906a4c1c5Salex.bluesman.smirnov@gmail.com }
212006a4c1c5Salex.bluesman.smirnov@gmail.com 
2121cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2122cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
2123cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	union fwnet_hwaddr *ha;
2124cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2125cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
2126cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
2127cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2128cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	ha = (union fwnet_hwaddr *)dev->dev_addr;
2129cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2130cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2131cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
2132cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
2133cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
2134cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2135073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2136073a8e0eSYOSHIFUJI Hideaki {
21371da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
21381da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
21391da177e4SLinus Torvalds 		return -1;
21401da177e4SLinus Torvalds 	memset(eui, 0, 7);
21411da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
21421da177e4SLinus Torvalds 	return 0;
2143073a8e0eSYOSHIFUJI Hideaki }
2144073a8e0eSYOSHIFUJI Hideaki 
2145073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2146073a8e0eSYOSHIFUJI Hideaki {
21471da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
21481da177e4SLinus Torvalds 		return -1;
21491da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
21501da177e4SLinus Torvalds 	eui[0] |= 2;
21511da177e4SLinus Torvalds 	return 0;
21521da177e4SLinus Torvalds }
2153073a8e0eSYOSHIFUJI Hideaki 
2154c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2155dfd982baSYOSHIFUJI Hideaki {
21569af28511SSascha Hlusiak 	if (addr == 0)
21579af28511SSascha Hlusiak 		return -1;
2158dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2159dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2160dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2161dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2162dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2163dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2164dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
2165dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
2166dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
2167dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
2168dfd982baSYOSHIFUJI Hideaki 	return 0;
2169dfd982baSYOSHIFUJI Hideaki }
2170dfd982baSYOSHIFUJI Hideaki 
2171dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2172dfd982baSYOSHIFUJI Hideaki {
2173dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
2174dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2175dfd982baSYOSHIFUJI Hideaki 	return -1;
2176dfd982baSYOSHIFUJI Hideaki }
2177dfd982baSYOSHIFUJI Hideaki 
2178aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2179aee80b54Sstephen hemminger {
2180aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2181aee80b54Sstephen hemminger }
2182aee80b54Sstephen hemminger 
2183e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2184e837735eSNicolas Dichtel {
2185e837735eSNicolas Dichtel 	memcpy(eui, dev->perm_addr, 3);
2186e837735eSNicolas Dichtel 	memcpy(eui + 5, dev->perm_addr + 3, 3);
2187e837735eSNicolas Dichtel 	eui[3] = 0xFF;
2188e837735eSNicolas Dichtel 	eui[4] = 0xFE;
2189e837735eSNicolas Dichtel 	eui[0] ^= 2;
2190e837735eSNicolas Dichtel 	return 0;
2191e837735eSNicolas Dichtel }
2192e837735eSNicolas Dichtel 
2193073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2194073a8e0eSYOSHIFUJI Hideaki {
2195073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
2196073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
2197073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
2198073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
2199073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
2200073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
2201073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
2202073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
2203c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
2204dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
2205aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
220645ce0fd1SFelix Jia 	case ARPHRD_TUNNEL:
2207aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
2208e74bccb8SJukka Rissanen 	case ARPHRD_6LOWPAN:
22098a7a4b47SAlexander Aring 		return addrconf_ifid_6lowpan(eui, dev);
2210cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
2211cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
2212e837735eSNicolas Dichtel 	case ARPHRD_TUNNEL6:
221345ce0fd1SFelix Jia 	case ARPHRD_IP6GRE:
2214e837735eSNicolas Dichtel 		return addrconf_ifid_ip6tnl(eui, dev);
2215073a8e0eSYOSHIFUJI Hideaki 	}
22161da177e4SLinus Torvalds 	return -1;
22171da177e4SLinus Torvalds }
22181da177e4SLinus Torvalds 
22191da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
22201da177e4SLinus Torvalds {
22211da177e4SLinus Torvalds 	int err = -1;
22221da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
22231da177e4SLinus Torvalds 
22241da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2225602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2226602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
2227602582caSHannes Frederic Sowa 			break;
22281da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
22291da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
22301da177e4SLinus Torvalds 			err = 0;
22311da177e4SLinus Torvalds 			break;
22321da177e4SLinus Torvalds 		}
22331da177e4SLinus Torvalds 	}
22341da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
22351da177e4SLinus Torvalds 	return err;
22361da177e4SLinus Torvalds }
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
22399d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev)
22401da177e4SLinus Torvalds {
22411da177e4SLinus Torvalds regen:
2242955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
22431da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 	/*
22461da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
22471da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
22481da177e4SLinus Torvalds 	 *
22491da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
22501da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
2251c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
22521da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
22531da177e4SLinus Torvalds 	 *  - value 0
22541da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
22551da177e4SLinus Torvalds 	 */
22561da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
22571da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
22581da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
22591da177e4SLinus Torvalds 		goto regen;
22601da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
22611da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
22621da177e4SLinus Torvalds 			goto regen;
22631da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
22641da177e4SLinus Torvalds 			goto regen;
22651da177e4SLinus Torvalds 	}
22661da177e4SLinus Torvalds }
22671da177e4SLinus Torvalds 
22689d6280daSJiri Bohac static void  ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
22698e5e8f30SEldad Zack {
22701da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
22719d6280daSJiri Bohac 		ipv6_regen_rndid(idev);
22721da177e4SLinus Torvalds }
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds /*
22751da177e4SLinus Torvalds  *	Add prefix route.
22761da177e4SLinus Torvalds  */
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds static void
22791da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2280e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
22811da177e4SLinus Torvalds {
228286872cb5SThomas Graf 	struct fib6_config cfg = {
2283ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
228486872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
228586872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
228686872cb5SThomas Graf 		.fc_expires = expires,
228786872cb5SThomas Graf 		.fc_dst_len = plen,
228886872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
2289c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
2290f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
229186872cb5SThomas Graf 	};
22921da177e4SLinus Torvalds 
22934e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
22941da177e4SLinus Torvalds 
22951da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
22961da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
22971da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
22981da177e4SLinus Torvalds 	 */
229907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
23001da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
230186872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
23020be669bbSJoerg Roedel #endif
23031da177e4SLinus Torvalds 
2304333c4301SDavid Ahern 	ip6_route_add(&cfg, NULL);
23051da177e4SLinus Torvalds }
23061da177e4SLinus Torvalds 
230714ef37b6SAndreas Hofmeister 
230814ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
230914ef37b6SAndreas Hofmeister 						  int plen,
231014ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
231114ef37b6SAndreas Hofmeister 						  u32 flags, u32 noflags)
231214ef37b6SAndreas Hofmeister {
231314ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
231414ef37b6SAndreas Hofmeister 	struct rt6_info *rt = NULL;
231514ef37b6SAndreas Hofmeister 	struct fib6_table *table;
2316ca254490SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
231714ef37b6SAndreas Hofmeister 
2318ca254490SDavid Ahern 	table = fib6_get_table(dev_net(dev), tb_id);
231963159f29SIan Morris 	if (!table)
232014ef37b6SAndreas Hofmeister 		return NULL;
232114ef37b6SAndreas Hofmeister 
232266f5d6ceSWei Wang 	rcu_read_lock();
232338fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
232414ef37b6SAndreas Hofmeister 	if (!fn)
232514ef37b6SAndreas Hofmeister 		goto out;
23261f56a01fSMartin KaFai Lau 
232766f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
2328d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != dev->ifindex)
232914ef37b6SAndreas Hofmeister 			continue;
233014ef37b6SAndreas Hofmeister 		if ((rt->rt6i_flags & flags) != flags)
233114ef37b6SAndreas Hofmeister 			continue;
233285da53bfSRomain Kuntz 		if ((rt->rt6i_flags & noflags) != 0)
233314ef37b6SAndreas Hofmeister 			continue;
2334d3843fe5SWei Wang 		if (!dst_hold_safe(&rt->dst))
2335d3843fe5SWei Wang 			rt = NULL;
233614ef37b6SAndreas Hofmeister 		break;
233714ef37b6SAndreas Hofmeister 	}
233814ef37b6SAndreas Hofmeister out:
233966f5d6ceSWei Wang 	rcu_read_unlock();
234014ef37b6SAndreas Hofmeister 	return rt;
234114ef37b6SAndreas Hofmeister }
234214ef37b6SAndreas Hofmeister 
234314ef37b6SAndreas Hofmeister 
23441da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
23471da177e4SLinus Torvalds {
234886872cb5SThomas Graf 	struct fib6_config cfg = {
2349ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
235086872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
235186872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
235286872cb5SThomas Graf 		.fc_dst_len = 8,
235386872cb5SThomas Graf 		.fc_flags = RTF_UP,
2354c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
235586872cb5SThomas Graf 	};
23561da177e4SLinus Torvalds 
235786872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
235886872cb5SThomas Graf 
2359333c4301SDavid Ahern 	ip6_route_add(&cfg, NULL);
23601da177e4SLinus Torvalds }
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
23631da177e4SLinus Torvalds {
23641da177e4SLinus Torvalds 	struct inet6_dev *idev;
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds 	ASSERT_RTNL();
23671da177e4SLinus Torvalds 
2368e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2369e21e8467SStephen Hemminger 	if (!idev)
237064e724f6SBrian Haley 		return ERR_PTR(-ENOBUFS);
237164e724f6SBrian Haley 
237264e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
237364e724f6SBrian Haley 		return ERR_PTR(-EACCES);
23741da177e4SLinus Torvalds 
23751da177e4SLinus Torvalds 	/* Add default multicast route */
2376ba46ee4cSDavid Ahern 	if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
23771da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds 	return idev;
23801da177e4SLinus Torvalds }
23811da177e4SLinus Torvalds 
238253bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev,
238353bd6749SJiri Pirko 			     struct inet6_ifaddr *ifp,
238453bd6749SJiri Pirko 			     __u32 valid_lft, __u32 prefered_lft,
238553bd6749SJiri Pirko 			     bool create, unsigned long now)
238653bd6749SJiri Pirko {
238753bd6749SJiri Pirko 	u32 flags;
238853bd6749SJiri Pirko 	struct inet6_ifaddr *ift;
238953bd6749SJiri Pirko 
239053bd6749SJiri Pirko 	read_lock_bh(&idev->lock);
239153bd6749SJiri Pirko 	/* update all temporary addresses in the list */
239253bd6749SJiri Pirko 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
239353bd6749SJiri Pirko 		int age, max_valid, max_prefered;
239453bd6749SJiri Pirko 
239553bd6749SJiri Pirko 		if (ifp != ift->ifpub)
239653bd6749SJiri Pirko 			continue;
239753bd6749SJiri Pirko 
239853bd6749SJiri Pirko 		/* RFC 4941 section 3.3:
239953bd6749SJiri Pirko 		 * If a received option will extend the lifetime of a public
240053bd6749SJiri Pirko 		 * address, the lifetimes of temporary addresses should
240153bd6749SJiri Pirko 		 * be extended, subject to the overall constraint that no
240253bd6749SJiri Pirko 		 * temporary addresses should ever remain "valid" or "preferred"
240353bd6749SJiri Pirko 		 * for a time longer than (TEMP_VALID_LIFETIME) or
240453bd6749SJiri Pirko 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
240553bd6749SJiri Pirko 		 */
240653bd6749SJiri Pirko 		age = (now - ift->cstamp) / HZ;
240753bd6749SJiri Pirko 		max_valid = idev->cnf.temp_valid_lft - age;
240853bd6749SJiri Pirko 		if (max_valid < 0)
240953bd6749SJiri Pirko 			max_valid = 0;
241053bd6749SJiri Pirko 
241153bd6749SJiri Pirko 		max_prefered = idev->cnf.temp_prefered_lft -
241276506a98SJiri Bohac 			       idev->desync_factor - age;
241353bd6749SJiri Pirko 		if (max_prefered < 0)
241453bd6749SJiri Pirko 			max_prefered = 0;
241553bd6749SJiri Pirko 
241653bd6749SJiri Pirko 		if (valid_lft > max_valid)
241753bd6749SJiri Pirko 			valid_lft = max_valid;
241853bd6749SJiri Pirko 
241953bd6749SJiri Pirko 		if (prefered_lft > max_prefered)
242053bd6749SJiri Pirko 			prefered_lft = max_prefered;
242153bd6749SJiri Pirko 
242253bd6749SJiri Pirko 		spin_lock(&ift->lock);
242353bd6749SJiri Pirko 		flags = ift->flags;
242453bd6749SJiri Pirko 		ift->valid_lft = valid_lft;
242553bd6749SJiri Pirko 		ift->prefered_lft = prefered_lft;
242653bd6749SJiri Pirko 		ift->tstamp = now;
242753bd6749SJiri Pirko 		if (prefered_lft > 0)
242853bd6749SJiri Pirko 			ift->flags &= ~IFA_F_DEPRECATED;
242953bd6749SJiri Pirko 
243053bd6749SJiri Pirko 		spin_unlock(&ift->lock);
243153bd6749SJiri Pirko 		if (!(flags&IFA_F_TENTATIVE))
243253bd6749SJiri Pirko 			ipv6_ifa_notify(0, ift);
243353bd6749SJiri Pirko 	}
243453bd6749SJiri Pirko 
243553bd6749SJiri Pirko 	if ((create || list_empty(&idev->tempaddr_list)) &&
243653bd6749SJiri Pirko 	    idev->cnf.use_tempaddr > 0) {
243753bd6749SJiri Pirko 		/* When a new public address is created as described
243853bd6749SJiri Pirko 		 * in [ADDRCONF], also create a new temporary address.
243953bd6749SJiri Pirko 		 * Also create a temporary address if it's enabled but
244053bd6749SJiri Pirko 		 * no temporary address currently exists.
244153bd6749SJiri Pirko 		 */
244253bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
244353bd6749SJiri Pirko 		ipv6_create_tempaddr(ifp, NULL);
244453bd6749SJiri Pirko 	} else {
244553bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
244653bd6749SJiri Pirko 	}
244753bd6749SJiri Pirko }
244853bd6749SJiri Pirko 
2449cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2450cc9da6ccSBjørn Mork {
2451d35a00b8SFelix Jia 	return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2452d35a00b8SFelix Jia 	       idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2453cc9da6ccSBjørn Mork }
2454cc9da6ccSBjørn Mork 
2455cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
24564f672235SAlexander Aring 				 const struct prefix_info *pinfo,
24574f672235SAlexander Aring 				 struct inet6_dev *in6_dev,
2458cc84b3c6SAlexander Aring 				 const struct in6_addr *addr, int addr_type,
2459cc84b3c6SAlexander Aring 				 u32 addr_flags, bool sllao, bool tokenized,
24604f672235SAlexander Aring 				 __u32 valid_lft, u32 prefered_lft)
24614f672235SAlexander Aring {
24624f672235SAlexander Aring 	struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
24634f672235SAlexander Aring 	int create = 0, update_lft = 0;
24644f672235SAlexander Aring 
24654f672235SAlexander Aring 	if (!ifp && valid_lft) {
24664f672235SAlexander Aring 		int max_addresses = in6_dev->cnf.max_addresses;
24674f672235SAlexander Aring 
24684f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
246935e015e1SMatteo Croce 		if ((net->ipv6.devconf_all->optimistic_dad ||
247035e015e1SMatteo Croce 		     in6_dev->cnf.optimistic_dad) &&
24714f672235SAlexander Aring 		    !net->ipv6.devconf_all->forwarding && sllao)
24724f672235SAlexander Aring 			addr_flags |= IFA_F_OPTIMISTIC;
24734f672235SAlexander Aring #endif
24744f672235SAlexander Aring 
24754f672235SAlexander Aring 		/* Do not allow to create too much of autoconfigured
24764f672235SAlexander Aring 		 * addresses; this would be too easy way to crash kernel.
24774f672235SAlexander Aring 		 */
24784f672235SAlexander Aring 		if (!max_addresses ||
24794f672235SAlexander Aring 		    ipv6_count_addresses(in6_dev) < max_addresses)
24804f672235SAlexander Aring 			ifp = ipv6_add_addr(in6_dev, addr, NULL,
24814f672235SAlexander Aring 					    pinfo->prefix_len,
24824f672235SAlexander Aring 					    addr_type&IPV6_ADDR_SCOPE_MASK,
24834f672235SAlexander Aring 					    addr_flags, valid_lft,
24844f672235SAlexander Aring 					    prefered_lft);
24854f672235SAlexander Aring 
24864f672235SAlexander Aring 		if (IS_ERR_OR_NULL(ifp))
24874f672235SAlexander Aring 			return -1;
24884f672235SAlexander Aring 
24894f672235SAlexander Aring 		update_lft = 0;
24904f672235SAlexander Aring 		create = 1;
24914f672235SAlexander Aring 		spin_lock_bh(&ifp->lock);
24924f672235SAlexander Aring 		ifp->flags |= IFA_F_MANAGETEMPADDR;
24934f672235SAlexander Aring 		ifp->cstamp = jiffies;
24944f672235SAlexander Aring 		ifp->tokenized = tokenized;
24954f672235SAlexander Aring 		spin_unlock_bh(&ifp->lock);
24964f672235SAlexander Aring 		addrconf_dad_start(ifp);
24974f672235SAlexander Aring 	}
24984f672235SAlexander Aring 
24994f672235SAlexander Aring 	if (ifp) {
25004f672235SAlexander Aring 		u32 flags;
25014f672235SAlexander Aring 		unsigned long now;
25024f672235SAlexander Aring 		u32 stored_lft;
25034f672235SAlexander Aring 
25044f672235SAlexander Aring 		/* update lifetime (RFC2462 5.5.3 e) */
25054f672235SAlexander Aring 		spin_lock_bh(&ifp->lock);
25064f672235SAlexander Aring 		now = jiffies;
25074f672235SAlexander Aring 		if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
25084f672235SAlexander Aring 			stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
25094f672235SAlexander Aring 		else
25104f672235SAlexander Aring 			stored_lft = 0;
25114f672235SAlexander Aring 		if (!update_lft && !create && stored_lft) {
25124f672235SAlexander Aring 			const u32 minimum_lft = min_t(u32,
25134f672235SAlexander Aring 				stored_lft, MIN_VALID_LIFETIME);
25144f672235SAlexander Aring 			valid_lft = max(valid_lft, minimum_lft);
25154f672235SAlexander Aring 
25164f672235SAlexander Aring 			/* RFC4862 Section 5.5.3e:
25174f672235SAlexander Aring 			 * "Note that the preferred lifetime of the
25184f672235SAlexander Aring 			 *  corresponding address is always reset to
25194f672235SAlexander Aring 			 *  the Preferred Lifetime in the received
25204f672235SAlexander Aring 			 *  Prefix Information option, regardless of
25214f672235SAlexander Aring 			 *  whether the valid lifetime is also reset or
25224f672235SAlexander Aring 			 *  ignored."
25234f672235SAlexander Aring 			 *
25244f672235SAlexander Aring 			 * So we should always update prefered_lft here.
25254f672235SAlexander Aring 			 */
25264f672235SAlexander Aring 			update_lft = 1;
25274f672235SAlexander Aring 		}
25284f672235SAlexander Aring 
25294f672235SAlexander Aring 		if (update_lft) {
25304f672235SAlexander Aring 			ifp->valid_lft = valid_lft;
25314f672235SAlexander Aring 			ifp->prefered_lft = prefered_lft;
25324f672235SAlexander Aring 			ifp->tstamp = now;
25334f672235SAlexander Aring 			flags = ifp->flags;
25344f672235SAlexander Aring 			ifp->flags &= ~IFA_F_DEPRECATED;
25354f672235SAlexander Aring 			spin_unlock_bh(&ifp->lock);
25364f672235SAlexander Aring 
25374f672235SAlexander Aring 			if (!(flags&IFA_F_TENTATIVE))
25384f672235SAlexander Aring 				ipv6_ifa_notify(0, ifp);
25394f672235SAlexander Aring 		} else
25404f672235SAlexander Aring 			spin_unlock_bh(&ifp->lock);
25414f672235SAlexander Aring 
25424f672235SAlexander Aring 		manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
25434f672235SAlexander Aring 				 create, now);
25444f672235SAlexander Aring 
25454f672235SAlexander Aring 		in6_ifa_put(ifp);
25464f672235SAlexander Aring 		addrconf_verify();
25474f672235SAlexander Aring 	}
25484f672235SAlexander Aring 
25494f672235SAlexander Aring 	return 0;
25504f672235SAlexander Aring }
2551cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
25524f672235SAlexander Aring 
2553e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
25541da177e4SLinus Torvalds {
25551da177e4SLinus Torvalds 	struct prefix_info *pinfo;
25561da177e4SLinus Torvalds 	__u32 valid_lft;
25571da177e4SLinus Torvalds 	__u32 prefered_lft;
25584f672235SAlexander Aring 	int addr_type, err;
255964236f3fSHannes Frederic Sowa 	u32 addr_flags = 0;
25601da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
256156d417b1SBrian Haley 	struct net *net = dev_net(dev);
25621da177e4SLinus Torvalds 
25631da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
2566ba3542e1Sdingtianhong 		ADBG("addrconf: prefix option too short\n");
25671da177e4SLinus Torvalds 		return;
25681da177e4SLinus Torvalds 	}
25691da177e4SLinus Torvalds 
25701da177e4SLinus Torvalds 	/*
25711da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
25721da177e4SLinus Torvalds 	 */
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
25771da177e4SLinus Torvalds 		return;
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
25801da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2583e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
25841da177e4SLinus Torvalds 		return;
25851da177e4SLinus Torvalds 	}
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
25881da177e4SLinus Torvalds 
258963159f29SIan Morris 	if (!in6_dev) {
2590e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2591e87cc472SJoe Perches 				    dev->name);
25921da177e4SLinus Torvalds 		return;
25931da177e4SLinus Torvalds 	}
25941da177e4SLinus Torvalds 
25951da177e4SLinus Torvalds 	/*
25961da177e4SLinus Torvalds 	 *	Two things going on here:
25971da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
25981da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
25991da177e4SLinus Torvalds 	 */
26001da177e4SLinus Torvalds 
26014bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
26024bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
26034bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
26044bed72e4SYOSHIFUJI Hideaki 
26051da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
26066f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
26076f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
26086f704992SYOSHIFUJI Hideaki 		 * not good.
26091da177e4SLinus Torvalds 		 */
26104bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
26114bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
26124bed72e4SYOSHIFUJI Hideaki 		else
26134bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
26143dd4bc68SYOSHIFUJI Hideaki 
26154bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
26164bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
26171da177e4SLinus Torvalds 
261814ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
261914ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
262014ef37b6SAndreas Hofmeister 					       dev,
262114ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
262214ef37b6SAndreas Hofmeister 					       RTF_GATEWAY | RTF_DEFAULT);
26231da177e4SLinus Torvalds 
262414ef37b6SAndreas Hofmeister 		if (rt) {
26256f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
26261da177e4SLinus Torvalds 			if (valid_lft == 0) {
2627e0a1ad73SThomas Graf 				ip6_del_rt(rt);
26281da177e4SLinus Torvalds 				rt = NULL;
26294bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
26306f704992SYOSHIFUJI Hideaki 				/* not infinity */
26311716a961SGao feng 				rt6_set_expires(rt, jiffies + rt_expires);
26326f704992SYOSHIFUJI Hideaki 			} else {
26331716a961SGao feng 				rt6_clean_expires(rt);
26341da177e4SLinus Torvalds 			}
26351da177e4SLinus Torvalds 		} else if (valid_lft) {
26366f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
26374bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
26384bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
26396f704992SYOSHIFUJI Hideaki 				/* not infinity */
26406f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
26416f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
26426f704992SYOSHIFUJI Hideaki 			}
26431da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
26446f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
26451da177e4SLinus Torvalds 		}
264694e187c0SAmerigo Wang 		ip6_rt_put(rt);
26471da177e4SLinus Torvalds 	}
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
26501da177e4SLinus Torvalds 
26511da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
26521da177e4SLinus Torvalds 		struct in6_addr addr;
2653f997c55cSAlexander Aring 		bool tokenized = false, dev_addr_generated = false;
26541da177e4SLinus Torvalds 
26551da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
26561da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2657f53adae4SDaniel Borkmann 
2658f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2659f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2660f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2661f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2662f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
2663617fe29dSDaniel Borkmann 				tokenized = true;
2664cc9da6ccSBjørn Mork 			} else if (is_addr_mode_generate_stable(in6_dev) &&
2665622c81d5SHannes Frederic Sowa 				   !ipv6_generate_stable_address(&addr, 0,
2666622c81d5SHannes Frederic Sowa 								 in6_dev)) {
266764236f3fSHannes Frederic Sowa 				addr_flags |= IFA_F_STABLE_PRIVACY;
2668622c81d5SHannes Frederic Sowa 				goto ok;
2669f53adae4SDaniel Borkmann 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
26701da177e4SLinus Torvalds 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
26714f672235SAlexander Aring 				goto put;
2672f997c55cSAlexander Aring 			} else {
2673f997c55cSAlexander Aring 				dev_addr_generated = true;
26741da177e4SLinus Torvalds 			}
26751da177e4SLinus Torvalds 			goto ok;
26761da177e4SLinus Torvalds 		}
2677e87cc472SJoe Perches 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
26781da177e4SLinus Torvalds 				    pinfo->prefix_len);
26794f672235SAlexander Aring 		goto put;
26801da177e4SLinus Torvalds 
26811da177e4SLinus Torvalds ok:
26824f672235SAlexander Aring 		err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
26834f672235SAlexander Aring 						   &addr, addr_type,
26844f672235SAlexander Aring 						   addr_flags, sllao,
26854f672235SAlexander Aring 						   tokenized, valid_lft,
26868a226b2cSJiri Benc 						   prefered_lft);
26874f672235SAlexander Aring 		if (err)
26884f672235SAlexander Aring 			goto put;
2689f997c55cSAlexander Aring 
2690f997c55cSAlexander Aring 		/* Ignore error case here because previous prefix add addr was
2691f997c55cSAlexander Aring 		 * successful which will be notified.
2692f997c55cSAlexander Aring 		 */
2693f997c55cSAlexander Aring 		ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2694f997c55cSAlexander Aring 					      addr_type, addr_flags, sllao,
2695f997c55cSAlexander Aring 					      tokenized, valid_lft,
2696f997c55cSAlexander Aring 					      prefered_lft,
2697f997c55cSAlexander Aring 					      dev_addr_generated);
26981da177e4SLinus Torvalds 	}
26991da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
27004f672235SAlexander Aring put:
27011da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
27021da177e4SLinus Torvalds }
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds /*
27051da177e4SLinus Torvalds  *	Set destination address.
27061da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
27071da177e4SLinus Torvalds  *	device.
27081da177e4SLinus Torvalds  */
2709af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
27101da177e4SLinus Torvalds {
27111da177e4SLinus Torvalds 	struct in6_ifreq ireq;
27121da177e4SLinus Torvalds 	struct net_device *dev;
27131da177e4SLinus Torvalds 	int err = -EINVAL;
27141da177e4SLinus Torvalds 
27151da177e4SLinus Torvalds 	rtnl_lock();
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds 	err = -EFAULT;
27181da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
27191da177e4SLinus Torvalds 		goto err_exit;
27201da177e4SLinus Torvalds 
2721af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds 	err = -ENODEV;
272463159f29SIan Morris 	if (!dev)
27251da177e4SLinus Torvalds 		goto err_exit;
27261da177e4SLinus Torvalds 
272707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
27281da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
27295bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
27301da177e4SLinus Torvalds 		struct ifreq ifr;
27311da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
27341da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
27351da177e4SLinus Torvalds 			goto err_exit;
27361da177e4SLinus Torvalds 
27371da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
27381da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
27391da177e4SLinus Torvalds 		p.iph.saddr = 0;
27401da177e4SLinus Torvalds 		p.iph.version = 4;
27411da177e4SLinus Torvalds 		p.iph.ihl = 5;
27421da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
27431da177e4SLinus Torvalds 		p.iph.ttl = 64;
2744d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
27451da177e4SLinus Torvalds 
27465bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
27475bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
27485bc3eb7eSStephen Hemminger 
27495bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
27505bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
27511da177e4SLinus Torvalds 			set_fs(oldfs);
27525bc3eb7eSStephen Hemminger 		} else
27535bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
27541da177e4SLinus Torvalds 
27551da177e4SLinus Torvalds 		if (err == 0) {
27561da177e4SLinus Torvalds 			err = -ENOBUFS;
2757af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2758af284937SDaniel Lezcano 			if (!dev)
27591da177e4SLinus Torvalds 				goto err_exit;
27601da177e4SLinus Torvalds 			err = dev_open(dev);
27611da177e4SLinus Torvalds 		}
27621da177e4SLinus Torvalds 	}
27630be669bbSJoerg Roedel #endif
27641da177e4SLinus Torvalds 
27651da177e4SLinus Torvalds err_exit:
27661da177e4SLinus Torvalds 	rtnl_unlock();
27671da177e4SLinus Torvalds 	return err;
27681da177e4SLinus Torvalds }
27691da177e4SLinus Torvalds 
277093a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join,
277193a714d6SMadhu Challa 			  const struct in6_addr *addr, int ifindex)
277293a714d6SMadhu Challa {
277393a714d6SMadhu Challa 	int ret;
277493a714d6SMadhu Challa 
277593a714d6SMadhu Challa 	ASSERT_RTNL();
277693a714d6SMadhu Challa 
277793a714d6SMadhu Challa 	lock_sock(sk);
277893a714d6SMadhu Challa 	if (join)
277954ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_join(sk, ifindex, addr);
278093a714d6SMadhu Challa 	else
278154ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_drop(sk, ifindex, addr);
278293a714d6SMadhu Challa 	release_sock(sk);
278393a714d6SMadhu Challa 
278493a714d6SMadhu Challa 	return ret;
278593a714d6SMadhu Challa }
278693a714d6SMadhu Challa 
27871da177e4SLinus Torvalds /*
27881da177e4SLinus Torvalds  *	Manual configuration of address on an interface
27891da177e4SLinus Torvalds  */
2790479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex,
2791479840ffSJiri Pirko 			  const struct in6_addr *pfx,
2792caeaba79SNicolas Dichtel 			  const struct in6_addr *peer_pfx,
2793479840ffSJiri Pirko 			  unsigned int plen, __u32 ifa_flags,
2794479840ffSJiri Pirko 			  __u32 prefered_lft, __u32 valid_lft)
27951da177e4SLinus Torvalds {
27961da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
27971da177e4SLinus Torvalds 	struct inet6_dev *idev;
27981da177e4SLinus Torvalds 	struct net_device *dev;
279993a714d6SMadhu Challa 	unsigned long timeout;
280093a714d6SMadhu Challa 	clock_t expires;
28011da177e4SLinus Torvalds 	int scope;
28026f704992SYOSHIFUJI Hideaki 	u32 flags;
28031da177e4SLinus Torvalds 
28041da177e4SLinus Torvalds 	ASSERT_RTNL();
28051da177e4SLinus Torvalds 
280624ef0da7SThomas Graf 	if (plen > 128)
280724ef0da7SThomas Graf 		return -EINVAL;
280824ef0da7SThomas Graf 
28090778769dSNoriaki TAKAMIYA 	/* check the lifetime */
28100778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
28110778769dSNoriaki TAKAMIYA 		return -EINVAL;
28120778769dSNoriaki TAKAMIYA 
281353bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
281453bd6749SJiri Pirko 		return -EINVAL;
281553bd6749SJiri Pirko 
2816af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2817af284937SDaniel Lezcano 	if (!dev)
28181da177e4SLinus Torvalds 		return -ENODEV;
28191da177e4SLinus Torvalds 
282064e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
282164e724f6SBrian Haley 	if (IS_ERR(idev))
282264e724f6SBrian Haley 		return PTR_ERR(idev);
28231da177e4SLinus Torvalds 
282493a714d6SMadhu Challa 	if (ifa_flags & IFA_F_MCAUTOJOIN) {
282593a714d6SMadhu Challa 		int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
282693a714d6SMadhu Challa 					 true, pfx, ifindex);
282793a714d6SMadhu Challa 
282893a714d6SMadhu Challa 		if (ret < 0)
282993a714d6SMadhu Challa 			return ret;
283093a714d6SMadhu Challa 	}
283193a714d6SMadhu Challa 
28321da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
28331da177e4SLinus Torvalds 
28344bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
28354bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
28364bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
28374bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
28386f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
28394bed72e4SYOSHIFUJI Hideaki 	} else {
28404bed72e4SYOSHIFUJI Hideaki 		expires = 0;
28414bed72e4SYOSHIFUJI Hideaki 		flags = 0;
28424bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
28436f704992SYOSHIFUJI Hideaki 	}
28440778769dSNoriaki TAKAMIYA 
28454bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
28464bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
28474bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
28480778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
28494bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
28504bed72e4SYOSHIFUJI Hideaki 	}
28510778769dSNoriaki TAKAMIYA 
28528a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
28538a226b2cSJiri Benc 			    valid_lft, prefered_lft);
28540778769dSNoriaki TAKAMIYA 
28551da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
2856761aac73SThomas Haller 		if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
285746d48046SYOSHIFUJI Hideaki 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
28586f704992SYOSHIFUJI Hideaki 					      expires, flags);
2859761aac73SThomas Haller 		}
2860761aac73SThomas Haller 
286195c385b4SNeil Horman 		/*
286295c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
286395c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
286495c385b4SNeil Horman 		 * manually configured addresses
286595c385b4SNeil Horman 		 */
2866cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
286753bd6749SJiri Pirko 		if (ifa_flags & IFA_F_MANAGETEMPADDR)
286853bd6749SJiri Pirko 			manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
286953bd6749SJiri Pirko 					 true, jiffies);
28701da177e4SLinus Torvalds 		in6_ifa_put(ifp);
2871c15b1ccaSHannes Frederic Sowa 		addrconf_verify_rtnl();
28721da177e4SLinus Torvalds 		return 0;
287393a714d6SMadhu Challa 	} else if (ifa_flags & IFA_F_MCAUTOJOIN) {
287493a714d6SMadhu Challa 		ipv6_mc_config(net->ipv6.mc_autojoin_sk,
287593a714d6SMadhu Challa 			       false, pfx, ifindex);
28761da177e4SLinus Torvalds 	}
28771da177e4SLinus Torvalds 
28781da177e4SLinus Torvalds 	return PTR_ERR(ifp);
28791da177e4SLinus Torvalds }
28801da177e4SLinus Torvalds 
28816046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
28826046d5b4SHeiner Kallweit 			  const struct in6_addr *pfx, unsigned int plen)
28831da177e4SLinus Torvalds {
28841da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
28851da177e4SLinus Torvalds 	struct inet6_dev *idev;
28861da177e4SLinus Torvalds 	struct net_device *dev;
28871da177e4SLinus Torvalds 
288824ef0da7SThomas Graf 	if (plen > 128)
288924ef0da7SThomas Graf 		return -EINVAL;
289024ef0da7SThomas Graf 
2891af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2892af284937SDaniel Lezcano 	if (!dev)
28931da177e4SLinus Torvalds 		return -ENODEV;
28941da177e4SLinus Torvalds 
2895e5d08d71SIan Morris 	idev = __in6_dev_get(dev);
289663159f29SIan Morris 	if (!idev)
28971da177e4SLinus Torvalds 		return -ENXIO;
28981da177e4SLinus Torvalds 
28991da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2900502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
29011da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
29021da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
29031da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
29041da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
29051da177e4SLinus Torvalds 
29066046d5b4SHeiner Kallweit 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
29076046d5b4SHeiner Kallweit 			    (ifa_flags & IFA_F_MANAGETEMPADDR))
29086046d5b4SHeiner Kallweit 				manage_tempaddrs(idev, ifp, 0, 0, false,
29096046d5b4SHeiner Kallweit 						 jiffies);
29101da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
29116046d5b4SHeiner Kallweit 			addrconf_verify_rtnl();
291293a714d6SMadhu Challa 			if (ipv6_addr_is_multicast(pfx)) {
291393a714d6SMadhu Challa 				ipv6_mc_config(net->ipv6.mc_autojoin_sk,
291493a714d6SMadhu Challa 					       false, pfx, dev->ifindex);
291593a714d6SMadhu Challa 			}
29161da177e4SLinus Torvalds 			return 0;
29171da177e4SLinus Torvalds 		}
29181da177e4SLinus Torvalds 	}
29191da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
29201da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
29211da177e4SLinus Torvalds }
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds 
2924af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
29251da177e4SLinus Torvalds {
29261da177e4SLinus Torvalds 	struct in6_ifreq ireq;
29271da177e4SLinus Torvalds 	int err;
29281da177e4SLinus Torvalds 
2929af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
29301da177e4SLinus Torvalds 		return -EPERM;
29311da177e4SLinus Torvalds 
29321da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
29331da177e4SLinus Torvalds 		return -EFAULT;
29341da177e4SLinus Torvalds 
29351da177e4SLinus Torvalds 	rtnl_lock();
2936caeaba79SNicolas Dichtel 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2937af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2938af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
29391da177e4SLinus Torvalds 	rtnl_unlock();
29401da177e4SLinus Torvalds 	return err;
29411da177e4SLinus Torvalds }
29421da177e4SLinus Torvalds 
2943af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
29441da177e4SLinus Torvalds {
29451da177e4SLinus Torvalds 	struct in6_ifreq ireq;
29461da177e4SLinus Torvalds 	int err;
29471da177e4SLinus Torvalds 
2948af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
29491da177e4SLinus Torvalds 		return -EPERM;
29501da177e4SLinus Torvalds 
29511da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
29521da177e4SLinus Torvalds 		return -EFAULT;
29531da177e4SLinus Torvalds 
29541da177e4SLinus Torvalds 	rtnl_lock();
29556046d5b4SHeiner Kallweit 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2956af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
29571da177e4SLinus Torvalds 	rtnl_unlock();
29581da177e4SLinus Torvalds 	return err;
29591da177e4SLinus Torvalds }
29601da177e4SLinus Torvalds 
2961b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2962b5f348e5SIlpo Järvinen 		     int plen, int scope)
2963b5f348e5SIlpo Järvinen {
2964b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
2965b5f348e5SIlpo Järvinen 
29668a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, addr, NULL, plen,
296707edd741SHannes Frederic Sowa 			    scope, IFA_F_PERMANENT,
296807edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2969b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
2970b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
2971b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
2972b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
2973764d3be6SPaolo Abeni 		rt_genid_bump_ipv6(dev_net(idev->dev));
2974b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2975b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
2976b5f348e5SIlpo Järvinen 	}
2977b5f348e5SIlpo Järvinen }
2978b5f348e5SIlpo Järvinen 
297907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
29801da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
29811da177e4SLinus Torvalds {
29821da177e4SLinus Torvalds 	struct in6_addr addr;
29831da177e4SLinus Torvalds 	struct net_device *dev;
2984c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
2985929c9cf3SNicolas Dichtel 	int scope, plen;
2986f0e2acfaSNicolas Dichtel 	u32 pflags = 0;
29871da177e4SLinus Torvalds 
29881da177e4SLinus Torvalds 	ASSERT_RTNL();
29891da177e4SLinus Torvalds 
29901da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
29911da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
29921da177e4SLinus Torvalds 
29931da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
29941da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
29951da177e4SLinus Torvalds 		scope = IFA_LINK;
2996929c9cf3SNicolas Dichtel 		plen = 64;
29971da177e4SLinus Torvalds 	} else {
29981da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
2999929c9cf3SNicolas Dichtel 		plen = 96;
3000f0e2acfaSNicolas Dichtel 		pflags |= RTF_NONEXTHOP;
30011da177e4SLinus Torvalds 	}
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
3004929c9cf3SNicolas Dichtel 		add_addr(idev, &addr, plen, scope);
3005f0e2acfaSNicolas Dichtel 		addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
30061da177e4SLinus Torvalds 		return;
30071da177e4SLinus Torvalds 	}
30081da177e4SLinus Torvalds 
30096fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
3010e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
30111da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
30121da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
30131da177e4SLinus Torvalds 
30141da177e4SLinus Torvalds 			int flag = scope;
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
30171da177e4SLinus Torvalds 
30181da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
30191da177e4SLinus Torvalds 
30201da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
30211da177e4SLinus Torvalds 					continue;
30221da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
30231da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
30241da177e4SLinus Torvalds 						continue;
30251da177e4SLinus Torvalds 					flag |= IFA_HOST;
30261da177e4SLinus Torvalds 				}
30271da177e4SLinus Torvalds 
3028b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
3029f0e2acfaSNicolas Dichtel 				addrconf_prefix_route(&addr, plen, idev->dev, 0,
3030f0e2acfaSNicolas Dichtel 						      pflags);
30311da177e4SLinus Torvalds 			}
30321da177e4SLinus Torvalds 		}
30331da177e4SLinus Torvalds 	}
30341da177e4SLinus Torvalds }
30350be669bbSJoerg Roedel #endif
30361da177e4SLinus Torvalds 
30371da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
30381da177e4SLinus Torvalds {
30391da177e4SLinus Torvalds 	struct inet6_dev  *idev;
30401da177e4SLinus Torvalds 
30411da177e4SLinus Torvalds 	/* ::1 */
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds 	ASSERT_RTNL();
30441da177e4SLinus Torvalds 
3045e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
304663159f29SIan Morris 	if (!idev) {
304791df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
30481da177e4SLinus Torvalds 		return;
30491da177e4SLinus Torvalds 	}
30501da177e4SLinus Torvalds 
3051b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
30521da177e4SLinus Torvalds }
30531da177e4SLinus Torvalds 
30542ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev,
305564236f3fSHannes Frederic Sowa 			    const struct in6_addr *addr, u32 flags)
30561da177e4SLinus Torvalds {
30571da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
305864236f3fSHannes Frederic Sowa 	u32 addr_flags = flags | IFA_F_PERMANENT;
30591da177e4SLinus Torvalds 
306095c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
306135e015e1SMatteo Croce 	if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
306235e015e1SMatteo Croce 	     idev->cnf.optimistic_dad) &&
3063702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
306495c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
306595c385b4SNeil Horman #endif
306695c385b4SNeil Horman 
306707edd741SHannes Frederic Sowa 	ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
306807edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
30691da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
307046d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
3071cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
30721da177e4SLinus Torvalds 		in6_ifa_put(ifp);
30731da177e4SLinus Torvalds 	}
30741da177e4SLinus Torvalds }
30752ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
30761da177e4SLinus Torvalds 
3077622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address)
3078622c81d5SHannes Frederic Sowa {
3079622c81d5SHannes Frederic Sowa 	if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3080622c81d5SHannes Frederic Sowa 		return true;
3081622c81d5SHannes Frederic Sowa 
3082622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0x02005eff) &&
3083622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3084622c81d5SHannes Frederic Sowa 		return true;
3085622c81d5SHannes Frederic Sowa 
3086622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3087622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3088622c81d5SHannes Frederic Sowa 		return true;
3089622c81d5SHannes Frederic Sowa 
3090622c81d5SHannes Frederic Sowa 	return false;
3091622c81d5SHannes Frederic Sowa }
3092622c81d5SHannes Frederic Sowa 
3093622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address,
3094622c81d5SHannes Frederic Sowa 					u8 dad_count,
3095622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev)
3096622c81d5SHannes Frederic Sowa {
3097622c81d5SHannes Frederic Sowa 	static DEFINE_SPINLOCK(lock);
3098622c81d5SHannes Frederic Sowa 	static __u32 digest[SHA_DIGEST_WORDS];
3099622c81d5SHannes Frederic Sowa 	static __u32 workspace[SHA_WORKSPACE_WORDS];
3100622c81d5SHannes Frederic Sowa 
3101622c81d5SHannes Frederic Sowa 	static union {
3102622c81d5SHannes Frederic Sowa 		char __data[SHA_MESSAGE_BYTES];
3103622c81d5SHannes Frederic Sowa 		struct {
3104622c81d5SHannes Frederic Sowa 			struct in6_addr secret;
3105ff40217eSHannes Frederic Sowa 			__be32 prefix[2];
3106622c81d5SHannes Frederic Sowa 			unsigned char hwaddr[MAX_ADDR_LEN];
3107622c81d5SHannes Frederic Sowa 			u8 dad_count;
3108622c81d5SHannes Frederic Sowa 		} __packed;
3109622c81d5SHannes Frederic Sowa 	} data;
3110622c81d5SHannes Frederic Sowa 
3111622c81d5SHannes Frederic Sowa 	struct in6_addr secret;
3112622c81d5SHannes Frederic Sowa 	struct in6_addr temp;
3113622c81d5SHannes Frederic Sowa 	struct net *net = dev_net(idev->dev);
3114622c81d5SHannes Frederic Sowa 
3115622c81d5SHannes Frederic Sowa 	BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3116622c81d5SHannes Frederic Sowa 
3117622c81d5SHannes Frederic Sowa 	if (idev->cnf.stable_secret.initialized)
3118622c81d5SHannes Frederic Sowa 		secret = idev->cnf.stable_secret.secret;
3119622c81d5SHannes Frederic Sowa 	else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3120622c81d5SHannes Frederic Sowa 		secret = net->ipv6.devconf_dflt->stable_secret.secret;
3121622c81d5SHannes Frederic Sowa 	else
3122622c81d5SHannes Frederic Sowa 		return -1;
3123622c81d5SHannes Frederic Sowa 
3124622c81d5SHannes Frederic Sowa retry:
3125622c81d5SHannes Frederic Sowa 	spin_lock_bh(&lock);
3126622c81d5SHannes Frederic Sowa 
3127622c81d5SHannes Frederic Sowa 	sha_init(digest);
3128622c81d5SHannes Frederic Sowa 	memset(&data, 0, sizeof(data));
3129622c81d5SHannes Frederic Sowa 	memset(workspace, 0, sizeof(workspace));
3130622c81d5SHannes Frederic Sowa 	memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3131ff40217eSHannes Frederic Sowa 	data.prefix[0] = address->s6_addr32[0];
3132ff40217eSHannes Frederic Sowa 	data.prefix[1] = address->s6_addr32[1];
3133622c81d5SHannes Frederic Sowa 	data.secret = secret;
3134622c81d5SHannes Frederic Sowa 	data.dad_count = dad_count;
3135622c81d5SHannes Frederic Sowa 
3136622c81d5SHannes Frederic Sowa 	sha_transform(digest, data.__data, workspace);
3137622c81d5SHannes Frederic Sowa 
3138622c81d5SHannes Frederic Sowa 	temp = *address;
3139ff40217eSHannes Frederic Sowa 	temp.s6_addr32[2] = (__force __be32)digest[0];
3140ff40217eSHannes Frederic Sowa 	temp.s6_addr32[3] = (__force __be32)digest[1];
3141622c81d5SHannes Frederic Sowa 
3142622c81d5SHannes Frederic Sowa 	spin_unlock_bh(&lock);
3143622c81d5SHannes Frederic Sowa 
3144622c81d5SHannes Frederic Sowa 	if (ipv6_reserved_interfaceid(temp)) {
3145622c81d5SHannes Frederic Sowa 		dad_count++;
31461855b7c3SHannes Frederic Sowa 		if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3147622c81d5SHannes Frederic Sowa 			return -1;
3148622c81d5SHannes Frederic Sowa 		goto retry;
3149622c81d5SHannes Frederic Sowa 	}
3150622c81d5SHannes Frederic Sowa 
3151622c81d5SHannes Frederic Sowa 	*address = temp;
3152622c81d5SHannes Frederic Sowa 	return 0;
3153622c81d5SHannes Frederic Sowa }
3154622c81d5SHannes Frederic Sowa 
3155cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3156cc9da6ccSBjørn Mork {
3157cc9da6ccSBjørn Mork 	struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3158cc9da6ccSBjørn Mork 
3159cc9da6ccSBjørn Mork 	if (s->initialized)
3160cc9da6ccSBjørn Mork 		return;
3161cc9da6ccSBjørn Mork 	s = &idev->cnf.stable_secret;
3162cc9da6ccSBjørn Mork 	get_random_bytes(&s->secret, sizeof(s->secret));
3163cc9da6ccSBjørn Mork 	s->initialized = true;
3164cc9da6ccSBjørn Mork }
3165cc9da6ccSBjørn Mork 
3166bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3167bc91b0f0SJiri Pirko {
3168bc91b0f0SJiri Pirko 	struct in6_addr addr;
3169bc91b0f0SJiri Pirko 
3170ca254490SDavid Ahern 	/* no link local addresses on L3 master devices */
3171ca254490SDavid Ahern 	if (netif_is_l3_master(idev->dev))
3172ca254490SDavid Ahern 		return;
3173ca254490SDavid Ahern 
3174bc91b0f0SJiri Pirko 	ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3175622c81d5SHannes Frederic Sowa 
3176d35a00b8SFelix Jia 	switch (idev->cnf.addr_gen_mode) {
3177cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_RANDOM:
3178cc9da6ccSBjørn Mork 		ipv6_gen_mode_random_init(idev);
3179cc9da6ccSBjørn Mork 		/* fallthrough */
3180cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3181622c81d5SHannes Frederic Sowa 		if (!ipv6_generate_stable_address(&addr, 0, idev))
318264236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr,
318364236f3fSHannes Frederic Sowa 					       IFA_F_STABLE_PRIVACY);
3184622c81d5SHannes Frederic Sowa 		else if (prefix_route)
3185622c81d5SHannes Frederic Sowa 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3186cc9da6ccSBjørn Mork 		break;
3187cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_EUI64:
3188bc91b0f0SJiri Pirko 		/* addrconf_add_linklocal also adds a prefix_route and we
3189bc91b0f0SJiri Pirko 		 * only need to care about prefix routes if ipv6_generate_eui64
3190bc91b0f0SJiri Pirko 		 * couldn't generate one.
3191bc91b0f0SJiri Pirko 		 */
3192bc91b0f0SJiri Pirko 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
319364236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr, 0);
3194bc91b0f0SJiri Pirko 		else if (prefix_route)
3195bc91b0f0SJiri Pirko 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3196cc9da6ccSBjørn Mork 		break;
3197cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_NONE:
3198cc9da6ccSBjørn Mork 	default:
3199cc9da6ccSBjørn Mork 		/* will not add any link local address */
3200cc9da6ccSBjørn Mork 		break;
3201bc91b0f0SJiri Pirko 	}
3202bc91b0f0SJiri Pirko }
3203bc91b0f0SJiri Pirko 
32041da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
32051da177e4SLinus Torvalds {
32061da177e4SLinus Torvalds 	struct inet6_dev *idev;
32071da177e4SLinus Torvalds 
32081da177e4SLinus Torvalds 	ASSERT_RTNL();
32091da177e4SLinus Torvalds 
321074235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
321174235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
321274235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
321306a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
3214e837735eSNicolas Dichtel 	    (dev->type != ARPHRD_IEEE1394) &&
3215e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_TUNNEL6) &&
3216cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_6LOWPAN) &&
321745ce0fd1SFelix Jia 	    (dev->type != ARPHRD_IP6GRE) &&
321845ce0fd1SFelix Jia 	    (dev->type != ARPHRD_IPGRE) &&
321945ce0fd1SFelix Jia 	    (dev->type != ARPHRD_TUNNEL) &&
3220cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_NONE)) {
322174235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
322274235a25SHerbert Xu 		return;
322374235a25SHerbert Xu 	}
322474235a25SHerbert Xu 
32251da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
322664e724f6SBrian Haley 	if (IS_ERR(idev))
32271da177e4SLinus Torvalds 		return;
32281da177e4SLinus Torvalds 
3229cc9da6ccSBjørn Mork 	/* this device type has no EUI support */
3230cc9da6ccSBjørn Mork 	if (dev->type == ARPHRD_NONE &&
3231d35a00b8SFelix Jia 	    idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3232d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3233cc9da6ccSBjørn Mork 
3234bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, false);
32351da177e4SLinus Torvalds }
32361da177e4SLinus Torvalds 
323707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
32381da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
32391da177e4SLinus Torvalds {
32401da177e4SLinus Torvalds 	struct inet6_dev *idev;
32411da177e4SLinus Torvalds 
32421da177e4SLinus Torvalds 	ASSERT_RTNL();
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 	/*
32451da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
32461da177e4SLinus Torvalds 	 * addresses... we should configure all of
32471da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
32481da177e4SLinus Torvalds 	 */
32491da177e4SLinus Torvalds 
3250e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
325163159f29SIan Morris 	if (!idev) {
325291df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
32531da177e4SLinus Torvalds 		return;
32541da177e4SLinus Torvalds 	}
32551da177e4SLinus Torvalds 
3256c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
3257bc91b0f0SJiri Pirko 		addrconf_addr_gen(idev, false);
3258c7dc89c0SFred L. Templin 		return;
3259c7dc89c0SFred L. Templin 	}
3260c7dc89c0SFred L. Templin 
32611da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
32621da177e4SLinus Torvalds 
326362b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
32641da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
32651da177e4SLinus Torvalds }
32660be669bbSJoerg Roedel #endif
32671da177e4SLinus Torvalds 
326807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3269aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
3270aee80b54Sstephen hemminger {
3271aee80b54Sstephen hemminger 	struct inet6_dev *idev;
3272aee80b54Sstephen hemminger 
3273aee80b54Sstephen hemminger 	ASSERT_RTNL();
3274aee80b54Sstephen hemminger 
3275e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
327663159f29SIan Morris 	if (!idev) {
327791df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
3278aee80b54Sstephen hemminger 		return;
3279aee80b54Sstephen hemminger 	}
3280aee80b54Sstephen hemminger 
3281bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, true);
3282d9e4ce65SHannes Frederic Sowa 	if (dev->flags & IFF_POINTOPOINT)
3283d9e4ce65SHannes Frederic Sowa 		addrconf_add_mroute(dev);
3284aee80b54Sstephen hemminger }
3285aee80b54Sstephen hemminger #endif
3286aee80b54Sstephen hemminger 
3287f1705ec1SDavid Ahern static int fixup_permanent_addr(struct inet6_dev *idev,
3288f1705ec1SDavid Ahern 				struct inet6_ifaddr *ifp)
3289f1705ec1SDavid Ahern {
3290fc882fcfSIdo Schimmel 	/* !rt6i_node means the host route was removed from the
32918048ced9SDavid Ahern 	 * FIB, for example, if 'lo' device is taken down. In that
32928048ced9SDavid Ahern 	 * case regenerate the host route.
32938048ced9SDavid Ahern 	 */
3294fc882fcfSIdo Schimmel 	if (!ifp->rt || !ifp->rt->rt6i_node) {
32958048ced9SDavid Ahern 		struct rt6_info *rt, *prev;
3296f1705ec1SDavid Ahern 
3297f1705ec1SDavid Ahern 		rt = addrconf_dst_alloc(idev, &ifp->addr, false);
329863a4e80bSTobias Klauser 		if (IS_ERR(rt))
3299f1705ec1SDavid Ahern 			return PTR_ERR(rt);
3300f1705ec1SDavid Ahern 
33018048ced9SDavid Ahern 		/* ifp->rt can be accessed outside of rtnl */
33028048ced9SDavid Ahern 		spin_lock(&ifp->lock);
33038048ced9SDavid Ahern 		prev = ifp->rt;
3304f1705ec1SDavid Ahern 		ifp->rt = rt;
33058048ced9SDavid Ahern 		spin_unlock(&ifp->lock);
33068048ced9SDavid Ahern 
33078048ced9SDavid Ahern 		ip6_rt_put(prev);
3308f1705ec1SDavid Ahern 	}
3309f1705ec1SDavid Ahern 
3310f1705ec1SDavid Ahern 	if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3311f1705ec1SDavid Ahern 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3312f1705ec1SDavid Ahern 				      idev->dev, 0, 0);
3313f1705ec1SDavid Ahern 	}
3314f1705ec1SDavid Ahern 
33156d717134SDavid Ahern 	if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3316f1705ec1SDavid Ahern 		addrconf_dad_start(ifp);
3317f1705ec1SDavid Ahern 
3318f1705ec1SDavid Ahern 	return 0;
3319f1705ec1SDavid Ahern }
3320f1705ec1SDavid Ahern 
3321f1705ec1SDavid Ahern static void addrconf_permanent_addr(struct net_device *dev)
3322f1705ec1SDavid Ahern {
3323f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifp, *tmp;
3324f1705ec1SDavid Ahern 	struct inet6_dev *idev;
3325f1705ec1SDavid Ahern 
3326f1705ec1SDavid Ahern 	idev = __in6_dev_get(dev);
3327f1705ec1SDavid Ahern 	if (!idev)
3328f1705ec1SDavid Ahern 		return;
3329f1705ec1SDavid Ahern 
3330f1705ec1SDavid Ahern 	write_lock_bh(&idev->lock);
3331f1705ec1SDavid Ahern 
3332f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3333f1705ec1SDavid Ahern 		if ((ifp->flags & IFA_F_PERMANENT) &&
3334f1705ec1SDavid Ahern 		    fixup_permanent_addr(idev, ifp) < 0) {
3335f1705ec1SDavid Ahern 			write_unlock_bh(&idev->lock);
3336f1705ec1SDavid Ahern 			ipv6_del_addr(ifp);
3337f1705ec1SDavid Ahern 			write_lock_bh(&idev->lock);
3338f1705ec1SDavid Ahern 
3339f1705ec1SDavid Ahern 			net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3340f1705ec1SDavid Ahern 					     idev->dev->name, &ifp->addr);
3341f1705ec1SDavid Ahern 		}
3342f1705ec1SDavid Ahern 	}
3343f1705ec1SDavid Ahern 
3344f1705ec1SDavid Ahern 	write_unlock_bh(&idev->lock);
3345f1705ec1SDavid Ahern }
3346f1705ec1SDavid Ahern 
33471da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
3348351638e7SJiri Pirko 			   void *ptr)
33491da177e4SLinus Torvalds {
3350351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
33514f7f34eaSDavid Ahern 	struct netdev_notifier_changeupper_info *info;
3352748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
335360abc0beSWANG Cong 	struct net *net = dev_net(dev);
3354c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
3355b217d616SHerbert Xu 	int err;
33561da177e4SLinus Torvalds 
33571da177e4SLinus Torvalds 	switch (event) {
335845ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
335974235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
336045ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
3361a317a2f1SWANG Cong 			if (IS_ERR(idev))
3362a317a2f1SWANG Cong 				return notifier_from_errno(PTR_ERR(idev));
336345ba9dd2SYOSHIFUJI Hideaki 		}
336445ba9dd2SYOSHIFUJI Hideaki 		break;
3365bcdd553fSStephen Hemminger 
3366b7b0b1d2SAlexander Duyck 	case NETDEV_CHANGEMTU:
3367b7b0b1d2SAlexander Duyck 		/* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3368b7b0b1d2SAlexander Duyck 		if (dev->mtu < IPV6_MIN_MTU) {
336960abc0beSWANG Cong 			addrconf_ifdown(dev, dev != net->loopback_dev);
3370b7b0b1d2SAlexander Duyck 			break;
3371b7b0b1d2SAlexander Duyck 		}
3372b7b0b1d2SAlexander Duyck 
3373b7b0b1d2SAlexander Duyck 		if (idev) {
3374b7b0b1d2SAlexander Duyck 			rt6_mtu_change(dev, dev->mtu);
3375b7b0b1d2SAlexander Duyck 			idev->cnf.mtu6 = dev->mtu;
3376b7b0b1d2SAlexander Duyck 			break;
3377b7b0b1d2SAlexander Duyck 		}
3378b7b0b1d2SAlexander Duyck 
3379b7b0b1d2SAlexander Duyck 		/* allocate new idev */
3380b7b0b1d2SAlexander Duyck 		idev = ipv6_add_dev(dev);
3381b7b0b1d2SAlexander Duyck 		if (IS_ERR(idev))
3382b7b0b1d2SAlexander Duyck 			break;
3383b7b0b1d2SAlexander Duyck 
3384b7b0b1d2SAlexander Duyck 		/* device is still not ready */
3385b7b0b1d2SAlexander Duyck 		if (!(idev->if_flags & IF_READY))
3386b7b0b1d2SAlexander Duyck 			break;
3387b7b0b1d2SAlexander Duyck 
3388b7b0b1d2SAlexander Duyck 		run_pending = 1;
3389b7b0b1d2SAlexander Duyck 
3390b7b0b1d2SAlexander Duyck 		/* fall through */
3391b7b0b1d2SAlexander Duyck 
33921da177e4SLinus Torvalds 	case NETDEV_UP:
33933c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
3394c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
3395c2edacf8SJay Vosburgh 			break;
3396c2edacf8SJay Vosburgh 
33973ce62a84SWANG Cong 		if (idev && idev->cnf.disable_ipv6)
33983ce62a84SWANG Cong 			break;
33993ce62a84SWANG Cong 
34003c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
340138bd10c4SDavid Ahern 			/* restore routes for permanent addresses */
340238bd10c4SDavid Ahern 			addrconf_permanent_addr(dev);
340338bd10c4SDavid Ahern 
34041f372c7bSMike Manning 			if (!addrconf_link_ready(dev)) {
34053c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
3406f3213831SJoe Perches 				pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
34073c21edbdSYOSHIFUJI Hideaki 					dev->name);
34083c21edbdSYOSHIFUJI Hideaki 				break;
34093c21edbdSYOSHIFUJI Hideaki 			}
341099081049SKristian Slavov 
3411d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
3412d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
3413d31c7b8fSEvgeniy Polyakov 
3414a317a2f1SWANG Cong 			if (!IS_ERR_OR_NULL(idev)) {
341599081049SKristian Slavov 				idev->if_flags |= IF_READY;
3416e3ec6cfcSBenjamin Thery 				run_pending = 1;
3417e3ec6cfcSBenjamin Thery 			}
3418b7b0b1d2SAlexander Duyck 		} else if (event == NETDEV_CHANGE) {
34191f372c7bSMike Manning 			if (!addrconf_link_ready(dev)) {
34203c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
34213c21edbdSYOSHIFUJI Hideaki 				break;
34223c21edbdSYOSHIFUJI Hideaki 			}
34233c21edbdSYOSHIFUJI Hideaki 
34243c21edbdSYOSHIFUJI Hideaki 			if (idev) {
3425a088d1d7SLinus Lüssing 				if (idev->if_flags & IF_READY) {
3426a088d1d7SLinus Lüssing 					/* device is already configured -
3427a088d1d7SLinus Lüssing 					 * but resend MLD reports, we might
3428a088d1d7SLinus Lüssing 					 * have roamed and need to update
3429a088d1d7SLinus Lüssing 					 * multicast snooping switches
3430a088d1d7SLinus Lüssing 					 */
3431a088d1d7SLinus Lüssing 					ipv6_mc_up(idev);
34323c21edbdSYOSHIFUJI Hideaki 					break;
3433a088d1d7SLinus Lüssing 				}
34343c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
34353c21edbdSYOSHIFUJI Hideaki 			}
34363c21edbdSYOSHIFUJI Hideaki 
3437f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
34383c21edbdSYOSHIFUJI Hideaki 				dev->name);
34393c21edbdSYOSHIFUJI Hideaki 
3440c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
34413c21edbdSYOSHIFUJI Hideaki 		}
34423c21edbdSYOSHIFUJI Hideaki 
34431da177e4SLinus Torvalds 		switch (dev->type) {
344407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
34451da177e4SLinus Torvalds 		case ARPHRD_SIT:
34461da177e4SLinus Torvalds 			addrconf_sit_config(dev);
34471da177e4SLinus Torvalds 			break;
34480be669bbSJoerg Roedel #endif
344907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3450aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
3451aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
3452aee80b54Sstephen hemminger 			break;
3453aee80b54Sstephen hemminger #endif
34541da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
34551da177e4SLinus Torvalds 			init_loopback(dev);
34561da177e4SLinus Torvalds 			break;
34571da177e4SLinus Torvalds 
34581da177e4SLinus Torvalds 		default:
34591da177e4SLinus Torvalds 			addrconf_dev_config(dev);
34601da177e4SLinus Torvalds 			break;
34613ff50b79SStephen Hemminger 		}
3462bcdd553fSStephen Hemminger 
3463a317a2f1SWANG Cong 		if (!IS_ERR_OR_NULL(idev)) {
3464c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
3465c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
3466c5e33bddSYOSHIFUJI Hideaki 
3467bcdd553fSStephen Hemminger 			/*
3468bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
3469bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
3470bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
34711da177e4SLinus Torvalds 			 */
3472bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
3473bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
34741da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
34751da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
34761da177e4SLinus Torvalds 			}
34771da177e4SLinus Torvalds 			idev->tstamp = jiffies;
34781da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
3479bcdd553fSStephen Hemminger 
3480bcdd553fSStephen Hemminger 			/*
3481bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
3482bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
34831da177e4SLinus Torvalds 			 */
34841da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
348560abc0beSWANG Cong 				addrconf_ifdown(dev, dev != net->loopback_dev);
34861da177e4SLinus Torvalds 		}
34871da177e4SLinus Torvalds 		break;
34881da177e4SLinus Torvalds 
34891da177e4SLinus Torvalds 	case NETDEV_DOWN:
34901da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
34911da177e4SLinus Torvalds 		/*
34921da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
34931da177e4SLinus Torvalds 		 */
34941da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
34951da177e4SLinus Torvalds 		break;
34963c21edbdSYOSHIFUJI Hideaki 
34971da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
34981da177e4SLinus Torvalds 		if (idev) {
34995632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
3500408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
3501a317a2f1SWANG Cong 			err = addrconf_sysctl_register(idev);
3502b217d616SHerbert Xu 			if (err)
3503b217d616SHerbert Xu 				return notifier_from_errno(err);
3504a317a2f1SWANG Cong 			err = snmp6_register_dev(idev);
3505a317a2f1SWANG Cong 			if (err) {
3506a317a2f1SWANG Cong 				addrconf_sysctl_unregister(idev);
3507a317a2f1SWANG Cong 				return notifier_from_errno(err);
3508a317a2f1SWANG Cong 			}
35095632c515SStephen Hemminger 		}
35101da177e4SLinus Torvalds 		break;
3511bcdd553fSStephen Hemminger 
351293d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
351393d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
35143ef0952cSAndrew Lunn 		if (idev)
351593d9b7d7SJiri Pirko 			addrconf_type_change(dev, event);
351675c78500SMoni Shoua 		break;
35174f7f34eaSDavid Ahern 
35184f7f34eaSDavid Ahern 	case NETDEV_CHANGEUPPER:
35194f7f34eaSDavid Ahern 		info = ptr;
35204f7f34eaSDavid Ahern 
35214f7f34eaSDavid Ahern 		/* flush all routes if dev is linked to or unlinked from
35224f7f34eaSDavid Ahern 		 * an L3 master device (e.g., VRF)
35234f7f34eaSDavid Ahern 		 */
35244f7f34eaSDavid Ahern 		if (info->upper_dev && netif_is_l3_master(info->upper_dev))
35254f7f34eaSDavid Ahern 			addrconf_ifdown(dev, 0);
35263ff50b79SStephen Hemminger 	}
35271da177e4SLinus Torvalds 
35281da177e4SLinus Torvalds 	return NOTIFY_OK;
35291da177e4SLinus Torvalds }
35301da177e4SLinus Torvalds 
35311da177e4SLinus Torvalds /*
35321da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
35331da177e4SLinus Torvalds  */
35341da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
35351da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
3536242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY,
35371da177e4SLinus Torvalds };
35381da177e4SLinus Torvalds 
353993d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
354075c78500SMoni Shoua {
354175c78500SMoni Shoua 	struct inet6_dev *idev;
354275c78500SMoni Shoua 	ASSERT_RTNL();
354375c78500SMoni Shoua 
354475c78500SMoni Shoua 	idev = __in6_dev_get(dev);
354575c78500SMoni Shoua 
354693d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
354775c78500SMoni Shoua 		ipv6_mc_remap(idev);
354893d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
354975c78500SMoni Shoua 		ipv6_mc_unmap(idev);
355075c78500SMoni Shoua }
355175c78500SMoni Shoua 
355270af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr)
355370af921dSDavid Ahern {
355470af921dSDavid Ahern 	return ipv6_addr_type(addr) &
355570af921dSDavid Ahern 		(IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
355670af921dSDavid Ahern }
355770af921dSDavid Ahern 
35581da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
35591da177e4SLinus Torvalds {
3560c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
3561502a2ffdSstephen hemminger 	struct inet6_dev *idev;
3562f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifa, *tmp;
3563f1705ec1SDavid Ahern 	int _keep_addr;
3564f1705ec1SDavid Ahern 	bool keep_addr;
356573a8bd74SDavid S. Miller 	int state, i;
35661da177e4SLinus Torvalds 
35671da177e4SLinus Torvalds 	ASSERT_RTNL();
35681da177e4SLinus Torvalds 
3569f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
357073a8bd74SDavid S. Miller 	neigh_ifdown(&nd_tbl, dev);
35711da177e4SLinus Torvalds 
35721da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
357363159f29SIan Morris 	if (!idev)
35741da177e4SLinus Torvalds 		return -ENODEV;
35751da177e4SLinus Torvalds 
3576bcdd553fSStephen Hemminger 	/*
3577bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3578bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
35791da177e4SLinus Torvalds 	 */
3580439e2385SDenis V. Lunev 	if (how) {
35811da177e4SLinus Torvalds 		idev->dead = 1;
35828814c4b5SYOSHIFUJI Hideaki 
35838814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3584a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
35851da177e4SLinus Torvalds 
35861da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
35871da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
35881da177e4SLinus Torvalds 
35891da177e4SLinus Torvalds 	}
35901da177e4SLinus Torvalds 
3591f1705ec1SDavid Ahern 	/* aggregate the system setting and interface setting */
3592f1705ec1SDavid Ahern 	_keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3593f1705ec1SDavid Ahern 	if (!_keep_addr)
3594f1705ec1SDavid Ahern 		_keep_addr = idev->cnf.keep_addr_on_down;
3595f1705ec1SDavid Ahern 
3596f1705ec1SDavid Ahern 	/* combine the user config with event to determine if permanent
3597f1705ec1SDavid Ahern 	 * addresses are to be removed from address hash table
3598f1705ec1SDavid Ahern 	 */
3599bc561632SMike Manning 	keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
3600f1705ec1SDavid Ahern 
360173a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
360273a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
360373a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
360473a8bd74SDavid S. Miller 
360573a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
360673a8bd74SDavid S. Miller restart:
3607b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
360873a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
3609c15b1ccaSHannes Frederic Sowa 				addrconf_del_dad_work(ifa);
3610f1705ec1SDavid Ahern 				/* combined flag + permanent flag decide if
3611f1705ec1SDavid Ahern 				 * address is retained on a down event
3612f1705ec1SDavid Ahern 				 */
3613f1705ec1SDavid Ahern 				if (!keep_addr ||
361470af921dSDavid Ahern 				    !(ifa->flags & IFA_F_PERMANENT) ||
361570af921dSDavid Ahern 				    addr_is_local(&ifa->addr)) {
3616f1705ec1SDavid Ahern 					hlist_del_init_rcu(&ifa->addr_lst);
361773a8bd74SDavid S. Miller 					goto restart;
361873a8bd74SDavid S. Miller 				}
361973a8bd74SDavid S. Miller 			}
3620f1705ec1SDavid Ahern 		}
362173a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
362273a8bd74SDavid S. Miller 	}
362373a8bd74SDavid S. Miller 
36241da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
36251da177e4SLinus Torvalds 
3626b7b1bfceSHannes Frederic Sowa 	addrconf_del_rs_timer(idev);
3627b7b1bfceSHannes Frederic Sowa 
3628bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3629439e2385SDenis V. Lunev 	if (!how)
36303c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
36311da177e4SLinus Torvalds 
3632bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3633372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3634372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3635372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3636372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
36371da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
36381da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
36391da177e4SLinus Torvalds 
36401da177e4SLinus Torvalds 		if (ifa->ifpub) {
36411da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
36421da177e4SLinus Torvalds 			ifa->ifpub = NULL;
36431da177e4SLinus Torvalds 		}
36441da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
36451da177e4SLinus Torvalds 		in6_ifa_put(ifa);
36461da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
36471da177e4SLinus Torvalds 	}
36488f37ada5Sstephen hemminger 
3649f1705ec1SDavid Ahern 	/* re-combine the user config with event to determine if permanent
3650f1705ec1SDavid Ahern 	 * addresses are to be removed from the interface list
3651f1705ec1SDavid Ahern 	 */
3652bc561632SMike Manning 	keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
3653f1705ec1SDavid Ahern 
3654f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
365538bd10c4SDavid Ahern 		struct rt6_info *rt = NULL;
3656a2d6cbb0SRabin Vincent 		bool keep;
365738bd10c4SDavid Ahern 
3658c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifa);
365984e8b803Sstephen hemminger 
3660a2d6cbb0SRabin Vincent 		keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3661a2d6cbb0SRabin Vincent 			!addr_is_local(&ifa->addr);
3662a2d6cbb0SRabin Vincent 
36634c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
36648e8e676dSHannes Frederic Sowa 		spin_lock_bh(&ifa->lock);
3665f1705ec1SDavid Ahern 
3666a2d6cbb0SRabin Vincent 		if (keep) {
3667f1705ec1SDavid Ahern 			/* set state to skip the notifier below */
3668f1705ec1SDavid Ahern 			state = INET6_IFADDR_STATE_DEAD;
36696d717134SDavid Ahern 			ifa->state = INET6_IFADDR_STATE_PREDAD;
3670f1705ec1SDavid Ahern 			if (!(ifa->flags & IFA_F_NODAD))
3671f1705ec1SDavid Ahern 				ifa->flags |= IFA_F_TENTATIVE;
367238bd10c4SDavid Ahern 
367338bd10c4SDavid Ahern 			rt = ifa->rt;
367438bd10c4SDavid Ahern 			ifa->rt = NULL;
3675f1705ec1SDavid Ahern 		} else {
36764c5ff6a6SHerbert Xu 			state = ifa->state;
36774c5ff6a6SHerbert Xu 			ifa->state = INET6_IFADDR_STATE_DEAD;
3678f1705ec1SDavid Ahern 		}
3679f1705ec1SDavid Ahern 
36808e8e676dSHannes Frederic Sowa 		spin_unlock_bh(&ifa->lock);
36814c5ff6a6SHerbert Xu 
368238bd10c4SDavid Ahern 		if (rt)
368338bd10c4SDavid Ahern 			ip6_del_rt(rt);
368438bd10c4SDavid Ahern 
36859d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
36861da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
3687f88c91ddSCong Wang 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3688ea06f717SMike Manning 		} else {
3689ea06f717SMike Manning 			if (idev->cnf.forwarding)
3690ea06f717SMike Manning 				addrconf_leave_anycast(ifa);
3691ea06f717SMike Manning 			addrconf_leave_solict(ifa->idev, &ifa->addr);
36922de79570SLorenzo Colitti 		}
369373a8bd74SDavid S. Miller 
36941da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
36958ef802aaSEric Dumazet 		if (!keep) {
36968ef802aaSEric Dumazet 			list_del_rcu(&ifa->if_list);
36978ef802aaSEric Dumazet 			in6_ifa_put(ifa);
36988ef802aaSEric Dumazet 		}
36991da177e4SLinus Torvalds 	}
37008f37ada5Sstephen hemminger 
37011da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
37021da177e4SLinus Torvalds 
3703381f4dcaSSabrina Dubroca 	/* Step 5: Discard anycast and multicast list */
3704381f4dcaSSabrina Dubroca 	if (how) {
3705381f4dcaSSabrina Dubroca 		ipv6_ac_destroy_dev(idev);
37061da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
3707381f4dcaSSabrina Dubroca 	} else {
37081da177e4SLinus Torvalds 		ipv6_mc_down(idev);
3709381f4dcaSSabrina Dubroca 	}
37101da177e4SLinus Torvalds 
37111da177e4SLinus Torvalds 	idev->tstamp = jiffies;
37121da177e4SLinus Torvalds 
3713bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3714439e2385SDenis V. Lunev 	if (how) {
3715408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
37161da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
37171da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
37181da177e4SLinus Torvalds 		in6_dev_put(idev);
37191da177e4SLinus Torvalds 	}
37201da177e4SLinus Torvalds 	return 0;
37211da177e4SLinus Torvalds }
37221da177e4SLinus Torvalds 
37231da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
37241da177e4SLinus Torvalds {
3725b7b1bfceSHannes Frederic Sowa 	struct inet6_dev *idev = (struct inet6_dev *)data;
3726caf92bc4SCong Wang 	struct net_device *dev = idev->dev;
3727b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
37281da177e4SLinus Torvalds 
3729b7b1bfceSHannes Frederic Sowa 	write_lock(&idev->lock);
37305b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
37311da177e4SLinus Torvalds 		goto out;
37321da177e4SLinus Torvalds 
37339ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
37341da177e4SLinus Torvalds 		goto out;
37355b2a1953Sstephen hemminger 
37365b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
37375b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
37385b2a1953Sstephen hemminger 		goto out;
37391da177e4SLinus Torvalds 
3740bd11f074SMaciej Żenczykowski 	if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3741caf92bc4SCong Wang 		write_unlock(&idev->lock);
3742caf92bc4SCong Wang 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3743caf92bc4SCong Wang 			ndisc_send_rs(dev, &lladdr,
3744b7b1bfceSHannes Frederic Sowa 				      &in6addr_linklocal_allrouters);
3745b7b1bfceSHannes Frederic Sowa 		else
3746caf92bc4SCong Wang 			goto put;
3747b7b1bfceSHannes Frederic Sowa 
3748caf92bc4SCong Wang 		write_lock(&idev->lock);
3749bd11f074SMaciej Żenczykowski 		idev->rs_interval = rfc3315_s14_backoff_update(
3750bd11f074SMaciej Żenczykowski 			idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
37511da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
3752b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3753b7b1bfceSHannes Frederic Sowa 					     idev->cnf.rtr_solicits) ?
37545b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_delay :
3755bd11f074SMaciej Żenczykowski 				      idev->rs_interval);
37561da177e4SLinus Torvalds 	} else {
37571da177e4SLinus Torvalds 		/*
37581da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
37591da177e4SLinus Torvalds 		 * assumption any longer.
37601da177e4SLinus Torvalds 		 */
376191df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
37621da177e4SLinus Torvalds 	}
37631da177e4SLinus Torvalds 
37641da177e4SLinus Torvalds out:
3765b7b1bfceSHannes Frederic Sowa 	write_unlock(&idev->lock);
3766caf92bc4SCong Wang put:
3767b7b1bfceSHannes Frederic Sowa 	in6_dev_put(idev);
37681da177e4SLinus Torvalds }
37691da177e4SLinus Torvalds 
37701da177e4SLinus Torvalds /*
37711da177e4SLinus Torvalds  *	Duplicate Address Detection
37721da177e4SLinus Torvalds  */
37733c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
37743c21edbdSYOSHIFUJI Hideaki {
37753c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
37763c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
3777adc176c5SErik Nordmark 	u64 nonce;
37783c21edbdSYOSHIFUJI Hideaki 
377995c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
378095c385b4SNeil Horman 		rand_num = 0;
378195c385b4SNeil Horman 	else
378263862b5bSAruna-Hewapathirane 		rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
378395c385b4SNeil Horman 
3784adc176c5SErik Nordmark 	nonce = 0;
3785adc176c5SErik Nordmark 	if (idev->cnf.enhanced_dad ||
3786adc176c5SErik Nordmark 	    dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3787adc176c5SErik Nordmark 		do
3788adc176c5SErik Nordmark 			get_random_bytes(&nonce, 6);
3789adc176c5SErik Nordmark 		while (nonce == 0);
3790adc176c5SErik Nordmark 	}
3791adc176c5SErik Nordmark 	ifp->dad_nonce = nonce;
3792b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes = idev->cnf.dad_transmits;
3793c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, rand_num);
37943c21edbdSYOSHIFUJI Hideaki }
37953c21edbdSYOSHIFUJI Hideaki 
3796c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
37971da177e4SLinus Torvalds {
37981da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
37991da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3800764d3be6SPaolo Abeni 	bool bump_id, notify = false;
38011da177e4SLinus Torvalds 
38021da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
38031da177e4SLinus Torvalds 
380463862b5bSAruna-Hewapathirane 	prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
38051da177e4SLinus Torvalds 
38061da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3807622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3808e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
38091da177e4SLinus Torvalds 		goto out;
38101da177e4SLinus Torvalds 
38111da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
381235e015e1SMatteo Croce 	    dev_net(dev)->ipv6.devconf_all->accept_dad < 1 ||
38131b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
381455ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
381555ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3816764d3be6SPaolo Abeni 		bump_id = ifp->flags & IFA_F_TENTATIVE;
3817cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
381821809fafSstephen hemminger 		spin_unlock(&ifp->lock);
38191da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
38201da177e4SLinus Torvalds 
3821764d3be6SPaolo Abeni 		addrconf_dad_completed(ifp, bump_id);
38221da177e4SLinus Torvalds 		return;
38231da177e4SLinus Torvalds 	}
38241da177e4SLinus Torvalds 
38256732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
382621809fafSstephen hemminger 		spin_unlock(&ifp->lock);
38276732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
38283c21edbdSYOSHIFUJI Hideaki 		/*
3829590a9887SMasatake YAMATO 		 * If the device is not ready:
38303c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
38313c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
38323c21edbdSYOSHIFUJI Hideaki 		 */
38333c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
3834cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
38356732badeSYOSHIFUJI Hideaki 		return;
38363c21edbdSYOSHIFUJI Hideaki 	}
383795c385b4SNeil Horman 
383895c385b4SNeil Horman 	/*
383995c385b4SNeil Horman 	 * Optimistic nodes can start receiving
384095c385b4SNeil Horman 	 * Frames right away
384195c385b4SNeil Horman 	 */
38427fd2561eSErik Kline 	if (ifp->flags & IFA_F_OPTIMISTIC) {
384395c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
384435e015e1SMatteo Croce 		if (ipv6_use_optimistic_addr(dev_net(dev), idev)) {
38457fd2561eSErik Kline 			/* Because optimistic nodes can use this address,
38467fd2561eSErik Kline 			 * notify listeners. If DAD fails, RTM_DELADDR is sent.
38477fd2561eSErik Kline 			 */
384816186a82Ssubashab@codeaurora.org 			notify = true;
38497fd2561eSErik Kline 		}
38507fd2561eSErik Kline 	}
385195c385b4SNeil Horman 
38526732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
38531da177e4SLinus Torvalds out:
3854622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
38551da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
385616186a82Ssubashab@codeaurora.org 	if (notify)
385716186a82Ssubashab@codeaurora.org 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
38581da177e4SLinus Torvalds }
38591da177e4SLinus Torvalds 
3860c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp)
38611da177e4SLinus Torvalds {
3862c15b1ccaSHannes Frederic Sowa 	bool begin_dad = false;
3863c15b1ccaSHannes Frederic Sowa 
38648e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3865c15b1ccaSHannes Frederic Sowa 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3866c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_PREDAD;
3867c15b1ccaSHannes Frederic Sowa 		begin_dad = true;
3868c15b1ccaSHannes Frederic Sowa 	}
38698e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3870c15b1ccaSHannes Frederic Sowa 
3871c15b1ccaSHannes Frederic Sowa 	if (begin_dad)
3872c15b1ccaSHannes Frederic Sowa 		addrconf_mod_dad_work(ifp, 0);
3873c15b1ccaSHannes Frederic Sowa }
3874c15b1ccaSHannes Frederic Sowa 
3875c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w)
3876c15b1ccaSHannes Frederic Sowa {
3877c15b1ccaSHannes Frederic Sowa 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3878c15b1ccaSHannes Frederic Sowa 						struct inet6_ifaddr,
3879c15b1ccaSHannes Frederic Sowa 						dad_work);
38801da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
3881764d3be6SPaolo Abeni 	bool bump_id, disable_ipv6 = false;
38821da177e4SLinus Torvalds 	struct in6_addr mcaddr;
38831da177e4SLinus Torvalds 
3884c15b1ccaSHannes Frederic Sowa 	enum {
3885c15b1ccaSHannes Frederic Sowa 		DAD_PROCESS,
3886c15b1ccaSHannes Frederic Sowa 		DAD_BEGIN,
3887c15b1ccaSHannes Frederic Sowa 		DAD_ABORT,
3888c15b1ccaSHannes Frederic Sowa 	} action = DAD_PROCESS;
3889c15b1ccaSHannes Frederic Sowa 
3890c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
3891c15b1ccaSHannes Frederic Sowa 
38928e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3893c15b1ccaSHannes Frederic Sowa 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3894c15b1ccaSHannes Frederic Sowa 		action = DAD_BEGIN;
3895c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_DAD;
3896c15b1ccaSHannes Frederic Sowa 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3897c15b1ccaSHannes Frederic Sowa 		action = DAD_ABORT;
3898c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
389985b51b12SMike Manning 
390035e015e1SMatteo Croce 		if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
390135e015e1SMatteo Croce 		     idev->cnf.accept_dad > 1) &&
390235e015e1SMatteo Croce 		    !idev->cnf.disable_ipv6 &&
390385b51b12SMike Manning 		    !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
390485b51b12SMike Manning 			struct in6_addr addr;
390585b51b12SMike Manning 
390685b51b12SMike Manning 			addr.s6_addr32[0] = htonl(0xfe800000);
390785b51b12SMike Manning 			addr.s6_addr32[1] = 0;
390885b51b12SMike Manning 
390985b51b12SMike Manning 			if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
391085b51b12SMike Manning 			    ipv6_addr_equal(&ifp->addr, &addr)) {
391185b51b12SMike Manning 				/* DAD failed for link-local based on MAC */
391285b51b12SMike Manning 				idev->cnf.disable_ipv6 = 1;
391385b51b12SMike Manning 
391485b51b12SMike Manning 				pr_info("%s: IPv6 being disabled!\n",
391585b51b12SMike Manning 					ifp->idev->dev->name);
391685b51b12SMike Manning 				disable_ipv6 = true;
391785b51b12SMike Manning 			}
391885b51b12SMike Manning 		}
3919c15b1ccaSHannes Frederic Sowa 	}
39208e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3921c15b1ccaSHannes Frederic Sowa 
3922c15b1ccaSHannes Frederic Sowa 	if (action == DAD_BEGIN) {
3923c15b1ccaSHannes Frederic Sowa 		addrconf_dad_begin(ifp);
3924c15b1ccaSHannes Frederic Sowa 		goto out;
3925c15b1ccaSHannes Frederic Sowa 	} else if (action == DAD_ABORT) {
3926751eb6b6SWei Yongjun 		in6_ifa_hold(ifp);
3927c15b1ccaSHannes Frederic Sowa 		addrconf_dad_stop(ifp, 1);
392885b51b12SMike Manning 		if (disable_ipv6)
392985b51b12SMike Manning 			addrconf_ifdown(idev->dev, 0);
3930c15b1ccaSHannes Frederic Sowa 		goto out;
3931c15b1ccaSHannes Frederic Sowa 	}
3932c15b1ccaSHannes Frederic Sowa 
3933b7b1bfceSHannes Frederic Sowa 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
3934f2344a13SHerbert Xu 		goto out;
3935f2344a13SHerbert Xu 
3936c15b1ccaSHannes Frederic Sowa 	write_lock_bh(&idev->lock);
3937122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
3938c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
39391da177e4SLinus Torvalds 		goto out;
39401da177e4SLinus Torvalds 	}
394121809fafSstephen hemminger 
394221809fafSstephen hemminger 	spin_lock(&ifp->lock);
3943622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3944622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
3945c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
3946622ccdf1SHerbert Xu 		goto out;
3947622ccdf1SHerbert Xu 	}
3948622ccdf1SHerbert Xu 
3949b7b1bfceSHannes Frederic Sowa 	if (ifp->dad_probes == 0) {
39501da177e4SLinus Torvalds 		/*
39511da177e4SLinus Torvalds 		 * DAD was successful
39521da177e4SLinus Torvalds 		 */
39531da177e4SLinus Torvalds 
3954764d3be6SPaolo Abeni 		bump_id = ifp->flags & IFA_F_TENTATIVE;
3955cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
395621809fafSstephen hemminger 		spin_unlock(&ifp->lock);
3957c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
39581da177e4SLinus Torvalds 
3959764d3be6SPaolo Abeni 		addrconf_dad_completed(ifp, bump_id);
39601da177e4SLinus Torvalds 
39611da177e4SLinus Torvalds 		goto out;
39621da177e4SLinus Torvalds 	}
39631da177e4SLinus Torvalds 
3964b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes--;
3965c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp,
39661f9248e5SJiri Pirko 			      NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
396721809fafSstephen hemminger 	spin_unlock(&ifp->lock);
3968c15b1ccaSHannes Frederic Sowa 	write_unlock_bh(&idev->lock);
39691da177e4SLinus Torvalds 
39701da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
39711da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3972adc176c5SErik Nordmark 	ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
3973adc176c5SErik Nordmark 		      ifp->dad_nonce);
39741da177e4SLinus Torvalds out:
39751da177e4SLinus Torvalds 	in6_ifa_put(ifp);
3976c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
39771da177e4SLinus Torvalds }
39781da177e4SLinus Torvalds 
397911ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */
398011ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
398111ffff75SHannes Frederic Sowa {
398211ffff75SHannes Frederic Sowa 	struct inet6_ifaddr *ifpiter;
398311ffff75SHannes Frederic Sowa 	struct inet6_dev *idev = ifp->idev;
398411ffff75SHannes Frederic Sowa 
3985602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3986602582caSHannes Frederic Sowa 		if (ifpiter->scope > IFA_LINK)
3987602582caSHannes Frederic Sowa 			break;
398811ffff75SHannes Frederic Sowa 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
398911ffff75SHannes Frederic Sowa 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
399011ffff75SHannes Frederic Sowa 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
399111ffff75SHannes Frederic Sowa 		    IFA_F_PERMANENT)
399211ffff75SHannes Frederic Sowa 			return false;
399311ffff75SHannes Frederic Sowa 	}
399411ffff75SHannes Frederic Sowa 	return true;
399511ffff75SHannes Frederic Sowa }
399611ffff75SHannes Frederic Sowa 
3997764d3be6SPaolo Abeni static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
39981da177e4SLinus Torvalds {
39991da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
4000b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
4001b173ee48SHannes Frederic Sowa 	bool send_rs, send_mld;
4002b7b1bfceSHannes Frederic Sowa 
4003c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
40041da177e4SLinus Torvalds 
40051da177e4SLinus Torvalds 	/*
40061da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
40071da177e4SLinus Torvalds 	 */
40081da177e4SLinus Torvalds 
40091da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
40101da177e4SLinus Torvalds 
4011026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
4012026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
40131da177e4SLinus Torvalds 	 */
40141da177e4SLinus Torvalds 
40151ec047ebSHannes Frederic Sowa 	read_lock_bh(&ifp->idev->lock);
401611ffff75SHannes Frederic Sowa 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4017b173ee48SHannes Frederic Sowa 	send_rs = send_mld &&
4018b173ee48SHannes Frederic Sowa 		  ipv6_accept_ra(ifp->idev) &&
4019bd11f074SMaciej Żenczykowski 		  ifp->idev->cnf.rtr_solicits != 0 &&
4020b173ee48SHannes Frederic Sowa 		  (dev->flags&IFF_LOOPBACK) == 0;
40211ec047ebSHannes Frederic Sowa 	read_unlock_bh(&ifp->idev->lock);
40221ec047ebSHannes Frederic Sowa 
4023b173ee48SHannes Frederic Sowa 	/* While dad is in progress mld report's source address is in6_addrany.
4024b173ee48SHannes Frederic Sowa 	 * Resend with proper ll now.
4025b173ee48SHannes Frederic Sowa 	 */
4026b173ee48SHannes Frederic Sowa 	if (send_mld)
4027b173ee48SHannes Frederic Sowa 		ipv6_mc_dad_complete(ifp->idev);
4028b173ee48SHannes Frederic Sowa 
40291ec047ebSHannes Frederic Sowa 	if (send_rs) {
40301da177e4SLinus Torvalds 		/*
40311da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
40321da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
40331da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
40341da177e4SLinus Torvalds 		 */
40351ec047ebSHannes Frederic Sowa 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4036b7b1bfceSHannes Frederic Sowa 			return;
40371ec047ebSHannes Frederic Sowa 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
40381da177e4SLinus Torvalds 
4039b7b1bfceSHannes Frederic Sowa 		write_lock_bh(&ifp->idev->lock);
4040b7b1bfceSHannes Frederic Sowa 		spin_lock(&ifp->lock);
4041bd11f074SMaciej Żenczykowski 		ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4042bd11f074SMaciej Żenczykowski 			ifp->idev->cnf.rtr_solicit_interval);
4043b7b1bfceSHannes Frederic Sowa 		ifp->idev->rs_probes = 1;
40441da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
4045bd11f074SMaciej Żenczykowski 		addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4046b7b1bfceSHannes Frederic Sowa 		spin_unlock(&ifp->lock);
4047b7b1bfceSHannes Frederic Sowa 		write_unlock_bh(&ifp->idev->lock);
40481da177e4SLinus Torvalds 	}
4049764d3be6SPaolo Abeni 
4050764d3be6SPaolo Abeni 	if (bump_id)
4051764d3be6SPaolo Abeni 		rt_genid_bump_ipv6(dev_net(dev));
4052a11a7f71SMarcus Huewe 
4053a11a7f71SMarcus Huewe 	/* Make sure that a new temporary address will be created
4054a11a7f71SMarcus Huewe 	 * before this temporary address becomes deprecated.
4055a11a7f71SMarcus Huewe 	 */
4056a11a7f71SMarcus Huewe 	if (ifp->flags & IFA_F_TEMPORARY)
4057a11a7f71SMarcus Huewe 		addrconf_verify_rtnl();
40581da177e4SLinus Torvalds }
40591da177e4SLinus Torvalds 
4060e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev)
4061e21e8467SStephen Hemminger {
4062c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
4063c5e33bddSYOSHIFUJI Hideaki 
4064c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
4065502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
406621809fafSstephen hemminger 		spin_lock(&ifp->lock);
4067f2344a13SHerbert Xu 		if (ifp->flags & IFA_F_TENTATIVE &&
4068f2344a13SHerbert Xu 		    ifp->state == INET6_IFADDR_STATE_DAD)
4069c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
4070f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
4071c5e33bddSYOSHIFUJI Hideaki 	}
4072c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
4073c5e33bddSYOSHIFUJI Hideaki }
4074c5e33bddSYOSHIFUJI Hideaki 
40751da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
40761da177e4SLinus Torvalds struct if6_iter_state {
40773c40090aSDaniel Lezcano 	struct seq_net_private p;
40781da177e4SLinus Torvalds 	int bucket;
40791d578303SMihai Maruseac 	int offset;
40801da177e4SLinus Torvalds };
40811da177e4SLinus Torvalds 
40821d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
40831da177e4SLinus Torvalds {
40841da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
40851da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
40861218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
40871d578303SMihai Maruseac 	int p = 0;
40881da177e4SLinus Torvalds 
40891d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
40901d578303SMihai Maruseac 	if (pos == 0) {
40911d578303SMihai Maruseac 		state->bucket = 0;
40921d578303SMihai Maruseac 		state->offset = 0;
40931d578303SMihai Maruseac 	}
40941d578303SMihai Maruseac 
40951d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4096b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
40971d578303SMihai Maruseac 					 addr_lst) {
40989f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
40999f0d3c27SEric Dumazet 				continue;
41001d578303SMihai Maruseac 			/* sync with offset */
41011d578303SMihai Maruseac 			if (p < state->offset) {
41021d578303SMihai Maruseac 				p++;
41031d578303SMihai Maruseac 				continue;
41041d578303SMihai Maruseac 			}
41051d578303SMihai Maruseac 			state->offset++;
41061da177e4SLinus Torvalds 			return ifa;
41071da177e4SLinus Torvalds 		}
41081d578303SMihai Maruseac 
41091d578303SMihai Maruseac 		/* prepare for next bucket */
41101d578303SMihai Maruseac 		state->offset = 0;
41111d578303SMihai Maruseac 		p = 0;
41121d578303SMihai Maruseac 	}
4113c2e21293Sstephen hemminger 	return NULL;
4114c2e21293Sstephen hemminger }
41151da177e4SLinus Torvalds 
4116c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4117c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
41181da177e4SLinus Torvalds {
41191da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
41201218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
41211da177e4SLinus Torvalds 
4122b67bfe0dSSasha Levin 	hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
41239f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
41249f0d3c27SEric Dumazet 			continue;
41251d578303SMihai Maruseac 		state->offset++;
41261da177e4SLinus Torvalds 		return ifa;
41271d578303SMihai Maruseac 	}
41281da177e4SLinus Torvalds 
4129c2e21293Sstephen hemminger 	while (++state->bucket < IN6_ADDR_HSIZE) {
41301d578303SMihai Maruseac 		state->offset = 0;
4131b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa,
4132c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
41339f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
41349f0d3c27SEric Dumazet 				continue;
41351d578303SMihai Maruseac 			state->offset++;
4136c2e21293Sstephen hemminger 			return ifa;
4137c2e21293Sstephen hemminger 		}
4138c2e21293Sstephen hemminger 	}
4139c2e21293Sstephen hemminger 
4140c2e21293Sstephen hemminger 	return NULL;
4141c2e21293Sstephen hemminger }
4142c2e21293Sstephen hemminger 
41431da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
41444f70eccaSEric Dumazet 	__acquires(rcu_bh)
41451da177e4SLinus Torvalds {
41465c578aedSstephen hemminger 	rcu_read_lock_bh();
41471d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
41481da177e4SLinus Torvalds }
41491da177e4SLinus Torvalds 
41501da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
41511da177e4SLinus Torvalds {
41521da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
41531da177e4SLinus Torvalds 
41541da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
41551da177e4SLinus Torvalds 	++*pos;
41561da177e4SLinus Torvalds 	return ifa;
41571da177e4SLinus Torvalds }
41581da177e4SLinus Torvalds 
41591da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
41604f70eccaSEric Dumazet 	__releases(rcu_bh)
41611da177e4SLinus Torvalds {
41625c578aedSstephen hemminger 	rcu_read_unlock_bh();
41631da177e4SLinus Torvalds }
41641da177e4SLinus Torvalds 
41651da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
41661da177e4SLinus Torvalds {
41671da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4168971a351cSJiri Pirko 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4169b071195dSHarvey Harrison 		   &ifp->addr,
41701da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
41711da177e4SLinus Torvalds 		   ifp->prefix_len,
41721da177e4SLinus Torvalds 		   ifp->scope,
4173971a351cSJiri Pirko 		   (u8) ifp->flags,
41741da177e4SLinus Torvalds 		   ifp->idev->dev->name);
41751da177e4SLinus Torvalds 	return 0;
41761da177e4SLinus Torvalds }
41771da177e4SLinus Torvalds 
417856b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
41791da177e4SLinus Torvalds 	.start	= if6_seq_start,
41801da177e4SLinus Torvalds 	.next	= if6_seq_next,
41811da177e4SLinus Torvalds 	.show	= if6_seq_show,
41821da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
41831da177e4SLinus Torvalds };
41841da177e4SLinus Torvalds 
41851da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
41861da177e4SLinus Torvalds {
41873c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
4188cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
41891da177e4SLinus Torvalds }
41901da177e4SLinus Torvalds 
41919a32144eSArjan van de Ven static const struct file_operations if6_fops = {
41921da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
41931da177e4SLinus Torvalds 	.open		= if6_seq_open,
41941da177e4SLinus Torvalds 	.read		= seq_read,
41951da177e4SLinus Torvalds 	.llseek		= seq_lseek,
41963c40090aSDaniel Lezcano 	.release	= seq_release_net,
41971da177e4SLinus Torvalds };
41981da177e4SLinus Torvalds 
41992c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
42001da177e4SLinus Torvalds {
4201d4beaa66SGao feng 	if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
42021da177e4SLinus Torvalds 		return -ENOMEM;
42031da177e4SLinus Torvalds 	return 0;
42041da177e4SLinus Torvalds }
42051da177e4SLinus Torvalds 
42062c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
42073c40090aSDaniel Lezcano {
4208ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
42093c40090aSDaniel Lezcano }
42103c40090aSDaniel Lezcano 
42113c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
42123c40090aSDaniel Lezcano 	.init = if6_proc_net_init,
42133c40090aSDaniel Lezcano 	.exit = if6_proc_net_exit,
42143c40090aSDaniel Lezcano };
42153c40090aSDaniel Lezcano 
42163c40090aSDaniel Lezcano int __init if6_proc_init(void)
42173c40090aSDaniel Lezcano {
42183c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
42193c40090aSDaniel Lezcano }
42203c40090aSDaniel Lezcano 
42211da177e4SLinus Torvalds void if6_proc_exit(void)
42221da177e4SLinus Torvalds {
42233c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
42241da177e4SLinus Torvalds }
42251da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
42261da177e4SLinus Torvalds 
422707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
42283b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
4229b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
42303b9f9a1cSNoriaki TAKAMIYA {
42313b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
4232c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
4233ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
4234c2e21293Sstephen hemminger 
42355c578aedSstephen hemminger 	rcu_read_lock_bh();
4236b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
4237878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
4238389f6612SDaniel Lezcano 			continue;
4239caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
42403b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
42413b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
42423b9f9a1cSNoriaki TAKAMIYA 			break;
42433b9f9a1cSNoriaki TAKAMIYA 		}
42443b9f9a1cSNoriaki TAKAMIYA 	}
42455c578aedSstephen hemminger 	rcu_read_unlock_bh();
42463b9f9a1cSNoriaki TAKAMIYA 	return ret;
42473b9f9a1cSNoriaki TAKAMIYA }
42483b9f9a1cSNoriaki TAKAMIYA #endif
42493b9f9a1cSNoriaki TAKAMIYA 
42501da177e4SLinus Torvalds /*
42511da177e4SLinus Torvalds  *	Periodic address status verification
42521da177e4SLinus Torvalds  */
42531da177e4SLinus Torvalds 
4254c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void)
42551da177e4SLinus Torvalds {
4256b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
42571da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
42581da177e4SLinus Torvalds 	int i;
42591da177e4SLinus Torvalds 
4260c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4261c15b1ccaSHannes Frederic Sowa 
42625c578aedSstephen hemminger 	rcu_read_lock_bh();
42631da177e4SLinus Torvalds 	now = jiffies;
4264b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
42651da177e4SLinus Torvalds 
4266c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
42671da177e4SLinus Torvalds 
42681da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
42691da177e4SLinus Torvalds restart:
4270c15b1ccaSHannes Frederic Sowa 		hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
42711da177e4SLinus Torvalds 			unsigned long age;
42721da177e4SLinus Torvalds 
4273fad8da3eSYasushi Asano 			/* When setting preferred_lft to a value not zero or
4274fad8da3eSYasushi Asano 			 * infinity, while valid_lft is infinity
4275fad8da3eSYasushi Asano 			 * IFA_F_PERMANENT has a non-infinity life time.
4276fad8da3eSYasushi Asano 			 */
4277fad8da3eSYasushi Asano 			if ((ifp->flags & IFA_F_PERMANENT) &&
4278fad8da3eSYasushi Asano 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
42791da177e4SLinus Torvalds 				continue;
42801da177e4SLinus Torvalds 
42811da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
4282b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
4283b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
42841da177e4SLinus Torvalds 
42858f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
42868f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
42871da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
42881da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
42891da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
42901da177e4SLinus Torvalds 				goto restart;
42918f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
42928f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
42938f27ebb9SYOSHIFUJI Hideaki 				continue;
42941da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
4295a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
42961da177e4SLinus Torvalds 				int deprecate = 0;
42971da177e4SLinus Torvalds 
42981da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
42991da177e4SLinus Torvalds 					deprecate = 1;
43001da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
43011da177e4SLinus Torvalds 				}
43021da177e4SLinus Torvalds 
4303fad8da3eSYasushi Asano 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4304fad8da3eSYasushi Asano 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
43051da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
43061da177e4SLinus Torvalds 
43071da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
43081da177e4SLinus Torvalds 
43091da177e4SLinus Torvalds 				if (deprecate) {
43101da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
43111da177e4SLinus Torvalds 
43121da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
43131da177e4SLinus Torvalds 					in6_ifa_put(ifp);
43141da177e4SLinus Torvalds 					goto restart;
43151da177e4SLinus Torvalds 				}
43161da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
43171da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
431888949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
431988949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
43201f9248e5SJiri Pirko 					NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
432188949cf4Sstephen hemminger 
43221da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
43231da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
43241da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
43251da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
43261da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
43271da177e4SLinus Torvalds 						ifp->regen_count++;
43281da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
43291da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
43301da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
43315c578aedSstephen hemminger 
4332291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
4333291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
4334291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
43351da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
43361da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
43371da177e4SLinus Torvalds 						in6_ifa_put(ifp);
43381da177e4SLinus Torvalds 						goto restart;
43391da177e4SLinus Torvalds 					}
43401da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
43411da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
43421da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
43431da177e4SLinus Torvalds 			} else {
43441da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
43451da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
43461da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
43471da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
43481da177e4SLinus Torvalds 			}
43491da177e4SLinus Torvalds 		}
43501da177e4SLinus Torvalds 	}
43511da177e4SLinus Torvalds 
4352b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
4353b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
4354b2db7564SYOSHIFUJI Hideaki 
4355b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
4356b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4357b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
4358b2db7564SYOSHIFUJI Hideaki 
4359b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4360b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4361b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4362b2db7564SYOSHIFUJI Hideaki 
4363ba3542e1Sdingtianhong 	ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4364ba3542e1Sdingtianhong 	      now, next, next_sec, next_sched);
4365c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
43665c578aedSstephen hemminger 	rcu_read_unlock_bh();
43671da177e4SLinus Torvalds }
43681da177e4SLinus Torvalds 
4369c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w)
4370c15b1ccaSHannes Frederic Sowa {
4371c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
4372c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4373c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
4374c15b1ccaSHannes Frederic Sowa }
4375c15b1ccaSHannes Frederic Sowa 
4376c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void)
4377c15b1ccaSHannes Frederic Sowa {
4378c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4379c15b1ccaSHannes Frederic Sowa }
4380c15b1ccaSHannes Frederic Sowa 
4381caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4382caeaba79SNicolas Dichtel 				     struct in6_addr **peer_pfx)
4383461d8837SThomas Graf {
4384461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
4385461d8837SThomas Graf 
4386caeaba79SNicolas Dichtel 	*peer_pfx = NULL;
4387caeaba79SNicolas Dichtel 
4388461d8837SThomas Graf 	if (addr)
4389461d8837SThomas Graf 		pfx = nla_data(addr);
4390461d8837SThomas Graf 
4391461d8837SThomas Graf 	if (local) {
4392461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4393caeaba79SNicolas Dichtel 			*peer_pfx = pfx;
4394461d8837SThomas Graf 		pfx = nla_data(local);
4395461d8837SThomas Graf 	}
4396461d8837SThomas Graf 
4397461d8837SThomas Graf 	return pfx;
4398461d8837SThomas Graf }
4399461d8837SThomas Graf 
4400ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4401461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
4402461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
4403461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
4404479840ffSJiri Pirko 	[IFA_FLAGS]		= { .len = sizeof(u32) },
4405461d8837SThomas Graf };
4406461d8837SThomas Graf 
44071da177e4SLinus Torvalds static int
4408c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4409c21ef3e3SDavid Ahern 		  struct netlink_ext_ack *extack)
44101da177e4SLinus Torvalds {
44113b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4412b933f716SThomas Graf 	struct ifaddrmsg *ifm;
4413b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4414caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
44156046d5b4SHeiner Kallweit 	u32 ifa_flags;
4416b933f716SThomas Graf 	int err;
44171da177e4SLinus Torvalds 
4418fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4419c21ef3e3SDavid Ahern 			  extack);
4420b933f716SThomas Graf 	if (err < 0)
4421b933f716SThomas Graf 		return err;
4422b933f716SThomas Graf 
4423b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
4424caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
442563159f29SIan Morris 	if (!pfx)
44261da177e4SLinus Torvalds 		return -EINVAL;
44271da177e4SLinus Torvalds 
44286046d5b4SHeiner Kallweit 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
44296046d5b4SHeiner Kallweit 
44306046d5b4SHeiner Kallweit 	/* We ignore other flags so far. */
44316046d5b4SHeiner Kallweit 	ifa_flags &= IFA_F_MANAGETEMPADDR;
44326046d5b4SHeiner Kallweit 
44336046d5b4SHeiner Kallweit 	return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
44346046d5b4SHeiner Kallweit 			      ifm->ifa_prefixlen);
44351da177e4SLinus Torvalds }
44361da177e4SLinus Torvalds 
4437479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
443855ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
4439081bba5bSNoriaki TAKAMIYA {
44406f704992SYOSHIFUJI Hideaki 	u32 flags;
44416f704992SYOSHIFUJI Hideaki 	clock_t expires;
44424bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
444353bd6749SJiri Pirko 	bool was_managetempaddr;
44445b84efecSThomas Haller 	bool had_prefixroute;
444546d48046SYOSHIFUJI Hideaki 
4446c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4447c15b1ccaSHannes Frederic Sowa 
4448081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
4449081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
4450081bba5bSNoriaki TAKAMIYA 
445153bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR &&
445253bd6749SJiri Pirko 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
445353bd6749SJiri Pirko 		return -EINVAL;
445453bd6749SJiri Pirko 
44554bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
44564bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
44574bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
44584bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
44596f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
44604bed72e4SYOSHIFUJI Hideaki 	} else {
44614bed72e4SYOSHIFUJI Hideaki 		expires = 0;
44624bed72e4SYOSHIFUJI Hideaki 		flags = 0;
44634bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
44646f704992SYOSHIFUJI Hideaki 	}
4465081bba5bSNoriaki TAKAMIYA 
44664bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
44674bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
44684bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
446955ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
44704bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
44714bed72e4SYOSHIFUJI Hideaki 	}
4472081bba5bSNoriaki TAKAMIYA 
4473081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
447453bd6749SJiri Pirko 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
44755b84efecSThomas Haller 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
44765b84efecSThomas Haller 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
447753bd6749SJiri Pirko 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4478761aac73SThomas Haller 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4479761aac73SThomas Haller 			IFA_F_NOPREFIXROUTE);
448053bd6749SJiri Pirko 	ifp->flags |= ifa_flags;
4481081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
4482081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
4483081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
4484081bba5bSNoriaki TAKAMIYA 
4485081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
4486081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
4487081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
4488081bba5bSNoriaki TAKAMIYA 
4489761aac73SThomas Haller 	if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
449046d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
44916f704992SYOSHIFUJI Hideaki 				      expires, flags);
44925b84efecSThomas Haller 	} else if (had_prefixroute) {
44935b84efecSThomas Haller 		enum cleanup_prefix_rt_t action;
44945b84efecSThomas Haller 		unsigned long rt_expires;
44955b84efecSThomas Haller 
44965b84efecSThomas Haller 		write_lock_bh(&ifp->idev->lock);
44975b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &rt_expires);
44985b84efecSThomas Haller 		write_unlock_bh(&ifp->idev->lock);
44995b84efecSThomas Haller 
45005b84efecSThomas Haller 		if (action != CLEANUP_PREFIX_RT_NOP) {
45015b84efecSThomas Haller 			cleanup_prefix_route(ifp, rt_expires,
45025b84efecSThomas Haller 				action == CLEANUP_PREFIX_RT_DEL);
45035b84efecSThomas Haller 		}
4504761aac73SThomas Haller 	}
450553bd6749SJiri Pirko 
450653bd6749SJiri Pirko 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
450753bd6749SJiri Pirko 		if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
450853bd6749SJiri Pirko 			valid_lft = prefered_lft = 0;
450953bd6749SJiri Pirko 		manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
451053bd6749SJiri Pirko 				 !was_managetempaddr, jiffies);
451153bd6749SJiri Pirko 	}
451253bd6749SJiri Pirko 
4513c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4514081bba5bSNoriaki TAKAMIYA 
4515081bba5bSNoriaki TAKAMIYA 	return 0;
4516081bba5bSNoriaki TAKAMIYA }
4517081bba5bSNoriaki TAKAMIYA 
4518081bba5bSNoriaki TAKAMIYA static int
4519c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4520c21ef3e3SDavid Ahern 		  struct netlink_ext_ack *extack)
45211da177e4SLinus Torvalds {
45223b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4523461d8837SThomas Graf 	struct ifaddrmsg *ifm;
4524461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4525caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
45267198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
45277198f8ceSThomas Graf 	struct net_device *dev;
452855ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4529479840ffSJiri Pirko 	u32 ifa_flags;
4530461d8837SThomas Graf 	int err;
45311da177e4SLinus Torvalds 
4532fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4533c21ef3e3SDavid Ahern 			  extack);
4534461d8837SThomas Graf 	if (err < 0)
4535461d8837SThomas Graf 		return err;
4536461d8837SThomas Graf 
4537461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
4538caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
453963159f29SIan Morris 	if (!pfx)
45401da177e4SLinus Torvalds 		return -EINVAL;
45411da177e4SLinus Torvalds 
4542461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
45430778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
4544461d8837SThomas Graf 
4545461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
45460778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
4547461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
4548461d8837SThomas Graf 	} else {
4549461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
4550461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
45510778769dSNoriaki TAKAMIYA 	}
45520778769dSNoriaki TAKAMIYA 
4553af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
455463159f29SIan Morris 	if (!dev)
45557198f8ceSThomas Graf 		return -ENODEV;
4556081bba5bSNoriaki TAKAMIYA 
4557479840ffSJiri Pirko 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4558479840ffSJiri Pirko 
455955ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
4560761aac73SThomas Haller 	ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
456193a714d6SMadhu Challa 		     IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
456255ebaef1SNoriaki TAKAMIYA 
45631cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
456463159f29SIan Morris 	if (!ifa) {
45657198f8ceSThomas Graf 		/*
45667198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
4567db9c7c39Sstephen hemminger 		 * userspace already relies on not having to provide this.
45687198f8ceSThomas Graf 		 */
4569caeaba79SNicolas Dichtel 		return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4570af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
4571af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
45721da177e4SLinus Torvalds 	}
45731da177e4SLinus Torvalds 
45747198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
45757198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
45767198f8ceSThomas Graf 		err = -EEXIST;
45777198f8ceSThomas Graf 	else
457855ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
45797198f8ceSThomas Graf 
45807198f8ceSThomas Graf 	in6_ifa_put(ifa);
45817198f8ceSThomas Graf 
45827198f8ceSThomas Graf 	return err;
45837198f8ceSThomas Graf }
45847198f8ceSThomas Graf 
4585479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4586101bb229SThomas Graf 			  u8 scope, int ifindex)
4587101bb229SThomas Graf {
4588101bb229SThomas Graf 	struct ifaddrmsg *ifm;
4589101bb229SThomas Graf 
4590101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
4591101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
4592101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
4593101bb229SThomas Graf 	ifm->ifa_flags = flags;
4594101bb229SThomas Graf 	ifm->ifa_scope = scope;
4595101bb229SThomas Graf 	ifm->ifa_index = ifindex;
4596101bb229SThomas Graf }
4597101bb229SThomas Graf 
459885486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
459985486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
460085486af0SThomas Graf {
460185486af0SThomas Graf 	struct ifa_cacheinfo ci;
460285486af0SThomas Graf 
460318a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
460418a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
460585486af0SThomas Graf 	ci.ifa_prefered = preferred;
460685486af0SThomas Graf 	ci.ifa_valid = valid;
460785486af0SThomas Graf 
460885486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
460985486af0SThomas Graf }
461085486af0SThomas Graf 
4611101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
4612101bb229SThomas Graf {
4613101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
4614101bb229SThomas Graf 		return RT_SCOPE_HOST;
4615101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
4616101bb229SThomas Graf 		return RT_SCOPE_LINK;
4617101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
4618101bb229SThomas Graf 		return RT_SCOPE_SITE;
4619101bb229SThomas Graf 	else
4620101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
4621101bb229SThomas Graf }
4622101bb229SThomas Graf 
46230ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
46240ab6803bSThomas Graf {
4625339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4626caeaba79SNicolas Dichtel 	       + nla_total_size(16) /* IFA_LOCAL */
4627339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
4628479840ffSJiri Pirko 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
4629479840ffSJiri Pirko 	       + nla_total_size(4)  /* IFA_FLAGS */;
46300ab6803bSThomas Graf }
4631c5396a31SYOSHIFUJI Hideaki 
46321da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
463315e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
46341da177e4SLinus Torvalds {
46351da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
463685486af0SThomas Graf 	u32 preferred, valid;
46371da177e4SLinus Torvalds 
463815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
463963159f29SIan Morris 	if (!nlh)
464026932566SPatrick McHardy 		return -EMSGSIZE;
46410ab6803bSThomas Graf 
4642101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4643101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
4644101bb229SThomas Graf 
4645fad8da3eSYasushi Asano 	if (!((ifa->flags&IFA_F_PERMANENT) &&
4646fad8da3eSYasushi Asano 	      (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
464785486af0SThomas Graf 		preferred = ifa->prefered_lft;
464885486af0SThomas Graf 		valid = ifa->valid_lft;
464985486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
46501da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
4651a1faa698SJens Rosenboom 			if (preferred > tval)
465285486af0SThomas Graf 				preferred -= tval;
4653a1faa698SJens Rosenboom 			else
4654a1faa698SJens Rosenboom 				preferred = 0;
4655f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
4656f56619fcSBen Hutchings 				if (valid > tval)
465785486af0SThomas Graf 					valid -= tval;
4658f56619fcSBen Hutchings 				else
4659f56619fcSBen Hutchings 					valid = 0;
4660f56619fcSBen Hutchings 			}
46611da177e4SLinus Torvalds 		}
46621da177e4SLinus Torvalds 	} else {
466385486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
466485486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
46651da177e4SLinus Torvalds 	}
466685486af0SThomas Graf 
46677996c799SCong Wang 	if (!ipv6_addr_any(&ifa->peer_addr)) {
4668930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4669930345eaSJiri Benc 		    nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4670caeaba79SNicolas Dichtel 			goto error;
4671caeaba79SNicolas Dichtel 	} else
4672930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4673caeaba79SNicolas Dichtel 			goto error;
4674caeaba79SNicolas Dichtel 
4675caeaba79SNicolas Dichtel 	if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4676caeaba79SNicolas Dichtel 		goto error;
467785486af0SThomas Graf 
4678479840ffSJiri Pirko 	if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4679479840ffSJiri Pirko 		goto error;
4680479840ffSJiri Pirko 
4681053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4682053c095aSJohannes Berg 	return 0;
4683caeaba79SNicolas Dichtel 
4684caeaba79SNicolas Dichtel error:
4685caeaba79SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
4686caeaba79SNicolas Dichtel 	return -EMSGSIZE;
46871da177e4SLinus Torvalds }
46881da177e4SLinus Torvalds 
46891da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
469015e47304SEric W. Biederman 				u32 portid, u32 seq, int event, u16 flags)
46911da177e4SLinus Torvalds {
46921da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4693101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4694101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
46951da177e4SLinus Torvalds 
46961da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4697101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4698101bb229SThomas Graf 
469915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
470063159f29SIan Morris 	if (!nlh)
470126932566SPatrick McHardy 		return -EMSGSIZE;
47020ab6803bSThomas Graf 
4703101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4704930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
47050ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
470626932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
470726932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
470826932566SPatrick McHardy 		return -EMSGSIZE;
470926932566SPatrick McHardy 	}
471085486af0SThomas Graf 
4711053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4712053c095aSJohannes Berg 	return 0;
47131da177e4SLinus Torvalds }
47141da177e4SLinus Torvalds 
47151da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
471615e47304SEric W. Biederman 				u32 portid, u32 seq, int event, unsigned int flags)
47171da177e4SLinus Torvalds {
47181da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4719101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4720101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
47211da177e4SLinus Torvalds 
47221da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4723101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4724101bb229SThomas Graf 
472515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
472663159f29SIan Morris 	if (!nlh)
472726932566SPatrick McHardy 		return -EMSGSIZE;
47280ab6803bSThomas Graf 
4729101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4730930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
47310ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
473226932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
473326932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
473426932566SPatrick McHardy 		return -EMSGSIZE;
473526932566SPatrick McHardy 	}
473685486af0SThomas Graf 
4737053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4738053c095aSJohannes Berg 	return 0;
47391da177e4SLinus Torvalds }
47401da177e4SLinus Torvalds 
4741e21e8467SStephen Hemminger enum addr_type_t {
47421da177e4SLinus Torvalds 	UNICAST_ADDR,
47431da177e4SLinus Torvalds 	MULTICAST_ADDR,
47441da177e4SLinus Torvalds 	ANYCAST_ADDR,
47451da177e4SLinus Torvalds };
47461da177e4SLinus Torvalds 
4747234b27c3SEric Dumazet /* called with rcu_read_lock() */
4748234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4749234b27c3SEric Dumazet 			  struct netlink_callback *cb, enum addr_type_t type,
4750234b27c3SEric Dumazet 			  int s_ip_idx, int *p_ip_idx)
47511da177e4SLinus Torvalds {
47521da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
47531da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
4754234b27c3SEric Dumazet 	int err = 1;
4755234b27c3SEric Dumazet 	int ip_idx = *p_ip_idx;
47561da177e4SLinus Torvalds 
47571da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
47581da177e4SLinus Torvalds 	switch (type) {
4759502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
4760502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
4761502a2ffdSstephen hemminger 
4762ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
4763502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
4764502a2ffdSstephen hemminger 			if (++ip_idx < s_ip_idx)
47651da177e4SLinus Torvalds 				continue;
47665d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifaddr(skb, ifa,
476715e47304SEric W. Biederman 						NETLINK_CB(cb->skb).portid,
47685d5619b4SYOSHIFUJI Hideaki 						cb->nlh->nlmsg_seq,
47695d5619b4SYOSHIFUJI Hideaki 						RTM_NEWADDR,
47705d5619b4SYOSHIFUJI Hideaki 						NLM_F_MULTI);
4771053c095aSJohannes Berg 			if (err < 0)
4772bcd32326SEric Dumazet 				break;
477363998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
47741da177e4SLinus Torvalds 		}
47751da177e4SLinus Torvalds 		break;
4776502a2ffdSstephen hemminger 	}
47771da177e4SLinus Torvalds 	case MULTICAST_ADDR:
47781da177e4SLinus Torvalds 		/* multicast address */
47791da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
47801da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
47811da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
47821da177e4SLinus Torvalds 				continue;
47835d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifmcaddr(skb, ifmca,
478415e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
47855d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
47865d5619b4SYOSHIFUJI Hideaki 						  RTM_GETMULTICAST,
47875d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4788053c095aSJohannes Berg 			if (err < 0)
4789bcd32326SEric Dumazet 				break;
47901da177e4SLinus Torvalds 		}
47911da177e4SLinus Torvalds 		break;
47921da177e4SLinus Torvalds 	case ANYCAST_ADDR:
47931da177e4SLinus Torvalds 		/* anycast address */
47941da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
47951da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
47961da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
47971da177e4SLinus Torvalds 				continue;
47985d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifacaddr(skb, ifaca,
479915e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
48005d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
48015d5619b4SYOSHIFUJI Hideaki 						  RTM_GETANYCAST,
48025d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4803053c095aSJohannes Berg 			if (err < 0)
4804bcd32326SEric Dumazet 				break;
48051da177e4SLinus Torvalds 		}
48061da177e4SLinus Torvalds 		break;
48071da177e4SLinus Torvalds 	default:
48081da177e4SLinus Torvalds 		break;
48091da177e4SLinus Torvalds 	}
48101da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
4811234b27c3SEric Dumazet 	*p_ip_idx = ip_idx;
4812234b27c3SEric Dumazet 	return err;
4813234b27c3SEric Dumazet }
48145d5619b4SYOSHIFUJI Hideaki 
4815234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4816234b27c3SEric Dumazet 			   enum addr_type_t type)
4817234b27c3SEric Dumazet {
4818234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
4819234b27c3SEric Dumazet 	int h, s_h;
4820234b27c3SEric Dumazet 	int idx, ip_idx;
4821234b27c3SEric Dumazet 	int s_idx, s_ip_idx;
4822234b27c3SEric Dumazet 	struct net_device *dev;
4823234b27c3SEric Dumazet 	struct inet6_dev *idev;
4824234b27c3SEric Dumazet 	struct hlist_head *head;
4825234b27c3SEric Dumazet 
4826234b27c3SEric Dumazet 	s_h = cb->args[0];
4827234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
4828234b27c3SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
4829234b27c3SEric Dumazet 
4830234b27c3SEric Dumazet 	rcu_read_lock();
483163998ac2SNicolas Dichtel 	cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4832234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4833234b27c3SEric Dumazet 		idx = 0;
4834234b27c3SEric Dumazet 		head = &net->dev_index_head[h];
4835b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
4836234b27c3SEric Dumazet 			if (idx < s_idx)
4837234b27c3SEric Dumazet 				goto cont;
48384b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
4839234b27c3SEric Dumazet 				s_ip_idx = 0;
4840234b27c3SEric Dumazet 			ip_idx = 0;
4841e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
4842e21e8467SStephen Hemminger 			if (!idev)
4843234b27c3SEric Dumazet 				goto cont;
4844234b27c3SEric Dumazet 
4845234b27c3SEric Dumazet 			if (in6_dump_addrs(idev, skb, cb, type,
48467b46a644SDavid S. Miller 					   s_ip_idx, &ip_idx) < 0)
4847234b27c3SEric Dumazet 				goto done;
48487562f876SPavel Emelianov cont:
48497562f876SPavel Emelianov 			idx++;
48501da177e4SLinus Torvalds 		}
4851234b27c3SEric Dumazet 	}
4852234b27c3SEric Dumazet done:
4853234b27c3SEric Dumazet 	rcu_read_unlock();
4854234b27c3SEric Dumazet 	cb->args[0] = h;
4855234b27c3SEric Dumazet 	cb->args[1] = idx;
4856234b27c3SEric Dumazet 	cb->args[2] = ip_idx;
4857234b27c3SEric Dumazet 
48581da177e4SLinus Torvalds 	return skb->len;
48591da177e4SLinus Torvalds }
48601da177e4SLinus Torvalds 
48611da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
48621da177e4SLinus Torvalds {
48631da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
4864b854272bSDenis V. Lunev 
48651da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
48661da177e4SLinus Torvalds }
48671da177e4SLinus Torvalds 
48681da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
48691da177e4SLinus Torvalds {
48701da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
4871b854272bSDenis V. Lunev 
48721da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
48731da177e4SLinus Torvalds }
48741da177e4SLinus Torvalds 
48751da177e4SLinus Torvalds 
48761da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
48771da177e4SLinus Torvalds {
48781da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
4879b854272bSDenis V. Lunev 
48801da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
48811da177e4SLinus Torvalds }
48821da177e4SLinus Torvalds 
4883c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4884c21ef3e3SDavid Ahern 			     struct netlink_ext_ack *extack)
48856c223828SNoriaki TAKAMIYA {
48863b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
48871b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
48881b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4889caeaba79SNicolas Dichtel 	struct in6_addr *addr = NULL, *peer;
48906c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
48916c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
48926c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
48936c223828SNoriaki TAKAMIYA 	int err;
48946c223828SNoriaki TAKAMIYA 
4895fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4896c21ef3e3SDavid Ahern 			  extack);
48971b29fc2cSThomas Graf 	if (err < 0)
48981b29fc2cSThomas Graf 		goto errout;
48996c223828SNoriaki TAKAMIYA 
4900caeaba79SNicolas Dichtel 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
490163159f29SIan Morris 	if (!addr) {
49021b29fc2cSThomas Graf 		err = -EINVAL;
49031b29fc2cSThomas Graf 		goto errout;
49041b29fc2cSThomas Graf 	}
49051b29fc2cSThomas Graf 
49061b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
49076c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
49086fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
49096c223828SNoriaki TAKAMIYA 
4910e21e8467SStephen Hemminger 	ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4911e21e8467SStephen Hemminger 	if (!ifa) {
49121b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
49131b29fc2cSThomas Graf 		goto errout;
49146c223828SNoriaki TAKAMIYA 	}
49156c223828SNoriaki TAKAMIYA 
4916e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4917e21e8467SStephen Hemminger 	if (!skb) {
49181b29fc2cSThomas Graf 		err = -ENOBUFS;
49191b29fc2cSThomas Graf 		goto errout_ifa;
49201b29fc2cSThomas Graf 	}
49211b29fc2cSThomas Graf 
492215e47304SEric W. Biederman 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
49236c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
492426932566SPatrick McHardy 	if (err < 0) {
492526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
492626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
492726932566SPatrick McHardy 		kfree_skb(skb);
492826932566SPatrick McHardy 		goto errout_ifa;
492926932566SPatrick McHardy 	}
493015e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
49311b29fc2cSThomas Graf errout_ifa:
49326c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
49331b29fc2cSThomas Graf errout:
49346c223828SNoriaki TAKAMIYA 	return err;
49356c223828SNoriaki TAKAMIYA }
49366c223828SNoriaki TAKAMIYA 
49371da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
49381da177e4SLinus Torvalds {
49391da177e4SLinus Torvalds 	struct sk_buff *skb;
4940c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
49415d620266SThomas Graf 	int err = -ENOBUFS;
49421da177e4SLinus Torvalds 
4943f784ad3dSMahesh Bandewar 	/* Don't send DELADDR notification for TENTATIVE address,
4944f784ad3dSMahesh Bandewar 	 * since NEWADDR notification is sent only after removing
49456819a14eSMike Manning 	 * TENTATIVE flag, if DAD has not failed.
4946f784ad3dSMahesh Bandewar 	 */
49476819a14eSMike Manning 	if (ifa->flags & IFA_F_TENTATIVE && !(ifa->flags & IFA_F_DADFAILED) &&
49486819a14eSMike Manning 	    event == RTM_DELADDR)
4949f784ad3dSMahesh Bandewar 		return;
4950f784ad3dSMahesh Bandewar 
49510ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
495263159f29SIan Morris 	if (!skb)
49535d620266SThomas Graf 		goto errout;
49545d620266SThomas Graf 
49555d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
495626932566SPatrick McHardy 	if (err < 0) {
495726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
495826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
495926932566SPatrick McHardy 		kfree_skb(skb);
496026932566SPatrick McHardy 		goto errout;
496126932566SPatrick McHardy 	}
49621ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
49631ce85fe4SPablo Neira Ayuso 	return;
49645d620266SThomas Graf errout:
49655d620266SThomas Graf 	if (err < 0)
49666fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
49671da177e4SLinus Torvalds }
49681da177e4SLinus Torvalds 
4969b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
49701da177e4SLinus Torvalds 				__s32 *array, int bytes)
49711da177e4SLinus Torvalds {
497204561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
497304561c1fSThomas Graf 
49741da177e4SLinus Torvalds 	memset(array, 0, bytes);
49751da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
49761da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
49771da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
49781da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
49791da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
49801da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
49811da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
49821da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
498393908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
498493908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
4985bd11f074SMaciej Żenczykowski 	array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4986bd11f074SMaciej Żenczykowski 		jiffies_to_msecs(cnf->rtr_solicit_max_interval);
498793908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
498893908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
49891da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4990fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4991fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4992fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4993fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
49941da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
49951da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
49961da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
49971da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
49981da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
49991da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
500065f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
50018013d1d7SHangbin Liu 	array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
5002c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
5003930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
5004930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
500593908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
500693908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
5007fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
5008bbea124bSJoel Scherpelz 	array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
500909c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
501009c884d4SYOSHIFUJI Hideaki #endif
5011930d6ff2SYOSHIFUJI Hideaki #endif
5012fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
50130bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
501495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
501595c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
50167fd2561eSErik Kline 	array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
501795c385b4SNeil Horman #endif
50187bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
50197bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
50207bc570c8SYOSHIFUJI Hideaki #endif
5021778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
50221b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
5023c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
50245cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5025b800c3b9SHannes Frederic Sowa 	array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5026d9333196SBen Greear 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5027c2943f14SHarout Hedeshian 	array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
502835103d11SAndy Gospodarek 	array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
50293d1bec99SHannes Frederic Sowa 	/* we omit DEVCONF_STABLE_SECRET for now */
50303985e8a3SErik Kline 	array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5031abbc3043SJohannes Berg 	array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
50327a02bf89SJohannes Berg 	array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5033f1705ec1SDavid Ahern 	array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
50341ababebaSDavid Lebrun 	array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5035bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
5036bf355b8dSDavid Lebrun 	array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5037bf355b8dSDavid Lebrun #endif
5038adc176c5SErik Nordmark 	array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5039d35a00b8SFelix Jia 	array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5040df789fe7SDavid Forster 	array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
50411da177e4SLinus Torvalds }
50421da177e4SLinus Torvalds 
5043b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
5044b382b191SThomas Graf {
5045b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
5046b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
5047b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5048b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5049f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5050f53adae4SDaniel Borkmann 	     + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
5051b382b191SThomas Graf }
5052b382b191SThomas Graf 
5053339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
5054339bf98fSThomas Graf {
5055339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5056339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5057339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5058339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
5059339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
50600344338bSAndy Gospodarek 	       + nla_total_size(1) /* IFLA_OPERSTATE */
5061b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5062339bf98fSThomas Graf }
5063c5396a31SYOSHIFUJI Hideaki 
5064be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5065aca05671SJia He 					int bytes)
50667f7d9a6bSHerbert Xu {
50677f7d9a6bSHerbert Xu 	int i;
5068aca05671SJia He 	int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
50697f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
50707f7d9a6bSHerbert Xu 
50717f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
5072aca05671SJia He 	put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5073aca05671SJia He 	for (i = 1; i < ICMP6_MIB_MAX; i++)
5074be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
50757f7d9a6bSHerbert Xu 
5076aca05671SJia He 	memset(&stats[ICMP6_MIB_MAX], 0, pad);
50777f7d9a6bSHerbert Xu }
50787f7d9a6bSHerbert Xu 
5079698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5080a3a77372SRaghavendra K T 					int bytes, size_t syncpoff)
50814ce3c183SEric Dumazet {
5082a3a77372SRaghavendra K T 	int i, c;
5083a3a77372SRaghavendra K T 	u64 buff[IPSTATS_MIB_MAX];
5084a3a77372SRaghavendra K T 	int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5085a3a77372SRaghavendra K T 
50864ce3c183SEric Dumazet 	BUG_ON(pad < 0);
50874ce3c183SEric Dumazet 
5088a3a77372SRaghavendra K T 	memset(buff, 0, sizeof(buff));
5089a3a77372SRaghavendra K T 	buff[0] = IPSTATS_MIB_MAX;
50904ce3c183SEric Dumazet 
5091a3a77372SRaghavendra K T 	for_each_possible_cpu(c) {
5092a3a77372SRaghavendra K T 		for (i = 1; i < IPSTATS_MIB_MAX; i++)
5093a3a77372SRaghavendra K T 			buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5094a3a77372SRaghavendra K T 	}
5095a3a77372SRaghavendra K T 
5096a3a77372SRaghavendra K T 	memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5097a3a77372SRaghavendra K T 	memset(&stats[IPSTATS_MIB_MAX], 0, pad);
50984ce3c183SEric Dumazet }
50994ce3c183SEric Dumazet 
51007f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
51017f7d9a6bSHerbert Xu 			     int bytes)
51027f7d9a6bSHerbert Xu {
51037f7d9a6bSHerbert Xu 	switch (attrtype) {
51047f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
5105a3a77372SRaghavendra K T 		__snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5106a3a77372SRaghavendra K T 				     offsetof(struct ipstats_mib, syncp));
51077f7d9a6bSHerbert Xu 		break;
51087f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
5109aca05671SJia He 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
51107f7d9a6bSHerbert Xu 		break;
51117f7d9a6bSHerbert Xu 	}
51127f7d9a6bSHerbert Xu }
51137f7d9a6bSHerbert Xu 
5114d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5115d5566fd7SSowmini Varadhan 				  u32 ext_filter_mask)
51161da177e4SLinus Torvalds {
5117bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
51181da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
51191da177e4SLinus Torvalds 
5120c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5121c78679e8SDavid S. Miller 		goto nla_put_failure;
51221da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
512324912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
512424912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
51251f9248e5SJiri Pirko 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5126c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5127c78679e8SDavid S. Miller 		goto nla_put_failure;
5128bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
512963159f29SIan Morris 	if (!nla)
513004561c1fSThomas Graf 		goto nla_put_failure;
5131bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
51321da177e4SLinus Torvalds 
5133bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
5134bf99f1bdSYOSHIFUJI Hideaki 
5135d5566fd7SSowmini Varadhan 	if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5136d5566fd7SSowmini Varadhan 		return 0;
5137d5566fd7SSowmini Varadhan 
5138bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
513963159f29SIan Morris 	if (!nla)
5140bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
5141bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5142bf99f1bdSYOSHIFUJI Hideaki 
5143bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
514463159f29SIan Morris 	if (!nla)
5145bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
5146bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
51471da177e4SLinus Torvalds 
5148f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
514963159f29SIan Morris 	if (!nla)
5150f53adae4SDaniel Borkmann 		goto nla_put_failure;
5151bc91b0f0SJiri Pirko 
5152d35a00b8SFelix Jia 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5153bc91b0f0SJiri Pirko 		goto nla_put_failure;
5154bc91b0f0SJiri Pirko 
5155f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
5156f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5157f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
5158f53adae4SDaniel Borkmann 
5159b382b191SThomas Graf 	return 0;
5160b382b191SThomas Graf 
5161b382b191SThomas Graf nla_put_failure:
5162b382b191SThomas Graf 	return -EMSGSIZE;
5163b382b191SThomas Graf }
5164b382b191SThomas Graf 
5165b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev,
5166b1974ed0SArad, Ronen 				     u32 ext_filter_mask)
5167b382b191SThomas Graf {
5168b382b191SThomas Graf 	if (!__in6_dev_get(dev))
5169b382b191SThomas Graf 		return 0;
5170b382b191SThomas Graf 
5171b382b191SThomas Graf 	return inet6_ifla6_size();
5172b382b191SThomas Graf }
5173b382b191SThomas Graf 
5174d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5175d5566fd7SSowmini Varadhan 			      u32 ext_filter_mask)
5176b382b191SThomas Graf {
5177b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
5178b382b191SThomas Graf 
5179b382b191SThomas Graf 	if (!idev)
5180b382b191SThomas Graf 		return -ENODATA;
5181b382b191SThomas Graf 
5182d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5183b382b191SThomas Graf 		return -EMSGSIZE;
5184b382b191SThomas Graf 
5185b382b191SThomas Graf 	return 0;
5186b382b191SThomas Graf }
5187b382b191SThomas Graf 
5188f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5189f53adae4SDaniel Borkmann {
5190f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
5191f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
519247e27d5eSDaniel Borkmann 	bool clear_token, update_rs = false;
5193dc848292SHannes Frederic Sowa 	struct in6_addr ll_addr;
5194f53adae4SDaniel Borkmann 
5195c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
5196c15b1ccaSHannes Frederic Sowa 
519763159f29SIan Morris 	if (!token)
5198f53adae4SDaniel Borkmann 		return -EINVAL;
5199f53adae4SDaniel Borkmann 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5200f53adae4SDaniel Borkmann 		return -EINVAL;
5201f53adae4SDaniel Borkmann 	if (!ipv6_accept_ra(idev))
5202f53adae4SDaniel Borkmann 		return -EINVAL;
5203bd11f074SMaciej Żenczykowski 	if (idev->cnf.rtr_solicits == 0)
5204f53adae4SDaniel Borkmann 		return -EINVAL;
5205f53adae4SDaniel Borkmann 
5206f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5207f53adae4SDaniel Borkmann 
5208f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5209f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5210f53adae4SDaniel Borkmann 
5211f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5212f53adae4SDaniel Borkmann 
521347e27d5eSDaniel Borkmann 	clear_token = ipv6_addr_any(token);
521447e27d5eSDaniel Borkmann 	if (clear_token)
521547e27d5eSDaniel Borkmann 		goto update_lft;
521647e27d5eSDaniel Borkmann 
5217dc848292SHannes Frederic Sowa 	if (!idev->dead && (idev->if_flags & IF_READY) &&
5218dc848292SHannes Frederic Sowa 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5219dc848292SHannes Frederic Sowa 			     IFA_F_OPTIMISTIC)) {
5220fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
5221fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
5222fc403832SDaniel Borkmann 		 */
5223f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5224fc403832SDaniel Borkmann 		update_rs = true;
5225fc403832SDaniel Borkmann 	}
5226f53adae4SDaniel Borkmann 
522747e27d5eSDaniel Borkmann update_lft:
5228f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5229fc403832SDaniel Borkmann 
523077ecaaceSHannes Frederic Sowa 	if (update_rs) {
5231f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
5232bd11f074SMaciej Żenczykowski 		idev->rs_interval = rfc3315_s14_backoff_init(
5233bd11f074SMaciej Żenczykowski 			idev->cnf.rtr_solicit_interval);
523477ecaaceSHannes Frederic Sowa 		idev->rs_probes = 1;
5235bd11f074SMaciej Żenczykowski 		addrconf_mod_rs_timer(idev, idev->rs_interval);
523677ecaaceSHannes Frederic Sowa 	}
5237f53adae4SDaniel Borkmann 
5238f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
5239f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
5240f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
5241617fe29dSDaniel Borkmann 		if (ifp->tokenized) {
5242f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
5243f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
5244f53adae4SDaniel Borkmann 		}
5245f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
5246f53adae4SDaniel Borkmann 	}
5247f53adae4SDaniel Borkmann 
5248f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5249b2ed64a9SLubomir Rintel 	inet6_ifinfo_notify(RTM_NEWLINK, idev);
5250c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
5251f53adae4SDaniel Borkmann 	return 0;
5252f53adae4SDaniel Borkmann }
5253f53adae4SDaniel Borkmann 
525411b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
525511b1f828SDaniel Borkmann 	[IFLA_INET6_ADDR_GEN_MODE]	= { .type = NLA_U8 },
525611b1f828SDaniel Borkmann 	[IFLA_INET6_TOKEN]		= { .len = sizeof(struct in6_addr) },
525711b1f828SDaniel Borkmann };
525811b1f828SDaniel Borkmann 
525911b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev,
526011b1f828SDaniel Borkmann 				  const struct nlattr *nla)
526111b1f828SDaniel Borkmann {
526211b1f828SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
526311b1f828SDaniel Borkmann 
526411b1f828SDaniel Borkmann 	if (dev && !__in6_dev_get(dev))
526511b1f828SDaniel Borkmann 		return -EAFNOSUPPORT;
526611b1f828SDaniel Borkmann 
5267fceb6435SJohannes Berg 	return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy,
5268fceb6435SJohannes Berg 				NULL);
526911b1f828SDaniel Borkmann }
527011b1f828SDaniel Borkmann 
5271d35a00b8SFelix Jia static int check_addr_gen_mode(int mode)
5272d35a00b8SFelix Jia {
5273d35a00b8SFelix Jia 	if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5274d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_NONE &&
5275d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5276d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_RANDOM)
5277d35a00b8SFelix Jia 		return -EINVAL;
5278d35a00b8SFelix Jia 	return 1;
5279d35a00b8SFelix Jia }
5280d35a00b8SFelix Jia 
5281d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5282d35a00b8SFelix Jia 				int mode)
5283d35a00b8SFelix Jia {
5284d35a00b8SFelix Jia 	if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5285d35a00b8SFelix Jia 	    !idev->cnf.stable_secret.initialized &&
5286d35a00b8SFelix Jia 	    !net->ipv6.devconf_dflt->stable_secret.initialized)
5287d35a00b8SFelix Jia 		return -EINVAL;
5288d35a00b8SFelix Jia 	return 1;
5289d35a00b8SFelix Jia }
5290d35a00b8SFelix Jia 
5291f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5292f53adae4SDaniel Borkmann {
5293f53adae4SDaniel Borkmann 	int err = -EINVAL;
5294f53adae4SDaniel Borkmann 	struct inet6_dev *idev = __in6_dev_get(dev);
5295f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
5296f53adae4SDaniel Borkmann 
5297f53adae4SDaniel Borkmann 	if (!idev)
5298f53adae4SDaniel Borkmann 		return -EAFNOSUPPORT;
5299f53adae4SDaniel Borkmann 
5300fceb6435SJohannes Berg 	if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
5301f53adae4SDaniel Borkmann 		BUG();
5302f53adae4SDaniel Borkmann 
5303bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_TOKEN]) {
5304f53adae4SDaniel Borkmann 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5305bc91b0f0SJiri Pirko 		if (err)
5306bc91b0f0SJiri Pirko 			return err;
5307bc91b0f0SJiri Pirko 	}
5308bc91b0f0SJiri Pirko 
5309bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5310bc91b0f0SJiri Pirko 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5311bc91b0f0SJiri Pirko 
5312d35a00b8SFelix Jia 		if (check_addr_gen_mode(mode) < 0 ||
5313d35a00b8SFelix Jia 		    check_stable_privacy(idev, dev_net(dev), mode) < 0)
5314bc91b0f0SJiri Pirko 			return -EINVAL;
5315622c81d5SHannes Frederic Sowa 
5316d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = mode;
5317bc91b0f0SJiri Pirko 		err = 0;
5318bc91b0f0SJiri Pirko 	}
5319f53adae4SDaniel Borkmann 
5320f53adae4SDaniel Borkmann 	return err;
5321f53adae4SDaniel Borkmann }
5322f53adae4SDaniel Borkmann 
5323b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
532415e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
5325b382b191SThomas Graf {
5326b382b191SThomas Graf 	struct net_device *dev = idev->dev;
5327b382b191SThomas Graf 	struct ifinfomsg *hdr;
5328b382b191SThomas Graf 	struct nlmsghdr *nlh;
5329b382b191SThomas Graf 	void *protoinfo;
5330b382b191SThomas Graf 
533115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
533263159f29SIan Morris 	if (!nlh)
5333b382b191SThomas Graf 		return -EMSGSIZE;
5334b382b191SThomas Graf 
5335b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
5336b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
5337b382b191SThomas Graf 	hdr->__ifi_pad = 0;
5338b382b191SThomas Graf 	hdr->ifi_type = dev->type;
5339b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
5340b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
5341b382b191SThomas Graf 	hdr->ifi_change = 0;
5342b382b191SThomas Graf 
5343c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5344c78679e8SDavid S. Miller 	    (dev->addr_len &&
5345c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5346c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5347a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
53480344338bSAndy Gospodarek 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
53490344338bSAndy Gospodarek 	    nla_put_u8(skb, IFLA_OPERSTATE,
53500344338bSAndy Gospodarek 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5351c78679e8SDavid S. Miller 		goto nla_put_failure;
5352b382b191SThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
535363159f29SIan Morris 	if (!protoinfo)
5354b382b191SThomas Graf 		goto nla_put_failure;
5355b382b191SThomas Graf 
5356d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5357b382b191SThomas Graf 		goto nla_put_failure;
5358b382b191SThomas Graf 
535904561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
5360053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5361053c095aSJohannes Berg 	return 0;
53621da177e4SLinus Torvalds 
536304561c1fSThomas Graf nla_put_failure:
536426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
536526932566SPatrick McHardy 	return -EMSGSIZE;
53661da177e4SLinus Torvalds }
53671da177e4SLinus Torvalds 
53681da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
53691da177e4SLinus Torvalds {
53703b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
537184d2697dSEric Dumazet 	int h, s_h;
5372434a8a58SDavid S. Miller 	int idx = 0, s_idx;
53731da177e4SLinus Torvalds 	struct net_device *dev;
53741da177e4SLinus Torvalds 	struct inet6_dev *idev;
537584d2697dSEric Dumazet 	struct hlist_head *head;
53761da177e4SLinus Torvalds 
537784d2697dSEric Dumazet 	s_h = cb->args[0];
537884d2697dSEric Dumazet 	s_idx = cb->args[1];
537984d2697dSEric Dumazet 
538084d2697dSEric Dumazet 	rcu_read_lock();
538184d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
53827562f876SPavel Emelianov 		idx = 0;
538384d2697dSEric Dumazet 		head = &net->dev_index_head[h];
5384b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
53851da177e4SLinus Torvalds 			if (idx < s_idx)
53867562f876SPavel Emelianov 				goto cont;
538784d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
538884d2697dSEric Dumazet 			if (!idev)
53897562f876SPavel Emelianov 				goto cont;
539084d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
539115e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
539284d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
5393053c095aSJohannes Berg 					      RTM_NEWLINK, NLM_F_MULTI) < 0)
539484d2697dSEric Dumazet 				goto out;
53957562f876SPavel Emelianov cont:
53967562f876SPavel Emelianov 			idx++;
53971da177e4SLinus Torvalds 		}
539884d2697dSEric Dumazet 	}
539984d2697dSEric Dumazet out:
540084d2697dSEric Dumazet 	rcu_read_unlock();
540184d2697dSEric Dumazet 	cb->args[1] = idx;
540284d2697dSEric Dumazet 	cb->args[0] = h;
54031da177e4SLinus Torvalds 
54041da177e4SLinus Torvalds 	return skb->len;
54051da177e4SLinus Torvalds }
54061da177e4SLinus Torvalds 
54071da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
54081da177e4SLinus Torvalds {
54091da177e4SLinus Torvalds 	struct sk_buff *skb;
5410c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
54118d7a76c9SThomas Graf 	int err = -ENOBUFS;
54121da177e4SLinus Torvalds 
5413339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
541463159f29SIan Morris 	if (!skb)
54158d7a76c9SThomas Graf 		goto errout;
54168d7a76c9SThomas Graf 
54178d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
541826932566SPatrick McHardy 	if (err < 0) {
541926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
542026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
542126932566SPatrick McHardy 		kfree_skb(skb);
542226932566SPatrick McHardy 		goto errout;
542326932566SPatrick McHardy 	}
54245f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
54251ce85fe4SPablo Neira Ayuso 	return;
54268d7a76c9SThomas Graf errout:
54278d7a76c9SThomas Graf 	if (err < 0)
54285f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
54291da177e4SLinus Torvalds }
54301da177e4SLinus Torvalds 
5431339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
5432339bf98fSThomas Graf {
5433339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
5434339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
5435339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
5436339bf98fSThomas Graf }
5437c5396a31SYOSHIFUJI Hideaki 
54381da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
543915e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
5440b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
54411da177e4SLinus Torvalds {
54421da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
54431da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
54441da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
54451da177e4SLinus Torvalds 
544615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
544763159f29SIan Morris 	if (!nlh)
544826932566SPatrick McHardy 		return -EMSGSIZE;
54496051e2f4SThomas Graf 
54506051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
54511da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
54528a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
54538a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
54541da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
54551da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
54561da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
54578a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
54581da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
54591da177e4SLinus Torvalds 	if (pinfo->onlink)
54601da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
54611da177e4SLinus Torvalds 	if (pinfo->autoconf)
54621da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
54631da177e4SLinus Torvalds 
5464c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5465c78679e8SDavid S. Miller 		goto nla_put_failure;
54661da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
54671da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
5468c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5469c78679e8SDavid S. Miller 		goto nla_put_failure;
5470053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5471053c095aSJohannes Berg 	return 0;
54721da177e4SLinus Torvalds 
54736051e2f4SThomas Graf nla_put_failure:
547426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
547526932566SPatrick McHardy 	return -EMSGSIZE;
54761da177e4SLinus Torvalds }
54771da177e4SLinus Torvalds 
54781da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
54791da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
54801da177e4SLinus Torvalds {
54811da177e4SLinus Torvalds 	struct sk_buff *skb;
5482c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
54838c384bfaSThomas Graf 	int err = -ENOBUFS;
54841da177e4SLinus Torvalds 
5485339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
548663159f29SIan Morris 	if (!skb)
54878c384bfaSThomas Graf 		goto errout;
54888c384bfaSThomas Graf 
54898c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
549026932566SPatrick McHardy 	if (err < 0) {
549126932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
549226932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
549326932566SPatrick McHardy 		kfree_skb(skb);
549426932566SPatrick McHardy 		goto errout;
549526932566SPatrick McHardy 	}
54961ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
54971ce85fe4SPablo Neira Ayuso 	return;
54988c384bfaSThomas Graf errout:
54998c384bfaSThomas Graf 	if (err < 0)
55006fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
55011da177e4SLinus Torvalds }
55021da177e4SLinus Torvalds 
55031da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
55041da177e4SLinus Torvalds {
550563998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
550663998ac2SNicolas Dichtel 
5507c15b1ccaSHannes Frederic Sowa 	if (event)
5508c15b1ccaSHannes Frederic Sowa 		ASSERT_RTNL();
5509c15b1ccaSHannes Frederic Sowa 
55101da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
55111da177e4SLinus Torvalds 
55121da177e4SLinus Torvalds 	switch (event) {
55131da177e4SLinus Torvalds 	case RTM_NEWADDR:
551495c385b4SNeil Horman 		/*
551595c385b4SNeil Horman 		 * If the address was optimistic
551695c385b4SNeil Horman 		 * we inserted the route at the start of
551795c385b4SNeil Horman 		 * our DAD process, so we don't need
551895c385b4SNeil Horman 		 * to do it again
551995c385b4SNeil Horman 		 */
55204e587ea7SWei Wang 		if (!rcu_access_pointer(ifp->rt->rt6i_node))
552140e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
55221da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
55231da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
55247996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr))
5525caeaba79SNicolas Dichtel 			addrconf_prefix_route(&ifp->peer_addr, 128,
5526caeaba79SNicolas Dichtel 					      ifp->idev->dev, 0, 0);
55271da177e4SLinus Torvalds 		break;
55281da177e4SLinus Torvalds 	case RTM_DELADDR:
55291da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
55301da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
55311da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
55327996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr)) {
5533caeaba79SNicolas Dichtel 			struct rt6_info *rt;
5534caeaba79SNicolas Dichtel 
5535e7478dfcSNicolas Dichtel 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5536e7478dfcSNicolas Dichtel 						       ifp->idev->dev, 0, 0);
55378e3d5be7SMartin KaFai Lau 			if (rt)
55388e3d5be7SMartin KaFai Lau 				ip6_del_rt(rt);
5539caeaba79SNicolas Dichtel 		}
554038bd10c4SDavid Ahern 		if (ifp->rt) {
5541ad65a2f0SWei Wang 			if (dst_hold_safe(&ifp->rt->dst))
55428e3d5be7SMartin KaFai Lau 				ip6_del_rt(ifp->rt);
554338bd10c4SDavid Ahern 		}
5544705f1c86SHannes Frederic Sowa 		rt_genid_bump_ipv6(net);
55451da177e4SLinus Torvalds 		break;
55461da177e4SLinus Torvalds 	}
554763998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
55481da177e4SLinus Torvalds }
55491da177e4SLinus Torvalds 
55501da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
55511da177e4SLinus Torvalds {
55528814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
55531da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
55541da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
55558814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
55561da177e4SLinus Torvalds }
55571da177e4SLinus Torvalds 
55581da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
55591da177e4SLinus Torvalds 
55601da177e4SLinus Torvalds static
5561fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
55621da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
55631da177e4SLinus Torvalds {
55641da177e4SLinus Torvalds 	int *valp = ctl->data;
55651da177e4SLinus Torvalds 	int val = *valp;
556688af182eSEric W. Biederman 	loff_t pos = *ppos;
5567fe2c6338SJoe Perches 	struct ctl_table lctl;
55681da177e4SLinus Torvalds 	int ret;
55691da177e4SLinus Torvalds 
5570013d97e9SFrancesco Ruggeri 	/*
5571013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
5572013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5573013d97e9SFrancesco Ruggeri 	 */
5574013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5575013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5576013d97e9SFrancesco Ruggeri 
5577013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
55781da177e4SLinus Torvalds 
5579c8fecf22SPavel Emelyanov 	if (write)
5580b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
558188af182eSEric W. Biederman 	if (ret)
558288af182eSEric W. Biederman 		*ppos = pos;
55831da177e4SLinus Torvalds 	return ret;
55841da177e4SLinus Torvalds }
55851da177e4SLinus Torvalds 
558677751427SMarcelo Leitner static
558777751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
558877751427SMarcelo Leitner 			void __user *buffer, size_t *lenp, loff_t *ppos)
558977751427SMarcelo Leitner {
559077751427SMarcelo Leitner 	struct inet6_dev *idev = ctl->extra1;
559177751427SMarcelo Leitner 	int min_mtu = IPV6_MIN_MTU;
559277751427SMarcelo Leitner 	struct ctl_table lctl;
559377751427SMarcelo Leitner 
559477751427SMarcelo Leitner 	lctl = *ctl;
559577751427SMarcelo Leitner 	lctl.extra1 = &min_mtu;
559677751427SMarcelo Leitner 	lctl.extra2 = idev ? &idev->dev->mtu : NULL;
559777751427SMarcelo Leitner 
559877751427SMarcelo Leitner 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
559977751427SMarcelo Leitner }
560077751427SMarcelo Leitner 
560156d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
560256d417b1SBrian Haley {
560375538c2bSCong Wang 	struct netdev_notifier_info info;
560475538c2bSCong Wang 
560556d417b1SBrian Haley 	if (!idev || !idev->dev)
560656d417b1SBrian Haley 		return;
560756d417b1SBrian Haley 
560875538c2bSCong Wang 	netdev_notifier_info_init(&info, idev->dev);
560956d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
561075538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_DOWN, &info);
561156d417b1SBrian Haley 	else
561275538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_UP, &info);
561356d417b1SBrian Haley }
561456d417b1SBrian Haley 
561556d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
561656d417b1SBrian Haley {
561756d417b1SBrian Haley 	struct net_device *dev;
561856d417b1SBrian Haley 	struct inet6_dev *idev;
561956d417b1SBrian Haley 
562003e4deffSKefeng Wang 	for_each_netdev(net, dev) {
562156d417b1SBrian Haley 		idev = __in6_dev_get(dev);
562256d417b1SBrian Haley 		if (idev) {
562356d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
562456d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
562556d417b1SBrian Haley 			if (changed)
562656d417b1SBrian Haley 				dev_disable_change(idev);
562756d417b1SBrian Haley 		}
562856d417b1SBrian Haley 	}
562956d417b1SBrian Haley }
563056d417b1SBrian Haley 
5631013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
563256d417b1SBrian Haley {
563356d417b1SBrian Haley 	struct net *net;
5634013d97e9SFrancesco Ruggeri 	int old;
5635013d97e9SFrancesco Ruggeri 
5636013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
5637013d97e9SFrancesco Ruggeri 		return restart_syscall();
563856d417b1SBrian Haley 
563956d417b1SBrian Haley 	net = (struct net *)table->extra2;
5640013d97e9SFrancesco Ruggeri 	old = *p;
5641013d97e9SFrancesco Ruggeri 	*p = newf;
564256d417b1SBrian Haley 
5643013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5644013d97e9SFrancesco Ruggeri 		rtnl_unlock();
564556d417b1SBrian Haley 		return 0;
564688af182eSEric W. Biederman 	}
564756d417b1SBrian Haley 
564856d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
564956d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
565056d417b1SBrian Haley 		addrconf_disable_change(net, newf);
5651013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
565256d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
565356d417b1SBrian Haley 
565456d417b1SBrian Haley 	rtnl_unlock();
565556d417b1SBrian Haley 	return 0;
565656d417b1SBrian Haley }
565756d417b1SBrian Haley 
565856d417b1SBrian Haley static
5659fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
566056d417b1SBrian Haley 			    void __user *buffer, size_t *lenp, loff_t *ppos)
566156d417b1SBrian Haley {
566256d417b1SBrian Haley 	int *valp = ctl->data;
566356d417b1SBrian Haley 	int val = *valp;
566488af182eSEric W. Biederman 	loff_t pos = *ppos;
5665fe2c6338SJoe Perches 	struct ctl_table lctl;
566656d417b1SBrian Haley 	int ret;
566756d417b1SBrian Haley 
5668013d97e9SFrancesco Ruggeri 	/*
5669013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
5670013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5671013d97e9SFrancesco Ruggeri 	 */
5672013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5673013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5674013d97e9SFrancesco Ruggeri 
5675013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
567656d417b1SBrian Haley 
567756d417b1SBrian Haley 	if (write)
567856d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
567988af182eSEric W. Biederman 	if (ret)
568088af182eSEric W. Biederman 		*ppos = pos;
568156d417b1SBrian Haley 	return ret;
568256d417b1SBrian Haley }
568356d417b1SBrian Haley 
5684c92d5491Sstephen hemminger static
5685c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5686c92d5491Sstephen hemminger 			      void __user *buffer, size_t *lenp, loff_t *ppos)
5687c92d5491Sstephen hemminger {
5688c92d5491Sstephen hemminger 	int *valp = ctl->data;
5689c92d5491Sstephen hemminger 	int ret;
5690c92d5491Sstephen hemminger 	int old, new;
5691c92d5491Sstephen hemminger 
5692c92d5491Sstephen hemminger 	old = *valp;
5693c92d5491Sstephen hemminger 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5694c92d5491Sstephen hemminger 	new = *valp;
5695c92d5491Sstephen hemminger 
5696c92d5491Sstephen hemminger 	if (write && old != new) {
5697c92d5491Sstephen hemminger 		struct net *net = ctl->extra2;
5698c92d5491Sstephen hemminger 
5699c92d5491Sstephen hemminger 		if (!rtnl_trylock())
5700c92d5491Sstephen hemminger 			return restart_syscall();
5701c92d5491Sstephen hemminger 
5702c92d5491Sstephen hemminger 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
570385b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
570485b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
5705c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_DEFAULT,
5706c92d5491Sstephen hemminger 						     net->ipv6.devconf_dflt);
5707c92d5491Sstephen hemminger 		else if (valp == &net->ipv6.devconf_all->proxy_ndp)
570885b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
570985b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
5710c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_ALL,
5711c92d5491Sstephen hemminger 						     net->ipv6.devconf_all);
5712c92d5491Sstephen hemminger 		else {
5713c92d5491Sstephen hemminger 			struct inet6_dev *idev = ctl->extra1;
5714c92d5491Sstephen hemminger 
571585b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
571685b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
5717c92d5491Sstephen hemminger 						     idev->dev->ifindex,
5718c92d5491Sstephen hemminger 						     &idev->cnf);
5719c92d5491Sstephen hemminger 		}
5720c92d5491Sstephen hemminger 		rtnl_unlock();
5721c92d5491Sstephen hemminger 	}
5722c92d5491Sstephen hemminger 
5723c92d5491Sstephen hemminger 	return ret;
5724c92d5491Sstephen hemminger }
5725c92d5491Sstephen hemminger 
5726d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5727d35a00b8SFelix Jia 					 void __user *buffer, size_t *lenp,
5728d35a00b8SFelix Jia 					 loff_t *ppos)
5729d35a00b8SFelix Jia {
5730d35a00b8SFelix Jia 	int ret = 0;
5731d35a00b8SFelix Jia 	int new_val;
5732d35a00b8SFelix Jia 	struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5733d35a00b8SFelix Jia 	struct net *net = (struct net *)ctl->extra2;
5734d35a00b8SFelix Jia 
57358c171d6cSFelix Jia 	if (!rtnl_trylock())
57368c171d6cSFelix Jia 		return restart_syscall();
57378c171d6cSFelix Jia 
5738d35a00b8SFelix Jia 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5739d35a00b8SFelix Jia 
5740d35a00b8SFelix Jia 	if (write) {
5741d35a00b8SFelix Jia 		new_val = *((int *)ctl->data);
5742d35a00b8SFelix Jia 
57438c171d6cSFelix Jia 		if (check_addr_gen_mode(new_val) < 0) {
57448c171d6cSFelix Jia 			ret = -EINVAL;
57458c171d6cSFelix Jia 			goto out;
57468c171d6cSFelix Jia 		}
5747d35a00b8SFelix Jia 
5748d35a00b8SFelix Jia 		/* request for default */
5749d35a00b8SFelix Jia 		if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5750d35a00b8SFelix Jia 			ipv6_devconf_dflt.addr_gen_mode = new_val;
5751d35a00b8SFelix Jia 
5752d35a00b8SFelix Jia 		/* request for individual net device */
5753d35a00b8SFelix Jia 		} else {
5754d35a00b8SFelix Jia 			if (!idev)
57558c171d6cSFelix Jia 				goto out;
5756d35a00b8SFelix Jia 
57578c171d6cSFelix Jia 			if (check_stable_privacy(idev, net, new_val) < 0) {
57588c171d6cSFelix Jia 				ret = -EINVAL;
57598c171d6cSFelix Jia 				goto out;
57608c171d6cSFelix Jia 			}
5761d35a00b8SFelix Jia 
5762d35a00b8SFelix Jia 			if (idev->cnf.addr_gen_mode != new_val) {
5763d35a00b8SFelix Jia 				idev->cnf.addr_gen_mode = new_val;
5764d35a00b8SFelix Jia 				addrconf_dev_config(idev->dev);
57658c171d6cSFelix Jia 			}
57668c171d6cSFelix Jia 		}
57678c171d6cSFelix Jia 	}
57688c171d6cSFelix Jia 
57698c171d6cSFelix Jia out:
5770d35a00b8SFelix Jia 	rtnl_unlock();
5771d35a00b8SFelix Jia 
5772d35a00b8SFelix Jia 	return ret;
5773d35a00b8SFelix Jia }
5774d35a00b8SFelix Jia 
57753d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
57763d1bec99SHannes Frederic Sowa 					 void __user *buffer, size_t *lenp,
57773d1bec99SHannes Frederic Sowa 					 loff_t *ppos)
57783d1bec99SHannes Frederic Sowa {
57793d1bec99SHannes Frederic Sowa 	int err;
57803d1bec99SHannes Frederic Sowa 	struct in6_addr addr;
57813d1bec99SHannes Frederic Sowa 	char str[IPV6_MAX_STRLEN];
57823d1bec99SHannes Frederic Sowa 	struct ctl_table lctl = *ctl;
5783622c81d5SHannes Frederic Sowa 	struct net *net = ctl->extra2;
57843d1bec99SHannes Frederic Sowa 	struct ipv6_stable_secret *secret = ctl->data;
57853d1bec99SHannes Frederic Sowa 
5786622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5787622c81d5SHannes Frederic Sowa 		return -EIO;
5788622c81d5SHannes Frederic Sowa 
57893d1bec99SHannes Frederic Sowa 	lctl.maxlen = IPV6_MAX_STRLEN;
57903d1bec99SHannes Frederic Sowa 	lctl.data = str;
57913d1bec99SHannes Frederic Sowa 
57923d1bec99SHannes Frederic Sowa 	if (!rtnl_trylock())
57933d1bec99SHannes Frederic Sowa 		return restart_syscall();
57943d1bec99SHannes Frederic Sowa 
57953d1bec99SHannes Frederic Sowa 	if (!write && !secret->initialized) {
57963d1bec99SHannes Frederic Sowa 		err = -EIO;
57973d1bec99SHannes Frederic Sowa 		goto out;
57983d1bec99SHannes Frederic Sowa 	}
57993d1bec99SHannes Frederic Sowa 
58005449a5caSWANG Cong 	err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
58013d1bec99SHannes Frederic Sowa 	if (err >= sizeof(str)) {
58023d1bec99SHannes Frederic Sowa 		err = -EIO;
58033d1bec99SHannes Frederic Sowa 		goto out;
58043d1bec99SHannes Frederic Sowa 	}
58053d1bec99SHannes Frederic Sowa 
58063d1bec99SHannes Frederic Sowa 	err = proc_dostring(&lctl, write, buffer, lenp, ppos);
58073d1bec99SHannes Frederic Sowa 	if (err || !write)
58083d1bec99SHannes Frederic Sowa 		goto out;
58093d1bec99SHannes Frederic Sowa 
58103d1bec99SHannes Frederic Sowa 	if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
58113d1bec99SHannes Frederic Sowa 		err = -EIO;
58123d1bec99SHannes Frederic Sowa 		goto out;
58133d1bec99SHannes Frederic Sowa 	}
58143d1bec99SHannes Frederic Sowa 
58153d1bec99SHannes Frederic Sowa 	secret->initialized = true;
58163d1bec99SHannes Frederic Sowa 	secret->secret = addr;
58173d1bec99SHannes Frederic Sowa 
5818622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5819622c81d5SHannes Frederic Sowa 		struct net_device *dev;
5820622c81d5SHannes Frederic Sowa 
5821622c81d5SHannes Frederic Sowa 		for_each_netdev(net, dev) {
5822622c81d5SHannes Frederic Sowa 			struct inet6_dev *idev = __in6_dev_get(dev);
5823622c81d5SHannes Frederic Sowa 
5824622c81d5SHannes Frederic Sowa 			if (idev) {
5825d35a00b8SFelix Jia 				idev->cnf.addr_gen_mode =
5826622c81d5SHannes Frederic Sowa 					IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5827622c81d5SHannes Frederic Sowa 			}
5828622c81d5SHannes Frederic Sowa 		}
5829622c81d5SHannes Frederic Sowa 	} else {
5830622c81d5SHannes Frederic Sowa 		struct inet6_dev *idev = ctl->extra1;
5831622c81d5SHannes Frederic Sowa 
5832d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5833622c81d5SHannes Frederic Sowa 	}
5834622c81d5SHannes Frederic Sowa 
58353d1bec99SHannes Frederic Sowa out:
58363d1bec99SHannes Frederic Sowa 	rtnl_unlock();
58373d1bec99SHannes Frederic Sowa 
58383d1bec99SHannes Frederic Sowa 	return err;
58393d1bec99SHannes Frederic Sowa }
5840c92d5491Sstephen hemminger 
584135103d11SAndy Gospodarek static
584235103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
584335103d11SAndy Gospodarek 						int write,
584435103d11SAndy Gospodarek 						void __user *buffer,
584535103d11SAndy Gospodarek 						size_t *lenp,
584635103d11SAndy Gospodarek 						loff_t *ppos)
584735103d11SAndy Gospodarek {
584835103d11SAndy Gospodarek 	int *valp = ctl->data;
584935103d11SAndy Gospodarek 	int val = *valp;
585035103d11SAndy Gospodarek 	loff_t pos = *ppos;
585135103d11SAndy Gospodarek 	struct ctl_table lctl;
585235103d11SAndy Gospodarek 	int ret;
585335103d11SAndy Gospodarek 
585435103d11SAndy Gospodarek 	/* ctl->data points to idev->cnf.ignore_routes_when_linkdown
585535103d11SAndy Gospodarek 	 * we should not modify it until we get the rtnl lock.
585635103d11SAndy Gospodarek 	 */
585735103d11SAndy Gospodarek 	lctl = *ctl;
585835103d11SAndy Gospodarek 	lctl.data = &val;
585935103d11SAndy Gospodarek 
586035103d11SAndy Gospodarek 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
586135103d11SAndy Gospodarek 
586235103d11SAndy Gospodarek 	if (write)
586335103d11SAndy Gospodarek 		ret = addrconf_fixup_linkdown(ctl, valp, val);
586435103d11SAndy Gospodarek 	if (ret)
586535103d11SAndy Gospodarek 		*ppos = pos;
586635103d11SAndy Gospodarek 	return ret;
586735103d11SAndy Gospodarek }
586835103d11SAndy Gospodarek 
5869df789fe7SDavid Forster static
5870df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action)
5871df789fe7SDavid Forster {
5872df789fe7SDavid Forster 	if (rt) {
5873df789fe7SDavid Forster 		if (action)
5874df789fe7SDavid Forster 			rt->dst.flags |= DST_NOPOLICY;
5875df789fe7SDavid Forster 		else
5876df789fe7SDavid Forster 			rt->dst.flags &= ~DST_NOPOLICY;
5877df789fe7SDavid Forster 	}
5878df789fe7SDavid Forster }
5879df789fe7SDavid Forster 
5880df789fe7SDavid Forster static
5881df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
5882df789fe7SDavid Forster {
5883df789fe7SDavid Forster 	struct inet6_ifaddr *ifa;
5884df789fe7SDavid Forster 
5885df789fe7SDavid Forster 	read_lock_bh(&idev->lock);
5886df789fe7SDavid Forster 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
5887df789fe7SDavid Forster 		spin_lock(&ifa->lock);
5888df789fe7SDavid Forster 		if (ifa->rt) {
5889df789fe7SDavid Forster 			struct rt6_info *rt = ifa->rt;
5890df789fe7SDavid Forster 			int cpu;
5891df789fe7SDavid Forster 
589266f5d6ceSWei Wang 			rcu_read_lock();
5893df789fe7SDavid Forster 			addrconf_set_nopolicy(ifa->rt, val);
5894df789fe7SDavid Forster 			if (rt->rt6i_pcpu) {
5895df789fe7SDavid Forster 				for_each_possible_cpu(cpu) {
5896df789fe7SDavid Forster 					struct rt6_info **rtp;
5897df789fe7SDavid Forster 
5898df789fe7SDavid Forster 					rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
5899df789fe7SDavid Forster 					addrconf_set_nopolicy(*rtp, val);
5900df789fe7SDavid Forster 				}
5901df789fe7SDavid Forster 			}
590266f5d6ceSWei Wang 			rcu_read_unlock();
5903df789fe7SDavid Forster 		}
5904df789fe7SDavid Forster 		spin_unlock(&ifa->lock);
5905df789fe7SDavid Forster 	}
5906df789fe7SDavid Forster 	read_unlock_bh(&idev->lock);
5907df789fe7SDavid Forster }
5908df789fe7SDavid Forster 
5909df789fe7SDavid Forster static
5910df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
5911df789fe7SDavid Forster {
5912df789fe7SDavid Forster 	struct inet6_dev *idev;
5913df789fe7SDavid Forster 	struct net *net;
5914df789fe7SDavid Forster 
5915df789fe7SDavid Forster 	if (!rtnl_trylock())
5916df789fe7SDavid Forster 		return restart_syscall();
5917df789fe7SDavid Forster 
5918df789fe7SDavid Forster 	*valp = val;
5919df789fe7SDavid Forster 
5920df789fe7SDavid Forster 	net = (struct net *)ctl->extra2;
5921df789fe7SDavid Forster 	if (valp == &net->ipv6.devconf_dflt->disable_policy) {
5922df789fe7SDavid Forster 		rtnl_unlock();
5923df789fe7SDavid Forster 		return 0;
5924df789fe7SDavid Forster 	}
5925df789fe7SDavid Forster 
5926df789fe7SDavid Forster 	if (valp == &net->ipv6.devconf_all->disable_policy)  {
5927df789fe7SDavid Forster 		struct net_device *dev;
5928df789fe7SDavid Forster 
5929df789fe7SDavid Forster 		for_each_netdev(net, dev) {
5930df789fe7SDavid Forster 			idev = __in6_dev_get(dev);
5931df789fe7SDavid Forster 			if (idev)
5932df789fe7SDavid Forster 				addrconf_disable_policy_idev(idev, val);
5933df789fe7SDavid Forster 		}
5934df789fe7SDavid Forster 	} else {
5935df789fe7SDavid Forster 		idev = (struct inet6_dev *)ctl->extra1;
5936df789fe7SDavid Forster 		addrconf_disable_policy_idev(idev, val);
5937df789fe7SDavid Forster 	}
5938df789fe7SDavid Forster 
5939df789fe7SDavid Forster 	rtnl_unlock();
5940df789fe7SDavid Forster 	return 0;
5941df789fe7SDavid Forster }
5942df789fe7SDavid Forster 
5943df789fe7SDavid Forster static
5944df789fe7SDavid Forster int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
5945df789fe7SDavid Forster 				   void __user *buffer, size_t *lenp,
5946df789fe7SDavid Forster 				   loff_t *ppos)
5947df789fe7SDavid Forster {
5948df789fe7SDavid Forster 	int *valp = ctl->data;
5949df789fe7SDavid Forster 	int val = *valp;
5950df789fe7SDavid Forster 	loff_t pos = *ppos;
5951df789fe7SDavid Forster 	struct ctl_table lctl;
5952df789fe7SDavid Forster 	int ret;
5953df789fe7SDavid Forster 
5954df789fe7SDavid Forster 	lctl = *ctl;
5955df789fe7SDavid Forster 	lctl.data = &val;
5956df789fe7SDavid Forster 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5957df789fe7SDavid Forster 
5958df789fe7SDavid Forster 	if (write && (*valp != val))
5959df789fe7SDavid Forster 		ret = addrconf_disable_policy(ctl, valp, val);
5960df789fe7SDavid Forster 
5961df789fe7SDavid Forster 	if (ret)
5962df789fe7SDavid Forster 		*ppos = pos;
5963df789fe7SDavid Forster 
5964df789fe7SDavid Forster 	return ret;
5965df789fe7SDavid Forster }
5966df789fe7SDavid Forster 
5967cb4a4c69SMaciej Żenczykowski static int minus_one = -1;
5968cb9e684eSMaciej Żenczykowski static const int one = 1;
5969cb9e684eSMaciej Żenczykowski static const int two_five_five = 255;
5970cb9e684eSMaciej Żenczykowski 
5971607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = {
59721da177e4SLinus Torvalds 	{
59731da177e4SLinus Torvalds 		.procname	= "forwarding",
59741da177e4SLinus Torvalds 		.data		= &ipv6_devconf.forwarding,
59751da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59761da177e4SLinus Torvalds 		.mode		= 0644,
59776d9f239aSAlexey Dobriyan 		.proc_handler	= addrconf_sysctl_forward,
59781da177e4SLinus Torvalds 	},
59791da177e4SLinus Torvalds 	{
59801da177e4SLinus Torvalds 		.procname	= "hop_limit",
59811da177e4SLinus Torvalds 		.data		= &ipv6_devconf.hop_limit,
59821da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59831da177e4SLinus Torvalds 		.mode		= 0644,
5984cb9e684eSMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
5985cb9e684eSMaciej Żenczykowski 		.extra1		= (void *)&one,
5986cb9e684eSMaciej Żenczykowski 		.extra2		= (void *)&two_five_five,
59871da177e4SLinus Torvalds 	},
59881da177e4SLinus Torvalds 	{
59891da177e4SLinus Torvalds 		.procname	= "mtu",
59901da177e4SLinus Torvalds 		.data		= &ipv6_devconf.mtu6,
59911da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59921da177e4SLinus Torvalds 		.mode		= 0644,
599377751427SMarcelo Leitner 		.proc_handler	= addrconf_sysctl_mtu,
59941da177e4SLinus Torvalds 	},
59951da177e4SLinus Torvalds 	{
59961da177e4SLinus Torvalds 		.procname	= "accept_ra",
59971da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_ra,
59981da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59991da177e4SLinus Torvalds 		.mode		= 0644,
60006d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60011da177e4SLinus Torvalds 	},
60021da177e4SLinus Torvalds 	{
60031da177e4SLinus Torvalds 		.procname	= "accept_redirects",
60041da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_redirects,
60051da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60061da177e4SLinus Torvalds 		.mode		= 0644,
60076d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60081da177e4SLinus Torvalds 	},
60091da177e4SLinus Torvalds 	{
60101da177e4SLinus Torvalds 		.procname	= "autoconf",
60111da177e4SLinus Torvalds 		.data		= &ipv6_devconf.autoconf,
60121da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60131da177e4SLinus Torvalds 		.mode		= 0644,
60146d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60151da177e4SLinus Torvalds 	},
60161da177e4SLinus Torvalds 	{
60171da177e4SLinus Torvalds 		.procname	= "dad_transmits",
60181da177e4SLinus Torvalds 		.data		= &ipv6_devconf.dad_transmits,
60191da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60201da177e4SLinus Torvalds 		.mode		= 0644,
60216d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60221da177e4SLinus Torvalds 	},
60231da177e4SLinus Torvalds 	{
60241da177e4SLinus Torvalds 		.procname	= "router_solicitations",
60251da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicits,
60261da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60271da177e4SLinus Torvalds 		.mode		= 0644,
6028cb4a4c69SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
6029cb4a4c69SMaciej Żenczykowski 		.extra1		= &minus_one,
60301da177e4SLinus Torvalds 	},
60311da177e4SLinus Torvalds 	{
60321da177e4SLinus Torvalds 		.procname	= "router_solicitation_interval",
60331da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_interval,
60341da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60351da177e4SLinus Torvalds 		.mode		= 0644,
60366d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
60371da177e4SLinus Torvalds 	},
60381da177e4SLinus Torvalds 	{
6039bd11f074SMaciej Żenczykowski 		.procname	= "router_solicitation_max_interval",
6040bd11f074SMaciej Żenczykowski 		.data		= &ipv6_devconf.rtr_solicit_max_interval,
6041bd11f074SMaciej Żenczykowski 		.maxlen		= sizeof(int),
6042bd11f074SMaciej Żenczykowski 		.mode		= 0644,
6043bd11f074SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_jiffies,
6044bd11f074SMaciej Żenczykowski 	},
6045bd11f074SMaciej Żenczykowski 	{
60461da177e4SLinus Torvalds 		.procname	= "router_solicitation_delay",
60471da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_delay,
60481da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60491da177e4SLinus Torvalds 		.mode		= 0644,
60506d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
60511da177e4SLinus Torvalds 	},
60521da177e4SLinus Torvalds 	{
60531da177e4SLinus Torvalds 		.procname	= "force_mld_version",
60541da177e4SLinus Torvalds 		.data		= &ipv6_devconf.force_mld_version,
60551da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60561da177e4SLinus Torvalds 		.mode		= 0644,
60576d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60581da177e4SLinus Torvalds 	},
6059fc4eba58SHannes Frederic Sowa 	{
6060fc4eba58SHannes Frederic Sowa 		.procname	= "mldv1_unsolicited_report_interval",
6061fc4eba58SHannes Frederic Sowa 		.data		=
6062fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv1_unsolicited_report_interval,
6063fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6064fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
6065fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
6066fc4eba58SHannes Frederic Sowa 	},
6067fc4eba58SHannes Frederic Sowa 	{
6068fc4eba58SHannes Frederic Sowa 		.procname	= "mldv2_unsolicited_report_interval",
6069fc4eba58SHannes Frederic Sowa 		.data		=
6070fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv2_unsolicited_report_interval,
6071fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6072fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
6073fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
6074fc4eba58SHannes Frederic Sowa 	},
60751da177e4SLinus Torvalds 	{
60761da177e4SLinus Torvalds 		.procname	= "use_tempaddr",
60771da177e4SLinus Torvalds 		.data		= &ipv6_devconf.use_tempaddr,
60781da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60791da177e4SLinus Torvalds 		.mode		= 0644,
60806d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60811da177e4SLinus Torvalds 	},
60821da177e4SLinus Torvalds 	{
60831da177e4SLinus Torvalds 		.procname	= "temp_valid_lft",
60841da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_valid_lft,
60851da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60861da177e4SLinus Torvalds 		.mode		= 0644,
60876d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60881da177e4SLinus Torvalds 	},
60891da177e4SLinus Torvalds 	{
60901da177e4SLinus Torvalds 		.procname	= "temp_prefered_lft",
60911da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_prefered_lft,
60921da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60931da177e4SLinus Torvalds 		.mode		= 0644,
60946d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60951da177e4SLinus Torvalds 	},
60961da177e4SLinus Torvalds 	{
60971da177e4SLinus Torvalds 		.procname	= "regen_max_retry",
60981da177e4SLinus Torvalds 		.data		= &ipv6_devconf.regen_max_retry,
60991da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61001da177e4SLinus Torvalds 		.mode		= 0644,
61016d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61021da177e4SLinus Torvalds 	},
61031da177e4SLinus Torvalds 	{
61041da177e4SLinus Torvalds 		.procname	= "max_desync_factor",
61051da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_desync_factor,
61061da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61071da177e4SLinus Torvalds 		.mode		= 0644,
61086d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61091da177e4SLinus Torvalds 	},
61101da177e4SLinus Torvalds 	{
61111da177e4SLinus Torvalds 		.procname	= "max_addresses",
61121da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_addresses,
61131da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61141da177e4SLinus Torvalds 		.mode		= 0644,
61156d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61161da177e4SLinus Torvalds 	},
61171da177e4SLinus Torvalds 	{
611865f5c7c1SYOSHIFUJI Hideaki 		.procname	= "accept_ra_defrtr",
611965f5c7c1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_defrtr,
612065f5c7c1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
612165f5c7c1SYOSHIFUJI Hideaki 		.mode		= 0644,
61226d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
612365f5c7c1SYOSHIFUJI Hideaki 	},
612465f5c7c1SYOSHIFUJI Hideaki 	{
61258013d1d7SHangbin Liu 		.procname	= "accept_ra_min_hop_limit",
61268013d1d7SHangbin Liu 		.data		= &ipv6_devconf.accept_ra_min_hop_limit,
61278013d1d7SHangbin Liu 		.maxlen		= sizeof(int),
61288013d1d7SHangbin Liu 		.mode		= 0644,
61298013d1d7SHangbin Liu 		.proc_handler	= proc_dointvec,
61308013d1d7SHangbin Liu 	},
61318013d1d7SHangbin Liu 	{
6132c4fd30ebSYOSHIFUJI Hideaki 		.procname	= "accept_ra_pinfo",
6133c4fd30ebSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_pinfo,
6134c4fd30ebSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6135c4fd30ebSYOSHIFUJI Hideaki 		.mode		= 0644,
61366d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
6137c4fd30ebSYOSHIFUJI Hideaki 	},
6138930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
6139930d6ff2SYOSHIFUJI Hideaki 	{
6140930d6ff2SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rtr_pref",
6141930d6ff2SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rtr_pref,
6142930d6ff2SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6143930d6ff2SYOSHIFUJI Hideaki 		.mode		= 0644,
61446d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
6145930d6ff2SYOSHIFUJI Hideaki 	},
614652e16356SYOSHIFUJI Hideaki 	{
614752e16356SYOSHIFUJI Hideaki 		.procname	= "router_probe_interval",
614852e16356SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.rtr_probe_interval,
614952e16356SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
615052e16356SYOSHIFUJI Hideaki 		.mode		= 0644,
61516d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
615252e16356SYOSHIFUJI Hideaki 	},
6153fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
615409c884d4SYOSHIFUJI Hideaki 	{
6155bbea124bSJoel Scherpelz 		.procname	= "accept_ra_rt_info_min_plen",
6156bbea124bSJoel Scherpelz 		.data		= &ipv6_devconf.accept_ra_rt_info_min_plen,
6157bbea124bSJoel Scherpelz 		.maxlen		= sizeof(int),
6158bbea124bSJoel Scherpelz 		.mode		= 0644,
6159bbea124bSJoel Scherpelz 		.proc_handler	= proc_dointvec,
6160bbea124bSJoel Scherpelz 	},
6161bbea124bSJoel Scherpelz 	{
616209c884d4SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rt_info_max_plen",
616309c884d4SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
616409c884d4SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
616509c884d4SYOSHIFUJI Hideaki 		.mode		= 0644,
61666d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
616709c884d4SYOSHIFUJI Hideaki 	},
616809c884d4SYOSHIFUJI Hideaki #endif
6169930d6ff2SYOSHIFUJI Hideaki #endif
6170c4fd30ebSYOSHIFUJI Hideaki 	{
6171fbea49e1SYOSHIFUJI Hideaki 		.procname	= "proxy_ndp",
6172fbea49e1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.proxy_ndp,
6173fbea49e1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6174fbea49e1SYOSHIFUJI Hideaki 		.mode		= 0644,
6175c92d5491Sstephen hemminger 		.proc_handler	= addrconf_sysctl_proxy_ndp,
6176fbea49e1SYOSHIFUJI Hideaki 	},
6177fbea49e1SYOSHIFUJI Hideaki 	{
61780bcbc926SYOSHIFUJI Hideaki 		.procname	= "accept_source_route",
61790bcbc926SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_source_route,
61800bcbc926SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
61810bcbc926SYOSHIFUJI Hideaki 		.mode		= 0644,
61826d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61830bcbc926SYOSHIFUJI Hideaki 	},
618495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
618595c385b4SNeil Horman 	{
618695c385b4SNeil Horman 		.procname	= "optimistic_dad",
618795c385b4SNeil Horman 		.data		= &ipv6_devconf.optimistic_dad,
618895c385b4SNeil Horman 		.maxlen		= sizeof(int),
618995c385b4SNeil Horman 		.mode		= 0644,
61906d9f239aSAlexey Dobriyan 		.proc_handler   = proc_dointvec,
619195c385b4SNeil Horman 	},
61927fd2561eSErik Kline 	{
61937fd2561eSErik Kline 		.procname	= "use_optimistic",
61947fd2561eSErik Kline 		.data		= &ipv6_devconf.use_optimistic,
61957fd2561eSErik Kline 		.maxlen		= sizeof(int),
61967fd2561eSErik Kline 		.mode		= 0644,
61977fd2561eSErik Kline 		.proc_handler	= proc_dointvec,
61987fd2561eSErik Kline 	},
619995c385b4SNeil Horman #endif
62007bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
62017bc570c8SYOSHIFUJI Hideaki 	{
62027bc570c8SYOSHIFUJI Hideaki 		.procname	= "mc_forwarding",
62037bc570c8SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.mc_forwarding,
62047bc570c8SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6205a4e6db07SDavid S. Miller 		.mode		= 0444,
62066d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
62077bc570c8SYOSHIFUJI Hideaki 	},
62087bc570c8SYOSHIFUJI Hideaki #endif
62090bcbc926SYOSHIFUJI Hideaki 	{
6210778d80beSYOSHIFUJI Hideaki 		.procname	= "disable_ipv6",
6211778d80beSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.disable_ipv6,
6212778d80beSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6213778d80beSYOSHIFUJI Hideaki 		.mode		= 0644,
621456d417b1SBrian Haley 		.proc_handler	= addrconf_sysctl_disable,
6215778d80beSYOSHIFUJI Hideaki 	},
6216778d80beSYOSHIFUJI Hideaki 	{
62171b34be74SYOSHIFUJI Hideaki 		.procname	= "accept_dad",
62181b34be74SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_dad,
62191b34be74SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
62201b34be74SYOSHIFUJI Hideaki 		.mode		= 0644,
62216d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
62221b34be74SYOSHIFUJI Hideaki 	},
62231b34be74SYOSHIFUJI Hideaki 	{
6224f7734fdfSOctavian Purdila 		.procname	= "force_tllao",
6225f7734fdfSOctavian Purdila 		.data		= &ipv6_devconf.force_tllao,
6226f7734fdfSOctavian Purdila 		.maxlen		= sizeof(int),
6227f7734fdfSOctavian Purdila 		.mode		= 0644,
6228f7734fdfSOctavian Purdila 		.proc_handler	= proc_dointvec
6229f7734fdfSOctavian Purdila 	},
6230f7734fdfSOctavian Purdila 	{
62315cb04436SHannes Frederic Sowa 		.procname	= "ndisc_notify",
62325cb04436SHannes Frederic Sowa 		.data		= &ipv6_devconf.ndisc_notify,
62335cb04436SHannes Frederic Sowa 		.maxlen		= sizeof(int),
62345cb04436SHannes Frederic Sowa 		.mode		= 0644,
62355cb04436SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
62365cb04436SHannes Frederic Sowa 	},
62375cb04436SHannes Frederic Sowa 	{
6238b800c3b9SHannes Frederic Sowa 		.procname	= "suppress_frag_ndisc",
6239b800c3b9SHannes Frederic Sowa 		.data		= &ipv6_devconf.suppress_frag_ndisc,
6240b800c3b9SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6241b800c3b9SHannes Frederic Sowa 		.mode		= 0644,
6242b800c3b9SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
6243b800c3b9SHannes Frederic Sowa 	},
6244b800c3b9SHannes Frederic Sowa 	{
6245d9333196SBen Greear 		.procname	= "accept_ra_from_local",
6246d9333196SBen Greear 		.data		= &ipv6_devconf.accept_ra_from_local,
6247d9333196SBen Greear 		.maxlen		= sizeof(int),
6248d9333196SBen Greear 		.mode		= 0644,
6249d9333196SBen Greear 		.proc_handler	= proc_dointvec,
6250d9333196SBen Greear 	},
6251d9333196SBen Greear 	{
6252c2943f14SHarout Hedeshian 		.procname	= "accept_ra_mtu",
6253c2943f14SHarout Hedeshian 		.data		= &ipv6_devconf.accept_ra_mtu,
6254c2943f14SHarout Hedeshian 		.maxlen		= sizeof(int),
6255c2943f14SHarout Hedeshian 		.mode		= 0644,
6256c2943f14SHarout Hedeshian 		.proc_handler	= proc_dointvec,
6257c2943f14SHarout Hedeshian 	},
6258c2943f14SHarout Hedeshian 	{
62593d1bec99SHannes Frederic Sowa 		.procname	= "stable_secret",
62603d1bec99SHannes Frederic Sowa 		.data		= &ipv6_devconf.stable_secret,
62613d1bec99SHannes Frederic Sowa 		.maxlen		= IPV6_MAX_STRLEN,
62623d1bec99SHannes Frederic Sowa 		.mode		= 0600,
62633d1bec99SHannes Frederic Sowa 		.proc_handler	= addrconf_sysctl_stable_secret,
62643d1bec99SHannes Frederic Sowa 	},
62653d1bec99SHannes Frederic Sowa 	{
62663985e8a3SErik Kline 		.procname	= "use_oif_addrs_only",
62673985e8a3SErik Kline 		.data		= &ipv6_devconf.use_oif_addrs_only,
62683985e8a3SErik Kline 		.maxlen		= sizeof(int),
62693985e8a3SErik Kline 		.mode		= 0644,
62703985e8a3SErik Kline 		.proc_handler	= proc_dointvec,
627135103d11SAndy Gospodarek 	},
627235103d11SAndy Gospodarek 	{
627335103d11SAndy Gospodarek 		.procname	= "ignore_routes_with_linkdown",
627435103d11SAndy Gospodarek 		.data		= &ipv6_devconf.ignore_routes_with_linkdown,
627535103d11SAndy Gospodarek 		.maxlen		= sizeof(int),
627635103d11SAndy Gospodarek 		.mode		= 0644,
627735103d11SAndy Gospodarek 		.proc_handler	= addrconf_sysctl_ignore_routes_with_linkdown,
62783985e8a3SErik Kline 	},
62793985e8a3SErik Kline 	{
6280abbc3043SJohannes Berg 		.procname	= "drop_unicast_in_l2_multicast",
6281abbc3043SJohannes Berg 		.data		= &ipv6_devconf.drop_unicast_in_l2_multicast,
6282abbc3043SJohannes Berg 		.maxlen		= sizeof(int),
6283abbc3043SJohannes Berg 		.mode		= 0644,
6284abbc3043SJohannes Berg 		.proc_handler	= proc_dointvec,
6285abbc3043SJohannes Berg 	},
6286abbc3043SJohannes Berg 	{
62877a02bf89SJohannes Berg 		.procname	= "drop_unsolicited_na",
62887a02bf89SJohannes Berg 		.data		= &ipv6_devconf.drop_unsolicited_na,
62897a02bf89SJohannes Berg 		.maxlen		= sizeof(int),
62907a02bf89SJohannes Berg 		.mode		= 0644,
62917a02bf89SJohannes Berg 		.proc_handler	= proc_dointvec,
62927a02bf89SJohannes Berg 	},
62937a02bf89SJohannes Berg 	{
6294f1705ec1SDavid Ahern 		.procname	= "keep_addr_on_down",
6295f1705ec1SDavid Ahern 		.data		= &ipv6_devconf.keep_addr_on_down,
6296f1705ec1SDavid Ahern 		.maxlen		= sizeof(int),
6297f1705ec1SDavid Ahern 		.mode		= 0644,
6298f1705ec1SDavid Ahern 		.proc_handler	= proc_dointvec,
6299f1705ec1SDavid Ahern 
6300f1705ec1SDavid Ahern 	},
6301f1705ec1SDavid Ahern 	{
63021ababebaSDavid Lebrun 		.procname	= "seg6_enabled",
63031ababebaSDavid Lebrun 		.data		= &ipv6_devconf.seg6_enabled,
63041ababebaSDavid Lebrun 		.maxlen		= sizeof(int),
63051ababebaSDavid Lebrun 		.mode		= 0644,
63061ababebaSDavid Lebrun 		.proc_handler	= proc_dointvec,
63071ababebaSDavid Lebrun 	},
6308bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
6309bf355b8dSDavid Lebrun 	{
6310bf355b8dSDavid Lebrun 		.procname	= "seg6_require_hmac",
6311bf355b8dSDavid Lebrun 		.data		= &ipv6_devconf.seg6_require_hmac,
6312bf355b8dSDavid Lebrun 		.maxlen		= sizeof(int),
6313bf355b8dSDavid Lebrun 		.mode		= 0644,
6314bf355b8dSDavid Lebrun 		.proc_handler	= proc_dointvec,
6315bf355b8dSDavid Lebrun 	},
6316bf355b8dSDavid Lebrun #endif
63171ababebaSDavid Lebrun 	{
6318adc176c5SErik Nordmark 		.procname       = "enhanced_dad",
6319adc176c5SErik Nordmark 		.data           = &ipv6_devconf.enhanced_dad,
6320adc176c5SErik Nordmark 		.maxlen         = sizeof(int),
6321adc176c5SErik Nordmark 		.mode           = 0644,
6322adc176c5SErik Nordmark 		.proc_handler   = proc_dointvec,
6323adc176c5SErik Nordmark 	},
6324adc176c5SErik Nordmark 	{
6325d35a00b8SFelix Jia 		.procname		= "addr_gen_mode",
6326d35a00b8SFelix Jia 		.data			= &ipv6_devconf.addr_gen_mode,
6327d35a00b8SFelix Jia 		.maxlen			= sizeof(int),
6328d35a00b8SFelix Jia 		.mode			= 0644,
6329d35a00b8SFelix Jia 		.proc_handler	= addrconf_sysctl_addr_gen_mode,
6330d35a00b8SFelix Jia 	},
6331d35a00b8SFelix Jia 	{
6332df789fe7SDavid Forster 		.procname       = "disable_policy",
6333df789fe7SDavid Forster 		.data           = &ipv6_devconf.disable_policy,
6334df789fe7SDavid Forster 		.maxlen         = sizeof(int),
6335df789fe7SDavid Forster 		.mode           = 0644,
6336df789fe7SDavid Forster 		.proc_handler   = addrconf_sysctl_disable_policy,
6337df789fe7SDavid Forster 	},
6338df789fe7SDavid Forster 	{
6339f8572d8fSEric W. Biederman 		/* sentinel */
63401da177e4SLinus Torvalds 	}
63411da177e4SLinus Torvalds };
63421da177e4SLinus Torvalds 
6343bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6344f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
63451da177e4SLinus Torvalds {
634629c994e3SNicolas Dichtel 	int i, ifindex;
6347607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
63486105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
63491dab6222SPavel Emelyanov 
6350607ea7cdSKonstantin Khlebnikov 	table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6351607ea7cdSKonstantin Khlebnikov 	if (!table)
6352f68635e6SPavel Emelyanov 		goto out;
6353f68635e6SPavel Emelyanov 
6354607ea7cdSKonstantin Khlebnikov 	for (i = 0; table[i].data; i++) {
6355607ea7cdSKonstantin Khlebnikov 		table[i].data += (char *)p - (char *)&ipv6_devconf;
6356cb9e684eSMaciej Żenczykowski 		/* If one of these is already set, then it is not safe to
6357cb9e684eSMaciej Żenczykowski 		 * overwrite either of them: this makes proc_dointvec_minmax
6358cb9e684eSMaciej Żenczykowski 		 * usable.
6359cb9e684eSMaciej Żenczykowski 		 */
6360cb9e684eSMaciej Żenczykowski 		if (!table[i].extra1 && !table[i].extra2) {
6361607ea7cdSKonstantin Khlebnikov 			table[i].extra1 = idev; /* embedded; no ref */
6362607ea7cdSKonstantin Khlebnikov 			table[i].extra2 = net;
63631da177e4SLinus Torvalds 		}
6364cb9e684eSMaciej Żenczykowski 	}
63651da177e4SLinus Torvalds 
63666105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
63671da177e4SLinus Torvalds 
6368607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = register_net_sysctl(net, path, table);
6369607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
63706105e293SEric W. Biederman 		goto free;
6371f68635e6SPavel Emelyanov 
637229c994e3SNicolas Dichtel 	if (!strcmp(dev_name, "all"))
637329c994e3SNicolas Dichtel 		ifindex = NETCONFA_IFINDEX_ALL;
637429c994e3SNicolas Dichtel 	else if (!strcmp(dev_name, "default"))
637529c994e3SNicolas Dichtel 		ifindex = NETCONFA_IFINDEX_DEFAULT;
637629c994e3SNicolas Dichtel 	else
637729c994e3SNicolas Dichtel 		ifindex = idev->dev->ifindex;
637885b3daadSDavid Ahern 	inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
637985b3daadSDavid Ahern 				     ifindex, p);
638095897312SPavel Emelyanov 	return 0;
63811da177e4SLinus Torvalds 
63821da177e4SLinus Torvalds free:
6383607ea7cdSKonstantin Khlebnikov 	kfree(table);
6384f68635e6SPavel Emelyanov out:
638595897312SPavel Emelyanov 	return -ENOBUFS;
63861da177e4SLinus Torvalds }
63871da177e4SLinus Torvalds 
638823452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net,
638923452170SDavid Ahern 					 struct ipv6_devconf *p, int ifindex)
6390f52295a9SPavel Emelyanov {
6391607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
6392f52295a9SPavel Emelyanov 
6393607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
6394408c4768SPavel Emelyanov 		return;
6395408c4768SPavel Emelyanov 
6396607ea7cdSKonstantin Khlebnikov 	table = p->sysctl_header->ctl_table_arg;
6397607ea7cdSKonstantin Khlebnikov 	unregister_net_sysctl_table(p->sysctl_header);
6398607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = NULL;
6399607ea7cdSKonstantin Khlebnikov 	kfree(table);
640023452170SDavid Ahern 
640123452170SDavid Ahern 	inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
64021da177e4SLinus Torvalds }
6403408c4768SPavel Emelyanov 
6404a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev)
6405408c4768SPavel Emelyanov {
6406a317a2f1SWANG Cong 	int err;
6407a317a2f1SWANG Cong 
6408a317a2f1SWANG Cong 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
6409a317a2f1SWANG Cong 		return -EINVAL;
6410a317a2f1SWANG Cong 
6411a317a2f1SWANG Cong 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6412f8572d8fSEric W. Biederman 				    &ndisc_ifinfo_sysctl_change);
6413a317a2f1SWANG Cong 	if (err)
6414a317a2f1SWANG Cong 		return err;
6415a317a2f1SWANG Cong 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6416f8572d8fSEric W. Biederman 					 idev, &idev->cnf);
6417a317a2f1SWANG Cong 	if (err)
6418a317a2f1SWANG Cong 		neigh_sysctl_unregister(idev->nd_parms);
6419a317a2f1SWANG Cong 
6420a317a2f1SWANG Cong 	return err;
6421408c4768SPavel Emelyanov }
6422408c4768SPavel Emelyanov 
6423408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6424408c4768SPavel Emelyanov {
642523452170SDavid Ahern 	__addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
642623452170SDavid Ahern 				     idev->dev->ifindex);
6427408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
64281da177e4SLinus Torvalds }
64291da177e4SLinus Torvalds 
64301da177e4SLinus Torvalds 
64311da177e4SLinus Torvalds #endif
64321da177e4SLinus Torvalds 
64332c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
6434e0da5a48SPavel Emelyanov {
6435a79ca223SHong Zhiguo 	int err = -ENOMEM;
6436e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
6437e0da5a48SPavel Emelyanov 
6438a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
643963159f29SIan Morris 	if (!all)
6440e0da5a48SPavel Emelyanov 		goto err_alloc_all;
6441e0da5a48SPavel Emelyanov 
6442a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
644363159f29SIan Morris 	if (!dflt)
6444e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
6445a79ca223SHong Zhiguo 
644656d417b1SBrian Haley 	/* these will be inherited by all namespaces */
644756d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
644856d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6449e0da5a48SPavel Emelyanov 
64503d1bec99SHannes Frederic Sowa 	dflt->stable_secret.initialized = false;
64513d1bec99SHannes Frederic Sowa 	all->stable_secret.initialized = false;
64523d1bec99SHannes Frederic Sowa 
6453e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
6454e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
6455e0da5a48SPavel Emelyanov 
6456e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
6457f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
6458e0da5a48SPavel Emelyanov 	if (err < 0)
6459e0da5a48SPavel Emelyanov 		goto err_reg_all;
6460e0da5a48SPavel Emelyanov 
6461f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6462e0da5a48SPavel Emelyanov 	if (err < 0)
6463e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
6464e0da5a48SPavel Emelyanov #endif
6465e0da5a48SPavel Emelyanov 	return 0;
6466e0da5a48SPavel Emelyanov 
6467e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
6468e0da5a48SPavel Emelyanov err_reg_dflt:
646923452170SDavid Ahern 	__addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
6470e0da5a48SPavel Emelyanov err_reg_all:
6471e0da5a48SPavel Emelyanov 	kfree(dflt);
6472e0da5a48SPavel Emelyanov #endif
6473e0da5a48SPavel Emelyanov err_alloc_dflt:
6474e0da5a48SPavel Emelyanov 	kfree(all);
6475e0da5a48SPavel Emelyanov err_alloc_all:
6476e0da5a48SPavel Emelyanov 	return err;
6477e0da5a48SPavel Emelyanov }
6478e0da5a48SPavel Emelyanov 
64792c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
6480e0da5a48SPavel Emelyanov {
6481e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
648223452170SDavid Ahern 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
648323452170SDavid Ahern 				     NETCONFA_IFINDEX_DEFAULT);
648423452170SDavid Ahern 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
648523452170SDavid Ahern 				     NETCONFA_IFINDEX_ALL);
6486e0da5a48SPavel Emelyanov #endif
6487e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_dflt);
6488e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_all);
6489e0da5a48SPavel Emelyanov }
6490e0da5a48SPavel Emelyanov 
6491e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
6492e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
6493e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
6494e0da5a48SPavel Emelyanov };
6495e0da5a48SPavel Emelyanov 
6496207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = {
6497b382b191SThomas Graf 	.family		  = AF_INET6,
6498b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
6499b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
650011b1f828SDaniel Borkmann 	.validate_link_af = inet6_validate_link_af,
6501f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
6502b382b191SThomas Graf };
6503b382b191SThomas Graf 
65041da177e4SLinus Torvalds /*
65051da177e4SLinus Torvalds  *	Init / cleanup code
65061da177e4SLinus Torvalds  */
65071da177e4SLinus Torvalds 
65081da177e4SLinus Torvalds int __init addrconf_init(void)
65091da177e4SLinus Torvalds {
6510a317a2f1SWANG Cong 	struct inet6_dev *idev;
6511c2e21293Sstephen hemminger 	int i, err;
65122a8cc6c8SYOSHIFUJI Hideaki 
6513e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
6514e21e8467SStephen Hemminger 	if (err < 0) {
6515f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
6516f3213831SJoe Perches 			__func__, err);
65172cc6d2bfSNeil Horman 		goto out;
65182a8cc6c8SYOSHIFUJI Hideaki 	}
65191da177e4SLinus Torvalds 
65202cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
65212cc6d2bfSNeil Horman 	if (err < 0)
65222cc6d2bfSNeil Horman 		goto out_addrlabel;
6523e0da5a48SPavel Emelyanov 
6524c15b1ccaSHannes Frederic Sowa 	addrconf_wq = create_workqueue("ipv6_addrconf");
6525c15b1ccaSHannes Frederic Sowa 	if (!addrconf_wq) {
6526c15b1ccaSHannes Frederic Sowa 		err = -ENOMEM;
6527c15b1ccaSHannes Frederic Sowa 		goto out_nowq;
6528c15b1ccaSHannes Frederic Sowa 	}
6529c15b1ccaSHannes Frederic Sowa 
65301da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
65311da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
65321da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
65331da177e4SLinus Torvalds 	 * to other devices which are up.
65341da177e4SLinus Torvalds 	 *
65351da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
65361da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
65371da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
65381da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
65391da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
65401da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
65411da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
65421da177e4SLinus Torvalds 	 *
65431da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
65441da177e4SLinus Torvalds 	 * loopback_dev first by hand.
65451da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
65461da177e4SLinus Torvalds 	 * device and it being up should be removed.
65471da177e4SLinus Torvalds 	 */
65481da177e4SLinus Torvalds 	rtnl_lock();
6549a317a2f1SWANG Cong 	idev = ipv6_add_dev(init_net.loopback_dev);
65501da177e4SLinus Torvalds 	rtnl_unlock();
6551a317a2f1SWANG Cong 	if (IS_ERR(idev)) {
6552a317a2f1SWANG Cong 		err = PTR_ERR(idev);
6553e0da5a48SPavel Emelyanov 		goto errlo;
6554a317a2f1SWANG Cong 	}
65551da177e4SLinus Torvalds 
65562f460933SWANG Cong 	ip6_route_init_special_entries();
65572f460933SWANG Cong 
6558c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
6559c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6560c2e21293Sstephen hemminger 
65611da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
65621da177e4SLinus Torvalds 
6563c15b1ccaSHannes Frederic Sowa 	addrconf_verify();
6564c127ea2cSThomas Graf 
65653678a9d8Sstephen hemminger 	rtnl_af_register(&inet6_ops);
6566b382b191SThomas Graf 
6567c7ac8679SGreg Rose 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6568b97bac64SFlorian Westphal 			      0);
6569c127ea2cSThomas Graf 	if (err < 0)
6570c127ea2cSThomas Graf 		goto errout;
6571c127ea2cSThomas Graf 
6572c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
6573b97bac64SFlorian Westphal 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, 0);
6574b97bac64SFlorian Westphal 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, 0);
6575c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6576b97bac64SFlorian Westphal 			inet6_dump_ifaddr, 0);
6577c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6578b97bac64SFlorian Westphal 			inet6_dump_ifmcaddr, 0);
6579c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6580b97bac64SFlorian Westphal 			inet6_dump_ifacaddr, 0);
658176f8f6cbSNicolas Dichtel 	__rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
6582b97bac64SFlorian Westphal 			inet6_netconf_dump_devconf, 0);
6583c127ea2cSThomas Graf 
65842a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
65852a8cc6c8SYOSHIFUJI Hideaki 
65861da177e4SLinus Torvalds 	return 0;
6587c127ea2cSThomas Graf errout:
6588b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
6589c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
6590e0da5a48SPavel Emelyanov errlo:
6591c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
6592c15b1ccaSHannes Frederic Sowa out_nowq:
6593e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
65942cc6d2bfSNeil Horman out_addrlabel:
65952cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
65962cc6d2bfSNeil Horman out:
6597c127ea2cSThomas Graf 	return err;
65981da177e4SLinus Torvalds }
65991da177e4SLinus Torvalds 
660009f7709fSDaniel Lezcano void addrconf_cleanup(void)
66011da177e4SLinus Torvalds {
6602176c39afSDaniel Lezcano 	struct net_device *dev;
66031da177e4SLinus Torvalds 	int i;
66041da177e4SLinus Torvalds 
66051da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
6606e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
66072cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
66081da177e4SLinus Torvalds 
66095c45121dSFlorian Westphal 	rtnl_af_unregister(&inet6_ops);
66101da177e4SLinus Torvalds 
66115c45121dSFlorian Westphal 	rtnl_lock();
6612b382b191SThomas Graf 
6613176c39afSDaniel Lezcano 	/* clean dev list */
6614176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
6615176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
6616176c39afSDaniel Lezcano 			continue;
6617176c39afSDaniel Lezcano 		addrconf_ifdown(dev, 1);
6618176c39afSDaniel Lezcano 	}
6619176c39afSDaniel Lezcano 	addrconf_ifdown(init_net.loopback_dev, 2);
6620176c39afSDaniel Lezcano 
66211da177e4SLinus Torvalds 	/*
66221da177e4SLinus Torvalds 	 *	Check hash table.
66231da177e4SLinus Torvalds 	 */
66245c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
6625c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
6626c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
66275c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
6628c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
66291da177e4SLinus Torvalds 	rtnl_unlock();
6630c15b1ccaSHannes Frederic Sowa 
6631c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
66321da177e4SLinus Torvalds }
6633