xref: /openbmc/linux/net/ipv6/addrconf.c (revision 81f6cb31222d286dab65579d61f96664eeb99e0b)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	IPv6 Address [auto]configuration
41da177e4SLinus Torvalds  *	Linux INET6 implementation
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	Authors:
71da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
81da177e4SLinus Torvalds  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /*
121da177e4SLinus Torvalds  *	Changes:
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *	Janos Farkas			:	delete timer on ifdown
151da177e4SLinus Torvalds  *	<chexum@bankinf.banki.hu>
161da177e4SLinus Torvalds  *	Andi Kleen			:	kill double kfree on module
171da177e4SLinus Torvalds  *						unload.
181da177e4SLinus Torvalds  *	Maciej W. Rozycki		:	FDDI support
191da177e4SLinus Torvalds  *	sekiya@USAGI			:	Don't send too many RS
201da177e4SLinus Torvalds  *						packets.
211da177e4SLinus Torvalds  *	yoshfuji@USAGI			:       Fixed interval between DAD
221da177e4SLinus Torvalds  *						packets.
231da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	improved accuracy of
241da177e4SLinus Torvalds  *						address validation timer.
251da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	Privacy Extensions (RFC3041)
261da177e4SLinus Torvalds  *						support.
271da177e4SLinus Torvalds  *	Yuji SEKIYA @USAGI		:	Don't assign a same IPv6
281da177e4SLinus Torvalds  *						address on a same interface.
291da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	ARCnet support
301da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	convert /proc/net/if_inet6 to
311da177e4SLinus Torvalds  *						seq_file.
32b1cacb68SYOSHIFUJI Hideaki  *	YOSHIFUJI Hideaki @USAGI	:	improved source address
33b1cacb68SYOSHIFUJI Hideaki  *						selection; consider scope,
34b1cacb68SYOSHIFUJI Hideaki  *						status etc.
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
37f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
38f3213831SJoe Perches 
391da177e4SLinus Torvalds #include <linux/errno.h>
401da177e4SLinus Torvalds #include <linux/types.h>
41a0bffffcSIlpo Järvinen #include <linux/kernel.h>
42174cd4b1SIngo Molnar #include <linux/sched/signal.h>
431da177e4SLinus Torvalds #include <linux/socket.h>
441da177e4SLinus Torvalds #include <linux/sockios.h>
451da177e4SLinus Torvalds #include <linux/net.h>
463d1bec99SHannes Frederic Sowa #include <linux/inet.h>
471da177e4SLinus Torvalds #include <linux/in6.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
491823730fSThomas Graf #include <linux/if_addr.h>
501da177e4SLinus Torvalds #include <linux/if_arp.h>
511da177e4SLinus Torvalds #include <linux/if_arcnet.h>
521da177e4SLinus Torvalds #include <linux/if_infiniband.h>
531da177e4SLinus Torvalds #include <linux/route.h>
541da177e4SLinus Torvalds #include <linux/inetdevice.h>
551da177e4SLinus Torvalds #include <linux/init.h>
565a0e3ad6STejun Heo #include <linux/slab.h>
571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
581da177e4SLinus Torvalds #include <linux/sysctl.h>
591da177e4SLinus Torvalds #endif
604fc268d2SRandy Dunlap #include <linux/capability.h>
611da177e4SLinus Torvalds #include <linux/delay.h>
621da177e4SLinus Torvalds #include <linux/notifier.h>
63543537bdSPaulo Marques #include <linux/string.h>
64ddbe5032SEric Dumazet #include <linux/hash.h>
651da177e4SLinus Torvalds 
66457c4cbcSEric W. Biederman #include <net/net_namespace.h>
671da177e4SLinus Torvalds #include <net/sock.h>
681da177e4SLinus Torvalds #include <net/snmp.h>
691da177e4SLinus Torvalds 
705241c2d7SAlexander Aring #include <net/6lowpan.h>
71cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h>
721da177e4SLinus Torvalds #include <net/ipv6.h>
731da177e4SLinus Torvalds #include <net/protocol.h>
741da177e4SLinus Torvalds #include <net/ndisc.h>
751da177e4SLinus Torvalds #include <net/ip6_route.h>
761da177e4SLinus Torvalds #include <net/addrconf.h>
771da177e4SLinus Torvalds #include <net/tcp.h>
781da177e4SLinus Torvalds #include <net/ip.h>
795d620266SThomas Graf #include <net/netlink.h>
80f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
81ca254490SDavid Ahern #include <net/l3mdev.h>
821da177e4SLinus Torvalds #include <linux/if_tunnel.h>
831da177e4SLinus Torvalds #include <linux/rtnetlink.h>
84f3a1bfb1SNicolas Dichtel #include <linux/netconf.h>
851da177e4SLinus Torvalds #include <linux/random.h>
86e21e8467SStephen Hemminger #include <linux/uaccess.h>
877f7d9a6bSHerbert Xu #include <asm/unaligned.h>
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds #include <linux/proc_fs.h>
901da177e4SLinus Torvalds #include <linux/seq_file.h>
91bc3b2d7fSPaul Gortmaker #include <linux/export.h>
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
9418a31e1eSThomas Graf 
953d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \
963d1bec99SHannes Frederic Sowa 	sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
973d1bec99SHannes Frederic Sowa 
9818a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp)
9918a31e1eSThomas Graf {
10018a31e1eSThomas Graf 	return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
10118a31e1eSThomas Graf }
1021da177e4SLinus Torvalds 
103bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_init(s32 irt)
104bd11f074SMaciej Żenczykowski {
105bd11f074SMaciej Żenczykowski 	/* multiply 'initial retransmission time' by 0.9 .. 1.1 */
106bd11f074SMaciej Żenczykowski 	u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
107bd11f074SMaciej Żenczykowski 	do_div(tmp, 1000000);
108bd11f074SMaciej Żenczykowski 	return (s32)tmp;
109bd11f074SMaciej Żenczykowski }
110bd11f074SMaciej Żenczykowski 
111bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
112bd11f074SMaciej Żenczykowski {
113bd11f074SMaciej Żenczykowski 	/* multiply 'retransmission timeout' by 1.9 .. 2.1 */
114bd11f074SMaciej Żenczykowski 	u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
115bd11f074SMaciej Żenczykowski 	do_div(tmp, 1000000);
116bd11f074SMaciej Żenczykowski 	if ((s32)tmp > mrt) {
117bd11f074SMaciej Żenczykowski 		/* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
118bd11f074SMaciej Żenczykowski 		tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
119bd11f074SMaciej Żenczykowski 		do_div(tmp, 1000000);
120bd11f074SMaciej Żenczykowski 	}
121bd11f074SMaciej Żenczykowski 	return (s32)tmp;
122bd11f074SMaciej Żenczykowski }
123bd11f074SMaciej Żenczykowski 
1241da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
125a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev);
126408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
127408c4768SPavel Emelyanov #else
128a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev)
129408c4768SPavel Emelyanov {
130a317a2f1SWANG Cong 	return 0;
131408c4768SPavel Emelyanov }
132408c4768SPavel Emelyanov 
133408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
134408c4768SPavel Emelyanov {
135408c4768SPavel Emelyanov }
1361da177e4SLinus Torvalds #endif
1371da177e4SLinus Torvalds 
138969c5464SFernando Gont static void ipv6_gen_rnd_iid(struct in6_addr *addr);
1391da177e4SLinus Torvalds 
1401b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
141d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev);
142622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr,
143622c81d5SHannes Frederic Sowa 					u8 dad_count,
144622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev);
1451da177e4SLinus Torvalds 
14627c565aeSEric Dumazet #define IN6_ADDR_HSIZE_SHIFT	8
14727c565aeSEric Dumazet #define IN6_ADDR_HSIZE		(1 << IN6_ADDR_HSIZE_SHIFT)
1481da177e4SLinus Torvalds /*
1491da177e4SLinus Torvalds  *	Configured unicast address hash table
1501da177e4SLinus Torvalds  */
151c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
1525c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock);
1531da177e4SLinus Torvalds 
154c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void);
155c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void);
156c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *);
1571da177e4SLinus Torvalds 
158c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq;
159c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1621da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1631da177e4SLinus Torvalds 
16493d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev,
16575c78500SMoni Shoua 				 unsigned long event);
166ae79dbf6SFlorent Fourcot static int addrconf_ifdown(struct net_device *dev, bool unregister);
1671da177e4SLinus Torvalds 
1688d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
16921caa662SRomain Kuntz 						  int plen,
17021caa662SRomain Kuntz 						  const struct net_device *dev,
1712b2450caSDavid Ahern 						  u32 flags, u32 noflags,
1722b2450caSDavid Ahern 						  bool no_gw);
17321caa662SRomain Kuntz 
174cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp);
175c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w);
176c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
177c76fe2d9SDavid Ahern 				   bool send_na);
178896585d4SHangbin Liu static void addrconf_dad_run(struct inet6_dev *idev, bool restart);
179e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t);
1801da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1811da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1841da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1851da177e4SLinus Torvalds 
186888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1871da177e4SLinus Torvalds 	.forwarding		= 0,
1881da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1891da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1901da177e4SLinus Torvalds 	.accept_ra		= 1,
1911da177e4SLinus Torvalds 	.accept_redirects	= 1,
1921da177e4SLinus Torvalds 	.autoconf		= 1,
1931da177e4SLinus Torvalds 	.force_mld_version	= 0,
194fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
195fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
1961da177e4SLinus Torvalds 	.dad_transmits		= 1,
1971da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1981da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
199bd11f074SMaciej Żenczykowski 	.rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
2001da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2011da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2021da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2031da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2041da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2051da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2061da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
20765f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
208d9333196SBen Greear 	.accept_ra_from_local	= 0,
2098013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
210c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
211930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
212930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
21352e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
21409c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
215bbea124bSJoel Scherpelz 	.accept_ra_rt_info_min_plen = 0,
21609c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
21709c884d4SYOSHIFUJI Hideaki #endif
218930d6ff2SYOSHIFUJI Hideaki #endif
219fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2200bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
221778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
22209400953SNicolas Dichtel 	.accept_dad		= 0,
223b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
224c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2253d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2263d1bec99SHannes Frederic Sowa 		.initialized = false,
2273985e8a3SErik Kline 	},
2283985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
22935103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
230f1705ec1SDavid Ahern 	.keep_addr_on_down	= 0,
2311ababebaSDavid Lebrun 	.seg6_enabled		= 0,
232bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
233bf355b8dSDavid Lebrun 	.seg6_require_hmac	= 0,
234bf355b8dSDavid Lebrun #endif
235adc176c5SErik Nordmark 	.enhanced_dad           = 1,
236d35a00b8SFelix Jia 	.addr_gen_mode		= IN6_ADDR_GEN_MODE_EUI64,
237df789fe7SDavid Forster 	.disable_policy		= 0,
2388610c7c6SAlexander Aring 	.rpl_seg_enabled	= 0,
2391da177e4SLinus Torvalds };
2401da177e4SLinus Torvalds 
241ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2421da177e4SLinus Torvalds 	.forwarding		= 0,
2431da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2441da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2451da177e4SLinus Torvalds 	.accept_ra		= 1,
2461da177e4SLinus Torvalds 	.accept_redirects	= 1,
2471da177e4SLinus Torvalds 	.autoconf		= 1,
248fc4eba58SHannes Frederic Sowa 	.force_mld_version	= 0,
249fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
250fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2511da177e4SLinus Torvalds 	.dad_transmits		= 1,
2521da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2531da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
254bd11f074SMaciej Żenczykowski 	.rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
2551da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2561da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2571da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2581da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2591da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2601da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2611da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
26265f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
263d9333196SBen Greear 	.accept_ra_from_local	= 0,
2648013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
265c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
266930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
267930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
26852e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
26909c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
270bbea124bSJoel Scherpelz 	.accept_ra_rt_info_min_plen = 0,
27109c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
27209c884d4SYOSHIFUJI Hideaki #endif
273930d6ff2SYOSHIFUJI Hideaki #endif
274fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2750bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
276778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2771b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
278b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
279c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2803d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2813d1bec99SHannes Frederic Sowa 		.initialized = false,
2823d1bec99SHannes Frederic Sowa 	},
2833985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
28435103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
285f1705ec1SDavid Ahern 	.keep_addr_on_down	= 0,
2861ababebaSDavid Lebrun 	.seg6_enabled		= 0,
287bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
288bf355b8dSDavid Lebrun 	.seg6_require_hmac	= 0,
289bf355b8dSDavid Lebrun #endif
290adc176c5SErik Nordmark 	.enhanced_dad           = 1,
291d35a00b8SFelix Jia 	.addr_gen_mode		= IN6_ADDR_GEN_MODE_EUI64,
292df789fe7SDavid Forster 	.disable_policy		= 0,
2938610c7c6SAlexander Aring 	.rpl_seg_enabled	= 0,
2941da177e4SLinus Torvalds };
2951da177e4SLinus Torvalds 
2961f372c7bSMike Manning /* Check if link is ready: is it up and is a valid qdisc available */
2971f372c7bSMike Manning static inline bool addrconf_link_ready(const struct net_device *dev)
298f24e3d65SMitsuru Chinen {
2991f372c7bSMike Manning 	return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
300f24e3d65SMitsuru Chinen }
301f24e3d65SMitsuru Chinen 
302b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev)
3031da177e4SLinus Torvalds {
304b7b1bfceSHannes Frederic Sowa 	if (del_timer(&idev->rs_timer))
305b7b1bfceSHannes Frederic Sowa 		__in6_dev_put(idev);
306b7b1bfceSHannes Frederic Sowa }
307b7b1bfceSHannes Frederic Sowa 
308c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
309b7b1bfceSHannes Frederic Sowa {
310c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
3111da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds 
314b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev,
3151da177e4SLinus Torvalds 				  unsigned long when)
3161da177e4SLinus Torvalds {
317b7b1bfceSHannes Frederic Sowa 	if (!timer_pending(&idev->rs_timer))
318b7b1bfceSHannes Frederic Sowa 		in6_dev_hold(idev);
319b7b1bfceSHannes Frederic Sowa 	mod_timer(&idev->rs_timer, jiffies + when);
3201da177e4SLinus Torvalds }
321b7b1bfceSHannes Frederic Sowa 
322c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
323c15b1ccaSHannes Frederic Sowa 				   unsigned long delay)
324b7b1bfceSHannes Frederic Sowa {
325b7b1bfceSHannes Frederic Sowa 	in6_ifa_hold(ifp);
326f8a894b2SXin Long 	if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
327f8a894b2SXin Long 		in6_ifa_put(ifp);
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3307f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
3317f7d9a6bSHerbert Xu {
332827da44cSJohn Stultz 	int i;
333827da44cSJohn Stultz 
334698365faSWANG Cong 	idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
335698365faSWANG Cong 	if (!idev->stats.ipv6)
3367f7d9a6bSHerbert Xu 		goto err_ip;
337827da44cSJohn Stultz 
338827da44cSJohn Stultz 	for_each_possible_cpu(i) {
339827da44cSJohn Stultz 		struct ipstats_mib *addrconf_stats;
340698365faSWANG Cong 		addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
341827da44cSJohn Stultz 		u64_stats_init(&addrconf_stats->syncp);
342827da44cSJohn Stultz 	}
343827da44cSJohn Stultz 
344827da44cSJohn Stultz 
345be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
346be281e55SEric Dumazet 					GFP_KERNEL);
347be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
3487f7d9a6bSHerbert Xu 		goto err_icmp;
349be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
350be281e55SEric Dumazet 					   GFP_KERNEL);
351be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
35214878f75SDavid L Stevens 		goto err_icmpmsg;
3537f7d9a6bSHerbert Xu 
3547f7d9a6bSHerbert Xu 	return 0;
3557f7d9a6bSHerbert Xu 
35614878f75SDavid L Stevens err_icmpmsg:
357be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3587f7d9a6bSHerbert Xu err_icmp:
359698365faSWANG Cong 	free_percpu(idev->stats.ipv6);
3607f7d9a6bSHerbert Xu err_ip:
361aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3627f7d9a6bSHerbert Xu }
3637f7d9a6bSHerbert Xu 
3641da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	struct inet6_dev *ndev;
367a317a2f1SWANG Cong 	int err = -ENOMEM;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	ASSERT_RTNL();
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
372a317a2f1SWANG Cong 		return ERR_PTR(-EINVAL);
3731da177e4SLinus Torvalds 
374322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
37563159f29SIan Morris 	if (!ndev)
376a317a2f1SWANG Cong 		return ERR_PTR(err);
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3791da177e4SLinus Torvalds 	ndev->dev = dev;
380502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
381e99e88a9SKees Cook 	timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0);
382c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3839b29c696SHannes Frederic Sowa 
3849b29c696SHannes Frederic Sowa 	if (ndev->cnf.stable_secret.initialized)
385d35a00b8SFelix Jia 		ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
3869b29c696SHannes Frederic Sowa 
3871da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3881da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
38963159f29SIan Morris 	if (!ndev->nd_parms) {
3901da177e4SLinus Torvalds 		kfree(ndev);
391a317a2f1SWANG Cong 		return ERR_PTR(err);
3921da177e4SLinus Torvalds 	}
3930187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
3940187bdfbSBen Hutchings 		dev_disable_lro(dev);
3951da177e4SLinus Torvalds 	/* We refer to the device */
3961da177e4SLinus Torvalds 	dev_hold(dev);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
399e32ac250SJoe Perches 		netdev_dbg(dev, "%s: cannot allocate memory for statistics\n",
400e32ac250SJoe Perches 			   __func__);
4011da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
4028603e33dSRoy Li 		dev_put(dev);
4038603e33dSRoy Li 		kfree(ndev);
404a317a2f1SWANG Cong 		return ERR_PTR(err);
4051da177e4SLinus Torvalds 	}
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
408e32ac250SJoe Perches 		netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n",
409ba3542e1Sdingtianhong 			   __func__, dev->name);
410a317a2f1SWANG Cong 		goto err_release;
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 
4139d6280daSJiri Bohac 	/* One reference from device. */
4141be92460SReshetova, Elena 	refcount_set(&ndev->refcnt, 1);
4151da177e4SLinus Torvalds 
4161b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
4171b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
4181b34be74SYOSHIFUJI Hideaki 
41907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
420c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
421f3213831SJoe Perches 		pr_info("%s: Disabled Multicast RS\n", dev->name);
422c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
423c7dc89c0SFred L. Templin 	}
424b077d7abSYOSHIFUJI Hideaki #endif
425b077d7abSYOSHIFUJI Hideaki 
426372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
42776506a98SJiri Bohac 	ndev->desync_factor = U32_MAX;
4281da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
4291da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4309625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4311da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4321da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4330be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
434969c5464SFernando Gont 	}
4355d9efa7eSDavid S. Miller 
436914faa14SDaniel Borkmann 	ndev->token = in6addr_any;
4371da177e4SLinus Torvalds 
4381f372c7bSMike Manning 	if (netif_running(dev) && addrconf_link_ready(dev))
43953aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
44053aadcc9SHerbert Xu 
4411da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4421da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
443a317a2f1SWANG Cong 	err = addrconf_sysctl_register(ndev);
444a317a2f1SWANG Cong 	if (err) {
445a317a2f1SWANG Cong 		ipv6_mc_destroy_dev(ndev);
4462a189f9eSSabrina Dubroca 		snmp6_unregister_dev(ndev);
447a317a2f1SWANG Cong 		goto err_release;
448a317a2f1SWANG Cong 	}
44930c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
450cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip6_ptr, ndev);
451d88ae4ccSYOSHIFUJI Hideaki 
4522c5e8933SHannes Frederic Sowa 	/* Join interface-local all-node multicast group */
4532c5e8933SHannes Frederic Sowa 	ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4542c5e8933SHannes Frederic Sowa 
455d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
456f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
457d88ae4ccSYOSHIFUJI Hideaki 
458d6ddef9eSLi Wei 	/* Join all-router multicast group if forwarding is set */
4598b2aaedeSLi Wei 	if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
460d6ddef9eSLi Wei 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
461d6ddef9eSLi Wei 
4621da177e4SLinus Torvalds 	return ndev;
463a317a2f1SWANG Cong 
464a317a2f1SWANG Cong err_release:
465a317a2f1SWANG Cong 	neigh_parms_release(&nd_tbl, ndev->nd_parms);
466a317a2f1SWANG Cong 	ndev->dead = 1;
467a317a2f1SWANG Cong 	in6_dev_finish_destroy(ndev);
468a317a2f1SWANG Cong 	return ERR_PTR(err);
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
471cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	struct inet6_dev *idev;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	ASSERT_RTNL();
4761da177e4SLinus Torvalds 
477e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
478e21e8467SStephen Hemminger 	if (!idev) {
479e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
480a317a2f1SWANG Cong 		if (IS_ERR(idev))
481db0b99f5SSabrina Dubroca 			return idev;
4821da177e4SLinus Torvalds 	}
483c5e33bddSYOSHIFUJI Hideaki 
4841da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4851da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4861da177e4SLinus Torvalds 	return idev;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
489f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
490f3a1bfb1SNicolas Dichtel {
491f3a1bfb1SNicolas Dichtel 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
492f3a1bfb1SNicolas Dichtel 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
493136ba622SZhang Shengju 	bool all = false;
494f3a1bfb1SNicolas Dichtel 
495136ba622SZhang Shengju 	if (type == NETCONFA_ALL)
496136ba622SZhang Shengju 		all = true;
497136ba622SZhang Shengju 
498136ba622SZhang Shengju 	if (all || type == NETCONFA_FORWARDING)
499f3a1bfb1SNicolas Dichtel 		size += nla_total_size(4);
500b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
501136ba622SZhang Shengju 	if (all || type == NETCONFA_MC_FORWARDING)
502d67b8c61SNicolas Dichtel 		size += nla_total_size(4);
503b1afce95SDavid S. Miller #endif
504136ba622SZhang Shengju 	if (all || type == NETCONFA_PROXY_NEIGH)
505c92d5491Sstephen hemminger 		size += nla_total_size(4);
506f3a1bfb1SNicolas Dichtel 
507136ba622SZhang Shengju 	if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
50835103d11SAndy Gospodarek 		size += nla_total_size(4);
50935103d11SAndy Gospodarek 
510f3a1bfb1SNicolas Dichtel 	return size;
511f3a1bfb1SNicolas Dichtel }
512f3a1bfb1SNicolas Dichtel 
513f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
514f3a1bfb1SNicolas Dichtel 				      struct ipv6_devconf *devconf, u32 portid,
515f3a1bfb1SNicolas Dichtel 				      u32 seq, int event, unsigned int flags,
516f3a1bfb1SNicolas Dichtel 				      int type)
517f3a1bfb1SNicolas Dichtel {
518f3a1bfb1SNicolas Dichtel 	struct nlmsghdr  *nlh;
519f3a1bfb1SNicolas Dichtel 	struct netconfmsg *ncm;
520136ba622SZhang Shengju 	bool all = false;
521f3a1bfb1SNicolas Dichtel 
522f3a1bfb1SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
523f3a1bfb1SNicolas Dichtel 			flags);
52463159f29SIan Morris 	if (!nlh)
525f3a1bfb1SNicolas Dichtel 		return -EMSGSIZE;
526f3a1bfb1SNicolas Dichtel 
527136ba622SZhang Shengju 	if (type == NETCONFA_ALL)
528136ba622SZhang Shengju 		all = true;
529136ba622SZhang Shengju 
530f3a1bfb1SNicolas Dichtel 	ncm = nlmsg_data(nlh);
531f3a1bfb1SNicolas Dichtel 	ncm->ncm_family = AF_INET6;
532f3a1bfb1SNicolas Dichtel 
533f3a1bfb1SNicolas Dichtel 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
534f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
535f3a1bfb1SNicolas Dichtel 
53623452170SDavid Ahern 	if (!devconf)
53723452170SDavid Ahern 		goto out;
53823452170SDavid Ahern 
539136ba622SZhang Shengju 	if ((all || type == NETCONFA_FORWARDING) &&
540f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
541f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
542b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
543136ba622SZhang Shengju 	if ((all || type == NETCONFA_MC_FORWARDING) &&
544d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
545d67b8c61SNicolas Dichtel 			devconf->mc_forwarding) < 0)
546d67b8c61SNicolas Dichtel 		goto nla_put_failure;
547b1afce95SDavid S. Miller #endif
548136ba622SZhang Shengju 	if ((all || type == NETCONFA_PROXY_NEIGH) &&
549c92d5491Sstephen hemminger 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
550c92d5491Sstephen hemminger 		goto nla_put_failure;
551c92d5491Sstephen hemminger 
552136ba622SZhang Shengju 	if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
55335103d11SAndy Gospodarek 	    nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
55435103d11SAndy Gospodarek 			devconf->ignore_routes_with_linkdown) < 0)
55535103d11SAndy Gospodarek 		goto nla_put_failure;
55635103d11SAndy Gospodarek 
55723452170SDavid Ahern out:
558053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
559053c095aSJohannes Berg 	return 0;
560f3a1bfb1SNicolas Dichtel 
561f3a1bfb1SNicolas Dichtel nla_put_failure:
562f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
563f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
564f3a1bfb1SNicolas Dichtel }
565f3a1bfb1SNicolas Dichtel 
56685b3daadSDavid Ahern void inet6_netconf_notify_devconf(struct net *net, int event, int type,
56785b3daadSDavid Ahern 				  int ifindex, struct ipv6_devconf *devconf)
568f3a1bfb1SNicolas Dichtel {
569f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
570f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
571f3a1bfb1SNicolas Dichtel 
572927265bcSEric Dumazet 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
57363159f29SIan Morris 	if (!skb)
574f3a1bfb1SNicolas Dichtel 		goto errout;
575f3a1bfb1SNicolas Dichtel 
576f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
57785b3daadSDavid Ahern 					 event, 0, type);
578f3a1bfb1SNicolas Dichtel 	if (err < 0) {
579f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
580f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
581f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
582f3a1bfb1SNicolas Dichtel 		goto errout;
583f3a1bfb1SNicolas Dichtel 	}
584927265bcSEric Dumazet 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
585f3a1bfb1SNicolas Dichtel 	return;
586f3a1bfb1SNicolas Dichtel errout:
587f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
588f3a1bfb1SNicolas Dichtel }
589f3a1bfb1SNicolas Dichtel 
59076f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
59176f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
59276f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
593c92d5491Sstephen hemminger 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
59435103d11SAndy Gospodarek 	[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]	= { .len = sizeof(int) },
59576f8f6cbSNicolas Dichtel };
59676f8f6cbSNicolas Dichtel 
59738d51810SJakub Kicinski static int inet6_netconf_valid_get_req(struct sk_buff *skb,
59838d51810SJakub Kicinski 				       const struct nlmsghdr *nlh,
59938d51810SJakub Kicinski 				       struct nlattr **tb,
60038d51810SJakub Kicinski 				       struct netlink_ext_ack *extack)
60138d51810SJakub Kicinski {
60238d51810SJakub Kicinski 	int i, err;
60338d51810SJakub Kicinski 
60438d51810SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
60538d51810SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request");
60638d51810SJakub Kicinski 		return -EINVAL;
60738d51810SJakub Kicinski 	}
60838d51810SJakub Kicinski 
60938d51810SJakub Kicinski 	if (!netlink_strict_get_check(skb))
6108cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
6118cb08174SJohannes Berg 					      tb, NETCONFA_MAX,
6128cb08174SJohannes Berg 					      devconf_ipv6_policy, extack);
61338d51810SJakub Kicinski 
6148cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
6158cb08174SJohannes Berg 					    tb, NETCONFA_MAX,
6168cb08174SJohannes Berg 					    devconf_ipv6_policy, extack);
61738d51810SJakub Kicinski 	if (err)
61838d51810SJakub Kicinski 		return err;
61938d51810SJakub Kicinski 
62038d51810SJakub Kicinski 	for (i = 0; i <= NETCONFA_MAX; i++) {
62138d51810SJakub Kicinski 		if (!tb[i])
62238d51810SJakub Kicinski 			continue;
62338d51810SJakub Kicinski 
62438d51810SJakub Kicinski 		switch (i) {
62538d51810SJakub Kicinski 		case NETCONFA_IFINDEX:
62638d51810SJakub Kicinski 			break;
62738d51810SJakub Kicinski 		default:
62838d51810SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
62938d51810SJakub Kicinski 			return -EINVAL;
63038d51810SJakub Kicinski 		}
63138d51810SJakub Kicinski 	}
63238d51810SJakub Kicinski 
63338d51810SJakub Kicinski 	return 0;
63438d51810SJakub Kicinski }
63538d51810SJakub Kicinski 
63676f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
637c21ef3e3SDavid Ahern 				     struct nlmsghdr *nlh,
638c21ef3e3SDavid Ahern 				     struct netlink_ext_ack *extack)
63976f8f6cbSNicolas Dichtel {
64076f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
64176f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
6424ea2607fSFlorian Westphal 	struct inet6_dev *in6_dev = NULL;
6434ea2607fSFlorian Westphal 	struct net_device *dev = NULL;
64476f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
64576f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
64676f8f6cbSNicolas Dichtel 	int ifindex;
64776f8f6cbSNicolas Dichtel 	int err;
64876f8f6cbSNicolas Dichtel 
64938d51810SJakub Kicinski 	err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack);
65076f8f6cbSNicolas Dichtel 	if (err < 0)
6514ea2607fSFlorian Westphal 		return err;
6524ea2607fSFlorian Westphal 
6534ea2607fSFlorian Westphal 	if (!tb[NETCONFA_IFINDEX])
6544ea2607fSFlorian Westphal 		return -EINVAL;
65576f8f6cbSNicolas Dichtel 
656a97eb33fSAnton Protopopov 	err = -EINVAL;
65776f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
65876f8f6cbSNicolas Dichtel 	switch (ifindex) {
65976f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
66076f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
66176f8f6cbSNicolas Dichtel 		break;
66276f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
66376f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
66476f8f6cbSNicolas Dichtel 		break;
66576f8f6cbSNicolas Dichtel 	default:
6664ea2607fSFlorian Westphal 		dev = dev_get_by_index(net, ifindex);
66763159f29SIan Morris 		if (!dev)
6684ea2607fSFlorian Westphal 			return -EINVAL;
6694ea2607fSFlorian Westphal 		in6_dev = in6_dev_get(dev);
67063159f29SIan Morris 		if (!in6_dev)
67176f8f6cbSNicolas Dichtel 			goto errout;
67276f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
67376f8f6cbSNicolas Dichtel 		break;
67476f8f6cbSNicolas Dichtel 	}
67576f8f6cbSNicolas Dichtel 
67676f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
6774ea2607fSFlorian Westphal 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
67863159f29SIan Morris 	if (!skb)
67976f8f6cbSNicolas Dichtel 		goto errout;
68076f8f6cbSNicolas Dichtel 
68176f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
68276f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
68376f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
684136ba622SZhang Shengju 					 NETCONFA_ALL);
68576f8f6cbSNicolas Dichtel 	if (err < 0) {
68676f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
68776f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
68876f8f6cbSNicolas Dichtel 		kfree_skb(skb);
68976f8f6cbSNicolas Dichtel 		goto errout;
69076f8f6cbSNicolas Dichtel 	}
69176f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
69276f8f6cbSNicolas Dichtel errout:
6934ea2607fSFlorian Westphal 	if (in6_dev)
6944ea2607fSFlorian Westphal 		in6_dev_put(in6_dev);
6954ea2607fSFlorian Westphal 	if (dev)
6964ea2607fSFlorian Westphal 		dev_put(dev);
69776f8f6cbSNicolas Dichtel 	return err;
69876f8f6cbSNicolas Dichtel }
69976f8f6cbSNicolas Dichtel 
7007a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
7017a674200SNicolas Dichtel 				      struct netlink_callback *cb)
7027a674200SNicolas Dichtel {
703addd383fSDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
7047a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
7057a674200SNicolas Dichtel 	int h, s_h;
7067a674200SNicolas Dichtel 	int idx, s_idx;
7077a674200SNicolas Dichtel 	struct net_device *dev;
7087a674200SNicolas Dichtel 	struct inet6_dev *idev;
7097a674200SNicolas Dichtel 	struct hlist_head *head;
7107a674200SNicolas Dichtel 
711addd383fSDavid Ahern 	if (cb->strict_check) {
712addd383fSDavid Ahern 		struct netlink_ext_ack *extack = cb->extack;
713addd383fSDavid Ahern 		struct netconfmsg *ncm;
714addd383fSDavid Ahern 
715addd383fSDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
716addd383fSDavid Ahern 			NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
717addd383fSDavid Ahern 			return -EINVAL;
718addd383fSDavid Ahern 		}
719addd383fSDavid Ahern 
720addd383fSDavid Ahern 		if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
721addd383fSDavid Ahern 			NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
722addd383fSDavid Ahern 			return -EINVAL;
723addd383fSDavid Ahern 		}
724addd383fSDavid Ahern 	}
725addd383fSDavid Ahern 
7267a674200SNicolas Dichtel 	s_h = cb->args[0];
7277a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
7287a674200SNicolas Dichtel 
7297a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
7307a674200SNicolas Dichtel 		idx = 0;
7317a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
7327a674200SNicolas Dichtel 		rcu_read_lock();
73363998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
73463998ac2SNicolas Dichtel 			  net->dev_base_seq;
7357a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
7367a674200SNicolas Dichtel 			if (idx < s_idx)
7377a674200SNicolas Dichtel 				goto cont;
7387a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
7397a674200SNicolas Dichtel 			if (!idev)
7407a674200SNicolas Dichtel 				goto cont;
7417a674200SNicolas Dichtel 
7427a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
7437a674200SNicolas Dichtel 						       &idev->cnf,
7447a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
745addd383fSDavid Ahern 						       nlh->nlmsg_seq,
7467a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
7477a674200SNicolas Dichtel 						       NLM_F_MULTI,
748136ba622SZhang Shengju 						       NETCONFA_ALL) < 0) {
7497a674200SNicolas Dichtel 				rcu_read_unlock();
7507a674200SNicolas Dichtel 				goto done;
7517a674200SNicolas Dichtel 			}
75263998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7537a674200SNicolas Dichtel cont:
7547a674200SNicolas Dichtel 			idx++;
7557a674200SNicolas Dichtel 		}
7567a674200SNicolas Dichtel 		rcu_read_unlock();
7577a674200SNicolas Dichtel 	}
7587a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
7597a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
7607a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
7617a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
762addd383fSDavid Ahern 					       nlh->nlmsg_seq,
7637a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
764136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
7657a674200SNicolas Dichtel 			goto done;
7667a674200SNicolas Dichtel 		else
7677a674200SNicolas Dichtel 			h++;
7687a674200SNicolas Dichtel 	}
7697a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
7707a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
7717a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
7727a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
773addd383fSDavid Ahern 					       nlh->nlmsg_seq,
7747a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
775136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
7767a674200SNicolas Dichtel 			goto done;
7777a674200SNicolas Dichtel 		else
7787a674200SNicolas Dichtel 			h++;
7797a674200SNicolas Dichtel 	}
7807a674200SNicolas Dichtel done:
7817a674200SNicolas Dichtel 	cb->args[0] = h;
7827a674200SNicolas Dichtel 	cb->args[1] = idx;
7837a674200SNicolas Dichtel 
7847a674200SNicolas Dichtel 	return skb->len;
7857a674200SNicolas Dichtel }
7867a674200SNicolas Dichtel 
7871da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
7881da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
7891da177e4SLinus Torvalds {
7901da177e4SLinus Torvalds 	struct net_device *dev;
7911da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	if (!idev)
7941da177e4SLinus Torvalds 		return;
7951da177e4SLinus Torvalds 	dev = idev->dev;
7960187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
7970187bdfbSBen Hutchings 		dev_disable_lro(dev);
7981a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
7992c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
800f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
8012c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
8022c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
8032c5e8933SHannes Frederic Sowa 		} else {
804f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
8052c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
8062c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
8072c5e8933SHannes Frederic Sowa 		}
8081da177e4SLinus Torvalds 	}
809502a2ffdSstephen hemminger 
810502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
8112c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
8122c12a74cSMichal Wrobel 			continue;
8131da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
8141da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
8151da177e4SLinus Torvalds 		else
8161da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
8171da177e4SLinus Torvalds 	}
81885b3daadSDavid Ahern 	inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
81985b3daadSDavid Ahern 				     NETCONFA_FORWARDING,
820f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 
824e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
8251da177e4SLinus Torvalds {
8261da177e4SLinus Torvalds 	struct net_device *dev;
8271da177e4SLinus Torvalds 	struct inet6_dev *idev;
8281da177e4SLinus Torvalds 
8294acd4945SBen Hutchings 	for_each_netdev(net, dev) {
8301da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
8311da177e4SLinus Torvalds 		if (idev) {
832e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
833e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
8341da177e4SLinus Torvalds 			if (changed)
8351da177e4SLinus Torvalds 				dev_forward_change(idev);
8361da177e4SLinus Torvalds 		}
8371da177e4SLinus Torvalds 	}
8381da177e4SLinus Torvalds }
839c8fecf22SPavel Emelyanov 
840013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
841c8fecf22SPavel Emelyanov {
842bff16c2fSPavel Emelyanov 	struct net *net;
843013d97e9SFrancesco Ruggeri 	int old;
844013d97e9SFrancesco Ruggeri 
845013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
846013d97e9SFrancesco Ruggeri 		return restart_syscall();
847bff16c2fSPavel Emelyanov 
848bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
849013d97e9SFrancesco Ruggeri 	old = *p;
850013d97e9SFrancesco Ruggeri 	*p = newf;
851c8fecf22SPavel Emelyanov 
852013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
853f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
85485b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
85585b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
856f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
857f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
858013d97e9SFrancesco Ruggeri 		rtnl_unlock();
859013d97e9SFrancesco Ruggeri 		return 0;
86088af182eSEric W. Biederman 	}
861b325fddbSStephen Hemminger 
862e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
863d26c638cSNicolas Dichtel 		int old_dflt = net->ipv6.devconf_dflt->forwarding;
864d26c638cSNicolas Dichtel 
865e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
866d26c638cSNicolas Dichtel 		if ((!newf) ^ (!old_dflt))
86785b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
86885b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
869d26c638cSNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
870d26c638cSNicolas Dichtel 						     net->ipv6.devconf_dflt);
871d26c638cSNicolas Dichtel 
872e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
873f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
87485b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
87585b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
876f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
877f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
878013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
879c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
8800187bdfbSBen Hutchings 	rtnl_unlock();
881c8fecf22SPavel Emelyanov 
882013d97e9SFrancesco Ruggeri 	if (newf)
8837b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
884b325fddbSStephen Hemminger 	return 1;
885c8fecf22SPavel Emelyanov }
88635103d11SAndy Gospodarek 
88735103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf)
88835103d11SAndy Gospodarek {
88935103d11SAndy Gospodarek 	struct net_device *dev;
89035103d11SAndy Gospodarek 	struct inet6_dev *idev;
89135103d11SAndy Gospodarek 
89235103d11SAndy Gospodarek 	for_each_netdev(net, dev) {
89335103d11SAndy Gospodarek 		idev = __in6_dev_get(dev);
89435103d11SAndy Gospodarek 		if (idev) {
89535103d11SAndy Gospodarek 			int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
89635103d11SAndy Gospodarek 
89735103d11SAndy Gospodarek 			idev->cnf.ignore_routes_with_linkdown = newf;
89835103d11SAndy Gospodarek 			if (changed)
89935103d11SAndy Gospodarek 				inet6_netconf_notify_devconf(dev_net(dev),
90085b3daadSDavid Ahern 							     RTM_NEWNETCONF,
90135103d11SAndy Gospodarek 							     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
90235103d11SAndy Gospodarek 							     dev->ifindex,
90335103d11SAndy Gospodarek 							     &idev->cnf);
90435103d11SAndy Gospodarek 		}
90535103d11SAndy Gospodarek 	}
90635103d11SAndy Gospodarek }
90735103d11SAndy Gospodarek 
90835103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
90935103d11SAndy Gospodarek {
91035103d11SAndy Gospodarek 	struct net *net;
91135103d11SAndy Gospodarek 	int old;
91235103d11SAndy Gospodarek 
91335103d11SAndy Gospodarek 	if (!rtnl_trylock())
91435103d11SAndy Gospodarek 		return restart_syscall();
91535103d11SAndy Gospodarek 
91635103d11SAndy Gospodarek 	net = (struct net *)table->extra2;
91735103d11SAndy Gospodarek 	old = *p;
91835103d11SAndy Gospodarek 	*p = newf;
91935103d11SAndy Gospodarek 
92035103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
92135103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
92235103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
92385b3daadSDavid Ahern 						     RTM_NEWNETCONF,
92435103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
92535103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_DEFAULT,
92635103d11SAndy Gospodarek 						     net->ipv6.devconf_dflt);
92735103d11SAndy Gospodarek 		rtnl_unlock();
92835103d11SAndy Gospodarek 		return 0;
92935103d11SAndy Gospodarek 	}
93035103d11SAndy Gospodarek 
93135103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
93235103d11SAndy Gospodarek 		net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
93335103d11SAndy Gospodarek 		addrconf_linkdown_change(net, newf);
93435103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
93535103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
93685b3daadSDavid Ahern 						     RTM_NEWNETCONF,
93735103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
93835103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_ALL,
93935103d11SAndy Gospodarek 						     net->ipv6.devconf_all);
94035103d11SAndy Gospodarek 	}
94135103d11SAndy Gospodarek 	rtnl_unlock();
94235103d11SAndy Gospodarek 
94335103d11SAndy Gospodarek 	return 1;
94435103d11SAndy Gospodarek }
94535103d11SAndy Gospodarek 
9461da177e4SLinus Torvalds #endif
9471da177e4SLinus Torvalds 
9485c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
9491da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
9501da177e4SLinus Torvalds {
951c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
952547b792cSIlpo Järvinen 
9531da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
95491df42beSJoe Perches 	pr_debug("%s\n", __func__);
9551da177e4SLinus Torvalds #endif
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
9581da177e4SLinus Torvalds 
959c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
960c15b1ccaSHannes Frederic Sowa 		pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
961c15b1ccaSHannes Frederic Sowa 			  ifp);
9621da177e4SLinus Torvalds 
963e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
964f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
9651da177e4SLinus Torvalds 		return;
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds 
968e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
971e55ffac6SBrian Haley static void
972e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
973e55ffac6SBrian Haley {
974502a2ffdSstephen hemminger 	struct list_head *p;
9758a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
976e55ffac6SBrian Haley 
977e55ffac6SBrian Haley 	/*
978e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
979e55ffac6SBrian Haley 	 * global before linklocal.
980e55ffac6SBrian Haley 	 */
981502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
982502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
983502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
9848a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
985e55ffac6SBrian Haley 			break;
986e55ffac6SBrian Haley 	}
987e55ffac6SBrian Haley 
9888ef802aaSEric Dumazet 	list_add_tail_rcu(&ifp->if_list, p);
989e55ffac6SBrian Haley }
990e55ffac6SBrian Haley 
9913f27fb23SEric Dumazet static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
9923eb84f49SYOSHIFUJI Hideaki {
9933f27fb23SEric Dumazet 	u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
9943f27fb23SEric Dumazet 
9953f27fb23SEric Dumazet 	return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
9963eb84f49SYOSHIFUJI Hideaki }
9973eb84f49SYOSHIFUJI Hideaki 
99856fc709bSEric Dumazet static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
999752a9292SEric Dumazet 			       struct net_device *dev, unsigned int hash)
100056fc709bSEric Dumazet {
100156fc709bSEric Dumazet 	struct inet6_ifaddr *ifp;
100256fc709bSEric Dumazet 
100356fc709bSEric Dumazet 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
100456fc709bSEric Dumazet 		if (!net_eq(dev_net(ifp->idev->dev), net))
100556fc709bSEric Dumazet 			continue;
100656fc709bSEric Dumazet 		if (ipv6_addr_equal(&ifp->addr, addr)) {
100756fc709bSEric Dumazet 			if (!dev || ifp->idev->dev == dev)
100856fc709bSEric Dumazet 				return true;
100956fc709bSEric Dumazet 		}
101056fc709bSEric Dumazet 	}
101156fc709bSEric Dumazet 	return false;
101256fc709bSEric Dumazet }
101356fc709bSEric Dumazet 
1014f3d9832eSDavid Ahern static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
1015f3d9832eSDavid Ahern {
10163f27fb23SEric Dumazet 	unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr);
1017f3d9832eSDavid Ahern 	int err = 0;
1018f3d9832eSDavid Ahern 
1019f3d9832eSDavid Ahern 	spin_lock(&addrconf_hash_lock);
1020f3d9832eSDavid Ahern 
1021f3d9832eSDavid Ahern 	/* Ignore adding duplicate addresses on an interface */
1022752a9292SEric Dumazet 	if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) {
1023e32ac250SJoe Perches 		netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
1024f3d9832eSDavid Ahern 		err = -EEXIST;
1025752a9292SEric Dumazet 	} else {
1026752a9292SEric Dumazet 		hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
1027f3d9832eSDavid Ahern 	}
1028f3d9832eSDavid Ahern 
1029f3d9832eSDavid Ahern 	spin_unlock(&addrconf_hash_lock);
1030f3d9832eSDavid Ahern 
1031f3d9832eSDavid Ahern 	return err;
1032f3d9832eSDavid Ahern }
1033f3d9832eSDavid Ahern 
10341da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds static struct inet6_ifaddr *
1037e6464b8cSDavid Ahern ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
1038de95e047SDavid Ahern 	      bool can_block, struct netlink_ext_ack *extack)
10391da177e4SLinus Torvalds {
1040f3d9832eSDavid Ahern 	gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
1041e6464b8cSDavid Ahern 	int addr_type = ipv6_addr_type(cfg->pfx);
1042df789fe7SDavid Forster 	struct net *net = dev_net(idev->dev);
10431da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
1044360a9887SDavid Ahern 	struct fib6_info *f6i = NULL;
10451da177e4SLinus Torvalds 	int err = 0;
1046d68b8270SYOSHIFUJI Hideaki 
1047d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
1048f17f7648SHangbin Liu 	    (addr_type & IPV6_ADDR_MULTICAST &&
1049f17f7648SHangbin Liu 	     !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) ||
1050d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
10513ede0bbcSRobert Shearman 	     !netif_is_l3_master(idev->dev) &&
1052d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
1053d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	if (idev->dead) {
10561da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
1057f3d9832eSDavid Ahern 		goto out;
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 
106056d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
10619bdd8d40SBrian Haley 		err = -EACCES;
1062f3d9832eSDavid Ahern 		goto out;
10639bdd8d40SBrian Haley 	}
10649bdd8d40SBrian Haley 
1065ff7883eaSDavid Ahern 	/* validator notifier needs to be blocking;
1066ff7883eaSDavid Ahern 	 * do not call in atomic context
1067ff7883eaSDavid Ahern 	 */
1068ff7883eaSDavid Ahern 	if (can_block) {
1069ff7883eaSDavid Ahern 		struct in6_validator_info i6vi = {
1070e6464b8cSDavid Ahern 			.i6vi_addr = *cfg->pfx,
1071ff7883eaSDavid Ahern 			.i6vi_dev = idev,
1072de95e047SDavid Ahern 			.extack = extack,
1073ff7883eaSDavid Ahern 		};
1074ff7883eaSDavid Ahern 
10753ad7d246SKrister Johansen 		err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
10763ad7d246SKrister Johansen 		err = notifier_to_errno(err);
1077f3d9832eSDavid Ahern 		if (err < 0)
10781da177e4SLinus Torvalds 			goto out;
1079ff7883eaSDavid Ahern 	}
10801da177e4SLinus Torvalds 
1081f3d9832eSDavid Ahern 	ifa = kzalloc(sizeof(*ifa), gfp_flags);
108263159f29SIan Morris 	if (!ifa) {
10831da177e4SLinus Torvalds 		err = -ENOBUFS;
10841da177e4SLinus Torvalds 		goto out;
10851da177e4SLinus Torvalds 	}
10861da177e4SLinus Torvalds 
1087e6464b8cSDavid Ahern 	f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags);
1088360a9887SDavid Ahern 	if (IS_ERR(f6i)) {
1089360a9887SDavid Ahern 		err = PTR_ERR(f6i);
1090360a9887SDavid Ahern 		f6i = NULL;
10911da177e4SLinus Torvalds 		goto out;
10921da177e4SLinus Torvalds 	}
10931da177e4SLinus Torvalds 
1094df789fe7SDavid Forster 	if (net->ipv6.devconf_all->disable_policy ||
1095df789fe7SDavid Forster 	    idev->cnf.disable_policy)
1096360a9887SDavid Ahern 		f6i->dst_nopolicy = true;
1097df789fe7SDavid Forster 
1098bba24896SJiri Pirko 	neigh_parms_data_state_setall(idev->nd_parms);
1099bba24896SJiri Pirko 
1100e6464b8cSDavid Ahern 	ifa->addr = *cfg->pfx;
1101e6464b8cSDavid Ahern 	if (cfg->peer_pfx)
1102e6464b8cSDavid Ahern 		ifa->peer_addr = *cfg->peer_pfx;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
1105c15b1ccaSHannes Frederic Sowa 	INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1106c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
1107e6464b8cSDavid Ahern 	ifa->scope = cfg->scope;
1108e6464b8cSDavid Ahern 	ifa->prefix_len = cfg->plen;
11098308f3ffSDavid Ahern 	ifa->rt_priority = cfg->rt_priority;
1110e6464b8cSDavid Ahern 	ifa->flags = cfg->ifa_flags;
111166eb9f86SMahesh Bandewar 	/* No need to add the TENTATIVE flag for addresses with NODAD */
1112e6464b8cSDavid Ahern 	if (!(cfg->ifa_flags & IFA_F_NODAD))
111366eb9f86SMahesh Bandewar 		ifa->flags |= IFA_F_TENTATIVE;
1114e6464b8cSDavid Ahern 	ifa->valid_lft = cfg->valid_lft;
1115e6464b8cSDavid Ahern 	ifa->prefered_lft = cfg->preferred_lft;
11161da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
1117617fe29dSDaniel Borkmann 	ifa->tokenized = false;
11181da177e4SLinus Torvalds 
1119360a9887SDavid Ahern 	ifa->rt = f6i;
112057f5f544SKeir Fraser 
11211da177e4SLinus Torvalds 	ifa->idev = idev;
1122f3d9832eSDavid Ahern 	in6_dev_hold(idev);
1123f3d9832eSDavid Ahern 
11241da177e4SLinus Torvalds 	/* For caller */
1125271201c0SReshetova, Elena 	refcount_set(&ifa->refcnt, 1);
11261da177e4SLinus Torvalds 
1127f3d9832eSDavid Ahern 	rcu_read_lock_bh();
11281da177e4SLinus Torvalds 
1129f3d9832eSDavid Ahern 	err = ipv6_add_addr_hash(idev->dev, ifa);
1130f3d9832eSDavid Ahern 	if (err < 0) {
1131f3d9832eSDavid Ahern 		rcu_read_unlock_bh();
1132f3d9832eSDavid Ahern 		goto out;
1133f3d9832eSDavid Ahern 	}
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	write_lock(&idev->lock);
1136f3d9832eSDavid Ahern 
11371da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
1138e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
1141372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
11421da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
11431da177e4SLinus Torvalds 	}
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
11461da177e4SLinus Torvalds 	write_unlock(&idev->lock);
1147f3d9832eSDavid Ahern 
11488814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
11491da177e4SLinus Torvalds 
1150f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1151f3d9832eSDavid Ahern out:
1152f3d9832eSDavid Ahern 	if (unlikely(err < 0)) {
1153360a9887SDavid Ahern 		fib6_info_release(f6i);
115493531c67SDavid Ahern 
1155f3d9832eSDavid Ahern 		if (ifa) {
1156f3d9832eSDavid Ahern 			if (ifa->idev)
1157f3d9832eSDavid Ahern 				in6_dev_put(ifa->idev);
11581da177e4SLinus Torvalds 			kfree(ifa);
1159f3d9832eSDavid Ahern 		}
11601da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
11611da177e4SLinus Torvalds 	}
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	return ifa;
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
11665b84efecSThomas Haller enum cleanup_prefix_rt_t {
11675b84efecSThomas Haller 	CLEANUP_PREFIX_RT_NOP,    /* no cleanup action for prefix route */
11685b84efecSThomas Haller 	CLEANUP_PREFIX_RT_DEL,    /* delete the prefix route */
11695b84efecSThomas Haller 	CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
11705b84efecSThomas Haller };
11715b84efecSThomas Haller 
11725b84efecSThomas Haller /*
11735b84efecSThomas Haller  * Check, whether the prefix for ifp would still need a prefix route
11745b84efecSThomas Haller  * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
11755b84efecSThomas Haller  * constants.
11765b84efecSThomas Haller  *
11775b84efecSThomas Haller  * 1) we don't purge prefix if address was not permanent.
11785b84efecSThomas Haller  *    prefix is managed by its own lifetime.
11795b84efecSThomas Haller  * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
11805b84efecSThomas Haller  * 3) if there are no addresses, delete prefix.
11815b84efecSThomas Haller  * 4) if there are still other permanent address(es),
11825b84efecSThomas Haller  *    corresponding prefix is still permanent.
11835b84efecSThomas Haller  * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
11845b84efecSThomas Haller  *    don't purge the prefix, assume user space is managing it.
11855b84efecSThomas Haller  * 6) otherwise, update prefix lifetime to the
11865b84efecSThomas Haller  *    longest valid lifetime among the corresponding
11875b84efecSThomas Haller  *    addresses on the device.
11885b84efecSThomas Haller  *    Note: subsequent RA will update lifetime.
11895b84efecSThomas Haller  **/
11905b84efecSThomas Haller static enum cleanup_prefix_rt_t
11915b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
11925b84efecSThomas Haller {
11935b84efecSThomas Haller 	struct inet6_ifaddr *ifa;
11945b84efecSThomas Haller 	struct inet6_dev *idev = ifp->idev;
11955b84efecSThomas Haller 	unsigned long lifetime;
11965b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
11975b84efecSThomas Haller 
11985b84efecSThomas Haller 	*expires = jiffies;
11995b84efecSThomas Haller 
12005b84efecSThomas Haller 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
12015b84efecSThomas Haller 		if (ifa == ifp)
12025b84efecSThomas Haller 			continue;
1203e75913c9SZhiqiang Liu 		if (ifa->prefix_len != ifp->prefix_len ||
1204e75913c9SZhiqiang Liu 		    !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
12055b84efecSThomas Haller 				       ifp->prefix_len))
12065b84efecSThomas Haller 			continue;
12075b84efecSThomas Haller 		if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
12085b84efecSThomas Haller 			return CLEANUP_PREFIX_RT_NOP;
12095b84efecSThomas Haller 
12105b84efecSThomas Haller 		action = CLEANUP_PREFIX_RT_EXPIRE;
12115b84efecSThomas Haller 
12125b84efecSThomas Haller 		spin_lock(&ifa->lock);
12135b84efecSThomas Haller 
12145b84efecSThomas Haller 		lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
12155b84efecSThomas Haller 		/*
12165b84efecSThomas Haller 		 * Note: Because this address is
12175b84efecSThomas Haller 		 * not permanent, lifetime <
12185b84efecSThomas Haller 		 * LONG_MAX / HZ here.
12195b84efecSThomas Haller 		 */
12205b84efecSThomas Haller 		if (time_before(*expires, ifa->tstamp + lifetime * HZ))
12215b84efecSThomas Haller 			*expires = ifa->tstamp + lifetime * HZ;
12225b84efecSThomas Haller 		spin_unlock(&ifa->lock);
12235b84efecSThomas Haller 	}
12245b84efecSThomas Haller 
12255b84efecSThomas Haller 	return action;
12265b84efecSThomas Haller }
12275b84efecSThomas Haller 
12285b84efecSThomas Haller static void
1229d0098e4cSHangbin Liu cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires,
1230d0098e4cSHangbin Liu 		     bool del_rt, bool del_peer)
12315b84efecSThomas Haller {
123293c2fb25SDavid Ahern 	struct fib6_info *f6i;
12335b84efecSThomas Haller 
1234d0098e4cSHangbin Liu 	f6i = addrconf_get_prefix_route(del_peer ? &ifp->peer_addr : &ifp->addr,
1235d0098e4cSHangbin Liu 					ifp->prefix_len,
12362b2450caSDavid Ahern 					ifp->idev->dev, 0, RTF_DEFAULT, true);
123793c2fb25SDavid Ahern 	if (f6i) {
12385b84efecSThomas Haller 		if (del_rt)
123911dd74b3SRoopa Prabhu 			ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
12405b84efecSThomas Haller 		else {
124193c2fb25SDavid Ahern 			if (!(f6i->fib6_flags & RTF_EXPIRES))
124293c2fb25SDavid Ahern 				fib6_set_expires(f6i, expires);
124393c2fb25SDavid Ahern 			fib6_info_release(f6i);
12445b84efecSThomas Haller 		}
12455b84efecSThomas Haller 	}
12465b84efecSThomas Haller }
12475b84efecSThomas Haller 
12485b84efecSThomas Haller 
12491da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
12521da177e4SLinus Torvalds {
12534c5ff6a6SHerbert Xu 	int state;
12545b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
12555b84efecSThomas Haller 	unsigned long expires;
12561da177e4SLinus Torvalds 
1257c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
1258c15b1ccaSHannes Frederic Sowa 
12598e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
12604c5ff6a6SHerbert Xu 	state = ifp->state;
1261e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
12628e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
12634c5ff6a6SHerbert Xu 
12644c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
12654c5ff6a6SHerbert Xu 		goto out;
12661da177e4SLinus Torvalds 
12675c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
12685c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
12695c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
12701da177e4SLinus Torvalds 
12715b84efecSThomas Haller 	write_lock_bh(&ifp->idev->lock);
12725d9efa7eSDavid S. Miller 
12731da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
1274372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
12751da177e4SLinus Torvalds 		if (ifp->ifpub) {
12761da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
12771da177e4SLinus Torvalds 			ifp->ifpub = NULL;
12781da177e4SLinus Torvalds 		}
12791da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
12801da177e4SLinus Torvalds 	}
12811da177e4SLinus Torvalds 
12825b84efecSThomas Haller 	if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
12835b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &expires);
12845b84efecSThomas Haller 
12858ef802aaSEric Dumazet 	list_del_rcu(&ifp->if_list);
12861da177e4SLinus Torvalds 	__in6_ifa_put(ifp);
1287502a2ffdSstephen hemminger 
12885b84efecSThomas Haller 	write_unlock_bh(&ifp->idev->lock);
12891da177e4SLinus Torvalds 
1290c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
1291b2238566SAndrey Vagin 
12921da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
12931da177e4SLinus Torvalds 
1294f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
12951da177e4SLinus Torvalds 
12965b84efecSThomas Haller 	if (action != CLEANUP_PREFIX_RT_NOP) {
12975b84efecSThomas Haller 		cleanup_prefix_route(ifp, expires,
1298d0098e4cSHangbin Liu 			action == CLEANUP_PREFIX_RT_DEL, false);
12991da177e4SLinus Torvalds 	}
13001da177e4SLinus Torvalds 
1301c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1302c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
13034c5ff6a6SHerbert Xu out:
13041da177e4SLinus Torvalds 	in6_ifa_put(ifp);
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
1307969c5464SFernando Gont static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, bool block)
13081da177e4SLinus Torvalds {
13091da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
1310e6464b8cSDavid Ahern 	unsigned long tmp_tstamp, age;
1311eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
131276f793e3SLorenzo Colitti 	unsigned long now = jiffies;
13137aa8e63fSJiri Bohac 	s32 cnf_temp_preferred_lft;
1314969c5464SFernando Gont 	struct inet6_ifaddr *ift;
1315969c5464SFernando Gont 	struct ifa6_config cfg;
1316969c5464SFernando Gont 	long max_desync_factor;
1317969c5464SFernando Gont 	struct in6_addr addr;
1318969c5464SFernando Gont 	int ret = 0;
13191da177e4SLinus Torvalds 
132053bd6749SJiri Pirko 	write_lock_bh(&idev->lock);
1321969c5464SFernando Gont 
13221da177e4SLinus Torvalds retry:
13231da177e4SLinus Torvalds 	in6_dev_hold(idev);
13241da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
132553bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1326f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
13271da177e4SLinus Torvalds 		in6_dev_put(idev);
13281da177e4SLinus Torvalds 		ret = -1;
13291da177e4SLinus Torvalds 		goto out;
13301da177e4SLinus Torvalds 	}
13311da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
13321da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
13331da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
13341da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
133553bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1336f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1337f3213831SJoe Perches 			__func__);
13381da177e4SLinus Torvalds 		in6_dev_put(idev);
13391da177e4SLinus Torvalds 		ret = -1;
13401da177e4SLinus Torvalds 		goto out;
13411da177e4SLinus Torvalds 	}
13421da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
13431da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1344969c5464SFernando Gont 	ipv6_gen_rnd_iid(&addr);
1345969c5464SFernando Gont 
134676f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
13471da177e4SLinus Torvalds 
1348eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1349eac55bf9SBenoit Boissinot 			idev->cnf.dad_transmits *
135019e16d22SHangbin Liu 			max(NEIGH_VAR(idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
135176506a98SJiri Bohac 
135276506a98SJiri Bohac 	/* recalculate max_desync_factor each time and update
135376506a98SJiri Bohac 	 * idev->desync_factor if it's larger
135476506a98SJiri Bohac 	 */
13557aa8e63fSJiri Bohac 	cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
135676506a98SJiri Bohac 	max_desync_factor = min_t(__u32,
135776506a98SJiri Bohac 				  idev->cnf.max_desync_factor,
13587aa8e63fSJiri Bohac 				  cnf_temp_preferred_lft - regen_advance);
135976506a98SJiri Bohac 
136076506a98SJiri Bohac 	if (unlikely(idev->desync_factor > max_desync_factor)) {
136176506a98SJiri Bohac 		if (max_desync_factor > 0) {
136276506a98SJiri Bohac 			get_random_bytes(&idev->desync_factor,
136376506a98SJiri Bohac 					 sizeof(idev->desync_factor));
136476506a98SJiri Bohac 			idev->desync_factor %= max_desync_factor;
136576506a98SJiri Bohac 		} else {
136676506a98SJiri Bohac 			idev->desync_factor = 0;
136776506a98SJiri Bohac 		}
136876506a98SJiri Bohac 	}
136976506a98SJiri Bohac 
13703f2d67b6SDavid Ahern 	memset(&cfg, 0, sizeof(cfg));
1371e6464b8cSDavid Ahern 	cfg.valid_lft = min_t(__u32, ifp->valid_lft,
137276506a98SJiri Bohac 			      idev->cnf.temp_valid_lft + age);
1373e6464b8cSDavid Ahern 	cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1374e6464b8cSDavid Ahern 	cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
1375e6464b8cSDavid Ahern 
1376e6464b8cSDavid Ahern 	cfg.plen = ifp->prefix_len;
137776506a98SJiri Bohac 	tmp_tstamp = ifp->tstamp;
137876506a98SJiri Bohac 	spin_unlock_bh(&ifp->lock);
137976506a98SJiri Bohac 
138053bd6749SJiri Pirko 	write_unlock_bh(&idev->lock);
138195c385b4SNeil Horman 
1382eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1383eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1384eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1385eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1386ecab6701SHeiner Kallweit 	 * Use age calculation as in addrconf_verify to avoid unnecessary
1387ecab6701SHeiner Kallweit 	 * temporary addresses being generated.
1388eac55bf9SBenoit Boissinot 	 */
1389ecab6701SHeiner Kallweit 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1390e6464b8cSDavid Ahern 	if (cfg.preferred_lft <= regen_advance + age) {
1391eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1392eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1393eac55bf9SBenoit Boissinot 		ret = -1;
1394eac55bf9SBenoit Boissinot 		goto out;
1395eac55bf9SBenoit Boissinot 	}
1396eac55bf9SBenoit Boissinot 
1397e6464b8cSDavid Ahern 	cfg.ifa_flags = IFA_F_TEMPORARY;
139895c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
139995c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
1400e6464b8cSDavid Ahern 		cfg.ifa_flags |= IFA_F_OPTIMISTIC;
140195c385b4SNeil Horman 
1402e6464b8cSDavid Ahern 	cfg.pfx = &addr;
1403e6464b8cSDavid Ahern 	cfg.scope = ipv6_addr_scope(cfg.pfx);
1404e6464b8cSDavid Ahern 
1405e6464b8cSDavid Ahern 	ift = ipv6_add_addr(idev, &cfg, block, NULL);
14064b08a8f1SHannes Frederic Sowa 	if (IS_ERR(ift)) {
14071da177e4SLinus Torvalds 		in6_ifa_put(ifp);
14081da177e4SLinus Torvalds 		in6_dev_put(idev);
1409f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
141053bd6749SJiri Pirko 		write_lock_bh(&idev->lock);
14111da177e4SLinus Torvalds 		goto retry;
14121da177e4SLinus Torvalds 	}
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
14151da177e4SLinus Torvalds 	ift->ifpub = ifp;
141676f793e3SLorenzo Colitti 	ift->cstamp = now;
14171da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
14181da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
14191da177e4SLinus Torvalds 
1420cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
14211da177e4SLinus Torvalds 	in6_ifa_put(ift);
14221da177e4SLinus Torvalds 	in6_dev_put(idev);
14231da177e4SLinus Torvalds out:
14241da177e4SLinus Torvalds 	return ret;
14251da177e4SLinus Torvalds }
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds /*
1428072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
14291da177e4SLinus Torvalds  */
1430a9b05723SYOSHIFUJI Hideaki enum {
1431a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1432a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1433a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1434a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1435a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1436a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1437a9b05723SYOSHIFUJI Hideaki #endif
1438a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1439a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1440a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1441a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1442a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
14437fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
14447fd2561eSErik Kline 	IPV6_SADDR_RULE_NOT_OPTIMISTIC,
14457fd2561eSErik Kline #endif
1446a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1447072047e4SYOSHIFUJI Hideaki };
1448072047e4SYOSHIFUJI Hideaki 
1449a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1450a9b05723SYOSHIFUJI Hideaki 	int			rule;
1451a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1452a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1453a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1454a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1455a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1456a9b05723SYOSHIFUJI Hideaki };
1457a9b05723SYOSHIFUJI Hideaki 
1458a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
14599acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1460a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1461a9b05723SYOSHIFUJI Hideaki 	int scope;
1462a9b05723SYOSHIFUJI Hideaki 	int label;
14637cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1464a9b05723SYOSHIFUJI Hideaki };
1465072047e4SYOSHIFUJI Hideaki 
1466b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
14671da177e4SLinus Torvalds {
146845bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1469072047e4SYOSHIFUJI Hideaki 		return 1;
1470072047e4SYOSHIFUJI Hideaki 	return 0;
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
147335e015e1SMatteo Croce static bool ipv6_use_optimistic_addr(struct net *net,
147435e015e1SMatteo Croce 				     struct inet6_dev *idev)
14757fd2561eSErik Kline {
14767fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
147735e015e1SMatteo Croce 	if (!idev)
147835e015e1SMatteo Croce 		return false;
147935e015e1SMatteo Croce 	if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
148035e015e1SMatteo Croce 		return false;
148135e015e1SMatteo Croce 	if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
148235e015e1SMatteo Croce 		return false;
148335e015e1SMatteo Croce 
148435e015e1SMatteo Croce 	return true;
14857fd2561eSErik Kline #else
14867fd2561eSErik Kline 	return false;
14877fd2561eSErik Kline #endif
14887fd2561eSErik Kline }
14897fd2561eSErik Kline 
1490f1c02cfbSSabrina Dubroca static bool ipv6_allow_optimistic_dad(struct net *net,
1491f1c02cfbSSabrina Dubroca 				      struct inet6_dev *idev)
1492f1c02cfbSSabrina Dubroca {
1493f1c02cfbSSabrina Dubroca #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1494f1c02cfbSSabrina Dubroca 	if (!idev)
1495f1c02cfbSSabrina Dubroca 		return false;
1496f1c02cfbSSabrina Dubroca 	if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1497f1c02cfbSSabrina Dubroca 		return false;
1498f1c02cfbSSabrina Dubroca 
1499f1c02cfbSSabrina Dubroca 	return true;
1500f1c02cfbSSabrina Dubroca #else
1501f1c02cfbSSabrina Dubroca 	return false;
1502f1c02cfbSSabrina Dubroca #endif
1503f1c02cfbSSabrina Dubroca }
1504f1c02cfbSSabrina Dubroca 
15053de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
15063de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1507a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1508a9b05723SYOSHIFUJI Hideaki 			       int i)
1509a9b05723SYOSHIFUJI Hideaki {
1510a9b05723SYOSHIFUJI Hideaki 	int ret;
1511a9b05723SYOSHIFUJI Hideaki 
1512a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1513a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1514a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1515a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1516a9b05723SYOSHIFUJI Hideaki 			break;
1517a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1518a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1519a9b05723SYOSHIFUJI Hideaki 			break;
1520a9b05723SYOSHIFUJI Hideaki 		default:
1521a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1522a9b05723SYOSHIFUJI Hideaki 		}
1523a9b05723SYOSHIFUJI Hideaki 		goto out;
1524a9b05723SYOSHIFUJI Hideaki 	}
1525a9b05723SYOSHIFUJI Hideaki 
1526a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1527a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1528a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1529a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1530a9b05723SYOSHIFUJI Hideaki 		break;
1531a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1532a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1533a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1534a9b05723SYOSHIFUJI Hideaki 		break;
1535a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1536a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1537a9b05723SYOSHIFUJI Hideaki 		 *
1538a9b05723SYOSHIFUJI Hideaki 		 *      ret
1539a9b05723SYOSHIFUJI Hideaki 		 *       ^
1540a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1541a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1542a9b05723SYOSHIFUJI Hideaki 		 *       |
1543a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1544a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1545a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1546db9c7c39Sstephen hemminger 		 *  B-15 |    \        if scope is enough for destination.
1547a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1548a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1549a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1550a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1551a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1552a9b05723SYOSHIFUJI Hideaki 		 *
1553a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1554a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1555a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1556a9b05723SYOSHIFUJI Hideaki 		 */
1557a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1558a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1559a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1560a9b05723SYOSHIFUJI Hideaki 		else
1561a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1562a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1563a9b05723SYOSHIFUJI Hideaki 		break;
1564a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
15657fd2561eSErik Kline 	    {
1566a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
15677fd2561eSErik Kline 		u8 avoid = IFA_F_DEPRECATED;
15687fd2561eSErik Kline 
156935e015e1SMatteo Croce 		if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
15707fd2561eSErik Kline 			avoid |= IFA_F_OPTIMISTIC;
1571a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
15727fd2561eSErik Kline 		      !(score->ifa->flags & avoid);
1573a9b05723SYOSHIFUJI Hideaki 		break;
15747fd2561eSErik Kline 	    }
1575a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1576a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
15777cbca67cSYOSHIFUJI Hideaki 	    {
1578a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
15797cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
15807cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1581a9b05723SYOSHIFUJI Hideaki 		break;
15827cbca67cSYOSHIFUJI Hideaki 	    }
1583a9b05723SYOSHIFUJI Hideaki #endif
1584a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1585a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1586a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1587a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1588a9b05723SYOSHIFUJI Hideaki 		break;
1589a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1590a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
15913de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
15923de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1593a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1594a9b05723SYOSHIFUJI Hideaki 		break;
1595a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
15967cbca67cSYOSHIFUJI Hideaki 	    {
1597a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
159825985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1599a9b05723SYOSHIFUJI Hideaki 		 */
16007cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
16017cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
16027cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
16037cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1604a9b05723SYOSHIFUJI Hideaki 		break;
16057cbca67cSYOSHIFUJI Hideaki 	    }
1606a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1607a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1608a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1609a9b05723SYOSHIFUJI Hideaki 		 */
1610a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1611a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1612a9b05723SYOSHIFUJI Hideaki 		break;
1613a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1614a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
161591b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
161691b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
161791b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
161891b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1619a9b05723SYOSHIFUJI Hideaki 		break;
16207fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
16217fd2561eSErik Kline 	case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
16227fd2561eSErik Kline 		/* Optimistic addresses still have lower precedence than other
16237fd2561eSErik Kline 		 * preferred addresses.
16247fd2561eSErik Kline 		 */
16257fd2561eSErik Kline 		ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
16267fd2561eSErik Kline 		break;
16277fd2561eSErik Kline #endif
1628a9b05723SYOSHIFUJI Hideaki 	default:
1629a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1630a9b05723SYOSHIFUJI Hideaki 	}
1631a9b05723SYOSHIFUJI Hideaki 
1632a9b05723SYOSHIFUJI Hideaki 	if (ret)
1633a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1634a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1635a9b05723SYOSHIFUJI Hideaki out:
1636a9b05723SYOSHIFUJI Hideaki 	return ret;
1637a9b05723SYOSHIFUJI Hideaki }
1638a9b05723SYOSHIFUJI Hideaki 
1639c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net,
16409131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_dst *dst,
16419131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct inet6_dev *idev,
1642c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_score *scores,
1643c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				int hiscore_idx)
16441da177e4SLinus Torvalds {
1645c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1646072047e4SYOSHIFUJI Hideaki 
1647f59c031eSEric Dumazet 	list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
1648a9b05723SYOSHIFUJI Hideaki 		int i;
1649072047e4SYOSHIFUJI Hideaki 
1650a9b05723SYOSHIFUJI Hideaki 		/*
16516b3ae80aSYOSHIFUJI Hideaki 		 * - Tentative Address (RFC2462 section 5.4)
16526b3ae80aSYOSHIFUJI Hideaki 		 *  - A tentative address is not considered
16536b3ae80aSYOSHIFUJI Hideaki 		 *    "assigned to an interface" in the traditional
165495c385b4SNeil Horman 		 *    sense, unless it is also flagged as optimistic.
16556b3ae80aSYOSHIFUJI Hideaki 		 * - Candidate Source Address (section 4)
1656072047e4SYOSHIFUJI Hideaki 		 *  - In any case, anycast addresses, multicast
1657072047e4SYOSHIFUJI Hideaki 		 *    addresses, and the unspecified address MUST
1658072047e4SYOSHIFUJI Hideaki 		 *    NOT be included in a candidate set.
1659072047e4SYOSHIFUJI Hideaki 		 */
1660a9b05723SYOSHIFUJI Hideaki 		if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1661a9b05723SYOSHIFUJI Hideaki 		    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
16626b3ae80aSYOSHIFUJI Hideaki 			continue;
1663a9b05723SYOSHIFUJI Hideaki 
1664a9b05723SYOSHIFUJI Hideaki 		score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1665a9b05723SYOSHIFUJI Hideaki 
1666a9b05723SYOSHIFUJI Hideaki 		if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1667a9b05723SYOSHIFUJI Hideaki 			     score->addr_type & IPV6_ADDR_MULTICAST)) {
1668ba7a46f1SJoe Perches 			net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
16699131f3deSYOSHIFUJI Hideaki/吉藤英明 					    idev->dev->name);
16701da177e4SLinus Torvalds 			continue;
1671072047e4SYOSHIFUJI Hideaki 		}
1672072047e4SYOSHIFUJI Hideaki 
1673a9b05723SYOSHIFUJI Hideaki 		score->rule = -1;
1674a9b05723SYOSHIFUJI Hideaki 		bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1675072047e4SYOSHIFUJI Hideaki 
1676a9b05723SYOSHIFUJI Hideaki 		for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1677a9b05723SYOSHIFUJI Hideaki 			int minihiscore, miniscore;
1678072047e4SYOSHIFUJI Hideaki 
16799131f3deSYOSHIFUJI Hideaki/吉藤英明 			minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
16809131f3deSYOSHIFUJI Hideaki/吉藤英明 			miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1681072047e4SYOSHIFUJI Hideaki 
1682a9b05723SYOSHIFUJI Hideaki 			if (minihiscore > miniscore) {
1683a9b05723SYOSHIFUJI Hideaki 				if (i == IPV6_SADDR_RULE_SCOPE &&
1684a9b05723SYOSHIFUJI Hideaki 				    score->scopedist > 0) {
1685a9b05723SYOSHIFUJI Hideaki 					/*
1686a9b05723SYOSHIFUJI Hideaki 					 * special case:
1687a9b05723SYOSHIFUJI Hideaki 					 * each remaining entry
1688a9b05723SYOSHIFUJI Hideaki 					 * has too small (not enough)
1689a9b05723SYOSHIFUJI Hideaki 					 * scope, because ifa entries
1690a9b05723SYOSHIFUJI Hideaki 					 * are sorted by their scope
1691a9b05723SYOSHIFUJI Hideaki 					 * values.
1692072047e4SYOSHIFUJI Hideaki 					 */
16939131f3deSYOSHIFUJI Hideaki/吉藤英明 					goto out;
1694072047e4SYOSHIFUJI Hideaki 				}
1695a9b05723SYOSHIFUJI Hideaki 				break;
1696a9b05723SYOSHIFUJI Hideaki 			} else if (minihiscore < miniscore) {
1697a0bffffcSIlpo Järvinen 				swap(hiscore, score);
1698c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				hiscore_idx = 1 - hiscore_idx;
1699a9b05723SYOSHIFUJI Hideaki 
1700a9b05723SYOSHIFUJI Hideaki 				/* restore our iterator */
1701a9b05723SYOSHIFUJI Hideaki 				score->ifa = hiscore->ifa;
1702a9b05723SYOSHIFUJI Hideaki 
1703a9b05723SYOSHIFUJI Hideaki 				break;
17041da177e4SLinus Torvalds 			}
1705a9b05723SYOSHIFUJI Hideaki 		}
1706a9b05723SYOSHIFUJI Hideaki 	}
17079131f3deSYOSHIFUJI Hideaki/吉藤英明 out:
1708c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	return hiscore_idx;
17091da177e4SLinus Torvalds }
17109131f3deSYOSHIFUJI Hideaki/吉藤英明 
1711afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net,
1712afbac601SDavid Ahern 				 const struct net_device *dst_dev,
1713afbac601SDavid Ahern 				 const struct net_device *master,
1714afbac601SDavid Ahern 				 struct ipv6_saddr_dst *dst,
1715afbac601SDavid Ahern 				 struct ipv6_saddr_score *scores,
1716afbac601SDavid Ahern 				 int hiscore_idx)
1717afbac601SDavid Ahern {
1718afbac601SDavid Ahern 	struct inet6_dev *idev;
1719afbac601SDavid Ahern 
1720afbac601SDavid Ahern 	idev = __in6_dev_get(dst_dev);
1721afbac601SDavid Ahern 	if (idev)
1722afbac601SDavid Ahern 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1723afbac601SDavid Ahern 						   scores, hiscore_idx);
1724afbac601SDavid Ahern 
1725afbac601SDavid Ahern 	idev = __in6_dev_get(master);
1726afbac601SDavid Ahern 	if (idev)
1727afbac601SDavid Ahern 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1728afbac601SDavid Ahern 						   scores, hiscore_idx);
1729afbac601SDavid Ahern 
1730afbac601SDavid Ahern 	return hiscore_idx;
1731afbac601SDavid Ahern }
1732afbac601SDavid Ahern 
17339131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
17349131f3deSYOSHIFUJI Hideaki/吉藤英明 		       const struct in6_addr *daddr, unsigned int prefs,
17359131f3deSYOSHIFUJI Hideaki/吉藤英明 		       struct in6_addr *saddr)
17369131f3deSYOSHIFUJI Hideaki/吉藤英明 {
1737c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score scores[2], *hiscore;
17389131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_dst dst;
17399131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct inet6_dev *idev;
17409131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct net_device *dev;
17419131f3deSYOSHIFUJI Hideaki/吉藤英明 	int dst_type;
17429131f3deSYOSHIFUJI Hideaki/吉藤英明 	bool use_oif_addr = false;
1743c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	int hiscore_idx = 0;
1744cc429c8fSEric Dumazet 	int ret = 0;
17459131f3deSYOSHIFUJI Hideaki/吉藤英明 
17469131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst_type = __ipv6_addr_type(daddr);
17479131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.addr = daddr;
17489131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
17499131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.scope = __ipv6_addr_src_scope(dst_type);
17509131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
17519131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.prefs = prefs;
17529131f3deSYOSHIFUJI Hideaki/吉藤英明 
1753c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].rule = -1;
1754c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].ifa = NULL;
17559131f3deSYOSHIFUJI Hideaki/吉藤英明 
17569131f3deSYOSHIFUJI Hideaki/吉藤英明 	rcu_read_lock();
17579131f3deSYOSHIFUJI Hideaki/吉藤英明 
17589131f3deSYOSHIFUJI Hideaki/吉藤英明 	/* Candidate Source Address (section 4)
17599131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *  - multicast and link-local destination address,
17609131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    the set of candidate source address MUST only
17619131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
17629131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same link as the outgoing
17639131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.
17649131f3deSYOSHIFUJI Hideaki/吉藤英明 	 * (- For site-local destination addresses, the
17659131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    set of candidate source addresses MUST only
17669131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
17679131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same site as the outgoing
17689131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.)
17693985e8a3SErik Kline 	 *  - "It is RECOMMENDED that the candidate source addresses
17703985e8a3SErik Kline 	 *    be the set of unicast addresses assigned to the
17713985e8a3SErik Kline 	 *    interface that will be used to send to the destination
17723985e8a3SErik Kline 	 *    (the 'outgoing' interface)." (RFC 6724)
17739131f3deSYOSHIFUJI Hideaki/吉藤英明 	 */
17749131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (dst_dev) {
17759131f3deSYOSHIFUJI Hideaki/吉藤英明 		idev = __in6_dev_get(dst_dev);
17763985e8a3SErik Kline 		if ((dst_type & IPV6_ADDR_MULTICAST) ||
17773985e8a3SErik Kline 		    dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
17783985e8a3SErik Kline 		    (idev && idev->cnf.use_oif_addrs_only)) {
17799131f3deSYOSHIFUJI Hideaki/吉藤英明 			use_oif_addr = true;
17809131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
17819131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
17829131f3deSYOSHIFUJI Hideaki/吉藤英明 
17839131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (use_oif_addr) {
1784c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 		if (idev)
1785c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
17869131f3deSYOSHIFUJI Hideaki/吉藤英明 	} else {
1787afbac601SDavid Ahern 		const struct net_device *master;
1788afbac601SDavid Ahern 		int master_idx = 0;
1789afbac601SDavid Ahern 
1790afbac601SDavid Ahern 		/* if dst_dev exists and is enslaved to an L3 device, then
1791afbac601SDavid Ahern 		 * prefer addresses from dst_dev and then the master over
1792afbac601SDavid Ahern 		 * any other enslaved devices in the L3 domain.
1793afbac601SDavid Ahern 		 */
1794afbac601SDavid Ahern 		master = l3mdev_master_dev_rcu(dst_dev);
1795afbac601SDavid Ahern 		if (master) {
1796afbac601SDavid Ahern 			master_idx = master->ifindex;
1797afbac601SDavid Ahern 
1798afbac601SDavid Ahern 			hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1799afbac601SDavid Ahern 							    master, &dst,
1800afbac601SDavid Ahern 							    scores, hiscore_idx);
1801afbac601SDavid Ahern 
1802afbac601SDavid Ahern 			if (scores[hiscore_idx].ifa)
1803afbac601SDavid Ahern 				goto out;
1804afbac601SDavid Ahern 		}
1805afbac601SDavid Ahern 
18069131f3deSYOSHIFUJI Hideaki/吉藤英明 		for_each_netdev_rcu(net, dev) {
1807afbac601SDavid Ahern 			/* only consider addresses on devices in the
1808afbac601SDavid Ahern 			 * same L3 domain
1809afbac601SDavid Ahern 			 */
1810afbac601SDavid Ahern 			if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1811afbac601SDavid Ahern 				continue;
18129131f3deSYOSHIFUJI Hideaki/吉藤英明 			idev = __in6_dev_get(dev);
18139131f3deSYOSHIFUJI Hideaki/吉藤英明 			if (!idev)
18149131f3deSYOSHIFUJI Hideaki/吉藤英明 				continue;
1815c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
18169131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
18179131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
1818afbac601SDavid Ahern 
1819afbac601SDavid Ahern out:
1820c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	hiscore = &scores[hiscore_idx];
1821a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1822cc429c8fSEric Dumazet 		ret = -EADDRNOTAVAIL;
1823cc429c8fSEric Dumazet 	else
18244e3fd7a0SAlexey Dobriyan 		*saddr = hiscore->ifa->addr;
1825cc429c8fSEric Dumazet 
1826cc429c8fSEric Dumazet 	rcu_read_unlock();
1827cc429c8fSEric Dumazet 	return ret;
18281da177e4SLinus Torvalds }
18295e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
18301da177e4SLinus Torvalds 
18318965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1832479840ffSJiri Pirko 		      u32 banned_flags)
1833b7b1bfceSHannes Frederic Sowa {
1834b7b1bfceSHannes Frederic Sowa 	struct inet6_ifaddr *ifp;
1835b7b1bfceSHannes Frederic Sowa 	int err = -EADDRNOTAVAIL;
1836b7b1bfceSHannes Frederic Sowa 
1837602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1838602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1839602582caSHannes Frederic Sowa 			break;
1840b7b1bfceSHannes Frederic Sowa 		if (ifp->scope == IFA_LINK &&
1841b7b1bfceSHannes Frederic Sowa 		    !(ifp->flags & banned_flags)) {
1842b7b1bfceSHannes Frederic Sowa 			*addr = ifp->addr;
1843b7b1bfceSHannes Frederic Sowa 			err = 0;
1844b7b1bfceSHannes Frederic Sowa 			break;
1845b7b1bfceSHannes Frederic Sowa 		}
1846b7b1bfceSHannes Frederic Sowa 	}
1847b7b1bfceSHannes Frederic Sowa 	return err;
1848b7b1bfceSHannes Frederic Sowa }
1849b7b1bfceSHannes Frederic Sowa 
185095c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1851479840ffSJiri Pirko 		    u32 banned_flags)
18521da177e4SLinus Torvalds {
18531da177e4SLinus Torvalds 	struct inet6_dev *idev;
18541da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
18551da177e4SLinus Torvalds 
18568814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1857e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1858e21e8467SStephen Hemminger 	if (idev) {
18591da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1860b7b1bfceSHannes Frederic Sowa 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
18611da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
18621da177e4SLinus Torvalds 	}
18638814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
18641da177e4SLinus Torvalds 	return err;
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds 
1867d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev)
18681da177e4SLinus Torvalds {
1869d9bf82c2SEric Dumazet 	const struct inet6_ifaddr *ifp;
18701da177e4SLinus Torvalds 	int cnt = 0;
18711da177e4SLinus Torvalds 
1872d9bf82c2SEric Dumazet 	rcu_read_lock();
1873d9bf82c2SEric Dumazet 	list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
18741da177e4SLinus Torvalds 		cnt++;
1875d9bf82c2SEric Dumazet 	rcu_read_unlock();
18761da177e4SLinus Torvalds 	return cnt;
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds 
1879b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
18802a7851bfSFlorian Westphal 		  const struct net_device *dev, int strict)
18811da177e4SLinus Torvalds {
1882232378e8SDavid Ahern 	return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1883232378e8SDavid Ahern 				       strict, IFA_F_TENTATIVE);
1884c58da4c6SErik Kline }
1885c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr);
1886c58da4c6SErik Kline 
18871893ff20SDavid Ahern /* device argument is used to find the L3 domain of interest. If
18881893ff20SDavid Ahern  * skip_dev_check is set, then the ifp device is not checked against
18891893ff20SDavid Ahern  * the passed in dev argument. So the 2 cases for addresses checks are:
18901893ff20SDavid Ahern  *   1. does the address exist in the L3 domain that dev is part of
18911893ff20SDavid Ahern  *      (skip_dev_check = true), or
18921893ff20SDavid Ahern  *
18931893ff20SDavid Ahern  *   2. does the address exist on the specific device
18941893ff20SDavid Ahern  *      (skip_dev_check = false)
18951893ff20SDavid Ahern  */
1896c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1897232378e8SDavid Ahern 			    const struct net_device *dev, bool skip_dev_check,
1898232378e8SDavid Ahern 			    int strict, u32 banned_flags)
1899c58da4c6SErik Kline {
19003f27fb23SEric Dumazet 	unsigned int hash = inet6_addr_hash(net, addr);
19011893ff20SDavid Ahern 	const struct net_device *l3mdev;
1902eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1903c58da4c6SErik Kline 	u32 ifp_flags;
19041da177e4SLinus Torvalds 
1905480318a0SEric Dumazet 	rcu_read_lock();
1906232378e8SDavid Ahern 
19071893ff20SDavid Ahern 	l3mdev = l3mdev_master_dev_rcu(dev);
1908232378e8SDavid Ahern 	if (skip_dev_check)
1909232378e8SDavid Ahern 		dev = NULL;
1910232378e8SDavid Ahern 
1911b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1912878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1913bfeade08SDaniel Lezcano 			continue;
19141893ff20SDavid Ahern 
19151893ff20SDavid Ahern 		if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev)
19161893ff20SDavid Ahern 			continue;
19171893ff20SDavid Ahern 
1918c58da4c6SErik Kline 		/* Decouple optimistic from tentative for evaluation here.
1919c58da4c6SErik Kline 		 * Ban optimistic addresses explicitly, when required.
1920c58da4c6SErik Kline 		 */
1921c58da4c6SErik Kline 		ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1922c58da4c6SErik Kline 			    ? (ifp->flags&~IFA_F_TENTATIVE)
1923c58da4c6SErik Kline 			    : ifp->flags;
19241da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1925c58da4c6SErik Kline 		    !(ifp_flags&banned_flags) &&
192663159f29SIan Morris 		    (!dev || ifp->idev->dev == dev ||
1927eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1928480318a0SEric Dumazet 			rcu_read_unlock();
1929eedf042aSStephen Hemminger 			return 1;
1930eedf042aSStephen Hemminger 		}
1931eedf042aSStephen Hemminger 	}
19325c578aedSstephen hemminger 
1933480318a0SEric Dumazet 	rcu_read_unlock();
1934eedf042aSStephen Hemminger 	return 0;
19351da177e4SLinus Torvalds }
1936c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
19377159039aSYOSHIFUJI Hideaki 
19381da177e4SLinus Torvalds 
19397df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev.
19407df37ff3SCatalin\(ux\) M. BOIE  * If one is found it returns true.
19417df37ff3SCatalin\(ux\) M. BOIE  */
19427df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
19437df37ff3SCatalin\(ux\) M. BOIE 	const unsigned int prefix_len, struct net_device *dev)
19447df37ff3SCatalin\(ux\) M. BOIE {
194547e26941SEric Dumazet 	const struct inet6_ifaddr *ifa;
194647e26941SEric Dumazet 	const struct inet6_dev *idev;
19477df37ff3SCatalin\(ux\) M. BOIE 	bool ret = false;
19487df37ff3SCatalin\(ux\) M. BOIE 
19497df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_lock();
19507df37ff3SCatalin\(ux\) M. BOIE 	idev = __in6_dev_get(dev);
19517df37ff3SCatalin\(ux\) M. BOIE 	if (idev) {
195247e26941SEric Dumazet 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
19537df37ff3SCatalin\(ux\) M. BOIE 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
19547df37ff3SCatalin\(ux\) M. BOIE 			if (ret)
19557df37ff3SCatalin\(ux\) M. BOIE 				break;
19567df37ff3SCatalin\(ux\) M. BOIE 		}
19577df37ff3SCatalin\(ux\) M. BOIE 	}
19587df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_unlock();
19597df37ff3SCatalin\(ux\) M. BOIE 
19607df37ff3SCatalin\(ux\) M. BOIE 	return ret;
19617df37ff3SCatalin\(ux\) M. BOIE }
19627df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix);
19637df37ff3SCatalin\(ux\) M. BOIE 
1964b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
196552eeeb84SYOSHIFUJI Hideaki {
196624ba333bSEric Dumazet 	const struct inet6_ifaddr *ifa;
196724ba333bSEric Dumazet 	const struct inet6_dev *idev;
196852eeeb84SYOSHIFUJI Hideaki 	int	onlink;
196952eeeb84SYOSHIFUJI Hideaki 
197052eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
197152eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
197252eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
197352eeeb84SYOSHIFUJI Hideaki 	if (idev) {
197424ba333bSEric Dumazet 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
197552eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
197652eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
197752eeeb84SYOSHIFUJI Hideaki 			if (onlink)
197852eeeb84SYOSHIFUJI Hideaki 				break;
197952eeeb84SYOSHIFUJI Hideaki 		}
198052eeeb84SYOSHIFUJI Hideaki 	}
198152eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
198252eeeb84SYOSHIFUJI Hideaki 	return onlink;
198352eeeb84SYOSHIFUJI Hideaki }
198452eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
198552eeeb84SYOSHIFUJI Hideaki 
1986*81f6cb31SXin Long /**
1987*81f6cb31SXin Long  * ipv6_dev_find - find the first device with a given source address.
1988*81f6cb31SXin Long  * @net: the net namespace
1989*81f6cb31SXin Long  * @addr: the source address
1990*81f6cb31SXin Long  *
1991*81f6cb31SXin Long  * The caller should be protected by RCU, or RTNL.
1992*81f6cb31SXin Long  */
1993*81f6cb31SXin Long struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr)
1994*81f6cb31SXin Long {
1995*81f6cb31SXin Long 	unsigned int hash = inet6_addr_hash(net, addr);
1996*81f6cb31SXin Long 	struct inet6_ifaddr *ifp, *result = NULL;
1997*81f6cb31SXin Long 	struct net_device *dev = NULL;
1998*81f6cb31SXin Long 
1999*81f6cb31SXin Long 	rcu_read_lock();
2000*81f6cb31SXin Long 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
2001*81f6cb31SXin Long 		if (net_eq(dev_net(ifp->idev->dev), net) &&
2002*81f6cb31SXin Long 		    ipv6_addr_equal(&ifp->addr, addr)) {
2003*81f6cb31SXin Long 			result = ifp;
2004*81f6cb31SXin Long 			break;
2005*81f6cb31SXin Long 		}
2006*81f6cb31SXin Long 	}
2007*81f6cb31SXin Long 
2008*81f6cb31SXin Long 	if (!result) {
2009*81f6cb31SXin Long 		struct rt6_info *rt;
2010*81f6cb31SXin Long 
2011*81f6cb31SXin Long 		rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
2012*81f6cb31SXin Long 		if (rt) {
2013*81f6cb31SXin Long 			dev = rt->dst.dev;
2014*81f6cb31SXin Long 			ip6_rt_put(rt);
2015*81f6cb31SXin Long 		}
2016*81f6cb31SXin Long 	} else {
2017*81f6cb31SXin Long 		dev = result->idev->dev;
2018*81f6cb31SXin Long 	}
2019*81f6cb31SXin Long 	rcu_read_unlock();
2020*81f6cb31SXin Long 
2021*81f6cb31SXin Long 	return dev;
2022*81f6cb31SXin Long }
2023*81f6cb31SXin Long EXPORT_SYMBOL(ipv6_dev_find);
2024*81f6cb31SXin Long 
20259acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
20261cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
20271da177e4SLinus Torvalds {
20283f27fb23SEric Dumazet 	unsigned int hash = inet6_addr_hash(net, addr);
2029b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
20301da177e4SLinus Torvalds 
203124f226daSEric Dumazet 	rcu_read_lock();
203224f226daSEric Dumazet 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
2033878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
20341cab3da6SDaniel Lezcano 			continue;
20351da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
203663159f29SIan Morris 			if (!dev || ifp->idev->dev == dev ||
20371da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
2038b79d1d54SDavid S. Miller 				result = ifp;
20391da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
20401da177e4SLinus Torvalds 				break;
20411da177e4SLinus Torvalds 			}
20421da177e4SLinus Torvalds 		}
20431da177e4SLinus Torvalds 	}
204424f226daSEric Dumazet 	rcu_read_unlock();
20451da177e4SLinus Torvalds 
2046b79d1d54SDavid S. Miller 	return result;
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
20501da177e4SLinus Torvalds 
2051cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
20521da177e4SLinus Torvalds {
20533d171f39SLubomir Rintel 	if (dad_failed)
20543d171f39SLubomir Rintel 		ifp->flags |= IFA_F_DADFAILED;
20553d171f39SLubomir Rintel 
2056ec8add2aSSabrina Dubroca 	if (ifp->flags&IFA_F_TEMPORARY) {
20571da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
20581da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
20591da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
20601da177e4SLinus Torvalds 		if (ifpub) {
20611da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
20621da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
2063969c5464SFernando Gont 			ipv6_create_tempaddr(ifpub, true);
20641da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
20651da177e4SLinus Torvalds 		} else {
20661da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
20671da177e4SLinus Torvalds 		}
20681da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
2069ec8add2aSSabrina Dubroca 	} else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2070ec8add2aSSabrina Dubroca 		spin_lock_bh(&ifp->lock);
2071ec8add2aSSabrina Dubroca 		addrconf_del_dad_work(ifp);
2072ec8add2aSSabrina Dubroca 		ifp->flags |= IFA_F_TENTATIVE;
2073f1c02cfbSSabrina Dubroca 		if (dad_failed)
2074f1c02cfbSSabrina Dubroca 			ifp->flags &= ~IFA_F_OPTIMISTIC;
2075ec8add2aSSabrina Dubroca 		spin_unlock_bh(&ifp->lock);
2076ec8add2aSSabrina Dubroca 		if (dad_failed)
2077ec8add2aSSabrina Dubroca 			ipv6_ifa_notify(0, ifp);
2078ec8add2aSSabrina Dubroca 		in6_ifa_put(ifp);
2079c15b1ccaSHannes Frederic Sowa 	} else {
20801da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
20811da177e4SLinus Torvalds 	}
2082c15b1ccaSHannes Frederic Sowa }
20831da177e4SLinus Torvalds 
2084f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2085f2344a13SHerbert Xu {
2086f2344a13SHerbert Xu 	int err = -ENOENT;
2087f2344a13SHerbert Xu 
20888e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
2089f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
2090f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
2091f2344a13SHerbert Xu 		err = 0;
2092f2344a13SHerbert Xu 	}
20938e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
2094f2344a13SHerbert Xu 
2095f2344a13SHerbert Xu 	return err;
2096f2344a13SHerbert Xu }
2097f2344a13SHerbert Xu 
2098da13c59bSVishwanath Pai void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
20993c21edbdSYOSHIFUJI Hideaki {
21001b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
21011855b7c3SHannes Frederic Sowa 	struct net *net = dev_net(ifp->idev->dev);
21029bdd8d40SBrian Haley 
2103853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
2104853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
2105f2344a13SHerbert Xu 		return;
2106853dc2e0SUrsula Braun 	}
2107f2344a13SHerbert Xu 
2108da13c59bSVishwanath Pai 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2109da13c59bSVishwanath Pai 			     ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
21109bdd8d40SBrian Haley 
21115f40ef77SHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
21121b34be74SYOSHIFUJI Hideaki 
21135f40ef77SHannes Frederic Sowa 	if (ifp->flags & IFA_F_STABLE_PRIVACY) {
21145f40ef77SHannes Frederic Sowa 		struct in6_addr new_addr;
21155f40ef77SHannes Frederic Sowa 		struct inet6_ifaddr *ifp2;
21165f40ef77SHannes Frederic Sowa 		int retries = ifp->stable_privacy_retry + 1;
2117e6464b8cSDavid Ahern 		struct ifa6_config cfg = {
2118e6464b8cSDavid Ahern 			.pfx = &new_addr,
2119e6464b8cSDavid Ahern 			.plen = ifp->prefix_len,
2120e6464b8cSDavid Ahern 			.ifa_flags = ifp->flags,
2121e6464b8cSDavid Ahern 			.valid_lft = ifp->valid_lft,
2122e6464b8cSDavid Ahern 			.preferred_lft = ifp->prefered_lft,
2123e6464b8cSDavid Ahern 			.scope = ifp->scope,
2124e6464b8cSDavid Ahern 		};
21255f40ef77SHannes Frederic Sowa 
21261855b7c3SHannes Frederic Sowa 		if (retries > net->ipv6.sysctl.idgen_retries) {
21275f40ef77SHannes Frederic Sowa 			net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
21285f40ef77SHannes Frederic Sowa 					     ifp->idev->dev->name);
21295f40ef77SHannes Frederic Sowa 			goto errdad;
21305f40ef77SHannes Frederic Sowa 		}
21315f40ef77SHannes Frederic Sowa 
21325f40ef77SHannes Frederic Sowa 		new_addr = ifp->addr;
21335f40ef77SHannes Frederic Sowa 		if (ipv6_generate_stable_address(&new_addr, retries,
21345f40ef77SHannes Frederic Sowa 						 idev))
21355f40ef77SHannes Frederic Sowa 			goto errdad;
21365f40ef77SHannes Frederic Sowa 
21375f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp->lock);
21385f40ef77SHannes Frederic Sowa 
21395f40ef77SHannes Frederic Sowa 		if (idev->cnf.max_addresses &&
21405f40ef77SHannes Frederic Sowa 		    ipv6_count_addresses(idev) >=
21415f40ef77SHannes Frederic Sowa 		    idev->cnf.max_addresses)
21425f40ef77SHannes Frederic Sowa 			goto lock_errdad;
21435f40ef77SHannes Frederic Sowa 
21445f40ef77SHannes Frederic Sowa 		net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
21455f40ef77SHannes Frederic Sowa 				     ifp->idev->dev->name);
21465f40ef77SHannes Frederic Sowa 
2147e6464b8cSDavid Ahern 		ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
21485f40ef77SHannes Frederic Sowa 		if (IS_ERR(ifp2))
21495f40ef77SHannes Frederic Sowa 			goto lock_errdad;
21505f40ef77SHannes Frederic Sowa 
21515f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp2->lock);
21525f40ef77SHannes Frederic Sowa 		ifp2->stable_privacy_retry = retries;
21535f40ef77SHannes Frederic Sowa 		ifp2->state = INET6_IFADDR_STATE_PREDAD;
21545f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp2->lock);
21555f40ef77SHannes Frederic Sowa 
21561855b7c3SHannes Frederic Sowa 		addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
21575f40ef77SHannes Frederic Sowa 		in6_ifa_put(ifp2);
21585f40ef77SHannes Frederic Sowa lock_errdad:
21595f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp->lock);
21601b34be74SYOSHIFUJI Hideaki 	}
21611b34be74SYOSHIFUJI Hideaki 
21625f40ef77SHannes Frederic Sowa errdad:
2163c15b1ccaSHannes Frederic Sowa 	/* transition from _POSTDAD to _ERRDAD */
2164c15b1ccaSHannes Frederic Sowa 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
21658e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
2166c15b1ccaSHannes Frederic Sowa 
2167c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, 0);
2168751eb6b6SWei Yongjun 	in6_ifa_put(ifp);
21693c21edbdSYOSHIFUJI Hideaki }
21701da177e4SLinus Torvalds 
2171a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group.
2172a9ed4a29SSabrina Dubroca  * caller must hold RTNL */
2173b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
21741da177e4SLinus Torvalds {
21751da177e4SLinus Torvalds 	struct in6_addr maddr;
21761da177e4SLinus Torvalds 
21771da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
21781da177e4SLinus Torvalds 		return;
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
21811da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
21821da177e4SLinus Torvalds }
21831da177e4SLinus Torvalds 
2184a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
2185b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
21861da177e4SLinus Torvalds {
21871da177e4SLinus Torvalds 	struct in6_addr maddr;
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
21901da177e4SLinus Torvalds 		return;
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
21931da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
21941da177e4SLinus Torvalds }
21951da177e4SLinus Torvalds 
2196a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
219720380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
21981da177e4SLinus Torvalds {
21991da177e4SLinus Torvalds 	struct in6_addr addr;
2200c15b1ccaSHannes Frederic Sowa 
220188ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
22022bda8a0cSBjørn Mork 		return;
22031da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
22041da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
22051da177e4SLinus Torvalds 		return;
2206013b4d90SWANG Cong 	__ipv6_dev_ac_inc(ifp->idev, &addr);
22071da177e4SLinus Torvalds }
22081da177e4SLinus Torvalds 
2209a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
221020380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
22111da177e4SLinus Torvalds {
22121da177e4SLinus Torvalds 	struct in6_addr addr;
2213c15b1ccaSHannes Frederic Sowa 
221488ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
221532019e65SYOSHIFUJI Hideaki 		return;
22161da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
22171da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
22181da177e4SLinus Torvalds 		return;
22191da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
22201da177e4SLinus Torvalds }
22211da177e4SLinus Torvalds 
22228a7a4b47SAlexander Aring static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
222306a4c1c5Salex.bluesman.smirnov@gmail.com {
22248a7a4b47SAlexander Aring 	switch (dev->addr_len) {
22258a7a4b47SAlexander Aring 	case ETH_ALEN:
22269dae2e03SLuiz Augusto von Dentz 		memcpy(eui, dev->dev_addr, 3);
22279dae2e03SLuiz Augusto von Dentz 		eui[3] = 0xFF;
22289dae2e03SLuiz Augusto von Dentz 		eui[4] = 0xFE;
22299dae2e03SLuiz Augusto von Dentz 		memcpy(eui + 5, dev->dev_addr + 3, 3);
22309dae2e03SLuiz Augusto von Dentz 		break;
22318a7a4b47SAlexander Aring 	case EUI64_ADDR_LEN:
22325241c2d7SAlexander Aring 		memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
22335e98a36eSYOSHIFUJI Hideaki / 吉藤英明 		eui[0] ^= 2;
22348a7a4b47SAlexander Aring 		break;
22358a7a4b47SAlexander Aring 	default:
22368a7a4b47SAlexander Aring 		return -1;
22378a7a4b47SAlexander Aring 	}
22388a7a4b47SAlexander Aring 
223906a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
224006a4c1c5Salex.bluesman.smirnov@gmail.com }
224106a4c1c5Salex.bluesman.smirnov@gmail.com 
2242cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2243cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
2244cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	union fwnet_hwaddr *ha;
2245cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2246cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
2247cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
2248cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2249cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	ha = (union fwnet_hwaddr *)dev->dev_addr;
2250cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2251cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2252cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
2253cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
2254cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
2255cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2256073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2257073a8e0eSYOSHIFUJI Hideaki {
22581da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
22591da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
22601da177e4SLinus Torvalds 		return -1;
22611da177e4SLinus Torvalds 	memset(eui, 0, 7);
22621da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
22631da177e4SLinus Torvalds 	return 0;
2264073a8e0eSYOSHIFUJI Hideaki }
2265073a8e0eSYOSHIFUJI Hideaki 
2266073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2267073a8e0eSYOSHIFUJI Hideaki {
22681da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
22691da177e4SLinus Torvalds 		return -1;
22701da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
22711da177e4SLinus Torvalds 	eui[0] |= 2;
22721da177e4SLinus Torvalds 	return 0;
22731da177e4SLinus Torvalds }
2274073a8e0eSYOSHIFUJI Hideaki 
2275c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2276dfd982baSYOSHIFUJI Hideaki {
22779af28511SSascha Hlusiak 	if (addr == 0)
22789af28511SSascha Hlusiak 		return -1;
2279dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2280dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2281dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2282dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2283dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2284dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2285dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
2286dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
2287dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
2288dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
2289dfd982baSYOSHIFUJI Hideaki 	return 0;
2290dfd982baSYOSHIFUJI Hideaki }
2291dfd982baSYOSHIFUJI Hideaki 
2292dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2293dfd982baSYOSHIFUJI Hideaki {
2294dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
2295dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2296dfd982baSYOSHIFUJI Hideaki 	return -1;
2297dfd982baSYOSHIFUJI Hideaki }
2298dfd982baSYOSHIFUJI Hideaki 
2299aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2300aee80b54Sstephen hemminger {
2301aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2302aee80b54Sstephen hemminger }
2303aee80b54Sstephen hemminger 
2304e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2305e837735eSNicolas Dichtel {
2306e837735eSNicolas Dichtel 	memcpy(eui, dev->perm_addr, 3);
2307e837735eSNicolas Dichtel 	memcpy(eui + 5, dev->perm_addr + 3, 3);
2308e837735eSNicolas Dichtel 	eui[3] = 0xFF;
2309e837735eSNicolas Dichtel 	eui[4] = 0xFE;
2310e837735eSNicolas Dichtel 	eui[0] ^= 2;
2311e837735eSNicolas Dichtel 	return 0;
2312e837735eSNicolas Dichtel }
2313e837735eSNicolas Dichtel 
2314073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2315073a8e0eSYOSHIFUJI Hideaki {
2316073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
2317073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
2318073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
2319073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
2320073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
2321073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
2322073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
2323073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
2324c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
2325dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
2326aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
232745ce0fd1SFelix Jia 	case ARPHRD_TUNNEL:
2328aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
2329e74bccb8SJukka Rissanen 	case ARPHRD_6LOWPAN:
23308a7a4b47SAlexander Aring 		return addrconf_ifid_6lowpan(eui, dev);
2331cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
2332cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
2333e837735eSNicolas Dichtel 	case ARPHRD_TUNNEL6:
233445ce0fd1SFelix Jia 	case ARPHRD_IP6GRE:
23359deb441cSSubash Abhinov Kasiviswanathan 	case ARPHRD_RAWIP:
2336e837735eSNicolas Dichtel 		return addrconf_ifid_ip6tnl(eui, dev);
2337073a8e0eSYOSHIFUJI Hideaki 	}
23381da177e4SLinus Torvalds 	return -1;
23391da177e4SLinus Torvalds }
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
23421da177e4SLinus Torvalds {
23431da177e4SLinus Torvalds 	int err = -1;
23441da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2347602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2348602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
2349602582caSHannes Frederic Sowa 			break;
23501da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
23511da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
23521da177e4SLinus Torvalds 			err = 0;
23531da177e4SLinus Torvalds 			break;
23541da177e4SLinus Torvalds 		}
23551da177e4SLinus Torvalds 	}
23561da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
23571da177e4SLinus Torvalds 	return err;
23581da177e4SLinus Torvalds }
23591da177e4SLinus Torvalds 
2360969c5464SFernando Gont /* Generation of a randomized Interface Identifier
2361969c5464SFernando Gont  * draft-ietf-6man-rfc4941bis, Section 3.3.1
2362969c5464SFernando Gont  */
2363969c5464SFernando Gont 
2364969c5464SFernando Gont static void ipv6_gen_rnd_iid(struct in6_addr *addr)
23651da177e4SLinus Torvalds {
23661da177e4SLinus Torvalds regen:
2367969c5464SFernando Gont 	get_random_bytes(&addr->s6_addr[8], 8);
23681da177e4SLinus Torvalds 
2369969c5464SFernando Gont 	/* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1:
2370969c5464SFernando Gont 	 * check if generated address is not inappropriate:
23711da177e4SLinus Torvalds 	 *
2372969c5464SFernando Gont 	 * - Reserved IPv6 Interface Identifers
23731da177e4SLinus Torvalds 	 * - XXX: already assigned to an address on the device
23741da177e4SLinus Torvalds 	 */
23751da177e4SLinus Torvalds 
2376969c5464SFernando Gont 	/* Subnet-router anycast: 0000:0000:0000:0000 */
2377969c5464SFernando Gont 	if (!(addr->s6_addr32[2] | addr->s6_addr32[3]))
2378969c5464SFernando Gont 		goto regen;
2379969c5464SFernando Gont 
2380969c5464SFernando Gont 	/* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212
2381969c5464SFernando Gont 	 * Proxy Mobile IPv6:   0200:5EFF:FE00:5213
2382969c5464SFernando Gont 	 * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF
2383969c5464SFernando Gont 	 */
2384969c5464SFernando Gont 	if (ntohl(addr->s6_addr32[2]) == 0x02005eff &&
2385969c5464SFernando Gont 	    (ntohl(addr->s6_addr32[3]) & 0Xff000000) == 0xfe000000)
2386969c5464SFernando Gont 		goto regen;
2387969c5464SFernando Gont 
2388969c5464SFernando Gont 	/* Reserved subnet anycast addresses */
2389969c5464SFernando Gont 	if (ntohl(addr->s6_addr32[2]) == 0xfdffffff &&
2390969c5464SFernando Gont 	    ntohl(addr->s6_addr32[3]) >= 0Xffffff80)
2391969c5464SFernando Gont 		goto regen;
23921da177e4SLinus Torvalds }
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds /*
23951da177e4SLinus Torvalds  *	Add prefix route.
23961da177e4SLinus Torvalds  */
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds static void
23998308f3ffSDavid Ahern addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
24008308f3ffSDavid Ahern 		      struct net_device *dev, unsigned long expires,
24018308f3ffSDavid Ahern 		      u32 flags, gfp_t gfp_flags)
24021da177e4SLinus Torvalds {
240386872cb5SThomas Graf 	struct fib6_config cfg = {
2404ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
24058308f3ffSDavid Ahern 		.fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
240686872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
240786872cb5SThomas Graf 		.fc_expires = expires,
240886872cb5SThomas Graf 		.fc_dst_len = plen,
240986872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
2410c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
2411f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
2412e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
241386872cb5SThomas Graf 	};
24141da177e4SLinus Torvalds 
24154e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
24181da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
24191da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
24201da177e4SLinus Torvalds 	 */
242107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
24221da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
242386872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
24240be669bbSJoerg Roedel #endif
24251da177e4SLinus Torvalds 
2426acb54e3cSDavid Ahern 	ip6_route_add(&cfg, gfp_flags, NULL);
24271da177e4SLinus Torvalds }
24281da177e4SLinus Torvalds 
242914ef37b6SAndreas Hofmeister 
24308d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
243114ef37b6SAndreas Hofmeister 						  int plen,
243214ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
24332b2450caSDavid Ahern 						  u32 flags, u32 noflags,
24342b2450caSDavid Ahern 						  bool no_gw)
243514ef37b6SAndreas Hofmeister {
243614ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
24378d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
243814ef37b6SAndreas Hofmeister 	struct fib6_table *table;
2439ca254490SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
244014ef37b6SAndreas Hofmeister 
2441ca254490SDavid Ahern 	table = fib6_get_table(dev_net(dev), tb_id);
244263159f29SIan Morris 	if (!table)
244314ef37b6SAndreas Hofmeister 		return NULL;
244414ef37b6SAndreas Hofmeister 
244566f5d6ceSWei Wang 	rcu_read_lock();
244638fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
244714ef37b6SAndreas Hofmeister 	if (!fn)
244814ef37b6SAndreas Hofmeister 		goto out;
24491f56a01fSMartin KaFai Lau 
245066f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
2451f88d8ea6SDavid Ahern 		/* prefix routes only use builtin fib6_nh */
2452f88d8ea6SDavid Ahern 		if (rt->nh)
2453f88d8ea6SDavid Ahern 			continue;
2454f88d8ea6SDavid Ahern 
24551cf844c7SDavid Ahern 		if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
245614ef37b6SAndreas Hofmeister 			continue;
24571cf844c7SDavid Ahern 		if (no_gw && rt->fib6_nh->fib_nh_gw_family)
24582b2450caSDavid Ahern 			continue;
245993c2fb25SDavid Ahern 		if ((rt->fib6_flags & flags) != flags)
246014ef37b6SAndreas Hofmeister 			continue;
246193c2fb25SDavid Ahern 		if ((rt->fib6_flags & noflags) != 0)
246214ef37b6SAndreas Hofmeister 			continue;
2463e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
2464e873e4b9SWei Wang 			continue;
246514ef37b6SAndreas Hofmeister 		break;
246614ef37b6SAndreas Hofmeister 	}
246714ef37b6SAndreas Hofmeister out:
246866f5d6ceSWei Wang 	rcu_read_unlock();
246914ef37b6SAndreas Hofmeister 	return rt;
247014ef37b6SAndreas Hofmeister }
247114ef37b6SAndreas Hofmeister 
247214ef37b6SAndreas Hofmeister 
24731da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
24761da177e4SLinus Torvalds {
247786872cb5SThomas Graf 	struct fib6_config cfg = {
2478ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
247986872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
248086872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
248186872cb5SThomas Graf 		.fc_dst_len = 8,
248286872cb5SThomas Graf 		.fc_flags = RTF_UP,
2483e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
2484c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
248586872cb5SThomas Graf 	};
24861da177e4SLinus Torvalds 
248786872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
248886872cb5SThomas Graf 
2489e500c6d3SCong Wang 	ip6_route_add(&cfg, GFP_KERNEL, NULL);
24901da177e4SLinus Torvalds }
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
24931da177e4SLinus Torvalds {
24941da177e4SLinus Torvalds 	struct inet6_dev *idev;
24951da177e4SLinus Torvalds 
24961da177e4SLinus Torvalds 	ASSERT_RTNL();
24971da177e4SLinus Torvalds 
2498e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2499db0b99f5SSabrina Dubroca 	if (IS_ERR(idev))
2500db0b99f5SSabrina Dubroca 		return idev;
250164e724f6SBrian Haley 
250264e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
250364e724f6SBrian Haley 		return ERR_PTR(-EACCES);
25041da177e4SLinus Torvalds 
25051da177e4SLinus Torvalds 	/* Add default multicast route */
2506ba46ee4cSDavid Ahern 	if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
25071da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
25081da177e4SLinus Torvalds 
25091da177e4SLinus Torvalds 	return idev;
25101da177e4SLinus Torvalds }
25111da177e4SLinus Torvalds 
251253bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev,
251353bd6749SJiri Pirko 			     struct inet6_ifaddr *ifp,
251453bd6749SJiri Pirko 			     __u32 valid_lft, __u32 prefered_lft,
251553bd6749SJiri Pirko 			     bool create, unsigned long now)
251653bd6749SJiri Pirko {
251753bd6749SJiri Pirko 	u32 flags;
251853bd6749SJiri Pirko 	struct inet6_ifaddr *ift;
251953bd6749SJiri Pirko 
252053bd6749SJiri Pirko 	read_lock_bh(&idev->lock);
252153bd6749SJiri Pirko 	/* update all temporary addresses in the list */
252253bd6749SJiri Pirko 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
252353bd6749SJiri Pirko 		int age, max_valid, max_prefered;
252453bd6749SJiri Pirko 
252553bd6749SJiri Pirko 		if (ifp != ift->ifpub)
252653bd6749SJiri Pirko 			continue;
252753bd6749SJiri Pirko 
252853bd6749SJiri Pirko 		/* RFC 4941 section 3.3:
252953bd6749SJiri Pirko 		 * If a received option will extend the lifetime of a public
253053bd6749SJiri Pirko 		 * address, the lifetimes of temporary addresses should
253153bd6749SJiri Pirko 		 * be extended, subject to the overall constraint that no
253253bd6749SJiri Pirko 		 * temporary addresses should ever remain "valid" or "preferred"
253353bd6749SJiri Pirko 		 * for a time longer than (TEMP_VALID_LIFETIME) or
253453bd6749SJiri Pirko 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
253553bd6749SJiri Pirko 		 */
253653bd6749SJiri Pirko 		age = (now - ift->cstamp) / HZ;
253753bd6749SJiri Pirko 		max_valid = idev->cnf.temp_valid_lft - age;
253853bd6749SJiri Pirko 		if (max_valid < 0)
253953bd6749SJiri Pirko 			max_valid = 0;
254053bd6749SJiri Pirko 
254153bd6749SJiri Pirko 		max_prefered = idev->cnf.temp_prefered_lft -
254276506a98SJiri Bohac 			       idev->desync_factor - age;
254353bd6749SJiri Pirko 		if (max_prefered < 0)
254453bd6749SJiri Pirko 			max_prefered = 0;
254553bd6749SJiri Pirko 
254653bd6749SJiri Pirko 		if (valid_lft > max_valid)
254753bd6749SJiri Pirko 			valid_lft = max_valid;
254853bd6749SJiri Pirko 
254953bd6749SJiri Pirko 		if (prefered_lft > max_prefered)
255053bd6749SJiri Pirko 			prefered_lft = max_prefered;
255153bd6749SJiri Pirko 
255253bd6749SJiri Pirko 		spin_lock(&ift->lock);
255353bd6749SJiri Pirko 		flags = ift->flags;
255453bd6749SJiri Pirko 		ift->valid_lft = valid_lft;
255553bd6749SJiri Pirko 		ift->prefered_lft = prefered_lft;
255653bd6749SJiri Pirko 		ift->tstamp = now;
255753bd6749SJiri Pirko 		if (prefered_lft > 0)
255853bd6749SJiri Pirko 			ift->flags &= ~IFA_F_DEPRECATED;
255953bd6749SJiri Pirko 
256053bd6749SJiri Pirko 		spin_unlock(&ift->lock);
256153bd6749SJiri Pirko 		if (!(flags&IFA_F_TENTATIVE))
256253bd6749SJiri Pirko 			ipv6_ifa_notify(0, ift);
256353bd6749SJiri Pirko 	}
256453bd6749SJiri Pirko 
256553bd6749SJiri Pirko 	if ((create || list_empty(&idev->tempaddr_list)) &&
256653bd6749SJiri Pirko 	    idev->cnf.use_tempaddr > 0) {
256753bd6749SJiri Pirko 		/* When a new public address is created as described
256853bd6749SJiri Pirko 		 * in [ADDRCONF], also create a new temporary address.
256953bd6749SJiri Pirko 		 * Also create a temporary address if it's enabled but
257053bd6749SJiri Pirko 		 * no temporary address currently exists.
257153bd6749SJiri Pirko 		 */
257253bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
2573969c5464SFernando Gont 		ipv6_create_tempaddr(ifp, false);
257453bd6749SJiri Pirko 	} else {
257553bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
257653bd6749SJiri Pirko 	}
257753bd6749SJiri Pirko }
257853bd6749SJiri Pirko 
2579cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2580cc9da6ccSBjørn Mork {
2581d35a00b8SFelix Jia 	return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2582d35a00b8SFelix Jia 	       idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2583cc9da6ccSBjørn Mork }
2584cc9da6ccSBjørn Mork 
2585cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
25864f672235SAlexander Aring 				 const struct prefix_info *pinfo,
25874f672235SAlexander Aring 				 struct inet6_dev *in6_dev,
2588cc84b3c6SAlexander Aring 				 const struct in6_addr *addr, int addr_type,
2589cc84b3c6SAlexander Aring 				 u32 addr_flags, bool sllao, bool tokenized,
25904f672235SAlexander Aring 				 __u32 valid_lft, u32 prefered_lft)
25914f672235SAlexander Aring {
25924f672235SAlexander Aring 	struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2593b75326c2SFernando Gont 	int create = 0;
25944f672235SAlexander Aring 
25954f672235SAlexander Aring 	if (!ifp && valid_lft) {
25964f672235SAlexander Aring 		int max_addresses = in6_dev->cnf.max_addresses;
2597e6464b8cSDavid Ahern 		struct ifa6_config cfg = {
2598e6464b8cSDavid Ahern 			.pfx = addr,
2599e6464b8cSDavid Ahern 			.plen = pinfo->prefix_len,
2600e6464b8cSDavid Ahern 			.ifa_flags = addr_flags,
2601e6464b8cSDavid Ahern 			.valid_lft = valid_lft,
2602e6464b8cSDavid Ahern 			.preferred_lft = prefered_lft,
2603e6464b8cSDavid Ahern 			.scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2604e6464b8cSDavid Ahern 		};
26054f672235SAlexander Aring 
26064f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
260735e015e1SMatteo Croce 		if ((net->ipv6.devconf_all->optimistic_dad ||
260835e015e1SMatteo Croce 		     in6_dev->cnf.optimistic_dad) &&
26094f672235SAlexander Aring 		    !net->ipv6.devconf_all->forwarding && sllao)
2610e6464b8cSDavid Ahern 			cfg.ifa_flags |= IFA_F_OPTIMISTIC;
26114f672235SAlexander Aring #endif
26124f672235SAlexander Aring 
26134f672235SAlexander Aring 		/* Do not allow to create too much of autoconfigured
26144f672235SAlexander Aring 		 * addresses; this would be too easy way to crash kernel.
26154f672235SAlexander Aring 		 */
26164f672235SAlexander Aring 		if (!max_addresses ||
26174f672235SAlexander Aring 		    ipv6_count_addresses(in6_dev) < max_addresses)
2618e6464b8cSDavid Ahern 			ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
26194f672235SAlexander Aring 
26204f672235SAlexander Aring 		if (IS_ERR_OR_NULL(ifp))
26214f672235SAlexander Aring 			return -1;
26224f672235SAlexander Aring 
26234f672235SAlexander Aring 		create = 1;
26244f672235SAlexander Aring 		spin_lock_bh(&ifp->lock);
26254f672235SAlexander Aring 		ifp->flags |= IFA_F_MANAGETEMPADDR;
26264f672235SAlexander Aring 		ifp->cstamp = jiffies;
26274f672235SAlexander Aring 		ifp->tokenized = tokenized;
26284f672235SAlexander Aring 		spin_unlock_bh(&ifp->lock);
26294f672235SAlexander Aring 		addrconf_dad_start(ifp);
26304f672235SAlexander Aring 	}
26314f672235SAlexander Aring 
26324f672235SAlexander Aring 	if (ifp) {
26334f672235SAlexander Aring 		u32 flags;
26344f672235SAlexander Aring 		unsigned long now;
26354f672235SAlexander Aring 		u32 stored_lft;
26364f672235SAlexander Aring 
2637b75326c2SFernando Gont 		/* Update lifetime (RFC4862 5.5.3 e)
2638b75326c2SFernando Gont 		 * We deviate from RFC4862 by honoring all Valid Lifetimes to
2639b75326c2SFernando Gont 		 * improve the reaction of SLAAC to renumbering events
2640b75326c2SFernando Gont 		 * (draft-gont-6man-slaac-renum-06, Section 4.2)
2641b75326c2SFernando Gont 		 */
26424f672235SAlexander Aring 		spin_lock_bh(&ifp->lock);
26434f672235SAlexander Aring 		now = jiffies;
26444f672235SAlexander Aring 		if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
26454f672235SAlexander Aring 			stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
26464f672235SAlexander Aring 		else
26474f672235SAlexander Aring 			stored_lft = 0;
2648b75326c2SFernando Gont 
2649f85f94b8SLorenzo Bianconi 		if (!create && stored_lft) {
26504f672235SAlexander Aring 			ifp->valid_lft = valid_lft;
26514f672235SAlexander Aring 			ifp->prefered_lft = prefered_lft;
26524f672235SAlexander Aring 			ifp->tstamp = now;
26534f672235SAlexander Aring 			flags = ifp->flags;
26544f672235SAlexander Aring 			ifp->flags &= ~IFA_F_DEPRECATED;
26554f672235SAlexander Aring 			spin_unlock_bh(&ifp->lock);
26564f672235SAlexander Aring 
26574f672235SAlexander Aring 			if (!(flags&IFA_F_TENTATIVE))
26584f672235SAlexander Aring 				ipv6_ifa_notify(0, ifp);
26594f672235SAlexander Aring 		} else
26604f672235SAlexander Aring 			spin_unlock_bh(&ifp->lock);
26614f672235SAlexander Aring 
26624f672235SAlexander Aring 		manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
26634f672235SAlexander Aring 				 create, now);
26644f672235SAlexander Aring 
26654f672235SAlexander Aring 		in6_ifa_put(ifp);
26664f672235SAlexander Aring 		addrconf_verify();
26674f672235SAlexander Aring 	}
26684f672235SAlexander Aring 
26694f672235SAlexander Aring 	return 0;
26704f672235SAlexander Aring }
2671cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
26724f672235SAlexander Aring 
2673e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
26741da177e4SLinus Torvalds {
26751da177e4SLinus Torvalds 	struct prefix_info *pinfo;
26761da177e4SLinus Torvalds 	__u32 valid_lft;
26771da177e4SLinus Torvalds 	__u32 prefered_lft;
26784f672235SAlexander Aring 	int addr_type, err;
267964236f3fSHannes Frederic Sowa 	u32 addr_flags = 0;
26801da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
268156d417b1SBrian Haley 	struct net *net = dev_net(dev);
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
26841da177e4SLinus Torvalds 
26851da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
2686e32ac250SJoe Perches 		netdev_dbg(dev, "addrconf: prefix option too short\n");
26871da177e4SLinus Torvalds 		return;
26881da177e4SLinus Torvalds 	}
26891da177e4SLinus Torvalds 
26901da177e4SLinus Torvalds 	/*
26911da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
26921da177e4SLinus Torvalds 	 */
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
26971da177e4SLinus Torvalds 		return;
26981da177e4SLinus Torvalds 
26991da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
27001da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2703e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
27041da177e4SLinus Torvalds 		return;
27051da177e4SLinus Torvalds 	}
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
27081da177e4SLinus Torvalds 
270963159f29SIan Morris 	if (!in6_dev) {
2710e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2711e87cc472SJoe Perches 				    dev->name);
27121da177e4SLinus Torvalds 		return;
27131da177e4SLinus Torvalds 	}
27141da177e4SLinus Torvalds 
27151da177e4SLinus Torvalds 	/*
27161da177e4SLinus Torvalds 	 *	Two things going on here:
27171da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
27181da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
27191da177e4SLinus Torvalds 	 */
27201da177e4SLinus Torvalds 
27214bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
27228d1c802bSDavid Ahern 		struct fib6_info *rt;
27234bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
27244bed72e4SYOSHIFUJI Hideaki 
27251da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
27266f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
27276f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
27286f704992SYOSHIFUJI Hideaki 		 * not good.
27291da177e4SLinus Torvalds 		 */
27304bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
27314bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
27324bed72e4SYOSHIFUJI Hideaki 		else
27334bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
27343dd4bc68SYOSHIFUJI Hideaki 
27354bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
27364bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
27371da177e4SLinus Torvalds 
273814ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
273914ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
274014ef37b6SAndreas Hofmeister 					       dev,
274114ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
27422b2450caSDavid Ahern 					       RTF_DEFAULT, true);
27431da177e4SLinus Torvalds 
274414ef37b6SAndreas Hofmeister 		if (rt) {
27456f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
27461da177e4SLinus Torvalds 			if (valid_lft == 0) {
274711dd74b3SRoopa Prabhu 				ip6_del_rt(net, rt, false);
27481da177e4SLinus Torvalds 				rt = NULL;
27494bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
27506f704992SYOSHIFUJI Hideaki 				/* not infinity */
275114895687SDavid Ahern 				fib6_set_expires(rt, jiffies + rt_expires);
27526f704992SYOSHIFUJI Hideaki 			} else {
275314895687SDavid Ahern 				fib6_clean_expires(rt);
27541da177e4SLinus Torvalds 			}
27551da177e4SLinus Torvalds 		} else if (valid_lft) {
27566f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
27574bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
27584bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
27596f704992SYOSHIFUJI Hideaki 				/* not infinity */
27606f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
27616f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
27626f704992SYOSHIFUJI Hideaki 			}
27631da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
27648308f3ffSDavid Ahern 					      0, dev, expires, flags,
27658308f3ffSDavid Ahern 					      GFP_ATOMIC);
27661da177e4SLinus Torvalds 		}
276793531c67SDavid Ahern 		fib6_info_release(rt);
27681da177e4SLinus Torvalds 	}
27691da177e4SLinus Torvalds 
27701da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
27711da177e4SLinus Torvalds 
27721da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
27731da177e4SLinus Torvalds 		struct in6_addr addr;
2774f997c55cSAlexander Aring 		bool tokenized = false, dev_addr_generated = false;
27751da177e4SLinus Torvalds 
27761da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
27771da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2778f53adae4SDaniel Borkmann 
2779f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2780f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2781f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2782f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2783f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
2784617fe29dSDaniel Borkmann 				tokenized = true;
2785cc9da6ccSBjørn Mork 			} else if (is_addr_mode_generate_stable(in6_dev) &&
2786622c81d5SHannes Frederic Sowa 				   !ipv6_generate_stable_address(&addr, 0,
2787622c81d5SHannes Frederic Sowa 								 in6_dev)) {
278864236f3fSHannes Frederic Sowa 				addr_flags |= IFA_F_STABLE_PRIVACY;
2789622c81d5SHannes Frederic Sowa 				goto ok;
2790f53adae4SDaniel Borkmann 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
27911da177e4SLinus Torvalds 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
27924f672235SAlexander Aring 				goto put;
2793f997c55cSAlexander Aring 			} else {
2794f997c55cSAlexander Aring 				dev_addr_generated = true;
27951da177e4SLinus Torvalds 			}
27961da177e4SLinus Torvalds 			goto ok;
27971da177e4SLinus Torvalds 		}
2798e87cc472SJoe Perches 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
27991da177e4SLinus Torvalds 				    pinfo->prefix_len);
28004f672235SAlexander Aring 		goto put;
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds ok:
28034f672235SAlexander Aring 		err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
28044f672235SAlexander Aring 						   &addr, addr_type,
28054f672235SAlexander Aring 						   addr_flags, sllao,
28064f672235SAlexander Aring 						   tokenized, valid_lft,
28078a226b2cSJiri Benc 						   prefered_lft);
28084f672235SAlexander Aring 		if (err)
28094f672235SAlexander Aring 			goto put;
2810f997c55cSAlexander Aring 
2811f997c55cSAlexander Aring 		/* Ignore error case here because previous prefix add addr was
2812f997c55cSAlexander Aring 		 * successful which will be notified.
2813f997c55cSAlexander Aring 		 */
2814f997c55cSAlexander Aring 		ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2815f997c55cSAlexander Aring 					      addr_type, addr_flags, sllao,
2816f997c55cSAlexander Aring 					      tokenized, valid_lft,
2817f997c55cSAlexander Aring 					      prefered_lft,
2818f997c55cSAlexander Aring 					      dev_addr_generated);
28191da177e4SLinus Torvalds 	}
28201da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
28214f672235SAlexander Aring put:
28221da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
28231da177e4SLinus Torvalds }
28241da177e4SLinus Torvalds 
282568ad6886SChristoph Hellwig static int addrconf_set_sit_dstaddr(struct net *net, struct net_device *dev,
282668ad6886SChristoph Hellwig 		struct in6_ifreq *ireq)
282768ad6886SChristoph Hellwig {
282868ad6886SChristoph Hellwig 	struct ip_tunnel_parm p = { };
282968ad6886SChristoph Hellwig 	int err;
283068ad6886SChristoph Hellwig 
283168ad6886SChristoph Hellwig 	if (!(ipv6_addr_type(&ireq->ifr6_addr) & IPV6_ADDR_COMPATv4))
283268ad6886SChristoph Hellwig 		return -EADDRNOTAVAIL;
283368ad6886SChristoph Hellwig 
283468ad6886SChristoph Hellwig 	p.iph.daddr = ireq->ifr6_addr.s6_addr32[3];
283568ad6886SChristoph Hellwig 	p.iph.version = 4;
283668ad6886SChristoph Hellwig 	p.iph.ihl = 5;
283768ad6886SChristoph Hellwig 	p.iph.protocol = IPPROTO_IPV6;
283868ad6886SChristoph Hellwig 	p.iph.ttl = 64;
283968ad6886SChristoph Hellwig 
28408e3db0bbSChristoph Hellwig 	if (!dev->netdev_ops->ndo_tunnel_ctl)
284168ad6886SChristoph Hellwig 		return -EOPNOTSUPP;
28428e3db0bbSChristoph Hellwig 	err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, SIOCADDTUNNEL);
284368ad6886SChristoph Hellwig 	if (err)
284468ad6886SChristoph Hellwig 		return err;
284568ad6886SChristoph Hellwig 
284668ad6886SChristoph Hellwig 	dev = __dev_get_by_name(net, p.name);
284768ad6886SChristoph Hellwig 	if (!dev)
284868ad6886SChristoph Hellwig 		return -ENOBUFS;
284968ad6886SChristoph Hellwig 	return dev_open(dev, NULL);
285068ad6886SChristoph Hellwig }
285168ad6886SChristoph Hellwig 
28521da177e4SLinus Torvalds /*
28531da177e4SLinus Torvalds  *	Set destination address.
28541da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
28551da177e4SLinus Torvalds  *	device.
28561da177e4SLinus Torvalds  */
2857af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
28581da177e4SLinus Torvalds {
28591da177e4SLinus Torvalds 	struct net_device *dev;
286068ad6886SChristoph Hellwig 	struct in6_ifreq ireq;
286168ad6886SChristoph Hellwig 	int err = -ENODEV;
28621da177e4SLinus Torvalds 
2863f0988460SChristoph Hellwig 	if (!IS_ENABLED(CONFIG_IPV6_SIT))
2864f0988460SChristoph Hellwig 		return -ENODEV;
286568ad6886SChristoph Hellwig 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
286668ad6886SChristoph Hellwig 		return -EFAULT;
28671da177e4SLinus Torvalds 
28681da177e4SLinus Torvalds 	rtnl_lock();
2869af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
287068ad6886SChristoph Hellwig 	if (dev && dev->type == ARPHRD_SIT)
287168ad6886SChristoph Hellwig 		err = addrconf_set_sit_dstaddr(net, dev, &ireq);
28721da177e4SLinus Torvalds 	rtnl_unlock();
28731da177e4SLinus Torvalds 	return err;
28741da177e4SLinus Torvalds }
28751da177e4SLinus Torvalds 
287693a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join,
287793a714d6SMadhu Challa 			  const struct in6_addr *addr, int ifindex)
287893a714d6SMadhu Challa {
287993a714d6SMadhu Challa 	int ret;
288093a714d6SMadhu Challa 
288193a714d6SMadhu Challa 	ASSERT_RTNL();
288293a714d6SMadhu Challa 
288393a714d6SMadhu Challa 	lock_sock(sk);
288493a714d6SMadhu Challa 	if (join)
288554ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_join(sk, ifindex, addr);
288693a714d6SMadhu Challa 	else
288754ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_drop(sk, ifindex, addr);
288893a714d6SMadhu Challa 	release_sock(sk);
288993a714d6SMadhu Challa 
289093a714d6SMadhu Challa 	return ret;
289193a714d6SMadhu Challa }
289293a714d6SMadhu Challa 
28931da177e4SLinus Torvalds /*
28941da177e4SLinus Torvalds  *	Manual configuration of address on an interface
28951da177e4SLinus Torvalds  */
2896479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex,
289719b1518cSDavid Ahern 			  struct ifa6_config *cfg,
2898de95e047SDavid Ahern 			  struct netlink_ext_ack *extack)
28991da177e4SLinus Torvalds {
29001da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
29011da177e4SLinus Torvalds 	struct inet6_dev *idev;
29021da177e4SLinus Torvalds 	struct net_device *dev;
290393a714d6SMadhu Challa 	unsigned long timeout;
290493a714d6SMadhu Challa 	clock_t expires;
29056f704992SYOSHIFUJI Hideaki 	u32 flags;
29061da177e4SLinus Torvalds 
29071da177e4SLinus Torvalds 	ASSERT_RTNL();
29081da177e4SLinus Torvalds 
290919b1518cSDavid Ahern 	if (cfg->plen > 128)
291024ef0da7SThomas Graf 		return -EINVAL;
291124ef0da7SThomas Graf 
29120778769dSNoriaki TAKAMIYA 	/* check the lifetime */
291319b1518cSDavid Ahern 	if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
29140778769dSNoriaki TAKAMIYA 		return -EINVAL;
29150778769dSNoriaki TAKAMIYA 
291619b1518cSDavid Ahern 	if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64)
291753bd6749SJiri Pirko 		return -EINVAL;
291853bd6749SJiri Pirko 
2919af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2920af284937SDaniel Lezcano 	if (!dev)
29211da177e4SLinus Torvalds 		return -ENODEV;
29221da177e4SLinus Torvalds 
292364e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
292464e724f6SBrian Haley 	if (IS_ERR(idev))
292564e724f6SBrian Haley 		return PTR_ERR(idev);
29261da177e4SLinus Torvalds 
292719b1518cSDavid Ahern 	if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
292893a714d6SMadhu Challa 		int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
292919b1518cSDavid Ahern 					 true, cfg->pfx, ifindex);
293093a714d6SMadhu Challa 
293193a714d6SMadhu Challa 		if (ret < 0)
293293a714d6SMadhu Challa 			return ret;
293393a714d6SMadhu Challa 	}
293493a714d6SMadhu Challa 
293519b1518cSDavid Ahern 	cfg->scope = ipv6_addr_scope(cfg->pfx);
29361da177e4SLinus Torvalds 
293719b1518cSDavid Ahern 	timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
29384bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
29394bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
294019b1518cSDavid Ahern 		cfg->valid_lft = timeout;
29416f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
29424bed72e4SYOSHIFUJI Hideaki 	} else {
29434bed72e4SYOSHIFUJI Hideaki 		expires = 0;
29444bed72e4SYOSHIFUJI Hideaki 		flags = 0;
294519b1518cSDavid Ahern 		cfg->ifa_flags |= IFA_F_PERMANENT;
29466f704992SYOSHIFUJI Hideaki 	}
29470778769dSNoriaki TAKAMIYA 
294819b1518cSDavid Ahern 	timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
29494bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
29504bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
295119b1518cSDavid Ahern 			cfg->ifa_flags |= IFA_F_DEPRECATED;
295219b1518cSDavid Ahern 		cfg->preferred_lft = timeout;
29534bed72e4SYOSHIFUJI Hideaki 	}
29540778769dSNoriaki TAKAMIYA 
295519b1518cSDavid Ahern 	ifp = ipv6_add_addr(idev, cfg, true, extack);
29561da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
295719b1518cSDavid Ahern 		if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
29588308f3ffSDavid Ahern 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
29598308f3ffSDavid Ahern 					      ifp->rt_priority, dev, expires,
29608308f3ffSDavid Ahern 					      flags, GFP_KERNEL);
2961761aac73SThomas Haller 		}
2962761aac73SThomas Haller 
2963a2d481b3SLorenzo Bianconi 		/* Send a netlink notification if DAD is enabled and
2964a2d481b3SLorenzo Bianconi 		 * optimistic flag is not set
2965a2d481b3SLorenzo Bianconi 		 */
2966a2d481b3SLorenzo Bianconi 		if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
2967a2d481b3SLorenzo Bianconi 			ipv6_ifa_notify(0, ifp);
296895c385b4SNeil Horman 		/*
296995c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
297095c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
297195c385b4SNeil Horman 		 * manually configured addresses
297295c385b4SNeil Horman 		 */
2973cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
297419b1518cSDavid Ahern 		if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
297519b1518cSDavid Ahern 			manage_tempaddrs(idev, ifp, cfg->valid_lft,
297619b1518cSDavid Ahern 					 cfg->preferred_lft, true, jiffies);
29771da177e4SLinus Torvalds 		in6_ifa_put(ifp);
2978c15b1ccaSHannes Frederic Sowa 		addrconf_verify_rtnl();
29791da177e4SLinus Torvalds 		return 0;
298019b1518cSDavid Ahern 	} else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
298119b1518cSDavid Ahern 		ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
298219b1518cSDavid Ahern 			       cfg->pfx, ifindex);
29831da177e4SLinus Torvalds 	}
29841da177e4SLinus Torvalds 
29851da177e4SLinus Torvalds 	return PTR_ERR(ifp);
29861da177e4SLinus Torvalds }
29871da177e4SLinus Torvalds 
29886046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
29896046d5b4SHeiner Kallweit 			  const struct in6_addr *pfx, unsigned int plen)
29901da177e4SLinus Torvalds {
29911da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
29921da177e4SLinus Torvalds 	struct inet6_dev *idev;
29931da177e4SLinus Torvalds 	struct net_device *dev;
29941da177e4SLinus Torvalds 
299524ef0da7SThomas Graf 	if (plen > 128)
299624ef0da7SThomas Graf 		return -EINVAL;
299724ef0da7SThomas Graf 
2998af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2999af284937SDaniel Lezcano 	if (!dev)
30001da177e4SLinus Torvalds 		return -ENODEV;
30011da177e4SLinus Torvalds 
3002e5d08d71SIan Morris 	idev = __in6_dev_get(dev);
300363159f29SIan Morris 	if (!idev)
30041da177e4SLinus Torvalds 		return -ENXIO;
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3007502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
30081da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
30091da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
30101da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
30111da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
30121da177e4SLinus Torvalds 
30136046d5b4SHeiner Kallweit 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
30146046d5b4SHeiner Kallweit 			    (ifa_flags & IFA_F_MANAGETEMPADDR))
30156046d5b4SHeiner Kallweit 				manage_tempaddrs(idev, ifp, 0, 0, false,
30166046d5b4SHeiner Kallweit 						 jiffies);
30171da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
30186046d5b4SHeiner Kallweit 			addrconf_verify_rtnl();
301993a714d6SMadhu Challa 			if (ipv6_addr_is_multicast(pfx)) {
302093a714d6SMadhu Challa 				ipv6_mc_config(net->ipv6.mc_autojoin_sk,
302193a714d6SMadhu Challa 					       false, pfx, dev->ifindex);
302293a714d6SMadhu Challa 			}
30231da177e4SLinus Torvalds 			return 0;
30241da177e4SLinus Torvalds 		}
30251da177e4SLinus Torvalds 	}
30261da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
30271da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
30281da177e4SLinus Torvalds }
30291da177e4SLinus Torvalds 
30301da177e4SLinus Torvalds 
3031af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
30321da177e4SLinus Torvalds {
303319b1518cSDavid Ahern 	struct ifa6_config cfg = {
303419b1518cSDavid Ahern 		.ifa_flags = IFA_F_PERMANENT,
303519b1518cSDavid Ahern 		.preferred_lft = INFINITY_LIFE_TIME,
303619b1518cSDavid Ahern 		.valid_lft = INFINITY_LIFE_TIME,
303719b1518cSDavid Ahern 	};
30381da177e4SLinus Torvalds 	struct in6_ifreq ireq;
30391da177e4SLinus Torvalds 	int err;
30401da177e4SLinus Torvalds 
3041af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
30421da177e4SLinus Torvalds 		return -EPERM;
30431da177e4SLinus Torvalds 
30441da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
30451da177e4SLinus Torvalds 		return -EFAULT;
30461da177e4SLinus Torvalds 
304719b1518cSDavid Ahern 	cfg.pfx = &ireq.ifr6_addr;
304819b1518cSDavid Ahern 	cfg.plen = ireq.ifr6_prefixlen;
304919b1518cSDavid Ahern 
30501da177e4SLinus Torvalds 	rtnl_lock();
305119b1518cSDavid Ahern 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
30521da177e4SLinus Torvalds 	rtnl_unlock();
30531da177e4SLinus Torvalds 	return err;
30541da177e4SLinus Torvalds }
30551da177e4SLinus Torvalds 
3056af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
30571da177e4SLinus Torvalds {
30581da177e4SLinus Torvalds 	struct in6_ifreq ireq;
30591da177e4SLinus Torvalds 	int err;
30601da177e4SLinus Torvalds 
3061af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
30621da177e4SLinus Torvalds 		return -EPERM;
30631da177e4SLinus Torvalds 
30641da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
30651da177e4SLinus Torvalds 		return -EFAULT;
30661da177e4SLinus Torvalds 
30671da177e4SLinus Torvalds 	rtnl_lock();
30686046d5b4SHeiner Kallweit 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
3069af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
30701da177e4SLinus Torvalds 	rtnl_unlock();
30711da177e4SLinus Torvalds 	return err;
30721da177e4SLinus Torvalds }
30731da177e4SLinus Torvalds 
3074b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3075b5f348e5SIlpo Järvinen 		     int plen, int scope)
3076b5f348e5SIlpo Järvinen {
3077b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
3078e6464b8cSDavid Ahern 	struct ifa6_config cfg = {
3079e6464b8cSDavid Ahern 		.pfx = addr,
3080e6464b8cSDavid Ahern 		.plen = plen,
3081e6464b8cSDavid Ahern 		.ifa_flags = IFA_F_PERMANENT,
3082e6464b8cSDavid Ahern 		.valid_lft = INFINITY_LIFE_TIME,
3083e6464b8cSDavid Ahern 		.preferred_lft = INFINITY_LIFE_TIME,
3084e6464b8cSDavid Ahern 		.scope = scope
3085e6464b8cSDavid Ahern 	};
3086b5f348e5SIlpo Järvinen 
3087e6464b8cSDavid Ahern 	ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3088b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
3089b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
3090b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
3091b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
3092764d3be6SPaolo Abeni 		rt_genid_bump_ipv6(dev_net(idev->dev));
3093b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
3094b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
3095b5f348e5SIlpo Järvinen 	}
3096b5f348e5SIlpo Järvinen }
3097b5f348e5SIlpo Järvinen 
309807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
30991da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
31001da177e4SLinus Torvalds {
31011da177e4SLinus Torvalds 	struct in6_addr addr;
31021da177e4SLinus Torvalds 	struct net_device *dev;
3103c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
3104929c9cf3SNicolas Dichtel 	int scope, plen;
3105f0e2acfaSNicolas Dichtel 	u32 pflags = 0;
31061da177e4SLinus Torvalds 
31071da177e4SLinus Torvalds 	ASSERT_RTNL();
31081da177e4SLinus Torvalds 
31091da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
31101da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
31111da177e4SLinus Torvalds 
31121da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
31131da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
31141da177e4SLinus Torvalds 		scope = IFA_LINK;
3115929c9cf3SNicolas Dichtel 		plen = 64;
31161da177e4SLinus Torvalds 	} else {
31171da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
3118929c9cf3SNicolas Dichtel 		plen = 96;
3119f0e2acfaSNicolas Dichtel 		pflags |= RTF_NONEXTHOP;
31201da177e4SLinus Torvalds 	}
31211da177e4SLinus Torvalds 
31221da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
3123929c9cf3SNicolas Dichtel 		add_addr(idev, &addr, plen, scope);
31248308f3ffSDavid Ahern 		addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
3125e500c6d3SCong Wang 				      GFP_KERNEL);
31261da177e4SLinus Torvalds 		return;
31271da177e4SLinus Torvalds 	}
31281da177e4SLinus Torvalds 
31296fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
3130e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
31311da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
31321da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
31331da177e4SLinus Torvalds 			int flag = scope;
31341da177e4SLinus Torvalds 
3135cd5a411dSFlorian Westphal 			in_dev_for_each_ifa_rtnl(ifa, in_dev) {
31361da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
31371da177e4SLinus Torvalds 
31381da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
31391da177e4SLinus Torvalds 					continue;
31401da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
31411da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
31421da177e4SLinus Torvalds 						continue;
31431da177e4SLinus Torvalds 					flag |= IFA_HOST;
31441da177e4SLinus Torvalds 				}
31451da177e4SLinus Torvalds 
3146b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
31478308f3ffSDavid Ahern 				addrconf_prefix_route(&addr, plen, 0, idev->dev,
3148e500c6d3SCong Wang 						      0, pflags, GFP_KERNEL);
31491da177e4SLinus Torvalds 			}
31501da177e4SLinus Torvalds 		}
31511da177e4SLinus Torvalds 	}
31521da177e4SLinus Torvalds }
31530be669bbSJoerg Roedel #endif
31541da177e4SLinus Torvalds 
31551da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
31561da177e4SLinus Torvalds {
31571da177e4SLinus Torvalds 	struct inet6_dev  *idev;
31581da177e4SLinus Torvalds 
31591da177e4SLinus Torvalds 	/* ::1 */
31601da177e4SLinus Torvalds 
31611da177e4SLinus Torvalds 	ASSERT_RTNL();
31621da177e4SLinus Torvalds 
3163e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
3164db0b99f5SSabrina Dubroca 	if (IS_ERR(idev)) {
316591df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
31661da177e4SLinus Torvalds 		return;
31671da177e4SLinus Torvalds 	}
31681da177e4SLinus Torvalds 
3169b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
31701da177e4SLinus Torvalds }
31711da177e4SLinus Torvalds 
31722ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev,
317364236f3fSHannes Frederic Sowa 			    const struct in6_addr *addr, u32 flags)
31741da177e4SLinus Torvalds {
3175e6464b8cSDavid Ahern 	struct ifa6_config cfg = {
3176e6464b8cSDavid Ahern 		.pfx = addr,
3177e6464b8cSDavid Ahern 		.plen = 64,
3178e6464b8cSDavid Ahern 		.ifa_flags = flags | IFA_F_PERMANENT,
3179e6464b8cSDavid Ahern 		.valid_lft = INFINITY_LIFE_TIME,
3180e6464b8cSDavid Ahern 		.preferred_lft = INFINITY_LIFE_TIME,
3181e6464b8cSDavid Ahern 		.scope = IFA_LINK
3182e6464b8cSDavid Ahern 	};
31831da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
31841da177e4SLinus Torvalds 
318595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
318635e015e1SMatteo Croce 	if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
318735e015e1SMatteo Croce 	     idev->cnf.optimistic_dad) &&
3188702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3189e6464b8cSDavid Ahern 		cfg.ifa_flags |= IFA_F_OPTIMISTIC;
319095c385b4SNeil Horman #endif
319195c385b4SNeil Horman 
3192e6464b8cSDavid Ahern 	ifp = ipv6_add_addr(idev, &cfg, true, NULL);
31931da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
31948308f3ffSDavid Ahern 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
3195acb54e3cSDavid Ahern 				      0, 0, GFP_ATOMIC);
3196cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
31971da177e4SLinus Torvalds 		in6_ifa_put(ifp);
31981da177e4SLinus Torvalds 	}
31991da177e4SLinus Torvalds }
32002ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
32011da177e4SLinus Torvalds 
3202622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address)
3203622c81d5SHannes Frederic Sowa {
3204622c81d5SHannes Frederic Sowa 	if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3205622c81d5SHannes Frederic Sowa 		return true;
3206622c81d5SHannes Frederic Sowa 
3207622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0x02005eff) &&
3208622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3209622c81d5SHannes Frederic Sowa 		return true;
3210622c81d5SHannes Frederic Sowa 
3211622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3212622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3213622c81d5SHannes Frederic Sowa 		return true;
3214622c81d5SHannes Frederic Sowa 
3215622c81d5SHannes Frederic Sowa 	return false;
3216622c81d5SHannes Frederic Sowa }
3217622c81d5SHannes Frederic Sowa 
3218622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address,
3219622c81d5SHannes Frederic Sowa 					u8 dad_count,
3220622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev)
3221622c81d5SHannes Frederic Sowa {
3222622c81d5SHannes Frederic Sowa 	static DEFINE_SPINLOCK(lock);
32236b0b0fa2SEric Biggers 	static __u32 digest[SHA1_DIGEST_WORDS];
32246b0b0fa2SEric Biggers 	static __u32 workspace[SHA1_WORKSPACE_WORDS];
3225622c81d5SHannes Frederic Sowa 
3226622c81d5SHannes Frederic Sowa 	static union {
32276b0b0fa2SEric Biggers 		char __data[SHA1_BLOCK_SIZE];
3228622c81d5SHannes Frederic Sowa 		struct {
3229622c81d5SHannes Frederic Sowa 			struct in6_addr secret;
3230ff40217eSHannes Frederic Sowa 			__be32 prefix[2];
3231622c81d5SHannes Frederic Sowa 			unsigned char hwaddr[MAX_ADDR_LEN];
3232622c81d5SHannes Frederic Sowa 			u8 dad_count;
3233622c81d5SHannes Frederic Sowa 		} __packed;
3234622c81d5SHannes Frederic Sowa 	} data;
3235622c81d5SHannes Frederic Sowa 
3236622c81d5SHannes Frederic Sowa 	struct in6_addr secret;
3237622c81d5SHannes Frederic Sowa 	struct in6_addr temp;
3238622c81d5SHannes Frederic Sowa 	struct net *net = dev_net(idev->dev);
3239622c81d5SHannes Frederic Sowa 
3240622c81d5SHannes Frederic Sowa 	BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3241622c81d5SHannes Frederic Sowa 
3242622c81d5SHannes Frederic Sowa 	if (idev->cnf.stable_secret.initialized)
3243622c81d5SHannes Frederic Sowa 		secret = idev->cnf.stable_secret.secret;
3244622c81d5SHannes Frederic Sowa 	else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3245622c81d5SHannes Frederic Sowa 		secret = net->ipv6.devconf_dflt->stable_secret.secret;
3246622c81d5SHannes Frederic Sowa 	else
3247622c81d5SHannes Frederic Sowa 		return -1;
3248622c81d5SHannes Frederic Sowa 
3249622c81d5SHannes Frederic Sowa retry:
3250622c81d5SHannes Frederic Sowa 	spin_lock_bh(&lock);
3251622c81d5SHannes Frederic Sowa 
32526b0b0fa2SEric Biggers 	sha1_init(digest);
3253622c81d5SHannes Frederic Sowa 	memset(&data, 0, sizeof(data));
3254622c81d5SHannes Frederic Sowa 	memset(workspace, 0, sizeof(workspace));
3255622c81d5SHannes Frederic Sowa 	memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3256ff40217eSHannes Frederic Sowa 	data.prefix[0] = address->s6_addr32[0];
3257ff40217eSHannes Frederic Sowa 	data.prefix[1] = address->s6_addr32[1];
3258622c81d5SHannes Frederic Sowa 	data.secret = secret;
3259622c81d5SHannes Frederic Sowa 	data.dad_count = dad_count;
3260622c81d5SHannes Frederic Sowa 
32616b0b0fa2SEric Biggers 	sha1_transform(digest, data.__data, workspace);
3262622c81d5SHannes Frederic Sowa 
3263622c81d5SHannes Frederic Sowa 	temp = *address;
3264ff40217eSHannes Frederic Sowa 	temp.s6_addr32[2] = (__force __be32)digest[0];
3265ff40217eSHannes Frederic Sowa 	temp.s6_addr32[3] = (__force __be32)digest[1];
3266622c81d5SHannes Frederic Sowa 
3267622c81d5SHannes Frederic Sowa 	spin_unlock_bh(&lock);
3268622c81d5SHannes Frederic Sowa 
3269622c81d5SHannes Frederic Sowa 	if (ipv6_reserved_interfaceid(temp)) {
3270622c81d5SHannes Frederic Sowa 		dad_count++;
32711855b7c3SHannes Frederic Sowa 		if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3272622c81d5SHannes Frederic Sowa 			return -1;
3273622c81d5SHannes Frederic Sowa 		goto retry;
3274622c81d5SHannes Frederic Sowa 	}
3275622c81d5SHannes Frederic Sowa 
3276622c81d5SHannes Frederic Sowa 	*address = temp;
3277622c81d5SHannes Frederic Sowa 	return 0;
3278622c81d5SHannes Frederic Sowa }
3279622c81d5SHannes Frederic Sowa 
3280cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3281cc9da6ccSBjørn Mork {
3282cc9da6ccSBjørn Mork 	struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3283cc9da6ccSBjørn Mork 
3284cc9da6ccSBjørn Mork 	if (s->initialized)
3285cc9da6ccSBjørn Mork 		return;
3286cc9da6ccSBjørn Mork 	s = &idev->cnf.stable_secret;
3287cc9da6ccSBjørn Mork 	get_random_bytes(&s->secret, sizeof(s->secret));
3288cc9da6ccSBjørn Mork 	s->initialized = true;
3289cc9da6ccSBjørn Mork }
3290cc9da6ccSBjørn Mork 
3291bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3292bc91b0f0SJiri Pirko {
3293bc91b0f0SJiri Pirko 	struct in6_addr addr;
3294bc91b0f0SJiri Pirko 
3295ca254490SDavid Ahern 	/* no link local addresses on L3 master devices */
3296ca254490SDavid Ahern 	if (netif_is_l3_master(idev->dev))
3297ca254490SDavid Ahern 		return;
3298ca254490SDavid Ahern 
3299744fdc82SJarod Wilson 	/* no link local addresses on devices flagged as slaves */
3300744fdc82SJarod Wilson 	if (idev->dev->flags & IFF_SLAVE)
3301744fdc82SJarod Wilson 		return;
3302744fdc82SJarod Wilson 
3303bc91b0f0SJiri Pirko 	ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3304622c81d5SHannes Frederic Sowa 
3305d35a00b8SFelix Jia 	switch (idev->cnf.addr_gen_mode) {
3306cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_RANDOM:
3307cc9da6ccSBjørn Mork 		ipv6_gen_mode_random_init(idev);
3308a8eceea8SJoe Perches 		fallthrough;
3309cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3310622c81d5SHannes Frederic Sowa 		if (!ipv6_generate_stable_address(&addr, 0, idev))
331164236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr,
331264236f3fSHannes Frederic Sowa 					       IFA_F_STABLE_PRIVACY);
3313622c81d5SHannes Frederic Sowa 		else if (prefix_route)
33148308f3ffSDavid Ahern 			addrconf_prefix_route(&addr, 64, 0, idev->dev,
3315acb54e3cSDavid Ahern 					      0, 0, GFP_KERNEL);
3316cc9da6ccSBjørn Mork 		break;
3317cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_EUI64:
3318bc91b0f0SJiri Pirko 		/* addrconf_add_linklocal also adds a prefix_route and we
3319bc91b0f0SJiri Pirko 		 * only need to care about prefix routes if ipv6_generate_eui64
3320bc91b0f0SJiri Pirko 		 * couldn't generate one.
3321bc91b0f0SJiri Pirko 		 */
3322bc91b0f0SJiri Pirko 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
332364236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr, 0);
3324bc91b0f0SJiri Pirko 		else if (prefix_route)
33258308f3ffSDavid Ahern 			addrconf_prefix_route(&addr, 64, 0, idev->dev,
332627b10608SDavid Ahern 					      0, 0, GFP_KERNEL);
3327cc9da6ccSBjørn Mork 		break;
3328cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_NONE:
3329cc9da6ccSBjørn Mork 	default:
3330cc9da6ccSBjørn Mork 		/* will not add any link local address */
3331cc9da6ccSBjørn Mork 		break;
3332bc91b0f0SJiri Pirko 	}
3333bc91b0f0SJiri Pirko }
3334bc91b0f0SJiri Pirko 
33351da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
33361da177e4SLinus Torvalds {
33371da177e4SLinus Torvalds 	struct inet6_dev *idev;
33381da177e4SLinus Torvalds 
33391da177e4SLinus Torvalds 	ASSERT_RTNL();
33401da177e4SLinus Torvalds 
334174235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
334274235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
334374235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
334406a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
3345e837735eSNicolas Dichtel 	    (dev->type != ARPHRD_IEEE1394) &&
3346e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_TUNNEL6) &&
3347cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_6LOWPAN) &&
334845ce0fd1SFelix Jia 	    (dev->type != ARPHRD_IP6GRE) &&
334945ce0fd1SFelix Jia 	    (dev->type != ARPHRD_IPGRE) &&
335045ce0fd1SFelix Jia 	    (dev->type != ARPHRD_TUNNEL) &&
33519deb441cSSubash Abhinov Kasiviswanathan 	    (dev->type != ARPHRD_NONE) &&
33529deb441cSSubash Abhinov Kasiviswanathan 	    (dev->type != ARPHRD_RAWIP)) {
335374235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
335460380488SHangbin Liu 		idev = __in6_dev_get(dev);
335560380488SHangbin Liu 		if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
335660380488SHangbin Liu 		    dev->flags & IFF_MULTICAST)
335760380488SHangbin Liu 			ipv6_mc_up(idev);
335874235a25SHerbert Xu 		return;
335974235a25SHerbert Xu 	}
336074235a25SHerbert Xu 
33611da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
336264e724f6SBrian Haley 	if (IS_ERR(idev))
33631da177e4SLinus Torvalds 		return;
33641da177e4SLinus Torvalds 
3365cc9da6ccSBjørn Mork 	/* this device type has no EUI support */
3366cc9da6ccSBjørn Mork 	if (dev->type == ARPHRD_NONE &&
3367d35a00b8SFelix Jia 	    idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3368d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3369cc9da6ccSBjørn Mork 
3370bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, false);
33711da177e4SLinus Torvalds }
33721da177e4SLinus Torvalds 
337307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
33741da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
33751da177e4SLinus Torvalds {
33761da177e4SLinus Torvalds 	struct inet6_dev *idev;
33771da177e4SLinus Torvalds 
33781da177e4SLinus Torvalds 	ASSERT_RTNL();
33791da177e4SLinus Torvalds 
33801da177e4SLinus Torvalds 	/*
33811da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
33821da177e4SLinus Torvalds 	 * addresses... we should configure all of
33831da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
33841da177e4SLinus Torvalds 	 */
33851da177e4SLinus Torvalds 
3386e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
3387db0b99f5SSabrina Dubroca 	if (IS_ERR(idev)) {
338891df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
33891da177e4SLinus Torvalds 		return;
33901da177e4SLinus Torvalds 	}
33911da177e4SLinus Torvalds 
3392c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
3393bc91b0f0SJiri Pirko 		addrconf_addr_gen(idev, false);
3394c7dc89c0SFred L. Templin 		return;
3395c7dc89c0SFred L. Templin 	}
3396c7dc89c0SFred L. Templin 
33971da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
33981da177e4SLinus Torvalds 
339962b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
34001da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
34011da177e4SLinus Torvalds }
34020be669bbSJoerg Roedel #endif
34031da177e4SLinus Torvalds 
340407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3405aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
3406aee80b54Sstephen hemminger {
3407aee80b54Sstephen hemminger 	struct inet6_dev *idev;
3408aee80b54Sstephen hemminger 
3409aee80b54Sstephen hemminger 	ASSERT_RTNL();
3410aee80b54Sstephen hemminger 
3411e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
3412db0b99f5SSabrina Dubroca 	if (IS_ERR(idev)) {
341391df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
3414aee80b54Sstephen hemminger 		return;
3415aee80b54Sstephen hemminger 	}
3416aee80b54Sstephen hemminger 
3417bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, true);
3418d9e4ce65SHannes Frederic Sowa 	if (dev->flags & IFF_POINTOPOINT)
3419d9e4ce65SHannes Frederic Sowa 		addrconf_add_mroute(dev);
3420aee80b54Sstephen hemminger }
3421aee80b54Sstephen hemminger #endif
3422aee80b54Sstephen hemminger 
3423afb1d4b5SDavid Ahern static int fixup_permanent_addr(struct net *net,
3424afb1d4b5SDavid Ahern 				struct inet6_dev *idev,
3425f1705ec1SDavid Ahern 				struct inet6_ifaddr *ifp)
3426f1705ec1SDavid Ahern {
342793c2fb25SDavid Ahern 	/* !fib6_node means the host route was removed from the
34288048ced9SDavid Ahern 	 * FIB, for example, if 'lo' device is taken down. In that
34298048ced9SDavid Ahern 	 * case regenerate the host route.
34308048ced9SDavid Ahern 	 */
343193c2fb25SDavid Ahern 	if (!ifp->rt || !ifp->rt->fib6_node) {
3432360a9887SDavid Ahern 		struct fib6_info *f6i, *prev;
3433f1705ec1SDavid Ahern 
3434360a9887SDavid Ahern 		f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3435acb54e3cSDavid Ahern 					 GFP_ATOMIC);
3436360a9887SDavid Ahern 		if (IS_ERR(f6i))
3437360a9887SDavid Ahern 			return PTR_ERR(f6i);
3438f1705ec1SDavid Ahern 
34398048ced9SDavid Ahern 		/* ifp->rt can be accessed outside of rtnl */
34408048ced9SDavid Ahern 		spin_lock(&ifp->lock);
34418048ced9SDavid Ahern 		prev = ifp->rt;
3442360a9887SDavid Ahern 		ifp->rt = f6i;
34438048ced9SDavid Ahern 		spin_unlock(&ifp->lock);
34448048ced9SDavid Ahern 
344593531c67SDavid Ahern 		fib6_info_release(prev);
3446f1705ec1SDavid Ahern 	}
3447f1705ec1SDavid Ahern 
3448f1705ec1SDavid Ahern 	if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3449f1705ec1SDavid Ahern 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
34508308f3ffSDavid Ahern 				      ifp->rt_priority, idev->dev, 0, 0,
34518308f3ffSDavid Ahern 				      GFP_ATOMIC);
3452f1705ec1SDavid Ahern 	}
3453f1705ec1SDavid Ahern 
34546d717134SDavid Ahern 	if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3455f1705ec1SDavid Ahern 		addrconf_dad_start(ifp);
3456f1705ec1SDavid Ahern 
3457f1705ec1SDavid Ahern 	return 0;
3458f1705ec1SDavid Ahern }
3459f1705ec1SDavid Ahern 
3460afb1d4b5SDavid Ahern static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
3461f1705ec1SDavid Ahern {
3462f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifp, *tmp;
3463f1705ec1SDavid Ahern 	struct inet6_dev *idev;
3464f1705ec1SDavid Ahern 
3465f1705ec1SDavid Ahern 	idev = __in6_dev_get(dev);
3466f1705ec1SDavid Ahern 	if (!idev)
3467f1705ec1SDavid Ahern 		return;
3468f1705ec1SDavid Ahern 
3469f1705ec1SDavid Ahern 	write_lock_bh(&idev->lock);
3470f1705ec1SDavid Ahern 
3471f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3472f1705ec1SDavid Ahern 		if ((ifp->flags & IFA_F_PERMANENT) &&
3473afb1d4b5SDavid Ahern 		    fixup_permanent_addr(net, idev, ifp) < 0) {
3474f1705ec1SDavid Ahern 			write_unlock_bh(&idev->lock);
3475e669b869SEric Dumazet 			in6_ifa_hold(ifp);
3476f1705ec1SDavid Ahern 			ipv6_del_addr(ifp);
3477f1705ec1SDavid Ahern 			write_lock_bh(&idev->lock);
3478f1705ec1SDavid Ahern 
3479f1705ec1SDavid Ahern 			net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3480f1705ec1SDavid Ahern 					     idev->dev->name, &ifp->addr);
3481f1705ec1SDavid Ahern 		}
3482f1705ec1SDavid Ahern 	}
3483f1705ec1SDavid Ahern 
3484f1705ec1SDavid Ahern 	write_unlock_bh(&idev->lock);
3485f1705ec1SDavid Ahern }
3486f1705ec1SDavid Ahern 
34871da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
3488351638e7SJiri Pirko 			   void *ptr)
34891da177e4SLinus Torvalds {
3490351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3491896585d4SHangbin Liu 	struct netdev_notifier_change_info *change_info;
34924f7f34eaSDavid Ahern 	struct netdev_notifier_changeupper_info *info;
3493748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
349460abc0beSWANG Cong 	struct net *net = dev_net(dev);
3495c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
3496b217d616SHerbert Xu 	int err;
34971da177e4SLinus Torvalds 
34981da177e4SLinus Torvalds 	switch (event) {
349945ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
350074235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
350145ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
3502a317a2f1SWANG Cong 			if (IS_ERR(idev))
3503a317a2f1SWANG Cong 				return notifier_from_errno(PTR_ERR(idev));
350445ba9dd2SYOSHIFUJI Hideaki 		}
350545ba9dd2SYOSHIFUJI Hideaki 		break;
3506bcdd553fSStephen Hemminger 
3507b7b0b1d2SAlexander Duyck 	case NETDEV_CHANGEMTU:
3508b7b0b1d2SAlexander Duyck 		/* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3509b7b0b1d2SAlexander Duyck 		if (dev->mtu < IPV6_MIN_MTU) {
351060abc0beSWANG Cong 			addrconf_ifdown(dev, dev != net->loopback_dev);
3511b7b0b1d2SAlexander Duyck 			break;
3512b7b0b1d2SAlexander Duyck 		}
3513b7b0b1d2SAlexander Duyck 
3514b7b0b1d2SAlexander Duyck 		if (idev) {
3515b7b0b1d2SAlexander Duyck 			rt6_mtu_change(dev, dev->mtu);
3516b7b0b1d2SAlexander Duyck 			idev->cnf.mtu6 = dev->mtu;
3517b7b0b1d2SAlexander Duyck 			break;
3518b7b0b1d2SAlexander Duyck 		}
3519b7b0b1d2SAlexander Duyck 
3520b7b0b1d2SAlexander Duyck 		/* allocate new idev */
3521b7b0b1d2SAlexander Duyck 		idev = ipv6_add_dev(dev);
3522b7b0b1d2SAlexander Duyck 		if (IS_ERR(idev))
3523b7b0b1d2SAlexander Duyck 			break;
3524b7b0b1d2SAlexander Duyck 
3525b7b0b1d2SAlexander Duyck 		/* device is still not ready */
3526b7b0b1d2SAlexander Duyck 		if (!(idev->if_flags & IF_READY))
3527b7b0b1d2SAlexander Duyck 			break;
3528b7b0b1d2SAlexander Duyck 
3529b7b0b1d2SAlexander Duyck 		run_pending = 1;
3530a8eceea8SJoe Perches 		fallthrough;
35311da177e4SLinus Torvalds 	case NETDEV_UP:
35323c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
3533c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
3534c2edacf8SJay Vosburgh 			break;
3535c2edacf8SJay Vosburgh 
35363ce62a84SWANG Cong 		if (idev && idev->cnf.disable_ipv6)
35373ce62a84SWANG Cong 			break;
35383ce62a84SWANG Cong 
35393c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
354038bd10c4SDavid Ahern 			/* restore routes for permanent addresses */
3541afb1d4b5SDavid Ahern 			addrconf_permanent_addr(net, dev);
354238bd10c4SDavid Ahern 
35431f372c7bSMike Manning 			if (!addrconf_link_ready(dev)) {
35443c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
35457c62b8ddSLubomir Rintel 				pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
35463c21edbdSYOSHIFUJI Hideaki 					 dev->name);
35473c21edbdSYOSHIFUJI Hideaki 				break;
35483c21edbdSYOSHIFUJI Hideaki 			}
354999081049SKristian Slavov 
3550d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
3551d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
3552d31c7b8fSEvgeniy Polyakov 
3553a317a2f1SWANG Cong 			if (!IS_ERR_OR_NULL(idev)) {
355499081049SKristian Slavov 				idev->if_flags |= IF_READY;
3555e3ec6cfcSBenjamin Thery 				run_pending = 1;
3556e3ec6cfcSBenjamin Thery 			}
3557b7b0b1d2SAlexander Duyck 		} else if (event == NETDEV_CHANGE) {
35581f372c7bSMike Manning 			if (!addrconf_link_ready(dev)) {
35593c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
356027c6fa73SIdo Schimmel 				rt6_sync_down_dev(dev, event);
35613c21edbdSYOSHIFUJI Hideaki 				break;
35623c21edbdSYOSHIFUJI Hideaki 			}
35633c21edbdSYOSHIFUJI Hideaki 
3564896585d4SHangbin Liu 			if (!IS_ERR_OR_NULL(idev)) {
3565a088d1d7SLinus Lüssing 				if (idev->if_flags & IF_READY) {
3566a088d1d7SLinus Lüssing 					/* device is already configured -
3567a088d1d7SLinus Lüssing 					 * but resend MLD reports, we might
3568a088d1d7SLinus Lüssing 					 * have roamed and need to update
3569a088d1d7SLinus Lüssing 					 * multicast snooping switches
3570a088d1d7SLinus Lüssing 					 */
3571a088d1d7SLinus Lüssing 					ipv6_mc_up(idev);
3572896585d4SHangbin Liu 					change_info = ptr;
3573896585d4SHangbin Liu 					if (change_info->flags_changed & IFF_NOARP)
3574896585d4SHangbin Liu 						addrconf_dad_run(idev, true);
357527c6fa73SIdo Schimmel 					rt6_sync_up(dev, RTNH_F_LINKDOWN);
35763c21edbdSYOSHIFUJI Hideaki 					break;
3577a088d1d7SLinus Lüssing 				}
35783c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
35793c21edbdSYOSHIFUJI Hideaki 			}
35803c21edbdSYOSHIFUJI Hideaki 
3581f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
35823c21edbdSYOSHIFUJI Hideaki 				dev->name);
35833c21edbdSYOSHIFUJI Hideaki 
3584c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
35853c21edbdSYOSHIFUJI Hideaki 		}
35863c21edbdSYOSHIFUJI Hideaki 
35871da177e4SLinus Torvalds 		switch (dev->type) {
358807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
35891da177e4SLinus Torvalds 		case ARPHRD_SIT:
35901da177e4SLinus Torvalds 			addrconf_sit_config(dev);
35911da177e4SLinus Torvalds 			break;
35920be669bbSJoerg Roedel #endif
359307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3594aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
3595aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
3596aee80b54Sstephen hemminger 			break;
3597aee80b54Sstephen hemminger #endif
35981da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
35991da177e4SLinus Torvalds 			init_loopback(dev);
36001da177e4SLinus Torvalds 			break;
36011da177e4SLinus Torvalds 
36021da177e4SLinus Torvalds 		default:
36031da177e4SLinus Torvalds 			addrconf_dev_config(dev);
36041da177e4SLinus Torvalds 			break;
36053ff50b79SStephen Hemminger 		}
3606bcdd553fSStephen Hemminger 
3607a317a2f1SWANG Cong 		if (!IS_ERR_OR_NULL(idev)) {
3608c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
3609896585d4SHangbin Liu 				addrconf_dad_run(idev, false);
3610c5e33bddSYOSHIFUJI Hideaki 
36112127d95aSIdo Schimmel 			/* Device has an address by now */
36122127d95aSIdo Schimmel 			rt6_sync_up(dev, RTNH_F_DEAD);
36132127d95aSIdo Schimmel 
3614bcdd553fSStephen Hemminger 			/*
3615bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
3616bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
3617bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
36181da177e4SLinus Torvalds 			 */
3619bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
3620bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
36211da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
36221da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
36231da177e4SLinus Torvalds 			}
36241da177e4SLinus Torvalds 			idev->tstamp = jiffies;
36251da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
3626bcdd553fSStephen Hemminger 
3627bcdd553fSStephen Hemminger 			/*
3628bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
3629bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
36301da177e4SLinus Torvalds 			 */
36311da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
363260abc0beSWANG Cong 				addrconf_ifdown(dev, dev != net->loopback_dev);
36331da177e4SLinus Torvalds 		}
36341da177e4SLinus Torvalds 		break;
36351da177e4SLinus Torvalds 
36361da177e4SLinus Torvalds 	case NETDEV_DOWN:
36371da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
36381da177e4SLinus Torvalds 		/*
36391da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
36401da177e4SLinus Torvalds 		 */
36411da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
36421da177e4SLinus Torvalds 		break;
36433c21edbdSYOSHIFUJI Hideaki 
36441da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
36451da177e4SLinus Torvalds 		if (idev) {
36465632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
3647408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
3648a317a2f1SWANG Cong 			err = addrconf_sysctl_register(idev);
3649b217d616SHerbert Xu 			if (err)
3650b217d616SHerbert Xu 				return notifier_from_errno(err);
3651a317a2f1SWANG Cong 			err = snmp6_register_dev(idev);
3652a317a2f1SWANG Cong 			if (err) {
3653a317a2f1SWANG Cong 				addrconf_sysctl_unregister(idev);
3654a317a2f1SWANG Cong 				return notifier_from_errno(err);
3655a317a2f1SWANG Cong 			}
36565632c515SStephen Hemminger 		}
36571da177e4SLinus Torvalds 		break;
3658bcdd553fSStephen Hemminger 
365993d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
366093d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
36613ef0952cSAndrew Lunn 		if (idev)
366293d9b7d7SJiri Pirko 			addrconf_type_change(dev, event);
366375c78500SMoni Shoua 		break;
36644f7f34eaSDavid Ahern 
36654f7f34eaSDavid Ahern 	case NETDEV_CHANGEUPPER:
36664f7f34eaSDavid Ahern 		info = ptr;
36674f7f34eaSDavid Ahern 
36684f7f34eaSDavid Ahern 		/* flush all routes if dev is linked to or unlinked from
36694f7f34eaSDavid Ahern 		 * an L3 master device (e.g., VRF)
36704f7f34eaSDavid Ahern 		 */
36714f7f34eaSDavid Ahern 		if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3672ae79dbf6SFlorent Fourcot 			addrconf_ifdown(dev, false);
36733ff50b79SStephen Hemminger 	}
36741da177e4SLinus Torvalds 
36751da177e4SLinus Torvalds 	return NOTIFY_OK;
36761da177e4SLinus Torvalds }
36771da177e4SLinus Torvalds 
36781da177e4SLinus Torvalds /*
36791da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
36801da177e4SLinus Torvalds  */
36811da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
36821da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
3683242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY,
36841da177e4SLinus Torvalds };
36851da177e4SLinus Torvalds 
368693d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
368775c78500SMoni Shoua {
368875c78500SMoni Shoua 	struct inet6_dev *idev;
368975c78500SMoni Shoua 	ASSERT_RTNL();
369075c78500SMoni Shoua 
369175c78500SMoni Shoua 	idev = __in6_dev_get(dev);
369275c78500SMoni Shoua 
369393d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
369475c78500SMoni Shoua 		ipv6_mc_remap(idev);
369593d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
369675c78500SMoni Shoua 		ipv6_mc_unmap(idev);
369775c78500SMoni Shoua }
369875c78500SMoni Shoua 
369970af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr)
370070af921dSDavid Ahern {
370170af921dSDavid Ahern 	return ipv6_addr_type(addr) &
370270af921dSDavid Ahern 		(IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
370370af921dSDavid Ahern }
370470af921dSDavid Ahern 
3705ae79dbf6SFlorent Fourcot static int addrconf_ifdown(struct net_device *dev, bool unregister)
37061da177e4SLinus Torvalds {
3707ae79dbf6SFlorent Fourcot 	unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN;
3708c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
3709502a2ffdSstephen hemminger 	struct inet6_dev *idev;
3710f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifa, *tmp;
37110aef78aaSIvan Vecera 	bool keep_addr = false;
371273a8bd74SDavid S. Miller 	int state, i;
37131da177e4SLinus Torvalds 
37141da177e4SLinus Torvalds 	ASSERT_RTNL();
37151da177e4SLinus Torvalds 
37164c981e28SIdo Schimmel 	rt6_disable_ip(dev, event);
37171da177e4SLinus Torvalds 
37181da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
371963159f29SIan Morris 	if (!idev)
37201da177e4SLinus Torvalds 		return -ENODEV;
37211da177e4SLinus Torvalds 
3722bcdd553fSStephen Hemminger 	/*
3723bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3724bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
37251da177e4SLinus Torvalds 	 */
3726ae79dbf6SFlorent Fourcot 	if (unregister) {
37271da177e4SLinus Torvalds 		idev->dead = 1;
37288814c4b5SYOSHIFUJI Hideaki 
37298814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3730a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
37311da177e4SLinus Torvalds 
37321da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
37331da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
37341da177e4SLinus Torvalds 
37351da177e4SLinus Torvalds 	}
37361da177e4SLinus Torvalds 
3737f1705ec1SDavid Ahern 	/* combine the user config with event to determine if permanent
3738f1705ec1SDavid Ahern 	 * addresses are to be removed from address hash table
3739f1705ec1SDavid Ahern 	 */
3740ae79dbf6SFlorent Fourcot 	if (!unregister && !idev->cnf.disable_ipv6) {
37410aef78aaSIvan Vecera 		/* aggregate the system setting and interface setting */
37420aef78aaSIvan Vecera 		int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
37430aef78aaSIvan Vecera 
37440aef78aaSIvan Vecera 		if (!_keep_addr)
37450aef78aaSIvan Vecera 			_keep_addr = idev->cnf.keep_addr_on_down;
37460aef78aaSIvan Vecera 
37470aef78aaSIvan Vecera 		keep_addr = (_keep_addr > 0);
37480aef78aaSIvan Vecera 	}
3749f1705ec1SDavid Ahern 
375073a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
375173a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
375273a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
375373a8bd74SDavid S. Miller 
375473a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
375573a8bd74SDavid S. Miller restart:
3756b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
375773a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
3758c15b1ccaSHannes Frederic Sowa 				addrconf_del_dad_work(ifa);
3759f1705ec1SDavid Ahern 				/* combined flag + permanent flag decide if
3760f1705ec1SDavid Ahern 				 * address is retained on a down event
3761f1705ec1SDavid Ahern 				 */
3762f1705ec1SDavid Ahern 				if (!keep_addr ||
376370af921dSDavid Ahern 				    !(ifa->flags & IFA_F_PERMANENT) ||
376470af921dSDavid Ahern 				    addr_is_local(&ifa->addr)) {
3765f1705ec1SDavid Ahern 					hlist_del_init_rcu(&ifa->addr_lst);
376673a8bd74SDavid S. Miller 					goto restart;
376773a8bd74SDavid S. Miller 				}
376873a8bd74SDavid S. Miller 			}
3769f1705ec1SDavid Ahern 		}
377073a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
377173a8bd74SDavid S. Miller 	}
377273a8bd74SDavid S. Miller 
37731da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
37741da177e4SLinus Torvalds 
3775b7b1bfceSHannes Frederic Sowa 	addrconf_del_rs_timer(idev);
3776b7b1bfceSHannes Frederic Sowa 
3777bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3778ae79dbf6SFlorent Fourcot 	if (!unregister)
37793c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
37801da177e4SLinus Torvalds 
3781bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3782372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3783372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3784372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3785372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
37861da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
37871da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
37881da177e4SLinus Torvalds 
37891da177e4SLinus Torvalds 		if (ifa->ifpub) {
37901da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
37911da177e4SLinus Torvalds 			ifa->ifpub = NULL;
37921da177e4SLinus Torvalds 		}
37931da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
37941da177e4SLinus Torvalds 		in6_ifa_put(ifa);
37951da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
37961da177e4SLinus Torvalds 	}
37978f37ada5Sstephen hemminger 
3798f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
37998d1c802bSDavid Ahern 		struct fib6_info *rt = NULL;
3800a2d6cbb0SRabin Vincent 		bool keep;
380138bd10c4SDavid Ahern 
3802c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifa);
380384e8b803Sstephen hemminger 
3804a2d6cbb0SRabin Vincent 		keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3805a2d6cbb0SRabin Vincent 			!addr_is_local(&ifa->addr);
3806a2d6cbb0SRabin Vincent 
38074c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
38088e8e676dSHannes Frederic Sowa 		spin_lock_bh(&ifa->lock);
3809f1705ec1SDavid Ahern 
3810a2d6cbb0SRabin Vincent 		if (keep) {
3811f1705ec1SDavid Ahern 			/* set state to skip the notifier below */
3812f1705ec1SDavid Ahern 			state = INET6_IFADDR_STATE_DEAD;
38136d717134SDavid Ahern 			ifa->state = INET6_IFADDR_STATE_PREDAD;
3814f1705ec1SDavid Ahern 			if (!(ifa->flags & IFA_F_NODAD))
3815f1705ec1SDavid Ahern 				ifa->flags |= IFA_F_TENTATIVE;
381638bd10c4SDavid Ahern 
381738bd10c4SDavid Ahern 			rt = ifa->rt;
381838bd10c4SDavid Ahern 			ifa->rt = NULL;
3819f1705ec1SDavid Ahern 		} else {
38204c5ff6a6SHerbert Xu 			state = ifa->state;
38214c5ff6a6SHerbert Xu 			ifa->state = INET6_IFADDR_STATE_DEAD;
3822f1705ec1SDavid Ahern 		}
3823f1705ec1SDavid Ahern 
38248e8e676dSHannes Frederic Sowa 		spin_unlock_bh(&ifa->lock);
38254c5ff6a6SHerbert Xu 
382638bd10c4SDavid Ahern 		if (rt)
382711dd74b3SRoopa Prabhu 			ip6_del_rt(net, rt, false);
382838bd10c4SDavid Ahern 
38299d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
38301da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
3831f88c91ddSCong Wang 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3832ea06f717SMike Manning 		} else {
3833ea06f717SMike Manning 			if (idev->cnf.forwarding)
3834ea06f717SMike Manning 				addrconf_leave_anycast(ifa);
3835ea06f717SMike Manning 			addrconf_leave_solict(ifa->idev, &ifa->addr);
38362de79570SLorenzo Colitti 		}
383773a8bd74SDavid S. Miller 
38381da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
38398ef802aaSEric Dumazet 		if (!keep) {
38408ef802aaSEric Dumazet 			list_del_rcu(&ifa->if_list);
38418ef802aaSEric Dumazet 			in6_ifa_put(ifa);
38428ef802aaSEric Dumazet 		}
38431da177e4SLinus Torvalds 	}
38448f37ada5Sstephen hemminger 
38451da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
38461da177e4SLinus Torvalds 
3847381f4dcaSSabrina Dubroca 	/* Step 5: Discard anycast and multicast list */
3848ae79dbf6SFlorent Fourcot 	if (unregister) {
3849381f4dcaSSabrina Dubroca 		ipv6_ac_destroy_dev(idev);
38501da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
3851381f4dcaSSabrina Dubroca 	} else {
38521da177e4SLinus Torvalds 		ipv6_mc_down(idev);
3853381f4dcaSSabrina Dubroca 	}
38541da177e4SLinus Torvalds 
38551da177e4SLinus Torvalds 	idev->tstamp = jiffies;
38561da177e4SLinus Torvalds 
3857bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3858ae79dbf6SFlorent Fourcot 	if (unregister) {
3859408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
38601da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
38611da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
38621da177e4SLinus Torvalds 		in6_dev_put(idev);
38631da177e4SLinus Torvalds 	}
38641da177e4SLinus Torvalds 	return 0;
38651da177e4SLinus Torvalds }
38661da177e4SLinus Torvalds 
3867e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t)
38681da177e4SLinus Torvalds {
3869e99e88a9SKees Cook 	struct inet6_dev *idev = from_timer(idev, t, rs_timer);
3870caf92bc4SCong Wang 	struct net_device *dev = idev->dev;
3871b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
38721da177e4SLinus Torvalds 
3873b7b1bfceSHannes Frederic Sowa 	write_lock(&idev->lock);
38745b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
38751da177e4SLinus Torvalds 		goto out;
38761da177e4SLinus Torvalds 
38779ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
38781da177e4SLinus Torvalds 		goto out;
38795b2a1953Sstephen hemminger 
38805b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
38815b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
38825b2a1953Sstephen hemminger 		goto out;
38831da177e4SLinus Torvalds 
3884bd11f074SMaciej Żenczykowski 	if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3885caf92bc4SCong Wang 		write_unlock(&idev->lock);
3886caf92bc4SCong Wang 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3887caf92bc4SCong Wang 			ndisc_send_rs(dev, &lladdr,
3888b7b1bfceSHannes Frederic Sowa 				      &in6addr_linklocal_allrouters);
3889b7b1bfceSHannes Frederic Sowa 		else
3890caf92bc4SCong Wang 			goto put;
3891b7b1bfceSHannes Frederic Sowa 
3892caf92bc4SCong Wang 		write_lock(&idev->lock);
3893bd11f074SMaciej Żenczykowski 		idev->rs_interval = rfc3315_s14_backoff_update(
3894bd11f074SMaciej Żenczykowski 			idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
38951da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
3896b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3897b7b1bfceSHannes Frederic Sowa 					     idev->cnf.rtr_solicits) ?
38985b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_delay :
3899bd11f074SMaciej Żenczykowski 				      idev->rs_interval);
39001da177e4SLinus Torvalds 	} else {
39011da177e4SLinus Torvalds 		/*
39021da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
39031da177e4SLinus Torvalds 		 * assumption any longer.
39041da177e4SLinus Torvalds 		 */
390591df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
39061da177e4SLinus Torvalds 	}
39071da177e4SLinus Torvalds 
39081da177e4SLinus Torvalds out:
3909b7b1bfceSHannes Frederic Sowa 	write_unlock(&idev->lock);
3910caf92bc4SCong Wang put:
3911b7b1bfceSHannes Frederic Sowa 	in6_dev_put(idev);
39121da177e4SLinus Torvalds }
39131da177e4SLinus Torvalds 
39141da177e4SLinus Torvalds /*
39151da177e4SLinus Torvalds  *	Duplicate Address Detection
39161da177e4SLinus Torvalds  */
39173c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
39183c21edbdSYOSHIFUJI Hideaki {
39193c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
39203c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
3921adc176c5SErik Nordmark 	u64 nonce;
39223c21edbdSYOSHIFUJI Hideaki 
392395c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
392495c385b4SNeil Horman 		rand_num = 0;
392595c385b4SNeil Horman 	else
392663862b5bSAruna-Hewapathirane 		rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
392795c385b4SNeil Horman 
3928adc176c5SErik Nordmark 	nonce = 0;
3929adc176c5SErik Nordmark 	if (idev->cnf.enhanced_dad ||
3930adc176c5SErik Nordmark 	    dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3931adc176c5SErik Nordmark 		do
3932adc176c5SErik Nordmark 			get_random_bytes(&nonce, 6);
3933adc176c5SErik Nordmark 		while (nonce == 0);
3934adc176c5SErik Nordmark 	}
3935adc176c5SErik Nordmark 	ifp->dad_nonce = nonce;
3936b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes = idev->cnf.dad_transmits;
3937c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, rand_num);
39383c21edbdSYOSHIFUJI Hideaki }
39393c21edbdSYOSHIFUJI Hideaki 
3940c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
39411da177e4SLinus Torvalds {
39421da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
39431da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3944764d3be6SPaolo Abeni 	bool bump_id, notify = false;
3945afb1d4b5SDavid Ahern 	struct net *net;
39461da177e4SLinus Torvalds 
39471da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
39481da177e4SLinus Torvalds 
394963862b5bSAruna-Hewapathirane 	prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
39501da177e4SLinus Torvalds 
39511da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3952622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3953e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
39541da177e4SLinus Torvalds 		goto out;
39551da177e4SLinus Torvalds 
3956afb1d4b5SDavid Ahern 	net = dev_net(dev);
39571da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3958afb1d4b5SDavid Ahern 	    (net->ipv6.devconf_all->accept_dad < 1 &&
3959a2d3f3e3SMatteo Croce 	     idev->cnf.accept_dad < 1) ||
396055ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
396155ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3962c76fe2d9SDavid Ahern 		bool send_na = false;
3963c76fe2d9SDavid Ahern 
3964c76fe2d9SDavid Ahern 		if (ifp->flags & IFA_F_TENTATIVE &&
3965c76fe2d9SDavid Ahern 		    !(ifp->flags & IFA_F_OPTIMISTIC))
3966c76fe2d9SDavid Ahern 			send_na = true;
3967764d3be6SPaolo Abeni 		bump_id = ifp->flags & IFA_F_TENTATIVE;
3968cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
396921809fafSstephen hemminger 		spin_unlock(&ifp->lock);
39701da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
39711da177e4SLinus Torvalds 
3972c76fe2d9SDavid Ahern 		addrconf_dad_completed(ifp, bump_id, send_na);
39731da177e4SLinus Torvalds 		return;
39741da177e4SLinus Torvalds 	}
39751da177e4SLinus Torvalds 
39766732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
397721809fafSstephen hemminger 		spin_unlock(&ifp->lock);
39786732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
39793c21edbdSYOSHIFUJI Hideaki 		/*
3980590a9887SMasatake YAMATO 		 * If the device is not ready:
39813c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
39823c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
39833c21edbdSYOSHIFUJI Hideaki 		 */
39843c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
3985cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
39866732badeSYOSHIFUJI Hideaki 		return;
39873c21edbdSYOSHIFUJI Hideaki 	}
398895c385b4SNeil Horman 
398995c385b4SNeil Horman 	/*
399095c385b4SNeil Horman 	 * Optimistic nodes can start receiving
399195c385b4SNeil Horman 	 * Frames right away
399295c385b4SNeil Horman 	 */
39937fd2561eSErik Kline 	if (ifp->flags & IFA_F_OPTIMISTIC) {
3994afb1d4b5SDavid Ahern 		ip6_ins_rt(net, ifp->rt);
3995afb1d4b5SDavid Ahern 		if (ipv6_use_optimistic_addr(net, idev)) {
39967fd2561eSErik Kline 			/* Because optimistic nodes can use this address,
39977fd2561eSErik Kline 			 * notify listeners. If DAD fails, RTM_DELADDR is sent.
39987fd2561eSErik Kline 			 */
399916186a82Ssubashab@codeaurora.org 			notify = true;
40007fd2561eSErik Kline 		}
40017fd2561eSErik Kline 	}
400295c385b4SNeil Horman 
40036732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
40041da177e4SLinus Torvalds out:
4005622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
40061da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
400716186a82Ssubashab@codeaurora.org 	if (notify)
400816186a82Ssubashab@codeaurora.org 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
40091da177e4SLinus Torvalds }
40101da177e4SLinus Torvalds 
4011c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp)
40121da177e4SLinus Torvalds {
4013c15b1ccaSHannes Frederic Sowa 	bool begin_dad = false;
4014c15b1ccaSHannes Frederic Sowa 
40158e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
4016c15b1ccaSHannes Frederic Sowa 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4017c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_PREDAD;
4018c15b1ccaSHannes Frederic Sowa 		begin_dad = true;
4019c15b1ccaSHannes Frederic Sowa 	}
40208e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
4021c15b1ccaSHannes Frederic Sowa 
4022c15b1ccaSHannes Frederic Sowa 	if (begin_dad)
4023c15b1ccaSHannes Frederic Sowa 		addrconf_mod_dad_work(ifp, 0);
4024c15b1ccaSHannes Frederic Sowa }
4025c15b1ccaSHannes Frederic Sowa 
4026c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w)
4027c15b1ccaSHannes Frederic Sowa {
4028c15b1ccaSHannes Frederic Sowa 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4029c15b1ccaSHannes Frederic Sowa 						struct inet6_ifaddr,
4030c15b1ccaSHannes Frederic Sowa 						dad_work);
40311da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
4032764d3be6SPaolo Abeni 	bool bump_id, disable_ipv6 = false;
40331da177e4SLinus Torvalds 	struct in6_addr mcaddr;
40341da177e4SLinus Torvalds 
4035c15b1ccaSHannes Frederic Sowa 	enum {
4036c15b1ccaSHannes Frederic Sowa 		DAD_PROCESS,
4037c15b1ccaSHannes Frederic Sowa 		DAD_BEGIN,
4038c15b1ccaSHannes Frederic Sowa 		DAD_ABORT,
4039c15b1ccaSHannes Frederic Sowa 	} action = DAD_PROCESS;
4040c15b1ccaSHannes Frederic Sowa 
4041c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
4042c15b1ccaSHannes Frederic Sowa 
40438e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
4044c15b1ccaSHannes Frederic Sowa 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4045c15b1ccaSHannes Frederic Sowa 		action = DAD_BEGIN;
4046c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_DAD;
4047c15b1ccaSHannes Frederic Sowa 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4048c15b1ccaSHannes Frederic Sowa 		action = DAD_ABORT;
4049c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
405085b51b12SMike Manning 
405135e015e1SMatteo Croce 		if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
405235e015e1SMatteo Croce 		     idev->cnf.accept_dad > 1) &&
405335e015e1SMatteo Croce 		    !idev->cnf.disable_ipv6 &&
405485b51b12SMike Manning 		    !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
405585b51b12SMike Manning 			struct in6_addr addr;
405685b51b12SMike Manning 
405785b51b12SMike Manning 			addr.s6_addr32[0] = htonl(0xfe800000);
405885b51b12SMike Manning 			addr.s6_addr32[1] = 0;
405985b51b12SMike Manning 
406085b51b12SMike Manning 			if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
406185b51b12SMike Manning 			    ipv6_addr_equal(&ifp->addr, &addr)) {
406285b51b12SMike Manning 				/* DAD failed for link-local based on MAC */
406385b51b12SMike Manning 				idev->cnf.disable_ipv6 = 1;
406485b51b12SMike Manning 
406585b51b12SMike Manning 				pr_info("%s: IPv6 being disabled!\n",
406685b51b12SMike Manning 					ifp->idev->dev->name);
406785b51b12SMike Manning 				disable_ipv6 = true;
406885b51b12SMike Manning 			}
406985b51b12SMike Manning 		}
4070c15b1ccaSHannes Frederic Sowa 	}
40718e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
4072c15b1ccaSHannes Frederic Sowa 
4073c15b1ccaSHannes Frederic Sowa 	if (action == DAD_BEGIN) {
4074c15b1ccaSHannes Frederic Sowa 		addrconf_dad_begin(ifp);
4075c15b1ccaSHannes Frederic Sowa 		goto out;
4076c15b1ccaSHannes Frederic Sowa 	} else if (action == DAD_ABORT) {
4077751eb6b6SWei Yongjun 		in6_ifa_hold(ifp);
4078c15b1ccaSHannes Frederic Sowa 		addrconf_dad_stop(ifp, 1);
407985b51b12SMike Manning 		if (disable_ipv6)
4080ae79dbf6SFlorent Fourcot 			addrconf_ifdown(idev->dev, false);
4081c15b1ccaSHannes Frederic Sowa 		goto out;
4082c15b1ccaSHannes Frederic Sowa 	}
4083c15b1ccaSHannes Frederic Sowa 
4084b7b1bfceSHannes Frederic Sowa 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
4085f2344a13SHerbert Xu 		goto out;
4086f2344a13SHerbert Xu 
4087c15b1ccaSHannes Frederic Sowa 	write_lock_bh(&idev->lock);
4088122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
4089c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
40901da177e4SLinus Torvalds 		goto out;
40911da177e4SLinus Torvalds 	}
409221809fafSstephen hemminger 
409321809fafSstephen hemminger 	spin_lock(&ifp->lock);
4094622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4095622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
4096c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
4097622ccdf1SHerbert Xu 		goto out;
4098622ccdf1SHerbert Xu 	}
4099622ccdf1SHerbert Xu 
4100b7b1bfceSHannes Frederic Sowa 	if (ifp->dad_probes == 0) {
4101c76fe2d9SDavid Ahern 		bool send_na = false;
4102c76fe2d9SDavid Ahern 
41031da177e4SLinus Torvalds 		/*
41041da177e4SLinus Torvalds 		 * DAD was successful
41051da177e4SLinus Torvalds 		 */
41061da177e4SLinus Torvalds 
4107c76fe2d9SDavid Ahern 		if (ifp->flags & IFA_F_TENTATIVE &&
4108c76fe2d9SDavid Ahern 		    !(ifp->flags & IFA_F_OPTIMISTIC))
4109c76fe2d9SDavid Ahern 			send_na = true;
4110764d3be6SPaolo Abeni 		bump_id = ifp->flags & IFA_F_TENTATIVE;
4111cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
411221809fafSstephen hemminger 		spin_unlock(&ifp->lock);
4113c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
41141da177e4SLinus Torvalds 
4115c76fe2d9SDavid Ahern 		addrconf_dad_completed(ifp, bump_id, send_na);
41161da177e4SLinus Torvalds 
41171da177e4SLinus Torvalds 		goto out;
41181da177e4SLinus Torvalds 	}
41191da177e4SLinus Torvalds 
4120b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes--;
4121c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp,
412219e16d22SHangbin Liu 			      max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME),
412319e16d22SHangbin Liu 				  HZ/100));
412421809fafSstephen hemminger 	spin_unlock(&ifp->lock);
4125c15b1ccaSHannes Frederic Sowa 	write_unlock_bh(&idev->lock);
41261da177e4SLinus Torvalds 
41271da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
41281da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
4129adc176c5SErik Nordmark 	ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4130adc176c5SErik Nordmark 		      ifp->dad_nonce);
41311da177e4SLinus Torvalds out:
41321da177e4SLinus Torvalds 	in6_ifa_put(ifp);
4133c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
41341da177e4SLinus Torvalds }
41351da177e4SLinus Torvalds 
413611ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */
413711ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
413811ffff75SHannes Frederic Sowa {
413911ffff75SHannes Frederic Sowa 	struct inet6_ifaddr *ifpiter;
414011ffff75SHannes Frederic Sowa 	struct inet6_dev *idev = ifp->idev;
414111ffff75SHannes Frederic Sowa 
4142602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4143602582caSHannes Frederic Sowa 		if (ifpiter->scope > IFA_LINK)
4144602582caSHannes Frederic Sowa 			break;
414511ffff75SHannes Frederic Sowa 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
414611ffff75SHannes Frederic Sowa 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
414711ffff75SHannes Frederic Sowa 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
414811ffff75SHannes Frederic Sowa 		    IFA_F_PERMANENT)
414911ffff75SHannes Frederic Sowa 			return false;
415011ffff75SHannes Frederic Sowa 	}
415111ffff75SHannes Frederic Sowa 	return true;
415211ffff75SHannes Frederic Sowa }
415311ffff75SHannes Frederic Sowa 
4154c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4155c76fe2d9SDavid Ahern 				   bool send_na)
41561da177e4SLinus Torvalds {
41571da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
4158b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
4159b173ee48SHannes Frederic Sowa 	bool send_rs, send_mld;
4160b7b1bfceSHannes Frederic Sowa 
4161c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
41621da177e4SLinus Torvalds 
41631da177e4SLinus Torvalds 	/*
41641da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
41651da177e4SLinus Torvalds 	 */
41661da177e4SLinus Torvalds 
41671da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
41681da177e4SLinus Torvalds 
4169026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
4170026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
41711da177e4SLinus Torvalds 	 */
41721da177e4SLinus Torvalds 
41731ec047ebSHannes Frederic Sowa 	read_lock_bh(&ifp->idev->lock);
417411ffff75SHannes Frederic Sowa 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4175b173ee48SHannes Frederic Sowa 	send_rs = send_mld &&
4176b173ee48SHannes Frederic Sowa 		  ipv6_accept_ra(ifp->idev) &&
4177bd11f074SMaciej Żenczykowski 		  ifp->idev->cnf.rtr_solicits != 0 &&
4178b173ee48SHannes Frederic Sowa 		  (dev->flags&IFF_LOOPBACK) == 0;
41791ec047ebSHannes Frederic Sowa 	read_unlock_bh(&ifp->idev->lock);
41801ec047ebSHannes Frederic Sowa 
4181b173ee48SHannes Frederic Sowa 	/* While dad is in progress mld report's source address is in6_addrany.
4182b173ee48SHannes Frederic Sowa 	 * Resend with proper ll now.
4183b173ee48SHannes Frederic Sowa 	 */
4184b173ee48SHannes Frederic Sowa 	if (send_mld)
4185b173ee48SHannes Frederic Sowa 		ipv6_mc_dad_complete(ifp->idev);
4186b173ee48SHannes Frederic Sowa 
4187c76fe2d9SDavid Ahern 	/* send unsolicited NA if enabled */
4188c76fe2d9SDavid Ahern 	if (send_na &&
4189c76fe2d9SDavid Ahern 	    (ifp->idev->cnf.ndisc_notify ||
4190c76fe2d9SDavid Ahern 	     dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
4191c76fe2d9SDavid Ahern 		ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4192c76fe2d9SDavid Ahern 			      /*router=*/ !!ifp->idev->cnf.forwarding,
4193c76fe2d9SDavid Ahern 			      /*solicited=*/ false, /*override=*/ true,
4194c76fe2d9SDavid Ahern 			      /*inc_opt=*/ true);
4195c76fe2d9SDavid Ahern 	}
4196c76fe2d9SDavid Ahern 
41971ec047ebSHannes Frederic Sowa 	if (send_rs) {
41981da177e4SLinus Torvalds 		/*
41991da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
42001da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
42011da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
42021da177e4SLinus Torvalds 		 */
42031ec047ebSHannes Frederic Sowa 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4204b7b1bfceSHannes Frederic Sowa 			return;
42051ec047ebSHannes Frederic Sowa 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
42061da177e4SLinus Torvalds 
4207b7b1bfceSHannes Frederic Sowa 		write_lock_bh(&ifp->idev->lock);
4208b7b1bfceSHannes Frederic Sowa 		spin_lock(&ifp->lock);
4209bd11f074SMaciej Żenczykowski 		ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4210bd11f074SMaciej Żenczykowski 			ifp->idev->cnf.rtr_solicit_interval);
4211b7b1bfceSHannes Frederic Sowa 		ifp->idev->rs_probes = 1;
42121da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
4213bd11f074SMaciej Żenczykowski 		addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4214b7b1bfceSHannes Frederic Sowa 		spin_unlock(&ifp->lock);
4215b7b1bfceSHannes Frederic Sowa 		write_unlock_bh(&ifp->idev->lock);
42161da177e4SLinus Torvalds 	}
4217764d3be6SPaolo Abeni 
4218764d3be6SPaolo Abeni 	if (bump_id)
4219764d3be6SPaolo Abeni 		rt_genid_bump_ipv6(dev_net(dev));
4220a11a7f71SMarcus Huewe 
4221a11a7f71SMarcus Huewe 	/* Make sure that a new temporary address will be created
4222a11a7f71SMarcus Huewe 	 * before this temporary address becomes deprecated.
4223a11a7f71SMarcus Huewe 	 */
4224a11a7f71SMarcus Huewe 	if (ifp->flags & IFA_F_TEMPORARY)
4225a11a7f71SMarcus Huewe 		addrconf_verify_rtnl();
42261da177e4SLinus Torvalds }
42271da177e4SLinus Torvalds 
4228896585d4SHangbin Liu static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
4229e21e8467SStephen Hemminger {
4230c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
4231c5e33bddSYOSHIFUJI Hideaki 
4232c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
4233502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
423421809fafSstephen hemminger 		spin_lock(&ifp->lock);
4235896585d4SHangbin Liu 		if ((ifp->flags & IFA_F_TENTATIVE &&
4236896585d4SHangbin Liu 		     ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4237896585d4SHangbin Liu 			if (restart)
4238896585d4SHangbin Liu 				ifp->state = INET6_IFADDR_STATE_PREDAD;
4239c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
4240896585d4SHangbin Liu 		}
4241f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
4242c5e33bddSYOSHIFUJI Hideaki 	}
4243c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
4244c5e33bddSYOSHIFUJI Hideaki }
4245c5e33bddSYOSHIFUJI Hideaki 
42461da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
42471da177e4SLinus Torvalds struct if6_iter_state {
42483c40090aSDaniel Lezcano 	struct seq_net_private p;
42491da177e4SLinus Torvalds 	int bucket;
42501d578303SMihai Maruseac 	int offset;
42511da177e4SLinus Torvalds };
42521da177e4SLinus Torvalds 
42531d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
42541da177e4SLinus Torvalds {
42551da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
42561218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
4257a5c1d98fSEric Dumazet 	struct inet6_ifaddr *ifa = NULL;
42581d578303SMihai Maruseac 	int p = 0;
42591da177e4SLinus Torvalds 
42601d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
42611d578303SMihai Maruseac 	if (pos == 0) {
42621d578303SMihai Maruseac 		state->bucket = 0;
42631d578303SMihai Maruseac 		state->offset = 0;
42641d578303SMihai Maruseac 	}
42651d578303SMihai Maruseac 
42661d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4267a5c1d98fSEric Dumazet 		hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket],
42681d578303SMihai Maruseac 					 addr_lst) {
42699f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
42709f0d3c27SEric Dumazet 				continue;
42711d578303SMihai Maruseac 			/* sync with offset */
42721d578303SMihai Maruseac 			if (p < state->offset) {
42731d578303SMihai Maruseac 				p++;
42741d578303SMihai Maruseac 				continue;
42751d578303SMihai Maruseac 			}
42761da177e4SLinus Torvalds 			return ifa;
42771da177e4SLinus Torvalds 		}
42781d578303SMihai Maruseac 
42791d578303SMihai Maruseac 		/* prepare for next bucket */
42801d578303SMihai Maruseac 		state->offset = 0;
42811d578303SMihai Maruseac 		p = 0;
42821d578303SMihai Maruseac 	}
4283c2e21293Sstephen hemminger 	return NULL;
4284c2e21293Sstephen hemminger }
42851da177e4SLinus Torvalds 
4286c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4287c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
42881da177e4SLinus Torvalds {
42891da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
42901218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
42911da177e4SLinus Torvalds 
4292a5c1d98fSEric Dumazet 	hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
42939f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
42949f0d3c27SEric Dumazet 			continue;
42951d578303SMihai Maruseac 		state->offset++;
42961da177e4SLinus Torvalds 		return ifa;
42971d578303SMihai Maruseac 	}
42981da177e4SLinus Torvalds 
42991d578303SMihai Maruseac 	state->offset = 0;
430086f9bd1fSJeff Barnhill 	while (++state->bucket < IN6_ADDR_HSIZE) {
4301a5c1d98fSEric Dumazet 		hlist_for_each_entry_rcu(ifa,
4302c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
43039f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
43049f0d3c27SEric Dumazet 				continue;
4305c2e21293Sstephen hemminger 			return ifa;
4306c2e21293Sstephen hemminger 		}
4307c2e21293Sstephen hemminger 	}
4308c2e21293Sstephen hemminger 
4309c2e21293Sstephen hemminger 	return NULL;
4310c2e21293Sstephen hemminger }
4311c2e21293Sstephen hemminger 
43121da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4313a5c1d98fSEric Dumazet 	__acquires(rcu)
43141da177e4SLinus Torvalds {
4315a5c1d98fSEric Dumazet 	rcu_read_lock();
43161d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
43171da177e4SLinus Torvalds }
43181da177e4SLinus Torvalds 
43191da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
43201da177e4SLinus Torvalds {
43211da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
43221da177e4SLinus Torvalds 
43231da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
43241da177e4SLinus Torvalds 	++*pos;
43251da177e4SLinus Torvalds 	return ifa;
43261da177e4SLinus Torvalds }
43271da177e4SLinus Torvalds 
43281da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
4329a5c1d98fSEric Dumazet 	__releases(rcu)
43301da177e4SLinus Torvalds {
4331a5c1d98fSEric Dumazet 	rcu_read_unlock();
43321da177e4SLinus Torvalds }
43331da177e4SLinus Torvalds 
43341da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
43351da177e4SLinus Torvalds {
43361da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4337971a351cSJiri Pirko 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4338b071195dSHarvey Harrison 		   &ifp->addr,
43391da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
43401da177e4SLinus Torvalds 		   ifp->prefix_len,
43411da177e4SLinus Torvalds 		   ifp->scope,
4342971a351cSJiri Pirko 		   (u8) ifp->flags,
43431da177e4SLinus Torvalds 		   ifp->idev->dev->name);
43441da177e4SLinus Torvalds 	return 0;
43451da177e4SLinus Torvalds }
43461da177e4SLinus Torvalds 
434756b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
43481da177e4SLinus Torvalds 	.start	= if6_seq_start,
43491da177e4SLinus Torvalds 	.next	= if6_seq_next,
43501da177e4SLinus Torvalds 	.show	= if6_seq_show,
43511da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
43521da177e4SLinus Torvalds };
43531da177e4SLinus Torvalds 
43542c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
43551da177e4SLinus Torvalds {
4356c3506372SChristoph Hellwig 	if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4357c3506372SChristoph Hellwig 			sizeof(struct if6_iter_state)))
43581da177e4SLinus Torvalds 		return -ENOMEM;
43591da177e4SLinus Torvalds 	return 0;
43601da177e4SLinus Torvalds }
43611da177e4SLinus Torvalds 
43622c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
43633c40090aSDaniel Lezcano {
4364ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
43653c40090aSDaniel Lezcano }
43663c40090aSDaniel Lezcano 
43673c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
43683c40090aSDaniel Lezcano 	.init = if6_proc_net_init,
43693c40090aSDaniel Lezcano 	.exit = if6_proc_net_exit,
43703c40090aSDaniel Lezcano };
43713c40090aSDaniel Lezcano 
43723c40090aSDaniel Lezcano int __init if6_proc_init(void)
43733c40090aSDaniel Lezcano {
43743c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
43753c40090aSDaniel Lezcano }
43763c40090aSDaniel Lezcano 
43771da177e4SLinus Torvalds void if6_proc_exit(void)
43781da177e4SLinus Torvalds {
43793c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
43801da177e4SLinus Torvalds }
43811da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
43821da177e4SLinus Torvalds 
438307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
43843b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
4385b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
43863b9f9a1cSNoriaki TAKAMIYA {
43873f27fb23SEric Dumazet 	unsigned int hash = inet6_addr_hash(net, addr);
4388c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
43893f27fb23SEric Dumazet 	int ret = 0;
4390c2e21293Sstephen hemminger 
43914e5f47abSEric Dumazet 	rcu_read_lock();
43924e5f47abSEric Dumazet 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
4393878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
4394389f6612SDaniel Lezcano 			continue;
4395caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
43963b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
43973b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
43983b9f9a1cSNoriaki TAKAMIYA 			break;
43993b9f9a1cSNoriaki TAKAMIYA 		}
44003b9f9a1cSNoriaki TAKAMIYA 	}
44014e5f47abSEric Dumazet 	rcu_read_unlock();
44023b9f9a1cSNoriaki TAKAMIYA 	return ret;
44033b9f9a1cSNoriaki TAKAMIYA }
44043b9f9a1cSNoriaki TAKAMIYA #endif
44053b9f9a1cSNoriaki TAKAMIYA 
4406f37c6059SAlexander Aring /* RFC6554 has some algorithm to avoid loops in segment routing by
4407f37c6059SAlexander Aring  * checking if the segments contains any of a local interface address.
4408f37c6059SAlexander Aring  *
4409f37c6059SAlexander Aring  * Quote:
4410f37c6059SAlexander Aring  *
4411f37c6059SAlexander Aring  * To detect loops in the SRH, a router MUST determine if the SRH
4412f37c6059SAlexander Aring  * includes multiple addresses assigned to any interface on that router.
4413f37c6059SAlexander Aring  * If such addresses appear more than once and are separated by at least
4414f37c6059SAlexander Aring  * one address not assigned to that router.
4415f37c6059SAlexander Aring  */
4416f37c6059SAlexander Aring int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
4417f37c6059SAlexander Aring 			  unsigned char nsegs)
4418f37c6059SAlexander Aring {
4419f37c6059SAlexander Aring 	const struct in6_addr *addr;
4420f37c6059SAlexander Aring 	int i, ret = 0, found = 0;
4421f37c6059SAlexander Aring 	struct inet6_ifaddr *ifp;
4422f37c6059SAlexander Aring 	bool separated = false;
4423f37c6059SAlexander Aring 	unsigned int hash;
4424f37c6059SAlexander Aring 	bool hash_found;
4425f37c6059SAlexander Aring 
4426f37c6059SAlexander Aring 	rcu_read_lock();
4427f37c6059SAlexander Aring 	for (i = 0; i < nsegs; i++) {
4428f37c6059SAlexander Aring 		addr = &segs[i];
4429f37c6059SAlexander Aring 		hash = inet6_addr_hash(net, addr);
4430f37c6059SAlexander Aring 
4431f37c6059SAlexander Aring 		hash_found = false;
4432f37c6059SAlexander Aring 		hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
4433f37c6059SAlexander Aring 			if (!net_eq(dev_net(ifp->idev->dev), net))
4434f37c6059SAlexander Aring 				continue;
4435f37c6059SAlexander Aring 
4436f37c6059SAlexander Aring 			if (ipv6_addr_equal(&ifp->addr, addr)) {
4437f37c6059SAlexander Aring 				hash_found = true;
4438f37c6059SAlexander Aring 				break;
4439f37c6059SAlexander Aring 			}
4440f37c6059SAlexander Aring 		}
4441f37c6059SAlexander Aring 
4442f37c6059SAlexander Aring 		if (hash_found) {
4443f37c6059SAlexander Aring 			if (found > 1 && separated) {
4444f37c6059SAlexander Aring 				ret = 1;
4445f37c6059SAlexander Aring 				break;
4446f37c6059SAlexander Aring 			}
4447f37c6059SAlexander Aring 
4448f37c6059SAlexander Aring 			separated = false;
4449f37c6059SAlexander Aring 			found++;
4450f37c6059SAlexander Aring 		} else {
4451f37c6059SAlexander Aring 			separated = true;
4452f37c6059SAlexander Aring 		}
4453f37c6059SAlexander Aring 	}
4454f37c6059SAlexander Aring 	rcu_read_unlock();
4455f37c6059SAlexander Aring 
4456f37c6059SAlexander Aring 	return ret;
4457f37c6059SAlexander Aring }
4458f37c6059SAlexander Aring 
44591da177e4SLinus Torvalds /*
44601da177e4SLinus Torvalds  *	Periodic address status verification
44611da177e4SLinus Torvalds  */
44621da177e4SLinus Torvalds 
4463c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void)
44641da177e4SLinus Torvalds {
4465b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
44661da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
44671da177e4SLinus Torvalds 	int i;
44681da177e4SLinus Torvalds 
4469c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4470c15b1ccaSHannes Frederic Sowa 
44715c578aedSstephen hemminger 	rcu_read_lock_bh();
44721da177e4SLinus Torvalds 	now = jiffies;
4473b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
44741da177e4SLinus Torvalds 
4475c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
44761da177e4SLinus Torvalds 
44771da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
44781da177e4SLinus Torvalds restart:
4479c15b1ccaSHannes Frederic Sowa 		hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
44801da177e4SLinus Torvalds 			unsigned long age;
44811da177e4SLinus Torvalds 
4482fad8da3eSYasushi Asano 			/* When setting preferred_lft to a value not zero or
4483fad8da3eSYasushi Asano 			 * infinity, while valid_lft is infinity
4484fad8da3eSYasushi Asano 			 * IFA_F_PERMANENT has a non-infinity life time.
4485fad8da3eSYasushi Asano 			 */
4486fad8da3eSYasushi Asano 			if ((ifp->flags & IFA_F_PERMANENT) &&
4487fad8da3eSYasushi Asano 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
44881da177e4SLinus Torvalds 				continue;
44891da177e4SLinus Torvalds 
44901da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
4491b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
4492b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
44931da177e4SLinus Torvalds 
44948f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
44958f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
44961da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
44971da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
44981da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
44991da177e4SLinus Torvalds 				goto restart;
45008f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
45018f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
45028f27ebb9SYOSHIFUJI Hideaki 				continue;
45031da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
4504a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
45051da177e4SLinus Torvalds 				int deprecate = 0;
45061da177e4SLinus Torvalds 
45071da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
45081da177e4SLinus Torvalds 					deprecate = 1;
45091da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
45101da177e4SLinus Torvalds 				}
45111da177e4SLinus Torvalds 
4512fad8da3eSYasushi Asano 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4513fad8da3eSYasushi Asano 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
45141da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
45151da177e4SLinus Torvalds 
45161da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
45171da177e4SLinus Torvalds 
45181da177e4SLinus Torvalds 				if (deprecate) {
45191da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
45201da177e4SLinus Torvalds 
45211da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
45221da177e4SLinus Torvalds 					in6_ifa_put(ifp);
45231da177e4SLinus Torvalds 					goto restart;
45241da177e4SLinus Torvalds 				}
45251da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
45261da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
452788949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
452888949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
452919e16d22SHangbin Liu 					max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
453088949cf4Sstephen hemminger 
45311da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
45321da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
45331da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
45341da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
45351da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
45361da177e4SLinus Torvalds 						ifp->regen_count++;
45371da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
45381da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
45391da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
45405c578aedSstephen hemminger 
4541291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
4542291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
4543291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
4544e64e469bSEric Dumazet 						rcu_read_unlock_bh();
4545969c5464SFernando Gont 						ipv6_create_tempaddr(ifpub, true);
45461da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
45471da177e4SLinus Torvalds 						in6_ifa_put(ifp);
4548e64e469bSEric Dumazet 						rcu_read_lock_bh();
45491da177e4SLinus Torvalds 						goto restart;
45501da177e4SLinus Torvalds 					}
45511da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
45521da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
45531da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
45541da177e4SLinus Torvalds 			} else {
45551da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
45561da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
45571da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
45581da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
45591da177e4SLinus Torvalds 			}
45601da177e4SLinus Torvalds 		}
45611da177e4SLinus Torvalds 	}
45621da177e4SLinus Torvalds 
4563b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
4564b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
4565b2db7564SYOSHIFUJI Hideaki 
4566b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
4567b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4568b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
4569b2db7564SYOSHIFUJI Hideaki 
4570b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4571b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4572b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4573b2db7564SYOSHIFUJI Hideaki 
4574e32ac250SJoe Perches 	pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4575ba3542e1Sdingtianhong 		 now, next, next_sec, next_sched);
4576c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
45775c578aedSstephen hemminger 	rcu_read_unlock_bh();
45781da177e4SLinus Torvalds }
45791da177e4SLinus Torvalds 
4580c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w)
4581c15b1ccaSHannes Frederic Sowa {
4582c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
4583c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4584c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
4585c15b1ccaSHannes Frederic Sowa }
4586c15b1ccaSHannes Frederic Sowa 
4587c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void)
4588c15b1ccaSHannes Frederic Sowa {
4589c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4590c15b1ccaSHannes Frederic Sowa }
4591c15b1ccaSHannes Frederic Sowa 
4592caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4593caeaba79SNicolas Dichtel 				     struct in6_addr **peer_pfx)
4594461d8837SThomas Graf {
4595461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
4596461d8837SThomas Graf 
4597caeaba79SNicolas Dichtel 	*peer_pfx = NULL;
4598caeaba79SNicolas Dichtel 
4599461d8837SThomas Graf 	if (addr)
4600461d8837SThomas Graf 		pfx = nla_data(addr);
4601461d8837SThomas Graf 
4602461d8837SThomas Graf 	if (local) {
4603461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4604caeaba79SNicolas Dichtel 			*peer_pfx = pfx;
4605461d8837SThomas Graf 		pfx = nla_data(local);
4606461d8837SThomas Graf 	}
4607461d8837SThomas Graf 
4608461d8837SThomas Graf 	return pfx;
4609461d8837SThomas Graf }
4610461d8837SThomas Graf 
4611ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4612461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
4613461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
4614461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
4615479840ffSJiri Pirko 	[IFA_FLAGS]		= { .len = sizeof(u32) },
46168308f3ffSDavid Ahern 	[IFA_RT_PRIORITY]	= { .len = sizeof(u32) },
46176ecf4c37SChristian Brauner 	[IFA_TARGET_NETNSID]	= { .type = NLA_S32 },
4618461d8837SThomas Graf };
4619461d8837SThomas Graf 
46201da177e4SLinus Torvalds static int
4621c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4622c21ef3e3SDavid Ahern 		  struct netlink_ext_ack *extack)
46231da177e4SLinus Torvalds {
46243b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4625b933f716SThomas Graf 	struct ifaddrmsg *ifm;
4626b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4627caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
46286046d5b4SHeiner Kallweit 	u32 ifa_flags;
4629b933f716SThomas Graf 	int err;
46301da177e4SLinus Torvalds 
46318cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
46328cb08174SJohannes Berg 				     ifa_ipv6_policy, extack);
4633b933f716SThomas Graf 	if (err < 0)
4634b933f716SThomas Graf 		return err;
4635b933f716SThomas Graf 
4636b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
4637caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
463863159f29SIan Morris 	if (!pfx)
46391da177e4SLinus Torvalds 		return -EINVAL;
46401da177e4SLinus Torvalds 
46416046d5b4SHeiner Kallweit 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
46426046d5b4SHeiner Kallweit 
46436046d5b4SHeiner Kallweit 	/* We ignore other flags so far. */
46446046d5b4SHeiner Kallweit 	ifa_flags &= IFA_F_MANAGETEMPADDR;
46456046d5b4SHeiner Kallweit 
46466046d5b4SHeiner Kallweit 	return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
46476046d5b4SHeiner Kallweit 			      ifm->ifa_prefixlen);
46481da177e4SLinus Torvalds }
46491da177e4SLinus Torvalds 
46508308f3ffSDavid Ahern static int modify_prefix_route(struct inet6_ifaddr *ifp,
465161794012SHangbin Liu 			       unsigned long expires, u32 flags,
465261794012SHangbin Liu 			       bool modify_peer)
46538308f3ffSDavid Ahern {
46548308f3ffSDavid Ahern 	struct fib6_info *f6i;
4655e7c7faa9SDavid Ahern 	u32 prio;
46568308f3ffSDavid Ahern 
465761794012SHangbin Liu 	f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
465861794012SHangbin Liu 					ifp->prefix_len,
46592b2450caSDavid Ahern 					ifp->idev->dev, 0, RTF_DEFAULT, true);
46608308f3ffSDavid Ahern 	if (!f6i)
46618308f3ffSDavid Ahern 		return -ENOENT;
46628308f3ffSDavid Ahern 
4663e7c7faa9SDavid Ahern 	prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4664e7c7faa9SDavid Ahern 	if (f6i->fib6_metric != prio) {
4665e7c7faa9SDavid Ahern 		/* delete old one */
466611dd74b3SRoopa Prabhu 		ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
4667e7c7faa9SDavid Ahern 
46688308f3ffSDavid Ahern 		/* add new one */
466961794012SHangbin Liu 		addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
467061794012SHangbin Liu 				      ifp->prefix_len,
46718308f3ffSDavid Ahern 				      ifp->rt_priority, ifp->idev->dev,
46728308f3ffSDavid Ahern 				      expires, flags, GFP_KERNEL);
46738308f3ffSDavid Ahern 	} else {
46748308f3ffSDavid Ahern 		if (!expires)
46758308f3ffSDavid Ahern 			fib6_clean_expires(f6i);
46768308f3ffSDavid Ahern 		else
46778308f3ffSDavid Ahern 			fib6_set_expires(f6i, expires);
46788308f3ffSDavid Ahern 
46798308f3ffSDavid Ahern 		fib6_info_release(f6i);
46808308f3ffSDavid Ahern 	}
46818308f3ffSDavid Ahern 
46828308f3ffSDavid Ahern 	return 0;
46838308f3ffSDavid Ahern }
46848308f3ffSDavid Ahern 
4685d169a1f8SDavid Ahern static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg)
4686081bba5bSNoriaki TAKAMIYA {
46876f704992SYOSHIFUJI Hideaki 	u32 flags;
46886f704992SYOSHIFUJI Hideaki 	clock_t expires;
46894bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
469053bd6749SJiri Pirko 	bool was_managetempaddr;
46915b84efecSThomas Haller 	bool had_prefixroute;
4692d0098e4cSHangbin Liu 	bool new_peer = false;
469346d48046SYOSHIFUJI Hideaki 
4694c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4695c15b1ccaSHannes Frederic Sowa 
4696d169a1f8SDavid Ahern 	if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
4697081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
4698081bba5bSNoriaki TAKAMIYA 
4699d169a1f8SDavid Ahern 	if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
470053bd6749SJiri Pirko 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
470153bd6749SJiri Pirko 		return -EINVAL;
470253bd6749SJiri Pirko 
4703f1c02cfbSSabrina Dubroca 	if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
4704d169a1f8SDavid Ahern 		cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
4705f1c02cfbSSabrina Dubroca 
4706d169a1f8SDavid Ahern 	timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
47074bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
47084bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
4709d169a1f8SDavid Ahern 		cfg->valid_lft = timeout;
47106f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
47114bed72e4SYOSHIFUJI Hideaki 	} else {
47124bed72e4SYOSHIFUJI Hideaki 		expires = 0;
47134bed72e4SYOSHIFUJI Hideaki 		flags = 0;
4714d169a1f8SDavid Ahern 		cfg->ifa_flags |= IFA_F_PERMANENT;
47156f704992SYOSHIFUJI Hideaki 	}
4716081bba5bSNoriaki TAKAMIYA 
4717d169a1f8SDavid Ahern 	timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
47184bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
47194bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
4720d169a1f8SDavid Ahern 			cfg->ifa_flags |= IFA_F_DEPRECATED;
4721d169a1f8SDavid Ahern 		cfg->preferred_lft = timeout;
47224bed72e4SYOSHIFUJI Hideaki 	}
4723081bba5bSNoriaki TAKAMIYA 
4724d0098e4cSHangbin Liu 	if (cfg->peer_pfx &&
4725d0098e4cSHangbin Liu 	    memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
4726d0098e4cSHangbin Liu 		if (!ipv6_addr_any(&ifp->peer_addr))
4727d0098e4cSHangbin Liu 			cleanup_prefix_route(ifp, expires, true, true);
4728d0098e4cSHangbin Liu 		new_peer = true;
4729d0098e4cSHangbin Liu 	}
4730d0098e4cSHangbin Liu 
4731081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
473253bd6749SJiri Pirko 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
47335b84efecSThomas Haller 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
47345b84efecSThomas Haller 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
473553bd6749SJiri Pirko 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4736761aac73SThomas Haller 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4737761aac73SThomas Haller 			IFA_F_NOPREFIXROUTE);
4738d169a1f8SDavid Ahern 	ifp->flags |= cfg->ifa_flags;
4739081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
4740d169a1f8SDavid Ahern 	ifp->valid_lft = cfg->valid_lft;
4741d169a1f8SDavid Ahern 	ifp->prefered_lft = cfg->preferred_lft;
4742081bba5bSNoriaki TAKAMIYA 
47438308f3ffSDavid Ahern 	if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
47448308f3ffSDavid Ahern 		ifp->rt_priority = cfg->rt_priority;
4745081bba5bSNoriaki TAKAMIYA 
4746d0098e4cSHangbin Liu 	if (new_peer)
4747d0098e4cSHangbin Liu 		ifp->peer_addr = *cfg->peer_pfx;
4748d0098e4cSHangbin Liu 
4749081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
4750081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
4751081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
4752081bba5bSNoriaki TAKAMIYA 
4753d169a1f8SDavid Ahern 	if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
47548308f3ffSDavid Ahern 		int rc = -ENOENT;
47558308f3ffSDavid Ahern 
47568308f3ffSDavid Ahern 		if (had_prefixroute)
475761794012SHangbin Liu 			rc = modify_prefix_route(ifp, expires, flags, false);
47588308f3ffSDavid Ahern 
47598308f3ffSDavid Ahern 		/* prefix route could have been deleted; if so restore it */
47608308f3ffSDavid Ahern 		if (rc == -ENOENT) {
4761acb54e3cSDavid Ahern 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
47628308f3ffSDavid Ahern 					      ifp->rt_priority, ifp->idev->dev,
47638308f3ffSDavid Ahern 					      expires, flags, GFP_KERNEL);
47648308f3ffSDavid Ahern 		}
476561794012SHangbin Liu 
476661794012SHangbin Liu 		if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
476761794012SHangbin Liu 			rc = modify_prefix_route(ifp, expires, flags, true);
476861794012SHangbin Liu 
476961794012SHangbin Liu 		if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
477061794012SHangbin Liu 			addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
477161794012SHangbin Liu 					      ifp->rt_priority, ifp->idev->dev,
477261794012SHangbin Liu 					      expires, flags, GFP_KERNEL);
477361794012SHangbin Liu 		}
47745b84efecSThomas Haller 	} else if (had_prefixroute) {
47755b84efecSThomas Haller 		enum cleanup_prefix_rt_t action;
47765b84efecSThomas Haller 		unsigned long rt_expires;
47775b84efecSThomas Haller 
47785b84efecSThomas Haller 		write_lock_bh(&ifp->idev->lock);
47795b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &rt_expires);
47805b84efecSThomas Haller 		write_unlock_bh(&ifp->idev->lock);
47815b84efecSThomas Haller 
47825b84efecSThomas Haller 		if (action != CLEANUP_PREFIX_RT_NOP) {
47835b84efecSThomas Haller 			cleanup_prefix_route(ifp, rt_expires,
4784d0098e4cSHangbin Liu 				action == CLEANUP_PREFIX_RT_DEL, false);
47855b84efecSThomas Haller 		}
4786761aac73SThomas Haller 	}
478753bd6749SJiri Pirko 
478853bd6749SJiri Pirko 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4789d169a1f8SDavid Ahern 		if (was_managetempaddr &&
4790d169a1f8SDavid Ahern 		    !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
4791d169a1f8SDavid Ahern 			cfg->valid_lft = 0;
4792d169a1f8SDavid Ahern 			cfg->preferred_lft = 0;
4793d169a1f8SDavid Ahern 		}
4794d169a1f8SDavid Ahern 		manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4795d169a1f8SDavid Ahern 				 cfg->preferred_lft, !was_managetempaddr,
4796d169a1f8SDavid Ahern 				 jiffies);
479753bd6749SJiri Pirko 	}
479853bd6749SJiri Pirko 
4799c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4800081bba5bSNoriaki TAKAMIYA 
4801081bba5bSNoriaki TAKAMIYA 	return 0;
4802081bba5bSNoriaki TAKAMIYA }
4803081bba5bSNoriaki TAKAMIYA 
4804081bba5bSNoriaki TAKAMIYA static int
4805c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4806c21ef3e3SDavid Ahern 		  struct netlink_ext_ack *extack)
48071da177e4SLinus Torvalds {
48083b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4809461d8837SThomas Graf 	struct ifaddrmsg *ifm;
4810461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
481119b1518cSDavid Ahern 	struct in6_addr *peer_pfx;
48127198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
48137198f8ceSThomas Graf 	struct net_device *dev;
4814f1c02cfbSSabrina Dubroca 	struct inet6_dev *idev;
481519b1518cSDavid Ahern 	struct ifa6_config cfg;
4816461d8837SThomas Graf 	int err;
48171da177e4SLinus Torvalds 
48188cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
48198cb08174SJohannes Berg 				     ifa_ipv6_policy, extack);
4820461d8837SThomas Graf 	if (err < 0)
4821461d8837SThomas Graf 		return err;
4822461d8837SThomas Graf 
482319b1518cSDavid Ahern 	memset(&cfg, 0, sizeof(cfg));
482419b1518cSDavid Ahern 
4825461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
482619b1518cSDavid Ahern 	cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
482719b1518cSDavid Ahern 	if (!cfg.pfx)
48281da177e4SLinus Torvalds 		return -EINVAL;
48291da177e4SLinus Torvalds 
483019b1518cSDavid Ahern 	cfg.peer_pfx = peer_pfx;
483119b1518cSDavid Ahern 	cfg.plen = ifm->ifa_prefixlen;
48328308f3ffSDavid Ahern 	if (tb[IFA_RT_PRIORITY])
48338308f3ffSDavid Ahern 		cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
48348308f3ffSDavid Ahern 
483519b1518cSDavid Ahern 	cfg.valid_lft = INFINITY_LIFE_TIME;
483619b1518cSDavid Ahern 	cfg.preferred_lft = INFINITY_LIFE_TIME;
483719b1518cSDavid Ahern 
4838461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
48390778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
4840461d8837SThomas Graf 
4841461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
484219b1518cSDavid Ahern 		cfg.valid_lft = ci->ifa_valid;
484319b1518cSDavid Ahern 		cfg.preferred_lft = ci->ifa_prefered;
48440778769dSNoriaki TAKAMIYA 	}
48450778769dSNoriaki TAKAMIYA 
4846af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
484763159f29SIan Morris 	if (!dev)
48487198f8ceSThomas Graf 		return -ENODEV;
4849081bba5bSNoriaki TAKAMIYA 
485019b1518cSDavid Ahern 	if (tb[IFA_FLAGS])
485119b1518cSDavid Ahern 		cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
485219b1518cSDavid Ahern 	else
485319b1518cSDavid Ahern 		cfg.ifa_flags = ifm->ifa_flags;
4854479840ffSJiri Pirko 
485555ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
485619b1518cSDavid Ahern 	cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
485719b1518cSDavid Ahern 			 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
485819b1518cSDavid Ahern 			 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
4859f1c02cfbSSabrina Dubroca 
4860f1c02cfbSSabrina Dubroca 	idev = ipv6_find_idev(dev);
4861db0b99f5SSabrina Dubroca 	if (IS_ERR(idev))
4862db0b99f5SSabrina Dubroca 		return PTR_ERR(idev);
4863f1c02cfbSSabrina Dubroca 
4864f1c02cfbSSabrina Dubroca 	if (!ipv6_allow_optimistic_dad(net, idev))
486519b1518cSDavid Ahern 		cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
4866f1c02cfbSSabrina Dubroca 
486719b1518cSDavid Ahern 	if (cfg.ifa_flags & IFA_F_NODAD &&
486819b1518cSDavid Ahern 	    cfg.ifa_flags & IFA_F_OPTIMISTIC) {
4869f1c02cfbSSabrina Dubroca 		NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4870f1c02cfbSSabrina Dubroca 		return -EINVAL;
4871f1c02cfbSSabrina Dubroca 	}
487255ebaef1SNoriaki TAKAMIYA 
487319b1518cSDavid Ahern 	ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
487463159f29SIan Morris 	if (!ifa) {
48757198f8ceSThomas Graf 		/*
48767198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
4877db9c7c39Sstephen hemminger 		 * userspace already relies on not having to provide this.
48787198f8ceSThomas Graf 		 */
487919b1518cSDavid Ahern 		return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
48801da177e4SLinus Torvalds 	}
48811da177e4SLinus Torvalds 
48827198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
48837198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
48847198f8ceSThomas Graf 		err = -EEXIST;
48857198f8ceSThomas Graf 	else
4886d169a1f8SDavid Ahern 		err = inet6_addr_modify(ifa, &cfg);
48877198f8ceSThomas Graf 
48887198f8ceSThomas Graf 	in6_ifa_put(ifa);
48897198f8ceSThomas Graf 
48907198f8ceSThomas Graf 	return err;
48917198f8ceSThomas Graf }
48927198f8ceSThomas Graf 
4893479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4894101bb229SThomas Graf 			  u8 scope, int ifindex)
4895101bb229SThomas Graf {
4896101bb229SThomas Graf 	struct ifaddrmsg *ifm;
4897101bb229SThomas Graf 
4898101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
4899101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
4900101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
4901101bb229SThomas Graf 	ifm->ifa_flags = flags;
4902101bb229SThomas Graf 	ifm->ifa_scope = scope;
4903101bb229SThomas Graf 	ifm->ifa_index = ifindex;
4904101bb229SThomas Graf }
4905101bb229SThomas Graf 
490685486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
490785486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
490885486af0SThomas Graf {
490985486af0SThomas Graf 	struct ifa_cacheinfo ci;
491085486af0SThomas Graf 
491118a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
491218a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
491385486af0SThomas Graf 	ci.ifa_prefered = preferred;
491485486af0SThomas Graf 	ci.ifa_valid = valid;
491585486af0SThomas Graf 
491685486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
491785486af0SThomas Graf }
491885486af0SThomas Graf 
4919101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
4920101bb229SThomas Graf {
4921101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
4922101bb229SThomas Graf 		return RT_SCOPE_HOST;
4923101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
4924101bb229SThomas Graf 		return RT_SCOPE_LINK;
4925101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
4926101bb229SThomas Graf 		return RT_SCOPE_SITE;
4927101bb229SThomas Graf 	else
4928101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
4929101bb229SThomas Graf }
4930101bb229SThomas Graf 
49310ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
49320ab6803bSThomas Graf {
4933339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4934caeaba79SNicolas Dichtel 	       + nla_total_size(16) /* IFA_LOCAL */
4935339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
4936479840ffSJiri Pirko 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
49378308f3ffSDavid Ahern 	       + nla_total_size(4)  /* IFA_FLAGS */
49388308f3ffSDavid Ahern 	       + nla_total_size(4)  /* IFA_RT_PRIORITY */;
49390ab6803bSThomas Graf }
4940c5396a31SYOSHIFUJI Hideaki 
49416ba1e6e8SDavid Ahern enum addr_type_t {
49426ba1e6e8SDavid Ahern 	UNICAST_ADDR,
49436ba1e6e8SDavid Ahern 	MULTICAST_ADDR,
49446ba1e6e8SDavid Ahern 	ANYCAST_ADDR,
49456ba1e6e8SDavid Ahern };
49466ba1e6e8SDavid Ahern 
4947203651b6SChristian Brauner struct inet6_fill_args {
4948203651b6SChristian Brauner 	u32 portid;
4949203651b6SChristian Brauner 	u32 seq;
4950203651b6SChristian Brauner 	int event;
4951203651b6SChristian Brauner 	unsigned int flags;
4952203651b6SChristian Brauner 	int netnsid;
49536371a71fSDavid Ahern 	int ifindex;
49546ba1e6e8SDavid Ahern 	enum addr_type_t type;
4955203651b6SChristian Brauner };
4956203651b6SChristian Brauner 
49571da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4958203651b6SChristian Brauner 			     struct inet6_fill_args *args)
49591da177e4SLinus Torvalds {
49601da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
496185486af0SThomas Graf 	u32 preferred, valid;
49621da177e4SLinus Torvalds 
4963203651b6SChristian Brauner 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4964203651b6SChristian Brauner 			sizeof(struct ifaddrmsg), args->flags);
496563159f29SIan Morris 	if (!nlh)
496626932566SPatrick McHardy 		return -EMSGSIZE;
49670ab6803bSThomas Graf 
4968101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4969101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
4970101bb229SThomas Graf 
4971203651b6SChristian Brauner 	if (args->netnsid >= 0 &&
4972203651b6SChristian Brauner 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
49736ecf4c37SChristian Brauner 		goto error;
49746ecf4c37SChristian Brauner 
4975fad8da3eSYasushi Asano 	if (!((ifa->flags&IFA_F_PERMANENT) &&
4976fad8da3eSYasushi Asano 	      (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
497785486af0SThomas Graf 		preferred = ifa->prefered_lft;
497885486af0SThomas Graf 		valid = ifa->valid_lft;
497985486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
49801da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
4981a1faa698SJens Rosenboom 			if (preferred > tval)
498285486af0SThomas Graf 				preferred -= tval;
4983a1faa698SJens Rosenboom 			else
4984a1faa698SJens Rosenboom 				preferred = 0;
4985f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
4986f56619fcSBen Hutchings 				if (valid > tval)
498785486af0SThomas Graf 					valid -= tval;
4988f56619fcSBen Hutchings 				else
4989f56619fcSBen Hutchings 					valid = 0;
4990f56619fcSBen Hutchings 			}
49911da177e4SLinus Torvalds 		}
49921da177e4SLinus Torvalds 	} else {
499385486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
499485486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
49951da177e4SLinus Torvalds 	}
499685486af0SThomas Graf 
49977996c799SCong Wang 	if (!ipv6_addr_any(&ifa->peer_addr)) {
4998930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4999930345eaSJiri Benc 		    nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
5000caeaba79SNicolas Dichtel 			goto error;
5001caeaba79SNicolas Dichtel 	} else
5002930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
5003caeaba79SNicolas Dichtel 			goto error;
5004caeaba79SNicolas Dichtel 
50058308f3ffSDavid Ahern 	if (ifa->rt_priority &&
50068308f3ffSDavid Ahern 	    nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
50078308f3ffSDavid Ahern 		goto error;
50088308f3ffSDavid Ahern 
5009caeaba79SNicolas Dichtel 	if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
5010caeaba79SNicolas Dichtel 		goto error;
501185486af0SThomas Graf 
5012479840ffSJiri Pirko 	if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
5013479840ffSJiri Pirko 		goto error;
5014479840ffSJiri Pirko 
5015053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5016053c095aSJohannes Berg 	return 0;
5017caeaba79SNicolas Dichtel 
5018caeaba79SNicolas Dichtel error:
5019caeaba79SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
5020caeaba79SNicolas Dichtel 	return -EMSGSIZE;
50211da177e4SLinus Torvalds }
50221da177e4SLinus Torvalds 
50231da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
5024203651b6SChristian Brauner 			       struct inet6_fill_args *args)
50251da177e4SLinus Torvalds {
50261da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
5027101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
5028101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
50291da177e4SLinus Torvalds 
50301da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
5031101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
5032101bb229SThomas Graf 
5033203651b6SChristian Brauner 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5034203651b6SChristian Brauner 			sizeof(struct ifaddrmsg), args->flags);
503563159f29SIan Morris 	if (!nlh)
503626932566SPatrick McHardy 		return -EMSGSIZE;
50370ab6803bSThomas Graf 
5038203651b6SChristian Brauner 	if (args->netnsid >= 0 &&
5039203651b6SChristian Brauner 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
50406ecf4c37SChristian Brauner 		return -EMSGSIZE;
50416ecf4c37SChristian Brauner 
5042101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5043930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
50440ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
504526932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
504626932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
504726932566SPatrick McHardy 		return -EMSGSIZE;
504826932566SPatrick McHardy 	}
504985486af0SThomas Graf 
5050053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5051053c095aSJohannes Berg 	return 0;
50521da177e4SLinus Torvalds }
50531da177e4SLinus Torvalds 
50541da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
5055203651b6SChristian Brauner 			       struct inet6_fill_args *args)
50561da177e4SLinus Torvalds {
50579ee8cbb2SDavid Ahern 	struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
50589ee8cbb2SDavid Ahern 	int ifindex = dev ? dev->ifindex : 1;
50591da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
5060101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
50611da177e4SLinus Torvalds 
50621da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
5063101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
5064101bb229SThomas Graf 
5065203651b6SChristian Brauner 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5066203651b6SChristian Brauner 			sizeof(struct ifaddrmsg), args->flags);
506763159f29SIan Morris 	if (!nlh)
506826932566SPatrick McHardy 		return -EMSGSIZE;
50690ab6803bSThomas Graf 
5070203651b6SChristian Brauner 	if (args->netnsid >= 0 &&
5071203651b6SChristian Brauner 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
50726ecf4c37SChristian Brauner 		return -EMSGSIZE;
50736ecf4c37SChristian Brauner 
5074101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5075930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
50760ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
507726932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
507826932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
507926932566SPatrick McHardy 		return -EMSGSIZE;
508026932566SPatrick McHardy 	}
508185486af0SThomas Graf 
5082053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5083053c095aSJohannes Berg 	return 0;
50841da177e4SLinus Torvalds }
50851da177e4SLinus Torvalds 
5086234b27c3SEric Dumazet /* called with rcu_read_lock() */
5087234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
5088fe884c2bSDavid Ahern 			  struct netlink_callback *cb, int s_ip_idx,
50896ba1e6e8SDavid Ahern 			  struct inet6_fill_args *fillargs)
50901da177e4SLinus Torvalds {
50911da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
50921da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
5093fe884c2bSDavid Ahern 	int ip_idx = 0;
5094234b27c3SEric Dumazet 	int err = 1;
50951da177e4SLinus Torvalds 
50961da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
50976ba1e6e8SDavid Ahern 	switch (fillargs->type) {
5098502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
5099502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
51006ba1e6e8SDavid Ahern 		fillargs->event = RTM_NEWADDR;
5101502a2ffdSstephen hemminger 
5102ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
5103502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
51044ba4c566SDavid Ahern 			if (ip_idx < s_ip_idx)
51054ba4c566SDavid Ahern 				goto next;
51066ba1e6e8SDavid Ahern 			err = inet6_fill_ifaddr(skb, ifa, fillargs);
5107053c095aSJohannes Berg 			if (err < 0)
5108bcd32326SEric Dumazet 				break;
510963998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
51104ba4c566SDavid Ahern next:
51114ba4c566SDavid Ahern 			ip_idx++;
51121da177e4SLinus Torvalds 		}
51131da177e4SLinus Torvalds 		break;
5114502a2ffdSstephen hemminger 	}
51151da177e4SLinus Torvalds 	case MULTICAST_ADDR:
51166ba1e6e8SDavid Ahern 		fillargs->event = RTM_GETMULTICAST;
5117203651b6SChristian Brauner 
51181da177e4SLinus Torvalds 		/* multicast address */
51191da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
51201da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
51211da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
51221da177e4SLinus Torvalds 				continue;
51236ba1e6e8SDavid Ahern 			err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
5124053c095aSJohannes Berg 			if (err < 0)
5125bcd32326SEric Dumazet 				break;
51261da177e4SLinus Torvalds 		}
51271da177e4SLinus Torvalds 		break;
51281da177e4SLinus Torvalds 	case ANYCAST_ADDR:
51296ba1e6e8SDavid Ahern 		fillargs->event = RTM_GETANYCAST;
51301da177e4SLinus Torvalds 		/* anycast address */
51311da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
51321da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
51331da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
51341da177e4SLinus Torvalds 				continue;
51356ba1e6e8SDavid Ahern 			err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
5136053c095aSJohannes Berg 			if (err < 0)
5137bcd32326SEric Dumazet 				break;
51381da177e4SLinus Torvalds 		}
51391da177e4SLinus Torvalds 		break;
51401da177e4SLinus Torvalds 	default:
51411da177e4SLinus Torvalds 		break;
51421da177e4SLinus Torvalds 	}
51431da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
5144fe884c2bSDavid Ahern 	cb->args[2] = ip_idx;
5145234b27c3SEric Dumazet 	return err;
5146234b27c3SEric Dumazet }
51475d5619b4SYOSHIFUJI Hideaki 
5148ed6eff11SDavid Ahern static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5149ed6eff11SDavid Ahern 				       struct inet6_fill_args *fillargs,
5150ed6eff11SDavid Ahern 				       struct net **tgt_net, struct sock *sk,
51516371a71fSDavid Ahern 				       struct netlink_callback *cb)
5152ed6eff11SDavid Ahern {
51536371a71fSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
5154ed6eff11SDavid Ahern 	struct nlattr *tb[IFA_MAX+1];
5155ed6eff11SDavid Ahern 	struct ifaddrmsg *ifm;
5156ed6eff11SDavid Ahern 	int err, i;
5157ed6eff11SDavid Ahern 
5158ed6eff11SDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5159ed6eff11SDavid Ahern 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5160ed6eff11SDavid Ahern 		return -EINVAL;
5161ed6eff11SDavid Ahern 	}
5162ed6eff11SDavid Ahern 
5163ed6eff11SDavid Ahern 	ifm = nlmsg_data(nlh);
5164ed6eff11SDavid Ahern 	if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5165ed6eff11SDavid Ahern 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5166ed6eff11SDavid Ahern 		return -EINVAL;
5167ed6eff11SDavid Ahern 	}
51686371a71fSDavid Ahern 
51696371a71fSDavid Ahern 	fillargs->ifindex = ifm->ifa_index;
51706371a71fSDavid Ahern 	if (fillargs->ifindex) {
51716371a71fSDavid Ahern 		cb->answer_flags |= NLM_F_DUMP_FILTERED;
51726371a71fSDavid Ahern 		fillargs->flags |= NLM_F_DUMP_FILTERED;
5173ed6eff11SDavid Ahern 	}
5174ed6eff11SDavid Ahern 
51758cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5176ed6eff11SDavid Ahern 					    ifa_ipv6_policy, extack);
5177ed6eff11SDavid Ahern 	if (err < 0)
5178ed6eff11SDavid Ahern 		return err;
5179ed6eff11SDavid Ahern 
5180ed6eff11SDavid Ahern 	for (i = 0; i <= IFA_MAX; ++i) {
5181ed6eff11SDavid Ahern 		if (!tb[i])
5182ed6eff11SDavid Ahern 			continue;
5183ed6eff11SDavid Ahern 
5184ed6eff11SDavid Ahern 		if (i == IFA_TARGET_NETNSID) {
5185ed6eff11SDavid Ahern 			struct net *net;
5186ed6eff11SDavid Ahern 
5187ed6eff11SDavid Ahern 			fillargs->netnsid = nla_get_s32(tb[i]);
5188ed6eff11SDavid Ahern 			net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5189ed6eff11SDavid Ahern 			if (IS_ERR(net)) {
5190bf4cc40eSBjørn Mork 				fillargs->netnsid = -1;
5191ed6eff11SDavid Ahern 				NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5192ed6eff11SDavid Ahern 				return PTR_ERR(net);
5193ed6eff11SDavid Ahern 			}
5194ed6eff11SDavid Ahern 			*tgt_net = net;
5195ed6eff11SDavid Ahern 		} else {
5196ed6eff11SDavid Ahern 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5197ed6eff11SDavid Ahern 			return -EINVAL;
5198ed6eff11SDavid Ahern 		}
5199ed6eff11SDavid Ahern 	}
5200ed6eff11SDavid Ahern 
5201ed6eff11SDavid Ahern 	return 0;
5202ed6eff11SDavid Ahern }
5203ed6eff11SDavid Ahern 
5204234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5205234b27c3SEric Dumazet 			   enum addr_type_t type)
5206234b27c3SEric Dumazet {
5207ed6eff11SDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
52086ba1e6e8SDavid Ahern 	struct inet6_fill_args fillargs = {
52096ba1e6e8SDavid Ahern 		.portid = NETLINK_CB(cb->skb).portid,
52106ba1e6e8SDavid Ahern 		.seq = cb->nlh->nlmsg_seq,
52116ba1e6e8SDavid Ahern 		.flags = NLM_F_MULTI,
52126ba1e6e8SDavid Ahern 		.netnsid = -1,
52136ba1e6e8SDavid Ahern 		.type = type,
52146ba1e6e8SDavid Ahern 	};
5215234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
52166ecf4c37SChristian Brauner 	struct net *tgt_net = net;
5217fe884c2bSDavid Ahern 	int idx, s_idx, s_ip_idx;
5218234b27c3SEric Dumazet 	int h, s_h;
5219234b27c3SEric Dumazet 	struct net_device *dev;
5220234b27c3SEric Dumazet 	struct inet6_dev *idev;
5221234b27c3SEric Dumazet 	struct hlist_head *head;
5222242afaa6SDavid Ahern 	int err = 0;
5223234b27c3SEric Dumazet 
5224234b27c3SEric Dumazet 	s_h = cb->args[0];
5225234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
5226fe884c2bSDavid Ahern 	s_ip_idx = cb->args[2];
5227234b27c3SEric Dumazet 
5228ed6eff11SDavid Ahern 	if (cb->strict_check) {
5229ed6eff11SDavid Ahern 		err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
52306371a71fSDavid Ahern 						  skb->sk, cb);
5231ed6eff11SDavid Ahern 		if (err < 0)
5232242afaa6SDavid Ahern 			goto put_tgt_net;
52336371a71fSDavid Ahern 
5234242afaa6SDavid Ahern 		err = 0;
52356371a71fSDavid Ahern 		if (fillargs.ifindex) {
52366371a71fSDavid Ahern 			dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
5237242afaa6SDavid Ahern 			if (!dev) {
5238242afaa6SDavid Ahern 				err = -ENODEV;
5239242afaa6SDavid Ahern 				goto put_tgt_net;
5240242afaa6SDavid Ahern 			}
52416371a71fSDavid Ahern 			idev = __in6_dev_get(dev);
52426371a71fSDavid Ahern 			if (idev) {
52436371a71fSDavid Ahern 				err = in6_dump_addrs(idev, skb, cb, s_ip_idx,
52446371a71fSDavid Ahern 						     &fillargs);
52451518039fSJakub Kicinski 				if (err > 0)
52461518039fSJakub Kicinski 					err = 0;
52476371a71fSDavid Ahern 			}
52486371a71fSDavid Ahern 			goto put_tgt_net;
52496371a71fSDavid Ahern 		}
52506ecf4c37SChristian Brauner 	}
52516ecf4c37SChristian Brauner 
5252234b27c3SEric Dumazet 	rcu_read_lock();
52536ecf4c37SChristian Brauner 	cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq;
5254234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5255234b27c3SEric Dumazet 		idx = 0;
52566ecf4c37SChristian Brauner 		head = &tgt_net->dev_index_head[h];
5257b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
5258234b27c3SEric Dumazet 			if (idx < s_idx)
5259234b27c3SEric Dumazet 				goto cont;
52604b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
5261234b27c3SEric Dumazet 				s_ip_idx = 0;
5262e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
5263e21e8467SStephen Hemminger 			if (!idev)
5264234b27c3SEric Dumazet 				goto cont;
5265234b27c3SEric Dumazet 
5266fe884c2bSDavid Ahern 			if (in6_dump_addrs(idev, skb, cb, s_ip_idx,
52676ba1e6e8SDavid Ahern 					   &fillargs) < 0)
5268234b27c3SEric Dumazet 				goto done;
52697562f876SPavel Emelianov cont:
52707562f876SPavel Emelianov 			idx++;
52711da177e4SLinus Torvalds 		}
5272234b27c3SEric Dumazet 	}
5273234b27c3SEric Dumazet done:
5274234b27c3SEric Dumazet 	rcu_read_unlock();
5275234b27c3SEric Dumazet 	cb->args[0] = h;
5276234b27c3SEric Dumazet 	cb->args[1] = idx;
52776371a71fSDavid Ahern put_tgt_net:
52786ba1e6e8SDavid Ahern 	if (fillargs.netnsid >= 0)
52796ecf4c37SChristian Brauner 		put_net(tgt_net);
5280234b27c3SEric Dumazet 
52817c1e8a38SArthur Gautier 	return skb->len ? : err;
52821da177e4SLinus Torvalds }
52831da177e4SLinus Torvalds 
52841da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
52851da177e4SLinus Torvalds {
52861da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
5287b854272bSDenis V. Lunev 
52881da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
52891da177e4SLinus Torvalds }
52901da177e4SLinus Torvalds 
52911da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
52921da177e4SLinus Torvalds {
52931da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
5294b854272bSDenis V. Lunev 
52951da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
52961da177e4SLinus Torvalds }
52971da177e4SLinus Torvalds 
52981da177e4SLinus Torvalds 
52991da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
53001da177e4SLinus Torvalds {
53011da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
5302b854272bSDenis V. Lunev 
53031da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
53041da177e4SLinus Torvalds }
53051da177e4SLinus Torvalds 
53064b1373deSJakub Kicinski static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
53074b1373deSJakub Kicinski 				       const struct nlmsghdr *nlh,
53084b1373deSJakub Kicinski 				       struct nlattr **tb,
53094b1373deSJakub Kicinski 				       struct netlink_ext_ack *extack)
53104b1373deSJakub Kicinski {
53114b1373deSJakub Kicinski 	struct ifaddrmsg *ifm;
53124b1373deSJakub Kicinski 	int i, err;
53134b1373deSJakub Kicinski 
53144b1373deSJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
53154b1373deSJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
53164b1373deSJakub Kicinski 		return -EINVAL;
53174b1373deSJakub Kicinski 	}
53184b1373deSJakub Kicinski 
53192beb6d29SHangbin Liu 	if (!netlink_strict_get_check(skb))
53202beb6d29SHangbin Liu 		return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
53212beb6d29SHangbin Liu 					      ifa_ipv6_policy, extack);
53222beb6d29SHangbin Liu 
53234b1373deSJakub Kicinski 	ifm = nlmsg_data(nlh);
53244b1373deSJakub Kicinski 	if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
53254b1373deSJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
53264b1373deSJakub Kicinski 		return -EINVAL;
53274b1373deSJakub Kicinski 	}
53284b1373deSJakub Kicinski 
53298cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
53304b1373deSJakub Kicinski 					    ifa_ipv6_policy, extack);
53314b1373deSJakub Kicinski 	if (err)
53324b1373deSJakub Kicinski 		return err;
53334b1373deSJakub Kicinski 
53344b1373deSJakub Kicinski 	for (i = 0; i <= IFA_MAX; i++) {
53354b1373deSJakub Kicinski 		if (!tb[i])
53364b1373deSJakub Kicinski 			continue;
53374b1373deSJakub Kicinski 
53384b1373deSJakub Kicinski 		switch (i) {
53394b1373deSJakub Kicinski 		case IFA_TARGET_NETNSID:
53404b1373deSJakub Kicinski 		case IFA_ADDRESS:
53414b1373deSJakub Kicinski 		case IFA_LOCAL:
53424b1373deSJakub Kicinski 			break;
53434b1373deSJakub Kicinski 		default:
53444b1373deSJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
53454b1373deSJakub Kicinski 			return -EINVAL;
53464b1373deSJakub Kicinski 		}
53474b1373deSJakub Kicinski 	}
53484b1373deSJakub Kicinski 
53494b1373deSJakub Kicinski 	return 0;
53504b1373deSJakub Kicinski }
53514b1373deSJakub Kicinski 
5352c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5353c21ef3e3SDavid Ahern 			     struct netlink_ext_ack *extack)
53546c223828SNoriaki TAKAMIYA {
53553b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
5356203651b6SChristian Brauner 	struct inet6_fill_args fillargs = {
5357203651b6SChristian Brauner 		.portid = NETLINK_CB(in_skb).portid,
5358203651b6SChristian Brauner 		.seq = nlh->nlmsg_seq,
5359203651b6SChristian Brauner 		.event = RTM_NEWADDR,
5360203651b6SChristian Brauner 		.flags = 0,
5361203651b6SChristian Brauner 		.netnsid = -1,
5362203651b6SChristian Brauner 	};
53636ecf4c37SChristian Brauner 	struct net *tgt_net = net;
53641b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
53651b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
5366caeaba79SNicolas Dichtel 	struct in6_addr *addr = NULL, *peer;
53676c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
53686c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
53696c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
53706c223828SNoriaki TAKAMIYA 	int err;
53716c223828SNoriaki TAKAMIYA 
53724b1373deSJakub Kicinski 	err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
53731b29fc2cSThomas Graf 	if (err < 0)
5374c24675f8SFlorian Westphal 		return err;
53756c223828SNoriaki TAKAMIYA 
53766ecf4c37SChristian Brauner 	if (tb[IFA_TARGET_NETNSID]) {
5377203651b6SChristian Brauner 		fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
53786ecf4c37SChristian Brauner 
53796ecf4c37SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
5380203651b6SChristian Brauner 						  fillargs.netnsid);
53816ecf4c37SChristian Brauner 		if (IS_ERR(tgt_net))
53826ecf4c37SChristian Brauner 			return PTR_ERR(tgt_net);
53836ecf4c37SChristian Brauner 	}
53846ecf4c37SChristian Brauner 
5385caeaba79SNicolas Dichtel 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
5386c24675f8SFlorian Westphal 	if (!addr)
5387c24675f8SFlorian Westphal 		return -EINVAL;
53881b29fc2cSThomas Graf 
53891b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
53906c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
53916ecf4c37SChristian Brauner 		dev = dev_get_by_index(tgt_net, ifm->ifa_index);
53926c223828SNoriaki TAKAMIYA 
53936ecf4c37SChristian Brauner 	ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
5394e21e8467SStephen Hemminger 	if (!ifa) {
53951b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
53961b29fc2cSThomas Graf 		goto errout;
53976c223828SNoriaki TAKAMIYA 	}
53986c223828SNoriaki TAKAMIYA 
5399e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5400e21e8467SStephen Hemminger 	if (!skb) {
54011b29fc2cSThomas Graf 		err = -ENOBUFS;
54021b29fc2cSThomas Graf 		goto errout_ifa;
54031b29fc2cSThomas Graf 	}
54041b29fc2cSThomas Graf 
5405203651b6SChristian Brauner 	err = inet6_fill_ifaddr(skb, ifa, &fillargs);
540626932566SPatrick McHardy 	if (err < 0) {
540726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
540826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
540926932566SPatrick McHardy 		kfree_skb(skb);
541026932566SPatrick McHardy 		goto errout_ifa;
541126932566SPatrick McHardy 	}
54126ecf4c37SChristian Brauner 	err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
54131b29fc2cSThomas Graf errout_ifa:
54146c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
54151b29fc2cSThomas Graf errout:
5416c24675f8SFlorian Westphal 	if (dev)
5417c24675f8SFlorian Westphal 		dev_put(dev);
5418203651b6SChristian Brauner 	if (fillargs.netnsid >= 0)
54196ecf4c37SChristian Brauner 		put_net(tgt_net);
54206ecf4c37SChristian Brauner 
54216c223828SNoriaki TAKAMIYA 	return err;
54226c223828SNoriaki TAKAMIYA }
54236c223828SNoriaki TAKAMIYA 
54241da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
54251da177e4SLinus Torvalds {
54261da177e4SLinus Torvalds 	struct sk_buff *skb;
5427c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
5428203651b6SChristian Brauner 	struct inet6_fill_args fillargs = {
5429203651b6SChristian Brauner 		.portid = 0,
5430203651b6SChristian Brauner 		.seq = 0,
5431203651b6SChristian Brauner 		.event = event,
5432203651b6SChristian Brauner 		.flags = 0,
5433203651b6SChristian Brauner 		.netnsid = -1,
5434203651b6SChristian Brauner 	};
54355d620266SThomas Graf 	int err = -ENOBUFS;
54361da177e4SLinus Torvalds 
54370ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
543863159f29SIan Morris 	if (!skb)
54395d620266SThomas Graf 		goto errout;
54405d620266SThomas Graf 
5441203651b6SChristian Brauner 	err = inet6_fill_ifaddr(skb, ifa, &fillargs);
544226932566SPatrick McHardy 	if (err < 0) {
544326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
544426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
544526932566SPatrick McHardy 		kfree_skb(skb);
544626932566SPatrick McHardy 		goto errout;
544726932566SPatrick McHardy 	}
54481ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
54491ce85fe4SPablo Neira Ayuso 	return;
54505d620266SThomas Graf errout:
54515d620266SThomas Graf 	if (err < 0)
54526fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
54531da177e4SLinus Torvalds }
54541da177e4SLinus Torvalds 
5455b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
54561da177e4SLinus Torvalds 				__s32 *array, int bytes)
54571da177e4SLinus Torvalds {
545804561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
545904561c1fSThomas Graf 
54601da177e4SLinus Torvalds 	memset(array, 0, bytes);
54611da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
54621da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
54631da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
54641da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
54651da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
54661da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
54671da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
54681da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
546993908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
547093908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
5471bd11f074SMaciej Żenczykowski 	array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5472bd11f074SMaciej Żenczykowski 		jiffies_to_msecs(cnf->rtr_solicit_max_interval);
547393908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
547493908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
54751da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
5476fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5477fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5478fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5479fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
54801da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
54811da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
54821da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
54831da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
54841da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
54851da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
548665f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
54878013d1d7SHangbin Liu 	array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
5488c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
5489930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
5490930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
549193908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
549293908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
5493fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
5494bbea124bSJoel Scherpelz 	array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
549509c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
549609c884d4SYOSHIFUJI Hideaki #endif
5497930d6ff2SYOSHIFUJI Hideaki #endif
5498fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
54990bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
550095c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
550195c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
55027fd2561eSErik Kline 	array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
550395c385b4SNeil Horman #endif
55047bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
55057bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
55067bc570c8SYOSHIFUJI Hideaki #endif
5507778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
55081b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
5509c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
55105cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5511b800c3b9SHannes Frederic Sowa 	array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5512d9333196SBen Greear 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5513c2943f14SHarout Hedeshian 	array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
551435103d11SAndy Gospodarek 	array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
55153d1bec99SHannes Frederic Sowa 	/* we omit DEVCONF_STABLE_SECRET for now */
55163985e8a3SErik Kline 	array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5517abbc3043SJohannes Berg 	array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
55187a02bf89SJohannes Berg 	array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5519f1705ec1SDavid Ahern 	array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
55201ababebaSDavid Lebrun 	array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5521bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
5522bf355b8dSDavid Lebrun 	array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5523bf355b8dSDavid Lebrun #endif
5524adc176c5SErik Nordmark 	array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5525d35a00b8SFelix Jia 	array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5526df789fe7SDavid Forster 	array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
55272210d6b2SMaciej Żenczykowski 	array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
55288610c7c6SAlexander Aring 	array[DEVCONF_RPL_SEG_ENABLED] = cnf->rpl_seg_enabled;
55291da177e4SLinus Torvalds }
55301da177e4SLinus Torvalds 
5531b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
5532b382b191SThomas Graf {
5533b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
5534b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
5535b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5536b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5537f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5538bdd72f41SSabrina Dubroca 	     + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5539bdd72f41SSabrina Dubroca 	     + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5540bdd72f41SSabrina Dubroca 	     + 0;
5541b382b191SThomas Graf }
5542b382b191SThomas Graf 
5543339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
5544339bf98fSThomas Graf {
5545339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5546339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5547339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5548339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
5549339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
55500344338bSAndy Gospodarek 	       + nla_total_size(1) /* IFLA_OPERSTATE */
5551b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5552339bf98fSThomas Graf }
5553c5396a31SYOSHIFUJI Hideaki 
5554be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5555aca05671SJia He 					int bytes)
55567f7d9a6bSHerbert Xu {
55577f7d9a6bSHerbert Xu 	int i;
5558aca05671SJia He 	int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
55597f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
55607f7d9a6bSHerbert Xu 
55617f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
5562aca05671SJia He 	put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5563aca05671SJia He 	for (i = 1; i < ICMP6_MIB_MAX; i++)
5564be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
55657f7d9a6bSHerbert Xu 
5566aca05671SJia He 	memset(&stats[ICMP6_MIB_MAX], 0, pad);
55677f7d9a6bSHerbert Xu }
55687f7d9a6bSHerbert Xu 
5569698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5570a3a77372SRaghavendra K T 					int bytes, size_t syncpoff)
55714ce3c183SEric Dumazet {
5572a3a77372SRaghavendra K T 	int i, c;
5573a3a77372SRaghavendra K T 	u64 buff[IPSTATS_MIB_MAX];
5574a3a77372SRaghavendra K T 	int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5575a3a77372SRaghavendra K T 
55764ce3c183SEric Dumazet 	BUG_ON(pad < 0);
55774ce3c183SEric Dumazet 
5578a3a77372SRaghavendra K T 	memset(buff, 0, sizeof(buff));
5579a3a77372SRaghavendra K T 	buff[0] = IPSTATS_MIB_MAX;
55804ce3c183SEric Dumazet 
5581a3a77372SRaghavendra K T 	for_each_possible_cpu(c) {
5582a3a77372SRaghavendra K T 		for (i = 1; i < IPSTATS_MIB_MAX; i++)
5583a3a77372SRaghavendra K T 			buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5584a3a77372SRaghavendra K T 	}
5585a3a77372SRaghavendra K T 
5586a3a77372SRaghavendra K T 	memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5587a3a77372SRaghavendra K T 	memset(&stats[IPSTATS_MIB_MAX], 0, pad);
55884ce3c183SEric Dumazet }
55894ce3c183SEric Dumazet 
55907f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
55917f7d9a6bSHerbert Xu 			     int bytes)
55927f7d9a6bSHerbert Xu {
55937f7d9a6bSHerbert Xu 	switch (attrtype) {
55947f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
5595a3a77372SRaghavendra K T 		__snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5596a3a77372SRaghavendra K T 				     offsetof(struct ipstats_mib, syncp));
55977f7d9a6bSHerbert Xu 		break;
55987f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
5599aca05671SJia He 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
56007f7d9a6bSHerbert Xu 		break;
56017f7d9a6bSHerbert Xu 	}
56027f7d9a6bSHerbert Xu }
56037f7d9a6bSHerbert Xu 
5604d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5605d5566fd7SSowmini Varadhan 				  u32 ext_filter_mask)
56061da177e4SLinus Torvalds {
5607bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
56081da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
56091da177e4SLinus Torvalds 
5610c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5611c78679e8SDavid S. Miller 		goto nla_put_failure;
56121da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
561324912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
561424912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
56151f9248e5SJiri Pirko 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5616c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5617c78679e8SDavid S. Miller 		goto nla_put_failure;
5618bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
561963159f29SIan Morris 	if (!nla)
562004561c1fSThomas Graf 		goto nla_put_failure;
5621bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
56221da177e4SLinus Torvalds 
5623bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
5624bf99f1bdSYOSHIFUJI Hideaki 
5625d5566fd7SSowmini Varadhan 	if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5626d5566fd7SSowmini Varadhan 		return 0;
5627d5566fd7SSowmini Varadhan 
5628bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
562963159f29SIan Morris 	if (!nla)
5630bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
5631bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5632bf99f1bdSYOSHIFUJI Hideaki 
5633bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
563463159f29SIan Morris 	if (!nla)
5635bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
5636bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
56371da177e4SLinus Torvalds 
5638f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
563963159f29SIan Morris 	if (!nla)
5640f53adae4SDaniel Borkmann 		goto nla_put_failure;
5641f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
5642f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5643f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
5644f53adae4SDaniel Borkmann 
56450d7982ceSNicolas Dichtel 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
56460d7982ceSNicolas Dichtel 		goto nla_put_failure;
56470d7982ceSNicolas Dichtel 
5648b382b191SThomas Graf 	return 0;
5649b382b191SThomas Graf 
5650b382b191SThomas Graf nla_put_failure:
5651b382b191SThomas Graf 	return -EMSGSIZE;
5652b382b191SThomas Graf }
5653b382b191SThomas Graf 
5654b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev,
5655b1974ed0SArad, Ronen 				     u32 ext_filter_mask)
5656b382b191SThomas Graf {
5657b382b191SThomas Graf 	if (!__in6_dev_get(dev))
5658b382b191SThomas Graf 		return 0;
5659b382b191SThomas Graf 
5660b382b191SThomas Graf 	return inet6_ifla6_size();
5661b382b191SThomas Graf }
5662b382b191SThomas Graf 
5663d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5664d5566fd7SSowmini Varadhan 			      u32 ext_filter_mask)
5665b382b191SThomas Graf {
5666b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
5667b382b191SThomas Graf 
5668b382b191SThomas Graf 	if (!idev)
5669b382b191SThomas Graf 		return -ENODATA;
5670b382b191SThomas Graf 
5671d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5672b382b191SThomas Graf 		return -EMSGSIZE;
5673b382b191SThomas Graf 
5674b382b191SThomas Graf 	return 0;
5675b382b191SThomas Graf }
5676b382b191SThomas Graf 
5677f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5678f53adae4SDaniel Borkmann {
5679f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
5680f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
568147e27d5eSDaniel Borkmann 	bool clear_token, update_rs = false;
5682dc848292SHannes Frederic Sowa 	struct in6_addr ll_addr;
5683f53adae4SDaniel Borkmann 
5684c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
5685c15b1ccaSHannes Frederic Sowa 
568663159f29SIan Morris 	if (!token)
5687f53adae4SDaniel Borkmann 		return -EINVAL;
5688f53adae4SDaniel Borkmann 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5689f53adae4SDaniel Borkmann 		return -EINVAL;
5690f53adae4SDaniel Borkmann 	if (!ipv6_accept_ra(idev))
5691f53adae4SDaniel Borkmann 		return -EINVAL;
5692bd11f074SMaciej Żenczykowski 	if (idev->cnf.rtr_solicits == 0)
5693f53adae4SDaniel Borkmann 		return -EINVAL;
5694f53adae4SDaniel Borkmann 
5695f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5696f53adae4SDaniel Borkmann 
5697f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5698f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5699f53adae4SDaniel Borkmann 
5700f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5701f53adae4SDaniel Borkmann 
570247e27d5eSDaniel Borkmann 	clear_token = ipv6_addr_any(token);
570347e27d5eSDaniel Borkmann 	if (clear_token)
570447e27d5eSDaniel Borkmann 		goto update_lft;
570547e27d5eSDaniel Borkmann 
5706dc848292SHannes Frederic Sowa 	if (!idev->dead && (idev->if_flags & IF_READY) &&
5707dc848292SHannes Frederic Sowa 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5708dc848292SHannes Frederic Sowa 			     IFA_F_OPTIMISTIC)) {
5709fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
5710fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
5711fc403832SDaniel Borkmann 		 */
5712f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5713fc403832SDaniel Borkmann 		update_rs = true;
5714fc403832SDaniel Borkmann 	}
5715f53adae4SDaniel Borkmann 
571647e27d5eSDaniel Borkmann update_lft:
5717f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5718fc403832SDaniel Borkmann 
571977ecaaceSHannes Frederic Sowa 	if (update_rs) {
5720f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
5721bd11f074SMaciej Żenczykowski 		idev->rs_interval = rfc3315_s14_backoff_init(
5722bd11f074SMaciej Żenczykowski 			idev->cnf.rtr_solicit_interval);
572377ecaaceSHannes Frederic Sowa 		idev->rs_probes = 1;
5724bd11f074SMaciej Żenczykowski 		addrconf_mod_rs_timer(idev, idev->rs_interval);
572577ecaaceSHannes Frederic Sowa 	}
5726f53adae4SDaniel Borkmann 
5727f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
5728f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
5729f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
5730617fe29dSDaniel Borkmann 		if (ifp->tokenized) {
5731f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
5732f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
5733f53adae4SDaniel Borkmann 		}
5734f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
5735f53adae4SDaniel Borkmann 	}
5736f53adae4SDaniel Borkmann 
5737f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5738b2ed64a9SLubomir Rintel 	inet6_ifinfo_notify(RTM_NEWLINK, idev);
5739c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
5740f53adae4SDaniel Borkmann 	return 0;
5741f53adae4SDaniel Borkmann }
5742f53adae4SDaniel Borkmann 
574311b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
574411b1f828SDaniel Borkmann 	[IFLA_INET6_ADDR_GEN_MODE]	= { .type = NLA_U8 },
574511b1f828SDaniel Borkmann 	[IFLA_INET6_TOKEN]		= { .len = sizeof(struct in6_addr) },
574611b1f828SDaniel Borkmann };
574711b1f828SDaniel Borkmann 
5748d35a00b8SFelix Jia static int check_addr_gen_mode(int mode)
5749d35a00b8SFelix Jia {
5750d35a00b8SFelix Jia 	if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5751d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_NONE &&
5752d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5753d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_RANDOM)
5754d35a00b8SFelix Jia 		return -EINVAL;
5755d35a00b8SFelix Jia 	return 1;
5756d35a00b8SFelix Jia }
5757d35a00b8SFelix Jia 
5758d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5759d35a00b8SFelix Jia 				int mode)
5760d35a00b8SFelix Jia {
5761d35a00b8SFelix Jia 	if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5762d35a00b8SFelix Jia 	    !idev->cnf.stable_secret.initialized &&
5763d35a00b8SFelix Jia 	    !net->ipv6.devconf_dflt->stable_secret.initialized)
5764d35a00b8SFelix Jia 		return -EINVAL;
5765d35a00b8SFelix Jia 	return 1;
5766d35a00b8SFelix Jia }
5767d35a00b8SFelix Jia 
57687dc2bccaSMaxim Mikityanskiy static int inet6_validate_link_af(const struct net_device *dev,
57697dc2bccaSMaxim Mikityanskiy 				  const struct nlattr *nla)
5770f53adae4SDaniel Borkmann {
5771f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
57727dc2bccaSMaxim Mikityanskiy 	struct inet6_dev *idev = NULL;
57737dc2bccaSMaxim Mikityanskiy 	int err;
5774f53adae4SDaniel Borkmann 
57757dc2bccaSMaxim Mikityanskiy 	if (dev) {
57767dc2bccaSMaxim Mikityanskiy 		idev = __in6_dev_get(dev);
5777f53adae4SDaniel Borkmann 		if (!idev)
5778f53adae4SDaniel Borkmann 			return -EAFNOSUPPORT;
57797dc2bccaSMaxim Mikityanskiy 	}
57807dc2bccaSMaxim Mikityanskiy 
57817dc2bccaSMaxim Mikityanskiy 	err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
57827dc2bccaSMaxim Mikityanskiy 					  inet6_af_policy, NULL);
57837dc2bccaSMaxim Mikityanskiy 	if (err)
57847dc2bccaSMaxim Mikityanskiy 		return err;
57857dc2bccaSMaxim Mikityanskiy 
57867dc2bccaSMaxim Mikityanskiy 	if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
57877dc2bccaSMaxim Mikityanskiy 		return -EINVAL;
57887dc2bccaSMaxim Mikityanskiy 
57897dc2bccaSMaxim Mikityanskiy 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
57907dc2bccaSMaxim Mikityanskiy 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
57917dc2bccaSMaxim Mikityanskiy 
57927dc2bccaSMaxim Mikityanskiy 		if (check_addr_gen_mode(mode) < 0)
57937dc2bccaSMaxim Mikityanskiy 			return -EINVAL;
57947dc2bccaSMaxim Mikityanskiy 		if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
57957dc2bccaSMaxim Mikityanskiy 			return -EINVAL;
57967dc2bccaSMaxim Mikityanskiy 	}
57977dc2bccaSMaxim Mikityanskiy 
57987dc2bccaSMaxim Mikityanskiy 	return 0;
57997dc2bccaSMaxim Mikityanskiy }
58007dc2bccaSMaxim Mikityanskiy 
58017dc2bccaSMaxim Mikityanskiy static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
58027dc2bccaSMaxim Mikityanskiy {
58037dc2bccaSMaxim Mikityanskiy 	struct inet6_dev *idev = __in6_dev_get(dev);
58047dc2bccaSMaxim Mikityanskiy 	struct nlattr *tb[IFLA_INET6_MAX + 1];
58057dc2bccaSMaxim Mikityanskiy 	int err;
5806f53adae4SDaniel Borkmann 
5807db3fa271SEric Dumazet 	if (!idev)
5808db3fa271SEric Dumazet 		return -EAFNOSUPPORT;
5809db3fa271SEric Dumazet 
58108cb08174SJohannes Berg 	if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
5811f53adae4SDaniel Borkmann 		BUG();
5812f53adae4SDaniel Borkmann 
5813bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_TOKEN]) {
5814f53adae4SDaniel Borkmann 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5815bc91b0f0SJiri Pirko 		if (err)
5816bc91b0f0SJiri Pirko 			return err;
5817bc91b0f0SJiri Pirko 	}
5818bc91b0f0SJiri Pirko 
5819bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5820bc91b0f0SJiri Pirko 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5821bc91b0f0SJiri Pirko 
5822d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = mode;
5823bc91b0f0SJiri Pirko 	}
5824f53adae4SDaniel Borkmann 
58257dc2bccaSMaxim Mikityanskiy 	return 0;
5826f53adae4SDaniel Borkmann }
5827f53adae4SDaniel Borkmann 
5828b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
582915e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
5830b382b191SThomas Graf {
5831b382b191SThomas Graf 	struct net_device *dev = idev->dev;
5832b382b191SThomas Graf 	struct ifinfomsg *hdr;
5833b382b191SThomas Graf 	struct nlmsghdr *nlh;
5834b382b191SThomas Graf 	void *protoinfo;
5835b382b191SThomas Graf 
583615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
583763159f29SIan Morris 	if (!nlh)
5838b382b191SThomas Graf 		return -EMSGSIZE;
5839b382b191SThomas Graf 
5840b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
5841b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
5842b382b191SThomas Graf 	hdr->__ifi_pad = 0;
5843b382b191SThomas Graf 	hdr->ifi_type = dev->type;
5844b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
5845b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
5846b382b191SThomas Graf 	hdr->ifi_change = 0;
5847b382b191SThomas Graf 
5848c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5849c78679e8SDavid S. Miller 	    (dev->addr_len &&
5850c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5851c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5852a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
58530344338bSAndy Gospodarek 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
58540344338bSAndy Gospodarek 	    nla_put_u8(skb, IFLA_OPERSTATE,
58550344338bSAndy Gospodarek 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5856c78679e8SDavid S. Miller 		goto nla_put_failure;
5857ae0be8deSMichal Kubecek 	protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
585863159f29SIan Morris 	if (!protoinfo)
5859b382b191SThomas Graf 		goto nla_put_failure;
5860b382b191SThomas Graf 
5861d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5862b382b191SThomas Graf 		goto nla_put_failure;
5863b382b191SThomas Graf 
586404561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
5865053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5866053c095aSJohannes Berg 	return 0;
58671da177e4SLinus Torvalds 
586804561c1fSThomas Graf nla_put_failure:
586926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
587026932566SPatrick McHardy 	return -EMSGSIZE;
58711da177e4SLinus Torvalds }
58721da177e4SLinus Torvalds 
5873786e0007SDavid Ahern static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
5874786e0007SDavid Ahern 				   struct netlink_ext_ack *extack)
5875786e0007SDavid Ahern {
5876786e0007SDavid Ahern 	struct ifinfomsg *ifm;
5877786e0007SDavid Ahern 
5878786e0007SDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5879786e0007SDavid Ahern 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
5880786e0007SDavid Ahern 		return -EINVAL;
5881786e0007SDavid Ahern 	}
5882786e0007SDavid Ahern 
5883786e0007SDavid Ahern 	if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
5884786e0007SDavid Ahern 		NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
5885786e0007SDavid Ahern 		return -EINVAL;
5886786e0007SDavid Ahern 	}
5887786e0007SDavid Ahern 
5888786e0007SDavid Ahern 	ifm = nlmsg_data(nlh);
5889786e0007SDavid Ahern 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
5890786e0007SDavid Ahern 	    ifm->ifi_change || ifm->ifi_index) {
5891786e0007SDavid Ahern 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
5892786e0007SDavid Ahern 		return -EINVAL;
5893786e0007SDavid Ahern 	}
5894786e0007SDavid Ahern 
5895786e0007SDavid Ahern 	return 0;
5896786e0007SDavid Ahern }
5897786e0007SDavid Ahern 
58981da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
58991da177e4SLinus Torvalds {
59003b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
590184d2697dSEric Dumazet 	int h, s_h;
5902434a8a58SDavid S. Miller 	int idx = 0, s_idx;
59031da177e4SLinus Torvalds 	struct net_device *dev;
59041da177e4SLinus Torvalds 	struct inet6_dev *idev;
590584d2697dSEric Dumazet 	struct hlist_head *head;
59061da177e4SLinus Torvalds 
5907786e0007SDavid Ahern 	/* only requests using strict checking can pass data to
5908786e0007SDavid Ahern 	 * influence the dump
5909786e0007SDavid Ahern 	 */
5910786e0007SDavid Ahern 	if (cb->strict_check) {
5911786e0007SDavid Ahern 		int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
5912786e0007SDavid Ahern 
5913786e0007SDavid Ahern 		if (err < 0)
5914786e0007SDavid Ahern 			return err;
5915786e0007SDavid Ahern 	}
5916786e0007SDavid Ahern 
591784d2697dSEric Dumazet 	s_h = cb->args[0];
591884d2697dSEric Dumazet 	s_idx = cb->args[1];
591984d2697dSEric Dumazet 
592084d2697dSEric Dumazet 	rcu_read_lock();
592184d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
59227562f876SPavel Emelianov 		idx = 0;
592384d2697dSEric Dumazet 		head = &net->dev_index_head[h];
5924b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
59251da177e4SLinus Torvalds 			if (idx < s_idx)
59267562f876SPavel Emelianov 				goto cont;
592784d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
592884d2697dSEric Dumazet 			if (!idev)
59297562f876SPavel Emelianov 				goto cont;
593084d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
593115e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
593284d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
5933053c095aSJohannes Berg 					      RTM_NEWLINK, NLM_F_MULTI) < 0)
593484d2697dSEric Dumazet 				goto out;
59357562f876SPavel Emelianov cont:
59367562f876SPavel Emelianov 			idx++;
59371da177e4SLinus Torvalds 		}
593884d2697dSEric Dumazet 	}
593984d2697dSEric Dumazet out:
594084d2697dSEric Dumazet 	rcu_read_unlock();
594184d2697dSEric Dumazet 	cb->args[1] = idx;
594284d2697dSEric Dumazet 	cb->args[0] = h;
59431da177e4SLinus Torvalds 
59441da177e4SLinus Torvalds 	return skb->len;
59451da177e4SLinus Torvalds }
59461da177e4SLinus Torvalds 
59471da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
59481da177e4SLinus Torvalds {
59491da177e4SLinus Torvalds 	struct sk_buff *skb;
5950c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
59518d7a76c9SThomas Graf 	int err = -ENOBUFS;
59521da177e4SLinus Torvalds 
5953339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
595463159f29SIan Morris 	if (!skb)
59558d7a76c9SThomas Graf 		goto errout;
59568d7a76c9SThomas Graf 
59578d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
595826932566SPatrick McHardy 	if (err < 0) {
595926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
596026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
596126932566SPatrick McHardy 		kfree_skb(skb);
596226932566SPatrick McHardy 		goto errout;
596326932566SPatrick McHardy 	}
59645f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
59651ce85fe4SPablo Neira Ayuso 	return;
59668d7a76c9SThomas Graf errout:
59678d7a76c9SThomas Graf 	if (err < 0)
59685f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
59691da177e4SLinus Torvalds }
59701da177e4SLinus Torvalds 
5971339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
5972339bf98fSThomas Graf {
5973339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
5974339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
5975339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
5976339bf98fSThomas Graf }
5977c5396a31SYOSHIFUJI Hideaki 
59781da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
597915e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
5980b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
59811da177e4SLinus Torvalds {
59821da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
59831da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
59841da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
59851da177e4SLinus Torvalds 
598615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
598763159f29SIan Morris 	if (!nlh)
598826932566SPatrick McHardy 		return -EMSGSIZE;
59896051e2f4SThomas Graf 
59906051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
59911da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
59928a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
59938a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
59941da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
59951da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
59961da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
59978a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
59981da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
59991da177e4SLinus Torvalds 	if (pinfo->onlink)
60001da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
60011da177e4SLinus Torvalds 	if (pinfo->autoconf)
60021da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
60031da177e4SLinus Torvalds 
6004c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
6005c78679e8SDavid S. Miller 		goto nla_put_failure;
60061da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
60071da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
6008c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
6009c78679e8SDavid S. Miller 		goto nla_put_failure;
6010053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
6011053c095aSJohannes Berg 	return 0;
60121da177e4SLinus Torvalds 
60136051e2f4SThomas Graf nla_put_failure:
601426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
601526932566SPatrick McHardy 	return -EMSGSIZE;
60161da177e4SLinus Torvalds }
60171da177e4SLinus Torvalds 
60181da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
60191da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
60201da177e4SLinus Torvalds {
60211da177e4SLinus Torvalds 	struct sk_buff *skb;
6022c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
60238c384bfaSThomas Graf 	int err = -ENOBUFS;
60241da177e4SLinus Torvalds 
6025339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
602663159f29SIan Morris 	if (!skb)
60278c384bfaSThomas Graf 		goto errout;
60288c384bfaSThomas Graf 
60298c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
603026932566SPatrick McHardy 	if (err < 0) {
603126932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
603226932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
603326932566SPatrick McHardy 		kfree_skb(skb);
603426932566SPatrick McHardy 		goto errout;
603526932566SPatrick McHardy 	}
60361ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
60371ce85fe4SPablo Neira Ayuso 	return;
60388c384bfaSThomas Graf errout:
60398c384bfaSThomas Graf 	if (err < 0)
60406fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
60411da177e4SLinus Torvalds }
60421da177e4SLinus Torvalds 
60431da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
60441da177e4SLinus Torvalds {
604563998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
604663998ac2SNicolas Dichtel 
6047c15b1ccaSHannes Frederic Sowa 	if (event)
6048c15b1ccaSHannes Frederic Sowa 		ASSERT_RTNL();
6049c15b1ccaSHannes Frederic Sowa 
60501da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
60511da177e4SLinus Torvalds 
60521da177e4SLinus Torvalds 	switch (event) {
60531da177e4SLinus Torvalds 	case RTM_NEWADDR:
605495c385b4SNeil Horman 		/*
60552d819d25SDavid Ahern 		 * If the address was optimistic we inserted the route at the
60562d819d25SDavid Ahern 		 * start of our DAD process, so we don't need to do it again.
60572d819d25SDavid Ahern 		 * If the device was taken down in the middle of the DAD
60582d819d25SDavid Ahern 		 * cycle there is a race where we could get here without a
60592d819d25SDavid Ahern 		 * host route, so nothing to insert. That will be fixed when
60602d819d25SDavid Ahern 		 * the device is brought up.
606195c385b4SNeil Horman 		 */
60622d819d25SDavid Ahern 		if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
6063afb1d4b5SDavid Ahern 			ip6_ins_rt(net, ifp->rt);
60642d819d25SDavid Ahern 		} else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
60652d819d25SDavid Ahern 			pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
60662d819d25SDavid Ahern 				&ifp->addr, ifp->idev->dev->name);
60672d819d25SDavid Ahern 		}
60682d819d25SDavid Ahern 
60691da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
60701da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
60717996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr))
607207758eb9SHangbin Liu 			addrconf_prefix_route(&ifp->peer_addr, 128,
607307758eb9SHangbin Liu 					      ifp->rt_priority, ifp->idev->dev,
607407758eb9SHangbin Liu 					      0, 0, GFP_ATOMIC);
60751da177e4SLinus Torvalds 		break;
60761da177e4SLinus Torvalds 	case RTM_DELADDR:
60771da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
60781da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
60791da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
60807996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr)) {
60818d1c802bSDavid Ahern 			struct fib6_info *rt;
6082caeaba79SNicolas Dichtel 
6083e7478dfcSNicolas Dichtel 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
60842b2450caSDavid Ahern 						       ifp->idev->dev, 0, 0,
60852b2450caSDavid Ahern 						       false);
60868e3d5be7SMartin KaFai Lau 			if (rt)
608711dd74b3SRoopa Prabhu 				ip6_del_rt(net, rt, false);
6088caeaba79SNicolas Dichtel 		}
608938bd10c4SDavid Ahern 		if (ifp->rt) {
609011dd74b3SRoopa Prabhu 			ip6_del_rt(net, ifp->rt, false);
609193531c67SDavid Ahern 			ifp->rt = NULL;
609238bd10c4SDavid Ahern 		}
6093705f1c86SHannes Frederic Sowa 		rt_genid_bump_ipv6(net);
60941da177e4SLinus Torvalds 		break;
60951da177e4SLinus Torvalds 	}
609663998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
60971da177e4SLinus Torvalds }
60981da177e4SLinus Torvalds 
60991da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
61001da177e4SLinus Torvalds {
61018814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
61021da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
61031da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
61048814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
61051da177e4SLinus Torvalds }
61061da177e4SLinus Torvalds 
61071da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
61081da177e4SLinus Torvalds 
610932927393SChristoph Hellwig static int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
611032927393SChristoph Hellwig 		void *buffer, size_t *lenp, loff_t *ppos)
61111da177e4SLinus Torvalds {
61121da177e4SLinus Torvalds 	int *valp = ctl->data;
61131da177e4SLinus Torvalds 	int val = *valp;
611488af182eSEric W. Biederman 	loff_t pos = *ppos;
6115fe2c6338SJoe Perches 	struct ctl_table lctl;
61161da177e4SLinus Torvalds 	int ret;
61171da177e4SLinus Torvalds 
6118013d97e9SFrancesco Ruggeri 	/*
6119013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
6120013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
6121013d97e9SFrancesco Ruggeri 	 */
6122013d97e9SFrancesco Ruggeri 	lctl = *ctl;
6123013d97e9SFrancesco Ruggeri 	lctl.data = &val;
6124013d97e9SFrancesco Ruggeri 
6125013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
61261da177e4SLinus Torvalds 
6127c8fecf22SPavel Emelyanov 	if (write)
6128b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
612988af182eSEric W. Biederman 	if (ret)
613088af182eSEric W. Biederman 		*ppos = pos;
61311da177e4SLinus Torvalds 	return ret;
61321da177e4SLinus Torvalds }
61331da177e4SLinus Torvalds 
613432927393SChristoph Hellwig static int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
613532927393SChristoph Hellwig 		void *buffer, size_t *lenp, loff_t *ppos)
613677751427SMarcelo Leitner {
613777751427SMarcelo Leitner 	struct inet6_dev *idev = ctl->extra1;
613877751427SMarcelo Leitner 	int min_mtu = IPV6_MIN_MTU;
613977751427SMarcelo Leitner 	struct ctl_table lctl;
614077751427SMarcelo Leitner 
614177751427SMarcelo Leitner 	lctl = *ctl;
614277751427SMarcelo Leitner 	lctl.extra1 = &min_mtu;
614377751427SMarcelo Leitner 	lctl.extra2 = idev ? &idev->dev->mtu : NULL;
614477751427SMarcelo Leitner 
614577751427SMarcelo Leitner 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
614677751427SMarcelo Leitner }
614777751427SMarcelo Leitner 
614856d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
614956d417b1SBrian Haley {
615075538c2bSCong Wang 	struct netdev_notifier_info info;
615175538c2bSCong Wang 
615256d417b1SBrian Haley 	if (!idev || !idev->dev)
615356d417b1SBrian Haley 		return;
615456d417b1SBrian Haley 
615575538c2bSCong Wang 	netdev_notifier_info_init(&info, idev->dev);
615656d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
615775538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_DOWN, &info);
615856d417b1SBrian Haley 	else
615975538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_UP, &info);
616056d417b1SBrian Haley }
616156d417b1SBrian Haley 
616256d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
616356d417b1SBrian Haley {
616456d417b1SBrian Haley 	struct net_device *dev;
616556d417b1SBrian Haley 	struct inet6_dev *idev;
616656d417b1SBrian Haley 
616703e4deffSKefeng Wang 	for_each_netdev(net, dev) {
616856d417b1SBrian Haley 		idev = __in6_dev_get(dev);
616956d417b1SBrian Haley 		if (idev) {
617056d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
617156d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
617256d417b1SBrian Haley 			if (changed)
617356d417b1SBrian Haley 				dev_disable_change(idev);
617456d417b1SBrian Haley 		}
617556d417b1SBrian Haley 	}
617656d417b1SBrian Haley }
617756d417b1SBrian Haley 
6178013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
617956d417b1SBrian Haley {
618056d417b1SBrian Haley 	struct net *net;
6181013d97e9SFrancesco Ruggeri 	int old;
6182013d97e9SFrancesco Ruggeri 
6183013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
6184013d97e9SFrancesco Ruggeri 		return restart_syscall();
618556d417b1SBrian Haley 
618656d417b1SBrian Haley 	net = (struct net *)table->extra2;
6187013d97e9SFrancesco Ruggeri 	old = *p;
6188013d97e9SFrancesco Ruggeri 	*p = newf;
618956d417b1SBrian Haley 
6190013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6191013d97e9SFrancesco Ruggeri 		rtnl_unlock();
619256d417b1SBrian Haley 		return 0;
619388af182eSEric W. Biederman 	}
619456d417b1SBrian Haley 
619556d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
619656d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
619756d417b1SBrian Haley 		addrconf_disable_change(net, newf);
6198013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
619956d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
620056d417b1SBrian Haley 
620156d417b1SBrian Haley 	rtnl_unlock();
620256d417b1SBrian Haley 	return 0;
620356d417b1SBrian Haley }
620456d417b1SBrian Haley 
620532927393SChristoph Hellwig static int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
620632927393SChristoph Hellwig 		void *buffer, size_t *lenp, loff_t *ppos)
620756d417b1SBrian Haley {
620856d417b1SBrian Haley 	int *valp = ctl->data;
620956d417b1SBrian Haley 	int val = *valp;
621088af182eSEric W. Biederman 	loff_t pos = *ppos;
6211fe2c6338SJoe Perches 	struct ctl_table lctl;
621256d417b1SBrian Haley 	int ret;
621356d417b1SBrian Haley 
6214013d97e9SFrancesco Ruggeri 	/*
6215013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
6216013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
6217013d97e9SFrancesco Ruggeri 	 */
6218013d97e9SFrancesco Ruggeri 	lctl = *ctl;
6219013d97e9SFrancesco Ruggeri 	lctl.data = &val;
6220013d97e9SFrancesco Ruggeri 
6221013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
622256d417b1SBrian Haley 
622356d417b1SBrian Haley 	if (write)
622456d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
622588af182eSEric W. Biederman 	if (ret)
622688af182eSEric W. Biederman 		*ppos = pos;
622756d417b1SBrian Haley 	return ret;
622856d417b1SBrian Haley }
622956d417b1SBrian Haley 
623032927393SChristoph Hellwig static int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
623132927393SChristoph Hellwig 		void *buffer, size_t *lenp, loff_t *ppos)
6232c92d5491Sstephen hemminger {
6233c92d5491Sstephen hemminger 	int *valp = ctl->data;
6234c92d5491Sstephen hemminger 	int ret;
6235c92d5491Sstephen hemminger 	int old, new;
6236c92d5491Sstephen hemminger 
6237c92d5491Sstephen hemminger 	old = *valp;
6238c92d5491Sstephen hemminger 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6239c92d5491Sstephen hemminger 	new = *valp;
6240c92d5491Sstephen hemminger 
6241c92d5491Sstephen hemminger 	if (write && old != new) {
6242c92d5491Sstephen hemminger 		struct net *net = ctl->extra2;
6243c92d5491Sstephen hemminger 
6244c92d5491Sstephen hemminger 		if (!rtnl_trylock())
6245c92d5491Sstephen hemminger 			return restart_syscall();
6246c92d5491Sstephen hemminger 
6247c92d5491Sstephen hemminger 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
624885b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
624985b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
6250c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_DEFAULT,
6251c92d5491Sstephen hemminger 						     net->ipv6.devconf_dflt);
6252c92d5491Sstephen hemminger 		else if (valp == &net->ipv6.devconf_all->proxy_ndp)
625385b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
625485b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
6255c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_ALL,
6256c92d5491Sstephen hemminger 						     net->ipv6.devconf_all);
6257c92d5491Sstephen hemminger 		else {
6258c92d5491Sstephen hemminger 			struct inet6_dev *idev = ctl->extra1;
6259c92d5491Sstephen hemminger 
626085b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
626185b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
6262c92d5491Sstephen hemminger 						     idev->dev->ifindex,
6263c92d5491Sstephen hemminger 						     &idev->cnf);
6264c92d5491Sstephen hemminger 		}
6265c92d5491Sstephen hemminger 		rtnl_unlock();
6266c92d5491Sstephen hemminger 	}
6267c92d5491Sstephen hemminger 
6268c92d5491Sstephen hemminger 	return ret;
6269c92d5491Sstephen hemminger }
6270c92d5491Sstephen hemminger 
6271d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
627232927393SChristoph Hellwig 					 void *buffer, size_t *lenp,
6273d35a00b8SFelix Jia 					 loff_t *ppos)
6274d35a00b8SFelix Jia {
6275d35a00b8SFelix Jia 	int ret = 0;
6276c6dbf7aaSSabrina Dubroca 	u32 new_val;
6277d35a00b8SFelix Jia 	struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6278d35a00b8SFelix Jia 	struct net *net = (struct net *)ctl->extra2;
6279c6dbf7aaSSabrina Dubroca 	struct ctl_table tmp = {
6280c6dbf7aaSSabrina Dubroca 		.data = &new_val,
6281c6dbf7aaSSabrina Dubroca 		.maxlen = sizeof(new_val),
6282c6dbf7aaSSabrina Dubroca 		.mode = ctl->mode,
6283c6dbf7aaSSabrina Dubroca 	};
6284d35a00b8SFelix Jia 
62858c171d6cSFelix Jia 	if (!rtnl_trylock())
62868c171d6cSFelix Jia 		return restart_syscall();
62878c171d6cSFelix Jia 
6288c6dbf7aaSSabrina Dubroca 	new_val = *((u32 *)ctl->data);
6289c6dbf7aaSSabrina Dubroca 
6290c6dbf7aaSSabrina Dubroca 	ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6291c6dbf7aaSSabrina Dubroca 	if (ret != 0)
6292c6dbf7aaSSabrina Dubroca 		goto out;
6293d35a00b8SFelix Jia 
6294d35a00b8SFelix Jia 	if (write) {
62958c171d6cSFelix Jia 		if (check_addr_gen_mode(new_val) < 0) {
62968c171d6cSFelix Jia 			ret = -EINVAL;
62978c171d6cSFelix Jia 			goto out;
62988c171d6cSFelix Jia 		}
6299d35a00b8SFelix Jia 
6300c6dbf7aaSSabrina Dubroca 		if (idev) {
63018c171d6cSFelix Jia 			if (check_stable_privacy(idev, net, new_val) < 0) {
63028c171d6cSFelix Jia 				ret = -EINVAL;
63038c171d6cSFelix Jia 				goto out;
63048c171d6cSFelix Jia 			}
6305d35a00b8SFelix Jia 
6306d35a00b8SFelix Jia 			if (idev->cnf.addr_gen_mode != new_val) {
6307d35a00b8SFelix Jia 				idev->cnf.addr_gen_mode = new_val;
6308d35a00b8SFelix Jia 				addrconf_dev_config(idev->dev);
63098c171d6cSFelix Jia 			}
6310f24c5987SSabrina Dubroca 		} else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6311f24c5987SSabrina Dubroca 			struct net_device *dev;
6312f24c5987SSabrina Dubroca 
6313f24c5987SSabrina Dubroca 			net->ipv6.devconf_dflt->addr_gen_mode = new_val;
6314f24c5987SSabrina Dubroca 			for_each_netdev(net, dev) {
6315f24c5987SSabrina Dubroca 				idev = __in6_dev_get(dev);
6316f24c5987SSabrina Dubroca 				if (idev &&
6317f24c5987SSabrina Dubroca 				    idev->cnf.addr_gen_mode != new_val) {
6318f24c5987SSabrina Dubroca 					idev->cnf.addr_gen_mode = new_val;
6319f24c5987SSabrina Dubroca 					addrconf_dev_config(idev->dev);
6320f24c5987SSabrina Dubroca 				}
6321f24c5987SSabrina Dubroca 			}
63228c171d6cSFelix Jia 		}
6323c6dbf7aaSSabrina Dubroca 
6324c6dbf7aaSSabrina Dubroca 		*((u32 *)ctl->data) = new_val;
63258c171d6cSFelix Jia 	}
63268c171d6cSFelix Jia 
63278c171d6cSFelix Jia out:
6328d35a00b8SFelix Jia 	rtnl_unlock();
6329d35a00b8SFelix Jia 
6330d35a00b8SFelix Jia 	return ret;
6331d35a00b8SFelix Jia }
6332d35a00b8SFelix Jia 
63333d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
633432927393SChristoph Hellwig 					 void *buffer, size_t *lenp,
63353d1bec99SHannes Frederic Sowa 					 loff_t *ppos)
63363d1bec99SHannes Frederic Sowa {
63373d1bec99SHannes Frederic Sowa 	int err;
63383d1bec99SHannes Frederic Sowa 	struct in6_addr addr;
63393d1bec99SHannes Frederic Sowa 	char str[IPV6_MAX_STRLEN];
63403d1bec99SHannes Frederic Sowa 	struct ctl_table lctl = *ctl;
6341622c81d5SHannes Frederic Sowa 	struct net *net = ctl->extra2;
63423d1bec99SHannes Frederic Sowa 	struct ipv6_stable_secret *secret = ctl->data;
63433d1bec99SHannes Frederic Sowa 
6344622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6345622c81d5SHannes Frederic Sowa 		return -EIO;
6346622c81d5SHannes Frederic Sowa 
63473d1bec99SHannes Frederic Sowa 	lctl.maxlen = IPV6_MAX_STRLEN;
63483d1bec99SHannes Frederic Sowa 	lctl.data = str;
63493d1bec99SHannes Frederic Sowa 
63503d1bec99SHannes Frederic Sowa 	if (!rtnl_trylock())
63513d1bec99SHannes Frederic Sowa 		return restart_syscall();
63523d1bec99SHannes Frederic Sowa 
63533d1bec99SHannes Frederic Sowa 	if (!write && !secret->initialized) {
63543d1bec99SHannes Frederic Sowa 		err = -EIO;
63553d1bec99SHannes Frederic Sowa 		goto out;
63563d1bec99SHannes Frederic Sowa 	}
63573d1bec99SHannes Frederic Sowa 
63585449a5caSWANG Cong 	err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
63593d1bec99SHannes Frederic Sowa 	if (err >= sizeof(str)) {
63603d1bec99SHannes Frederic Sowa 		err = -EIO;
63613d1bec99SHannes Frederic Sowa 		goto out;
63623d1bec99SHannes Frederic Sowa 	}
63633d1bec99SHannes Frederic Sowa 
63643d1bec99SHannes Frederic Sowa 	err = proc_dostring(&lctl, write, buffer, lenp, ppos);
63653d1bec99SHannes Frederic Sowa 	if (err || !write)
63663d1bec99SHannes Frederic Sowa 		goto out;
63673d1bec99SHannes Frederic Sowa 
63683d1bec99SHannes Frederic Sowa 	if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
63693d1bec99SHannes Frederic Sowa 		err = -EIO;
63703d1bec99SHannes Frederic Sowa 		goto out;
63713d1bec99SHannes Frederic Sowa 	}
63723d1bec99SHannes Frederic Sowa 
63733d1bec99SHannes Frederic Sowa 	secret->initialized = true;
63743d1bec99SHannes Frederic Sowa 	secret->secret = addr;
63753d1bec99SHannes Frederic Sowa 
6376622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6377622c81d5SHannes Frederic Sowa 		struct net_device *dev;
6378622c81d5SHannes Frederic Sowa 
6379622c81d5SHannes Frederic Sowa 		for_each_netdev(net, dev) {
6380622c81d5SHannes Frederic Sowa 			struct inet6_dev *idev = __in6_dev_get(dev);
6381622c81d5SHannes Frederic Sowa 
6382622c81d5SHannes Frederic Sowa 			if (idev) {
6383d35a00b8SFelix Jia 				idev->cnf.addr_gen_mode =
6384622c81d5SHannes Frederic Sowa 					IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6385622c81d5SHannes Frederic Sowa 			}
6386622c81d5SHannes Frederic Sowa 		}
6387622c81d5SHannes Frederic Sowa 	} else {
6388622c81d5SHannes Frederic Sowa 		struct inet6_dev *idev = ctl->extra1;
6389622c81d5SHannes Frederic Sowa 
6390d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6391622c81d5SHannes Frederic Sowa 	}
6392622c81d5SHannes Frederic Sowa 
63933d1bec99SHannes Frederic Sowa out:
63943d1bec99SHannes Frederic Sowa 	rtnl_unlock();
63953d1bec99SHannes Frederic Sowa 
63963d1bec99SHannes Frederic Sowa 	return err;
63973d1bec99SHannes Frederic Sowa }
6398c92d5491Sstephen hemminger 
639935103d11SAndy Gospodarek static
640035103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
640132927393SChristoph Hellwig 						int write, void *buffer,
640235103d11SAndy Gospodarek 						size_t *lenp,
640335103d11SAndy Gospodarek 						loff_t *ppos)
640435103d11SAndy Gospodarek {
640535103d11SAndy Gospodarek 	int *valp = ctl->data;
640635103d11SAndy Gospodarek 	int val = *valp;
640735103d11SAndy Gospodarek 	loff_t pos = *ppos;
640835103d11SAndy Gospodarek 	struct ctl_table lctl;
640935103d11SAndy Gospodarek 	int ret;
641035103d11SAndy Gospodarek 
641135103d11SAndy Gospodarek 	/* ctl->data points to idev->cnf.ignore_routes_when_linkdown
641235103d11SAndy Gospodarek 	 * we should not modify it until we get the rtnl lock.
641335103d11SAndy Gospodarek 	 */
641435103d11SAndy Gospodarek 	lctl = *ctl;
641535103d11SAndy Gospodarek 	lctl.data = &val;
641635103d11SAndy Gospodarek 
641735103d11SAndy Gospodarek 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
641835103d11SAndy Gospodarek 
641935103d11SAndy Gospodarek 	if (write)
642035103d11SAndy Gospodarek 		ret = addrconf_fixup_linkdown(ctl, valp, val);
642135103d11SAndy Gospodarek 	if (ret)
642235103d11SAndy Gospodarek 		*ppos = pos;
642335103d11SAndy Gospodarek 	return ret;
642435103d11SAndy Gospodarek }
642535103d11SAndy Gospodarek 
6426df789fe7SDavid Forster static
6427df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6428df789fe7SDavid Forster {
6429df789fe7SDavid Forster 	if (rt) {
6430df789fe7SDavid Forster 		if (action)
6431df789fe7SDavid Forster 			rt->dst.flags |= DST_NOPOLICY;
6432df789fe7SDavid Forster 		else
6433df789fe7SDavid Forster 			rt->dst.flags &= ~DST_NOPOLICY;
6434df789fe7SDavid Forster 	}
6435df789fe7SDavid Forster }
6436df789fe7SDavid Forster 
6437df789fe7SDavid Forster static
6438df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6439df789fe7SDavid Forster {
6440df789fe7SDavid Forster 	struct inet6_ifaddr *ifa;
6441df789fe7SDavid Forster 
6442df789fe7SDavid Forster 	read_lock_bh(&idev->lock);
6443df789fe7SDavid Forster 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
6444df789fe7SDavid Forster 		spin_lock(&ifa->lock);
6445df789fe7SDavid Forster 		if (ifa->rt) {
6446f88d8ea6SDavid Ahern 			/* host routes only use builtin fib6_nh */
64471cf844c7SDavid Ahern 			struct fib6_nh *nh = ifa->rt->fib6_nh;
6448df789fe7SDavid Forster 			int cpu;
6449df789fe7SDavid Forster 
645066f5d6ceSWei Wang 			rcu_read_lock();
64513b6761d1SDavid Ahern 			ifa->rt->dst_nopolicy = val ? true : false;
6452f40b6ae2SDavid Ahern 			if (nh->rt6i_pcpu) {
6453df789fe7SDavid Forster 				for_each_possible_cpu(cpu) {
6454df789fe7SDavid Forster 					struct rt6_info **rtp;
6455df789fe7SDavid Forster 
6456f40b6ae2SDavid Ahern 					rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
6457df789fe7SDavid Forster 					addrconf_set_nopolicy(*rtp, val);
6458df789fe7SDavid Forster 				}
6459df789fe7SDavid Forster 			}
646066f5d6ceSWei Wang 			rcu_read_unlock();
6461df789fe7SDavid Forster 		}
6462df789fe7SDavid Forster 		spin_unlock(&ifa->lock);
6463df789fe7SDavid Forster 	}
6464df789fe7SDavid Forster 	read_unlock_bh(&idev->lock);
6465df789fe7SDavid Forster }
6466df789fe7SDavid Forster 
6467df789fe7SDavid Forster static
6468df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
6469df789fe7SDavid Forster {
6470df789fe7SDavid Forster 	struct inet6_dev *idev;
6471df789fe7SDavid Forster 	struct net *net;
6472df789fe7SDavid Forster 
6473df789fe7SDavid Forster 	if (!rtnl_trylock())
6474df789fe7SDavid Forster 		return restart_syscall();
6475df789fe7SDavid Forster 
6476df789fe7SDavid Forster 	*valp = val;
6477df789fe7SDavid Forster 
6478df789fe7SDavid Forster 	net = (struct net *)ctl->extra2;
6479df789fe7SDavid Forster 	if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6480df789fe7SDavid Forster 		rtnl_unlock();
6481df789fe7SDavid Forster 		return 0;
6482df789fe7SDavid Forster 	}
6483df789fe7SDavid Forster 
6484df789fe7SDavid Forster 	if (valp == &net->ipv6.devconf_all->disable_policy)  {
6485df789fe7SDavid Forster 		struct net_device *dev;
6486df789fe7SDavid Forster 
6487df789fe7SDavid Forster 		for_each_netdev(net, dev) {
6488df789fe7SDavid Forster 			idev = __in6_dev_get(dev);
6489df789fe7SDavid Forster 			if (idev)
6490df789fe7SDavid Forster 				addrconf_disable_policy_idev(idev, val);
6491df789fe7SDavid Forster 		}
6492df789fe7SDavid Forster 	} else {
6493df789fe7SDavid Forster 		idev = (struct inet6_dev *)ctl->extra1;
6494df789fe7SDavid Forster 		addrconf_disable_policy_idev(idev, val);
6495df789fe7SDavid Forster 	}
6496df789fe7SDavid Forster 
6497df789fe7SDavid Forster 	rtnl_unlock();
6498df789fe7SDavid Forster 	return 0;
6499df789fe7SDavid Forster }
6500df789fe7SDavid Forster 
650132927393SChristoph Hellwig static int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
650232927393SChristoph Hellwig 				   void *buffer, size_t *lenp, loff_t *ppos)
6503df789fe7SDavid Forster {
6504df789fe7SDavid Forster 	int *valp = ctl->data;
6505df789fe7SDavid Forster 	int val = *valp;
6506df789fe7SDavid Forster 	loff_t pos = *ppos;
6507df789fe7SDavid Forster 	struct ctl_table lctl;
6508df789fe7SDavid Forster 	int ret;
6509df789fe7SDavid Forster 
6510df789fe7SDavid Forster 	lctl = *ctl;
6511df789fe7SDavid Forster 	lctl.data = &val;
6512df789fe7SDavid Forster 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6513df789fe7SDavid Forster 
6514df789fe7SDavid Forster 	if (write && (*valp != val))
6515df789fe7SDavid Forster 		ret = addrconf_disable_policy(ctl, valp, val);
6516df789fe7SDavid Forster 
6517df789fe7SDavid Forster 	if (ret)
6518df789fe7SDavid Forster 		*ppos = pos;
6519df789fe7SDavid Forster 
6520df789fe7SDavid Forster 	return ret;
6521df789fe7SDavid Forster }
6522df789fe7SDavid Forster 
6523cb4a4c69SMaciej Żenczykowski static int minus_one = -1;
6524cb9e684eSMaciej Żenczykowski static const int two_five_five = 255;
6525cb9e684eSMaciej Żenczykowski 
6526607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = {
65271da177e4SLinus Torvalds 	{
65281da177e4SLinus Torvalds 		.procname	= "forwarding",
65291da177e4SLinus Torvalds 		.data		= &ipv6_devconf.forwarding,
65301da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
65311da177e4SLinus Torvalds 		.mode		= 0644,
65326d9f239aSAlexey Dobriyan 		.proc_handler	= addrconf_sysctl_forward,
65331da177e4SLinus Torvalds 	},
65341da177e4SLinus Torvalds 	{
65351da177e4SLinus Torvalds 		.procname	= "hop_limit",
65361da177e4SLinus Torvalds 		.data		= &ipv6_devconf.hop_limit,
65371da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
65381da177e4SLinus Torvalds 		.mode		= 0644,
6539cb9e684eSMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
6540eec4844fSMatteo Croce 		.extra1		= (void *)SYSCTL_ONE,
6541cb9e684eSMaciej Żenczykowski 		.extra2		= (void *)&two_five_five,
65421da177e4SLinus Torvalds 	},
65431da177e4SLinus Torvalds 	{
65441da177e4SLinus Torvalds 		.procname	= "mtu",
65451da177e4SLinus Torvalds 		.data		= &ipv6_devconf.mtu6,
65461da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
65471da177e4SLinus Torvalds 		.mode		= 0644,
654877751427SMarcelo Leitner 		.proc_handler	= addrconf_sysctl_mtu,
65491da177e4SLinus Torvalds 	},
65501da177e4SLinus Torvalds 	{
65511da177e4SLinus Torvalds 		.procname	= "accept_ra",
65521da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_ra,
65531da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
65541da177e4SLinus Torvalds 		.mode		= 0644,
65556d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
65561da177e4SLinus Torvalds 	},
65571da177e4SLinus Torvalds 	{
65581da177e4SLinus Torvalds 		.procname	= "accept_redirects",
65591da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_redirects,
65601da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
65611da177e4SLinus Torvalds 		.mode		= 0644,
65626d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
65631da177e4SLinus Torvalds 	},
65641da177e4SLinus Torvalds 	{
65651da177e4SLinus Torvalds 		.procname	= "autoconf",
65661da177e4SLinus Torvalds 		.data		= &ipv6_devconf.autoconf,
65671da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
65681da177e4SLinus Torvalds 		.mode		= 0644,
65696d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
65701da177e4SLinus Torvalds 	},
65711da177e4SLinus Torvalds 	{
65721da177e4SLinus Torvalds 		.procname	= "dad_transmits",
65731da177e4SLinus Torvalds 		.data		= &ipv6_devconf.dad_transmits,
65741da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
65751da177e4SLinus Torvalds 		.mode		= 0644,
65766d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
65771da177e4SLinus Torvalds 	},
65781da177e4SLinus Torvalds 	{
65791da177e4SLinus Torvalds 		.procname	= "router_solicitations",
65801da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicits,
65811da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
65821da177e4SLinus Torvalds 		.mode		= 0644,
6583cb4a4c69SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
6584cb4a4c69SMaciej Żenczykowski 		.extra1		= &minus_one,
65851da177e4SLinus Torvalds 	},
65861da177e4SLinus Torvalds 	{
65871da177e4SLinus Torvalds 		.procname	= "router_solicitation_interval",
65881da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_interval,
65891da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
65901da177e4SLinus Torvalds 		.mode		= 0644,
65916d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
65921da177e4SLinus Torvalds 	},
65931da177e4SLinus Torvalds 	{
6594bd11f074SMaciej Żenczykowski 		.procname	= "router_solicitation_max_interval",
6595bd11f074SMaciej Żenczykowski 		.data		= &ipv6_devconf.rtr_solicit_max_interval,
6596bd11f074SMaciej Żenczykowski 		.maxlen		= sizeof(int),
6597bd11f074SMaciej Żenczykowski 		.mode		= 0644,
6598bd11f074SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_jiffies,
6599bd11f074SMaciej Żenczykowski 	},
6600bd11f074SMaciej Żenczykowski 	{
66011da177e4SLinus Torvalds 		.procname	= "router_solicitation_delay",
66021da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_delay,
66031da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66041da177e4SLinus Torvalds 		.mode		= 0644,
66056d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
66061da177e4SLinus Torvalds 	},
66071da177e4SLinus Torvalds 	{
66081da177e4SLinus Torvalds 		.procname	= "force_mld_version",
66091da177e4SLinus Torvalds 		.data		= &ipv6_devconf.force_mld_version,
66101da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66111da177e4SLinus Torvalds 		.mode		= 0644,
66126d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66131da177e4SLinus Torvalds 	},
6614fc4eba58SHannes Frederic Sowa 	{
6615fc4eba58SHannes Frederic Sowa 		.procname	= "mldv1_unsolicited_report_interval",
6616fc4eba58SHannes Frederic Sowa 		.data		=
6617fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv1_unsolicited_report_interval,
6618fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6619fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
6620fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
6621fc4eba58SHannes Frederic Sowa 	},
6622fc4eba58SHannes Frederic Sowa 	{
6623fc4eba58SHannes Frederic Sowa 		.procname	= "mldv2_unsolicited_report_interval",
6624fc4eba58SHannes Frederic Sowa 		.data		=
6625fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv2_unsolicited_report_interval,
6626fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6627fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
6628fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
6629fc4eba58SHannes Frederic Sowa 	},
66301da177e4SLinus Torvalds 	{
66311da177e4SLinus Torvalds 		.procname	= "use_tempaddr",
66321da177e4SLinus Torvalds 		.data		= &ipv6_devconf.use_tempaddr,
66331da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66341da177e4SLinus Torvalds 		.mode		= 0644,
66356d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66361da177e4SLinus Torvalds 	},
66371da177e4SLinus Torvalds 	{
66381da177e4SLinus Torvalds 		.procname	= "temp_valid_lft",
66391da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_valid_lft,
66401da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66411da177e4SLinus Torvalds 		.mode		= 0644,
66426d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66431da177e4SLinus Torvalds 	},
66441da177e4SLinus Torvalds 	{
66451da177e4SLinus Torvalds 		.procname	= "temp_prefered_lft",
66461da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_prefered_lft,
66471da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66481da177e4SLinus Torvalds 		.mode		= 0644,
66496d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66501da177e4SLinus Torvalds 	},
66511da177e4SLinus Torvalds 	{
66521da177e4SLinus Torvalds 		.procname	= "regen_max_retry",
66531da177e4SLinus Torvalds 		.data		= &ipv6_devconf.regen_max_retry,
66541da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66551da177e4SLinus Torvalds 		.mode		= 0644,
66566d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66571da177e4SLinus Torvalds 	},
66581da177e4SLinus Torvalds 	{
66591da177e4SLinus Torvalds 		.procname	= "max_desync_factor",
66601da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_desync_factor,
66611da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66621da177e4SLinus Torvalds 		.mode		= 0644,
66636d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66641da177e4SLinus Torvalds 	},
66651da177e4SLinus Torvalds 	{
66661da177e4SLinus Torvalds 		.procname	= "max_addresses",
66671da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_addresses,
66681da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66691da177e4SLinus Torvalds 		.mode		= 0644,
66706d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66711da177e4SLinus Torvalds 	},
66721da177e4SLinus Torvalds 	{
667365f5c7c1SYOSHIFUJI Hideaki 		.procname	= "accept_ra_defrtr",
667465f5c7c1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_defrtr,
667565f5c7c1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
667665f5c7c1SYOSHIFUJI Hideaki 		.mode		= 0644,
66776d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
667865f5c7c1SYOSHIFUJI Hideaki 	},
667965f5c7c1SYOSHIFUJI Hideaki 	{
66808013d1d7SHangbin Liu 		.procname	= "accept_ra_min_hop_limit",
66818013d1d7SHangbin Liu 		.data		= &ipv6_devconf.accept_ra_min_hop_limit,
66828013d1d7SHangbin Liu 		.maxlen		= sizeof(int),
66838013d1d7SHangbin Liu 		.mode		= 0644,
66848013d1d7SHangbin Liu 		.proc_handler	= proc_dointvec,
66858013d1d7SHangbin Liu 	},
66868013d1d7SHangbin Liu 	{
6687c4fd30ebSYOSHIFUJI Hideaki 		.procname	= "accept_ra_pinfo",
6688c4fd30ebSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_pinfo,
6689c4fd30ebSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6690c4fd30ebSYOSHIFUJI Hideaki 		.mode		= 0644,
66916d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
6692c4fd30ebSYOSHIFUJI Hideaki 	},
6693930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
6694930d6ff2SYOSHIFUJI Hideaki 	{
6695930d6ff2SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rtr_pref",
6696930d6ff2SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rtr_pref,
6697930d6ff2SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6698930d6ff2SYOSHIFUJI Hideaki 		.mode		= 0644,
66996d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
6700930d6ff2SYOSHIFUJI Hideaki 	},
670152e16356SYOSHIFUJI Hideaki 	{
670252e16356SYOSHIFUJI Hideaki 		.procname	= "router_probe_interval",
670352e16356SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.rtr_probe_interval,
670452e16356SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
670552e16356SYOSHIFUJI Hideaki 		.mode		= 0644,
67066d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
670752e16356SYOSHIFUJI Hideaki 	},
6708fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
670909c884d4SYOSHIFUJI Hideaki 	{
6710bbea124bSJoel Scherpelz 		.procname	= "accept_ra_rt_info_min_plen",
6711bbea124bSJoel Scherpelz 		.data		= &ipv6_devconf.accept_ra_rt_info_min_plen,
6712bbea124bSJoel Scherpelz 		.maxlen		= sizeof(int),
6713bbea124bSJoel Scherpelz 		.mode		= 0644,
6714bbea124bSJoel Scherpelz 		.proc_handler	= proc_dointvec,
6715bbea124bSJoel Scherpelz 	},
6716bbea124bSJoel Scherpelz 	{
671709c884d4SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rt_info_max_plen",
671809c884d4SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
671909c884d4SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
672009c884d4SYOSHIFUJI Hideaki 		.mode		= 0644,
67216d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
672209c884d4SYOSHIFUJI Hideaki 	},
672309c884d4SYOSHIFUJI Hideaki #endif
6724930d6ff2SYOSHIFUJI Hideaki #endif
6725c4fd30ebSYOSHIFUJI Hideaki 	{
6726fbea49e1SYOSHIFUJI Hideaki 		.procname	= "proxy_ndp",
6727fbea49e1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.proxy_ndp,
6728fbea49e1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6729fbea49e1SYOSHIFUJI Hideaki 		.mode		= 0644,
6730c92d5491Sstephen hemminger 		.proc_handler	= addrconf_sysctl_proxy_ndp,
6731fbea49e1SYOSHIFUJI Hideaki 	},
6732fbea49e1SYOSHIFUJI Hideaki 	{
67330bcbc926SYOSHIFUJI Hideaki 		.procname	= "accept_source_route",
67340bcbc926SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_source_route,
67350bcbc926SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
67360bcbc926SYOSHIFUJI Hideaki 		.mode		= 0644,
67376d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
67380bcbc926SYOSHIFUJI Hideaki 	},
673995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
674095c385b4SNeil Horman 	{
674195c385b4SNeil Horman 		.procname	= "optimistic_dad",
674295c385b4SNeil Horman 		.data		= &ipv6_devconf.optimistic_dad,
674395c385b4SNeil Horman 		.maxlen		= sizeof(int),
674495c385b4SNeil Horman 		.mode		= 0644,
67456d9f239aSAlexey Dobriyan 		.proc_handler   = proc_dointvec,
674695c385b4SNeil Horman 	},
67477fd2561eSErik Kline 	{
67487fd2561eSErik Kline 		.procname	= "use_optimistic",
67497fd2561eSErik Kline 		.data		= &ipv6_devconf.use_optimistic,
67507fd2561eSErik Kline 		.maxlen		= sizeof(int),
67517fd2561eSErik Kline 		.mode		= 0644,
67527fd2561eSErik Kline 		.proc_handler	= proc_dointvec,
67537fd2561eSErik Kline 	},
675495c385b4SNeil Horman #endif
67557bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
67567bc570c8SYOSHIFUJI Hideaki 	{
67577bc570c8SYOSHIFUJI Hideaki 		.procname	= "mc_forwarding",
67587bc570c8SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.mc_forwarding,
67597bc570c8SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6760a4e6db07SDavid S. Miller 		.mode		= 0444,
67616d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
67627bc570c8SYOSHIFUJI Hideaki 	},
67637bc570c8SYOSHIFUJI Hideaki #endif
67640bcbc926SYOSHIFUJI Hideaki 	{
6765778d80beSYOSHIFUJI Hideaki 		.procname	= "disable_ipv6",
6766778d80beSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.disable_ipv6,
6767778d80beSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6768778d80beSYOSHIFUJI Hideaki 		.mode		= 0644,
676956d417b1SBrian Haley 		.proc_handler	= addrconf_sysctl_disable,
6770778d80beSYOSHIFUJI Hideaki 	},
6771778d80beSYOSHIFUJI Hideaki 	{
67721b34be74SYOSHIFUJI Hideaki 		.procname	= "accept_dad",
67731b34be74SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_dad,
67741b34be74SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
67751b34be74SYOSHIFUJI Hideaki 		.mode		= 0644,
67766d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
67771b34be74SYOSHIFUJI Hideaki 	},
67781b34be74SYOSHIFUJI Hideaki 	{
6779f7734fdfSOctavian Purdila 		.procname	= "force_tllao",
6780f7734fdfSOctavian Purdila 		.data		= &ipv6_devconf.force_tllao,
6781f7734fdfSOctavian Purdila 		.maxlen		= sizeof(int),
6782f7734fdfSOctavian Purdila 		.mode		= 0644,
6783f7734fdfSOctavian Purdila 		.proc_handler	= proc_dointvec
6784f7734fdfSOctavian Purdila 	},
6785f7734fdfSOctavian Purdila 	{
67865cb04436SHannes Frederic Sowa 		.procname	= "ndisc_notify",
67875cb04436SHannes Frederic Sowa 		.data		= &ipv6_devconf.ndisc_notify,
67885cb04436SHannes Frederic Sowa 		.maxlen		= sizeof(int),
67895cb04436SHannes Frederic Sowa 		.mode		= 0644,
67905cb04436SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
67915cb04436SHannes Frederic Sowa 	},
67925cb04436SHannes Frederic Sowa 	{
6793b800c3b9SHannes Frederic Sowa 		.procname	= "suppress_frag_ndisc",
6794b800c3b9SHannes Frederic Sowa 		.data		= &ipv6_devconf.suppress_frag_ndisc,
6795b800c3b9SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6796b800c3b9SHannes Frederic Sowa 		.mode		= 0644,
6797b800c3b9SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
6798b800c3b9SHannes Frederic Sowa 	},
6799b800c3b9SHannes Frederic Sowa 	{
6800d9333196SBen Greear 		.procname	= "accept_ra_from_local",
6801d9333196SBen Greear 		.data		= &ipv6_devconf.accept_ra_from_local,
6802d9333196SBen Greear 		.maxlen		= sizeof(int),
6803d9333196SBen Greear 		.mode		= 0644,
6804d9333196SBen Greear 		.proc_handler	= proc_dointvec,
6805d9333196SBen Greear 	},
6806d9333196SBen Greear 	{
6807c2943f14SHarout Hedeshian 		.procname	= "accept_ra_mtu",
6808c2943f14SHarout Hedeshian 		.data		= &ipv6_devconf.accept_ra_mtu,
6809c2943f14SHarout Hedeshian 		.maxlen		= sizeof(int),
6810c2943f14SHarout Hedeshian 		.mode		= 0644,
6811c2943f14SHarout Hedeshian 		.proc_handler	= proc_dointvec,
6812c2943f14SHarout Hedeshian 	},
6813c2943f14SHarout Hedeshian 	{
68143d1bec99SHannes Frederic Sowa 		.procname	= "stable_secret",
68153d1bec99SHannes Frederic Sowa 		.data		= &ipv6_devconf.stable_secret,
68163d1bec99SHannes Frederic Sowa 		.maxlen		= IPV6_MAX_STRLEN,
68173d1bec99SHannes Frederic Sowa 		.mode		= 0600,
68183d1bec99SHannes Frederic Sowa 		.proc_handler	= addrconf_sysctl_stable_secret,
68193d1bec99SHannes Frederic Sowa 	},
68203d1bec99SHannes Frederic Sowa 	{
68213985e8a3SErik Kline 		.procname	= "use_oif_addrs_only",
68223985e8a3SErik Kline 		.data		= &ipv6_devconf.use_oif_addrs_only,
68233985e8a3SErik Kline 		.maxlen		= sizeof(int),
68243985e8a3SErik Kline 		.mode		= 0644,
68253985e8a3SErik Kline 		.proc_handler	= proc_dointvec,
682635103d11SAndy Gospodarek 	},
682735103d11SAndy Gospodarek 	{
682835103d11SAndy Gospodarek 		.procname	= "ignore_routes_with_linkdown",
682935103d11SAndy Gospodarek 		.data		= &ipv6_devconf.ignore_routes_with_linkdown,
683035103d11SAndy Gospodarek 		.maxlen		= sizeof(int),
683135103d11SAndy Gospodarek 		.mode		= 0644,
683235103d11SAndy Gospodarek 		.proc_handler	= addrconf_sysctl_ignore_routes_with_linkdown,
68333985e8a3SErik Kline 	},
68343985e8a3SErik Kline 	{
6835abbc3043SJohannes Berg 		.procname	= "drop_unicast_in_l2_multicast",
6836abbc3043SJohannes Berg 		.data		= &ipv6_devconf.drop_unicast_in_l2_multicast,
6837abbc3043SJohannes Berg 		.maxlen		= sizeof(int),
6838abbc3043SJohannes Berg 		.mode		= 0644,
6839abbc3043SJohannes Berg 		.proc_handler	= proc_dointvec,
6840abbc3043SJohannes Berg 	},
6841abbc3043SJohannes Berg 	{
68427a02bf89SJohannes Berg 		.procname	= "drop_unsolicited_na",
68437a02bf89SJohannes Berg 		.data		= &ipv6_devconf.drop_unsolicited_na,
68447a02bf89SJohannes Berg 		.maxlen		= sizeof(int),
68457a02bf89SJohannes Berg 		.mode		= 0644,
68467a02bf89SJohannes Berg 		.proc_handler	= proc_dointvec,
68477a02bf89SJohannes Berg 	},
68487a02bf89SJohannes Berg 	{
6849f1705ec1SDavid Ahern 		.procname	= "keep_addr_on_down",
6850f1705ec1SDavid Ahern 		.data		= &ipv6_devconf.keep_addr_on_down,
6851f1705ec1SDavid Ahern 		.maxlen		= sizeof(int),
6852f1705ec1SDavid Ahern 		.mode		= 0644,
6853f1705ec1SDavid Ahern 		.proc_handler	= proc_dointvec,
6854f1705ec1SDavid Ahern 
6855f1705ec1SDavid Ahern 	},
6856f1705ec1SDavid Ahern 	{
68571ababebaSDavid Lebrun 		.procname	= "seg6_enabled",
68581ababebaSDavid Lebrun 		.data		= &ipv6_devconf.seg6_enabled,
68591ababebaSDavid Lebrun 		.maxlen		= sizeof(int),
68601ababebaSDavid Lebrun 		.mode		= 0644,
68611ababebaSDavid Lebrun 		.proc_handler	= proc_dointvec,
68621ababebaSDavid Lebrun 	},
6863bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
6864bf355b8dSDavid Lebrun 	{
6865bf355b8dSDavid Lebrun 		.procname	= "seg6_require_hmac",
6866bf355b8dSDavid Lebrun 		.data		= &ipv6_devconf.seg6_require_hmac,
6867bf355b8dSDavid Lebrun 		.maxlen		= sizeof(int),
6868bf355b8dSDavid Lebrun 		.mode		= 0644,
6869bf355b8dSDavid Lebrun 		.proc_handler	= proc_dointvec,
6870bf355b8dSDavid Lebrun 	},
6871bf355b8dSDavid Lebrun #endif
68721ababebaSDavid Lebrun 	{
6873adc176c5SErik Nordmark 		.procname       = "enhanced_dad",
6874adc176c5SErik Nordmark 		.data           = &ipv6_devconf.enhanced_dad,
6875adc176c5SErik Nordmark 		.maxlen         = sizeof(int),
6876adc176c5SErik Nordmark 		.mode           = 0644,
6877adc176c5SErik Nordmark 		.proc_handler   = proc_dointvec,
6878adc176c5SErik Nordmark 	},
6879adc176c5SErik Nordmark 	{
6880d35a00b8SFelix Jia 		.procname		= "addr_gen_mode",
6881d35a00b8SFelix Jia 		.data			= &ipv6_devconf.addr_gen_mode,
6882d35a00b8SFelix Jia 		.maxlen			= sizeof(int),
6883d35a00b8SFelix Jia 		.mode			= 0644,
6884d35a00b8SFelix Jia 		.proc_handler	= addrconf_sysctl_addr_gen_mode,
6885d35a00b8SFelix Jia 	},
6886d35a00b8SFelix Jia 	{
6887df789fe7SDavid Forster 		.procname       = "disable_policy",
6888df789fe7SDavid Forster 		.data           = &ipv6_devconf.disable_policy,
6889df789fe7SDavid Forster 		.maxlen         = sizeof(int),
6890df789fe7SDavid Forster 		.mode           = 0644,
6891df789fe7SDavid Forster 		.proc_handler   = addrconf_sysctl_disable_policy,
6892df789fe7SDavid Forster 	},
6893df789fe7SDavid Forster 	{
68942210d6b2SMaciej Żenczykowski 		.procname	= "ndisc_tclass",
68952210d6b2SMaciej Żenczykowski 		.data		= &ipv6_devconf.ndisc_tclass,
68962210d6b2SMaciej Żenczykowski 		.maxlen		= sizeof(int),
68972210d6b2SMaciej Żenczykowski 		.mode		= 0644,
68982210d6b2SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
6899eec4844fSMatteo Croce 		.extra1		= (void *)SYSCTL_ZERO,
69002210d6b2SMaciej Żenczykowski 		.extra2		= (void *)&two_five_five,
69012210d6b2SMaciej Żenczykowski 	},
69022210d6b2SMaciej Żenczykowski 	{
69038610c7c6SAlexander Aring 		.procname	= "rpl_seg_enabled",
69048610c7c6SAlexander Aring 		.data		= &ipv6_devconf.rpl_seg_enabled,
69058610c7c6SAlexander Aring 		.maxlen		= sizeof(int),
69068610c7c6SAlexander Aring 		.mode		= 0644,
69078610c7c6SAlexander Aring 		.proc_handler	= proc_dointvec,
69088610c7c6SAlexander Aring 	},
69098610c7c6SAlexander Aring 	{
6910f8572d8fSEric W. Biederman 		/* sentinel */
69111da177e4SLinus Torvalds 	}
69121da177e4SLinus Torvalds };
69131da177e4SLinus Torvalds 
6914bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6915f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
69161da177e4SLinus Torvalds {
691729c994e3SNicolas Dichtel 	int i, ifindex;
6918607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
69196105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
69201dab6222SPavel Emelyanov 
6921607ea7cdSKonstantin Khlebnikov 	table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6922607ea7cdSKonstantin Khlebnikov 	if (!table)
6923f68635e6SPavel Emelyanov 		goto out;
6924f68635e6SPavel Emelyanov 
6925607ea7cdSKonstantin Khlebnikov 	for (i = 0; table[i].data; i++) {
6926607ea7cdSKonstantin Khlebnikov 		table[i].data += (char *)p - (char *)&ipv6_devconf;
6927cb9e684eSMaciej Żenczykowski 		/* If one of these is already set, then it is not safe to
6928cb9e684eSMaciej Żenczykowski 		 * overwrite either of them: this makes proc_dointvec_minmax
6929cb9e684eSMaciej Żenczykowski 		 * usable.
6930cb9e684eSMaciej Żenczykowski 		 */
6931cb9e684eSMaciej Żenczykowski 		if (!table[i].extra1 && !table[i].extra2) {
6932607ea7cdSKonstantin Khlebnikov 			table[i].extra1 = idev; /* embedded; no ref */
6933607ea7cdSKonstantin Khlebnikov 			table[i].extra2 = net;
69341da177e4SLinus Torvalds 		}
6935cb9e684eSMaciej Żenczykowski 	}
69361da177e4SLinus Torvalds 
69376105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
69381da177e4SLinus Torvalds 
6939607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = register_net_sysctl(net, path, table);
6940607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
69416105e293SEric W. Biederman 		goto free;
6942f68635e6SPavel Emelyanov 
694329c994e3SNicolas Dichtel 	if (!strcmp(dev_name, "all"))
694429c994e3SNicolas Dichtel 		ifindex = NETCONFA_IFINDEX_ALL;
694529c994e3SNicolas Dichtel 	else if (!strcmp(dev_name, "default"))
694629c994e3SNicolas Dichtel 		ifindex = NETCONFA_IFINDEX_DEFAULT;
694729c994e3SNicolas Dichtel 	else
694829c994e3SNicolas Dichtel 		ifindex = idev->dev->ifindex;
694985b3daadSDavid Ahern 	inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
695085b3daadSDavid Ahern 				     ifindex, p);
695195897312SPavel Emelyanov 	return 0;
69521da177e4SLinus Torvalds 
69531da177e4SLinus Torvalds free:
6954607ea7cdSKonstantin Khlebnikov 	kfree(table);
6955f68635e6SPavel Emelyanov out:
695695897312SPavel Emelyanov 	return -ENOBUFS;
69571da177e4SLinus Torvalds }
69581da177e4SLinus Torvalds 
695923452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net,
696023452170SDavid Ahern 					 struct ipv6_devconf *p, int ifindex)
6961f52295a9SPavel Emelyanov {
6962607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
6963f52295a9SPavel Emelyanov 
6964607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
6965408c4768SPavel Emelyanov 		return;
6966408c4768SPavel Emelyanov 
6967607ea7cdSKonstantin Khlebnikov 	table = p->sysctl_header->ctl_table_arg;
6968607ea7cdSKonstantin Khlebnikov 	unregister_net_sysctl_table(p->sysctl_header);
6969607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = NULL;
6970607ea7cdSKonstantin Khlebnikov 	kfree(table);
697123452170SDavid Ahern 
697223452170SDavid Ahern 	inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
69731da177e4SLinus Torvalds }
6974408c4768SPavel Emelyanov 
6975a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev)
6976408c4768SPavel Emelyanov {
6977a317a2f1SWANG Cong 	int err;
6978a317a2f1SWANG Cong 
6979a317a2f1SWANG Cong 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
6980a317a2f1SWANG Cong 		return -EINVAL;
6981a317a2f1SWANG Cong 
6982a317a2f1SWANG Cong 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6983f8572d8fSEric W. Biederman 				    &ndisc_ifinfo_sysctl_change);
6984a317a2f1SWANG Cong 	if (err)
6985a317a2f1SWANG Cong 		return err;
6986a317a2f1SWANG Cong 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6987f8572d8fSEric W. Biederman 					 idev, &idev->cnf);
6988a317a2f1SWANG Cong 	if (err)
6989a317a2f1SWANG Cong 		neigh_sysctl_unregister(idev->nd_parms);
6990a317a2f1SWANG Cong 
6991a317a2f1SWANG Cong 	return err;
6992408c4768SPavel Emelyanov }
6993408c4768SPavel Emelyanov 
6994408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6995408c4768SPavel Emelyanov {
699623452170SDavid Ahern 	__addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
699723452170SDavid Ahern 				     idev->dev->ifindex);
6998408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
69991da177e4SLinus Torvalds }
70001da177e4SLinus Torvalds 
70011da177e4SLinus Torvalds 
70021da177e4SLinus Torvalds #endif
70031da177e4SLinus Torvalds 
70042c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
7005e0da5a48SPavel Emelyanov {
7006a79ca223SHong Zhiguo 	int err = -ENOMEM;
7007e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
7008e0da5a48SPavel Emelyanov 
7009a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
701063159f29SIan Morris 	if (!all)
7011e0da5a48SPavel Emelyanov 		goto err_alloc_all;
7012e0da5a48SPavel Emelyanov 
7013a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
701463159f29SIan Morris 	if (!dflt)
7015e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
7016a79ca223SHong Zhiguo 
7017a154d5d8SArnd Bergmann 	if (IS_ENABLED(CONFIG_SYSCTL) &&
70189efd6a3cSNicolas Dichtel 	    !net_eq(net, &init_net)) {
70199efd6a3cSNicolas Dichtel 		switch (sysctl_devconf_inherit_init_net) {
70209efd6a3cSNicolas Dichtel 		case 1:  /* copy from init_net */
70219efd6a3cSNicolas Dichtel 			memcpy(all, init_net.ipv6.devconf_all,
70229efd6a3cSNicolas Dichtel 			       sizeof(ipv6_devconf));
70239efd6a3cSNicolas Dichtel 			memcpy(dflt, init_net.ipv6.devconf_dflt,
70249efd6a3cSNicolas Dichtel 			       sizeof(ipv6_devconf_dflt));
70259efd6a3cSNicolas Dichtel 			break;
70269efd6a3cSNicolas Dichtel 		case 3: /* copy from the current netns */
70279efd6a3cSNicolas Dichtel 			memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all,
70289efd6a3cSNicolas Dichtel 			       sizeof(ipv6_devconf));
70299efd6a3cSNicolas Dichtel 			memcpy(dflt,
70309efd6a3cSNicolas Dichtel 			       current->nsproxy->net_ns->ipv6.devconf_dflt,
70319efd6a3cSNicolas Dichtel 			       sizeof(ipv6_devconf_dflt));
70329efd6a3cSNicolas Dichtel 			break;
70339efd6a3cSNicolas Dichtel 		case 0:
70349efd6a3cSNicolas Dichtel 		case 2:
70359efd6a3cSNicolas Dichtel 			/* use compiled values */
70369efd6a3cSNicolas Dichtel 			break;
70379efd6a3cSNicolas Dichtel 		}
7038856c395cSCong Wang 	}
7039856c395cSCong Wang 
704056d417b1SBrian Haley 	/* these will be inherited by all namespaces */
704156d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
704256d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
7043e0da5a48SPavel Emelyanov 
70443d1bec99SHannes Frederic Sowa 	dflt->stable_secret.initialized = false;
70453d1bec99SHannes Frederic Sowa 	all->stable_secret.initialized = false;
70463d1bec99SHannes Frederic Sowa 
7047e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
7048e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
7049e0da5a48SPavel Emelyanov 
7050e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
7051f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
7052e0da5a48SPavel Emelyanov 	if (err < 0)
7053e0da5a48SPavel Emelyanov 		goto err_reg_all;
7054e0da5a48SPavel Emelyanov 
7055f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
7056e0da5a48SPavel Emelyanov 	if (err < 0)
7057e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
7058e0da5a48SPavel Emelyanov #endif
7059e0da5a48SPavel Emelyanov 	return 0;
7060e0da5a48SPavel Emelyanov 
7061e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
7062e0da5a48SPavel Emelyanov err_reg_dflt:
706323452170SDavid Ahern 	__addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
7064e0da5a48SPavel Emelyanov err_reg_all:
7065e0da5a48SPavel Emelyanov 	kfree(dflt);
7066e0da5a48SPavel Emelyanov #endif
7067e0da5a48SPavel Emelyanov err_alloc_dflt:
7068e0da5a48SPavel Emelyanov 	kfree(all);
7069e0da5a48SPavel Emelyanov err_alloc_all:
7070e0da5a48SPavel Emelyanov 	return err;
7071e0da5a48SPavel Emelyanov }
7072e0da5a48SPavel Emelyanov 
70732c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
7074e0da5a48SPavel Emelyanov {
7075e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
707623452170SDavid Ahern 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
707723452170SDavid Ahern 				     NETCONFA_IFINDEX_DEFAULT);
707823452170SDavid Ahern 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
707923452170SDavid Ahern 				     NETCONFA_IFINDEX_ALL);
7080e0da5a48SPavel Emelyanov #endif
7081e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_dflt);
7082e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_all);
7083e0da5a48SPavel Emelyanov }
7084e0da5a48SPavel Emelyanov 
7085e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
7086e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
7087e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
7088e0da5a48SPavel Emelyanov };
7089e0da5a48SPavel Emelyanov 
7090207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = {
7091b382b191SThomas Graf 	.family		  = AF_INET6,
7092b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
7093b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
709411b1f828SDaniel Borkmann 	.validate_link_af = inet6_validate_link_af,
7095f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
7096b382b191SThomas Graf };
7097b382b191SThomas Graf 
70981da177e4SLinus Torvalds /*
70991da177e4SLinus Torvalds  *	Init / cleanup code
71001da177e4SLinus Torvalds  */
71011da177e4SLinus Torvalds 
71021da177e4SLinus Torvalds int __init addrconf_init(void)
71031da177e4SLinus Torvalds {
7104a317a2f1SWANG Cong 	struct inet6_dev *idev;
7105c2e21293Sstephen hemminger 	int i, err;
71062a8cc6c8SYOSHIFUJI Hideaki 
7107e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
7108e21e8467SStephen Hemminger 	if (err < 0) {
7109f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
7110f3213831SJoe Perches 			__func__, err);
71112cc6d2bfSNeil Horman 		goto out;
71122a8cc6c8SYOSHIFUJI Hideaki 	}
71131da177e4SLinus Torvalds 
71142cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
71152cc6d2bfSNeil Horman 	if (err < 0)
71162cc6d2bfSNeil Horman 		goto out_addrlabel;
7117e0da5a48SPavel Emelyanov 
7118c15b1ccaSHannes Frederic Sowa 	addrconf_wq = create_workqueue("ipv6_addrconf");
7119c15b1ccaSHannes Frederic Sowa 	if (!addrconf_wq) {
7120c15b1ccaSHannes Frederic Sowa 		err = -ENOMEM;
7121c15b1ccaSHannes Frederic Sowa 		goto out_nowq;
7122c15b1ccaSHannes Frederic Sowa 	}
7123c15b1ccaSHannes Frederic Sowa 
71241da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
71251da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
71261da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
71271da177e4SLinus Torvalds 	 * to other devices which are up.
71281da177e4SLinus Torvalds 	 *
71291da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
71301da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
71311da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
71321da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
71331da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
71341da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
71351da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
71361da177e4SLinus Torvalds 	 *
71371da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
71381da177e4SLinus Torvalds 	 * loopback_dev first by hand.
71391da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
71401da177e4SLinus Torvalds 	 * device and it being up should be removed.
71411da177e4SLinus Torvalds 	 */
71421da177e4SLinus Torvalds 	rtnl_lock();
7143a317a2f1SWANG Cong 	idev = ipv6_add_dev(init_net.loopback_dev);
71441da177e4SLinus Torvalds 	rtnl_unlock();
7145a317a2f1SWANG Cong 	if (IS_ERR(idev)) {
7146a317a2f1SWANG Cong 		err = PTR_ERR(idev);
7147e0da5a48SPavel Emelyanov 		goto errlo;
7148a317a2f1SWANG Cong 	}
71491da177e4SLinus Torvalds 
71502f460933SWANG Cong 	ip6_route_init_special_entries();
71512f460933SWANG Cong 
7152c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
7153c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
7154c2e21293Sstephen hemminger 
71551da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
71561da177e4SLinus Torvalds 
7157c15b1ccaSHannes Frederic Sowa 	addrconf_verify();
7158c127ea2cSThomas Graf 
71593678a9d8Sstephen hemminger 	rtnl_af_register(&inet6_ops);
7160b382b191SThomas Graf 
716116feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
716216feebcfSFlorian Westphal 				   NULL, inet6_dump_ifinfo, 0);
7163c127ea2cSThomas Graf 	if (err < 0)
7164c127ea2cSThomas Graf 		goto errout;
7165c127ea2cSThomas Graf 
716616feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
716716feebcfSFlorian Westphal 				   inet6_rtm_newaddr, NULL, 0);
716816feebcfSFlorian Westphal 	if (err < 0)
716916feebcfSFlorian Westphal 		goto errout;
717016feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
717116feebcfSFlorian Westphal 				   inet6_rtm_deladdr, NULL, 0);
717216feebcfSFlorian Westphal 	if (err < 0)
717316feebcfSFlorian Westphal 		goto errout;
717416feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
717516feebcfSFlorian Westphal 				   inet6_rtm_getaddr, inet6_dump_ifaddr,
717616feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
717716feebcfSFlorian Westphal 	if (err < 0)
717816feebcfSFlorian Westphal 		goto errout;
717916feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
718016feebcfSFlorian Westphal 				   NULL, inet6_dump_ifmcaddr, 0);
718116feebcfSFlorian Westphal 	if (err < 0)
718216feebcfSFlorian Westphal 		goto errout;
718316feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
718416feebcfSFlorian Westphal 				   NULL, inet6_dump_ifacaddr, 0);
718516feebcfSFlorian Westphal 	if (err < 0)
718616feebcfSFlorian Westphal 		goto errout;
718716feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
718816feebcfSFlorian Westphal 				   inet6_netconf_get_devconf,
718916feebcfSFlorian Westphal 				   inet6_netconf_dump_devconf,
719016feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
719116feebcfSFlorian Westphal 	if (err < 0)
719216feebcfSFlorian Westphal 		goto errout;
7193a3fde2adSFlorian Westphal 	err = ipv6_addr_label_rtnl_register();
7194a3fde2adSFlorian Westphal 	if (err < 0)
7195a3fde2adSFlorian Westphal 		goto errout;
71962a8cc6c8SYOSHIFUJI Hideaki 
71971da177e4SLinus Torvalds 	return 0;
7198c127ea2cSThomas Graf errout:
719916feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
7200b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
7201c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
7202e0da5a48SPavel Emelyanov errlo:
7203c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
7204c15b1ccaSHannes Frederic Sowa out_nowq:
7205e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
72062cc6d2bfSNeil Horman out_addrlabel:
72072cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
72082cc6d2bfSNeil Horman out:
7209c127ea2cSThomas Graf 	return err;
72101da177e4SLinus Torvalds }
72111da177e4SLinus Torvalds 
721209f7709fSDaniel Lezcano void addrconf_cleanup(void)
72131da177e4SLinus Torvalds {
7214176c39afSDaniel Lezcano 	struct net_device *dev;
72151da177e4SLinus Torvalds 	int i;
72161da177e4SLinus Torvalds 
72171da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
7218e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
72192cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
72201da177e4SLinus Torvalds 
72215c45121dSFlorian Westphal 	rtnl_af_unregister(&inet6_ops);
72221da177e4SLinus Torvalds 
72235c45121dSFlorian Westphal 	rtnl_lock();
7224b382b191SThomas Graf 
7225176c39afSDaniel Lezcano 	/* clean dev list */
7226176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
7227176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
7228176c39afSDaniel Lezcano 			continue;
7229ae79dbf6SFlorent Fourcot 		addrconf_ifdown(dev, true);
7230176c39afSDaniel Lezcano 	}
7231ae79dbf6SFlorent Fourcot 	addrconf_ifdown(init_net.loopback_dev, true);
7232176c39afSDaniel Lezcano 
72331da177e4SLinus Torvalds 	/*
72341da177e4SLinus Torvalds 	 *	Check hash table.
72351da177e4SLinus Torvalds 	 */
72365c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
7237c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
7238c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
72395c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
7240c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
72411da177e4SLinus Torvalds 	rtnl_unlock();
7242c15b1ccaSHannes Frederic Sowa 
7243c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
72441da177e4SLinus Torvalds }
7245