xref: /openbmc/linux/net/ipv6/addrconf.c (revision 8308f3ff1753d001f7a73f9bb0f02292b5400557)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	IPv6 Address [auto]configuration
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
101da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
111da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
121da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds /*
161da177e4SLinus Torvalds  *	Changes:
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *	Janos Farkas			:	delete timer on ifdown
191da177e4SLinus Torvalds  *	<chexum@bankinf.banki.hu>
201da177e4SLinus Torvalds  *	Andi Kleen			:	kill double kfree on module
211da177e4SLinus Torvalds  *						unload.
221da177e4SLinus Torvalds  *	Maciej W. Rozycki		:	FDDI support
231da177e4SLinus Torvalds  *	sekiya@USAGI			:	Don't send too many RS
241da177e4SLinus Torvalds  *						packets.
251da177e4SLinus Torvalds  *	yoshfuji@USAGI			:       Fixed interval between DAD
261da177e4SLinus Torvalds  *						packets.
271da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	improved accuracy of
281da177e4SLinus Torvalds  *						address validation timer.
291da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	Privacy Extensions (RFC3041)
301da177e4SLinus Torvalds  *						support.
311da177e4SLinus Torvalds  *	Yuji SEKIYA @USAGI		:	Don't assign a same IPv6
321da177e4SLinus Torvalds  *						address on a same interface.
331da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	ARCnet support
341da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	convert /proc/net/if_inet6 to
351da177e4SLinus Torvalds  *						seq_file.
36b1cacb68SYOSHIFUJI Hideaki  *	YOSHIFUJI Hideaki @USAGI	:	improved source address
37b1cacb68SYOSHIFUJI Hideaki  *						selection; consider scope,
38b1cacb68SYOSHIFUJI Hideaki  *						status etc.
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
41f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
42f3213831SJoe Perches 
431da177e4SLinus Torvalds #include <linux/errno.h>
441da177e4SLinus Torvalds #include <linux/types.h>
45a0bffffcSIlpo Järvinen #include <linux/kernel.h>
46174cd4b1SIngo Molnar #include <linux/sched/signal.h>
471da177e4SLinus Torvalds #include <linux/socket.h>
481da177e4SLinus Torvalds #include <linux/sockios.h>
491da177e4SLinus Torvalds #include <linux/net.h>
503d1bec99SHannes Frederic Sowa #include <linux/inet.h>
511da177e4SLinus Torvalds #include <linux/in6.h>
521da177e4SLinus Torvalds #include <linux/netdevice.h>
531823730fSThomas Graf #include <linux/if_addr.h>
541da177e4SLinus Torvalds #include <linux/if_arp.h>
551da177e4SLinus Torvalds #include <linux/if_arcnet.h>
561da177e4SLinus Torvalds #include <linux/if_infiniband.h>
571da177e4SLinus Torvalds #include <linux/route.h>
581da177e4SLinus Torvalds #include <linux/inetdevice.h>
591da177e4SLinus Torvalds #include <linux/init.h>
605a0e3ad6STejun Heo #include <linux/slab.h>
611da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
621da177e4SLinus Torvalds #include <linux/sysctl.h>
631da177e4SLinus Torvalds #endif
644fc268d2SRandy Dunlap #include <linux/capability.h>
651da177e4SLinus Torvalds #include <linux/delay.h>
661da177e4SLinus Torvalds #include <linux/notifier.h>
67543537bdSPaulo Marques #include <linux/string.h>
68ddbe5032SEric Dumazet #include <linux/hash.h>
691da177e4SLinus Torvalds 
70457c4cbcSEric W. Biederman #include <net/net_namespace.h>
711da177e4SLinus Torvalds #include <net/sock.h>
721da177e4SLinus Torvalds #include <net/snmp.h>
731da177e4SLinus Torvalds 
745241c2d7SAlexander Aring #include <net/6lowpan.h>
75cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h>
761da177e4SLinus Torvalds #include <net/ipv6.h>
771da177e4SLinus Torvalds #include <net/protocol.h>
781da177e4SLinus Torvalds #include <net/ndisc.h>
791da177e4SLinus Torvalds #include <net/ip6_route.h>
801da177e4SLinus Torvalds #include <net/addrconf.h>
811da177e4SLinus Torvalds #include <net/tcp.h>
821da177e4SLinus Torvalds #include <net/ip.h>
835d620266SThomas Graf #include <net/netlink.h>
84f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
85ca254490SDavid Ahern #include <net/l3mdev.h>
861da177e4SLinus Torvalds #include <linux/if_tunnel.h>
871da177e4SLinus Torvalds #include <linux/rtnetlink.h>
88f3a1bfb1SNicolas Dichtel #include <linux/netconf.h>
891da177e4SLinus Torvalds #include <linux/random.h>
90e21e8467SStephen Hemminger #include <linux/uaccess.h>
917f7d9a6bSHerbert Xu #include <asm/unaligned.h>
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #include <linux/proc_fs.h>
941da177e4SLinus Torvalds #include <linux/seq_file.h>
95bc3b2d7fSPaul Gortmaker #include <linux/export.h>
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
9818a31e1eSThomas Graf 
993d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \
1003d1bec99SHannes Frederic Sowa 	sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
1013d1bec99SHannes Frederic Sowa 
10218a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp)
10318a31e1eSThomas Graf {
10418a31e1eSThomas Graf 	return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
10518a31e1eSThomas Graf }
1061da177e4SLinus Torvalds 
107bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_init(s32 irt)
108bd11f074SMaciej Żenczykowski {
109bd11f074SMaciej Żenczykowski 	/* multiply 'initial retransmission time' by 0.9 .. 1.1 */
110bd11f074SMaciej Żenczykowski 	u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
111bd11f074SMaciej Żenczykowski 	do_div(tmp, 1000000);
112bd11f074SMaciej Żenczykowski 	return (s32)tmp;
113bd11f074SMaciej Żenczykowski }
114bd11f074SMaciej Żenczykowski 
115bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
116bd11f074SMaciej Żenczykowski {
117bd11f074SMaciej Żenczykowski 	/* multiply 'retransmission timeout' by 1.9 .. 2.1 */
118bd11f074SMaciej Żenczykowski 	u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
119bd11f074SMaciej Żenczykowski 	do_div(tmp, 1000000);
120bd11f074SMaciej Żenczykowski 	if ((s32)tmp > mrt) {
121bd11f074SMaciej Żenczykowski 		/* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
122bd11f074SMaciej Żenczykowski 		tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
123bd11f074SMaciej Żenczykowski 		do_div(tmp, 1000000);
124bd11f074SMaciej Żenczykowski 	}
125bd11f074SMaciej Żenczykowski 	return (s32)tmp;
126bd11f074SMaciej Żenczykowski }
127bd11f074SMaciej Żenczykowski 
1281da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
129a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev);
130408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
131408c4768SPavel Emelyanov #else
132a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev)
133408c4768SPavel Emelyanov {
134a317a2f1SWANG Cong 	return 0;
135408c4768SPavel Emelyanov }
136408c4768SPavel Emelyanov 
137408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
138408c4768SPavel Emelyanov {
139408c4768SPavel Emelyanov }
1401da177e4SLinus Torvalds #endif
1411da177e4SLinus Torvalds 
1429d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev);
1439d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1441da177e4SLinus Torvalds 
1451b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
146d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev);
147622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr,
148622c81d5SHannes Frederic Sowa 					u8 dad_count,
149622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev);
1501da177e4SLinus Torvalds 
15127c565aeSEric Dumazet #define IN6_ADDR_HSIZE_SHIFT	8
15227c565aeSEric Dumazet #define IN6_ADDR_HSIZE		(1 << IN6_ADDR_HSIZE_SHIFT)
1531da177e4SLinus Torvalds /*
1541da177e4SLinus Torvalds  *	Configured unicast address hash table
1551da177e4SLinus Torvalds  */
156c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
1575c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock);
1581da177e4SLinus Torvalds 
159c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void);
160c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void);
161c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *);
1621da177e4SLinus Torvalds 
163c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq;
164c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1671da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1681da177e4SLinus Torvalds 
16993d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev,
17075c78500SMoni Shoua 				 unsigned long event);
1711da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1721da177e4SLinus Torvalds 
1738d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
17421caa662SRomain Kuntz 						  int plen,
17521caa662SRomain Kuntz 						  const struct net_device *dev,
17621caa662SRomain Kuntz 						  u32 flags, u32 noflags);
17721caa662SRomain Kuntz 
178cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp);
179c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w);
180c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
181c76fe2d9SDavid Ahern 				   bool send_na);
182c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
183e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t);
1841da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1851da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1881da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1891da177e4SLinus Torvalds 
190888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1911da177e4SLinus Torvalds 	.forwarding		= 0,
1921da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1931da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1941da177e4SLinus Torvalds 	.accept_ra		= 1,
1951da177e4SLinus Torvalds 	.accept_redirects	= 1,
1961da177e4SLinus Torvalds 	.autoconf		= 1,
1971da177e4SLinus Torvalds 	.force_mld_version	= 0,
198fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
199fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2001da177e4SLinus Torvalds 	.dad_transmits		= 1,
2011da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2021da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
203bd11f074SMaciej Żenczykowski 	.rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
2041da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2051da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2061da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2071da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2081da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2091da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2101da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
21165f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
212d9333196SBen Greear 	.accept_ra_from_local	= 0,
2138013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
214c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
215930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
216930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
21752e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
21809c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
219bbea124bSJoel Scherpelz 	.accept_ra_rt_info_min_plen = 0,
22009c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
22109c884d4SYOSHIFUJI Hideaki #endif
222930d6ff2SYOSHIFUJI Hideaki #endif
223fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2240bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
225778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
22609400953SNicolas Dichtel 	.accept_dad		= 0,
227b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
228c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2293d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2303d1bec99SHannes Frederic Sowa 		.initialized = false,
2313985e8a3SErik Kline 	},
2323985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
23335103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
234f1705ec1SDavid Ahern 	.keep_addr_on_down	= 0,
2351ababebaSDavid Lebrun 	.seg6_enabled		= 0,
236bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
237bf355b8dSDavid Lebrun 	.seg6_require_hmac	= 0,
238bf355b8dSDavid Lebrun #endif
239adc176c5SErik Nordmark 	.enhanced_dad           = 1,
240d35a00b8SFelix Jia 	.addr_gen_mode		= IN6_ADDR_GEN_MODE_EUI64,
241df789fe7SDavid Forster 	.disable_policy		= 0,
2421da177e4SLinus Torvalds };
2431da177e4SLinus Torvalds 
244ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2451da177e4SLinus Torvalds 	.forwarding		= 0,
2461da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2471da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2481da177e4SLinus Torvalds 	.accept_ra		= 1,
2491da177e4SLinus Torvalds 	.accept_redirects	= 1,
2501da177e4SLinus Torvalds 	.autoconf		= 1,
251fc4eba58SHannes Frederic Sowa 	.force_mld_version	= 0,
252fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
253fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2541da177e4SLinus Torvalds 	.dad_transmits		= 1,
2551da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2561da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
257bd11f074SMaciej Żenczykowski 	.rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
2581da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2591da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2601da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2611da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2621da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2631da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2641da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
26565f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
266d9333196SBen Greear 	.accept_ra_from_local	= 0,
2678013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
268c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
269930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
270930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
27152e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
27209c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
273bbea124bSJoel Scherpelz 	.accept_ra_rt_info_min_plen = 0,
27409c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
27509c884d4SYOSHIFUJI Hideaki #endif
276930d6ff2SYOSHIFUJI Hideaki #endif
277fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2780bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
279778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2801b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
281b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
282c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2833d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2843d1bec99SHannes Frederic Sowa 		.initialized = false,
2853d1bec99SHannes Frederic Sowa 	},
2863985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
28735103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
288f1705ec1SDavid Ahern 	.keep_addr_on_down	= 0,
2891ababebaSDavid Lebrun 	.seg6_enabled		= 0,
290bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
291bf355b8dSDavid Lebrun 	.seg6_require_hmac	= 0,
292bf355b8dSDavid Lebrun #endif
293adc176c5SErik Nordmark 	.enhanced_dad           = 1,
294d35a00b8SFelix Jia 	.addr_gen_mode		= IN6_ADDR_GEN_MODE_EUI64,
295df789fe7SDavid Forster 	.disable_policy		= 0,
2961da177e4SLinus Torvalds };
2971da177e4SLinus Torvalds 
2981f372c7bSMike Manning /* Check if link is ready: is it up and is a valid qdisc available */
2991f372c7bSMike Manning static inline bool addrconf_link_ready(const struct net_device *dev)
300f24e3d65SMitsuru Chinen {
3011f372c7bSMike Manning 	return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
302f24e3d65SMitsuru Chinen }
303f24e3d65SMitsuru Chinen 
304b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev)
3051da177e4SLinus Torvalds {
306b7b1bfceSHannes Frederic Sowa 	if (del_timer(&idev->rs_timer))
307b7b1bfceSHannes Frederic Sowa 		__in6_dev_put(idev);
308b7b1bfceSHannes Frederic Sowa }
309b7b1bfceSHannes Frederic Sowa 
310c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
311b7b1bfceSHannes Frederic Sowa {
312c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
3131da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
316b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev,
3171da177e4SLinus Torvalds 				  unsigned long when)
3181da177e4SLinus Torvalds {
319b7b1bfceSHannes Frederic Sowa 	if (!timer_pending(&idev->rs_timer))
320b7b1bfceSHannes Frederic Sowa 		in6_dev_hold(idev);
321b7b1bfceSHannes Frederic Sowa 	mod_timer(&idev->rs_timer, jiffies + when);
3221da177e4SLinus Torvalds }
323b7b1bfceSHannes Frederic Sowa 
324c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
325c15b1ccaSHannes Frederic Sowa 				   unsigned long delay)
326b7b1bfceSHannes Frederic Sowa {
327b7b1bfceSHannes Frederic Sowa 	in6_ifa_hold(ifp);
328f8a894b2SXin Long 	if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
329f8a894b2SXin Long 		in6_ifa_put(ifp);
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3327f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
3337f7d9a6bSHerbert Xu {
334827da44cSJohn Stultz 	int i;
335827da44cSJohn Stultz 
336698365faSWANG Cong 	idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
337698365faSWANG Cong 	if (!idev->stats.ipv6)
3387f7d9a6bSHerbert Xu 		goto err_ip;
339827da44cSJohn Stultz 
340827da44cSJohn Stultz 	for_each_possible_cpu(i) {
341827da44cSJohn Stultz 		struct ipstats_mib *addrconf_stats;
342698365faSWANG Cong 		addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
343827da44cSJohn Stultz 		u64_stats_init(&addrconf_stats->syncp);
344827da44cSJohn Stultz 	}
345827da44cSJohn Stultz 
346827da44cSJohn Stultz 
347be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
348be281e55SEric Dumazet 					GFP_KERNEL);
349be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
3507f7d9a6bSHerbert Xu 		goto err_icmp;
351be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
352be281e55SEric Dumazet 					   GFP_KERNEL);
353be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
35414878f75SDavid L Stevens 		goto err_icmpmsg;
3557f7d9a6bSHerbert Xu 
3567f7d9a6bSHerbert Xu 	return 0;
3577f7d9a6bSHerbert Xu 
35814878f75SDavid L Stevens err_icmpmsg:
359be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3607f7d9a6bSHerbert Xu err_icmp:
361698365faSWANG Cong 	free_percpu(idev->stats.ipv6);
3627f7d9a6bSHerbert Xu err_ip:
363aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3647f7d9a6bSHerbert Xu }
3657f7d9a6bSHerbert Xu 
3661da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	struct inet6_dev *ndev;
369a317a2f1SWANG Cong 	int err = -ENOMEM;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	ASSERT_RTNL();
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
374a317a2f1SWANG Cong 		return ERR_PTR(-EINVAL);
3751da177e4SLinus Torvalds 
376322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
37763159f29SIan Morris 	if (!ndev)
378a317a2f1SWANG Cong 		return ERR_PTR(err);
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3811da177e4SLinus Torvalds 	ndev->dev = dev;
382502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
383e99e88a9SKees Cook 	timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0);
384c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3859b29c696SHannes Frederic Sowa 
3869b29c696SHannes Frederic Sowa 	if (ndev->cnf.stable_secret.initialized)
387d35a00b8SFelix Jia 		ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
3889b29c696SHannes Frederic Sowa 	else
389d35a00b8SFelix Jia 		ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode;
3909b29c696SHannes Frederic Sowa 
3911da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3921da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
39363159f29SIan Morris 	if (!ndev->nd_parms) {
3941da177e4SLinus Torvalds 		kfree(ndev);
395a317a2f1SWANG Cong 		return ERR_PTR(err);
3961da177e4SLinus Torvalds 	}
3970187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
3980187bdfbSBen Hutchings 		dev_disable_lro(dev);
3991da177e4SLinus Torvalds 	/* We refer to the device */
4001da177e4SLinus Torvalds 	dev_hold(dev);
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
403e32ac250SJoe Perches 		netdev_dbg(dev, "%s: cannot allocate memory for statistics\n",
404e32ac250SJoe Perches 			   __func__);
4051da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
4068603e33dSRoy Li 		dev_put(dev);
4078603e33dSRoy Li 		kfree(ndev);
408a317a2f1SWANG Cong 		return ERR_PTR(err);
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
412e32ac250SJoe Perches 		netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n",
413ba3542e1Sdingtianhong 			   __func__, dev->name);
414a317a2f1SWANG Cong 		goto err_release;
4151da177e4SLinus Torvalds 	}
4161da177e4SLinus Torvalds 
4179d6280daSJiri Bohac 	/* One reference from device. */
4181be92460SReshetova, Elena 	refcount_set(&ndev->refcnt, 1);
4191da177e4SLinus Torvalds 
4201b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
4211b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
4221b34be74SYOSHIFUJI Hideaki 
42307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
424c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
425f3213831SJoe Perches 		pr_info("%s: Disabled Multicast RS\n", dev->name);
426c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
427c7dc89c0SFred L. Templin 	}
428b077d7abSYOSHIFUJI Hideaki #endif
429b077d7abSYOSHIFUJI Hideaki 
430372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
43176506a98SJiri Bohac 	ndev->desync_factor = U32_MAX;
4321da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
4331da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4349625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4351da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4361da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4370be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
4389d6280daSJiri Bohac 	} else
4399d6280daSJiri Bohac 		ipv6_regen_rndid(ndev);
4405d9efa7eSDavid S. Miller 
441914faa14SDaniel Borkmann 	ndev->token = in6addr_any;
4421da177e4SLinus Torvalds 
4431f372c7bSMike Manning 	if (netif_running(dev) && addrconf_link_ready(dev))
44453aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
44553aadcc9SHerbert Xu 
4461da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4471da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
448a317a2f1SWANG Cong 	err = addrconf_sysctl_register(ndev);
449a317a2f1SWANG Cong 	if (err) {
450a317a2f1SWANG Cong 		ipv6_mc_destroy_dev(ndev);
4512a189f9eSSabrina Dubroca 		snmp6_unregister_dev(ndev);
452a317a2f1SWANG Cong 		goto err_release;
453a317a2f1SWANG Cong 	}
45430c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
455cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip6_ptr, ndev);
456d88ae4ccSYOSHIFUJI Hideaki 
4572c5e8933SHannes Frederic Sowa 	/* Join interface-local all-node multicast group */
4582c5e8933SHannes Frederic Sowa 	ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4592c5e8933SHannes Frederic Sowa 
460d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
461f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
462d88ae4ccSYOSHIFUJI Hideaki 
463d6ddef9eSLi Wei 	/* Join all-router multicast group if forwarding is set */
4648b2aaedeSLi Wei 	if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
465d6ddef9eSLi Wei 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
466d6ddef9eSLi Wei 
4671da177e4SLinus Torvalds 	return ndev;
468a317a2f1SWANG Cong 
469a317a2f1SWANG Cong err_release:
470a317a2f1SWANG Cong 	neigh_parms_release(&nd_tbl, ndev->nd_parms);
471a317a2f1SWANG Cong 	ndev->dead = 1;
472a317a2f1SWANG Cong 	in6_dev_finish_destroy(ndev);
473a317a2f1SWANG Cong 	return ERR_PTR(err);
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
476cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	struct inet6_dev *idev;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	ASSERT_RTNL();
4811da177e4SLinus Torvalds 
482e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
483e21e8467SStephen Hemminger 	if (!idev) {
484e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
485a317a2f1SWANG Cong 		if (IS_ERR(idev))
4861da177e4SLinus Torvalds 			return NULL;
4871da177e4SLinus Torvalds 	}
488c5e33bddSYOSHIFUJI Hideaki 
4891da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4901da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4911da177e4SLinus Torvalds 	return idev;
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
494f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
495f3a1bfb1SNicolas Dichtel {
496f3a1bfb1SNicolas Dichtel 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
497f3a1bfb1SNicolas Dichtel 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
498136ba622SZhang Shengju 	bool all = false;
499f3a1bfb1SNicolas Dichtel 
500136ba622SZhang Shengju 	if (type == NETCONFA_ALL)
501136ba622SZhang Shengju 		all = true;
502136ba622SZhang Shengju 
503136ba622SZhang Shengju 	if (all || type == NETCONFA_FORWARDING)
504f3a1bfb1SNicolas Dichtel 		size += nla_total_size(4);
505b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
506136ba622SZhang Shengju 	if (all || type == NETCONFA_MC_FORWARDING)
507d67b8c61SNicolas Dichtel 		size += nla_total_size(4);
508b1afce95SDavid S. Miller #endif
509136ba622SZhang Shengju 	if (all || type == NETCONFA_PROXY_NEIGH)
510c92d5491Sstephen hemminger 		size += nla_total_size(4);
511f3a1bfb1SNicolas Dichtel 
512136ba622SZhang Shengju 	if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
51335103d11SAndy Gospodarek 		size += nla_total_size(4);
51435103d11SAndy Gospodarek 
515f3a1bfb1SNicolas Dichtel 	return size;
516f3a1bfb1SNicolas Dichtel }
517f3a1bfb1SNicolas Dichtel 
518f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
519f3a1bfb1SNicolas Dichtel 				      struct ipv6_devconf *devconf, u32 portid,
520f3a1bfb1SNicolas Dichtel 				      u32 seq, int event, unsigned int flags,
521f3a1bfb1SNicolas Dichtel 				      int type)
522f3a1bfb1SNicolas Dichtel {
523f3a1bfb1SNicolas Dichtel 	struct nlmsghdr  *nlh;
524f3a1bfb1SNicolas Dichtel 	struct netconfmsg *ncm;
525136ba622SZhang Shengju 	bool all = false;
526f3a1bfb1SNicolas Dichtel 
527f3a1bfb1SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
528f3a1bfb1SNicolas Dichtel 			flags);
52963159f29SIan Morris 	if (!nlh)
530f3a1bfb1SNicolas Dichtel 		return -EMSGSIZE;
531f3a1bfb1SNicolas Dichtel 
532136ba622SZhang Shengju 	if (type == NETCONFA_ALL)
533136ba622SZhang Shengju 		all = true;
534136ba622SZhang Shengju 
535f3a1bfb1SNicolas Dichtel 	ncm = nlmsg_data(nlh);
536f3a1bfb1SNicolas Dichtel 	ncm->ncm_family = AF_INET6;
537f3a1bfb1SNicolas Dichtel 
538f3a1bfb1SNicolas Dichtel 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
539f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
540f3a1bfb1SNicolas Dichtel 
54123452170SDavid Ahern 	if (!devconf)
54223452170SDavid Ahern 		goto out;
54323452170SDavid Ahern 
544136ba622SZhang Shengju 	if ((all || type == NETCONFA_FORWARDING) &&
545f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
546f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
547b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
548136ba622SZhang Shengju 	if ((all || type == NETCONFA_MC_FORWARDING) &&
549d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
550d67b8c61SNicolas Dichtel 			devconf->mc_forwarding) < 0)
551d67b8c61SNicolas Dichtel 		goto nla_put_failure;
552b1afce95SDavid S. Miller #endif
553136ba622SZhang Shengju 	if ((all || type == NETCONFA_PROXY_NEIGH) &&
554c92d5491Sstephen hemminger 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
555c92d5491Sstephen hemminger 		goto nla_put_failure;
556c92d5491Sstephen hemminger 
557136ba622SZhang Shengju 	if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
55835103d11SAndy Gospodarek 	    nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
55935103d11SAndy Gospodarek 			devconf->ignore_routes_with_linkdown) < 0)
56035103d11SAndy Gospodarek 		goto nla_put_failure;
56135103d11SAndy Gospodarek 
56223452170SDavid Ahern out:
563053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
564053c095aSJohannes Berg 	return 0;
565f3a1bfb1SNicolas Dichtel 
566f3a1bfb1SNicolas Dichtel nla_put_failure:
567f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
568f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
569f3a1bfb1SNicolas Dichtel }
570f3a1bfb1SNicolas Dichtel 
57185b3daadSDavid Ahern void inet6_netconf_notify_devconf(struct net *net, int event, int type,
57285b3daadSDavid Ahern 				  int ifindex, struct ipv6_devconf *devconf)
573f3a1bfb1SNicolas Dichtel {
574f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
575f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
576f3a1bfb1SNicolas Dichtel 
577927265bcSEric Dumazet 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
57863159f29SIan Morris 	if (!skb)
579f3a1bfb1SNicolas Dichtel 		goto errout;
580f3a1bfb1SNicolas Dichtel 
581f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
58285b3daadSDavid Ahern 					 event, 0, type);
583f3a1bfb1SNicolas Dichtel 	if (err < 0) {
584f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
585f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
586f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
587f3a1bfb1SNicolas Dichtel 		goto errout;
588f3a1bfb1SNicolas Dichtel 	}
589927265bcSEric Dumazet 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
590f3a1bfb1SNicolas Dichtel 	return;
591f3a1bfb1SNicolas Dichtel errout:
592f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
593f3a1bfb1SNicolas Dichtel }
594f3a1bfb1SNicolas Dichtel 
59576f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
59676f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
59776f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
598c92d5491Sstephen hemminger 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
59935103d11SAndy Gospodarek 	[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]	= { .len = sizeof(int) },
60076f8f6cbSNicolas Dichtel };
60176f8f6cbSNicolas Dichtel 
60276f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
603c21ef3e3SDavid Ahern 				     struct nlmsghdr *nlh,
604c21ef3e3SDavid Ahern 				     struct netlink_ext_ack *extack)
60576f8f6cbSNicolas Dichtel {
60676f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
60776f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
6084ea2607fSFlorian Westphal 	struct inet6_dev *in6_dev = NULL;
6094ea2607fSFlorian Westphal 	struct net_device *dev = NULL;
61076f8f6cbSNicolas Dichtel 	struct netconfmsg *ncm;
61176f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
61276f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
61376f8f6cbSNicolas Dichtel 	int ifindex;
61476f8f6cbSNicolas Dichtel 	int err;
61576f8f6cbSNicolas Dichtel 
61676f8f6cbSNicolas Dichtel 	err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
617c21ef3e3SDavid Ahern 			  devconf_ipv6_policy, extack);
61876f8f6cbSNicolas Dichtel 	if (err < 0)
6194ea2607fSFlorian Westphal 		return err;
6204ea2607fSFlorian Westphal 
6214ea2607fSFlorian Westphal 	if (!tb[NETCONFA_IFINDEX])
6224ea2607fSFlorian Westphal 		return -EINVAL;
62376f8f6cbSNicolas Dichtel 
624a97eb33fSAnton Protopopov 	err = -EINVAL;
62576f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
62676f8f6cbSNicolas Dichtel 	switch (ifindex) {
62776f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
62876f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
62976f8f6cbSNicolas Dichtel 		break;
63076f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
63176f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
63276f8f6cbSNicolas Dichtel 		break;
63376f8f6cbSNicolas Dichtel 	default:
6344ea2607fSFlorian Westphal 		dev = dev_get_by_index(net, ifindex);
63563159f29SIan Morris 		if (!dev)
6364ea2607fSFlorian Westphal 			return -EINVAL;
6374ea2607fSFlorian Westphal 		in6_dev = in6_dev_get(dev);
63863159f29SIan Morris 		if (!in6_dev)
63976f8f6cbSNicolas Dichtel 			goto errout;
64076f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
64176f8f6cbSNicolas Dichtel 		break;
64276f8f6cbSNicolas Dichtel 	}
64376f8f6cbSNicolas Dichtel 
64476f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
6454ea2607fSFlorian Westphal 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
64663159f29SIan Morris 	if (!skb)
64776f8f6cbSNicolas Dichtel 		goto errout;
64876f8f6cbSNicolas Dichtel 
64976f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
65076f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
65176f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
652136ba622SZhang Shengju 					 NETCONFA_ALL);
65376f8f6cbSNicolas Dichtel 	if (err < 0) {
65476f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
65576f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
65676f8f6cbSNicolas Dichtel 		kfree_skb(skb);
65776f8f6cbSNicolas Dichtel 		goto errout;
65876f8f6cbSNicolas Dichtel 	}
65976f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
66076f8f6cbSNicolas Dichtel errout:
6614ea2607fSFlorian Westphal 	if (in6_dev)
6624ea2607fSFlorian Westphal 		in6_dev_put(in6_dev);
6634ea2607fSFlorian Westphal 	if (dev)
6644ea2607fSFlorian Westphal 		dev_put(dev);
66576f8f6cbSNicolas Dichtel 	return err;
66676f8f6cbSNicolas Dichtel }
66776f8f6cbSNicolas Dichtel 
6687a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
6697a674200SNicolas Dichtel 				      struct netlink_callback *cb)
6707a674200SNicolas Dichtel {
6717a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
6727a674200SNicolas Dichtel 	int h, s_h;
6737a674200SNicolas Dichtel 	int idx, s_idx;
6747a674200SNicolas Dichtel 	struct net_device *dev;
6757a674200SNicolas Dichtel 	struct inet6_dev *idev;
6767a674200SNicolas Dichtel 	struct hlist_head *head;
6777a674200SNicolas Dichtel 
6787a674200SNicolas Dichtel 	s_h = cb->args[0];
6797a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
6807a674200SNicolas Dichtel 
6817a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6827a674200SNicolas Dichtel 		idx = 0;
6837a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
6847a674200SNicolas Dichtel 		rcu_read_lock();
68563998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
68663998ac2SNicolas Dichtel 			  net->dev_base_seq;
6877a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
6887a674200SNicolas Dichtel 			if (idx < s_idx)
6897a674200SNicolas Dichtel 				goto cont;
6907a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
6917a674200SNicolas Dichtel 			if (!idev)
6927a674200SNicolas Dichtel 				goto cont;
6937a674200SNicolas Dichtel 
6947a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
6957a674200SNicolas Dichtel 						       &idev->cnf,
6967a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
6977a674200SNicolas Dichtel 						       cb->nlh->nlmsg_seq,
6987a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
6997a674200SNicolas Dichtel 						       NLM_F_MULTI,
700136ba622SZhang Shengju 						       NETCONFA_ALL) < 0) {
7017a674200SNicolas Dichtel 				rcu_read_unlock();
7027a674200SNicolas Dichtel 				goto done;
7037a674200SNicolas Dichtel 			}
70463998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7057a674200SNicolas Dichtel cont:
7067a674200SNicolas Dichtel 			idx++;
7077a674200SNicolas Dichtel 		}
7087a674200SNicolas Dichtel 		rcu_read_unlock();
7097a674200SNicolas Dichtel 	}
7107a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
7117a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
7127a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
7137a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
7147a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
7157a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
716136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
7177a674200SNicolas Dichtel 			goto done;
7187a674200SNicolas Dichtel 		else
7197a674200SNicolas Dichtel 			h++;
7207a674200SNicolas Dichtel 	}
7217a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
7227a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
7237a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
7247a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
7257a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
7267a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
727136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
7287a674200SNicolas Dichtel 			goto done;
7297a674200SNicolas Dichtel 		else
7307a674200SNicolas Dichtel 			h++;
7317a674200SNicolas Dichtel 	}
7327a674200SNicolas Dichtel done:
7337a674200SNicolas Dichtel 	cb->args[0] = h;
7347a674200SNicolas Dichtel 	cb->args[1] = idx;
7357a674200SNicolas Dichtel 
7367a674200SNicolas Dichtel 	return skb->len;
7377a674200SNicolas Dichtel }
7387a674200SNicolas Dichtel 
7391da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
7401da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
7411da177e4SLinus Torvalds {
7421da177e4SLinus Torvalds 	struct net_device *dev;
7431da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	if (!idev)
7461da177e4SLinus Torvalds 		return;
7471da177e4SLinus Torvalds 	dev = idev->dev;
7480187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
7490187bdfbSBen Hutchings 		dev_disable_lro(dev);
7501a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
7512c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
752f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
7532c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
7542c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
7552c5e8933SHannes Frederic Sowa 		} else {
756f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
7572c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
7582c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
7592c5e8933SHannes Frederic Sowa 		}
7601da177e4SLinus Torvalds 	}
761502a2ffdSstephen hemminger 
762502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
7632c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
7642c12a74cSMichal Wrobel 			continue;
7651da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
7661da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
7671da177e4SLinus Torvalds 		else
7681da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
7691da177e4SLinus Torvalds 	}
77085b3daadSDavid Ahern 	inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
77185b3daadSDavid Ahern 				     NETCONFA_FORWARDING,
772f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 
776e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
7771da177e4SLinus Torvalds {
7781da177e4SLinus Torvalds 	struct net_device *dev;
7791da177e4SLinus Torvalds 	struct inet6_dev *idev;
7801da177e4SLinus Torvalds 
7814acd4945SBen Hutchings 	for_each_netdev(net, dev) {
7821da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
7831da177e4SLinus Torvalds 		if (idev) {
784e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
785e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
7861da177e4SLinus Torvalds 			if (changed)
7871da177e4SLinus Torvalds 				dev_forward_change(idev);
7881da177e4SLinus Torvalds 		}
7891da177e4SLinus Torvalds 	}
7901da177e4SLinus Torvalds }
791c8fecf22SPavel Emelyanov 
792013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
793c8fecf22SPavel Emelyanov {
794bff16c2fSPavel Emelyanov 	struct net *net;
795013d97e9SFrancesco Ruggeri 	int old;
796013d97e9SFrancesco Ruggeri 
797013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
798013d97e9SFrancesco Ruggeri 		return restart_syscall();
799bff16c2fSPavel Emelyanov 
800bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
801013d97e9SFrancesco Ruggeri 	old = *p;
802013d97e9SFrancesco Ruggeri 	*p = newf;
803c8fecf22SPavel Emelyanov 
804013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
805f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
80685b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
80785b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
808f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
809f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
810013d97e9SFrancesco Ruggeri 		rtnl_unlock();
811013d97e9SFrancesco Ruggeri 		return 0;
81288af182eSEric W. Biederman 	}
813b325fddbSStephen Hemminger 
814e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
815d26c638cSNicolas Dichtel 		int old_dflt = net->ipv6.devconf_dflt->forwarding;
816d26c638cSNicolas Dichtel 
817e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
818d26c638cSNicolas Dichtel 		if ((!newf) ^ (!old_dflt))
81985b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
82085b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
821d26c638cSNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
822d26c638cSNicolas Dichtel 						     net->ipv6.devconf_dflt);
823d26c638cSNicolas Dichtel 
824e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
825f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
82685b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
82785b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
828f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
829f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
830013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
831c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
8320187bdfbSBen Hutchings 	rtnl_unlock();
833c8fecf22SPavel Emelyanov 
834013d97e9SFrancesco Ruggeri 	if (newf)
8357b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
836b325fddbSStephen Hemminger 	return 1;
837c8fecf22SPavel Emelyanov }
83835103d11SAndy Gospodarek 
83935103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf)
84035103d11SAndy Gospodarek {
84135103d11SAndy Gospodarek 	struct net_device *dev;
84235103d11SAndy Gospodarek 	struct inet6_dev *idev;
84335103d11SAndy Gospodarek 
84435103d11SAndy Gospodarek 	for_each_netdev(net, dev) {
84535103d11SAndy Gospodarek 		idev = __in6_dev_get(dev);
84635103d11SAndy Gospodarek 		if (idev) {
84735103d11SAndy Gospodarek 			int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
84835103d11SAndy Gospodarek 
84935103d11SAndy Gospodarek 			idev->cnf.ignore_routes_with_linkdown = newf;
85035103d11SAndy Gospodarek 			if (changed)
85135103d11SAndy Gospodarek 				inet6_netconf_notify_devconf(dev_net(dev),
85285b3daadSDavid Ahern 							     RTM_NEWNETCONF,
85335103d11SAndy Gospodarek 							     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
85435103d11SAndy Gospodarek 							     dev->ifindex,
85535103d11SAndy Gospodarek 							     &idev->cnf);
85635103d11SAndy Gospodarek 		}
85735103d11SAndy Gospodarek 	}
85835103d11SAndy Gospodarek }
85935103d11SAndy Gospodarek 
86035103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
86135103d11SAndy Gospodarek {
86235103d11SAndy Gospodarek 	struct net *net;
86335103d11SAndy Gospodarek 	int old;
86435103d11SAndy Gospodarek 
86535103d11SAndy Gospodarek 	if (!rtnl_trylock())
86635103d11SAndy Gospodarek 		return restart_syscall();
86735103d11SAndy Gospodarek 
86835103d11SAndy Gospodarek 	net = (struct net *)table->extra2;
86935103d11SAndy Gospodarek 	old = *p;
87035103d11SAndy Gospodarek 	*p = newf;
87135103d11SAndy Gospodarek 
87235103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
87335103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
87435103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
87585b3daadSDavid Ahern 						     RTM_NEWNETCONF,
87635103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
87735103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_DEFAULT,
87835103d11SAndy Gospodarek 						     net->ipv6.devconf_dflt);
87935103d11SAndy Gospodarek 		rtnl_unlock();
88035103d11SAndy Gospodarek 		return 0;
88135103d11SAndy Gospodarek 	}
88235103d11SAndy Gospodarek 
88335103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
88435103d11SAndy Gospodarek 		net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
88535103d11SAndy Gospodarek 		addrconf_linkdown_change(net, newf);
88635103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
88735103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
88885b3daadSDavid Ahern 						     RTM_NEWNETCONF,
88935103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
89035103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_ALL,
89135103d11SAndy Gospodarek 						     net->ipv6.devconf_all);
89235103d11SAndy Gospodarek 	}
89335103d11SAndy Gospodarek 	rtnl_unlock();
89435103d11SAndy Gospodarek 
89535103d11SAndy Gospodarek 	return 1;
89635103d11SAndy Gospodarek }
89735103d11SAndy Gospodarek 
8981da177e4SLinus Torvalds #endif
8991da177e4SLinus Torvalds 
9005c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
9011da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
9021da177e4SLinus Torvalds {
903c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
904547b792cSIlpo Järvinen 
9051da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
90691df42beSJoe Perches 	pr_debug("%s\n", __func__);
9071da177e4SLinus Torvalds #endif
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
9101da177e4SLinus Torvalds 
911c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
912c15b1ccaSHannes Frederic Sowa 		pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
913c15b1ccaSHannes Frederic Sowa 			  ifp);
9141da177e4SLinus Torvalds 
915e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
916f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
9171da177e4SLinus Torvalds 		return;
9181da177e4SLinus Torvalds 	}
9191da177e4SLinus Torvalds 
920e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
923e55ffac6SBrian Haley static void
924e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
925e55ffac6SBrian Haley {
926502a2ffdSstephen hemminger 	struct list_head *p;
9278a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
928e55ffac6SBrian Haley 
929e55ffac6SBrian Haley 	/*
930e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
931e55ffac6SBrian Haley 	 * global before linklocal.
932e55ffac6SBrian Haley 	 */
933502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
934502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
935502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
9368a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
937e55ffac6SBrian Haley 			break;
938e55ffac6SBrian Haley 	}
939e55ffac6SBrian Haley 
9408ef802aaSEric Dumazet 	list_add_tail_rcu(&ifp->if_list, p);
941e55ffac6SBrian Haley }
942e55ffac6SBrian Haley 
9433f27fb23SEric Dumazet static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
9443eb84f49SYOSHIFUJI Hideaki {
9453f27fb23SEric Dumazet 	u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
9463f27fb23SEric Dumazet 
9473f27fb23SEric Dumazet 	return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
9483eb84f49SYOSHIFUJI Hideaki }
9493eb84f49SYOSHIFUJI Hideaki 
95056fc709bSEric Dumazet static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
951752a9292SEric Dumazet 			       struct net_device *dev, unsigned int hash)
95256fc709bSEric Dumazet {
95356fc709bSEric Dumazet 	struct inet6_ifaddr *ifp;
95456fc709bSEric Dumazet 
95556fc709bSEric Dumazet 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
95656fc709bSEric Dumazet 		if (!net_eq(dev_net(ifp->idev->dev), net))
95756fc709bSEric Dumazet 			continue;
95856fc709bSEric Dumazet 		if (ipv6_addr_equal(&ifp->addr, addr)) {
95956fc709bSEric Dumazet 			if (!dev || ifp->idev->dev == dev)
96056fc709bSEric Dumazet 				return true;
96156fc709bSEric Dumazet 		}
96256fc709bSEric Dumazet 	}
96356fc709bSEric Dumazet 	return false;
96456fc709bSEric Dumazet }
96556fc709bSEric Dumazet 
966f3d9832eSDavid Ahern static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
967f3d9832eSDavid Ahern {
9683f27fb23SEric Dumazet 	unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr);
969f3d9832eSDavid Ahern 	int err = 0;
970f3d9832eSDavid Ahern 
971f3d9832eSDavid Ahern 	spin_lock(&addrconf_hash_lock);
972f3d9832eSDavid Ahern 
973f3d9832eSDavid Ahern 	/* Ignore adding duplicate addresses on an interface */
974752a9292SEric Dumazet 	if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) {
975e32ac250SJoe Perches 		netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
976f3d9832eSDavid Ahern 		err = -EEXIST;
977752a9292SEric Dumazet 	} else {
978752a9292SEric Dumazet 		hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
979f3d9832eSDavid Ahern 	}
980f3d9832eSDavid Ahern 
981f3d9832eSDavid Ahern 	spin_unlock(&addrconf_hash_lock);
982f3d9832eSDavid Ahern 
983f3d9832eSDavid Ahern 	return err;
984f3d9832eSDavid Ahern }
985f3d9832eSDavid Ahern 
9861da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds static struct inet6_ifaddr *
989e6464b8cSDavid Ahern ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
990de95e047SDavid Ahern 	      bool can_block, struct netlink_ext_ack *extack)
9911da177e4SLinus Torvalds {
992f3d9832eSDavid Ahern 	gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
993e6464b8cSDavid Ahern 	int addr_type = ipv6_addr_type(cfg->pfx);
994df789fe7SDavid Forster 	struct net *net = dev_net(idev->dev);
9951da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
996360a9887SDavid Ahern 	struct fib6_info *f6i = NULL;
9971da177e4SLinus Torvalds 	int err = 0;
998d68b8270SYOSHIFUJI Hideaki 
999d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
1000d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
1001d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
1002d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
1003d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	if (idev->dead) {
10061da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
1007f3d9832eSDavid Ahern 		goto out;
10081da177e4SLinus Torvalds 	}
10091da177e4SLinus Torvalds 
101056d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
10119bdd8d40SBrian Haley 		err = -EACCES;
1012f3d9832eSDavid Ahern 		goto out;
10139bdd8d40SBrian Haley 	}
10149bdd8d40SBrian Haley 
1015ff7883eaSDavid Ahern 	/* validator notifier needs to be blocking;
1016ff7883eaSDavid Ahern 	 * do not call in atomic context
1017ff7883eaSDavid Ahern 	 */
1018ff7883eaSDavid Ahern 	if (can_block) {
1019ff7883eaSDavid Ahern 		struct in6_validator_info i6vi = {
1020e6464b8cSDavid Ahern 			.i6vi_addr = *cfg->pfx,
1021ff7883eaSDavid Ahern 			.i6vi_dev = idev,
1022de95e047SDavid Ahern 			.extack = extack,
1023ff7883eaSDavid Ahern 		};
1024ff7883eaSDavid Ahern 
10253ad7d246SKrister Johansen 		err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
10263ad7d246SKrister Johansen 		err = notifier_to_errno(err);
1027f3d9832eSDavid Ahern 		if (err < 0)
10281da177e4SLinus Torvalds 			goto out;
1029ff7883eaSDavid Ahern 	}
10301da177e4SLinus Torvalds 
1031f3d9832eSDavid Ahern 	ifa = kzalloc(sizeof(*ifa), gfp_flags);
103263159f29SIan Morris 	if (!ifa) {
10331da177e4SLinus Torvalds 		err = -ENOBUFS;
10341da177e4SLinus Torvalds 		goto out;
10351da177e4SLinus Torvalds 	}
10361da177e4SLinus Torvalds 
1037e6464b8cSDavid Ahern 	f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags);
1038360a9887SDavid Ahern 	if (IS_ERR(f6i)) {
1039360a9887SDavid Ahern 		err = PTR_ERR(f6i);
1040360a9887SDavid Ahern 		f6i = NULL;
10411da177e4SLinus Torvalds 		goto out;
10421da177e4SLinus Torvalds 	}
10431da177e4SLinus Torvalds 
1044df789fe7SDavid Forster 	if (net->ipv6.devconf_all->disable_policy ||
1045df789fe7SDavid Forster 	    idev->cnf.disable_policy)
1046360a9887SDavid Ahern 		f6i->dst_nopolicy = true;
1047df789fe7SDavid Forster 
1048bba24896SJiri Pirko 	neigh_parms_data_state_setall(idev->nd_parms);
1049bba24896SJiri Pirko 
1050e6464b8cSDavid Ahern 	ifa->addr = *cfg->pfx;
1051e6464b8cSDavid Ahern 	if (cfg->peer_pfx)
1052e6464b8cSDavid Ahern 		ifa->peer_addr = *cfg->peer_pfx;
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
1055c15b1ccaSHannes Frederic Sowa 	INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1056c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
1057e6464b8cSDavid Ahern 	ifa->scope = cfg->scope;
1058e6464b8cSDavid Ahern 	ifa->prefix_len = cfg->plen;
1059*8308f3ffSDavid Ahern 	ifa->rt_priority = cfg->rt_priority;
1060e6464b8cSDavid Ahern 	ifa->flags = cfg->ifa_flags;
106166eb9f86SMahesh Bandewar 	/* No need to add the TENTATIVE flag for addresses with NODAD */
1062e6464b8cSDavid Ahern 	if (!(cfg->ifa_flags & IFA_F_NODAD))
106366eb9f86SMahesh Bandewar 		ifa->flags |= IFA_F_TENTATIVE;
1064e6464b8cSDavid Ahern 	ifa->valid_lft = cfg->valid_lft;
1065e6464b8cSDavid Ahern 	ifa->prefered_lft = cfg->preferred_lft;
10661da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
1067617fe29dSDaniel Borkmann 	ifa->tokenized = false;
10681da177e4SLinus Torvalds 
1069360a9887SDavid Ahern 	ifa->rt = f6i;
107057f5f544SKeir Fraser 
10711da177e4SLinus Torvalds 	ifa->idev = idev;
1072f3d9832eSDavid Ahern 	in6_dev_hold(idev);
1073f3d9832eSDavid Ahern 
10741da177e4SLinus Torvalds 	/* For caller */
1075271201c0SReshetova, Elena 	refcount_set(&ifa->refcnt, 1);
10761da177e4SLinus Torvalds 
1077f3d9832eSDavid Ahern 	rcu_read_lock_bh();
10781da177e4SLinus Torvalds 
1079f3d9832eSDavid Ahern 	err = ipv6_add_addr_hash(idev->dev, ifa);
1080f3d9832eSDavid Ahern 	if (err < 0) {
1081f3d9832eSDavid Ahern 		rcu_read_unlock_bh();
1082f3d9832eSDavid Ahern 		goto out;
1083f3d9832eSDavid Ahern 	}
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	write_lock(&idev->lock);
1086f3d9832eSDavid Ahern 
10871da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
1088e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
1091372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
10921da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
10961da177e4SLinus Torvalds 	write_unlock(&idev->lock);
1097f3d9832eSDavid Ahern 
10988814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
10991da177e4SLinus Torvalds 
1100f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1101f3d9832eSDavid Ahern out:
1102f3d9832eSDavid Ahern 	if (unlikely(err < 0)) {
1103360a9887SDavid Ahern 		fib6_info_release(f6i);
110493531c67SDavid Ahern 
1105f3d9832eSDavid Ahern 		if (ifa) {
1106f3d9832eSDavid Ahern 			if (ifa->idev)
1107f3d9832eSDavid Ahern 				in6_dev_put(ifa->idev);
11081da177e4SLinus Torvalds 			kfree(ifa);
1109f3d9832eSDavid Ahern 		}
11101da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
11111da177e4SLinus Torvalds 	}
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	return ifa;
11141da177e4SLinus Torvalds }
11151da177e4SLinus Torvalds 
11165b84efecSThomas Haller enum cleanup_prefix_rt_t {
11175b84efecSThomas Haller 	CLEANUP_PREFIX_RT_NOP,    /* no cleanup action for prefix route */
11185b84efecSThomas Haller 	CLEANUP_PREFIX_RT_DEL,    /* delete the prefix route */
11195b84efecSThomas Haller 	CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
11205b84efecSThomas Haller };
11215b84efecSThomas Haller 
11225b84efecSThomas Haller /*
11235b84efecSThomas Haller  * Check, whether the prefix for ifp would still need a prefix route
11245b84efecSThomas Haller  * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
11255b84efecSThomas Haller  * constants.
11265b84efecSThomas Haller  *
11275b84efecSThomas Haller  * 1) we don't purge prefix if address was not permanent.
11285b84efecSThomas Haller  *    prefix is managed by its own lifetime.
11295b84efecSThomas Haller  * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
11305b84efecSThomas Haller  * 3) if there are no addresses, delete prefix.
11315b84efecSThomas Haller  * 4) if there are still other permanent address(es),
11325b84efecSThomas Haller  *    corresponding prefix is still permanent.
11335b84efecSThomas Haller  * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
11345b84efecSThomas Haller  *    don't purge the prefix, assume user space is managing it.
11355b84efecSThomas Haller  * 6) otherwise, update prefix lifetime to the
11365b84efecSThomas Haller  *    longest valid lifetime among the corresponding
11375b84efecSThomas Haller  *    addresses on the device.
11385b84efecSThomas Haller  *    Note: subsequent RA will update lifetime.
11395b84efecSThomas Haller  **/
11405b84efecSThomas Haller static enum cleanup_prefix_rt_t
11415b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
11425b84efecSThomas Haller {
11435b84efecSThomas Haller 	struct inet6_ifaddr *ifa;
11445b84efecSThomas Haller 	struct inet6_dev *idev = ifp->idev;
11455b84efecSThomas Haller 	unsigned long lifetime;
11465b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
11475b84efecSThomas Haller 
11485b84efecSThomas Haller 	*expires = jiffies;
11495b84efecSThomas Haller 
11505b84efecSThomas Haller 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
11515b84efecSThomas Haller 		if (ifa == ifp)
11525b84efecSThomas Haller 			continue;
11535b84efecSThomas Haller 		if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
11545b84efecSThomas Haller 				       ifp->prefix_len))
11555b84efecSThomas Haller 			continue;
11565b84efecSThomas Haller 		if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
11575b84efecSThomas Haller 			return CLEANUP_PREFIX_RT_NOP;
11585b84efecSThomas Haller 
11595b84efecSThomas Haller 		action = CLEANUP_PREFIX_RT_EXPIRE;
11605b84efecSThomas Haller 
11615b84efecSThomas Haller 		spin_lock(&ifa->lock);
11625b84efecSThomas Haller 
11635b84efecSThomas Haller 		lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
11645b84efecSThomas Haller 		/*
11655b84efecSThomas Haller 		 * Note: Because this address is
11665b84efecSThomas Haller 		 * not permanent, lifetime <
11675b84efecSThomas Haller 		 * LONG_MAX / HZ here.
11685b84efecSThomas Haller 		 */
11695b84efecSThomas Haller 		if (time_before(*expires, ifa->tstamp + lifetime * HZ))
11705b84efecSThomas Haller 			*expires = ifa->tstamp + lifetime * HZ;
11715b84efecSThomas Haller 		spin_unlock(&ifa->lock);
11725b84efecSThomas Haller 	}
11735b84efecSThomas Haller 
11745b84efecSThomas Haller 	return action;
11755b84efecSThomas Haller }
11765b84efecSThomas Haller 
11775b84efecSThomas Haller static void
11785b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
11795b84efecSThomas Haller {
118093c2fb25SDavid Ahern 	struct fib6_info *f6i;
11815b84efecSThomas Haller 
118293c2fb25SDavid Ahern 	f6i = addrconf_get_prefix_route(&ifp->addr,
11835b84efecSThomas Haller 				       ifp->prefix_len,
11845b84efecSThomas Haller 				       ifp->idev->dev,
11855b84efecSThomas Haller 				       0, RTF_GATEWAY | RTF_DEFAULT);
118693c2fb25SDavid Ahern 	if (f6i) {
11875b84efecSThomas Haller 		if (del_rt)
118893c2fb25SDavid Ahern 			ip6_del_rt(dev_net(ifp->idev->dev), f6i);
11895b84efecSThomas Haller 		else {
119093c2fb25SDavid Ahern 			if (!(f6i->fib6_flags & RTF_EXPIRES))
119193c2fb25SDavid Ahern 				fib6_set_expires(f6i, expires);
119293c2fb25SDavid Ahern 			fib6_info_release(f6i);
11935b84efecSThomas Haller 		}
11945b84efecSThomas Haller 	}
11955b84efecSThomas Haller }
11965b84efecSThomas Haller 
11975b84efecSThomas Haller 
11981da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
12011da177e4SLinus Torvalds {
12024c5ff6a6SHerbert Xu 	int state;
12035b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
12045b84efecSThomas Haller 	unsigned long expires;
12051da177e4SLinus Torvalds 
1206c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
1207c15b1ccaSHannes Frederic Sowa 
12088e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
12094c5ff6a6SHerbert Xu 	state = ifp->state;
1210e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
12118e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
12124c5ff6a6SHerbert Xu 
12134c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
12144c5ff6a6SHerbert Xu 		goto out;
12151da177e4SLinus Torvalds 
12165c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
12175c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
12185c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
12191da177e4SLinus Torvalds 
12205b84efecSThomas Haller 	write_lock_bh(&ifp->idev->lock);
12215d9efa7eSDavid S. Miller 
12221da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
1223372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
12241da177e4SLinus Torvalds 		if (ifp->ifpub) {
12251da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
12261da177e4SLinus Torvalds 			ifp->ifpub = NULL;
12271da177e4SLinus Torvalds 		}
12281da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 
12315b84efecSThomas Haller 	if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
12325b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &expires);
12335b84efecSThomas Haller 
12348ef802aaSEric Dumazet 	list_del_rcu(&ifp->if_list);
12351da177e4SLinus Torvalds 	__in6_ifa_put(ifp);
1236502a2ffdSstephen hemminger 
12375b84efecSThomas Haller 	write_unlock_bh(&ifp->idev->lock);
12381da177e4SLinus Torvalds 
1239c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
1240b2238566SAndrey Vagin 
12411da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
12421da177e4SLinus Torvalds 
1243f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
12441da177e4SLinus Torvalds 
12455b84efecSThomas Haller 	if (action != CLEANUP_PREFIX_RT_NOP) {
12465b84efecSThomas Haller 		cleanup_prefix_route(ifp, expires,
12475b84efecSThomas Haller 			action == CLEANUP_PREFIX_RT_DEL);
12481da177e4SLinus Torvalds 	}
12491da177e4SLinus Torvalds 
1250c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1251c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
12524c5ff6a6SHerbert Xu out:
12531da177e4SLinus Torvalds 	in6_ifa_put(ifp);
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
1256fffcefe9SEric Dumazet static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp,
1257fffcefe9SEric Dumazet 				struct inet6_ifaddr *ift,
1258fffcefe9SEric Dumazet 				bool block)
12591da177e4SLinus Torvalds {
12601da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
12611da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
1262e6464b8cSDavid Ahern 	unsigned long tmp_tstamp, age;
1263eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
1264e6464b8cSDavid Ahern 	struct ifa6_config cfg;
12651da177e4SLinus Torvalds 	int ret = 0;
126676f793e3SLorenzo Colitti 	unsigned long now = jiffies;
126776506a98SJiri Bohac 	long max_desync_factor;
12687aa8e63fSJiri Bohac 	s32 cnf_temp_preferred_lft;
12691da177e4SLinus Torvalds 
127053bd6749SJiri Pirko 	write_lock_bh(&idev->lock);
12711da177e4SLinus Torvalds 	if (ift) {
12721da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
12731da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
12741da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
12751da177e4SLinus Torvalds 		tmpaddr = &addr;
12761da177e4SLinus Torvalds 	} else {
12771da177e4SLinus Torvalds 		tmpaddr = NULL;
12781da177e4SLinus Torvalds 	}
12791da177e4SLinus Torvalds retry:
12801da177e4SLinus Torvalds 	in6_dev_hold(idev);
12811da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
128253bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1283f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
12841da177e4SLinus Torvalds 		in6_dev_put(idev);
12851da177e4SLinus Torvalds 		ret = -1;
12861da177e4SLinus Torvalds 		goto out;
12871da177e4SLinus Torvalds 	}
12881da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
12891da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
12901da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
12911da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
129253bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1293f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1294f3213831SJoe Perches 			__func__);
12951da177e4SLinus Torvalds 		in6_dev_put(idev);
12961da177e4SLinus Torvalds 		ret = -1;
12971da177e4SLinus Torvalds 		goto out;
12981da177e4SLinus Torvalds 	}
12991da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
13001da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
13019d6280daSJiri Bohac 	ipv6_try_regen_rndid(idev, tmpaddr);
13021da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
130376f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
13041da177e4SLinus Torvalds 
1305eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1306eac55bf9SBenoit Boissinot 			idev->cnf.dad_transmits *
13071f9248e5SJiri Pirko 			NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
130876506a98SJiri Bohac 
130976506a98SJiri Bohac 	/* recalculate max_desync_factor each time and update
131076506a98SJiri Bohac 	 * idev->desync_factor if it's larger
131176506a98SJiri Bohac 	 */
13127aa8e63fSJiri Bohac 	cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
131376506a98SJiri Bohac 	max_desync_factor = min_t(__u32,
131476506a98SJiri Bohac 				  idev->cnf.max_desync_factor,
13157aa8e63fSJiri Bohac 				  cnf_temp_preferred_lft - regen_advance);
131676506a98SJiri Bohac 
131776506a98SJiri Bohac 	if (unlikely(idev->desync_factor > max_desync_factor)) {
131876506a98SJiri Bohac 		if (max_desync_factor > 0) {
131976506a98SJiri Bohac 			get_random_bytes(&idev->desync_factor,
132076506a98SJiri Bohac 					 sizeof(idev->desync_factor));
132176506a98SJiri Bohac 			idev->desync_factor %= max_desync_factor;
132276506a98SJiri Bohac 		} else {
132376506a98SJiri Bohac 			idev->desync_factor = 0;
132476506a98SJiri Bohac 		}
132576506a98SJiri Bohac 	}
132676506a98SJiri Bohac 
1327e6464b8cSDavid Ahern 	cfg.valid_lft = min_t(__u32, ifp->valid_lft,
132876506a98SJiri Bohac 			      idev->cnf.temp_valid_lft + age);
1329e6464b8cSDavid Ahern 	cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1330e6464b8cSDavid Ahern 	cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
1331e6464b8cSDavid Ahern 
1332e6464b8cSDavid Ahern 	cfg.plen = ifp->prefix_len;
133376506a98SJiri Bohac 	tmp_tstamp = ifp->tstamp;
133476506a98SJiri Bohac 	spin_unlock_bh(&ifp->lock);
133576506a98SJiri Bohac 
133653bd6749SJiri Pirko 	write_unlock_bh(&idev->lock);
133795c385b4SNeil Horman 
1338eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1339eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1340eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1341eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1342ecab6701SHeiner Kallweit 	 * Use age calculation as in addrconf_verify to avoid unnecessary
1343ecab6701SHeiner Kallweit 	 * temporary addresses being generated.
1344eac55bf9SBenoit Boissinot 	 */
1345ecab6701SHeiner Kallweit 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1346e6464b8cSDavid Ahern 	if (cfg.preferred_lft <= regen_advance + age) {
1347eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1348eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1349eac55bf9SBenoit Boissinot 		ret = -1;
1350eac55bf9SBenoit Boissinot 		goto out;
1351eac55bf9SBenoit Boissinot 	}
1352eac55bf9SBenoit Boissinot 
1353e6464b8cSDavid Ahern 	cfg.ifa_flags = IFA_F_TEMPORARY;
135495c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
135595c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
1356e6464b8cSDavid Ahern 		cfg.ifa_flags |= IFA_F_OPTIMISTIC;
135795c385b4SNeil Horman 
1358e6464b8cSDavid Ahern 	cfg.pfx = &addr;
1359e6464b8cSDavid Ahern 	cfg.scope = ipv6_addr_scope(cfg.pfx);
1360*8308f3ffSDavid Ahern 	cfg.rt_priority = 0;
1361e6464b8cSDavid Ahern 
1362e6464b8cSDavid Ahern 	ift = ipv6_add_addr(idev, &cfg, block, NULL);
13634b08a8f1SHannes Frederic Sowa 	if (IS_ERR(ift)) {
13641da177e4SLinus Torvalds 		in6_ifa_put(ifp);
13651da177e4SLinus Torvalds 		in6_dev_put(idev);
1366f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
13671da177e4SLinus Torvalds 		tmpaddr = &addr;
136853bd6749SJiri Pirko 		write_lock_bh(&idev->lock);
13691da177e4SLinus Torvalds 		goto retry;
13701da177e4SLinus Torvalds 	}
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
13731da177e4SLinus Torvalds 	ift->ifpub = ifp;
137476f793e3SLorenzo Colitti 	ift->cstamp = now;
13751da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
13761da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
13771da177e4SLinus Torvalds 
1378cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
13791da177e4SLinus Torvalds 	in6_ifa_put(ift);
13801da177e4SLinus Torvalds 	in6_dev_put(idev);
13811da177e4SLinus Torvalds out:
13821da177e4SLinus Torvalds 	return ret;
13831da177e4SLinus Torvalds }
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds /*
1386072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
13871da177e4SLinus Torvalds  */
1388a9b05723SYOSHIFUJI Hideaki enum {
1389a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1390a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1391a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1392a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1393a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1394a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1395a9b05723SYOSHIFUJI Hideaki #endif
1396a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1397a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1398a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1399a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1400a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
14017fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
14027fd2561eSErik Kline 	IPV6_SADDR_RULE_NOT_OPTIMISTIC,
14037fd2561eSErik Kline #endif
1404a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1405072047e4SYOSHIFUJI Hideaki };
1406072047e4SYOSHIFUJI Hideaki 
1407a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1408a9b05723SYOSHIFUJI Hideaki 	int			rule;
1409a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1410a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1411a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1412a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1413a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1414a9b05723SYOSHIFUJI Hideaki };
1415a9b05723SYOSHIFUJI Hideaki 
1416a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
14179acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1418a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1419a9b05723SYOSHIFUJI Hideaki 	int scope;
1420a9b05723SYOSHIFUJI Hideaki 	int label;
14217cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1422a9b05723SYOSHIFUJI Hideaki };
1423072047e4SYOSHIFUJI Hideaki 
1424b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
14251da177e4SLinus Torvalds {
142645bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1427072047e4SYOSHIFUJI Hideaki 		return 1;
1428072047e4SYOSHIFUJI Hideaki 	return 0;
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
143135e015e1SMatteo Croce static bool ipv6_use_optimistic_addr(struct net *net,
143235e015e1SMatteo Croce 				     struct inet6_dev *idev)
14337fd2561eSErik Kline {
14347fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
143535e015e1SMatteo Croce 	if (!idev)
143635e015e1SMatteo Croce 		return false;
143735e015e1SMatteo Croce 	if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
143835e015e1SMatteo Croce 		return false;
143935e015e1SMatteo Croce 	if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
144035e015e1SMatteo Croce 		return false;
144135e015e1SMatteo Croce 
144235e015e1SMatteo Croce 	return true;
14437fd2561eSErik Kline #else
14447fd2561eSErik Kline 	return false;
14457fd2561eSErik Kline #endif
14467fd2561eSErik Kline }
14477fd2561eSErik Kline 
1448f1c02cfbSSabrina Dubroca static bool ipv6_allow_optimistic_dad(struct net *net,
1449f1c02cfbSSabrina Dubroca 				      struct inet6_dev *idev)
1450f1c02cfbSSabrina Dubroca {
1451f1c02cfbSSabrina Dubroca #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1452f1c02cfbSSabrina Dubroca 	if (!idev)
1453f1c02cfbSSabrina Dubroca 		return false;
1454f1c02cfbSSabrina Dubroca 	if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1455f1c02cfbSSabrina Dubroca 		return false;
1456f1c02cfbSSabrina Dubroca 
1457f1c02cfbSSabrina Dubroca 	return true;
1458f1c02cfbSSabrina Dubroca #else
1459f1c02cfbSSabrina Dubroca 	return false;
1460f1c02cfbSSabrina Dubroca #endif
1461f1c02cfbSSabrina Dubroca }
1462f1c02cfbSSabrina Dubroca 
14633de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
14643de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1465a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1466a9b05723SYOSHIFUJI Hideaki 			       int i)
1467a9b05723SYOSHIFUJI Hideaki {
1468a9b05723SYOSHIFUJI Hideaki 	int ret;
1469a9b05723SYOSHIFUJI Hideaki 
1470a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1471a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1472a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1473a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1474a9b05723SYOSHIFUJI Hideaki 			break;
1475a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1476a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1477a9b05723SYOSHIFUJI Hideaki 			break;
1478a9b05723SYOSHIFUJI Hideaki 		default:
1479a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1480a9b05723SYOSHIFUJI Hideaki 		}
1481a9b05723SYOSHIFUJI Hideaki 		goto out;
1482a9b05723SYOSHIFUJI Hideaki 	}
1483a9b05723SYOSHIFUJI Hideaki 
1484a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1485a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1486a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1487a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1488a9b05723SYOSHIFUJI Hideaki 		break;
1489a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1490a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1491a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1492a9b05723SYOSHIFUJI Hideaki 		break;
1493a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1494a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1495a9b05723SYOSHIFUJI Hideaki 		 *
1496a9b05723SYOSHIFUJI Hideaki 		 *      ret
1497a9b05723SYOSHIFUJI Hideaki 		 *       ^
1498a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1499a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1500a9b05723SYOSHIFUJI Hideaki 		 *       |
1501a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1502a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1503a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1504db9c7c39Sstephen hemminger 		 *  B-15 |    \        if scope is enough for destination.
1505a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1506a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1507a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1508a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1509a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1510a9b05723SYOSHIFUJI Hideaki 		 *
1511a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1512a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1513a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1514a9b05723SYOSHIFUJI Hideaki 		 */
1515a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1516a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1517a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1518a9b05723SYOSHIFUJI Hideaki 		else
1519a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1520a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1521a9b05723SYOSHIFUJI Hideaki 		break;
1522a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
15237fd2561eSErik Kline 	    {
1524a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
15257fd2561eSErik Kline 		u8 avoid = IFA_F_DEPRECATED;
15267fd2561eSErik Kline 
152735e015e1SMatteo Croce 		if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
15287fd2561eSErik Kline 			avoid |= IFA_F_OPTIMISTIC;
1529a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
15307fd2561eSErik Kline 		      !(score->ifa->flags & avoid);
1531a9b05723SYOSHIFUJI Hideaki 		break;
15327fd2561eSErik Kline 	    }
1533a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1534a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
15357cbca67cSYOSHIFUJI Hideaki 	    {
1536a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
15377cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
15387cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1539a9b05723SYOSHIFUJI Hideaki 		break;
15407cbca67cSYOSHIFUJI Hideaki 	    }
1541a9b05723SYOSHIFUJI Hideaki #endif
1542a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1543a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1544a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1545a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1546a9b05723SYOSHIFUJI Hideaki 		break;
1547a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1548a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
15493de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
15503de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1551a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1552a9b05723SYOSHIFUJI Hideaki 		break;
1553a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
15547cbca67cSYOSHIFUJI Hideaki 	    {
1555a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
155625985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1557a9b05723SYOSHIFUJI Hideaki 		 */
15587cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
15597cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
15607cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
15617cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1562a9b05723SYOSHIFUJI Hideaki 		break;
15637cbca67cSYOSHIFUJI Hideaki 	    }
1564a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1565a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1566a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1567a9b05723SYOSHIFUJI Hideaki 		 */
1568a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1569a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1570a9b05723SYOSHIFUJI Hideaki 		break;
1571a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1572a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
157391b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
157491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
157591b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
157691b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1577a9b05723SYOSHIFUJI Hideaki 		break;
15787fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
15797fd2561eSErik Kline 	case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
15807fd2561eSErik Kline 		/* Optimistic addresses still have lower precedence than other
15817fd2561eSErik Kline 		 * preferred addresses.
15827fd2561eSErik Kline 		 */
15837fd2561eSErik Kline 		ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
15847fd2561eSErik Kline 		break;
15857fd2561eSErik Kline #endif
1586a9b05723SYOSHIFUJI Hideaki 	default:
1587a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1588a9b05723SYOSHIFUJI Hideaki 	}
1589a9b05723SYOSHIFUJI Hideaki 
1590a9b05723SYOSHIFUJI Hideaki 	if (ret)
1591a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1592a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1593a9b05723SYOSHIFUJI Hideaki out:
1594a9b05723SYOSHIFUJI Hideaki 	return ret;
1595a9b05723SYOSHIFUJI Hideaki }
1596a9b05723SYOSHIFUJI Hideaki 
1597c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net,
15989131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_dst *dst,
15999131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct inet6_dev *idev,
1600c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_score *scores,
1601c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				int hiscore_idx)
16021da177e4SLinus Torvalds {
1603c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1604072047e4SYOSHIFUJI Hideaki 
1605f59c031eSEric Dumazet 	list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
1606a9b05723SYOSHIFUJI Hideaki 		int i;
1607072047e4SYOSHIFUJI Hideaki 
1608a9b05723SYOSHIFUJI Hideaki 		/*
16096b3ae80aSYOSHIFUJI Hideaki 		 * - Tentative Address (RFC2462 section 5.4)
16106b3ae80aSYOSHIFUJI Hideaki 		 *  - A tentative address is not considered
16116b3ae80aSYOSHIFUJI Hideaki 		 *    "assigned to an interface" in the traditional
161295c385b4SNeil Horman 		 *    sense, unless it is also flagged as optimistic.
16136b3ae80aSYOSHIFUJI Hideaki 		 * - Candidate Source Address (section 4)
1614072047e4SYOSHIFUJI Hideaki 		 *  - In any case, anycast addresses, multicast
1615072047e4SYOSHIFUJI Hideaki 		 *    addresses, and the unspecified address MUST
1616072047e4SYOSHIFUJI Hideaki 		 *    NOT be included in a candidate set.
1617072047e4SYOSHIFUJI Hideaki 		 */
1618a9b05723SYOSHIFUJI Hideaki 		if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1619a9b05723SYOSHIFUJI Hideaki 		    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
16206b3ae80aSYOSHIFUJI Hideaki 			continue;
1621a9b05723SYOSHIFUJI Hideaki 
1622a9b05723SYOSHIFUJI Hideaki 		score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1623a9b05723SYOSHIFUJI Hideaki 
1624a9b05723SYOSHIFUJI Hideaki 		if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1625a9b05723SYOSHIFUJI Hideaki 			     score->addr_type & IPV6_ADDR_MULTICAST)) {
1626ba7a46f1SJoe Perches 			net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
16279131f3deSYOSHIFUJI Hideaki/吉藤英明 					    idev->dev->name);
16281da177e4SLinus Torvalds 			continue;
1629072047e4SYOSHIFUJI Hideaki 		}
1630072047e4SYOSHIFUJI Hideaki 
1631a9b05723SYOSHIFUJI Hideaki 		score->rule = -1;
1632a9b05723SYOSHIFUJI Hideaki 		bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1633072047e4SYOSHIFUJI Hideaki 
1634a9b05723SYOSHIFUJI Hideaki 		for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1635a9b05723SYOSHIFUJI Hideaki 			int minihiscore, miniscore;
1636072047e4SYOSHIFUJI Hideaki 
16379131f3deSYOSHIFUJI Hideaki/吉藤英明 			minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
16389131f3deSYOSHIFUJI Hideaki/吉藤英明 			miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1639072047e4SYOSHIFUJI Hideaki 
1640a9b05723SYOSHIFUJI Hideaki 			if (minihiscore > miniscore) {
1641a9b05723SYOSHIFUJI Hideaki 				if (i == IPV6_SADDR_RULE_SCOPE &&
1642a9b05723SYOSHIFUJI Hideaki 				    score->scopedist > 0) {
1643a9b05723SYOSHIFUJI Hideaki 					/*
1644a9b05723SYOSHIFUJI Hideaki 					 * special case:
1645a9b05723SYOSHIFUJI Hideaki 					 * each remaining entry
1646a9b05723SYOSHIFUJI Hideaki 					 * has too small (not enough)
1647a9b05723SYOSHIFUJI Hideaki 					 * scope, because ifa entries
1648a9b05723SYOSHIFUJI Hideaki 					 * are sorted by their scope
1649a9b05723SYOSHIFUJI Hideaki 					 * values.
1650072047e4SYOSHIFUJI Hideaki 					 */
16519131f3deSYOSHIFUJI Hideaki/吉藤英明 					goto out;
1652072047e4SYOSHIFUJI Hideaki 				}
1653a9b05723SYOSHIFUJI Hideaki 				break;
1654a9b05723SYOSHIFUJI Hideaki 			} else if (minihiscore < miniscore) {
1655a0bffffcSIlpo Järvinen 				swap(hiscore, score);
1656c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				hiscore_idx = 1 - hiscore_idx;
1657a9b05723SYOSHIFUJI Hideaki 
1658a9b05723SYOSHIFUJI Hideaki 				/* restore our iterator */
1659a9b05723SYOSHIFUJI Hideaki 				score->ifa = hiscore->ifa;
1660a9b05723SYOSHIFUJI Hideaki 
1661a9b05723SYOSHIFUJI Hideaki 				break;
16621da177e4SLinus Torvalds 			}
1663a9b05723SYOSHIFUJI Hideaki 		}
1664a9b05723SYOSHIFUJI Hideaki 	}
16659131f3deSYOSHIFUJI Hideaki/吉藤英明 out:
1666c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	return hiscore_idx;
16671da177e4SLinus Torvalds }
16689131f3deSYOSHIFUJI Hideaki/吉藤英明 
1669afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net,
1670afbac601SDavid Ahern 				 const struct net_device *dst_dev,
1671afbac601SDavid Ahern 				 const struct net_device *master,
1672afbac601SDavid Ahern 				 struct ipv6_saddr_dst *dst,
1673afbac601SDavid Ahern 				 struct ipv6_saddr_score *scores,
1674afbac601SDavid Ahern 				 int hiscore_idx)
1675afbac601SDavid Ahern {
1676afbac601SDavid Ahern 	struct inet6_dev *idev;
1677afbac601SDavid Ahern 
1678afbac601SDavid Ahern 	idev = __in6_dev_get(dst_dev);
1679afbac601SDavid Ahern 	if (idev)
1680afbac601SDavid Ahern 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1681afbac601SDavid Ahern 						   scores, hiscore_idx);
1682afbac601SDavid Ahern 
1683afbac601SDavid Ahern 	idev = __in6_dev_get(master);
1684afbac601SDavid Ahern 	if (idev)
1685afbac601SDavid Ahern 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1686afbac601SDavid Ahern 						   scores, hiscore_idx);
1687afbac601SDavid Ahern 
1688afbac601SDavid Ahern 	return hiscore_idx;
1689afbac601SDavid Ahern }
1690afbac601SDavid Ahern 
16919131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
16929131f3deSYOSHIFUJI Hideaki/吉藤英明 		       const struct in6_addr *daddr, unsigned int prefs,
16939131f3deSYOSHIFUJI Hideaki/吉藤英明 		       struct in6_addr *saddr)
16949131f3deSYOSHIFUJI Hideaki/吉藤英明 {
1695c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score scores[2], *hiscore;
16969131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_dst dst;
16979131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct inet6_dev *idev;
16989131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct net_device *dev;
16999131f3deSYOSHIFUJI Hideaki/吉藤英明 	int dst_type;
17009131f3deSYOSHIFUJI Hideaki/吉藤英明 	bool use_oif_addr = false;
1701c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	int hiscore_idx = 0;
1702cc429c8fSEric Dumazet 	int ret = 0;
17039131f3deSYOSHIFUJI Hideaki/吉藤英明 
17049131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst_type = __ipv6_addr_type(daddr);
17059131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.addr = daddr;
17069131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
17079131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.scope = __ipv6_addr_src_scope(dst_type);
17089131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
17099131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.prefs = prefs;
17109131f3deSYOSHIFUJI Hideaki/吉藤英明 
1711c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].rule = -1;
1712c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].ifa = NULL;
17139131f3deSYOSHIFUJI Hideaki/吉藤英明 
17149131f3deSYOSHIFUJI Hideaki/吉藤英明 	rcu_read_lock();
17159131f3deSYOSHIFUJI Hideaki/吉藤英明 
17169131f3deSYOSHIFUJI Hideaki/吉藤英明 	/* Candidate Source Address (section 4)
17179131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *  - multicast and link-local destination address,
17189131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    the set of candidate source address MUST only
17199131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
17209131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same link as the outgoing
17219131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.
17229131f3deSYOSHIFUJI Hideaki/吉藤英明 	 * (- For site-local destination addresses, the
17239131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    set of candidate source addresses MUST only
17249131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
17259131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same site as the outgoing
17269131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.)
17273985e8a3SErik Kline 	 *  - "It is RECOMMENDED that the candidate source addresses
17283985e8a3SErik Kline 	 *    be the set of unicast addresses assigned to the
17293985e8a3SErik Kline 	 *    interface that will be used to send to the destination
17303985e8a3SErik Kline 	 *    (the 'outgoing' interface)." (RFC 6724)
17319131f3deSYOSHIFUJI Hideaki/吉藤英明 	 */
17329131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (dst_dev) {
17339131f3deSYOSHIFUJI Hideaki/吉藤英明 		idev = __in6_dev_get(dst_dev);
17343985e8a3SErik Kline 		if ((dst_type & IPV6_ADDR_MULTICAST) ||
17353985e8a3SErik Kline 		    dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
17363985e8a3SErik Kline 		    (idev && idev->cnf.use_oif_addrs_only)) {
17379131f3deSYOSHIFUJI Hideaki/吉藤英明 			use_oif_addr = true;
17389131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
17399131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
17409131f3deSYOSHIFUJI Hideaki/吉藤英明 
17419131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (use_oif_addr) {
1742c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 		if (idev)
1743c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
17449131f3deSYOSHIFUJI Hideaki/吉藤英明 	} else {
1745afbac601SDavid Ahern 		const struct net_device *master;
1746afbac601SDavid Ahern 		int master_idx = 0;
1747afbac601SDavid Ahern 
1748afbac601SDavid Ahern 		/* if dst_dev exists and is enslaved to an L3 device, then
1749afbac601SDavid Ahern 		 * prefer addresses from dst_dev and then the master over
1750afbac601SDavid Ahern 		 * any other enslaved devices in the L3 domain.
1751afbac601SDavid Ahern 		 */
1752afbac601SDavid Ahern 		master = l3mdev_master_dev_rcu(dst_dev);
1753afbac601SDavid Ahern 		if (master) {
1754afbac601SDavid Ahern 			master_idx = master->ifindex;
1755afbac601SDavid Ahern 
1756afbac601SDavid Ahern 			hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1757afbac601SDavid Ahern 							    master, &dst,
1758afbac601SDavid Ahern 							    scores, hiscore_idx);
1759afbac601SDavid Ahern 
1760afbac601SDavid Ahern 			if (scores[hiscore_idx].ifa)
1761afbac601SDavid Ahern 				goto out;
1762afbac601SDavid Ahern 		}
1763afbac601SDavid Ahern 
17649131f3deSYOSHIFUJI Hideaki/吉藤英明 		for_each_netdev_rcu(net, dev) {
1765afbac601SDavid Ahern 			/* only consider addresses on devices in the
1766afbac601SDavid Ahern 			 * same L3 domain
1767afbac601SDavid Ahern 			 */
1768afbac601SDavid Ahern 			if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1769afbac601SDavid Ahern 				continue;
17709131f3deSYOSHIFUJI Hideaki/吉藤英明 			idev = __in6_dev_get(dev);
17719131f3deSYOSHIFUJI Hideaki/吉藤英明 			if (!idev)
17729131f3deSYOSHIFUJI Hideaki/吉藤英明 				continue;
1773c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
17749131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
17759131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
1776afbac601SDavid Ahern 
1777afbac601SDavid Ahern out:
1778c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	hiscore = &scores[hiscore_idx];
1779a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1780cc429c8fSEric Dumazet 		ret = -EADDRNOTAVAIL;
1781cc429c8fSEric Dumazet 	else
17824e3fd7a0SAlexey Dobriyan 		*saddr = hiscore->ifa->addr;
1783cc429c8fSEric Dumazet 
1784cc429c8fSEric Dumazet 	rcu_read_unlock();
1785cc429c8fSEric Dumazet 	return ret;
17861da177e4SLinus Torvalds }
17875e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
17881da177e4SLinus Torvalds 
17898965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1790479840ffSJiri Pirko 		      u32 banned_flags)
1791b7b1bfceSHannes Frederic Sowa {
1792b7b1bfceSHannes Frederic Sowa 	struct inet6_ifaddr *ifp;
1793b7b1bfceSHannes Frederic Sowa 	int err = -EADDRNOTAVAIL;
1794b7b1bfceSHannes Frederic Sowa 
1795602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1796602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1797602582caSHannes Frederic Sowa 			break;
1798b7b1bfceSHannes Frederic Sowa 		if (ifp->scope == IFA_LINK &&
1799b7b1bfceSHannes Frederic Sowa 		    !(ifp->flags & banned_flags)) {
1800b7b1bfceSHannes Frederic Sowa 			*addr = ifp->addr;
1801b7b1bfceSHannes Frederic Sowa 			err = 0;
1802b7b1bfceSHannes Frederic Sowa 			break;
1803b7b1bfceSHannes Frederic Sowa 		}
1804b7b1bfceSHannes Frederic Sowa 	}
1805b7b1bfceSHannes Frederic Sowa 	return err;
1806b7b1bfceSHannes Frederic Sowa }
1807b7b1bfceSHannes Frederic Sowa 
180895c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1809479840ffSJiri Pirko 		    u32 banned_flags)
18101da177e4SLinus Torvalds {
18111da177e4SLinus Torvalds 	struct inet6_dev *idev;
18121da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
18131da177e4SLinus Torvalds 
18148814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1815e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1816e21e8467SStephen Hemminger 	if (idev) {
18171da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1818b7b1bfceSHannes Frederic Sowa 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
18191da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
18201da177e4SLinus Torvalds 	}
18218814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
18221da177e4SLinus Torvalds 	return err;
18231da177e4SLinus Torvalds }
18241da177e4SLinus Torvalds 
1825d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev)
18261da177e4SLinus Torvalds {
1827d9bf82c2SEric Dumazet 	const struct inet6_ifaddr *ifp;
18281da177e4SLinus Torvalds 	int cnt = 0;
18291da177e4SLinus Torvalds 
1830d9bf82c2SEric Dumazet 	rcu_read_lock();
1831d9bf82c2SEric Dumazet 	list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
18321da177e4SLinus Torvalds 		cnt++;
1833d9bf82c2SEric Dumazet 	rcu_read_unlock();
18341da177e4SLinus Torvalds 	return cnt;
18351da177e4SLinus Torvalds }
18361da177e4SLinus Torvalds 
1837b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
18382a7851bfSFlorian Westphal 		  const struct net_device *dev, int strict)
18391da177e4SLinus Torvalds {
1840232378e8SDavid Ahern 	return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1841232378e8SDavid Ahern 				       strict, IFA_F_TENTATIVE);
1842c58da4c6SErik Kline }
1843c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr);
1844c58da4c6SErik Kline 
18451893ff20SDavid Ahern /* device argument is used to find the L3 domain of interest. If
18461893ff20SDavid Ahern  * skip_dev_check is set, then the ifp device is not checked against
18471893ff20SDavid Ahern  * the passed in dev argument. So the 2 cases for addresses checks are:
18481893ff20SDavid Ahern  *   1. does the address exist in the L3 domain that dev is part of
18491893ff20SDavid Ahern  *      (skip_dev_check = true), or
18501893ff20SDavid Ahern  *
18511893ff20SDavid Ahern  *   2. does the address exist on the specific device
18521893ff20SDavid Ahern  *      (skip_dev_check = false)
18531893ff20SDavid Ahern  */
1854c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1855232378e8SDavid Ahern 			    const struct net_device *dev, bool skip_dev_check,
1856232378e8SDavid Ahern 			    int strict, u32 banned_flags)
1857c58da4c6SErik Kline {
18583f27fb23SEric Dumazet 	unsigned int hash = inet6_addr_hash(net, addr);
18591893ff20SDavid Ahern 	const struct net_device *l3mdev;
1860eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1861c58da4c6SErik Kline 	u32 ifp_flags;
18621da177e4SLinus Torvalds 
1863480318a0SEric Dumazet 	rcu_read_lock();
1864232378e8SDavid Ahern 
18651893ff20SDavid Ahern 	l3mdev = l3mdev_master_dev_rcu(dev);
1866232378e8SDavid Ahern 	if (skip_dev_check)
1867232378e8SDavid Ahern 		dev = NULL;
1868232378e8SDavid Ahern 
1869b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1870878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1871bfeade08SDaniel Lezcano 			continue;
18721893ff20SDavid Ahern 
18731893ff20SDavid Ahern 		if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev)
18741893ff20SDavid Ahern 			continue;
18751893ff20SDavid Ahern 
1876c58da4c6SErik Kline 		/* Decouple optimistic from tentative for evaluation here.
1877c58da4c6SErik Kline 		 * Ban optimistic addresses explicitly, when required.
1878c58da4c6SErik Kline 		 */
1879c58da4c6SErik Kline 		ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1880c58da4c6SErik Kline 			    ? (ifp->flags&~IFA_F_TENTATIVE)
1881c58da4c6SErik Kline 			    : ifp->flags;
18821da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1883c58da4c6SErik Kline 		    !(ifp_flags&banned_flags) &&
188463159f29SIan Morris 		    (!dev || ifp->idev->dev == dev ||
1885eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1886480318a0SEric Dumazet 			rcu_read_unlock();
1887eedf042aSStephen Hemminger 			return 1;
1888eedf042aSStephen Hemminger 		}
1889eedf042aSStephen Hemminger 	}
18905c578aedSstephen hemminger 
1891480318a0SEric Dumazet 	rcu_read_unlock();
1892eedf042aSStephen Hemminger 	return 0;
18931da177e4SLinus Torvalds }
1894c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
18957159039aSYOSHIFUJI Hideaki 
18961da177e4SLinus Torvalds 
18977df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev.
18987df37ff3SCatalin\(ux\) M. BOIE  * If one is found it returns true.
18997df37ff3SCatalin\(ux\) M. BOIE  */
19007df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
19017df37ff3SCatalin\(ux\) M. BOIE 	const unsigned int prefix_len, struct net_device *dev)
19027df37ff3SCatalin\(ux\) M. BOIE {
190347e26941SEric Dumazet 	const struct inet6_ifaddr *ifa;
190447e26941SEric Dumazet 	const struct inet6_dev *idev;
19057df37ff3SCatalin\(ux\) M. BOIE 	bool ret = false;
19067df37ff3SCatalin\(ux\) M. BOIE 
19077df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_lock();
19087df37ff3SCatalin\(ux\) M. BOIE 	idev = __in6_dev_get(dev);
19097df37ff3SCatalin\(ux\) M. BOIE 	if (idev) {
191047e26941SEric Dumazet 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
19117df37ff3SCatalin\(ux\) M. BOIE 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
19127df37ff3SCatalin\(ux\) M. BOIE 			if (ret)
19137df37ff3SCatalin\(ux\) M. BOIE 				break;
19147df37ff3SCatalin\(ux\) M. BOIE 		}
19157df37ff3SCatalin\(ux\) M. BOIE 	}
19167df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_unlock();
19177df37ff3SCatalin\(ux\) M. BOIE 
19187df37ff3SCatalin\(ux\) M. BOIE 	return ret;
19197df37ff3SCatalin\(ux\) M. BOIE }
19207df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix);
19217df37ff3SCatalin\(ux\) M. BOIE 
1922b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
192352eeeb84SYOSHIFUJI Hideaki {
192424ba333bSEric Dumazet 	const struct inet6_ifaddr *ifa;
192524ba333bSEric Dumazet 	const struct inet6_dev *idev;
192652eeeb84SYOSHIFUJI Hideaki 	int	onlink;
192752eeeb84SYOSHIFUJI Hideaki 
192852eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
192952eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
193052eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
193152eeeb84SYOSHIFUJI Hideaki 	if (idev) {
193224ba333bSEric Dumazet 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
193352eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
193452eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
193552eeeb84SYOSHIFUJI Hideaki 			if (onlink)
193652eeeb84SYOSHIFUJI Hideaki 				break;
193752eeeb84SYOSHIFUJI Hideaki 		}
193852eeeb84SYOSHIFUJI Hideaki 	}
193952eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
194052eeeb84SYOSHIFUJI Hideaki 	return onlink;
194152eeeb84SYOSHIFUJI Hideaki }
194252eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
194352eeeb84SYOSHIFUJI Hideaki 
19449acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
19451cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
19461da177e4SLinus Torvalds {
19473f27fb23SEric Dumazet 	unsigned int hash = inet6_addr_hash(net, addr);
1948b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
19491da177e4SLinus Torvalds 
195024f226daSEric Dumazet 	rcu_read_lock();
195124f226daSEric Dumazet 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1952878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
19531cab3da6SDaniel Lezcano 			continue;
19541da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
195563159f29SIan Morris 			if (!dev || ifp->idev->dev == dev ||
19561da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1957b79d1d54SDavid S. Miller 				result = ifp;
19581da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
19591da177e4SLinus Torvalds 				break;
19601da177e4SLinus Torvalds 			}
19611da177e4SLinus Torvalds 		}
19621da177e4SLinus Torvalds 	}
196324f226daSEric Dumazet 	rcu_read_unlock();
19641da177e4SLinus Torvalds 
1965b79d1d54SDavid S. Miller 	return result;
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
19691da177e4SLinus Torvalds 
1970cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
19711da177e4SLinus Torvalds {
19723d171f39SLubomir Rintel 	if (dad_failed)
19733d171f39SLubomir Rintel 		ifp->flags |= IFA_F_DADFAILED;
19743d171f39SLubomir Rintel 
1975ec8add2aSSabrina Dubroca 	if (ifp->flags&IFA_F_TEMPORARY) {
19761da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
19771da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
19781da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
19791da177e4SLinus Torvalds 		if (ifpub) {
19801da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
19811da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
1982fffcefe9SEric Dumazet 			ipv6_create_tempaddr(ifpub, ifp, true);
19831da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
19841da177e4SLinus Torvalds 		} else {
19851da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
19861da177e4SLinus Torvalds 		}
19871da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
1988ec8add2aSSabrina Dubroca 	} else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
1989ec8add2aSSabrina Dubroca 		spin_lock_bh(&ifp->lock);
1990ec8add2aSSabrina Dubroca 		addrconf_del_dad_work(ifp);
1991ec8add2aSSabrina Dubroca 		ifp->flags |= IFA_F_TENTATIVE;
1992f1c02cfbSSabrina Dubroca 		if (dad_failed)
1993f1c02cfbSSabrina Dubroca 			ifp->flags &= ~IFA_F_OPTIMISTIC;
1994ec8add2aSSabrina Dubroca 		spin_unlock_bh(&ifp->lock);
1995ec8add2aSSabrina Dubroca 		if (dad_failed)
1996ec8add2aSSabrina Dubroca 			ipv6_ifa_notify(0, ifp);
1997ec8add2aSSabrina Dubroca 		in6_ifa_put(ifp);
1998c15b1ccaSHannes Frederic Sowa 	} else {
19991da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
20001da177e4SLinus Torvalds 	}
2001c15b1ccaSHannes Frederic Sowa }
20021da177e4SLinus Torvalds 
2003f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2004f2344a13SHerbert Xu {
2005f2344a13SHerbert Xu 	int err = -ENOENT;
2006f2344a13SHerbert Xu 
20078e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
2008f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
2009f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
2010f2344a13SHerbert Xu 		err = 0;
2011f2344a13SHerbert Xu 	}
20128e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
2013f2344a13SHerbert Xu 
2014f2344a13SHerbert Xu 	return err;
2015f2344a13SHerbert Xu }
2016f2344a13SHerbert Xu 
2017da13c59bSVishwanath Pai void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
20183c21edbdSYOSHIFUJI Hideaki {
20191b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
20201855b7c3SHannes Frederic Sowa 	struct net *net = dev_net(ifp->idev->dev);
20219bdd8d40SBrian Haley 
2022853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
2023853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
2024f2344a13SHerbert Xu 		return;
2025853dc2e0SUrsula Braun 	}
2026f2344a13SHerbert Xu 
2027da13c59bSVishwanath Pai 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2028da13c59bSVishwanath Pai 			     ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
20299bdd8d40SBrian Haley 
20305f40ef77SHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
20311b34be74SYOSHIFUJI Hideaki 
20325f40ef77SHannes Frederic Sowa 	if (ifp->flags & IFA_F_STABLE_PRIVACY) {
20335f40ef77SHannes Frederic Sowa 		struct in6_addr new_addr;
20345f40ef77SHannes Frederic Sowa 		struct inet6_ifaddr *ifp2;
20355f40ef77SHannes Frederic Sowa 		int retries = ifp->stable_privacy_retry + 1;
2036e6464b8cSDavid Ahern 		struct ifa6_config cfg = {
2037e6464b8cSDavid Ahern 			.pfx = &new_addr,
2038e6464b8cSDavid Ahern 			.plen = ifp->prefix_len,
2039e6464b8cSDavid Ahern 			.ifa_flags = ifp->flags,
2040e6464b8cSDavid Ahern 			.valid_lft = ifp->valid_lft,
2041e6464b8cSDavid Ahern 			.preferred_lft = ifp->prefered_lft,
2042e6464b8cSDavid Ahern 			.scope = ifp->scope,
2043e6464b8cSDavid Ahern 		};
20445f40ef77SHannes Frederic Sowa 
20451855b7c3SHannes Frederic Sowa 		if (retries > net->ipv6.sysctl.idgen_retries) {
20465f40ef77SHannes Frederic Sowa 			net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
20475f40ef77SHannes Frederic Sowa 					     ifp->idev->dev->name);
20485f40ef77SHannes Frederic Sowa 			goto errdad;
20495f40ef77SHannes Frederic Sowa 		}
20505f40ef77SHannes Frederic Sowa 
20515f40ef77SHannes Frederic Sowa 		new_addr = ifp->addr;
20525f40ef77SHannes Frederic Sowa 		if (ipv6_generate_stable_address(&new_addr, retries,
20535f40ef77SHannes Frederic Sowa 						 idev))
20545f40ef77SHannes Frederic Sowa 			goto errdad;
20555f40ef77SHannes Frederic Sowa 
20565f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp->lock);
20575f40ef77SHannes Frederic Sowa 
20585f40ef77SHannes Frederic Sowa 		if (idev->cnf.max_addresses &&
20595f40ef77SHannes Frederic Sowa 		    ipv6_count_addresses(idev) >=
20605f40ef77SHannes Frederic Sowa 		    idev->cnf.max_addresses)
20615f40ef77SHannes Frederic Sowa 			goto lock_errdad;
20625f40ef77SHannes Frederic Sowa 
20635f40ef77SHannes Frederic Sowa 		net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
20645f40ef77SHannes Frederic Sowa 				     ifp->idev->dev->name);
20655f40ef77SHannes Frederic Sowa 
2066e6464b8cSDavid Ahern 		ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
20675f40ef77SHannes Frederic Sowa 		if (IS_ERR(ifp2))
20685f40ef77SHannes Frederic Sowa 			goto lock_errdad;
20695f40ef77SHannes Frederic Sowa 
20705f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp2->lock);
20715f40ef77SHannes Frederic Sowa 		ifp2->stable_privacy_retry = retries;
20725f40ef77SHannes Frederic Sowa 		ifp2->state = INET6_IFADDR_STATE_PREDAD;
20735f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp2->lock);
20745f40ef77SHannes Frederic Sowa 
20751855b7c3SHannes Frederic Sowa 		addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
20765f40ef77SHannes Frederic Sowa 		in6_ifa_put(ifp2);
20775f40ef77SHannes Frederic Sowa lock_errdad:
20785f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp->lock);
20791b34be74SYOSHIFUJI Hideaki 	}
20801b34be74SYOSHIFUJI Hideaki 
20815f40ef77SHannes Frederic Sowa errdad:
2082c15b1ccaSHannes Frederic Sowa 	/* transition from _POSTDAD to _ERRDAD */
2083c15b1ccaSHannes Frederic Sowa 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
20848e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
2085c15b1ccaSHannes Frederic Sowa 
2086c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, 0);
2087751eb6b6SWei Yongjun 	in6_ifa_put(ifp);
20883c21edbdSYOSHIFUJI Hideaki }
20891da177e4SLinus Torvalds 
2090a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group.
2091a9ed4a29SSabrina Dubroca  * caller must hold RTNL */
2092b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
20931da177e4SLinus Torvalds {
20941da177e4SLinus Torvalds 	struct in6_addr maddr;
20951da177e4SLinus Torvalds 
20961da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
20971da177e4SLinus Torvalds 		return;
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
21001da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
21011da177e4SLinus Torvalds }
21021da177e4SLinus Torvalds 
2103a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
2104b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
21051da177e4SLinus Torvalds {
21061da177e4SLinus Torvalds 	struct in6_addr maddr;
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
21091da177e4SLinus Torvalds 		return;
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
21121da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds 
2115a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
211620380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
21171da177e4SLinus Torvalds {
21181da177e4SLinus Torvalds 	struct in6_addr addr;
2119c15b1ccaSHannes Frederic Sowa 
212088ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
21212bda8a0cSBjørn Mork 		return;
21221da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
21231da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
21241da177e4SLinus Torvalds 		return;
2125013b4d90SWANG Cong 	__ipv6_dev_ac_inc(ifp->idev, &addr);
21261da177e4SLinus Torvalds }
21271da177e4SLinus Torvalds 
2128a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
212920380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
21301da177e4SLinus Torvalds {
21311da177e4SLinus Torvalds 	struct in6_addr addr;
2132c15b1ccaSHannes Frederic Sowa 
213388ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
213432019e65SYOSHIFUJI Hideaki 		return;
21351da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
21361da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
21371da177e4SLinus Torvalds 		return;
21381da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
21391da177e4SLinus Torvalds }
21401da177e4SLinus Torvalds 
21418a7a4b47SAlexander Aring static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
214206a4c1c5Salex.bluesman.smirnov@gmail.com {
21438a7a4b47SAlexander Aring 	switch (dev->addr_len) {
21448a7a4b47SAlexander Aring 	case ETH_ALEN:
21459dae2e03SLuiz Augusto von Dentz 		memcpy(eui, dev->dev_addr, 3);
21469dae2e03SLuiz Augusto von Dentz 		eui[3] = 0xFF;
21479dae2e03SLuiz Augusto von Dentz 		eui[4] = 0xFE;
21489dae2e03SLuiz Augusto von Dentz 		memcpy(eui + 5, dev->dev_addr + 3, 3);
21499dae2e03SLuiz Augusto von Dentz 		break;
21508a7a4b47SAlexander Aring 	case EUI64_ADDR_LEN:
21515241c2d7SAlexander Aring 		memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
21525e98a36eSYOSHIFUJI Hideaki / 吉藤英明 		eui[0] ^= 2;
21538a7a4b47SAlexander Aring 		break;
21548a7a4b47SAlexander Aring 	default:
21558a7a4b47SAlexander Aring 		return -1;
21568a7a4b47SAlexander Aring 	}
21578a7a4b47SAlexander Aring 
215806a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
215906a4c1c5Salex.bluesman.smirnov@gmail.com }
216006a4c1c5Salex.bluesman.smirnov@gmail.com 
2161cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2162cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
2163cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	union fwnet_hwaddr *ha;
2164cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2165cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
2166cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
2167cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2168cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	ha = (union fwnet_hwaddr *)dev->dev_addr;
2169cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2170cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2171cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
2172cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
2173cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
2174cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2175073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2176073a8e0eSYOSHIFUJI Hideaki {
21771da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
21781da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
21791da177e4SLinus Torvalds 		return -1;
21801da177e4SLinus Torvalds 	memset(eui, 0, 7);
21811da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
21821da177e4SLinus Torvalds 	return 0;
2183073a8e0eSYOSHIFUJI Hideaki }
2184073a8e0eSYOSHIFUJI Hideaki 
2185073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2186073a8e0eSYOSHIFUJI Hideaki {
21871da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
21881da177e4SLinus Torvalds 		return -1;
21891da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
21901da177e4SLinus Torvalds 	eui[0] |= 2;
21911da177e4SLinus Torvalds 	return 0;
21921da177e4SLinus Torvalds }
2193073a8e0eSYOSHIFUJI Hideaki 
2194c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2195dfd982baSYOSHIFUJI Hideaki {
21969af28511SSascha Hlusiak 	if (addr == 0)
21979af28511SSascha Hlusiak 		return -1;
2198dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2199dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2200dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2201dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2202dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2203dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2204dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
2205dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
2206dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
2207dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
2208dfd982baSYOSHIFUJI Hideaki 	return 0;
2209dfd982baSYOSHIFUJI Hideaki }
2210dfd982baSYOSHIFUJI Hideaki 
2211dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2212dfd982baSYOSHIFUJI Hideaki {
2213dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
2214dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2215dfd982baSYOSHIFUJI Hideaki 	return -1;
2216dfd982baSYOSHIFUJI Hideaki }
2217dfd982baSYOSHIFUJI Hideaki 
2218aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2219aee80b54Sstephen hemminger {
2220aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2221aee80b54Sstephen hemminger }
2222aee80b54Sstephen hemminger 
2223e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2224e837735eSNicolas Dichtel {
2225e837735eSNicolas Dichtel 	memcpy(eui, dev->perm_addr, 3);
2226e837735eSNicolas Dichtel 	memcpy(eui + 5, dev->perm_addr + 3, 3);
2227e837735eSNicolas Dichtel 	eui[3] = 0xFF;
2228e837735eSNicolas Dichtel 	eui[4] = 0xFE;
2229e837735eSNicolas Dichtel 	eui[0] ^= 2;
2230e837735eSNicolas Dichtel 	return 0;
2231e837735eSNicolas Dichtel }
2232e837735eSNicolas Dichtel 
2233073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2234073a8e0eSYOSHIFUJI Hideaki {
2235073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
2236073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
2237073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
2238073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
2239073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
2240073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
2241073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
2242073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
2243c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
2244dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
2245aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
224645ce0fd1SFelix Jia 	case ARPHRD_TUNNEL:
2247aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
2248e74bccb8SJukka Rissanen 	case ARPHRD_6LOWPAN:
22498a7a4b47SAlexander Aring 		return addrconf_ifid_6lowpan(eui, dev);
2250cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
2251cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
2252e837735eSNicolas Dichtel 	case ARPHRD_TUNNEL6:
225345ce0fd1SFelix Jia 	case ARPHRD_IP6GRE:
2254e837735eSNicolas Dichtel 		return addrconf_ifid_ip6tnl(eui, dev);
2255073a8e0eSYOSHIFUJI Hideaki 	}
22561da177e4SLinus Torvalds 	return -1;
22571da177e4SLinus Torvalds }
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
22601da177e4SLinus Torvalds {
22611da177e4SLinus Torvalds 	int err = -1;
22621da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
22631da177e4SLinus Torvalds 
22641da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2265602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2266602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
2267602582caSHannes Frederic Sowa 			break;
22681da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
22691da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
22701da177e4SLinus Torvalds 			err = 0;
22711da177e4SLinus Torvalds 			break;
22721da177e4SLinus Torvalds 		}
22731da177e4SLinus Torvalds 	}
22741da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
22751da177e4SLinus Torvalds 	return err;
22761da177e4SLinus Torvalds }
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
22799d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev)
22801da177e4SLinus Torvalds {
22811da177e4SLinus Torvalds regen:
2282955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
22831da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
22841da177e4SLinus Torvalds 
22851da177e4SLinus Torvalds 	/*
22861da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
22871da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
22881da177e4SLinus Torvalds 	 *
22891da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
22901da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
2291c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
22921da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
22931da177e4SLinus Torvalds 	 *  - value 0
22941da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
22951da177e4SLinus Torvalds 	 */
22961da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
22971da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
22981da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
22991da177e4SLinus Torvalds 		goto regen;
23001da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
23011da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
23021da177e4SLinus Torvalds 			goto regen;
23031da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
23041da177e4SLinus Torvalds 			goto regen;
23051da177e4SLinus Torvalds 	}
23061da177e4SLinus Torvalds }
23071da177e4SLinus Torvalds 
23089d6280daSJiri Bohac static void  ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
23098e5e8f30SEldad Zack {
23101da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
23119d6280daSJiri Bohac 		ipv6_regen_rndid(idev);
23121da177e4SLinus Torvalds }
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds /*
23151da177e4SLinus Torvalds  *	Add prefix route.
23161da177e4SLinus Torvalds  */
23171da177e4SLinus Torvalds 
23181da177e4SLinus Torvalds static void
2319*8308f3ffSDavid Ahern addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
2320*8308f3ffSDavid Ahern 		      struct net_device *dev, unsigned long expires,
2321*8308f3ffSDavid Ahern 		      u32 flags, gfp_t gfp_flags)
23221da177e4SLinus Torvalds {
232386872cb5SThomas Graf 	struct fib6_config cfg = {
2324ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2325*8308f3ffSDavid Ahern 		.fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
232686872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
232786872cb5SThomas Graf 		.fc_expires = expires,
232886872cb5SThomas Graf 		.fc_dst_len = plen,
232986872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
2330c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
2331f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
2332e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
233386872cb5SThomas Graf 	};
23341da177e4SLinus Torvalds 
23354e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
23381da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
23391da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
23401da177e4SLinus Torvalds 	 */
234107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
23421da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
234386872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
23440be669bbSJoerg Roedel #endif
23451da177e4SLinus Torvalds 
2346acb54e3cSDavid Ahern 	ip6_route_add(&cfg, gfp_flags, NULL);
23471da177e4SLinus Torvalds }
23481da177e4SLinus Torvalds 
234914ef37b6SAndreas Hofmeister 
23508d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
235114ef37b6SAndreas Hofmeister 						  int plen,
235214ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
235314ef37b6SAndreas Hofmeister 						  u32 flags, u32 noflags)
235414ef37b6SAndreas Hofmeister {
235514ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
23568d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
235714ef37b6SAndreas Hofmeister 	struct fib6_table *table;
2358ca254490SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
235914ef37b6SAndreas Hofmeister 
2360ca254490SDavid Ahern 	table = fib6_get_table(dev_net(dev), tb_id);
236163159f29SIan Morris 	if (!table)
236214ef37b6SAndreas Hofmeister 		return NULL;
236314ef37b6SAndreas Hofmeister 
236466f5d6ceSWei Wang 	rcu_read_lock();
236538fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
236614ef37b6SAndreas Hofmeister 	if (!fn)
236714ef37b6SAndreas Hofmeister 		goto out;
23681f56a01fSMartin KaFai Lau 
236966f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
23705e670d84SDavid Ahern 		if (rt->fib6_nh.nh_dev->ifindex != dev->ifindex)
237114ef37b6SAndreas Hofmeister 			continue;
237293c2fb25SDavid Ahern 		if ((rt->fib6_flags & flags) != flags)
237314ef37b6SAndreas Hofmeister 			continue;
237493c2fb25SDavid Ahern 		if ((rt->fib6_flags & noflags) != 0)
237514ef37b6SAndreas Hofmeister 			continue;
237693531c67SDavid Ahern 		fib6_info_hold(rt);
237714ef37b6SAndreas Hofmeister 		break;
237814ef37b6SAndreas Hofmeister 	}
237914ef37b6SAndreas Hofmeister out:
238066f5d6ceSWei Wang 	rcu_read_unlock();
238114ef37b6SAndreas Hofmeister 	return rt;
238214ef37b6SAndreas Hofmeister }
238314ef37b6SAndreas Hofmeister 
238414ef37b6SAndreas Hofmeister 
23851da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
23881da177e4SLinus Torvalds {
238986872cb5SThomas Graf 	struct fib6_config cfg = {
2390ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
239186872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
239286872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
239386872cb5SThomas Graf 		.fc_dst_len = 8,
239486872cb5SThomas Graf 		.fc_flags = RTF_UP,
2395e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
2396c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
239786872cb5SThomas Graf 	};
23981da177e4SLinus Torvalds 
239986872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
240086872cb5SThomas Graf 
2401acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
24021da177e4SLinus Torvalds }
24031da177e4SLinus Torvalds 
24041da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
24051da177e4SLinus Torvalds {
24061da177e4SLinus Torvalds 	struct inet6_dev *idev;
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds 	ASSERT_RTNL();
24091da177e4SLinus Torvalds 
2410e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2411e21e8467SStephen Hemminger 	if (!idev)
241264e724f6SBrian Haley 		return ERR_PTR(-ENOBUFS);
241364e724f6SBrian Haley 
241464e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
241564e724f6SBrian Haley 		return ERR_PTR(-EACCES);
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds 	/* Add default multicast route */
2418ba46ee4cSDavid Ahern 	if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
24191da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 	return idev;
24221da177e4SLinus Torvalds }
24231da177e4SLinus Torvalds 
242453bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev,
242553bd6749SJiri Pirko 			     struct inet6_ifaddr *ifp,
242653bd6749SJiri Pirko 			     __u32 valid_lft, __u32 prefered_lft,
242753bd6749SJiri Pirko 			     bool create, unsigned long now)
242853bd6749SJiri Pirko {
242953bd6749SJiri Pirko 	u32 flags;
243053bd6749SJiri Pirko 	struct inet6_ifaddr *ift;
243153bd6749SJiri Pirko 
243253bd6749SJiri Pirko 	read_lock_bh(&idev->lock);
243353bd6749SJiri Pirko 	/* update all temporary addresses in the list */
243453bd6749SJiri Pirko 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
243553bd6749SJiri Pirko 		int age, max_valid, max_prefered;
243653bd6749SJiri Pirko 
243753bd6749SJiri Pirko 		if (ifp != ift->ifpub)
243853bd6749SJiri Pirko 			continue;
243953bd6749SJiri Pirko 
244053bd6749SJiri Pirko 		/* RFC 4941 section 3.3:
244153bd6749SJiri Pirko 		 * If a received option will extend the lifetime of a public
244253bd6749SJiri Pirko 		 * address, the lifetimes of temporary addresses should
244353bd6749SJiri Pirko 		 * be extended, subject to the overall constraint that no
244453bd6749SJiri Pirko 		 * temporary addresses should ever remain "valid" or "preferred"
244553bd6749SJiri Pirko 		 * for a time longer than (TEMP_VALID_LIFETIME) or
244653bd6749SJiri Pirko 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
244753bd6749SJiri Pirko 		 */
244853bd6749SJiri Pirko 		age = (now - ift->cstamp) / HZ;
244953bd6749SJiri Pirko 		max_valid = idev->cnf.temp_valid_lft - age;
245053bd6749SJiri Pirko 		if (max_valid < 0)
245153bd6749SJiri Pirko 			max_valid = 0;
245253bd6749SJiri Pirko 
245353bd6749SJiri Pirko 		max_prefered = idev->cnf.temp_prefered_lft -
245476506a98SJiri Bohac 			       idev->desync_factor - age;
245553bd6749SJiri Pirko 		if (max_prefered < 0)
245653bd6749SJiri Pirko 			max_prefered = 0;
245753bd6749SJiri Pirko 
245853bd6749SJiri Pirko 		if (valid_lft > max_valid)
245953bd6749SJiri Pirko 			valid_lft = max_valid;
246053bd6749SJiri Pirko 
246153bd6749SJiri Pirko 		if (prefered_lft > max_prefered)
246253bd6749SJiri Pirko 			prefered_lft = max_prefered;
246353bd6749SJiri Pirko 
246453bd6749SJiri Pirko 		spin_lock(&ift->lock);
246553bd6749SJiri Pirko 		flags = ift->flags;
246653bd6749SJiri Pirko 		ift->valid_lft = valid_lft;
246753bd6749SJiri Pirko 		ift->prefered_lft = prefered_lft;
246853bd6749SJiri Pirko 		ift->tstamp = now;
246953bd6749SJiri Pirko 		if (prefered_lft > 0)
247053bd6749SJiri Pirko 			ift->flags &= ~IFA_F_DEPRECATED;
247153bd6749SJiri Pirko 
247253bd6749SJiri Pirko 		spin_unlock(&ift->lock);
247353bd6749SJiri Pirko 		if (!(flags&IFA_F_TENTATIVE))
247453bd6749SJiri Pirko 			ipv6_ifa_notify(0, ift);
247553bd6749SJiri Pirko 	}
247653bd6749SJiri Pirko 
247753bd6749SJiri Pirko 	if ((create || list_empty(&idev->tempaddr_list)) &&
247853bd6749SJiri Pirko 	    idev->cnf.use_tempaddr > 0) {
247953bd6749SJiri Pirko 		/* When a new public address is created as described
248053bd6749SJiri Pirko 		 * in [ADDRCONF], also create a new temporary address.
248153bd6749SJiri Pirko 		 * Also create a temporary address if it's enabled but
248253bd6749SJiri Pirko 		 * no temporary address currently exists.
248353bd6749SJiri Pirko 		 */
248453bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
2485fffcefe9SEric Dumazet 		ipv6_create_tempaddr(ifp, NULL, false);
248653bd6749SJiri Pirko 	} else {
248753bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
248853bd6749SJiri Pirko 	}
248953bd6749SJiri Pirko }
249053bd6749SJiri Pirko 
2491cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2492cc9da6ccSBjørn Mork {
2493d35a00b8SFelix Jia 	return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2494d35a00b8SFelix Jia 	       idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2495cc9da6ccSBjørn Mork }
2496cc9da6ccSBjørn Mork 
2497cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
24984f672235SAlexander Aring 				 const struct prefix_info *pinfo,
24994f672235SAlexander Aring 				 struct inet6_dev *in6_dev,
2500cc84b3c6SAlexander Aring 				 const struct in6_addr *addr, int addr_type,
2501cc84b3c6SAlexander Aring 				 u32 addr_flags, bool sllao, bool tokenized,
25024f672235SAlexander Aring 				 __u32 valid_lft, u32 prefered_lft)
25034f672235SAlexander Aring {
25044f672235SAlexander Aring 	struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
25054f672235SAlexander Aring 	int create = 0, update_lft = 0;
25064f672235SAlexander Aring 
25074f672235SAlexander Aring 	if (!ifp && valid_lft) {
25084f672235SAlexander Aring 		int max_addresses = in6_dev->cnf.max_addresses;
2509e6464b8cSDavid Ahern 		struct ifa6_config cfg = {
2510e6464b8cSDavid Ahern 			.pfx = addr,
2511e6464b8cSDavid Ahern 			.plen = pinfo->prefix_len,
2512e6464b8cSDavid Ahern 			.ifa_flags = addr_flags,
2513e6464b8cSDavid Ahern 			.valid_lft = valid_lft,
2514e6464b8cSDavid Ahern 			.preferred_lft = prefered_lft,
2515e6464b8cSDavid Ahern 			.scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2516e6464b8cSDavid Ahern 		};
25174f672235SAlexander Aring 
25184f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
251935e015e1SMatteo Croce 		if ((net->ipv6.devconf_all->optimistic_dad ||
252035e015e1SMatteo Croce 		     in6_dev->cnf.optimistic_dad) &&
25214f672235SAlexander Aring 		    !net->ipv6.devconf_all->forwarding && sllao)
2522e6464b8cSDavid Ahern 			cfg.ifa_flags |= IFA_F_OPTIMISTIC;
25234f672235SAlexander Aring #endif
25244f672235SAlexander Aring 
25254f672235SAlexander Aring 		/* Do not allow to create too much of autoconfigured
25264f672235SAlexander Aring 		 * addresses; this would be too easy way to crash kernel.
25274f672235SAlexander Aring 		 */
25284f672235SAlexander Aring 		if (!max_addresses ||
25294f672235SAlexander Aring 		    ipv6_count_addresses(in6_dev) < max_addresses)
2530e6464b8cSDavid Ahern 			ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
25314f672235SAlexander Aring 
25324f672235SAlexander Aring 		if (IS_ERR_OR_NULL(ifp))
25334f672235SAlexander Aring 			return -1;
25344f672235SAlexander Aring 
25354f672235SAlexander Aring 		create = 1;
25364f672235SAlexander Aring 		spin_lock_bh(&ifp->lock);
25374f672235SAlexander Aring 		ifp->flags |= IFA_F_MANAGETEMPADDR;
25384f672235SAlexander Aring 		ifp->cstamp = jiffies;
25394f672235SAlexander Aring 		ifp->tokenized = tokenized;
25404f672235SAlexander Aring 		spin_unlock_bh(&ifp->lock);
25414f672235SAlexander Aring 		addrconf_dad_start(ifp);
25424f672235SAlexander Aring 	}
25434f672235SAlexander Aring 
25444f672235SAlexander Aring 	if (ifp) {
25454f672235SAlexander Aring 		u32 flags;
25464f672235SAlexander Aring 		unsigned long now;
25474f672235SAlexander Aring 		u32 stored_lft;
25484f672235SAlexander Aring 
25494f672235SAlexander Aring 		/* update lifetime (RFC2462 5.5.3 e) */
25504f672235SAlexander Aring 		spin_lock_bh(&ifp->lock);
25514f672235SAlexander Aring 		now = jiffies;
25524f672235SAlexander Aring 		if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
25534f672235SAlexander Aring 			stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
25544f672235SAlexander Aring 		else
25554f672235SAlexander Aring 			stored_lft = 0;
2556f85f94b8SLorenzo Bianconi 		if (!create && stored_lft) {
25574f672235SAlexander Aring 			const u32 minimum_lft = min_t(u32,
25584f672235SAlexander Aring 				stored_lft, MIN_VALID_LIFETIME);
25594f672235SAlexander Aring 			valid_lft = max(valid_lft, minimum_lft);
25604f672235SAlexander Aring 
25614f672235SAlexander Aring 			/* RFC4862 Section 5.5.3e:
25624f672235SAlexander Aring 			 * "Note that the preferred lifetime of the
25634f672235SAlexander Aring 			 *  corresponding address is always reset to
25644f672235SAlexander Aring 			 *  the Preferred Lifetime in the received
25654f672235SAlexander Aring 			 *  Prefix Information option, regardless of
25664f672235SAlexander Aring 			 *  whether the valid lifetime is also reset or
25674f672235SAlexander Aring 			 *  ignored."
25684f672235SAlexander Aring 			 *
25694f672235SAlexander Aring 			 * So we should always update prefered_lft here.
25704f672235SAlexander Aring 			 */
25714f672235SAlexander Aring 			update_lft = 1;
25724f672235SAlexander Aring 		}
25734f672235SAlexander Aring 
25744f672235SAlexander Aring 		if (update_lft) {
25754f672235SAlexander Aring 			ifp->valid_lft = valid_lft;
25764f672235SAlexander Aring 			ifp->prefered_lft = prefered_lft;
25774f672235SAlexander Aring 			ifp->tstamp = now;
25784f672235SAlexander Aring 			flags = ifp->flags;
25794f672235SAlexander Aring 			ifp->flags &= ~IFA_F_DEPRECATED;
25804f672235SAlexander Aring 			spin_unlock_bh(&ifp->lock);
25814f672235SAlexander Aring 
25824f672235SAlexander Aring 			if (!(flags&IFA_F_TENTATIVE))
25834f672235SAlexander Aring 				ipv6_ifa_notify(0, ifp);
25844f672235SAlexander Aring 		} else
25854f672235SAlexander Aring 			spin_unlock_bh(&ifp->lock);
25864f672235SAlexander Aring 
25874f672235SAlexander Aring 		manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
25884f672235SAlexander Aring 				 create, now);
25894f672235SAlexander Aring 
25904f672235SAlexander Aring 		in6_ifa_put(ifp);
25914f672235SAlexander Aring 		addrconf_verify();
25924f672235SAlexander Aring 	}
25934f672235SAlexander Aring 
25944f672235SAlexander Aring 	return 0;
25954f672235SAlexander Aring }
2596cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
25974f672235SAlexander Aring 
2598e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
25991da177e4SLinus Torvalds {
26001da177e4SLinus Torvalds 	struct prefix_info *pinfo;
26011da177e4SLinus Torvalds 	__u32 valid_lft;
26021da177e4SLinus Torvalds 	__u32 prefered_lft;
26034f672235SAlexander Aring 	int addr_type, err;
260464236f3fSHannes Frederic Sowa 	u32 addr_flags = 0;
26051da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
260656d417b1SBrian Haley 	struct net *net = dev_net(dev);
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
2611e32ac250SJoe Perches 		netdev_dbg(dev, "addrconf: prefix option too short\n");
26121da177e4SLinus Torvalds 		return;
26131da177e4SLinus Torvalds 	}
26141da177e4SLinus Torvalds 
26151da177e4SLinus Torvalds 	/*
26161da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
26171da177e4SLinus Torvalds 	 */
26181da177e4SLinus Torvalds 
26191da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
26221da177e4SLinus Torvalds 		return;
26231da177e4SLinus Torvalds 
26241da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
26251da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
26261da177e4SLinus Torvalds 
26271da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2628e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
26291da177e4SLinus Torvalds 		return;
26301da177e4SLinus Torvalds 	}
26311da177e4SLinus Torvalds 
26321da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
26331da177e4SLinus Torvalds 
263463159f29SIan Morris 	if (!in6_dev) {
2635e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2636e87cc472SJoe Perches 				    dev->name);
26371da177e4SLinus Torvalds 		return;
26381da177e4SLinus Torvalds 	}
26391da177e4SLinus Torvalds 
26401da177e4SLinus Torvalds 	/*
26411da177e4SLinus Torvalds 	 *	Two things going on here:
26421da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
26431da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
26441da177e4SLinus Torvalds 	 */
26451da177e4SLinus Torvalds 
26464bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
26478d1c802bSDavid Ahern 		struct fib6_info *rt;
26484bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
26494bed72e4SYOSHIFUJI Hideaki 
26501da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
26516f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
26526f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
26536f704992SYOSHIFUJI Hideaki 		 * not good.
26541da177e4SLinus Torvalds 		 */
26554bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
26564bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
26574bed72e4SYOSHIFUJI Hideaki 		else
26584bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
26593dd4bc68SYOSHIFUJI Hideaki 
26604bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
26614bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
26621da177e4SLinus Torvalds 
266314ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
266414ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
266514ef37b6SAndreas Hofmeister 					       dev,
266614ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
266714ef37b6SAndreas Hofmeister 					       RTF_GATEWAY | RTF_DEFAULT);
26681da177e4SLinus Torvalds 
266914ef37b6SAndreas Hofmeister 		if (rt) {
26706f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
26711da177e4SLinus Torvalds 			if (valid_lft == 0) {
2672afb1d4b5SDavid Ahern 				ip6_del_rt(net, rt);
26731da177e4SLinus Torvalds 				rt = NULL;
26744bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
26756f704992SYOSHIFUJI Hideaki 				/* not infinity */
267614895687SDavid Ahern 				fib6_set_expires(rt, jiffies + rt_expires);
26776f704992SYOSHIFUJI Hideaki 			} else {
267814895687SDavid Ahern 				fib6_clean_expires(rt);
26791da177e4SLinus Torvalds 			}
26801da177e4SLinus Torvalds 		} else if (valid_lft) {
26816f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
26824bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
26834bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
26846f704992SYOSHIFUJI Hideaki 				/* not infinity */
26856f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
26866f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
26876f704992SYOSHIFUJI Hideaki 			}
26881da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2689*8308f3ffSDavid Ahern 					      0, dev, expires, flags,
2690*8308f3ffSDavid Ahern 					      GFP_ATOMIC);
26911da177e4SLinus Torvalds 		}
269293531c67SDavid Ahern 		fib6_info_release(rt);
26931da177e4SLinus Torvalds 	}
26941da177e4SLinus Torvalds 
26951da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
26981da177e4SLinus Torvalds 		struct in6_addr addr;
2699f997c55cSAlexander Aring 		bool tokenized = false, dev_addr_generated = false;
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
27021da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2703f53adae4SDaniel Borkmann 
2704f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2705f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2706f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2707f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2708f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
2709617fe29dSDaniel Borkmann 				tokenized = true;
2710cc9da6ccSBjørn Mork 			} else if (is_addr_mode_generate_stable(in6_dev) &&
2711622c81d5SHannes Frederic Sowa 				   !ipv6_generate_stable_address(&addr, 0,
2712622c81d5SHannes Frederic Sowa 								 in6_dev)) {
271364236f3fSHannes Frederic Sowa 				addr_flags |= IFA_F_STABLE_PRIVACY;
2714622c81d5SHannes Frederic Sowa 				goto ok;
2715f53adae4SDaniel Borkmann 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
27161da177e4SLinus Torvalds 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
27174f672235SAlexander Aring 				goto put;
2718f997c55cSAlexander Aring 			} else {
2719f997c55cSAlexander Aring 				dev_addr_generated = true;
27201da177e4SLinus Torvalds 			}
27211da177e4SLinus Torvalds 			goto ok;
27221da177e4SLinus Torvalds 		}
2723e87cc472SJoe Perches 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
27241da177e4SLinus Torvalds 				    pinfo->prefix_len);
27254f672235SAlexander Aring 		goto put;
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds ok:
27284f672235SAlexander Aring 		err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
27294f672235SAlexander Aring 						   &addr, addr_type,
27304f672235SAlexander Aring 						   addr_flags, sllao,
27314f672235SAlexander Aring 						   tokenized, valid_lft,
27328a226b2cSJiri Benc 						   prefered_lft);
27334f672235SAlexander Aring 		if (err)
27344f672235SAlexander Aring 			goto put;
2735f997c55cSAlexander Aring 
2736f997c55cSAlexander Aring 		/* Ignore error case here because previous prefix add addr was
2737f997c55cSAlexander Aring 		 * successful which will be notified.
2738f997c55cSAlexander Aring 		 */
2739f997c55cSAlexander Aring 		ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2740f997c55cSAlexander Aring 					      addr_type, addr_flags, sllao,
2741f997c55cSAlexander Aring 					      tokenized, valid_lft,
2742f997c55cSAlexander Aring 					      prefered_lft,
2743f997c55cSAlexander Aring 					      dev_addr_generated);
27441da177e4SLinus Torvalds 	}
27451da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
27464f672235SAlexander Aring put:
27471da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
27481da177e4SLinus Torvalds }
27491da177e4SLinus Torvalds 
27501da177e4SLinus Torvalds /*
27511da177e4SLinus Torvalds  *	Set destination address.
27521da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
27531da177e4SLinus Torvalds  *	device.
27541da177e4SLinus Torvalds  */
2755af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
27561da177e4SLinus Torvalds {
27571da177e4SLinus Torvalds 	struct in6_ifreq ireq;
27581da177e4SLinus Torvalds 	struct net_device *dev;
27591da177e4SLinus Torvalds 	int err = -EINVAL;
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds 	rtnl_lock();
27621da177e4SLinus Torvalds 
27631da177e4SLinus Torvalds 	err = -EFAULT;
27641da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
27651da177e4SLinus Torvalds 		goto err_exit;
27661da177e4SLinus Torvalds 
2767af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
27681da177e4SLinus Torvalds 
27691da177e4SLinus Torvalds 	err = -ENODEV;
277063159f29SIan Morris 	if (!dev)
27711da177e4SLinus Torvalds 		goto err_exit;
27721da177e4SLinus Torvalds 
277307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
27741da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
27755bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
27761da177e4SLinus Torvalds 		struct ifreq ifr;
27771da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
27781da177e4SLinus Torvalds 
27791da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
27801da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
27811da177e4SLinus Torvalds 			goto err_exit;
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
27841da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
27851da177e4SLinus Torvalds 		p.iph.saddr = 0;
27861da177e4SLinus Torvalds 		p.iph.version = 4;
27871da177e4SLinus Torvalds 		p.iph.ihl = 5;
27881da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
27891da177e4SLinus Torvalds 		p.iph.ttl = 64;
2790d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
27911da177e4SLinus Torvalds 
27925bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
27935bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
27945bc3eb7eSStephen Hemminger 
27955bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
27965bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
27971da177e4SLinus Torvalds 			set_fs(oldfs);
27985bc3eb7eSStephen Hemminger 		} else
27995bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds 		if (err == 0) {
28021da177e4SLinus Torvalds 			err = -ENOBUFS;
2803af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2804af284937SDaniel Lezcano 			if (!dev)
28051da177e4SLinus Torvalds 				goto err_exit;
28061da177e4SLinus Torvalds 			err = dev_open(dev);
28071da177e4SLinus Torvalds 		}
28081da177e4SLinus Torvalds 	}
28090be669bbSJoerg Roedel #endif
28101da177e4SLinus Torvalds 
28111da177e4SLinus Torvalds err_exit:
28121da177e4SLinus Torvalds 	rtnl_unlock();
28131da177e4SLinus Torvalds 	return err;
28141da177e4SLinus Torvalds }
28151da177e4SLinus Torvalds 
281693a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join,
281793a714d6SMadhu Challa 			  const struct in6_addr *addr, int ifindex)
281893a714d6SMadhu Challa {
281993a714d6SMadhu Challa 	int ret;
282093a714d6SMadhu Challa 
282193a714d6SMadhu Challa 	ASSERT_RTNL();
282293a714d6SMadhu Challa 
282393a714d6SMadhu Challa 	lock_sock(sk);
282493a714d6SMadhu Challa 	if (join)
282554ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_join(sk, ifindex, addr);
282693a714d6SMadhu Challa 	else
282754ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_drop(sk, ifindex, addr);
282893a714d6SMadhu Challa 	release_sock(sk);
282993a714d6SMadhu Challa 
283093a714d6SMadhu Challa 	return ret;
283193a714d6SMadhu Challa }
283293a714d6SMadhu Challa 
28331da177e4SLinus Torvalds /*
28341da177e4SLinus Torvalds  *	Manual configuration of address on an interface
28351da177e4SLinus Torvalds  */
2836479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex,
283719b1518cSDavid Ahern 			  struct ifa6_config *cfg,
2838de95e047SDavid Ahern 			  struct netlink_ext_ack *extack)
28391da177e4SLinus Torvalds {
28401da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
28411da177e4SLinus Torvalds 	struct inet6_dev *idev;
28421da177e4SLinus Torvalds 	struct net_device *dev;
284393a714d6SMadhu Challa 	unsigned long timeout;
284493a714d6SMadhu Challa 	clock_t expires;
28456f704992SYOSHIFUJI Hideaki 	u32 flags;
28461da177e4SLinus Torvalds 
28471da177e4SLinus Torvalds 	ASSERT_RTNL();
28481da177e4SLinus Torvalds 
284919b1518cSDavid Ahern 	if (cfg->plen > 128)
285024ef0da7SThomas Graf 		return -EINVAL;
285124ef0da7SThomas Graf 
28520778769dSNoriaki TAKAMIYA 	/* check the lifetime */
285319b1518cSDavid Ahern 	if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
28540778769dSNoriaki TAKAMIYA 		return -EINVAL;
28550778769dSNoriaki TAKAMIYA 
285619b1518cSDavid Ahern 	if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64)
285753bd6749SJiri Pirko 		return -EINVAL;
285853bd6749SJiri Pirko 
2859af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2860af284937SDaniel Lezcano 	if (!dev)
28611da177e4SLinus Torvalds 		return -ENODEV;
28621da177e4SLinus Torvalds 
286364e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
286464e724f6SBrian Haley 	if (IS_ERR(idev))
286564e724f6SBrian Haley 		return PTR_ERR(idev);
28661da177e4SLinus Torvalds 
286719b1518cSDavid Ahern 	if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
286893a714d6SMadhu Challa 		int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
286919b1518cSDavid Ahern 					 true, cfg->pfx, ifindex);
287093a714d6SMadhu Challa 
287193a714d6SMadhu Challa 		if (ret < 0)
287293a714d6SMadhu Challa 			return ret;
287393a714d6SMadhu Challa 	}
287493a714d6SMadhu Challa 
287519b1518cSDavid Ahern 	cfg->scope = ipv6_addr_scope(cfg->pfx);
28761da177e4SLinus Torvalds 
287719b1518cSDavid Ahern 	timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
28784bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
28794bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
288019b1518cSDavid Ahern 		cfg->valid_lft = timeout;
28816f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
28824bed72e4SYOSHIFUJI Hideaki 	} else {
28834bed72e4SYOSHIFUJI Hideaki 		expires = 0;
28844bed72e4SYOSHIFUJI Hideaki 		flags = 0;
288519b1518cSDavid Ahern 		cfg->ifa_flags |= IFA_F_PERMANENT;
28866f704992SYOSHIFUJI Hideaki 	}
28870778769dSNoriaki TAKAMIYA 
288819b1518cSDavid Ahern 	timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
28894bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
28904bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
289119b1518cSDavid Ahern 			cfg->ifa_flags |= IFA_F_DEPRECATED;
289219b1518cSDavid Ahern 		cfg->preferred_lft = timeout;
28934bed72e4SYOSHIFUJI Hideaki 	}
28940778769dSNoriaki TAKAMIYA 
289519b1518cSDavid Ahern 	ifp = ipv6_add_addr(idev, cfg, true, extack);
28961da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
289719b1518cSDavid Ahern 		if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
2898*8308f3ffSDavid Ahern 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
2899*8308f3ffSDavid Ahern 					      ifp->rt_priority, dev, expires,
2900*8308f3ffSDavid Ahern 					      flags, GFP_KERNEL);
2901761aac73SThomas Haller 		}
2902761aac73SThomas Haller 
2903a2d481b3SLorenzo Bianconi 		/* Send a netlink notification if DAD is enabled and
2904a2d481b3SLorenzo Bianconi 		 * optimistic flag is not set
2905a2d481b3SLorenzo Bianconi 		 */
2906a2d481b3SLorenzo Bianconi 		if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
2907a2d481b3SLorenzo Bianconi 			ipv6_ifa_notify(0, ifp);
290895c385b4SNeil Horman 		/*
290995c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
291095c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
291195c385b4SNeil Horman 		 * manually configured addresses
291295c385b4SNeil Horman 		 */
2913cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
291419b1518cSDavid Ahern 		if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
291519b1518cSDavid Ahern 			manage_tempaddrs(idev, ifp, cfg->valid_lft,
291619b1518cSDavid Ahern 					 cfg->preferred_lft, true, jiffies);
29171da177e4SLinus Torvalds 		in6_ifa_put(ifp);
2918c15b1ccaSHannes Frederic Sowa 		addrconf_verify_rtnl();
29191da177e4SLinus Torvalds 		return 0;
292019b1518cSDavid Ahern 	} else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
292119b1518cSDavid Ahern 		ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
292219b1518cSDavid Ahern 			       cfg->pfx, ifindex);
29231da177e4SLinus Torvalds 	}
29241da177e4SLinus Torvalds 
29251da177e4SLinus Torvalds 	return PTR_ERR(ifp);
29261da177e4SLinus Torvalds }
29271da177e4SLinus Torvalds 
29286046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
29296046d5b4SHeiner Kallweit 			  const struct in6_addr *pfx, unsigned int plen)
29301da177e4SLinus Torvalds {
29311da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
29321da177e4SLinus Torvalds 	struct inet6_dev *idev;
29331da177e4SLinus Torvalds 	struct net_device *dev;
29341da177e4SLinus Torvalds 
293524ef0da7SThomas Graf 	if (plen > 128)
293624ef0da7SThomas Graf 		return -EINVAL;
293724ef0da7SThomas Graf 
2938af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2939af284937SDaniel Lezcano 	if (!dev)
29401da177e4SLinus Torvalds 		return -ENODEV;
29411da177e4SLinus Torvalds 
2942e5d08d71SIan Morris 	idev = __in6_dev_get(dev);
294363159f29SIan Morris 	if (!idev)
29441da177e4SLinus Torvalds 		return -ENXIO;
29451da177e4SLinus Torvalds 
29461da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2947502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
29481da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
29491da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
29501da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
29511da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
29521da177e4SLinus Torvalds 
29536046d5b4SHeiner Kallweit 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
29546046d5b4SHeiner Kallweit 			    (ifa_flags & IFA_F_MANAGETEMPADDR))
29556046d5b4SHeiner Kallweit 				manage_tempaddrs(idev, ifp, 0, 0, false,
29566046d5b4SHeiner Kallweit 						 jiffies);
29571da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
29586046d5b4SHeiner Kallweit 			addrconf_verify_rtnl();
295993a714d6SMadhu Challa 			if (ipv6_addr_is_multicast(pfx)) {
296093a714d6SMadhu Challa 				ipv6_mc_config(net->ipv6.mc_autojoin_sk,
296193a714d6SMadhu Challa 					       false, pfx, dev->ifindex);
296293a714d6SMadhu Challa 			}
29631da177e4SLinus Torvalds 			return 0;
29641da177e4SLinus Torvalds 		}
29651da177e4SLinus Torvalds 	}
29661da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
29671da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
29681da177e4SLinus Torvalds }
29691da177e4SLinus Torvalds 
29701da177e4SLinus Torvalds 
2971af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
29721da177e4SLinus Torvalds {
297319b1518cSDavid Ahern 	struct ifa6_config cfg = {
297419b1518cSDavid Ahern 		.ifa_flags = IFA_F_PERMANENT,
297519b1518cSDavid Ahern 		.preferred_lft = INFINITY_LIFE_TIME,
297619b1518cSDavid Ahern 		.valid_lft = INFINITY_LIFE_TIME,
297719b1518cSDavid Ahern 	};
29781da177e4SLinus Torvalds 	struct in6_ifreq ireq;
29791da177e4SLinus Torvalds 	int err;
29801da177e4SLinus Torvalds 
2981af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
29821da177e4SLinus Torvalds 		return -EPERM;
29831da177e4SLinus Torvalds 
29841da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
29851da177e4SLinus Torvalds 		return -EFAULT;
29861da177e4SLinus Torvalds 
298719b1518cSDavid Ahern 	cfg.pfx = &ireq.ifr6_addr;
298819b1518cSDavid Ahern 	cfg.plen = ireq.ifr6_prefixlen;
298919b1518cSDavid Ahern 
29901da177e4SLinus Torvalds 	rtnl_lock();
299119b1518cSDavid Ahern 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
29921da177e4SLinus Torvalds 	rtnl_unlock();
29931da177e4SLinus Torvalds 	return err;
29941da177e4SLinus Torvalds }
29951da177e4SLinus Torvalds 
2996af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
29971da177e4SLinus Torvalds {
29981da177e4SLinus Torvalds 	struct in6_ifreq ireq;
29991da177e4SLinus Torvalds 	int err;
30001da177e4SLinus Torvalds 
3001af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
30021da177e4SLinus Torvalds 		return -EPERM;
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
30051da177e4SLinus Torvalds 		return -EFAULT;
30061da177e4SLinus Torvalds 
30071da177e4SLinus Torvalds 	rtnl_lock();
30086046d5b4SHeiner Kallweit 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
3009af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
30101da177e4SLinus Torvalds 	rtnl_unlock();
30111da177e4SLinus Torvalds 	return err;
30121da177e4SLinus Torvalds }
30131da177e4SLinus Torvalds 
3014b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3015b5f348e5SIlpo Järvinen 		     int plen, int scope)
3016b5f348e5SIlpo Järvinen {
3017b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
3018e6464b8cSDavid Ahern 	struct ifa6_config cfg = {
3019e6464b8cSDavid Ahern 		.pfx = addr,
3020e6464b8cSDavid Ahern 		.plen = plen,
3021e6464b8cSDavid Ahern 		.ifa_flags = IFA_F_PERMANENT,
3022e6464b8cSDavid Ahern 		.valid_lft = INFINITY_LIFE_TIME,
3023e6464b8cSDavid Ahern 		.preferred_lft = INFINITY_LIFE_TIME,
3024e6464b8cSDavid Ahern 		.scope = scope
3025e6464b8cSDavid Ahern 	};
3026b5f348e5SIlpo Järvinen 
3027e6464b8cSDavid Ahern 	ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3028b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
3029b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
3030b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
3031b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
3032764d3be6SPaolo Abeni 		rt_genid_bump_ipv6(dev_net(idev->dev));
3033b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
3034b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
3035b5f348e5SIlpo Järvinen 	}
3036b5f348e5SIlpo Järvinen }
3037b5f348e5SIlpo Järvinen 
303807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
30391da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
30401da177e4SLinus Torvalds {
30411da177e4SLinus Torvalds 	struct in6_addr addr;
30421da177e4SLinus Torvalds 	struct net_device *dev;
3043c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
3044929c9cf3SNicolas Dichtel 	int scope, plen;
3045f0e2acfaSNicolas Dichtel 	u32 pflags = 0;
30461da177e4SLinus Torvalds 
30471da177e4SLinus Torvalds 	ASSERT_RTNL();
30481da177e4SLinus Torvalds 
30491da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
30501da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
30511da177e4SLinus Torvalds 
30521da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
30531da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
30541da177e4SLinus Torvalds 		scope = IFA_LINK;
3055929c9cf3SNicolas Dichtel 		plen = 64;
30561da177e4SLinus Torvalds 	} else {
30571da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
3058929c9cf3SNicolas Dichtel 		plen = 96;
3059f0e2acfaSNicolas Dichtel 		pflags |= RTF_NONEXTHOP;
30601da177e4SLinus Torvalds 	}
30611da177e4SLinus Torvalds 
30621da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
3063929c9cf3SNicolas Dichtel 		add_addr(idev, &addr, plen, scope);
3064*8308f3ffSDavid Ahern 		addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
3065acb54e3cSDavid Ahern 				      GFP_ATOMIC);
30661da177e4SLinus Torvalds 		return;
30671da177e4SLinus Torvalds 	}
30681da177e4SLinus Torvalds 
30696fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
3070e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
30711da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
30721da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds 			int flag = scope;
30751da177e4SLinus Torvalds 
30761da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
30771da177e4SLinus Torvalds 
30781da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
30811da177e4SLinus Torvalds 					continue;
30821da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
30831da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
30841da177e4SLinus Torvalds 						continue;
30851da177e4SLinus Torvalds 					flag |= IFA_HOST;
30861da177e4SLinus Torvalds 				}
30871da177e4SLinus Torvalds 
3088b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
3089*8308f3ffSDavid Ahern 				addrconf_prefix_route(&addr, plen, 0, idev->dev,
3090*8308f3ffSDavid Ahern 						      0, pflags, GFP_ATOMIC);
30911da177e4SLinus Torvalds 			}
30921da177e4SLinus Torvalds 		}
30931da177e4SLinus Torvalds 	}
30941da177e4SLinus Torvalds }
30950be669bbSJoerg Roedel #endif
30961da177e4SLinus Torvalds 
30971da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
30981da177e4SLinus Torvalds {
30991da177e4SLinus Torvalds 	struct inet6_dev  *idev;
31001da177e4SLinus Torvalds 
31011da177e4SLinus Torvalds 	/* ::1 */
31021da177e4SLinus Torvalds 
31031da177e4SLinus Torvalds 	ASSERT_RTNL();
31041da177e4SLinus Torvalds 
3105e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
310663159f29SIan Morris 	if (!idev) {
310791df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
31081da177e4SLinus Torvalds 		return;
31091da177e4SLinus Torvalds 	}
31101da177e4SLinus Torvalds 
3111b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
31121da177e4SLinus Torvalds }
31131da177e4SLinus Torvalds 
31142ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev,
311564236f3fSHannes Frederic Sowa 			    const struct in6_addr *addr, u32 flags)
31161da177e4SLinus Torvalds {
3117e6464b8cSDavid Ahern 	struct ifa6_config cfg = {
3118e6464b8cSDavid Ahern 		.pfx = addr,
3119e6464b8cSDavid Ahern 		.plen = 64,
3120e6464b8cSDavid Ahern 		.ifa_flags = flags | IFA_F_PERMANENT,
3121e6464b8cSDavid Ahern 		.valid_lft = INFINITY_LIFE_TIME,
3122e6464b8cSDavid Ahern 		.preferred_lft = INFINITY_LIFE_TIME,
3123e6464b8cSDavid Ahern 		.scope = IFA_LINK
3124e6464b8cSDavid Ahern 	};
31251da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
31261da177e4SLinus Torvalds 
312795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
312835e015e1SMatteo Croce 	if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
312935e015e1SMatteo Croce 	     idev->cnf.optimistic_dad) &&
3130702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3131e6464b8cSDavid Ahern 		cfg.ifa_flags |= IFA_F_OPTIMISTIC;
313295c385b4SNeil Horman #endif
313395c385b4SNeil Horman 
3134e6464b8cSDavid Ahern 	ifp = ipv6_add_addr(idev, &cfg, true, NULL);
31351da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
3136*8308f3ffSDavid Ahern 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
3137acb54e3cSDavid Ahern 				      0, 0, GFP_ATOMIC);
3138cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
31391da177e4SLinus Torvalds 		in6_ifa_put(ifp);
31401da177e4SLinus Torvalds 	}
31411da177e4SLinus Torvalds }
31422ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
31431da177e4SLinus Torvalds 
3144622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address)
3145622c81d5SHannes Frederic Sowa {
3146622c81d5SHannes Frederic Sowa 	if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3147622c81d5SHannes Frederic Sowa 		return true;
3148622c81d5SHannes Frederic Sowa 
3149622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0x02005eff) &&
3150622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3151622c81d5SHannes Frederic Sowa 		return true;
3152622c81d5SHannes Frederic Sowa 
3153622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3154622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3155622c81d5SHannes Frederic Sowa 		return true;
3156622c81d5SHannes Frederic Sowa 
3157622c81d5SHannes Frederic Sowa 	return false;
3158622c81d5SHannes Frederic Sowa }
3159622c81d5SHannes Frederic Sowa 
3160622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address,
3161622c81d5SHannes Frederic Sowa 					u8 dad_count,
3162622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev)
3163622c81d5SHannes Frederic Sowa {
3164622c81d5SHannes Frederic Sowa 	static DEFINE_SPINLOCK(lock);
3165622c81d5SHannes Frederic Sowa 	static __u32 digest[SHA_DIGEST_WORDS];
3166622c81d5SHannes Frederic Sowa 	static __u32 workspace[SHA_WORKSPACE_WORDS];
3167622c81d5SHannes Frederic Sowa 
3168622c81d5SHannes Frederic Sowa 	static union {
3169622c81d5SHannes Frederic Sowa 		char __data[SHA_MESSAGE_BYTES];
3170622c81d5SHannes Frederic Sowa 		struct {
3171622c81d5SHannes Frederic Sowa 			struct in6_addr secret;
3172ff40217eSHannes Frederic Sowa 			__be32 prefix[2];
3173622c81d5SHannes Frederic Sowa 			unsigned char hwaddr[MAX_ADDR_LEN];
3174622c81d5SHannes Frederic Sowa 			u8 dad_count;
3175622c81d5SHannes Frederic Sowa 		} __packed;
3176622c81d5SHannes Frederic Sowa 	} data;
3177622c81d5SHannes Frederic Sowa 
3178622c81d5SHannes Frederic Sowa 	struct in6_addr secret;
3179622c81d5SHannes Frederic Sowa 	struct in6_addr temp;
3180622c81d5SHannes Frederic Sowa 	struct net *net = dev_net(idev->dev);
3181622c81d5SHannes Frederic Sowa 
3182622c81d5SHannes Frederic Sowa 	BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3183622c81d5SHannes Frederic Sowa 
3184622c81d5SHannes Frederic Sowa 	if (idev->cnf.stable_secret.initialized)
3185622c81d5SHannes Frederic Sowa 		secret = idev->cnf.stable_secret.secret;
3186622c81d5SHannes Frederic Sowa 	else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3187622c81d5SHannes Frederic Sowa 		secret = net->ipv6.devconf_dflt->stable_secret.secret;
3188622c81d5SHannes Frederic Sowa 	else
3189622c81d5SHannes Frederic Sowa 		return -1;
3190622c81d5SHannes Frederic Sowa 
3191622c81d5SHannes Frederic Sowa retry:
3192622c81d5SHannes Frederic Sowa 	spin_lock_bh(&lock);
3193622c81d5SHannes Frederic Sowa 
3194622c81d5SHannes Frederic Sowa 	sha_init(digest);
3195622c81d5SHannes Frederic Sowa 	memset(&data, 0, sizeof(data));
3196622c81d5SHannes Frederic Sowa 	memset(workspace, 0, sizeof(workspace));
3197622c81d5SHannes Frederic Sowa 	memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3198ff40217eSHannes Frederic Sowa 	data.prefix[0] = address->s6_addr32[0];
3199ff40217eSHannes Frederic Sowa 	data.prefix[1] = address->s6_addr32[1];
3200622c81d5SHannes Frederic Sowa 	data.secret = secret;
3201622c81d5SHannes Frederic Sowa 	data.dad_count = dad_count;
3202622c81d5SHannes Frederic Sowa 
3203622c81d5SHannes Frederic Sowa 	sha_transform(digest, data.__data, workspace);
3204622c81d5SHannes Frederic Sowa 
3205622c81d5SHannes Frederic Sowa 	temp = *address;
3206ff40217eSHannes Frederic Sowa 	temp.s6_addr32[2] = (__force __be32)digest[0];
3207ff40217eSHannes Frederic Sowa 	temp.s6_addr32[3] = (__force __be32)digest[1];
3208622c81d5SHannes Frederic Sowa 
3209622c81d5SHannes Frederic Sowa 	spin_unlock_bh(&lock);
3210622c81d5SHannes Frederic Sowa 
3211622c81d5SHannes Frederic Sowa 	if (ipv6_reserved_interfaceid(temp)) {
3212622c81d5SHannes Frederic Sowa 		dad_count++;
32131855b7c3SHannes Frederic Sowa 		if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3214622c81d5SHannes Frederic Sowa 			return -1;
3215622c81d5SHannes Frederic Sowa 		goto retry;
3216622c81d5SHannes Frederic Sowa 	}
3217622c81d5SHannes Frederic Sowa 
3218622c81d5SHannes Frederic Sowa 	*address = temp;
3219622c81d5SHannes Frederic Sowa 	return 0;
3220622c81d5SHannes Frederic Sowa }
3221622c81d5SHannes Frederic Sowa 
3222cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3223cc9da6ccSBjørn Mork {
3224cc9da6ccSBjørn Mork 	struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3225cc9da6ccSBjørn Mork 
3226cc9da6ccSBjørn Mork 	if (s->initialized)
3227cc9da6ccSBjørn Mork 		return;
3228cc9da6ccSBjørn Mork 	s = &idev->cnf.stable_secret;
3229cc9da6ccSBjørn Mork 	get_random_bytes(&s->secret, sizeof(s->secret));
3230cc9da6ccSBjørn Mork 	s->initialized = true;
3231cc9da6ccSBjørn Mork }
3232cc9da6ccSBjørn Mork 
3233bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3234bc91b0f0SJiri Pirko {
3235bc91b0f0SJiri Pirko 	struct in6_addr addr;
3236bc91b0f0SJiri Pirko 
3237ca254490SDavid Ahern 	/* no link local addresses on L3 master devices */
3238ca254490SDavid Ahern 	if (netif_is_l3_master(idev->dev))
3239ca254490SDavid Ahern 		return;
3240ca254490SDavid Ahern 
3241bc91b0f0SJiri Pirko 	ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3242622c81d5SHannes Frederic Sowa 
3243d35a00b8SFelix Jia 	switch (idev->cnf.addr_gen_mode) {
3244cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_RANDOM:
3245cc9da6ccSBjørn Mork 		ipv6_gen_mode_random_init(idev);
3246cc9da6ccSBjørn Mork 		/* fallthrough */
3247cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3248622c81d5SHannes Frederic Sowa 		if (!ipv6_generate_stable_address(&addr, 0, idev))
324964236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr,
325064236f3fSHannes Frederic Sowa 					       IFA_F_STABLE_PRIVACY);
3251622c81d5SHannes Frederic Sowa 		else if (prefix_route)
3252*8308f3ffSDavid Ahern 			addrconf_prefix_route(&addr, 64, 0, idev->dev,
3253acb54e3cSDavid Ahern 					      0, 0, GFP_KERNEL);
3254cc9da6ccSBjørn Mork 		break;
3255cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_EUI64:
3256bc91b0f0SJiri Pirko 		/* addrconf_add_linklocal also adds a prefix_route and we
3257bc91b0f0SJiri Pirko 		 * only need to care about prefix routes if ipv6_generate_eui64
3258bc91b0f0SJiri Pirko 		 * couldn't generate one.
3259bc91b0f0SJiri Pirko 		 */
3260bc91b0f0SJiri Pirko 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
326164236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr, 0);
3262bc91b0f0SJiri Pirko 		else if (prefix_route)
3263*8308f3ffSDavid Ahern 			addrconf_prefix_route(&addr, 64, 0, idev->dev,
326427b10608SDavid Ahern 					      0, 0, GFP_KERNEL);
3265cc9da6ccSBjørn Mork 		break;
3266cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_NONE:
3267cc9da6ccSBjørn Mork 	default:
3268cc9da6ccSBjørn Mork 		/* will not add any link local address */
3269cc9da6ccSBjørn Mork 		break;
3270bc91b0f0SJiri Pirko 	}
3271bc91b0f0SJiri Pirko }
3272bc91b0f0SJiri Pirko 
32731da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
32741da177e4SLinus Torvalds {
32751da177e4SLinus Torvalds 	struct inet6_dev *idev;
32761da177e4SLinus Torvalds 
32771da177e4SLinus Torvalds 	ASSERT_RTNL();
32781da177e4SLinus Torvalds 
327974235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
328074235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
328174235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
328206a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
3283e837735eSNicolas Dichtel 	    (dev->type != ARPHRD_IEEE1394) &&
3284e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_TUNNEL6) &&
3285cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_6LOWPAN) &&
328645ce0fd1SFelix Jia 	    (dev->type != ARPHRD_IP6GRE) &&
328745ce0fd1SFelix Jia 	    (dev->type != ARPHRD_IPGRE) &&
328845ce0fd1SFelix Jia 	    (dev->type != ARPHRD_TUNNEL) &&
3289cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_NONE)) {
329074235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
329174235a25SHerbert Xu 		return;
329274235a25SHerbert Xu 	}
329374235a25SHerbert Xu 
32941da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
329564e724f6SBrian Haley 	if (IS_ERR(idev))
32961da177e4SLinus Torvalds 		return;
32971da177e4SLinus Torvalds 
3298cc9da6ccSBjørn Mork 	/* this device type has no EUI support */
3299cc9da6ccSBjørn Mork 	if (dev->type == ARPHRD_NONE &&
3300d35a00b8SFelix Jia 	    idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3301d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3302cc9da6ccSBjørn Mork 
3303bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, false);
33041da177e4SLinus Torvalds }
33051da177e4SLinus Torvalds 
330607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
33071da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
33081da177e4SLinus Torvalds {
33091da177e4SLinus Torvalds 	struct inet6_dev *idev;
33101da177e4SLinus Torvalds 
33111da177e4SLinus Torvalds 	ASSERT_RTNL();
33121da177e4SLinus Torvalds 
33131da177e4SLinus Torvalds 	/*
33141da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
33151da177e4SLinus Torvalds 	 * addresses... we should configure all of
33161da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
33171da177e4SLinus Torvalds 	 */
33181da177e4SLinus Torvalds 
3319e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
332063159f29SIan Morris 	if (!idev) {
332191df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
33221da177e4SLinus Torvalds 		return;
33231da177e4SLinus Torvalds 	}
33241da177e4SLinus Torvalds 
3325c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
3326bc91b0f0SJiri Pirko 		addrconf_addr_gen(idev, false);
3327c7dc89c0SFred L. Templin 		return;
3328c7dc89c0SFred L. Templin 	}
3329c7dc89c0SFred L. Templin 
33301da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
33311da177e4SLinus Torvalds 
333262b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
33331da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
33341da177e4SLinus Torvalds }
33350be669bbSJoerg Roedel #endif
33361da177e4SLinus Torvalds 
333707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3338aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
3339aee80b54Sstephen hemminger {
3340aee80b54Sstephen hemminger 	struct inet6_dev *idev;
3341aee80b54Sstephen hemminger 
3342aee80b54Sstephen hemminger 	ASSERT_RTNL();
3343aee80b54Sstephen hemminger 
3344e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
334563159f29SIan Morris 	if (!idev) {
334691df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
3347aee80b54Sstephen hemminger 		return;
3348aee80b54Sstephen hemminger 	}
3349aee80b54Sstephen hemminger 
3350bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, true);
3351d9e4ce65SHannes Frederic Sowa 	if (dev->flags & IFF_POINTOPOINT)
3352d9e4ce65SHannes Frederic Sowa 		addrconf_add_mroute(dev);
3353aee80b54Sstephen hemminger }
3354aee80b54Sstephen hemminger #endif
3355aee80b54Sstephen hemminger 
3356afb1d4b5SDavid Ahern static int fixup_permanent_addr(struct net *net,
3357afb1d4b5SDavid Ahern 				struct inet6_dev *idev,
3358f1705ec1SDavid Ahern 				struct inet6_ifaddr *ifp)
3359f1705ec1SDavid Ahern {
336093c2fb25SDavid Ahern 	/* !fib6_node means the host route was removed from the
33618048ced9SDavid Ahern 	 * FIB, for example, if 'lo' device is taken down. In that
33628048ced9SDavid Ahern 	 * case regenerate the host route.
33638048ced9SDavid Ahern 	 */
336493c2fb25SDavid Ahern 	if (!ifp->rt || !ifp->rt->fib6_node) {
3365360a9887SDavid Ahern 		struct fib6_info *f6i, *prev;
3366f1705ec1SDavid Ahern 
3367360a9887SDavid Ahern 		f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3368acb54e3cSDavid Ahern 					 GFP_ATOMIC);
3369360a9887SDavid Ahern 		if (IS_ERR(f6i))
3370360a9887SDavid Ahern 			return PTR_ERR(f6i);
3371f1705ec1SDavid Ahern 
33728048ced9SDavid Ahern 		/* ifp->rt can be accessed outside of rtnl */
33738048ced9SDavid Ahern 		spin_lock(&ifp->lock);
33748048ced9SDavid Ahern 		prev = ifp->rt;
3375360a9887SDavid Ahern 		ifp->rt = f6i;
33768048ced9SDavid Ahern 		spin_unlock(&ifp->lock);
33778048ced9SDavid Ahern 
337893531c67SDavid Ahern 		fib6_info_release(prev);
3379f1705ec1SDavid Ahern 	}
3380f1705ec1SDavid Ahern 
3381f1705ec1SDavid Ahern 	if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3382f1705ec1SDavid Ahern 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3383*8308f3ffSDavid Ahern 				      ifp->rt_priority, idev->dev, 0, 0,
3384*8308f3ffSDavid Ahern 				      GFP_ATOMIC);
3385f1705ec1SDavid Ahern 	}
3386f1705ec1SDavid Ahern 
33876d717134SDavid Ahern 	if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3388f1705ec1SDavid Ahern 		addrconf_dad_start(ifp);
3389f1705ec1SDavid Ahern 
3390f1705ec1SDavid Ahern 	return 0;
3391f1705ec1SDavid Ahern }
3392f1705ec1SDavid Ahern 
3393afb1d4b5SDavid Ahern static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
3394f1705ec1SDavid Ahern {
3395f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifp, *tmp;
3396f1705ec1SDavid Ahern 	struct inet6_dev *idev;
3397f1705ec1SDavid Ahern 
3398f1705ec1SDavid Ahern 	idev = __in6_dev_get(dev);
3399f1705ec1SDavid Ahern 	if (!idev)
3400f1705ec1SDavid Ahern 		return;
3401f1705ec1SDavid Ahern 
3402f1705ec1SDavid Ahern 	write_lock_bh(&idev->lock);
3403f1705ec1SDavid Ahern 
3404f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3405f1705ec1SDavid Ahern 		if ((ifp->flags & IFA_F_PERMANENT) &&
3406afb1d4b5SDavid Ahern 		    fixup_permanent_addr(net, idev, ifp) < 0) {
3407f1705ec1SDavid Ahern 			write_unlock_bh(&idev->lock);
3408e669b869SEric Dumazet 			in6_ifa_hold(ifp);
3409f1705ec1SDavid Ahern 			ipv6_del_addr(ifp);
3410f1705ec1SDavid Ahern 			write_lock_bh(&idev->lock);
3411f1705ec1SDavid Ahern 
3412f1705ec1SDavid Ahern 			net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3413f1705ec1SDavid Ahern 					     idev->dev->name, &ifp->addr);
3414f1705ec1SDavid Ahern 		}
3415f1705ec1SDavid Ahern 	}
3416f1705ec1SDavid Ahern 
3417f1705ec1SDavid Ahern 	write_unlock_bh(&idev->lock);
3418f1705ec1SDavid Ahern }
3419f1705ec1SDavid Ahern 
34201da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
3421351638e7SJiri Pirko 			   void *ptr)
34221da177e4SLinus Torvalds {
3423351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
34244f7f34eaSDavid Ahern 	struct netdev_notifier_changeupper_info *info;
3425748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
342660abc0beSWANG Cong 	struct net *net = dev_net(dev);
3427c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
3428b217d616SHerbert Xu 	int err;
34291da177e4SLinus Torvalds 
34301da177e4SLinus Torvalds 	switch (event) {
343145ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
343274235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
343345ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
3434a317a2f1SWANG Cong 			if (IS_ERR(idev))
3435a317a2f1SWANG Cong 				return notifier_from_errno(PTR_ERR(idev));
343645ba9dd2SYOSHIFUJI Hideaki 		}
343745ba9dd2SYOSHIFUJI Hideaki 		break;
3438bcdd553fSStephen Hemminger 
3439b7b0b1d2SAlexander Duyck 	case NETDEV_CHANGEMTU:
3440b7b0b1d2SAlexander Duyck 		/* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3441b7b0b1d2SAlexander Duyck 		if (dev->mtu < IPV6_MIN_MTU) {
344260abc0beSWANG Cong 			addrconf_ifdown(dev, dev != net->loopback_dev);
3443b7b0b1d2SAlexander Duyck 			break;
3444b7b0b1d2SAlexander Duyck 		}
3445b7b0b1d2SAlexander Duyck 
3446b7b0b1d2SAlexander Duyck 		if (idev) {
3447b7b0b1d2SAlexander Duyck 			rt6_mtu_change(dev, dev->mtu);
3448b7b0b1d2SAlexander Duyck 			idev->cnf.mtu6 = dev->mtu;
3449b7b0b1d2SAlexander Duyck 			break;
3450b7b0b1d2SAlexander Duyck 		}
3451b7b0b1d2SAlexander Duyck 
3452b7b0b1d2SAlexander Duyck 		/* allocate new idev */
3453b7b0b1d2SAlexander Duyck 		idev = ipv6_add_dev(dev);
3454b7b0b1d2SAlexander Duyck 		if (IS_ERR(idev))
3455b7b0b1d2SAlexander Duyck 			break;
3456b7b0b1d2SAlexander Duyck 
3457b7b0b1d2SAlexander Duyck 		/* device is still not ready */
3458b7b0b1d2SAlexander Duyck 		if (!(idev->if_flags & IF_READY))
3459b7b0b1d2SAlexander Duyck 			break;
3460b7b0b1d2SAlexander Duyck 
3461b7b0b1d2SAlexander Duyck 		run_pending = 1;
3462b7b0b1d2SAlexander Duyck 
3463b7b0b1d2SAlexander Duyck 		/* fall through */
3464b7b0b1d2SAlexander Duyck 
34651da177e4SLinus Torvalds 	case NETDEV_UP:
34663c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
3467c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
3468c2edacf8SJay Vosburgh 			break;
3469c2edacf8SJay Vosburgh 
34703ce62a84SWANG Cong 		if (idev && idev->cnf.disable_ipv6)
34713ce62a84SWANG Cong 			break;
34723ce62a84SWANG Cong 
34733c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
347438bd10c4SDavid Ahern 			/* restore routes for permanent addresses */
3475afb1d4b5SDavid Ahern 			addrconf_permanent_addr(net, dev);
347638bd10c4SDavid Ahern 
34771f372c7bSMike Manning 			if (!addrconf_link_ready(dev)) {
34783c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
3479f3213831SJoe Perches 				pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
34803c21edbdSYOSHIFUJI Hideaki 					dev->name);
34813c21edbdSYOSHIFUJI Hideaki 				break;
34823c21edbdSYOSHIFUJI Hideaki 			}
348399081049SKristian Slavov 
3484d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
3485d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
3486d31c7b8fSEvgeniy Polyakov 
3487a317a2f1SWANG Cong 			if (!IS_ERR_OR_NULL(idev)) {
348899081049SKristian Slavov 				idev->if_flags |= IF_READY;
3489e3ec6cfcSBenjamin Thery 				run_pending = 1;
3490e3ec6cfcSBenjamin Thery 			}
3491b7b0b1d2SAlexander Duyck 		} else if (event == NETDEV_CHANGE) {
34921f372c7bSMike Manning 			if (!addrconf_link_ready(dev)) {
34933c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
349427c6fa73SIdo Schimmel 				rt6_sync_down_dev(dev, event);
34953c21edbdSYOSHIFUJI Hideaki 				break;
34963c21edbdSYOSHIFUJI Hideaki 			}
34973c21edbdSYOSHIFUJI Hideaki 
34983c21edbdSYOSHIFUJI Hideaki 			if (idev) {
3499a088d1d7SLinus Lüssing 				if (idev->if_flags & IF_READY) {
3500a088d1d7SLinus Lüssing 					/* device is already configured -
3501a088d1d7SLinus Lüssing 					 * but resend MLD reports, we might
3502a088d1d7SLinus Lüssing 					 * have roamed and need to update
3503a088d1d7SLinus Lüssing 					 * multicast snooping switches
3504a088d1d7SLinus Lüssing 					 */
3505a088d1d7SLinus Lüssing 					ipv6_mc_up(idev);
350627c6fa73SIdo Schimmel 					rt6_sync_up(dev, RTNH_F_LINKDOWN);
35073c21edbdSYOSHIFUJI Hideaki 					break;
3508a088d1d7SLinus Lüssing 				}
35093c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
35103c21edbdSYOSHIFUJI Hideaki 			}
35113c21edbdSYOSHIFUJI Hideaki 
3512f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
35133c21edbdSYOSHIFUJI Hideaki 				dev->name);
35143c21edbdSYOSHIFUJI Hideaki 
3515c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
35163c21edbdSYOSHIFUJI Hideaki 		}
35173c21edbdSYOSHIFUJI Hideaki 
35181da177e4SLinus Torvalds 		switch (dev->type) {
351907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
35201da177e4SLinus Torvalds 		case ARPHRD_SIT:
35211da177e4SLinus Torvalds 			addrconf_sit_config(dev);
35221da177e4SLinus Torvalds 			break;
35230be669bbSJoerg Roedel #endif
352407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3525aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
3526aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
3527aee80b54Sstephen hemminger 			break;
3528aee80b54Sstephen hemminger #endif
35291da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
35301da177e4SLinus Torvalds 			init_loopback(dev);
35311da177e4SLinus Torvalds 			break;
35321da177e4SLinus Torvalds 
35331da177e4SLinus Torvalds 		default:
35341da177e4SLinus Torvalds 			addrconf_dev_config(dev);
35351da177e4SLinus Torvalds 			break;
35363ff50b79SStephen Hemminger 		}
3537bcdd553fSStephen Hemminger 
3538a317a2f1SWANG Cong 		if (!IS_ERR_OR_NULL(idev)) {
3539c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
3540c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
3541c5e33bddSYOSHIFUJI Hideaki 
35422127d95aSIdo Schimmel 			/* Device has an address by now */
35432127d95aSIdo Schimmel 			rt6_sync_up(dev, RTNH_F_DEAD);
35442127d95aSIdo Schimmel 
3545bcdd553fSStephen Hemminger 			/*
3546bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
3547bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
3548bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
35491da177e4SLinus Torvalds 			 */
3550bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
3551bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
35521da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
35531da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
35541da177e4SLinus Torvalds 			}
35551da177e4SLinus Torvalds 			idev->tstamp = jiffies;
35561da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
3557bcdd553fSStephen Hemminger 
3558bcdd553fSStephen Hemminger 			/*
3559bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
3560bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
35611da177e4SLinus Torvalds 			 */
35621da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
356360abc0beSWANG Cong 				addrconf_ifdown(dev, dev != net->loopback_dev);
35641da177e4SLinus Torvalds 		}
35651da177e4SLinus Torvalds 		break;
35661da177e4SLinus Torvalds 
35671da177e4SLinus Torvalds 	case NETDEV_DOWN:
35681da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
35691da177e4SLinus Torvalds 		/*
35701da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
35711da177e4SLinus Torvalds 		 */
35721da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
35731da177e4SLinus Torvalds 		break;
35743c21edbdSYOSHIFUJI Hideaki 
35751da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
35761da177e4SLinus Torvalds 		if (idev) {
35775632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
3578408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
3579a317a2f1SWANG Cong 			err = addrconf_sysctl_register(idev);
3580b217d616SHerbert Xu 			if (err)
3581b217d616SHerbert Xu 				return notifier_from_errno(err);
3582a317a2f1SWANG Cong 			err = snmp6_register_dev(idev);
3583a317a2f1SWANG Cong 			if (err) {
3584a317a2f1SWANG Cong 				addrconf_sysctl_unregister(idev);
3585a317a2f1SWANG Cong 				return notifier_from_errno(err);
3586a317a2f1SWANG Cong 			}
35875632c515SStephen Hemminger 		}
35881da177e4SLinus Torvalds 		break;
3589bcdd553fSStephen Hemminger 
359093d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
359193d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
35923ef0952cSAndrew Lunn 		if (idev)
359393d9b7d7SJiri Pirko 			addrconf_type_change(dev, event);
359475c78500SMoni Shoua 		break;
35954f7f34eaSDavid Ahern 
35964f7f34eaSDavid Ahern 	case NETDEV_CHANGEUPPER:
35974f7f34eaSDavid Ahern 		info = ptr;
35984f7f34eaSDavid Ahern 
35994f7f34eaSDavid Ahern 		/* flush all routes if dev is linked to or unlinked from
36004f7f34eaSDavid Ahern 		 * an L3 master device (e.g., VRF)
36014f7f34eaSDavid Ahern 		 */
36024f7f34eaSDavid Ahern 		if (info->upper_dev && netif_is_l3_master(info->upper_dev))
36034f7f34eaSDavid Ahern 			addrconf_ifdown(dev, 0);
36043ff50b79SStephen Hemminger 	}
36051da177e4SLinus Torvalds 
36061da177e4SLinus Torvalds 	return NOTIFY_OK;
36071da177e4SLinus Torvalds }
36081da177e4SLinus Torvalds 
36091da177e4SLinus Torvalds /*
36101da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
36111da177e4SLinus Torvalds  */
36121da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
36131da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
3614242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY,
36151da177e4SLinus Torvalds };
36161da177e4SLinus Torvalds 
361793d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
361875c78500SMoni Shoua {
361975c78500SMoni Shoua 	struct inet6_dev *idev;
362075c78500SMoni Shoua 	ASSERT_RTNL();
362175c78500SMoni Shoua 
362275c78500SMoni Shoua 	idev = __in6_dev_get(dev);
362375c78500SMoni Shoua 
362493d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
362575c78500SMoni Shoua 		ipv6_mc_remap(idev);
362693d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
362775c78500SMoni Shoua 		ipv6_mc_unmap(idev);
362875c78500SMoni Shoua }
362975c78500SMoni Shoua 
363070af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr)
363170af921dSDavid Ahern {
363270af921dSDavid Ahern 	return ipv6_addr_type(addr) &
363370af921dSDavid Ahern 		(IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
363470af921dSDavid Ahern }
363570af921dSDavid Ahern 
36361da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
36371da177e4SLinus Torvalds {
36384c981e28SIdo Schimmel 	unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN;
3639c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
3640502a2ffdSstephen hemminger 	struct inet6_dev *idev;
3641f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifa, *tmp;
36420aef78aaSIvan Vecera 	bool keep_addr = false;
364373a8bd74SDavid S. Miller 	int state, i;
36441da177e4SLinus Torvalds 
36451da177e4SLinus Torvalds 	ASSERT_RTNL();
36461da177e4SLinus Torvalds 
36474c981e28SIdo Schimmel 	rt6_disable_ip(dev, event);
36481da177e4SLinus Torvalds 
36491da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
365063159f29SIan Morris 	if (!idev)
36511da177e4SLinus Torvalds 		return -ENODEV;
36521da177e4SLinus Torvalds 
3653bcdd553fSStephen Hemminger 	/*
3654bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3655bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
36561da177e4SLinus Torvalds 	 */
3657439e2385SDenis V. Lunev 	if (how) {
36581da177e4SLinus Torvalds 		idev->dead = 1;
36598814c4b5SYOSHIFUJI Hideaki 
36608814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3661a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
36621da177e4SLinus Torvalds 
36631da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
36641da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
36651da177e4SLinus Torvalds 
36661da177e4SLinus Torvalds 	}
36671da177e4SLinus Torvalds 
3668f1705ec1SDavid Ahern 	/* combine the user config with event to determine if permanent
3669f1705ec1SDavid Ahern 	 * addresses are to be removed from address hash table
3670f1705ec1SDavid Ahern 	 */
36710aef78aaSIvan Vecera 	if (!how && !idev->cnf.disable_ipv6) {
36720aef78aaSIvan Vecera 		/* aggregate the system setting and interface setting */
36730aef78aaSIvan Vecera 		int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
36740aef78aaSIvan Vecera 
36750aef78aaSIvan Vecera 		if (!_keep_addr)
36760aef78aaSIvan Vecera 			_keep_addr = idev->cnf.keep_addr_on_down;
36770aef78aaSIvan Vecera 
36780aef78aaSIvan Vecera 		keep_addr = (_keep_addr > 0);
36790aef78aaSIvan Vecera 	}
3680f1705ec1SDavid Ahern 
368173a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
368273a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
368373a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
368473a8bd74SDavid S. Miller 
368573a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
368673a8bd74SDavid S. Miller restart:
3687b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
368873a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
3689c15b1ccaSHannes Frederic Sowa 				addrconf_del_dad_work(ifa);
3690f1705ec1SDavid Ahern 				/* combined flag + permanent flag decide if
3691f1705ec1SDavid Ahern 				 * address is retained on a down event
3692f1705ec1SDavid Ahern 				 */
3693f1705ec1SDavid Ahern 				if (!keep_addr ||
369470af921dSDavid Ahern 				    !(ifa->flags & IFA_F_PERMANENT) ||
369570af921dSDavid Ahern 				    addr_is_local(&ifa->addr)) {
3696f1705ec1SDavid Ahern 					hlist_del_init_rcu(&ifa->addr_lst);
369773a8bd74SDavid S. Miller 					goto restart;
369873a8bd74SDavid S. Miller 				}
369973a8bd74SDavid S. Miller 			}
3700f1705ec1SDavid Ahern 		}
370173a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
370273a8bd74SDavid S. Miller 	}
370373a8bd74SDavid S. Miller 
37041da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
37051da177e4SLinus Torvalds 
3706b7b1bfceSHannes Frederic Sowa 	addrconf_del_rs_timer(idev);
3707b7b1bfceSHannes Frederic Sowa 
3708bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3709439e2385SDenis V. Lunev 	if (!how)
37103c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
37111da177e4SLinus Torvalds 
3712bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3713372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3714372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3715372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3716372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
37171da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
37181da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
37191da177e4SLinus Torvalds 
37201da177e4SLinus Torvalds 		if (ifa->ifpub) {
37211da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
37221da177e4SLinus Torvalds 			ifa->ifpub = NULL;
37231da177e4SLinus Torvalds 		}
37241da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
37251da177e4SLinus Torvalds 		in6_ifa_put(ifa);
37261da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
37271da177e4SLinus Torvalds 	}
37288f37ada5Sstephen hemminger 
3729f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
37308d1c802bSDavid Ahern 		struct fib6_info *rt = NULL;
3731a2d6cbb0SRabin Vincent 		bool keep;
373238bd10c4SDavid Ahern 
3733c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifa);
373484e8b803Sstephen hemminger 
3735a2d6cbb0SRabin Vincent 		keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3736a2d6cbb0SRabin Vincent 			!addr_is_local(&ifa->addr);
3737a2d6cbb0SRabin Vincent 
37384c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
37398e8e676dSHannes Frederic Sowa 		spin_lock_bh(&ifa->lock);
3740f1705ec1SDavid Ahern 
3741a2d6cbb0SRabin Vincent 		if (keep) {
3742f1705ec1SDavid Ahern 			/* set state to skip the notifier below */
3743f1705ec1SDavid Ahern 			state = INET6_IFADDR_STATE_DEAD;
37446d717134SDavid Ahern 			ifa->state = INET6_IFADDR_STATE_PREDAD;
3745f1705ec1SDavid Ahern 			if (!(ifa->flags & IFA_F_NODAD))
3746f1705ec1SDavid Ahern 				ifa->flags |= IFA_F_TENTATIVE;
374738bd10c4SDavid Ahern 
374838bd10c4SDavid Ahern 			rt = ifa->rt;
374938bd10c4SDavid Ahern 			ifa->rt = NULL;
3750f1705ec1SDavid Ahern 		} else {
37514c5ff6a6SHerbert Xu 			state = ifa->state;
37524c5ff6a6SHerbert Xu 			ifa->state = INET6_IFADDR_STATE_DEAD;
3753f1705ec1SDavid Ahern 		}
3754f1705ec1SDavid Ahern 
37558e8e676dSHannes Frederic Sowa 		spin_unlock_bh(&ifa->lock);
37564c5ff6a6SHerbert Xu 
375738bd10c4SDavid Ahern 		if (rt)
3758afb1d4b5SDavid Ahern 			ip6_del_rt(net, rt);
375938bd10c4SDavid Ahern 
37609d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
37611da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
3762f88c91ddSCong Wang 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3763ea06f717SMike Manning 		} else {
3764ea06f717SMike Manning 			if (idev->cnf.forwarding)
3765ea06f717SMike Manning 				addrconf_leave_anycast(ifa);
3766ea06f717SMike Manning 			addrconf_leave_solict(ifa->idev, &ifa->addr);
37672de79570SLorenzo Colitti 		}
376873a8bd74SDavid S. Miller 
37691da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
37708ef802aaSEric Dumazet 		if (!keep) {
37718ef802aaSEric Dumazet 			list_del_rcu(&ifa->if_list);
37728ef802aaSEric Dumazet 			in6_ifa_put(ifa);
37738ef802aaSEric Dumazet 		}
37741da177e4SLinus Torvalds 	}
37758f37ada5Sstephen hemminger 
37761da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
37771da177e4SLinus Torvalds 
3778381f4dcaSSabrina Dubroca 	/* Step 5: Discard anycast and multicast list */
3779381f4dcaSSabrina Dubroca 	if (how) {
3780381f4dcaSSabrina Dubroca 		ipv6_ac_destroy_dev(idev);
37811da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
3782381f4dcaSSabrina Dubroca 	} else {
37831da177e4SLinus Torvalds 		ipv6_mc_down(idev);
3784381f4dcaSSabrina Dubroca 	}
37851da177e4SLinus Torvalds 
37861da177e4SLinus Torvalds 	idev->tstamp = jiffies;
37871da177e4SLinus Torvalds 
3788bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3789439e2385SDenis V. Lunev 	if (how) {
3790408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
37911da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
37921da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
37931da177e4SLinus Torvalds 		in6_dev_put(idev);
37941da177e4SLinus Torvalds 	}
37951da177e4SLinus Torvalds 	return 0;
37961da177e4SLinus Torvalds }
37971da177e4SLinus Torvalds 
3798e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t)
37991da177e4SLinus Torvalds {
3800e99e88a9SKees Cook 	struct inet6_dev *idev = from_timer(idev, t, rs_timer);
3801caf92bc4SCong Wang 	struct net_device *dev = idev->dev;
3802b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
38031da177e4SLinus Torvalds 
3804b7b1bfceSHannes Frederic Sowa 	write_lock(&idev->lock);
38055b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
38061da177e4SLinus Torvalds 		goto out;
38071da177e4SLinus Torvalds 
38089ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
38091da177e4SLinus Torvalds 		goto out;
38105b2a1953Sstephen hemminger 
38115b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
38125b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
38135b2a1953Sstephen hemminger 		goto out;
38141da177e4SLinus Torvalds 
3815bd11f074SMaciej Żenczykowski 	if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3816caf92bc4SCong Wang 		write_unlock(&idev->lock);
3817caf92bc4SCong Wang 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3818caf92bc4SCong Wang 			ndisc_send_rs(dev, &lladdr,
3819b7b1bfceSHannes Frederic Sowa 				      &in6addr_linklocal_allrouters);
3820b7b1bfceSHannes Frederic Sowa 		else
3821caf92bc4SCong Wang 			goto put;
3822b7b1bfceSHannes Frederic Sowa 
3823caf92bc4SCong Wang 		write_lock(&idev->lock);
3824bd11f074SMaciej Żenczykowski 		idev->rs_interval = rfc3315_s14_backoff_update(
3825bd11f074SMaciej Żenczykowski 			idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
38261da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
3827b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3828b7b1bfceSHannes Frederic Sowa 					     idev->cnf.rtr_solicits) ?
38295b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_delay :
3830bd11f074SMaciej Żenczykowski 				      idev->rs_interval);
38311da177e4SLinus Torvalds 	} else {
38321da177e4SLinus Torvalds 		/*
38331da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
38341da177e4SLinus Torvalds 		 * assumption any longer.
38351da177e4SLinus Torvalds 		 */
383691df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
38371da177e4SLinus Torvalds 	}
38381da177e4SLinus Torvalds 
38391da177e4SLinus Torvalds out:
3840b7b1bfceSHannes Frederic Sowa 	write_unlock(&idev->lock);
3841caf92bc4SCong Wang put:
3842b7b1bfceSHannes Frederic Sowa 	in6_dev_put(idev);
38431da177e4SLinus Torvalds }
38441da177e4SLinus Torvalds 
38451da177e4SLinus Torvalds /*
38461da177e4SLinus Torvalds  *	Duplicate Address Detection
38471da177e4SLinus Torvalds  */
38483c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
38493c21edbdSYOSHIFUJI Hideaki {
38503c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
38513c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
3852adc176c5SErik Nordmark 	u64 nonce;
38533c21edbdSYOSHIFUJI Hideaki 
385495c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
385595c385b4SNeil Horman 		rand_num = 0;
385695c385b4SNeil Horman 	else
385763862b5bSAruna-Hewapathirane 		rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
385895c385b4SNeil Horman 
3859adc176c5SErik Nordmark 	nonce = 0;
3860adc176c5SErik Nordmark 	if (idev->cnf.enhanced_dad ||
3861adc176c5SErik Nordmark 	    dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3862adc176c5SErik Nordmark 		do
3863adc176c5SErik Nordmark 			get_random_bytes(&nonce, 6);
3864adc176c5SErik Nordmark 		while (nonce == 0);
3865adc176c5SErik Nordmark 	}
3866adc176c5SErik Nordmark 	ifp->dad_nonce = nonce;
3867b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes = idev->cnf.dad_transmits;
3868c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, rand_num);
38693c21edbdSYOSHIFUJI Hideaki }
38703c21edbdSYOSHIFUJI Hideaki 
3871c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
38721da177e4SLinus Torvalds {
38731da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
38741da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3875764d3be6SPaolo Abeni 	bool bump_id, notify = false;
3876afb1d4b5SDavid Ahern 	struct net *net;
38771da177e4SLinus Torvalds 
38781da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
38791da177e4SLinus Torvalds 
388063862b5bSAruna-Hewapathirane 	prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
38811da177e4SLinus Torvalds 
38821da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3883622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3884e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
38851da177e4SLinus Torvalds 		goto out;
38861da177e4SLinus Torvalds 
3887afb1d4b5SDavid Ahern 	net = dev_net(dev);
38881da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3889afb1d4b5SDavid Ahern 	    (net->ipv6.devconf_all->accept_dad < 1 &&
3890a2d3f3e3SMatteo Croce 	     idev->cnf.accept_dad < 1) ||
389155ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
389255ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3893c76fe2d9SDavid Ahern 		bool send_na = false;
3894c76fe2d9SDavid Ahern 
3895c76fe2d9SDavid Ahern 		if (ifp->flags & IFA_F_TENTATIVE &&
3896c76fe2d9SDavid Ahern 		    !(ifp->flags & IFA_F_OPTIMISTIC))
3897c76fe2d9SDavid Ahern 			send_na = true;
3898764d3be6SPaolo Abeni 		bump_id = ifp->flags & IFA_F_TENTATIVE;
3899cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
390021809fafSstephen hemminger 		spin_unlock(&ifp->lock);
39011da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
39021da177e4SLinus Torvalds 
3903c76fe2d9SDavid Ahern 		addrconf_dad_completed(ifp, bump_id, send_na);
39041da177e4SLinus Torvalds 		return;
39051da177e4SLinus Torvalds 	}
39061da177e4SLinus Torvalds 
39076732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
390821809fafSstephen hemminger 		spin_unlock(&ifp->lock);
39096732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
39103c21edbdSYOSHIFUJI Hideaki 		/*
3911590a9887SMasatake YAMATO 		 * If the device is not ready:
39123c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
39133c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
39143c21edbdSYOSHIFUJI Hideaki 		 */
39153c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
3916cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
39176732badeSYOSHIFUJI Hideaki 		return;
39183c21edbdSYOSHIFUJI Hideaki 	}
391995c385b4SNeil Horman 
392095c385b4SNeil Horman 	/*
392195c385b4SNeil Horman 	 * Optimistic nodes can start receiving
392295c385b4SNeil Horman 	 * Frames right away
392395c385b4SNeil Horman 	 */
39247fd2561eSErik Kline 	if (ifp->flags & IFA_F_OPTIMISTIC) {
3925afb1d4b5SDavid Ahern 		ip6_ins_rt(net, ifp->rt);
3926afb1d4b5SDavid Ahern 		if (ipv6_use_optimistic_addr(net, idev)) {
39277fd2561eSErik Kline 			/* Because optimistic nodes can use this address,
39287fd2561eSErik Kline 			 * notify listeners. If DAD fails, RTM_DELADDR is sent.
39297fd2561eSErik Kline 			 */
393016186a82Ssubashab@codeaurora.org 			notify = true;
39317fd2561eSErik Kline 		}
39327fd2561eSErik Kline 	}
393395c385b4SNeil Horman 
39346732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
39351da177e4SLinus Torvalds out:
3936622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
39371da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
393816186a82Ssubashab@codeaurora.org 	if (notify)
393916186a82Ssubashab@codeaurora.org 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
39401da177e4SLinus Torvalds }
39411da177e4SLinus Torvalds 
3942c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp)
39431da177e4SLinus Torvalds {
3944c15b1ccaSHannes Frederic Sowa 	bool begin_dad = false;
3945c15b1ccaSHannes Frederic Sowa 
39468e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3947c15b1ccaSHannes Frederic Sowa 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3948c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_PREDAD;
3949c15b1ccaSHannes Frederic Sowa 		begin_dad = true;
3950c15b1ccaSHannes Frederic Sowa 	}
39518e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3952c15b1ccaSHannes Frederic Sowa 
3953c15b1ccaSHannes Frederic Sowa 	if (begin_dad)
3954c15b1ccaSHannes Frederic Sowa 		addrconf_mod_dad_work(ifp, 0);
3955c15b1ccaSHannes Frederic Sowa }
3956c15b1ccaSHannes Frederic Sowa 
3957c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w)
3958c15b1ccaSHannes Frederic Sowa {
3959c15b1ccaSHannes Frederic Sowa 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3960c15b1ccaSHannes Frederic Sowa 						struct inet6_ifaddr,
3961c15b1ccaSHannes Frederic Sowa 						dad_work);
39621da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
3963764d3be6SPaolo Abeni 	bool bump_id, disable_ipv6 = false;
39641da177e4SLinus Torvalds 	struct in6_addr mcaddr;
39651da177e4SLinus Torvalds 
3966c15b1ccaSHannes Frederic Sowa 	enum {
3967c15b1ccaSHannes Frederic Sowa 		DAD_PROCESS,
3968c15b1ccaSHannes Frederic Sowa 		DAD_BEGIN,
3969c15b1ccaSHannes Frederic Sowa 		DAD_ABORT,
3970c15b1ccaSHannes Frederic Sowa 	} action = DAD_PROCESS;
3971c15b1ccaSHannes Frederic Sowa 
3972c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
3973c15b1ccaSHannes Frederic Sowa 
39748e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3975c15b1ccaSHannes Frederic Sowa 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3976c15b1ccaSHannes Frederic Sowa 		action = DAD_BEGIN;
3977c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_DAD;
3978c15b1ccaSHannes Frederic Sowa 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3979c15b1ccaSHannes Frederic Sowa 		action = DAD_ABORT;
3980c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
398185b51b12SMike Manning 
398235e015e1SMatteo Croce 		if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
398335e015e1SMatteo Croce 		     idev->cnf.accept_dad > 1) &&
398435e015e1SMatteo Croce 		    !idev->cnf.disable_ipv6 &&
398585b51b12SMike Manning 		    !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
398685b51b12SMike Manning 			struct in6_addr addr;
398785b51b12SMike Manning 
398885b51b12SMike Manning 			addr.s6_addr32[0] = htonl(0xfe800000);
398985b51b12SMike Manning 			addr.s6_addr32[1] = 0;
399085b51b12SMike Manning 
399185b51b12SMike Manning 			if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
399285b51b12SMike Manning 			    ipv6_addr_equal(&ifp->addr, &addr)) {
399385b51b12SMike Manning 				/* DAD failed for link-local based on MAC */
399485b51b12SMike Manning 				idev->cnf.disable_ipv6 = 1;
399585b51b12SMike Manning 
399685b51b12SMike Manning 				pr_info("%s: IPv6 being disabled!\n",
399785b51b12SMike Manning 					ifp->idev->dev->name);
399885b51b12SMike Manning 				disable_ipv6 = true;
399985b51b12SMike Manning 			}
400085b51b12SMike Manning 		}
4001c15b1ccaSHannes Frederic Sowa 	}
40028e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
4003c15b1ccaSHannes Frederic Sowa 
4004c15b1ccaSHannes Frederic Sowa 	if (action == DAD_BEGIN) {
4005c15b1ccaSHannes Frederic Sowa 		addrconf_dad_begin(ifp);
4006c15b1ccaSHannes Frederic Sowa 		goto out;
4007c15b1ccaSHannes Frederic Sowa 	} else if (action == DAD_ABORT) {
4008751eb6b6SWei Yongjun 		in6_ifa_hold(ifp);
4009c15b1ccaSHannes Frederic Sowa 		addrconf_dad_stop(ifp, 1);
401085b51b12SMike Manning 		if (disable_ipv6)
401185b51b12SMike Manning 			addrconf_ifdown(idev->dev, 0);
4012c15b1ccaSHannes Frederic Sowa 		goto out;
4013c15b1ccaSHannes Frederic Sowa 	}
4014c15b1ccaSHannes Frederic Sowa 
4015b7b1bfceSHannes Frederic Sowa 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
4016f2344a13SHerbert Xu 		goto out;
4017f2344a13SHerbert Xu 
4018c15b1ccaSHannes Frederic Sowa 	write_lock_bh(&idev->lock);
4019122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
4020c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
40211da177e4SLinus Torvalds 		goto out;
40221da177e4SLinus Torvalds 	}
402321809fafSstephen hemminger 
402421809fafSstephen hemminger 	spin_lock(&ifp->lock);
4025622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4026622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
4027c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
4028622ccdf1SHerbert Xu 		goto out;
4029622ccdf1SHerbert Xu 	}
4030622ccdf1SHerbert Xu 
4031b7b1bfceSHannes Frederic Sowa 	if (ifp->dad_probes == 0) {
4032c76fe2d9SDavid Ahern 		bool send_na = false;
4033c76fe2d9SDavid Ahern 
40341da177e4SLinus Torvalds 		/*
40351da177e4SLinus Torvalds 		 * DAD was successful
40361da177e4SLinus Torvalds 		 */
40371da177e4SLinus Torvalds 
4038c76fe2d9SDavid Ahern 		if (ifp->flags & IFA_F_TENTATIVE &&
4039c76fe2d9SDavid Ahern 		    !(ifp->flags & IFA_F_OPTIMISTIC))
4040c76fe2d9SDavid Ahern 			send_na = true;
4041764d3be6SPaolo Abeni 		bump_id = ifp->flags & IFA_F_TENTATIVE;
4042cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
404321809fafSstephen hemminger 		spin_unlock(&ifp->lock);
4044c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
40451da177e4SLinus Torvalds 
4046c76fe2d9SDavid Ahern 		addrconf_dad_completed(ifp, bump_id, send_na);
40471da177e4SLinus Torvalds 
40481da177e4SLinus Torvalds 		goto out;
40491da177e4SLinus Torvalds 	}
40501da177e4SLinus Torvalds 
4051b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes--;
4052c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp,
40531f9248e5SJiri Pirko 			      NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
405421809fafSstephen hemminger 	spin_unlock(&ifp->lock);
4055c15b1ccaSHannes Frederic Sowa 	write_unlock_bh(&idev->lock);
40561da177e4SLinus Torvalds 
40571da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
40581da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
4059adc176c5SErik Nordmark 	ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4060adc176c5SErik Nordmark 		      ifp->dad_nonce);
40611da177e4SLinus Torvalds out:
40621da177e4SLinus Torvalds 	in6_ifa_put(ifp);
4063c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
40641da177e4SLinus Torvalds }
40651da177e4SLinus Torvalds 
406611ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */
406711ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
406811ffff75SHannes Frederic Sowa {
406911ffff75SHannes Frederic Sowa 	struct inet6_ifaddr *ifpiter;
407011ffff75SHannes Frederic Sowa 	struct inet6_dev *idev = ifp->idev;
407111ffff75SHannes Frederic Sowa 
4072602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4073602582caSHannes Frederic Sowa 		if (ifpiter->scope > IFA_LINK)
4074602582caSHannes Frederic Sowa 			break;
407511ffff75SHannes Frederic Sowa 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
407611ffff75SHannes Frederic Sowa 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
407711ffff75SHannes Frederic Sowa 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
407811ffff75SHannes Frederic Sowa 		    IFA_F_PERMANENT)
407911ffff75SHannes Frederic Sowa 			return false;
408011ffff75SHannes Frederic Sowa 	}
408111ffff75SHannes Frederic Sowa 	return true;
408211ffff75SHannes Frederic Sowa }
408311ffff75SHannes Frederic Sowa 
4084c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4085c76fe2d9SDavid Ahern 				   bool send_na)
40861da177e4SLinus Torvalds {
40871da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
4088b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
4089b173ee48SHannes Frederic Sowa 	bool send_rs, send_mld;
4090b7b1bfceSHannes Frederic Sowa 
4091c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
40921da177e4SLinus Torvalds 
40931da177e4SLinus Torvalds 	/*
40941da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
40951da177e4SLinus Torvalds 	 */
40961da177e4SLinus Torvalds 
40971da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
40981da177e4SLinus Torvalds 
4099026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
4100026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
41011da177e4SLinus Torvalds 	 */
41021da177e4SLinus Torvalds 
41031ec047ebSHannes Frederic Sowa 	read_lock_bh(&ifp->idev->lock);
410411ffff75SHannes Frederic Sowa 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4105b173ee48SHannes Frederic Sowa 	send_rs = send_mld &&
4106b173ee48SHannes Frederic Sowa 		  ipv6_accept_ra(ifp->idev) &&
4107bd11f074SMaciej Żenczykowski 		  ifp->idev->cnf.rtr_solicits != 0 &&
4108b173ee48SHannes Frederic Sowa 		  (dev->flags&IFF_LOOPBACK) == 0;
41091ec047ebSHannes Frederic Sowa 	read_unlock_bh(&ifp->idev->lock);
41101ec047ebSHannes Frederic Sowa 
4111b173ee48SHannes Frederic Sowa 	/* While dad is in progress mld report's source address is in6_addrany.
4112b173ee48SHannes Frederic Sowa 	 * Resend with proper ll now.
4113b173ee48SHannes Frederic Sowa 	 */
4114b173ee48SHannes Frederic Sowa 	if (send_mld)
4115b173ee48SHannes Frederic Sowa 		ipv6_mc_dad_complete(ifp->idev);
4116b173ee48SHannes Frederic Sowa 
4117c76fe2d9SDavid Ahern 	/* send unsolicited NA if enabled */
4118c76fe2d9SDavid Ahern 	if (send_na &&
4119c76fe2d9SDavid Ahern 	    (ifp->idev->cnf.ndisc_notify ||
4120c76fe2d9SDavid Ahern 	     dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
4121c76fe2d9SDavid Ahern 		ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4122c76fe2d9SDavid Ahern 			      /*router=*/ !!ifp->idev->cnf.forwarding,
4123c76fe2d9SDavid Ahern 			      /*solicited=*/ false, /*override=*/ true,
4124c76fe2d9SDavid Ahern 			      /*inc_opt=*/ true);
4125c76fe2d9SDavid Ahern 	}
4126c76fe2d9SDavid Ahern 
41271ec047ebSHannes Frederic Sowa 	if (send_rs) {
41281da177e4SLinus Torvalds 		/*
41291da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
41301da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
41311da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
41321da177e4SLinus Torvalds 		 */
41331ec047ebSHannes Frederic Sowa 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4134b7b1bfceSHannes Frederic Sowa 			return;
41351ec047ebSHannes Frederic Sowa 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
41361da177e4SLinus Torvalds 
4137b7b1bfceSHannes Frederic Sowa 		write_lock_bh(&ifp->idev->lock);
4138b7b1bfceSHannes Frederic Sowa 		spin_lock(&ifp->lock);
4139bd11f074SMaciej Żenczykowski 		ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4140bd11f074SMaciej Żenczykowski 			ifp->idev->cnf.rtr_solicit_interval);
4141b7b1bfceSHannes Frederic Sowa 		ifp->idev->rs_probes = 1;
41421da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
4143bd11f074SMaciej Żenczykowski 		addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4144b7b1bfceSHannes Frederic Sowa 		spin_unlock(&ifp->lock);
4145b7b1bfceSHannes Frederic Sowa 		write_unlock_bh(&ifp->idev->lock);
41461da177e4SLinus Torvalds 	}
4147764d3be6SPaolo Abeni 
4148764d3be6SPaolo Abeni 	if (bump_id)
4149764d3be6SPaolo Abeni 		rt_genid_bump_ipv6(dev_net(dev));
4150a11a7f71SMarcus Huewe 
4151a11a7f71SMarcus Huewe 	/* Make sure that a new temporary address will be created
4152a11a7f71SMarcus Huewe 	 * before this temporary address becomes deprecated.
4153a11a7f71SMarcus Huewe 	 */
4154a11a7f71SMarcus Huewe 	if (ifp->flags & IFA_F_TEMPORARY)
4155a11a7f71SMarcus Huewe 		addrconf_verify_rtnl();
41561da177e4SLinus Torvalds }
41571da177e4SLinus Torvalds 
4158e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev)
4159e21e8467SStephen Hemminger {
4160c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
4161c5e33bddSYOSHIFUJI Hideaki 
4162c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
4163502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
416421809fafSstephen hemminger 		spin_lock(&ifp->lock);
4165f2344a13SHerbert Xu 		if (ifp->flags & IFA_F_TENTATIVE &&
4166f2344a13SHerbert Xu 		    ifp->state == INET6_IFADDR_STATE_DAD)
4167c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
4168f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
4169c5e33bddSYOSHIFUJI Hideaki 	}
4170c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
4171c5e33bddSYOSHIFUJI Hideaki }
4172c5e33bddSYOSHIFUJI Hideaki 
41731da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
41741da177e4SLinus Torvalds struct if6_iter_state {
41753c40090aSDaniel Lezcano 	struct seq_net_private p;
41761da177e4SLinus Torvalds 	int bucket;
41771d578303SMihai Maruseac 	int offset;
41781da177e4SLinus Torvalds };
41791da177e4SLinus Torvalds 
41801d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
41811da177e4SLinus Torvalds {
41821da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
41831218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
4184a5c1d98fSEric Dumazet 	struct inet6_ifaddr *ifa = NULL;
41851d578303SMihai Maruseac 	int p = 0;
41861da177e4SLinus Torvalds 
41871d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
41881d578303SMihai Maruseac 	if (pos == 0) {
41891d578303SMihai Maruseac 		state->bucket = 0;
41901d578303SMihai Maruseac 		state->offset = 0;
41911d578303SMihai Maruseac 	}
41921d578303SMihai Maruseac 
41931d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4194a5c1d98fSEric Dumazet 		hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket],
41951d578303SMihai Maruseac 					 addr_lst) {
41969f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
41979f0d3c27SEric Dumazet 				continue;
41981d578303SMihai Maruseac 			/* sync with offset */
41991d578303SMihai Maruseac 			if (p < state->offset) {
42001d578303SMihai Maruseac 				p++;
42011d578303SMihai Maruseac 				continue;
42021d578303SMihai Maruseac 			}
42031d578303SMihai Maruseac 			state->offset++;
42041da177e4SLinus Torvalds 			return ifa;
42051da177e4SLinus Torvalds 		}
42061d578303SMihai Maruseac 
42071d578303SMihai Maruseac 		/* prepare for next bucket */
42081d578303SMihai Maruseac 		state->offset = 0;
42091d578303SMihai Maruseac 		p = 0;
42101d578303SMihai Maruseac 	}
4211c2e21293Sstephen hemminger 	return NULL;
4212c2e21293Sstephen hemminger }
42131da177e4SLinus Torvalds 
4214c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4215c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
42161da177e4SLinus Torvalds {
42171da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
42181218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
42191da177e4SLinus Torvalds 
4220a5c1d98fSEric Dumazet 	hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
42219f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
42229f0d3c27SEric Dumazet 			continue;
42231d578303SMihai Maruseac 		state->offset++;
42241da177e4SLinus Torvalds 		return ifa;
42251d578303SMihai Maruseac 	}
42261da177e4SLinus Torvalds 
4227c2e21293Sstephen hemminger 	while (++state->bucket < IN6_ADDR_HSIZE) {
42281d578303SMihai Maruseac 		state->offset = 0;
4229a5c1d98fSEric Dumazet 		hlist_for_each_entry_rcu(ifa,
4230c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
42319f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
42329f0d3c27SEric Dumazet 				continue;
42331d578303SMihai Maruseac 			state->offset++;
4234c2e21293Sstephen hemminger 			return ifa;
4235c2e21293Sstephen hemminger 		}
4236c2e21293Sstephen hemminger 	}
4237c2e21293Sstephen hemminger 
4238c2e21293Sstephen hemminger 	return NULL;
4239c2e21293Sstephen hemminger }
4240c2e21293Sstephen hemminger 
42411da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4242a5c1d98fSEric Dumazet 	__acquires(rcu)
42431da177e4SLinus Torvalds {
4244a5c1d98fSEric Dumazet 	rcu_read_lock();
42451d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
42461da177e4SLinus Torvalds }
42471da177e4SLinus Torvalds 
42481da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
42491da177e4SLinus Torvalds {
42501da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
42511da177e4SLinus Torvalds 
42521da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
42531da177e4SLinus Torvalds 	++*pos;
42541da177e4SLinus Torvalds 	return ifa;
42551da177e4SLinus Torvalds }
42561da177e4SLinus Torvalds 
42571da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
4258a5c1d98fSEric Dumazet 	__releases(rcu)
42591da177e4SLinus Torvalds {
4260a5c1d98fSEric Dumazet 	rcu_read_unlock();
42611da177e4SLinus Torvalds }
42621da177e4SLinus Torvalds 
42631da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
42641da177e4SLinus Torvalds {
42651da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4266971a351cSJiri Pirko 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4267b071195dSHarvey Harrison 		   &ifp->addr,
42681da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
42691da177e4SLinus Torvalds 		   ifp->prefix_len,
42701da177e4SLinus Torvalds 		   ifp->scope,
4271971a351cSJiri Pirko 		   (u8) ifp->flags,
42721da177e4SLinus Torvalds 		   ifp->idev->dev->name);
42731da177e4SLinus Torvalds 	return 0;
42741da177e4SLinus Torvalds }
42751da177e4SLinus Torvalds 
427656b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
42771da177e4SLinus Torvalds 	.start	= if6_seq_start,
42781da177e4SLinus Torvalds 	.next	= if6_seq_next,
42791da177e4SLinus Torvalds 	.show	= if6_seq_show,
42801da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
42811da177e4SLinus Torvalds };
42821da177e4SLinus Torvalds 
42831da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
42841da177e4SLinus Torvalds {
42853c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
4286cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
42871da177e4SLinus Torvalds }
42881da177e4SLinus Torvalds 
42899a32144eSArjan van de Ven static const struct file_operations if6_fops = {
42901da177e4SLinus Torvalds 	.open		= if6_seq_open,
42911da177e4SLinus Torvalds 	.read		= seq_read,
42921da177e4SLinus Torvalds 	.llseek		= seq_lseek,
42933c40090aSDaniel Lezcano 	.release	= seq_release_net,
42941da177e4SLinus Torvalds };
42951da177e4SLinus Torvalds 
42962c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
42971da177e4SLinus Torvalds {
4298d6444062SJoe Perches 	if (!proc_create("if_inet6", 0444, net->proc_net, &if6_fops))
42991da177e4SLinus Torvalds 		return -ENOMEM;
43001da177e4SLinus Torvalds 	return 0;
43011da177e4SLinus Torvalds }
43021da177e4SLinus Torvalds 
43032c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
43043c40090aSDaniel Lezcano {
4305ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
43063c40090aSDaniel Lezcano }
43073c40090aSDaniel Lezcano 
43083c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
43093c40090aSDaniel Lezcano 	.init = if6_proc_net_init,
43103c40090aSDaniel Lezcano 	.exit = if6_proc_net_exit,
43113c40090aSDaniel Lezcano };
43123c40090aSDaniel Lezcano 
43133c40090aSDaniel Lezcano int __init if6_proc_init(void)
43143c40090aSDaniel Lezcano {
43153c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
43163c40090aSDaniel Lezcano }
43173c40090aSDaniel Lezcano 
43181da177e4SLinus Torvalds void if6_proc_exit(void)
43191da177e4SLinus Torvalds {
43203c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
43211da177e4SLinus Torvalds }
43221da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
43231da177e4SLinus Torvalds 
432407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
43253b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
4326b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
43273b9f9a1cSNoriaki TAKAMIYA {
43283f27fb23SEric Dumazet 	unsigned int hash = inet6_addr_hash(net, addr);
4329c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
43303f27fb23SEric Dumazet 	int ret = 0;
4331c2e21293Sstephen hemminger 
43324e5f47abSEric Dumazet 	rcu_read_lock();
43334e5f47abSEric Dumazet 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
4334878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
4335389f6612SDaniel Lezcano 			continue;
4336caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
43373b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
43383b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
43393b9f9a1cSNoriaki TAKAMIYA 			break;
43403b9f9a1cSNoriaki TAKAMIYA 		}
43413b9f9a1cSNoriaki TAKAMIYA 	}
43424e5f47abSEric Dumazet 	rcu_read_unlock();
43433b9f9a1cSNoriaki TAKAMIYA 	return ret;
43443b9f9a1cSNoriaki TAKAMIYA }
43453b9f9a1cSNoriaki TAKAMIYA #endif
43463b9f9a1cSNoriaki TAKAMIYA 
43471da177e4SLinus Torvalds /*
43481da177e4SLinus Torvalds  *	Periodic address status verification
43491da177e4SLinus Torvalds  */
43501da177e4SLinus Torvalds 
4351c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void)
43521da177e4SLinus Torvalds {
4353b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
43541da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
43551da177e4SLinus Torvalds 	int i;
43561da177e4SLinus Torvalds 
4357c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4358c15b1ccaSHannes Frederic Sowa 
43595c578aedSstephen hemminger 	rcu_read_lock_bh();
43601da177e4SLinus Torvalds 	now = jiffies;
4361b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
43621da177e4SLinus Torvalds 
4363c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
43641da177e4SLinus Torvalds 
43651da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
43661da177e4SLinus Torvalds restart:
4367c15b1ccaSHannes Frederic Sowa 		hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
43681da177e4SLinus Torvalds 			unsigned long age;
43691da177e4SLinus Torvalds 
4370fad8da3eSYasushi Asano 			/* When setting preferred_lft to a value not zero or
4371fad8da3eSYasushi Asano 			 * infinity, while valid_lft is infinity
4372fad8da3eSYasushi Asano 			 * IFA_F_PERMANENT has a non-infinity life time.
4373fad8da3eSYasushi Asano 			 */
4374fad8da3eSYasushi Asano 			if ((ifp->flags & IFA_F_PERMANENT) &&
4375fad8da3eSYasushi Asano 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
43761da177e4SLinus Torvalds 				continue;
43771da177e4SLinus Torvalds 
43781da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
4379b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
4380b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
43811da177e4SLinus Torvalds 
43828f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
43838f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
43841da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
43851da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
43861da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
43871da177e4SLinus Torvalds 				goto restart;
43888f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
43898f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
43908f27ebb9SYOSHIFUJI Hideaki 				continue;
43911da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
4392a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
43931da177e4SLinus Torvalds 				int deprecate = 0;
43941da177e4SLinus Torvalds 
43951da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
43961da177e4SLinus Torvalds 					deprecate = 1;
43971da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
43981da177e4SLinus Torvalds 				}
43991da177e4SLinus Torvalds 
4400fad8da3eSYasushi Asano 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4401fad8da3eSYasushi Asano 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
44021da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
44031da177e4SLinus Torvalds 
44041da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
44051da177e4SLinus Torvalds 
44061da177e4SLinus Torvalds 				if (deprecate) {
44071da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
44081da177e4SLinus Torvalds 
44091da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
44101da177e4SLinus Torvalds 					in6_ifa_put(ifp);
44111da177e4SLinus Torvalds 					goto restart;
44121da177e4SLinus Torvalds 				}
44131da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
44141da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
441588949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
441688949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
44171f9248e5SJiri Pirko 					NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
441888949cf4Sstephen hemminger 
44191da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
44201da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
44211da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
44221da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
44231da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
44241da177e4SLinus Torvalds 						ifp->regen_count++;
44251da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
44261da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
44271da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
44285c578aedSstephen hemminger 
4429291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
4430291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
4431291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
4432e64e469bSEric Dumazet 						rcu_read_unlock_bh();
4433fffcefe9SEric Dumazet 						ipv6_create_tempaddr(ifpub, ifp, true);
44341da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
44351da177e4SLinus Torvalds 						in6_ifa_put(ifp);
4436e64e469bSEric Dumazet 						rcu_read_lock_bh();
44371da177e4SLinus Torvalds 						goto restart;
44381da177e4SLinus Torvalds 					}
44391da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
44401da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
44411da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
44421da177e4SLinus Torvalds 			} else {
44431da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
44441da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
44451da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
44461da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
44471da177e4SLinus Torvalds 			}
44481da177e4SLinus Torvalds 		}
44491da177e4SLinus Torvalds 	}
44501da177e4SLinus Torvalds 
4451b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
4452b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
4453b2db7564SYOSHIFUJI Hideaki 
4454b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
4455b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4456b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
4457b2db7564SYOSHIFUJI Hideaki 
4458b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4459b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4460b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4461b2db7564SYOSHIFUJI Hideaki 
4462e32ac250SJoe Perches 	pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4463ba3542e1Sdingtianhong 		 now, next, next_sec, next_sched);
4464c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
44655c578aedSstephen hemminger 	rcu_read_unlock_bh();
44661da177e4SLinus Torvalds }
44671da177e4SLinus Torvalds 
4468c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w)
4469c15b1ccaSHannes Frederic Sowa {
4470c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
4471c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4472c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
4473c15b1ccaSHannes Frederic Sowa }
4474c15b1ccaSHannes Frederic Sowa 
4475c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void)
4476c15b1ccaSHannes Frederic Sowa {
4477c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4478c15b1ccaSHannes Frederic Sowa }
4479c15b1ccaSHannes Frederic Sowa 
4480caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4481caeaba79SNicolas Dichtel 				     struct in6_addr **peer_pfx)
4482461d8837SThomas Graf {
4483461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
4484461d8837SThomas Graf 
4485caeaba79SNicolas Dichtel 	*peer_pfx = NULL;
4486caeaba79SNicolas Dichtel 
4487461d8837SThomas Graf 	if (addr)
4488461d8837SThomas Graf 		pfx = nla_data(addr);
4489461d8837SThomas Graf 
4490461d8837SThomas Graf 	if (local) {
4491461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4492caeaba79SNicolas Dichtel 			*peer_pfx = pfx;
4493461d8837SThomas Graf 		pfx = nla_data(local);
4494461d8837SThomas Graf 	}
4495461d8837SThomas Graf 
4496461d8837SThomas Graf 	return pfx;
4497461d8837SThomas Graf }
4498461d8837SThomas Graf 
4499ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4500461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
4501461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
4502461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
4503479840ffSJiri Pirko 	[IFA_FLAGS]		= { .len = sizeof(u32) },
4504*8308f3ffSDavid Ahern 	[IFA_RT_PRIORITY]	= { .len = sizeof(u32) },
4505461d8837SThomas Graf };
4506461d8837SThomas Graf 
45071da177e4SLinus Torvalds static int
4508c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4509c21ef3e3SDavid Ahern 		  struct netlink_ext_ack *extack)
45101da177e4SLinus Torvalds {
45113b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4512b933f716SThomas Graf 	struct ifaddrmsg *ifm;
4513b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4514caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
45156046d5b4SHeiner Kallweit 	u32 ifa_flags;
4516b933f716SThomas Graf 	int err;
45171da177e4SLinus Torvalds 
4518fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4519c21ef3e3SDavid Ahern 			  extack);
4520b933f716SThomas Graf 	if (err < 0)
4521b933f716SThomas Graf 		return err;
4522b933f716SThomas Graf 
4523b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
4524caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
452563159f29SIan Morris 	if (!pfx)
45261da177e4SLinus Torvalds 		return -EINVAL;
45271da177e4SLinus Torvalds 
45286046d5b4SHeiner Kallweit 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
45296046d5b4SHeiner Kallweit 
45306046d5b4SHeiner Kallweit 	/* We ignore other flags so far. */
45316046d5b4SHeiner Kallweit 	ifa_flags &= IFA_F_MANAGETEMPADDR;
45326046d5b4SHeiner Kallweit 
45336046d5b4SHeiner Kallweit 	return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
45346046d5b4SHeiner Kallweit 			      ifm->ifa_prefixlen);
45351da177e4SLinus Torvalds }
45361da177e4SLinus Torvalds 
4537*8308f3ffSDavid Ahern static int modify_prefix_route(struct inet6_ifaddr *ifp,
4538*8308f3ffSDavid Ahern 			       unsigned long expires, u32 flags)
4539*8308f3ffSDavid Ahern {
4540*8308f3ffSDavid Ahern 	struct fib6_info *f6i;
4541*8308f3ffSDavid Ahern 
4542*8308f3ffSDavid Ahern 	f6i = addrconf_get_prefix_route(&ifp->addr,
4543*8308f3ffSDavid Ahern 					ifp->prefix_len,
4544*8308f3ffSDavid Ahern 					ifp->idev->dev,
4545*8308f3ffSDavid Ahern 					0, RTF_GATEWAY | RTF_DEFAULT);
4546*8308f3ffSDavid Ahern 	if (!f6i)
4547*8308f3ffSDavid Ahern 		return -ENOENT;
4548*8308f3ffSDavid Ahern 
4549*8308f3ffSDavid Ahern 	if (f6i->fib6_metric != ifp->rt_priority) {
4550*8308f3ffSDavid Ahern 		/* add new one */
4551*8308f3ffSDavid Ahern 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4552*8308f3ffSDavid Ahern 				      ifp->rt_priority, ifp->idev->dev,
4553*8308f3ffSDavid Ahern 				      expires, flags, GFP_KERNEL);
4554*8308f3ffSDavid Ahern 		/* delete old one */
4555*8308f3ffSDavid Ahern 		ip6_del_rt(dev_net(ifp->idev->dev), f6i);
4556*8308f3ffSDavid Ahern 	} else {
4557*8308f3ffSDavid Ahern 		if (!expires)
4558*8308f3ffSDavid Ahern 			fib6_clean_expires(f6i);
4559*8308f3ffSDavid Ahern 		else
4560*8308f3ffSDavid Ahern 			fib6_set_expires(f6i, expires);
4561*8308f3ffSDavid Ahern 
4562*8308f3ffSDavid Ahern 		fib6_info_release(f6i);
4563*8308f3ffSDavid Ahern 	}
4564*8308f3ffSDavid Ahern 
4565*8308f3ffSDavid Ahern 	return 0;
4566*8308f3ffSDavid Ahern }
4567*8308f3ffSDavid Ahern 
4568d169a1f8SDavid Ahern static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg)
4569081bba5bSNoriaki TAKAMIYA {
45706f704992SYOSHIFUJI Hideaki 	u32 flags;
45716f704992SYOSHIFUJI Hideaki 	clock_t expires;
45724bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
457353bd6749SJiri Pirko 	bool was_managetempaddr;
45745b84efecSThomas Haller 	bool had_prefixroute;
457546d48046SYOSHIFUJI Hideaki 
4576c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4577c15b1ccaSHannes Frederic Sowa 
4578d169a1f8SDavid Ahern 	if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
4579081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
4580081bba5bSNoriaki TAKAMIYA 
4581d169a1f8SDavid Ahern 	if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
458253bd6749SJiri Pirko 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
458353bd6749SJiri Pirko 		return -EINVAL;
458453bd6749SJiri Pirko 
4585f1c02cfbSSabrina Dubroca 	if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
4586d169a1f8SDavid Ahern 		cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
4587f1c02cfbSSabrina Dubroca 
4588d169a1f8SDavid Ahern 	timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
45894bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
45904bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
4591d169a1f8SDavid Ahern 		cfg->valid_lft = timeout;
45926f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
45934bed72e4SYOSHIFUJI Hideaki 	} else {
45944bed72e4SYOSHIFUJI Hideaki 		expires = 0;
45954bed72e4SYOSHIFUJI Hideaki 		flags = 0;
4596d169a1f8SDavid Ahern 		cfg->ifa_flags |= IFA_F_PERMANENT;
45976f704992SYOSHIFUJI Hideaki 	}
4598081bba5bSNoriaki TAKAMIYA 
4599d169a1f8SDavid Ahern 	timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
46004bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
46014bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
4602d169a1f8SDavid Ahern 			cfg->ifa_flags |= IFA_F_DEPRECATED;
4603d169a1f8SDavid Ahern 		cfg->preferred_lft = timeout;
46044bed72e4SYOSHIFUJI Hideaki 	}
4605081bba5bSNoriaki TAKAMIYA 
4606081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
460753bd6749SJiri Pirko 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
46085b84efecSThomas Haller 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
46095b84efecSThomas Haller 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
461053bd6749SJiri Pirko 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4611761aac73SThomas Haller 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4612761aac73SThomas Haller 			IFA_F_NOPREFIXROUTE);
4613d169a1f8SDavid Ahern 	ifp->flags |= cfg->ifa_flags;
4614081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
4615d169a1f8SDavid Ahern 	ifp->valid_lft = cfg->valid_lft;
4616d169a1f8SDavid Ahern 	ifp->prefered_lft = cfg->preferred_lft;
4617081bba5bSNoriaki TAKAMIYA 
4618*8308f3ffSDavid Ahern 	if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
4619*8308f3ffSDavid Ahern 		ifp->rt_priority = cfg->rt_priority;
4620*8308f3ffSDavid Ahern 
4621081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
4622081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
4623081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
4624081bba5bSNoriaki TAKAMIYA 
4625d169a1f8SDavid Ahern 	if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
4626*8308f3ffSDavid Ahern 		int rc = -ENOENT;
4627*8308f3ffSDavid Ahern 
4628*8308f3ffSDavid Ahern 		if (had_prefixroute)
4629*8308f3ffSDavid Ahern 			rc = modify_prefix_route(ifp, expires, flags);
4630*8308f3ffSDavid Ahern 
4631*8308f3ffSDavid Ahern 		/* prefix route could have been deleted; if so restore it */
4632*8308f3ffSDavid Ahern 		if (rc == -ENOENT) {
4633acb54e3cSDavid Ahern 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4634*8308f3ffSDavid Ahern 					      ifp->rt_priority, ifp->idev->dev,
4635*8308f3ffSDavid Ahern 					      expires, flags, GFP_KERNEL);
4636*8308f3ffSDavid Ahern 		}
46375b84efecSThomas Haller 	} else if (had_prefixroute) {
46385b84efecSThomas Haller 		enum cleanup_prefix_rt_t action;
46395b84efecSThomas Haller 		unsigned long rt_expires;
46405b84efecSThomas Haller 
46415b84efecSThomas Haller 		write_lock_bh(&ifp->idev->lock);
46425b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &rt_expires);
46435b84efecSThomas Haller 		write_unlock_bh(&ifp->idev->lock);
46445b84efecSThomas Haller 
46455b84efecSThomas Haller 		if (action != CLEANUP_PREFIX_RT_NOP) {
46465b84efecSThomas Haller 			cleanup_prefix_route(ifp, rt_expires,
46475b84efecSThomas Haller 				action == CLEANUP_PREFIX_RT_DEL);
46485b84efecSThomas Haller 		}
4649761aac73SThomas Haller 	}
465053bd6749SJiri Pirko 
465153bd6749SJiri Pirko 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4652d169a1f8SDavid Ahern 		if (was_managetempaddr &&
4653d169a1f8SDavid Ahern 		    !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
4654d169a1f8SDavid Ahern 			cfg->valid_lft = 0;
4655d169a1f8SDavid Ahern 			cfg->preferred_lft = 0;
4656d169a1f8SDavid Ahern 		}
4657d169a1f8SDavid Ahern 		manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4658d169a1f8SDavid Ahern 				 cfg->preferred_lft, !was_managetempaddr,
4659d169a1f8SDavid Ahern 				 jiffies);
466053bd6749SJiri Pirko 	}
466153bd6749SJiri Pirko 
4662c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4663081bba5bSNoriaki TAKAMIYA 
4664081bba5bSNoriaki TAKAMIYA 	return 0;
4665081bba5bSNoriaki TAKAMIYA }
4666081bba5bSNoriaki TAKAMIYA 
4667081bba5bSNoriaki TAKAMIYA static int
4668c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4669c21ef3e3SDavid Ahern 		  struct netlink_ext_ack *extack)
46701da177e4SLinus Torvalds {
46713b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4672461d8837SThomas Graf 	struct ifaddrmsg *ifm;
4673461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
467419b1518cSDavid Ahern 	struct in6_addr *peer_pfx;
46757198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
46767198f8ceSThomas Graf 	struct net_device *dev;
4677f1c02cfbSSabrina Dubroca 	struct inet6_dev *idev;
467819b1518cSDavid Ahern 	struct ifa6_config cfg;
4679461d8837SThomas Graf 	int err;
46801da177e4SLinus Torvalds 
4681fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4682c21ef3e3SDavid Ahern 			  extack);
4683461d8837SThomas Graf 	if (err < 0)
4684461d8837SThomas Graf 		return err;
4685461d8837SThomas Graf 
468619b1518cSDavid Ahern 	memset(&cfg, 0, sizeof(cfg));
468719b1518cSDavid Ahern 
4688461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
468919b1518cSDavid Ahern 	cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
469019b1518cSDavid Ahern 	if (!cfg.pfx)
46911da177e4SLinus Torvalds 		return -EINVAL;
46921da177e4SLinus Torvalds 
469319b1518cSDavid Ahern 	cfg.peer_pfx = peer_pfx;
469419b1518cSDavid Ahern 	cfg.plen = ifm->ifa_prefixlen;
4695*8308f3ffSDavid Ahern 	if (tb[IFA_RT_PRIORITY])
4696*8308f3ffSDavid Ahern 		cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
4697*8308f3ffSDavid Ahern 
469819b1518cSDavid Ahern 	cfg.valid_lft = INFINITY_LIFE_TIME;
469919b1518cSDavid Ahern 	cfg.preferred_lft = INFINITY_LIFE_TIME;
470019b1518cSDavid Ahern 
4701461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
47020778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
4703461d8837SThomas Graf 
4704461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
470519b1518cSDavid Ahern 		cfg.valid_lft = ci->ifa_valid;
470619b1518cSDavid Ahern 		cfg.preferred_lft = ci->ifa_prefered;
47070778769dSNoriaki TAKAMIYA 	}
47080778769dSNoriaki TAKAMIYA 
4709af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
471063159f29SIan Morris 	if (!dev)
47117198f8ceSThomas Graf 		return -ENODEV;
4712081bba5bSNoriaki TAKAMIYA 
471319b1518cSDavid Ahern 	if (tb[IFA_FLAGS])
471419b1518cSDavid Ahern 		cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
471519b1518cSDavid Ahern 	else
471619b1518cSDavid Ahern 		cfg.ifa_flags = ifm->ifa_flags;
4717479840ffSJiri Pirko 
471855ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
471919b1518cSDavid Ahern 	cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
472019b1518cSDavid Ahern 			 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
472119b1518cSDavid Ahern 			 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
4722f1c02cfbSSabrina Dubroca 
4723f1c02cfbSSabrina Dubroca 	idev = ipv6_find_idev(dev);
4724f1c02cfbSSabrina Dubroca 	if (IS_ERR(idev))
4725f1c02cfbSSabrina Dubroca 		return PTR_ERR(idev);
4726f1c02cfbSSabrina Dubroca 
4727f1c02cfbSSabrina Dubroca 	if (!ipv6_allow_optimistic_dad(net, idev))
472819b1518cSDavid Ahern 		cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
4729f1c02cfbSSabrina Dubroca 
473019b1518cSDavid Ahern 	if (cfg.ifa_flags & IFA_F_NODAD &&
473119b1518cSDavid Ahern 	    cfg.ifa_flags & IFA_F_OPTIMISTIC) {
4732f1c02cfbSSabrina Dubroca 		NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4733f1c02cfbSSabrina Dubroca 		return -EINVAL;
4734f1c02cfbSSabrina Dubroca 	}
473555ebaef1SNoriaki TAKAMIYA 
473619b1518cSDavid Ahern 	ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
473763159f29SIan Morris 	if (!ifa) {
47387198f8ceSThomas Graf 		/*
47397198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
4740db9c7c39Sstephen hemminger 		 * userspace already relies on not having to provide this.
47417198f8ceSThomas Graf 		 */
474219b1518cSDavid Ahern 		return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
47431da177e4SLinus Torvalds 	}
47441da177e4SLinus Torvalds 
47457198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
47467198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
47477198f8ceSThomas Graf 		err = -EEXIST;
47487198f8ceSThomas Graf 	else
4749d169a1f8SDavid Ahern 		err = inet6_addr_modify(ifa, &cfg);
47507198f8ceSThomas Graf 
47517198f8ceSThomas Graf 	in6_ifa_put(ifa);
47527198f8ceSThomas Graf 
47537198f8ceSThomas Graf 	return err;
47547198f8ceSThomas Graf }
47557198f8ceSThomas Graf 
4756479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4757101bb229SThomas Graf 			  u8 scope, int ifindex)
4758101bb229SThomas Graf {
4759101bb229SThomas Graf 	struct ifaddrmsg *ifm;
4760101bb229SThomas Graf 
4761101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
4762101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
4763101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
4764101bb229SThomas Graf 	ifm->ifa_flags = flags;
4765101bb229SThomas Graf 	ifm->ifa_scope = scope;
4766101bb229SThomas Graf 	ifm->ifa_index = ifindex;
4767101bb229SThomas Graf }
4768101bb229SThomas Graf 
476985486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
477085486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
477185486af0SThomas Graf {
477285486af0SThomas Graf 	struct ifa_cacheinfo ci;
477385486af0SThomas Graf 
477418a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
477518a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
477685486af0SThomas Graf 	ci.ifa_prefered = preferred;
477785486af0SThomas Graf 	ci.ifa_valid = valid;
477885486af0SThomas Graf 
477985486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
478085486af0SThomas Graf }
478185486af0SThomas Graf 
4782101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
4783101bb229SThomas Graf {
4784101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
4785101bb229SThomas Graf 		return RT_SCOPE_HOST;
4786101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
4787101bb229SThomas Graf 		return RT_SCOPE_LINK;
4788101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
4789101bb229SThomas Graf 		return RT_SCOPE_SITE;
4790101bb229SThomas Graf 	else
4791101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
4792101bb229SThomas Graf }
4793101bb229SThomas Graf 
47940ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
47950ab6803bSThomas Graf {
4796339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4797caeaba79SNicolas Dichtel 	       + nla_total_size(16) /* IFA_LOCAL */
4798339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
4799479840ffSJiri Pirko 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
4800*8308f3ffSDavid Ahern 	       + nla_total_size(4)  /* IFA_FLAGS */
4801*8308f3ffSDavid Ahern 	       + nla_total_size(4)  /* IFA_RT_PRIORITY */;
48020ab6803bSThomas Graf }
4803c5396a31SYOSHIFUJI Hideaki 
48041da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
480515e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
48061da177e4SLinus Torvalds {
48071da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
480885486af0SThomas Graf 	u32 preferred, valid;
48091da177e4SLinus Torvalds 
481015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
481163159f29SIan Morris 	if (!nlh)
481226932566SPatrick McHardy 		return -EMSGSIZE;
48130ab6803bSThomas Graf 
4814101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4815101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
4816101bb229SThomas Graf 
4817fad8da3eSYasushi Asano 	if (!((ifa->flags&IFA_F_PERMANENT) &&
4818fad8da3eSYasushi Asano 	      (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
481985486af0SThomas Graf 		preferred = ifa->prefered_lft;
482085486af0SThomas Graf 		valid = ifa->valid_lft;
482185486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
48221da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
4823a1faa698SJens Rosenboom 			if (preferred > tval)
482485486af0SThomas Graf 				preferred -= tval;
4825a1faa698SJens Rosenboom 			else
4826a1faa698SJens Rosenboom 				preferred = 0;
4827f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
4828f56619fcSBen Hutchings 				if (valid > tval)
482985486af0SThomas Graf 					valid -= tval;
4830f56619fcSBen Hutchings 				else
4831f56619fcSBen Hutchings 					valid = 0;
4832f56619fcSBen Hutchings 			}
48331da177e4SLinus Torvalds 		}
48341da177e4SLinus Torvalds 	} else {
483585486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
483685486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
48371da177e4SLinus Torvalds 	}
483885486af0SThomas Graf 
48397996c799SCong Wang 	if (!ipv6_addr_any(&ifa->peer_addr)) {
4840930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4841930345eaSJiri Benc 		    nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4842caeaba79SNicolas Dichtel 			goto error;
4843caeaba79SNicolas Dichtel 	} else
4844930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4845caeaba79SNicolas Dichtel 			goto error;
4846caeaba79SNicolas Dichtel 
4847*8308f3ffSDavid Ahern 	if (ifa->rt_priority &&
4848*8308f3ffSDavid Ahern 	    nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
4849*8308f3ffSDavid Ahern 		goto error;
4850*8308f3ffSDavid Ahern 
4851caeaba79SNicolas Dichtel 	if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4852caeaba79SNicolas Dichtel 		goto error;
485385486af0SThomas Graf 
4854479840ffSJiri Pirko 	if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4855479840ffSJiri Pirko 		goto error;
4856479840ffSJiri Pirko 
4857053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4858053c095aSJohannes Berg 	return 0;
4859caeaba79SNicolas Dichtel 
4860caeaba79SNicolas Dichtel error:
4861caeaba79SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
4862caeaba79SNicolas Dichtel 	return -EMSGSIZE;
48631da177e4SLinus Torvalds }
48641da177e4SLinus Torvalds 
48651da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
486615e47304SEric W. Biederman 				u32 portid, u32 seq, int event, u16 flags)
48671da177e4SLinus Torvalds {
48681da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4869101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4870101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
48711da177e4SLinus Torvalds 
48721da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4873101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4874101bb229SThomas Graf 
487515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
487663159f29SIan Morris 	if (!nlh)
487726932566SPatrick McHardy 		return -EMSGSIZE;
48780ab6803bSThomas Graf 
4879101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4880930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
48810ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
488226932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
488326932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
488426932566SPatrick McHardy 		return -EMSGSIZE;
488526932566SPatrick McHardy 	}
488685486af0SThomas Graf 
4887053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4888053c095aSJohannes Berg 	return 0;
48891da177e4SLinus Torvalds }
48901da177e4SLinus Torvalds 
48911da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
489215e47304SEric W. Biederman 				u32 portid, u32 seq, int event, unsigned int flags)
48931da177e4SLinus Torvalds {
48949ee8cbb2SDavid Ahern 	struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
48959ee8cbb2SDavid Ahern 	int ifindex = dev ? dev->ifindex : 1;
48961da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4897101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
48981da177e4SLinus Torvalds 
48991da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4900101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4901101bb229SThomas Graf 
490215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
490363159f29SIan Morris 	if (!nlh)
490426932566SPatrick McHardy 		return -EMSGSIZE;
49050ab6803bSThomas Graf 
4906101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4907930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
49080ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
490926932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
491026932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
491126932566SPatrick McHardy 		return -EMSGSIZE;
491226932566SPatrick McHardy 	}
491385486af0SThomas Graf 
4914053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4915053c095aSJohannes Berg 	return 0;
49161da177e4SLinus Torvalds }
49171da177e4SLinus Torvalds 
4918e21e8467SStephen Hemminger enum addr_type_t {
49191da177e4SLinus Torvalds 	UNICAST_ADDR,
49201da177e4SLinus Torvalds 	MULTICAST_ADDR,
49211da177e4SLinus Torvalds 	ANYCAST_ADDR,
49221da177e4SLinus Torvalds };
49231da177e4SLinus Torvalds 
4924234b27c3SEric Dumazet /* called with rcu_read_lock() */
4925234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4926234b27c3SEric Dumazet 			  struct netlink_callback *cb, enum addr_type_t type,
4927234b27c3SEric Dumazet 			  int s_ip_idx, int *p_ip_idx)
49281da177e4SLinus Torvalds {
49291da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
49301da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
4931234b27c3SEric Dumazet 	int err = 1;
4932234b27c3SEric Dumazet 	int ip_idx = *p_ip_idx;
49331da177e4SLinus Torvalds 
49341da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
49351da177e4SLinus Torvalds 	switch (type) {
4936502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
4937502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
4938502a2ffdSstephen hemminger 
4939ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
4940502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
4941502a2ffdSstephen hemminger 			if (++ip_idx < s_ip_idx)
49421da177e4SLinus Torvalds 				continue;
49435d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifaddr(skb, ifa,
494415e47304SEric W. Biederman 						NETLINK_CB(cb->skb).portid,
49455d5619b4SYOSHIFUJI Hideaki 						cb->nlh->nlmsg_seq,
49465d5619b4SYOSHIFUJI Hideaki 						RTM_NEWADDR,
49475d5619b4SYOSHIFUJI Hideaki 						NLM_F_MULTI);
4948053c095aSJohannes Berg 			if (err < 0)
4949bcd32326SEric Dumazet 				break;
495063998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
49511da177e4SLinus Torvalds 		}
49521da177e4SLinus Torvalds 		break;
4953502a2ffdSstephen hemminger 	}
49541da177e4SLinus Torvalds 	case MULTICAST_ADDR:
49551da177e4SLinus Torvalds 		/* multicast address */
49561da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
49571da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
49581da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
49591da177e4SLinus Torvalds 				continue;
49605d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifmcaddr(skb, ifmca,
496115e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
49625d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
49635d5619b4SYOSHIFUJI Hideaki 						  RTM_GETMULTICAST,
49645d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4965053c095aSJohannes Berg 			if (err < 0)
4966bcd32326SEric Dumazet 				break;
49671da177e4SLinus Torvalds 		}
49681da177e4SLinus Torvalds 		break;
49691da177e4SLinus Torvalds 	case ANYCAST_ADDR:
49701da177e4SLinus Torvalds 		/* anycast address */
49711da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
49721da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
49731da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
49741da177e4SLinus Torvalds 				continue;
49755d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifacaddr(skb, ifaca,
497615e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
49775d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
49785d5619b4SYOSHIFUJI Hideaki 						  RTM_GETANYCAST,
49795d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4980053c095aSJohannes Berg 			if (err < 0)
4981bcd32326SEric Dumazet 				break;
49821da177e4SLinus Torvalds 		}
49831da177e4SLinus Torvalds 		break;
49841da177e4SLinus Torvalds 	default:
49851da177e4SLinus Torvalds 		break;
49861da177e4SLinus Torvalds 	}
49871da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
4988234b27c3SEric Dumazet 	*p_ip_idx = ip_idx;
4989234b27c3SEric Dumazet 	return err;
4990234b27c3SEric Dumazet }
49915d5619b4SYOSHIFUJI Hideaki 
4992234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4993234b27c3SEric Dumazet 			   enum addr_type_t type)
4994234b27c3SEric Dumazet {
4995234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
4996234b27c3SEric Dumazet 	int h, s_h;
4997234b27c3SEric Dumazet 	int idx, ip_idx;
4998234b27c3SEric Dumazet 	int s_idx, s_ip_idx;
4999234b27c3SEric Dumazet 	struct net_device *dev;
5000234b27c3SEric Dumazet 	struct inet6_dev *idev;
5001234b27c3SEric Dumazet 	struct hlist_head *head;
5002234b27c3SEric Dumazet 
5003234b27c3SEric Dumazet 	s_h = cb->args[0];
5004234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
5005234b27c3SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
5006234b27c3SEric Dumazet 
5007234b27c3SEric Dumazet 	rcu_read_lock();
500863998ac2SNicolas Dichtel 	cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
5009234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5010234b27c3SEric Dumazet 		idx = 0;
5011234b27c3SEric Dumazet 		head = &net->dev_index_head[h];
5012b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
5013234b27c3SEric Dumazet 			if (idx < s_idx)
5014234b27c3SEric Dumazet 				goto cont;
50154b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
5016234b27c3SEric Dumazet 				s_ip_idx = 0;
5017234b27c3SEric Dumazet 			ip_idx = 0;
5018e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
5019e21e8467SStephen Hemminger 			if (!idev)
5020234b27c3SEric Dumazet 				goto cont;
5021234b27c3SEric Dumazet 
5022234b27c3SEric Dumazet 			if (in6_dump_addrs(idev, skb, cb, type,
50237b46a644SDavid S. Miller 					   s_ip_idx, &ip_idx) < 0)
5024234b27c3SEric Dumazet 				goto done;
50257562f876SPavel Emelianov cont:
50267562f876SPavel Emelianov 			idx++;
50271da177e4SLinus Torvalds 		}
5028234b27c3SEric Dumazet 	}
5029234b27c3SEric Dumazet done:
5030234b27c3SEric Dumazet 	rcu_read_unlock();
5031234b27c3SEric Dumazet 	cb->args[0] = h;
5032234b27c3SEric Dumazet 	cb->args[1] = idx;
5033234b27c3SEric Dumazet 	cb->args[2] = ip_idx;
5034234b27c3SEric Dumazet 
50351da177e4SLinus Torvalds 	return skb->len;
50361da177e4SLinus Torvalds }
50371da177e4SLinus Torvalds 
50381da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
50391da177e4SLinus Torvalds {
50401da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
5041b854272bSDenis V. Lunev 
50421da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
50431da177e4SLinus Torvalds }
50441da177e4SLinus Torvalds 
50451da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
50461da177e4SLinus Torvalds {
50471da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
5048b854272bSDenis V. Lunev 
50491da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
50501da177e4SLinus Torvalds }
50511da177e4SLinus Torvalds 
50521da177e4SLinus Torvalds 
50531da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
50541da177e4SLinus Torvalds {
50551da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
5056b854272bSDenis V. Lunev 
50571da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
50581da177e4SLinus Torvalds }
50591da177e4SLinus Torvalds 
5060c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5061c21ef3e3SDavid Ahern 			     struct netlink_ext_ack *extack)
50626c223828SNoriaki TAKAMIYA {
50633b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
50641b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
50651b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
5066caeaba79SNicolas Dichtel 	struct in6_addr *addr = NULL, *peer;
50676c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
50686c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
50696c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
50706c223828SNoriaki TAKAMIYA 	int err;
50716c223828SNoriaki TAKAMIYA 
5072fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
5073c21ef3e3SDavid Ahern 			  extack);
50741b29fc2cSThomas Graf 	if (err < 0)
5075c24675f8SFlorian Westphal 		return err;
50766c223828SNoriaki TAKAMIYA 
5077caeaba79SNicolas Dichtel 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
5078c24675f8SFlorian Westphal 	if (!addr)
5079c24675f8SFlorian Westphal 		return -EINVAL;
50801b29fc2cSThomas Graf 
50811b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
50826c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
5083c24675f8SFlorian Westphal 		dev = dev_get_by_index(net, ifm->ifa_index);
50846c223828SNoriaki TAKAMIYA 
5085e21e8467SStephen Hemminger 	ifa = ipv6_get_ifaddr(net, addr, dev, 1);
5086e21e8467SStephen Hemminger 	if (!ifa) {
50871b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
50881b29fc2cSThomas Graf 		goto errout;
50896c223828SNoriaki TAKAMIYA 	}
50906c223828SNoriaki TAKAMIYA 
5091e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5092e21e8467SStephen Hemminger 	if (!skb) {
50931b29fc2cSThomas Graf 		err = -ENOBUFS;
50941b29fc2cSThomas Graf 		goto errout_ifa;
50951b29fc2cSThomas Graf 	}
50961b29fc2cSThomas Graf 
509715e47304SEric W. Biederman 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
50986c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
509926932566SPatrick McHardy 	if (err < 0) {
510026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
510126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
510226932566SPatrick McHardy 		kfree_skb(skb);
510326932566SPatrick McHardy 		goto errout_ifa;
510426932566SPatrick McHardy 	}
510515e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
51061b29fc2cSThomas Graf errout_ifa:
51076c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
51081b29fc2cSThomas Graf errout:
5109c24675f8SFlorian Westphal 	if (dev)
5110c24675f8SFlorian Westphal 		dev_put(dev);
51116c223828SNoriaki TAKAMIYA 	return err;
51126c223828SNoriaki TAKAMIYA }
51136c223828SNoriaki TAKAMIYA 
51141da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
51151da177e4SLinus Torvalds {
51161da177e4SLinus Torvalds 	struct sk_buff *skb;
5117c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
51185d620266SThomas Graf 	int err = -ENOBUFS;
51191da177e4SLinus Torvalds 
51200ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
512163159f29SIan Morris 	if (!skb)
51225d620266SThomas Graf 		goto errout;
51235d620266SThomas Graf 
51245d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
512526932566SPatrick McHardy 	if (err < 0) {
512626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
512726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
512826932566SPatrick McHardy 		kfree_skb(skb);
512926932566SPatrick McHardy 		goto errout;
513026932566SPatrick McHardy 	}
51311ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
51321ce85fe4SPablo Neira Ayuso 	return;
51335d620266SThomas Graf errout:
51345d620266SThomas Graf 	if (err < 0)
51356fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
51361da177e4SLinus Torvalds }
51371da177e4SLinus Torvalds 
5138b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
51391da177e4SLinus Torvalds 				__s32 *array, int bytes)
51401da177e4SLinus Torvalds {
514104561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
514204561c1fSThomas Graf 
51431da177e4SLinus Torvalds 	memset(array, 0, bytes);
51441da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
51451da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
51461da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
51471da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
51481da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
51491da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
51501da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
51511da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
515293908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
515393908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
5154bd11f074SMaciej Żenczykowski 	array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5155bd11f074SMaciej Żenczykowski 		jiffies_to_msecs(cnf->rtr_solicit_max_interval);
515693908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
515793908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
51581da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
5159fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5160fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5161fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5162fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
51631da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
51641da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
51651da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
51661da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
51671da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
51681da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
516965f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
51708013d1d7SHangbin Liu 	array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
5171c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
5172930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
5173930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
517493908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
517593908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
5176fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
5177bbea124bSJoel Scherpelz 	array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
517809c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
517909c884d4SYOSHIFUJI Hideaki #endif
5180930d6ff2SYOSHIFUJI Hideaki #endif
5181fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
51820bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
518395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
518495c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
51857fd2561eSErik Kline 	array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
518695c385b4SNeil Horman #endif
51877bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
51887bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
51897bc570c8SYOSHIFUJI Hideaki #endif
5190778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
51911b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
5192c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
51935cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5194b800c3b9SHannes Frederic Sowa 	array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5195d9333196SBen Greear 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5196c2943f14SHarout Hedeshian 	array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
519735103d11SAndy Gospodarek 	array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
51983d1bec99SHannes Frederic Sowa 	/* we omit DEVCONF_STABLE_SECRET for now */
51993985e8a3SErik Kline 	array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5200abbc3043SJohannes Berg 	array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
52017a02bf89SJohannes Berg 	array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5202f1705ec1SDavid Ahern 	array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
52031ababebaSDavid Lebrun 	array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5204bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
5205bf355b8dSDavid Lebrun 	array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5206bf355b8dSDavid Lebrun #endif
5207adc176c5SErik Nordmark 	array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5208d35a00b8SFelix Jia 	array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5209df789fe7SDavid Forster 	array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
52102210d6b2SMaciej Żenczykowski 	array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
52111da177e4SLinus Torvalds }
52121da177e4SLinus Torvalds 
5213b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
5214b382b191SThomas Graf {
5215b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
5216b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
5217b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5218b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5219f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5220f53adae4SDaniel Borkmann 	     + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
5221b382b191SThomas Graf }
5222b382b191SThomas Graf 
5223339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
5224339bf98fSThomas Graf {
5225339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5226339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5227339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5228339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
5229339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
52300344338bSAndy Gospodarek 	       + nla_total_size(1) /* IFLA_OPERSTATE */
5231b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5232339bf98fSThomas Graf }
5233c5396a31SYOSHIFUJI Hideaki 
5234be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5235aca05671SJia He 					int bytes)
52367f7d9a6bSHerbert Xu {
52377f7d9a6bSHerbert Xu 	int i;
5238aca05671SJia He 	int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
52397f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
52407f7d9a6bSHerbert Xu 
52417f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
5242aca05671SJia He 	put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5243aca05671SJia He 	for (i = 1; i < ICMP6_MIB_MAX; i++)
5244be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
52457f7d9a6bSHerbert Xu 
5246aca05671SJia He 	memset(&stats[ICMP6_MIB_MAX], 0, pad);
52477f7d9a6bSHerbert Xu }
52487f7d9a6bSHerbert Xu 
5249698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5250a3a77372SRaghavendra K T 					int bytes, size_t syncpoff)
52514ce3c183SEric Dumazet {
5252a3a77372SRaghavendra K T 	int i, c;
5253a3a77372SRaghavendra K T 	u64 buff[IPSTATS_MIB_MAX];
5254a3a77372SRaghavendra K T 	int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5255a3a77372SRaghavendra K T 
52564ce3c183SEric Dumazet 	BUG_ON(pad < 0);
52574ce3c183SEric Dumazet 
5258a3a77372SRaghavendra K T 	memset(buff, 0, sizeof(buff));
5259a3a77372SRaghavendra K T 	buff[0] = IPSTATS_MIB_MAX;
52604ce3c183SEric Dumazet 
5261a3a77372SRaghavendra K T 	for_each_possible_cpu(c) {
5262a3a77372SRaghavendra K T 		for (i = 1; i < IPSTATS_MIB_MAX; i++)
5263a3a77372SRaghavendra K T 			buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5264a3a77372SRaghavendra K T 	}
5265a3a77372SRaghavendra K T 
5266a3a77372SRaghavendra K T 	memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5267a3a77372SRaghavendra K T 	memset(&stats[IPSTATS_MIB_MAX], 0, pad);
52684ce3c183SEric Dumazet }
52694ce3c183SEric Dumazet 
52707f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
52717f7d9a6bSHerbert Xu 			     int bytes)
52727f7d9a6bSHerbert Xu {
52737f7d9a6bSHerbert Xu 	switch (attrtype) {
52747f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
5275a3a77372SRaghavendra K T 		__snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5276a3a77372SRaghavendra K T 				     offsetof(struct ipstats_mib, syncp));
52777f7d9a6bSHerbert Xu 		break;
52787f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
5279aca05671SJia He 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
52807f7d9a6bSHerbert Xu 		break;
52817f7d9a6bSHerbert Xu 	}
52827f7d9a6bSHerbert Xu }
52837f7d9a6bSHerbert Xu 
5284d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5285d5566fd7SSowmini Varadhan 				  u32 ext_filter_mask)
52861da177e4SLinus Torvalds {
5287bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
52881da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
52891da177e4SLinus Torvalds 
5290c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5291c78679e8SDavid S. Miller 		goto nla_put_failure;
52921da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
529324912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
529424912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
52951f9248e5SJiri Pirko 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5296c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5297c78679e8SDavid S. Miller 		goto nla_put_failure;
5298bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
529963159f29SIan Morris 	if (!nla)
530004561c1fSThomas Graf 		goto nla_put_failure;
5301bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
53021da177e4SLinus Torvalds 
5303bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
5304bf99f1bdSYOSHIFUJI Hideaki 
5305d5566fd7SSowmini Varadhan 	if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5306d5566fd7SSowmini Varadhan 		return 0;
5307d5566fd7SSowmini Varadhan 
5308bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
530963159f29SIan Morris 	if (!nla)
5310bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
5311bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5312bf99f1bdSYOSHIFUJI Hideaki 
5313bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
531463159f29SIan Morris 	if (!nla)
5315bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
5316bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
53171da177e4SLinus Torvalds 
5318f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
531963159f29SIan Morris 	if (!nla)
5320f53adae4SDaniel Borkmann 		goto nla_put_failure;
5321bc91b0f0SJiri Pirko 
5322d35a00b8SFelix Jia 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5323bc91b0f0SJiri Pirko 		goto nla_put_failure;
5324bc91b0f0SJiri Pirko 
5325f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
5326f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5327f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
5328f53adae4SDaniel Borkmann 
5329b382b191SThomas Graf 	return 0;
5330b382b191SThomas Graf 
5331b382b191SThomas Graf nla_put_failure:
5332b382b191SThomas Graf 	return -EMSGSIZE;
5333b382b191SThomas Graf }
5334b382b191SThomas Graf 
5335b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev,
5336b1974ed0SArad, Ronen 				     u32 ext_filter_mask)
5337b382b191SThomas Graf {
5338b382b191SThomas Graf 	if (!__in6_dev_get(dev))
5339b382b191SThomas Graf 		return 0;
5340b382b191SThomas Graf 
5341b382b191SThomas Graf 	return inet6_ifla6_size();
5342b382b191SThomas Graf }
5343b382b191SThomas Graf 
5344d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5345d5566fd7SSowmini Varadhan 			      u32 ext_filter_mask)
5346b382b191SThomas Graf {
5347b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
5348b382b191SThomas Graf 
5349b382b191SThomas Graf 	if (!idev)
5350b382b191SThomas Graf 		return -ENODATA;
5351b382b191SThomas Graf 
5352d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5353b382b191SThomas Graf 		return -EMSGSIZE;
5354b382b191SThomas Graf 
5355b382b191SThomas Graf 	return 0;
5356b382b191SThomas Graf }
5357b382b191SThomas Graf 
5358f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5359f53adae4SDaniel Borkmann {
5360f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
5361f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
536247e27d5eSDaniel Borkmann 	bool clear_token, update_rs = false;
5363dc848292SHannes Frederic Sowa 	struct in6_addr ll_addr;
5364f53adae4SDaniel Borkmann 
5365c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
5366c15b1ccaSHannes Frederic Sowa 
536763159f29SIan Morris 	if (!token)
5368f53adae4SDaniel Borkmann 		return -EINVAL;
5369f53adae4SDaniel Borkmann 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5370f53adae4SDaniel Borkmann 		return -EINVAL;
5371f53adae4SDaniel Borkmann 	if (!ipv6_accept_ra(idev))
5372f53adae4SDaniel Borkmann 		return -EINVAL;
5373bd11f074SMaciej Żenczykowski 	if (idev->cnf.rtr_solicits == 0)
5374f53adae4SDaniel Borkmann 		return -EINVAL;
5375f53adae4SDaniel Borkmann 
5376f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5377f53adae4SDaniel Borkmann 
5378f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5379f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5380f53adae4SDaniel Borkmann 
5381f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5382f53adae4SDaniel Borkmann 
538347e27d5eSDaniel Borkmann 	clear_token = ipv6_addr_any(token);
538447e27d5eSDaniel Borkmann 	if (clear_token)
538547e27d5eSDaniel Borkmann 		goto update_lft;
538647e27d5eSDaniel Borkmann 
5387dc848292SHannes Frederic Sowa 	if (!idev->dead && (idev->if_flags & IF_READY) &&
5388dc848292SHannes Frederic Sowa 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5389dc848292SHannes Frederic Sowa 			     IFA_F_OPTIMISTIC)) {
5390fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
5391fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
5392fc403832SDaniel Borkmann 		 */
5393f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5394fc403832SDaniel Borkmann 		update_rs = true;
5395fc403832SDaniel Borkmann 	}
5396f53adae4SDaniel Borkmann 
539747e27d5eSDaniel Borkmann update_lft:
5398f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5399fc403832SDaniel Borkmann 
540077ecaaceSHannes Frederic Sowa 	if (update_rs) {
5401f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
5402bd11f074SMaciej Żenczykowski 		idev->rs_interval = rfc3315_s14_backoff_init(
5403bd11f074SMaciej Żenczykowski 			idev->cnf.rtr_solicit_interval);
540477ecaaceSHannes Frederic Sowa 		idev->rs_probes = 1;
5405bd11f074SMaciej Żenczykowski 		addrconf_mod_rs_timer(idev, idev->rs_interval);
540677ecaaceSHannes Frederic Sowa 	}
5407f53adae4SDaniel Borkmann 
5408f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
5409f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
5410f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
5411617fe29dSDaniel Borkmann 		if (ifp->tokenized) {
5412f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
5413f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
5414f53adae4SDaniel Borkmann 		}
5415f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
5416f53adae4SDaniel Borkmann 	}
5417f53adae4SDaniel Borkmann 
5418f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5419b2ed64a9SLubomir Rintel 	inet6_ifinfo_notify(RTM_NEWLINK, idev);
5420c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
5421f53adae4SDaniel Borkmann 	return 0;
5422f53adae4SDaniel Borkmann }
5423f53adae4SDaniel Borkmann 
542411b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
542511b1f828SDaniel Borkmann 	[IFLA_INET6_ADDR_GEN_MODE]	= { .type = NLA_U8 },
542611b1f828SDaniel Borkmann 	[IFLA_INET6_TOKEN]		= { .len = sizeof(struct in6_addr) },
542711b1f828SDaniel Borkmann };
542811b1f828SDaniel Borkmann 
542911b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev,
543011b1f828SDaniel Borkmann 				  const struct nlattr *nla)
543111b1f828SDaniel Borkmann {
543211b1f828SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
543311b1f828SDaniel Borkmann 
543411b1f828SDaniel Borkmann 	if (dev && !__in6_dev_get(dev))
543511b1f828SDaniel Borkmann 		return -EAFNOSUPPORT;
543611b1f828SDaniel Borkmann 
5437fceb6435SJohannes Berg 	return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy,
5438fceb6435SJohannes Berg 				NULL);
543911b1f828SDaniel Borkmann }
544011b1f828SDaniel Borkmann 
5441d35a00b8SFelix Jia static int check_addr_gen_mode(int mode)
5442d35a00b8SFelix Jia {
5443d35a00b8SFelix Jia 	if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5444d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_NONE &&
5445d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5446d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_RANDOM)
5447d35a00b8SFelix Jia 		return -EINVAL;
5448d35a00b8SFelix Jia 	return 1;
5449d35a00b8SFelix Jia }
5450d35a00b8SFelix Jia 
5451d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5452d35a00b8SFelix Jia 				int mode)
5453d35a00b8SFelix Jia {
5454d35a00b8SFelix Jia 	if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5455d35a00b8SFelix Jia 	    !idev->cnf.stable_secret.initialized &&
5456d35a00b8SFelix Jia 	    !net->ipv6.devconf_dflt->stable_secret.initialized)
5457d35a00b8SFelix Jia 		return -EINVAL;
5458d35a00b8SFelix Jia 	return 1;
5459d35a00b8SFelix Jia }
5460d35a00b8SFelix Jia 
5461f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5462f53adae4SDaniel Borkmann {
5463f53adae4SDaniel Borkmann 	int err = -EINVAL;
5464f53adae4SDaniel Borkmann 	struct inet6_dev *idev = __in6_dev_get(dev);
5465f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
5466f53adae4SDaniel Borkmann 
5467f53adae4SDaniel Borkmann 	if (!idev)
5468f53adae4SDaniel Borkmann 		return -EAFNOSUPPORT;
5469f53adae4SDaniel Borkmann 
5470fceb6435SJohannes Berg 	if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
5471f53adae4SDaniel Borkmann 		BUG();
5472f53adae4SDaniel Borkmann 
5473bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_TOKEN]) {
5474f53adae4SDaniel Borkmann 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5475bc91b0f0SJiri Pirko 		if (err)
5476bc91b0f0SJiri Pirko 			return err;
5477bc91b0f0SJiri Pirko 	}
5478bc91b0f0SJiri Pirko 
5479bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5480bc91b0f0SJiri Pirko 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5481bc91b0f0SJiri Pirko 
5482d35a00b8SFelix Jia 		if (check_addr_gen_mode(mode) < 0 ||
5483d35a00b8SFelix Jia 		    check_stable_privacy(idev, dev_net(dev), mode) < 0)
5484bc91b0f0SJiri Pirko 			return -EINVAL;
5485622c81d5SHannes Frederic Sowa 
5486d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = mode;
5487bc91b0f0SJiri Pirko 		err = 0;
5488bc91b0f0SJiri Pirko 	}
5489f53adae4SDaniel Borkmann 
5490f53adae4SDaniel Borkmann 	return err;
5491f53adae4SDaniel Borkmann }
5492f53adae4SDaniel Borkmann 
5493b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
549415e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
5495b382b191SThomas Graf {
5496b382b191SThomas Graf 	struct net_device *dev = idev->dev;
5497b382b191SThomas Graf 	struct ifinfomsg *hdr;
5498b382b191SThomas Graf 	struct nlmsghdr *nlh;
5499b382b191SThomas Graf 	void *protoinfo;
5500b382b191SThomas Graf 
550115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
550263159f29SIan Morris 	if (!nlh)
5503b382b191SThomas Graf 		return -EMSGSIZE;
5504b382b191SThomas Graf 
5505b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
5506b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
5507b382b191SThomas Graf 	hdr->__ifi_pad = 0;
5508b382b191SThomas Graf 	hdr->ifi_type = dev->type;
5509b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
5510b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
5511b382b191SThomas Graf 	hdr->ifi_change = 0;
5512b382b191SThomas Graf 
5513c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5514c78679e8SDavid S. Miller 	    (dev->addr_len &&
5515c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5516c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5517a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
55180344338bSAndy Gospodarek 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
55190344338bSAndy Gospodarek 	    nla_put_u8(skb, IFLA_OPERSTATE,
55200344338bSAndy Gospodarek 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5521c78679e8SDavid S. Miller 		goto nla_put_failure;
5522b382b191SThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
552363159f29SIan Morris 	if (!protoinfo)
5524b382b191SThomas Graf 		goto nla_put_failure;
5525b382b191SThomas Graf 
5526d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5527b382b191SThomas Graf 		goto nla_put_failure;
5528b382b191SThomas Graf 
552904561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
5530053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5531053c095aSJohannes Berg 	return 0;
55321da177e4SLinus Torvalds 
553304561c1fSThomas Graf nla_put_failure:
553426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
553526932566SPatrick McHardy 	return -EMSGSIZE;
55361da177e4SLinus Torvalds }
55371da177e4SLinus Torvalds 
55381da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
55391da177e4SLinus Torvalds {
55403b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
554184d2697dSEric Dumazet 	int h, s_h;
5542434a8a58SDavid S. Miller 	int idx = 0, s_idx;
55431da177e4SLinus Torvalds 	struct net_device *dev;
55441da177e4SLinus Torvalds 	struct inet6_dev *idev;
554584d2697dSEric Dumazet 	struct hlist_head *head;
55461da177e4SLinus Torvalds 
554784d2697dSEric Dumazet 	s_h = cb->args[0];
554884d2697dSEric Dumazet 	s_idx = cb->args[1];
554984d2697dSEric Dumazet 
555084d2697dSEric Dumazet 	rcu_read_lock();
555184d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
55527562f876SPavel Emelianov 		idx = 0;
555384d2697dSEric Dumazet 		head = &net->dev_index_head[h];
5554b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
55551da177e4SLinus Torvalds 			if (idx < s_idx)
55567562f876SPavel Emelianov 				goto cont;
555784d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
555884d2697dSEric Dumazet 			if (!idev)
55597562f876SPavel Emelianov 				goto cont;
556084d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
556115e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
556284d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
5563053c095aSJohannes Berg 					      RTM_NEWLINK, NLM_F_MULTI) < 0)
556484d2697dSEric Dumazet 				goto out;
55657562f876SPavel Emelianov cont:
55667562f876SPavel Emelianov 			idx++;
55671da177e4SLinus Torvalds 		}
556884d2697dSEric Dumazet 	}
556984d2697dSEric Dumazet out:
557084d2697dSEric Dumazet 	rcu_read_unlock();
557184d2697dSEric Dumazet 	cb->args[1] = idx;
557284d2697dSEric Dumazet 	cb->args[0] = h;
55731da177e4SLinus Torvalds 
55741da177e4SLinus Torvalds 	return skb->len;
55751da177e4SLinus Torvalds }
55761da177e4SLinus Torvalds 
55771da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
55781da177e4SLinus Torvalds {
55791da177e4SLinus Torvalds 	struct sk_buff *skb;
5580c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
55818d7a76c9SThomas Graf 	int err = -ENOBUFS;
55821da177e4SLinus Torvalds 
5583339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
558463159f29SIan Morris 	if (!skb)
55858d7a76c9SThomas Graf 		goto errout;
55868d7a76c9SThomas Graf 
55878d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
558826932566SPatrick McHardy 	if (err < 0) {
558926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
559026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
559126932566SPatrick McHardy 		kfree_skb(skb);
559226932566SPatrick McHardy 		goto errout;
559326932566SPatrick McHardy 	}
55945f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
55951ce85fe4SPablo Neira Ayuso 	return;
55968d7a76c9SThomas Graf errout:
55978d7a76c9SThomas Graf 	if (err < 0)
55985f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
55991da177e4SLinus Torvalds }
56001da177e4SLinus Torvalds 
5601339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
5602339bf98fSThomas Graf {
5603339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
5604339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
5605339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
5606339bf98fSThomas Graf }
5607c5396a31SYOSHIFUJI Hideaki 
56081da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
560915e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
5610b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
56111da177e4SLinus Torvalds {
56121da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
56131da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
56141da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
56151da177e4SLinus Torvalds 
561615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
561763159f29SIan Morris 	if (!nlh)
561826932566SPatrick McHardy 		return -EMSGSIZE;
56196051e2f4SThomas Graf 
56206051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
56211da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
56228a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
56238a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
56241da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
56251da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
56261da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
56278a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
56281da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
56291da177e4SLinus Torvalds 	if (pinfo->onlink)
56301da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
56311da177e4SLinus Torvalds 	if (pinfo->autoconf)
56321da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
56331da177e4SLinus Torvalds 
5634c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5635c78679e8SDavid S. Miller 		goto nla_put_failure;
56361da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
56371da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
5638c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5639c78679e8SDavid S. Miller 		goto nla_put_failure;
5640053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5641053c095aSJohannes Berg 	return 0;
56421da177e4SLinus Torvalds 
56436051e2f4SThomas Graf nla_put_failure:
564426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
564526932566SPatrick McHardy 	return -EMSGSIZE;
56461da177e4SLinus Torvalds }
56471da177e4SLinus Torvalds 
56481da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
56491da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
56501da177e4SLinus Torvalds {
56511da177e4SLinus Torvalds 	struct sk_buff *skb;
5652c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
56538c384bfaSThomas Graf 	int err = -ENOBUFS;
56541da177e4SLinus Torvalds 
5655339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
565663159f29SIan Morris 	if (!skb)
56578c384bfaSThomas Graf 		goto errout;
56588c384bfaSThomas Graf 
56598c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
566026932566SPatrick McHardy 	if (err < 0) {
566126932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
566226932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
566326932566SPatrick McHardy 		kfree_skb(skb);
566426932566SPatrick McHardy 		goto errout;
566526932566SPatrick McHardy 	}
56661ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
56671ce85fe4SPablo Neira Ayuso 	return;
56688c384bfaSThomas Graf errout:
56698c384bfaSThomas Graf 	if (err < 0)
56706fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
56711da177e4SLinus Torvalds }
56721da177e4SLinus Torvalds 
56731da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
56741da177e4SLinus Torvalds {
567563998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
567663998ac2SNicolas Dichtel 
5677c15b1ccaSHannes Frederic Sowa 	if (event)
5678c15b1ccaSHannes Frederic Sowa 		ASSERT_RTNL();
5679c15b1ccaSHannes Frederic Sowa 
56801da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
56811da177e4SLinus Torvalds 
56821da177e4SLinus Torvalds 	switch (event) {
56831da177e4SLinus Torvalds 	case RTM_NEWADDR:
568495c385b4SNeil Horman 		/*
568595c385b4SNeil Horman 		 * If the address was optimistic
568695c385b4SNeil Horman 		 * we inserted the route at the start of
568795c385b4SNeil Horman 		 * our DAD process, so we don't need
568895c385b4SNeil Horman 		 * to do it again
568995c385b4SNeil Horman 		 */
569093c2fb25SDavid Ahern 		if (!rcu_access_pointer(ifp->rt->fib6_node))
5691afb1d4b5SDavid Ahern 			ip6_ins_rt(net, ifp->rt);
56921da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
56931da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
56947996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr))
5695*8308f3ffSDavid Ahern 			addrconf_prefix_route(&ifp->peer_addr, 128, 0,
5696acb54e3cSDavid Ahern 					      ifp->idev->dev, 0, 0,
569727b10608SDavid Ahern 					      GFP_ATOMIC);
56981da177e4SLinus Torvalds 		break;
56991da177e4SLinus Torvalds 	case RTM_DELADDR:
57001da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
57011da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
57021da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
57037996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr)) {
57048d1c802bSDavid Ahern 			struct fib6_info *rt;
5705caeaba79SNicolas Dichtel 
5706e7478dfcSNicolas Dichtel 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5707e7478dfcSNicolas Dichtel 						       ifp->idev->dev, 0, 0);
57088e3d5be7SMartin KaFai Lau 			if (rt)
5709afb1d4b5SDavid Ahern 				ip6_del_rt(net, rt);
5710caeaba79SNicolas Dichtel 		}
571138bd10c4SDavid Ahern 		if (ifp->rt) {
5712afb1d4b5SDavid Ahern 			ip6_del_rt(net, ifp->rt);
571393531c67SDavid Ahern 			ifp->rt = NULL;
571438bd10c4SDavid Ahern 		}
5715705f1c86SHannes Frederic Sowa 		rt_genid_bump_ipv6(net);
57161da177e4SLinus Torvalds 		break;
57171da177e4SLinus Torvalds 	}
571863998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
57191da177e4SLinus Torvalds }
57201da177e4SLinus Torvalds 
57211da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
57221da177e4SLinus Torvalds {
57238814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
57241da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
57251da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
57268814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
57271da177e4SLinus Torvalds }
57281da177e4SLinus Torvalds 
57291da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
57301da177e4SLinus Torvalds 
57311da177e4SLinus Torvalds static
5732fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
57331da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
57341da177e4SLinus Torvalds {
57351da177e4SLinus Torvalds 	int *valp = ctl->data;
57361da177e4SLinus Torvalds 	int val = *valp;
573788af182eSEric W. Biederman 	loff_t pos = *ppos;
5738fe2c6338SJoe Perches 	struct ctl_table lctl;
57391da177e4SLinus Torvalds 	int ret;
57401da177e4SLinus Torvalds 
5741013d97e9SFrancesco Ruggeri 	/*
5742013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
5743013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5744013d97e9SFrancesco Ruggeri 	 */
5745013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5746013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5747013d97e9SFrancesco Ruggeri 
5748013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
57491da177e4SLinus Torvalds 
5750c8fecf22SPavel Emelyanov 	if (write)
5751b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
575288af182eSEric W. Biederman 	if (ret)
575388af182eSEric W. Biederman 		*ppos = pos;
57541da177e4SLinus Torvalds 	return ret;
57551da177e4SLinus Torvalds }
57561da177e4SLinus Torvalds 
575777751427SMarcelo Leitner static
575877751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
575977751427SMarcelo Leitner 			void __user *buffer, size_t *lenp, loff_t *ppos)
576077751427SMarcelo Leitner {
576177751427SMarcelo Leitner 	struct inet6_dev *idev = ctl->extra1;
576277751427SMarcelo Leitner 	int min_mtu = IPV6_MIN_MTU;
576377751427SMarcelo Leitner 	struct ctl_table lctl;
576477751427SMarcelo Leitner 
576577751427SMarcelo Leitner 	lctl = *ctl;
576677751427SMarcelo Leitner 	lctl.extra1 = &min_mtu;
576777751427SMarcelo Leitner 	lctl.extra2 = idev ? &idev->dev->mtu : NULL;
576877751427SMarcelo Leitner 
576977751427SMarcelo Leitner 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
577077751427SMarcelo Leitner }
577177751427SMarcelo Leitner 
577256d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
577356d417b1SBrian Haley {
577475538c2bSCong Wang 	struct netdev_notifier_info info;
577575538c2bSCong Wang 
577656d417b1SBrian Haley 	if (!idev || !idev->dev)
577756d417b1SBrian Haley 		return;
577856d417b1SBrian Haley 
577975538c2bSCong Wang 	netdev_notifier_info_init(&info, idev->dev);
578056d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
578175538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_DOWN, &info);
578256d417b1SBrian Haley 	else
578375538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_UP, &info);
578456d417b1SBrian Haley }
578556d417b1SBrian Haley 
578656d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
578756d417b1SBrian Haley {
578856d417b1SBrian Haley 	struct net_device *dev;
578956d417b1SBrian Haley 	struct inet6_dev *idev;
579056d417b1SBrian Haley 
579103e4deffSKefeng Wang 	for_each_netdev(net, dev) {
579256d417b1SBrian Haley 		idev = __in6_dev_get(dev);
579356d417b1SBrian Haley 		if (idev) {
579456d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
579556d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
579656d417b1SBrian Haley 			if (changed)
579756d417b1SBrian Haley 				dev_disable_change(idev);
579856d417b1SBrian Haley 		}
579956d417b1SBrian Haley 	}
580056d417b1SBrian Haley }
580156d417b1SBrian Haley 
5802013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
580356d417b1SBrian Haley {
580456d417b1SBrian Haley 	struct net *net;
5805013d97e9SFrancesco Ruggeri 	int old;
5806013d97e9SFrancesco Ruggeri 
5807013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
5808013d97e9SFrancesco Ruggeri 		return restart_syscall();
580956d417b1SBrian Haley 
581056d417b1SBrian Haley 	net = (struct net *)table->extra2;
5811013d97e9SFrancesco Ruggeri 	old = *p;
5812013d97e9SFrancesco Ruggeri 	*p = newf;
581356d417b1SBrian Haley 
5814013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5815013d97e9SFrancesco Ruggeri 		rtnl_unlock();
581656d417b1SBrian Haley 		return 0;
581788af182eSEric W. Biederman 	}
581856d417b1SBrian Haley 
581956d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
582056d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
582156d417b1SBrian Haley 		addrconf_disable_change(net, newf);
5822013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
582356d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
582456d417b1SBrian Haley 
582556d417b1SBrian Haley 	rtnl_unlock();
582656d417b1SBrian Haley 	return 0;
582756d417b1SBrian Haley }
582856d417b1SBrian Haley 
582956d417b1SBrian Haley static
5830fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
583156d417b1SBrian Haley 			    void __user *buffer, size_t *lenp, loff_t *ppos)
583256d417b1SBrian Haley {
583356d417b1SBrian Haley 	int *valp = ctl->data;
583456d417b1SBrian Haley 	int val = *valp;
583588af182eSEric W. Biederman 	loff_t pos = *ppos;
5836fe2c6338SJoe Perches 	struct ctl_table lctl;
583756d417b1SBrian Haley 	int ret;
583856d417b1SBrian Haley 
5839013d97e9SFrancesco Ruggeri 	/*
5840013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
5841013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5842013d97e9SFrancesco Ruggeri 	 */
5843013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5844013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5845013d97e9SFrancesco Ruggeri 
5846013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
584756d417b1SBrian Haley 
584856d417b1SBrian Haley 	if (write)
584956d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
585088af182eSEric W. Biederman 	if (ret)
585188af182eSEric W. Biederman 		*ppos = pos;
585256d417b1SBrian Haley 	return ret;
585356d417b1SBrian Haley }
585456d417b1SBrian Haley 
5855c92d5491Sstephen hemminger static
5856c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5857c92d5491Sstephen hemminger 			      void __user *buffer, size_t *lenp, loff_t *ppos)
5858c92d5491Sstephen hemminger {
5859c92d5491Sstephen hemminger 	int *valp = ctl->data;
5860c92d5491Sstephen hemminger 	int ret;
5861c92d5491Sstephen hemminger 	int old, new;
5862c92d5491Sstephen hemminger 
5863c92d5491Sstephen hemminger 	old = *valp;
5864c92d5491Sstephen hemminger 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5865c92d5491Sstephen hemminger 	new = *valp;
5866c92d5491Sstephen hemminger 
5867c92d5491Sstephen hemminger 	if (write && old != new) {
5868c92d5491Sstephen hemminger 		struct net *net = ctl->extra2;
5869c92d5491Sstephen hemminger 
5870c92d5491Sstephen hemminger 		if (!rtnl_trylock())
5871c92d5491Sstephen hemminger 			return restart_syscall();
5872c92d5491Sstephen hemminger 
5873c92d5491Sstephen hemminger 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
587485b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
587585b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
5876c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_DEFAULT,
5877c92d5491Sstephen hemminger 						     net->ipv6.devconf_dflt);
5878c92d5491Sstephen hemminger 		else if (valp == &net->ipv6.devconf_all->proxy_ndp)
587985b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
588085b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
5881c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_ALL,
5882c92d5491Sstephen hemminger 						     net->ipv6.devconf_all);
5883c92d5491Sstephen hemminger 		else {
5884c92d5491Sstephen hemminger 			struct inet6_dev *idev = ctl->extra1;
5885c92d5491Sstephen hemminger 
588685b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
588785b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
5888c92d5491Sstephen hemminger 						     idev->dev->ifindex,
5889c92d5491Sstephen hemminger 						     &idev->cnf);
5890c92d5491Sstephen hemminger 		}
5891c92d5491Sstephen hemminger 		rtnl_unlock();
5892c92d5491Sstephen hemminger 	}
5893c92d5491Sstephen hemminger 
5894c92d5491Sstephen hemminger 	return ret;
5895c92d5491Sstephen hemminger }
5896c92d5491Sstephen hemminger 
5897d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5898d35a00b8SFelix Jia 					 void __user *buffer, size_t *lenp,
5899d35a00b8SFelix Jia 					 loff_t *ppos)
5900d35a00b8SFelix Jia {
5901d35a00b8SFelix Jia 	int ret = 0;
5902d35a00b8SFelix Jia 	int new_val;
5903d35a00b8SFelix Jia 	struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5904d35a00b8SFelix Jia 	struct net *net = (struct net *)ctl->extra2;
5905d35a00b8SFelix Jia 
59068c171d6cSFelix Jia 	if (!rtnl_trylock())
59078c171d6cSFelix Jia 		return restart_syscall();
59088c171d6cSFelix Jia 
5909d35a00b8SFelix Jia 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5910d35a00b8SFelix Jia 
5911d35a00b8SFelix Jia 	if (write) {
5912d35a00b8SFelix Jia 		new_val = *((int *)ctl->data);
5913d35a00b8SFelix Jia 
59148c171d6cSFelix Jia 		if (check_addr_gen_mode(new_val) < 0) {
59158c171d6cSFelix Jia 			ret = -EINVAL;
59168c171d6cSFelix Jia 			goto out;
59178c171d6cSFelix Jia 		}
5918d35a00b8SFelix Jia 
5919d35a00b8SFelix Jia 		/* request for default */
5920d35a00b8SFelix Jia 		if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5921d35a00b8SFelix Jia 			ipv6_devconf_dflt.addr_gen_mode = new_val;
5922d35a00b8SFelix Jia 
5923d35a00b8SFelix Jia 		/* request for individual net device */
5924d35a00b8SFelix Jia 		} else {
5925d35a00b8SFelix Jia 			if (!idev)
59268c171d6cSFelix Jia 				goto out;
5927d35a00b8SFelix Jia 
59288c171d6cSFelix Jia 			if (check_stable_privacy(idev, net, new_val) < 0) {
59298c171d6cSFelix Jia 				ret = -EINVAL;
59308c171d6cSFelix Jia 				goto out;
59318c171d6cSFelix Jia 			}
5932d35a00b8SFelix Jia 
5933d35a00b8SFelix Jia 			if (idev->cnf.addr_gen_mode != new_val) {
5934d35a00b8SFelix Jia 				idev->cnf.addr_gen_mode = new_val;
5935d35a00b8SFelix Jia 				addrconf_dev_config(idev->dev);
59368c171d6cSFelix Jia 			}
59378c171d6cSFelix Jia 		}
59388c171d6cSFelix Jia 	}
59398c171d6cSFelix Jia 
59408c171d6cSFelix Jia out:
5941d35a00b8SFelix Jia 	rtnl_unlock();
5942d35a00b8SFelix Jia 
5943d35a00b8SFelix Jia 	return ret;
5944d35a00b8SFelix Jia }
5945d35a00b8SFelix Jia 
59463d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
59473d1bec99SHannes Frederic Sowa 					 void __user *buffer, size_t *lenp,
59483d1bec99SHannes Frederic Sowa 					 loff_t *ppos)
59493d1bec99SHannes Frederic Sowa {
59503d1bec99SHannes Frederic Sowa 	int err;
59513d1bec99SHannes Frederic Sowa 	struct in6_addr addr;
59523d1bec99SHannes Frederic Sowa 	char str[IPV6_MAX_STRLEN];
59533d1bec99SHannes Frederic Sowa 	struct ctl_table lctl = *ctl;
5954622c81d5SHannes Frederic Sowa 	struct net *net = ctl->extra2;
59553d1bec99SHannes Frederic Sowa 	struct ipv6_stable_secret *secret = ctl->data;
59563d1bec99SHannes Frederic Sowa 
5957622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5958622c81d5SHannes Frederic Sowa 		return -EIO;
5959622c81d5SHannes Frederic Sowa 
59603d1bec99SHannes Frederic Sowa 	lctl.maxlen = IPV6_MAX_STRLEN;
59613d1bec99SHannes Frederic Sowa 	lctl.data = str;
59623d1bec99SHannes Frederic Sowa 
59633d1bec99SHannes Frederic Sowa 	if (!rtnl_trylock())
59643d1bec99SHannes Frederic Sowa 		return restart_syscall();
59653d1bec99SHannes Frederic Sowa 
59663d1bec99SHannes Frederic Sowa 	if (!write && !secret->initialized) {
59673d1bec99SHannes Frederic Sowa 		err = -EIO;
59683d1bec99SHannes Frederic Sowa 		goto out;
59693d1bec99SHannes Frederic Sowa 	}
59703d1bec99SHannes Frederic Sowa 
59715449a5caSWANG Cong 	err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
59723d1bec99SHannes Frederic Sowa 	if (err >= sizeof(str)) {
59733d1bec99SHannes Frederic Sowa 		err = -EIO;
59743d1bec99SHannes Frederic Sowa 		goto out;
59753d1bec99SHannes Frederic Sowa 	}
59763d1bec99SHannes Frederic Sowa 
59773d1bec99SHannes Frederic Sowa 	err = proc_dostring(&lctl, write, buffer, lenp, ppos);
59783d1bec99SHannes Frederic Sowa 	if (err || !write)
59793d1bec99SHannes Frederic Sowa 		goto out;
59803d1bec99SHannes Frederic Sowa 
59813d1bec99SHannes Frederic Sowa 	if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
59823d1bec99SHannes Frederic Sowa 		err = -EIO;
59833d1bec99SHannes Frederic Sowa 		goto out;
59843d1bec99SHannes Frederic Sowa 	}
59853d1bec99SHannes Frederic Sowa 
59863d1bec99SHannes Frederic Sowa 	secret->initialized = true;
59873d1bec99SHannes Frederic Sowa 	secret->secret = addr;
59883d1bec99SHannes Frederic Sowa 
5989622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5990622c81d5SHannes Frederic Sowa 		struct net_device *dev;
5991622c81d5SHannes Frederic Sowa 
5992622c81d5SHannes Frederic Sowa 		for_each_netdev(net, dev) {
5993622c81d5SHannes Frederic Sowa 			struct inet6_dev *idev = __in6_dev_get(dev);
5994622c81d5SHannes Frederic Sowa 
5995622c81d5SHannes Frederic Sowa 			if (idev) {
5996d35a00b8SFelix Jia 				idev->cnf.addr_gen_mode =
5997622c81d5SHannes Frederic Sowa 					IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5998622c81d5SHannes Frederic Sowa 			}
5999622c81d5SHannes Frederic Sowa 		}
6000622c81d5SHannes Frederic Sowa 	} else {
6001622c81d5SHannes Frederic Sowa 		struct inet6_dev *idev = ctl->extra1;
6002622c81d5SHannes Frederic Sowa 
6003d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6004622c81d5SHannes Frederic Sowa 	}
6005622c81d5SHannes Frederic Sowa 
60063d1bec99SHannes Frederic Sowa out:
60073d1bec99SHannes Frederic Sowa 	rtnl_unlock();
60083d1bec99SHannes Frederic Sowa 
60093d1bec99SHannes Frederic Sowa 	return err;
60103d1bec99SHannes Frederic Sowa }
6011c92d5491Sstephen hemminger 
601235103d11SAndy Gospodarek static
601335103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
601435103d11SAndy Gospodarek 						int write,
601535103d11SAndy Gospodarek 						void __user *buffer,
601635103d11SAndy Gospodarek 						size_t *lenp,
601735103d11SAndy Gospodarek 						loff_t *ppos)
601835103d11SAndy Gospodarek {
601935103d11SAndy Gospodarek 	int *valp = ctl->data;
602035103d11SAndy Gospodarek 	int val = *valp;
602135103d11SAndy Gospodarek 	loff_t pos = *ppos;
602235103d11SAndy Gospodarek 	struct ctl_table lctl;
602335103d11SAndy Gospodarek 	int ret;
602435103d11SAndy Gospodarek 
602535103d11SAndy Gospodarek 	/* ctl->data points to idev->cnf.ignore_routes_when_linkdown
602635103d11SAndy Gospodarek 	 * we should not modify it until we get the rtnl lock.
602735103d11SAndy Gospodarek 	 */
602835103d11SAndy Gospodarek 	lctl = *ctl;
602935103d11SAndy Gospodarek 	lctl.data = &val;
603035103d11SAndy Gospodarek 
603135103d11SAndy Gospodarek 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
603235103d11SAndy Gospodarek 
603335103d11SAndy Gospodarek 	if (write)
603435103d11SAndy Gospodarek 		ret = addrconf_fixup_linkdown(ctl, valp, val);
603535103d11SAndy Gospodarek 	if (ret)
603635103d11SAndy Gospodarek 		*ppos = pos;
603735103d11SAndy Gospodarek 	return ret;
603835103d11SAndy Gospodarek }
603935103d11SAndy Gospodarek 
6040df789fe7SDavid Forster static
6041df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6042df789fe7SDavid Forster {
6043df789fe7SDavid Forster 	if (rt) {
6044df789fe7SDavid Forster 		if (action)
6045df789fe7SDavid Forster 			rt->dst.flags |= DST_NOPOLICY;
6046df789fe7SDavid Forster 		else
6047df789fe7SDavid Forster 			rt->dst.flags &= ~DST_NOPOLICY;
6048df789fe7SDavid Forster 	}
6049df789fe7SDavid Forster }
6050df789fe7SDavid Forster 
6051df789fe7SDavid Forster static
6052df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6053df789fe7SDavid Forster {
6054df789fe7SDavid Forster 	struct inet6_ifaddr *ifa;
6055df789fe7SDavid Forster 
6056df789fe7SDavid Forster 	read_lock_bh(&idev->lock);
6057df789fe7SDavid Forster 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
6058df789fe7SDavid Forster 		spin_lock(&ifa->lock);
6059df789fe7SDavid Forster 		if (ifa->rt) {
60608d1c802bSDavid Ahern 			struct fib6_info *rt = ifa->rt;
6061df789fe7SDavid Forster 			int cpu;
6062df789fe7SDavid Forster 
606366f5d6ceSWei Wang 			rcu_read_lock();
60643b6761d1SDavid Ahern 			ifa->rt->dst_nopolicy = val ? true : false;
6065df789fe7SDavid Forster 			if (rt->rt6i_pcpu) {
6066df789fe7SDavid Forster 				for_each_possible_cpu(cpu) {
6067df789fe7SDavid Forster 					struct rt6_info **rtp;
6068df789fe7SDavid Forster 
6069df789fe7SDavid Forster 					rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
6070df789fe7SDavid Forster 					addrconf_set_nopolicy(*rtp, val);
6071df789fe7SDavid Forster 				}
6072df789fe7SDavid Forster 			}
607366f5d6ceSWei Wang 			rcu_read_unlock();
6074df789fe7SDavid Forster 		}
6075df789fe7SDavid Forster 		spin_unlock(&ifa->lock);
6076df789fe7SDavid Forster 	}
6077df789fe7SDavid Forster 	read_unlock_bh(&idev->lock);
6078df789fe7SDavid Forster }
6079df789fe7SDavid Forster 
6080df789fe7SDavid Forster static
6081df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
6082df789fe7SDavid Forster {
6083df789fe7SDavid Forster 	struct inet6_dev *idev;
6084df789fe7SDavid Forster 	struct net *net;
6085df789fe7SDavid Forster 
6086df789fe7SDavid Forster 	if (!rtnl_trylock())
6087df789fe7SDavid Forster 		return restart_syscall();
6088df789fe7SDavid Forster 
6089df789fe7SDavid Forster 	*valp = val;
6090df789fe7SDavid Forster 
6091df789fe7SDavid Forster 	net = (struct net *)ctl->extra2;
6092df789fe7SDavid Forster 	if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6093df789fe7SDavid Forster 		rtnl_unlock();
6094df789fe7SDavid Forster 		return 0;
6095df789fe7SDavid Forster 	}
6096df789fe7SDavid Forster 
6097df789fe7SDavid Forster 	if (valp == &net->ipv6.devconf_all->disable_policy)  {
6098df789fe7SDavid Forster 		struct net_device *dev;
6099df789fe7SDavid Forster 
6100df789fe7SDavid Forster 		for_each_netdev(net, dev) {
6101df789fe7SDavid Forster 			idev = __in6_dev_get(dev);
6102df789fe7SDavid Forster 			if (idev)
6103df789fe7SDavid Forster 				addrconf_disable_policy_idev(idev, val);
6104df789fe7SDavid Forster 		}
6105df789fe7SDavid Forster 	} else {
6106df789fe7SDavid Forster 		idev = (struct inet6_dev *)ctl->extra1;
6107df789fe7SDavid Forster 		addrconf_disable_policy_idev(idev, val);
6108df789fe7SDavid Forster 	}
6109df789fe7SDavid Forster 
6110df789fe7SDavid Forster 	rtnl_unlock();
6111df789fe7SDavid Forster 	return 0;
6112df789fe7SDavid Forster }
6113df789fe7SDavid Forster 
6114df789fe7SDavid Forster static
6115df789fe7SDavid Forster int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
6116df789fe7SDavid Forster 				   void __user *buffer, size_t *lenp,
6117df789fe7SDavid Forster 				   loff_t *ppos)
6118df789fe7SDavid Forster {
6119df789fe7SDavid Forster 	int *valp = ctl->data;
6120df789fe7SDavid Forster 	int val = *valp;
6121df789fe7SDavid Forster 	loff_t pos = *ppos;
6122df789fe7SDavid Forster 	struct ctl_table lctl;
6123df789fe7SDavid Forster 	int ret;
6124df789fe7SDavid Forster 
6125df789fe7SDavid Forster 	lctl = *ctl;
6126df789fe7SDavid Forster 	lctl.data = &val;
6127df789fe7SDavid Forster 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6128df789fe7SDavid Forster 
6129df789fe7SDavid Forster 	if (write && (*valp != val))
6130df789fe7SDavid Forster 		ret = addrconf_disable_policy(ctl, valp, val);
6131df789fe7SDavid Forster 
6132df789fe7SDavid Forster 	if (ret)
6133df789fe7SDavid Forster 		*ppos = pos;
6134df789fe7SDavid Forster 
6135df789fe7SDavid Forster 	return ret;
6136df789fe7SDavid Forster }
6137df789fe7SDavid Forster 
6138cb4a4c69SMaciej Żenczykowski static int minus_one = -1;
61392210d6b2SMaciej Żenczykowski static const int zero = 0;
6140cb9e684eSMaciej Żenczykowski static const int one = 1;
6141cb9e684eSMaciej Żenczykowski static const int two_five_five = 255;
6142cb9e684eSMaciej Żenczykowski 
6143607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = {
61441da177e4SLinus Torvalds 	{
61451da177e4SLinus Torvalds 		.procname	= "forwarding",
61461da177e4SLinus Torvalds 		.data		= &ipv6_devconf.forwarding,
61471da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61481da177e4SLinus Torvalds 		.mode		= 0644,
61496d9f239aSAlexey Dobriyan 		.proc_handler	= addrconf_sysctl_forward,
61501da177e4SLinus Torvalds 	},
61511da177e4SLinus Torvalds 	{
61521da177e4SLinus Torvalds 		.procname	= "hop_limit",
61531da177e4SLinus Torvalds 		.data		= &ipv6_devconf.hop_limit,
61541da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61551da177e4SLinus Torvalds 		.mode		= 0644,
6156cb9e684eSMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
6157cb9e684eSMaciej Żenczykowski 		.extra1		= (void *)&one,
6158cb9e684eSMaciej Żenczykowski 		.extra2		= (void *)&two_five_five,
61591da177e4SLinus Torvalds 	},
61601da177e4SLinus Torvalds 	{
61611da177e4SLinus Torvalds 		.procname	= "mtu",
61621da177e4SLinus Torvalds 		.data		= &ipv6_devconf.mtu6,
61631da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61641da177e4SLinus Torvalds 		.mode		= 0644,
616577751427SMarcelo Leitner 		.proc_handler	= addrconf_sysctl_mtu,
61661da177e4SLinus Torvalds 	},
61671da177e4SLinus Torvalds 	{
61681da177e4SLinus Torvalds 		.procname	= "accept_ra",
61691da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_ra,
61701da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61711da177e4SLinus Torvalds 		.mode		= 0644,
61726d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61731da177e4SLinus Torvalds 	},
61741da177e4SLinus Torvalds 	{
61751da177e4SLinus Torvalds 		.procname	= "accept_redirects",
61761da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_redirects,
61771da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61781da177e4SLinus Torvalds 		.mode		= 0644,
61796d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61801da177e4SLinus Torvalds 	},
61811da177e4SLinus Torvalds 	{
61821da177e4SLinus Torvalds 		.procname	= "autoconf",
61831da177e4SLinus Torvalds 		.data		= &ipv6_devconf.autoconf,
61841da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61851da177e4SLinus Torvalds 		.mode		= 0644,
61866d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61871da177e4SLinus Torvalds 	},
61881da177e4SLinus Torvalds 	{
61891da177e4SLinus Torvalds 		.procname	= "dad_transmits",
61901da177e4SLinus Torvalds 		.data		= &ipv6_devconf.dad_transmits,
61911da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61921da177e4SLinus Torvalds 		.mode		= 0644,
61936d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61941da177e4SLinus Torvalds 	},
61951da177e4SLinus Torvalds 	{
61961da177e4SLinus Torvalds 		.procname	= "router_solicitations",
61971da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicits,
61981da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61991da177e4SLinus Torvalds 		.mode		= 0644,
6200cb4a4c69SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
6201cb4a4c69SMaciej Żenczykowski 		.extra1		= &minus_one,
62021da177e4SLinus Torvalds 	},
62031da177e4SLinus Torvalds 	{
62041da177e4SLinus Torvalds 		.procname	= "router_solicitation_interval",
62051da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_interval,
62061da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
62071da177e4SLinus Torvalds 		.mode		= 0644,
62086d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
62091da177e4SLinus Torvalds 	},
62101da177e4SLinus Torvalds 	{
6211bd11f074SMaciej Żenczykowski 		.procname	= "router_solicitation_max_interval",
6212bd11f074SMaciej Żenczykowski 		.data		= &ipv6_devconf.rtr_solicit_max_interval,
6213bd11f074SMaciej Żenczykowski 		.maxlen		= sizeof(int),
6214bd11f074SMaciej Żenczykowski 		.mode		= 0644,
6215bd11f074SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_jiffies,
6216bd11f074SMaciej Żenczykowski 	},
6217bd11f074SMaciej Żenczykowski 	{
62181da177e4SLinus Torvalds 		.procname	= "router_solicitation_delay",
62191da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_delay,
62201da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
62211da177e4SLinus Torvalds 		.mode		= 0644,
62226d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
62231da177e4SLinus Torvalds 	},
62241da177e4SLinus Torvalds 	{
62251da177e4SLinus Torvalds 		.procname	= "force_mld_version",
62261da177e4SLinus Torvalds 		.data		= &ipv6_devconf.force_mld_version,
62271da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
62281da177e4SLinus Torvalds 		.mode		= 0644,
62296d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
62301da177e4SLinus Torvalds 	},
6231fc4eba58SHannes Frederic Sowa 	{
6232fc4eba58SHannes Frederic Sowa 		.procname	= "mldv1_unsolicited_report_interval",
6233fc4eba58SHannes Frederic Sowa 		.data		=
6234fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv1_unsolicited_report_interval,
6235fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6236fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
6237fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
6238fc4eba58SHannes Frederic Sowa 	},
6239fc4eba58SHannes Frederic Sowa 	{
6240fc4eba58SHannes Frederic Sowa 		.procname	= "mldv2_unsolicited_report_interval",
6241fc4eba58SHannes Frederic Sowa 		.data		=
6242fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv2_unsolicited_report_interval,
6243fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6244fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
6245fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
6246fc4eba58SHannes Frederic Sowa 	},
62471da177e4SLinus Torvalds 	{
62481da177e4SLinus Torvalds 		.procname	= "use_tempaddr",
62491da177e4SLinus Torvalds 		.data		= &ipv6_devconf.use_tempaddr,
62501da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
62511da177e4SLinus Torvalds 		.mode		= 0644,
62526d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
62531da177e4SLinus Torvalds 	},
62541da177e4SLinus Torvalds 	{
62551da177e4SLinus Torvalds 		.procname	= "temp_valid_lft",
62561da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_valid_lft,
62571da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
62581da177e4SLinus Torvalds 		.mode		= 0644,
62596d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
62601da177e4SLinus Torvalds 	},
62611da177e4SLinus Torvalds 	{
62621da177e4SLinus Torvalds 		.procname	= "temp_prefered_lft",
62631da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_prefered_lft,
62641da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
62651da177e4SLinus Torvalds 		.mode		= 0644,
62666d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
62671da177e4SLinus Torvalds 	},
62681da177e4SLinus Torvalds 	{
62691da177e4SLinus Torvalds 		.procname	= "regen_max_retry",
62701da177e4SLinus Torvalds 		.data		= &ipv6_devconf.regen_max_retry,
62711da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
62721da177e4SLinus Torvalds 		.mode		= 0644,
62736d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
62741da177e4SLinus Torvalds 	},
62751da177e4SLinus Torvalds 	{
62761da177e4SLinus Torvalds 		.procname	= "max_desync_factor",
62771da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_desync_factor,
62781da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
62791da177e4SLinus Torvalds 		.mode		= 0644,
62806d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
62811da177e4SLinus Torvalds 	},
62821da177e4SLinus Torvalds 	{
62831da177e4SLinus Torvalds 		.procname	= "max_addresses",
62841da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_addresses,
62851da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
62861da177e4SLinus Torvalds 		.mode		= 0644,
62876d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
62881da177e4SLinus Torvalds 	},
62891da177e4SLinus Torvalds 	{
629065f5c7c1SYOSHIFUJI Hideaki 		.procname	= "accept_ra_defrtr",
629165f5c7c1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_defrtr,
629265f5c7c1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
629365f5c7c1SYOSHIFUJI Hideaki 		.mode		= 0644,
62946d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
629565f5c7c1SYOSHIFUJI Hideaki 	},
629665f5c7c1SYOSHIFUJI Hideaki 	{
62978013d1d7SHangbin Liu 		.procname	= "accept_ra_min_hop_limit",
62988013d1d7SHangbin Liu 		.data		= &ipv6_devconf.accept_ra_min_hop_limit,
62998013d1d7SHangbin Liu 		.maxlen		= sizeof(int),
63008013d1d7SHangbin Liu 		.mode		= 0644,
63018013d1d7SHangbin Liu 		.proc_handler	= proc_dointvec,
63028013d1d7SHangbin Liu 	},
63038013d1d7SHangbin Liu 	{
6304c4fd30ebSYOSHIFUJI Hideaki 		.procname	= "accept_ra_pinfo",
6305c4fd30ebSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_pinfo,
6306c4fd30ebSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6307c4fd30ebSYOSHIFUJI Hideaki 		.mode		= 0644,
63086d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
6309c4fd30ebSYOSHIFUJI Hideaki 	},
6310930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
6311930d6ff2SYOSHIFUJI Hideaki 	{
6312930d6ff2SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rtr_pref",
6313930d6ff2SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rtr_pref,
6314930d6ff2SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6315930d6ff2SYOSHIFUJI Hideaki 		.mode		= 0644,
63166d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
6317930d6ff2SYOSHIFUJI Hideaki 	},
631852e16356SYOSHIFUJI Hideaki 	{
631952e16356SYOSHIFUJI Hideaki 		.procname	= "router_probe_interval",
632052e16356SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.rtr_probe_interval,
632152e16356SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
632252e16356SYOSHIFUJI Hideaki 		.mode		= 0644,
63236d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
632452e16356SYOSHIFUJI Hideaki 	},
6325fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
632609c884d4SYOSHIFUJI Hideaki 	{
6327bbea124bSJoel Scherpelz 		.procname	= "accept_ra_rt_info_min_plen",
6328bbea124bSJoel Scherpelz 		.data		= &ipv6_devconf.accept_ra_rt_info_min_plen,
6329bbea124bSJoel Scherpelz 		.maxlen		= sizeof(int),
6330bbea124bSJoel Scherpelz 		.mode		= 0644,
6331bbea124bSJoel Scherpelz 		.proc_handler	= proc_dointvec,
6332bbea124bSJoel Scherpelz 	},
6333bbea124bSJoel Scherpelz 	{
633409c884d4SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rt_info_max_plen",
633509c884d4SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
633609c884d4SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
633709c884d4SYOSHIFUJI Hideaki 		.mode		= 0644,
63386d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
633909c884d4SYOSHIFUJI Hideaki 	},
634009c884d4SYOSHIFUJI Hideaki #endif
6341930d6ff2SYOSHIFUJI Hideaki #endif
6342c4fd30ebSYOSHIFUJI Hideaki 	{
6343fbea49e1SYOSHIFUJI Hideaki 		.procname	= "proxy_ndp",
6344fbea49e1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.proxy_ndp,
6345fbea49e1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6346fbea49e1SYOSHIFUJI Hideaki 		.mode		= 0644,
6347c92d5491Sstephen hemminger 		.proc_handler	= addrconf_sysctl_proxy_ndp,
6348fbea49e1SYOSHIFUJI Hideaki 	},
6349fbea49e1SYOSHIFUJI Hideaki 	{
63500bcbc926SYOSHIFUJI Hideaki 		.procname	= "accept_source_route",
63510bcbc926SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_source_route,
63520bcbc926SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
63530bcbc926SYOSHIFUJI Hideaki 		.mode		= 0644,
63546d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
63550bcbc926SYOSHIFUJI Hideaki 	},
635695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
635795c385b4SNeil Horman 	{
635895c385b4SNeil Horman 		.procname	= "optimistic_dad",
635995c385b4SNeil Horman 		.data		= &ipv6_devconf.optimistic_dad,
636095c385b4SNeil Horman 		.maxlen		= sizeof(int),
636195c385b4SNeil Horman 		.mode		= 0644,
63626d9f239aSAlexey Dobriyan 		.proc_handler   = proc_dointvec,
636395c385b4SNeil Horman 	},
63647fd2561eSErik Kline 	{
63657fd2561eSErik Kline 		.procname	= "use_optimistic",
63667fd2561eSErik Kline 		.data		= &ipv6_devconf.use_optimistic,
63677fd2561eSErik Kline 		.maxlen		= sizeof(int),
63687fd2561eSErik Kline 		.mode		= 0644,
63697fd2561eSErik Kline 		.proc_handler	= proc_dointvec,
63707fd2561eSErik Kline 	},
637195c385b4SNeil Horman #endif
63727bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
63737bc570c8SYOSHIFUJI Hideaki 	{
63747bc570c8SYOSHIFUJI Hideaki 		.procname	= "mc_forwarding",
63757bc570c8SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.mc_forwarding,
63767bc570c8SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6377a4e6db07SDavid S. Miller 		.mode		= 0444,
63786d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
63797bc570c8SYOSHIFUJI Hideaki 	},
63807bc570c8SYOSHIFUJI Hideaki #endif
63810bcbc926SYOSHIFUJI Hideaki 	{
6382778d80beSYOSHIFUJI Hideaki 		.procname	= "disable_ipv6",
6383778d80beSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.disable_ipv6,
6384778d80beSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6385778d80beSYOSHIFUJI Hideaki 		.mode		= 0644,
638656d417b1SBrian Haley 		.proc_handler	= addrconf_sysctl_disable,
6387778d80beSYOSHIFUJI Hideaki 	},
6388778d80beSYOSHIFUJI Hideaki 	{
63891b34be74SYOSHIFUJI Hideaki 		.procname	= "accept_dad",
63901b34be74SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_dad,
63911b34be74SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
63921b34be74SYOSHIFUJI Hideaki 		.mode		= 0644,
63936d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
63941b34be74SYOSHIFUJI Hideaki 	},
63951b34be74SYOSHIFUJI Hideaki 	{
6396f7734fdfSOctavian Purdila 		.procname	= "force_tllao",
6397f7734fdfSOctavian Purdila 		.data		= &ipv6_devconf.force_tllao,
6398f7734fdfSOctavian Purdila 		.maxlen		= sizeof(int),
6399f7734fdfSOctavian Purdila 		.mode		= 0644,
6400f7734fdfSOctavian Purdila 		.proc_handler	= proc_dointvec
6401f7734fdfSOctavian Purdila 	},
6402f7734fdfSOctavian Purdila 	{
64035cb04436SHannes Frederic Sowa 		.procname	= "ndisc_notify",
64045cb04436SHannes Frederic Sowa 		.data		= &ipv6_devconf.ndisc_notify,
64055cb04436SHannes Frederic Sowa 		.maxlen		= sizeof(int),
64065cb04436SHannes Frederic Sowa 		.mode		= 0644,
64075cb04436SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
64085cb04436SHannes Frederic Sowa 	},
64095cb04436SHannes Frederic Sowa 	{
6410b800c3b9SHannes Frederic Sowa 		.procname	= "suppress_frag_ndisc",
6411b800c3b9SHannes Frederic Sowa 		.data		= &ipv6_devconf.suppress_frag_ndisc,
6412b800c3b9SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6413b800c3b9SHannes Frederic Sowa 		.mode		= 0644,
6414b800c3b9SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
6415b800c3b9SHannes Frederic Sowa 	},
6416b800c3b9SHannes Frederic Sowa 	{
6417d9333196SBen Greear 		.procname	= "accept_ra_from_local",
6418d9333196SBen Greear 		.data		= &ipv6_devconf.accept_ra_from_local,
6419d9333196SBen Greear 		.maxlen		= sizeof(int),
6420d9333196SBen Greear 		.mode		= 0644,
6421d9333196SBen Greear 		.proc_handler	= proc_dointvec,
6422d9333196SBen Greear 	},
6423d9333196SBen Greear 	{
6424c2943f14SHarout Hedeshian 		.procname	= "accept_ra_mtu",
6425c2943f14SHarout Hedeshian 		.data		= &ipv6_devconf.accept_ra_mtu,
6426c2943f14SHarout Hedeshian 		.maxlen		= sizeof(int),
6427c2943f14SHarout Hedeshian 		.mode		= 0644,
6428c2943f14SHarout Hedeshian 		.proc_handler	= proc_dointvec,
6429c2943f14SHarout Hedeshian 	},
6430c2943f14SHarout Hedeshian 	{
64313d1bec99SHannes Frederic Sowa 		.procname	= "stable_secret",
64323d1bec99SHannes Frederic Sowa 		.data		= &ipv6_devconf.stable_secret,
64333d1bec99SHannes Frederic Sowa 		.maxlen		= IPV6_MAX_STRLEN,
64343d1bec99SHannes Frederic Sowa 		.mode		= 0600,
64353d1bec99SHannes Frederic Sowa 		.proc_handler	= addrconf_sysctl_stable_secret,
64363d1bec99SHannes Frederic Sowa 	},
64373d1bec99SHannes Frederic Sowa 	{
64383985e8a3SErik Kline 		.procname	= "use_oif_addrs_only",
64393985e8a3SErik Kline 		.data		= &ipv6_devconf.use_oif_addrs_only,
64403985e8a3SErik Kline 		.maxlen		= sizeof(int),
64413985e8a3SErik Kline 		.mode		= 0644,
64423985e8a3SErik Kline 		.proc_handler	= proc_dointvec,
644335103d11SAndy Gospodarek 	},
644435103d11SAndy Gospodarek 	{
644535103d11SAndy Gospodarek 		.procname	= "ignore_routes_with_linkdown",
644635103d11SAndy Gospodarek 		.data		= &ipv6_devconf.ignore_routes_with_linkdown,
644735103d11SAndy Gospodarek 		.maxlen		= sizeof(int),
644835103d11SAndy Gospodarek 		.mode		= 0644,
644935103d11SAndy Gospodarek 		.proc_handler	= addrconf_sysctl_ignore_routes_with_linkdown,
64503985e8a3SErik Kline 	},
64513985e8a3SErik Kline 	{
6452abbc3043SJohannes Berg 		.procname	= "drop_unicast_in_l2_multicast",
6453abbc3043SJohannes Berg 		.data		= &ipv6_devconf.drop_unicast_in_l2_multicast,
6454abbc3043SJohannes Berg 		.maxlen		= sizeof(int),
6455abbc3043SJohannes Berg 		.mode		= 0644,
6456abbc3043SJohannes Berg 		.proc_handler	= proc_dointvec,
6457abbc3043SJohannes Berg 	},
6458abbc3043SJohannes Berg 	{
64597a02bf89SJohannes Berg 		.procname	= "drop_unsolicited_na",
64607a02bf89SJohannes Berg 		.data		= &ipv6_devconf.drop_unsolicited_na,
64617a02bf89SJohannes Berg 		.maxlen		= sizeof(int),
64627a02bf89SJohannes Berg 		.mode		= 0644,
64637a02bf89SJohannes Berg 		.proc_handler	= proc_dointvec,
64647a02bf89SJohannes Berg 	},
64657a02bf89SJohannes Berg 	{
6466f1705ec1SDavid Ahern 		.procname	= "keep_addr_on_down",
6467f1705ec1SDavid Ahern 		.data		= &ipv6_devconf.keep_addr_on_down,
6468f1705ec1SDavid Ahern 		.maxlen		= sizeof(int),
6469f1705ec1SDavid Ahern 		.mode		= 0644,
6470f1705ec1SDavid Ahern 		.proc_handler	= proc_dointvec,
6471f1705ec1SDavid Ahern 
6472f1705ec1SDavid Ahern 	},
6473f1705ec1SDavid Ahern 	{
64741ababebaSDavid Lebrun 		.procname	= "seg6_enabled",
64751ababebaSDavid Lebrun 		.data		= &ipv6_devconf.seg6_enabled,
64761ababebaSDavid Lebrun 		.maxlen		= sizeof(int),
64771ababebaSDavid Lebrun 		.mode		= 0644,
64781ababebaSDavid Lebrun 		.proc_handler	= proc_dointvec,
64791ababebaSDavid Lebrun 	},
6480bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
6481bf355b8dSDavid Lebrun 	{
6482bf355b8dSDavid Lebrun 		.procname	= "seg6_require_hmac",
6483bf355b8dSDavid Lebrun 		.data		= &ipv6_devconf.seg6_require_hmac,
6484bf355b8dSDavid Lebrun 		.maxlen		= sizeof(int),
6485bf355b8dSDavid Lebrun 		.mode		= 0644,
6486bf355b8dSDavid Lebrun 		.proc_handler	= proc_dointvec,
6487bf355b8dSDavid Lebrun 	},
6488bf355b8dSDavid Lebrun #endif
64891ababebaSDavid Lebrun 	{
6490adc176c5SErik Nordmark 		.procname       = "enhanced_dad",
6491adc176c5SErik Nordmark 		.data           = &ipv6_devconf.enhanced_dad,
6492adc176c5SErik Nordmark 		.maxlen         = sizeof(int),
6493adc176c5SErik Nordmark 		.mode           = 0644,
6494adc176c5SErik Nordmark 		.proc_handler   = proc_dointvec,
6495adc176c5SErik Nordmark 	},
6496adc176c5SErik Nordmark 	{
6497d35a00b8SFelix Jia 		.procname		= "addr_gen_mode",
6498d35a00b8SFelix Jia 		.data			= &ipv6_devconf.addr_gen_mode,
6499d35a00b8SFelix Jia 		.maxlen			= sizeof(int),
6500d35a00b8SFelix Jia 		.mode			= 0644,
6501d35a00b8SFelix Jia 		.proc_handler	= addrconf_sysctl_addr_gen_mode,
6502d35a00b8SFelix Jia 	},
6503d35a00b8SFelix Jia 	{
6504df789fe7SDavid Forster 		.procname       = "disable_policy",
6505df789fe7SDavid Forster 		.data           = &ipv6_devconf.disable_policy,
6506df789fe7SDavid Forster 		.maxlen         = sizeof(int),
6507df789fe7SDavid Forster 		.mode           = 0644,
6508df789fe7SDavid Forster 		.proc_handler   = addrconf_sysctl_disable_policy,
6509df789fe7SDavid Forster 	},
6510df789fe7SDavid Forster 	{
65112210d6b2SMaciej Żenczykowski 		.procname	= "ndisc_tclass",
65122210d6b2SMaciej Żenczykowski 		.data		= &ipv6_devconf.ndisc_tclass,
65132210d6b2SMaciej Żenczykowski 		.maxlen		= sizeof(int),
65142210d6b2SMaciej Żenczykowski 		.mode		= 0644,
65152210d6b2SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
65162210d6b2SMaciej Żenczykowski 		.extra1		= (void *)&zero,
65172210d6b2SMaciej Żenczykowski 		.extra2		= (void *)&two_five_five,
65182210d6b2SMaciej Żenczykowski 	},
65192210d6b2SMaciej Żenczykowski 	{
6520f8572d8fSEric W. Biederman 		/* sentinel */
65211da177e4SLinus Torvalds 	}
65221da177e4SLinus Torvalds };
65231da177e4SLinus Torvalds 
6524bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6525f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
65261da177e4SLinus Torvalds {
652729c994e3SNicolas Dichtel 	int i, ifindex;
6528607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
65296105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
65301dab6222SPavel Emelyanov 
6531607ea7cdSKonstantin Khlebnikov 	table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6532607ea7cdSKonstantin Khlebnikov 	if (!table)
6533f68635e6SPavel Emelyanov 		goto out;
6534f68635e6SPavel Emelyanov 
6535607ea7cdSKonstantin Khlebnikov 	for (i = 0; table[i].data; i++) {
6536607ea7cdSKonstantin Khlebnikov 		table[i].data += (char *)p - (char *)&ipv6_devconf;
6537cb9e684eSMaciej Żenczykowski 		/* If one of these is already set, then it is not safe to
6538cb9e684eSMaciej Żenczykowski 		 * overwrite either of them: this makes proc_dointvec_minmax
6539cb9e684eSMaciej Żenczykowski 		 * usable.
6540cb9e684eSMaciej Żenczykowski 		 */
6541cb9e684eSMaciej Żenczykowski 		if (!table[i].extra1 && !table[i].extra2) {
6542607ea7cdSKonstantin Khlebnikov 			table[i].extra1 = idev; /* embedded; no ref */
6543607ea7cdSKonstantin Khlebnikov 			table[i].extra2 = net;
65441da177e4SLinus Torvalds 		}
6545cb9e684eSMaciej Żenczykowski 	}
65461da177e4SLinus Torvalds 
65476105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
65481da177e4SLinus Torvalds 
6549607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = register_net_sysctl(net, path, table);
6550607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
65516105e293SEric W. Biederman 		goto free;
6552f68635e6SPavel Emelyanov 
655329c994e3SNicolas Dichtel 	if (!strcmp(dev_name, "all"))
655429c994e3SNicolas Dichtel 		ifindex = NETCONFA_IFINDEX_ALL;
655529c994e3SNicolas Dichtel 	else if (!strcmp(dev_name, "default"))
655629c994e3SNicolas Dichtel 		ifindex = NETCONFA_IFINDEX_DEFAULT;
655729c994e3SNicolas Dichtel 	else
655829c994e3SNicolas Dichtel 		ifindex = idev->dev->ifindex;
655985b3daadSDavid Ahern 	inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
656085b3daadSDavid Ahern 				     ifindex, p);
656195897312SPavel Emelyanov 	return 0;
65621da177e4SLinus Torvalds 
65631da177e4SLinus Torvalds free:
6564607ea7cdSKonstantin Khlebnikov 	kfree(table);
6565f68635e6SPavel Emelyanov out:
656695897312SPavel Emelyanov 	return -ENOBUFS;
65671da177e4SLinus Torvalds }
65681da177e4SLinus Torvalds 
656923452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net,
657023452170SDavid Ahern 					 struct ipv6_devconf *p, int ifindex)
6571f52295a9SPavel Emelyanov {
6572607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
6573f52295a9SPavel Emelyanov 
6574607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
6575408c4768SPavel Emelyanov 		return;
6576408c4768SPavel Emelyanov 
6577607ea7cdSKonstantin Khlebnikov 	table = p->sysctl_header->ctl_table_arg;
6578607ea7cdSKonstantin Khlebnikov 	unregister_net_sysctl_table(p->sysctl_header);
6579607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = NULL;
6580607ea7cdSKonstantin Khlebnikov 	kfree(table);
658123452170SDavid Ahern 
658223452170SDavid Ahern 	inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
65831da177e4SLinus Torvalds }
6584408c4768SPavel Emelyanov 
6585a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev)
6586408c4768SPavel Emelyanov {
6587a317a2f1SWANG Cong 	int err;
6588a317a2f1SWANG Cong 
6589a317a2f1SWANG Cong 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
6590a317a2f1SWANG Cong 		return -EINVAL;
6591a317a2f1SWANG Cong 
6592a317a2f1SWANG Cong 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6593f8572d8fSEric W. Biederman 				    &ndisc_ifinfo_sysctl_change);
6594a317a2f1SWANG Cong 	if (err)
6595a317a2f1SWANG Cong 		return err;
6596a317a2f1SWANG Cong 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6597f8572d8fSEric W. Biederman 					 idev, &idev->cnf);
6598a317a2f1SWANG Cong 	if (err)
6599a317a2f1SWANG Cong 		neigh_sysctl_unregister(idev->nd_parms);
6600a317a2f1SWANG Cong 
6601a317a2f1SWANG Cong 	return err;
6602408c4768SPavel Emelyanov }
6603408c4768SPavel Emelyanov 
6604408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6605408c4768SPavel Emelyanov {
660623452170SDavid Ahern 	__addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
660723452170SDavid Ahern 				     idev->dev->ifindex);
6608408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
66091da177e4SLinus Torvalds }
66101da177e4SLinus Torvalds 
66111da177e4SLinus Torvalds 
66121da177e4SLinus Torvalds #endif
66131da177e4SLinus Torvalds 
66142c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
6615e0da5a48SPavel Emelyanov {
6616a79ca223SHong Zhiguo 	int err = -ENOMEM;
6617e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
6618e0da5a48SPavel Emelyanov 
6619a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
662063159f29SIan Morris 	if (!all)
6621e0da5a48SPavel Emelyanov 		goto err_alloc_all;
6622e0da5a48SPavel Emelyanov 
6623a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
662463159f29SIan Morris 	if (!dflt)
6625e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
6626a79ca223SHong Zhiguo 
662756d417b1SBrian Haley 	/* these will be inherited by all namespaces */
662856d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
662956d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6630e0da5a48SPavel Emelyanov 
66313d1bec99SHannes Frederic Sowa 	dflt->stable_secret.initialized = false;
66323d1bec99SHannes Frederic Sowa 	all->stable_secret.initialized = false;
66333d1bec99SHannes Frederic Sowa 
6634e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
6635e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
6636e0da5a48SPavel Emelyanov 
6637e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
6638f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
6639e0da5a48SPavel Emelyanov 	if (err < 0)
6640e0da5a48SPavel Emelyanov 		goto err_reg_all;
6641e0da5a48SPavel Emelyanov 
6642f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6643e0da5a48SPavel Emelyanov 	if (err < 0)
6644e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
6645e0da5a48SPavel Emelyanov #endif
6646e0da5a48SPavel Emelyanov 	return 0;
6647e0da5a48SPavel Emelyanov 
6648e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
6649e0da5a48SPavel Emelyanov err_reg_dflt:
665023452170SDavid Ahern 	__addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
6651e0da5a48SPavel Emelyanov err_reg_all:
6652e0da5a48SPavel Emelyanov 	kfree(dflt);
6653e0da5a48SPavel Emelyanov #endif
6654e0da5a48SPavel Emelyanov err_alloc_dflt:
6655e0da5a48SPavel Emelyanov 	kfree(all);
6656e0da5a48SPavel Emelyanov err_alloc_all:
6657e0da5a48SPavel Emelyanov 	return err;
6658e0da5a48SPavel Emelyanov }
6659e0da5a48SPavel Emelyanov 
66602c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
6661e0da5a48SPavel Emelyanov {
6662e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
666323452170SDavid Ahern 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
666423452170SDavid Ahern 				     NETCONFA_IFINDEX_DEFAULT);
666523452170SDavid Ahern 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
666623452170SDavid Ahern 				     NETCONFA_IFINDEX_ALL);
6667e0da5a48SPavel Emelyanov #endif
6668e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_dflt);
6669e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_all);
6670e0da5a48SPavel Emelyanov }
6671e0da5a48SPavel Emelyanov 
6672e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
6673e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
6674e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
6675e0da5a48SPavel Emelyanov };
6676e0da5a48SPavel Emelyanov 
6677207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = {
6678b382b191SThomas Graf 	.family		  = AF_INET6,
6679b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
6680b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
668111b1f828SDaniel Borkmann 	.validate_link_af = inet6_validate_link_af,
6682f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
6683b382b191SThomas Graf };
6684b382b191SThomas Graf 
66851da177e4SLinus Torvalds /*
66861da177e4SLinus Torvalds  *	Init / cleanup code
66871da177e4SLinus Torvalds  */
66881da177e4SLinus Torvalds 
66891da177e4SLinus Torvalds int __init addrconf_init(void)
66901da177e4SLinus Torvalds {
6691a317a2f1SWANG Cong 	struct inet6_dev *idev;
6692c2e21293Sstephen hemminger 	int i, err;
66932a8cc6c8SYOSHIFUJI Hideaki 
6694e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
6695e21e8467SStephen Hemminger 	if (err < 0) {
6696f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
6697f3213831SJoe Perches 			__func__, err);
66982cc6d2bfSNeil Horman 		goto out;
66992a8cc6c8SYOSHIFUJI Hideaki 	}
67001da177e4SLinus Torvalds 
67012cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
67022cc6d2bfSNeil Horman 	if (err < 0)
67032cc6d2bfSNeil Horman 		goto out_addrlabel;
6704e0da5a48SPavel Emelyanov 
6705c15b1ccaSHannes Frederic Sowa 	addrconf_wq = create_workqueue("ipv6_addrconf");
6706c15b1ccaSHannes Frederic Sowa 	if (!addrconf_wq) {
6707c15b1ccaSHannes Frederic Sowa 		err = -ENOMEM;
6708c15b1ccaSHannes Frederic Sowa 		goto out_nowq;
6709c15b1ccaSHannes Frederic Sowa 	}
6710c15b1ccaSHannes Frederic Sowa 
67111da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
67121da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
67131da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
67141da177e4SLinus Torvalds 	 * to other devices which are up.
67151da177e4SLinus Torvalds 	 *
67161da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
67171da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
67181da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
67191da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
67201da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
67211da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
67221da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
67231da177e4SLinus Torvalds 	 *
67241da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
67251da177e4SLinus Torvalds 	 * loopback_dev first by hand.
67261da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
67271da177e4SLinus Torvalds 	 * device and it being up should be removed.
67281da177e4SLinus Torvalds 	 */
67291da177e4SLinus Torvalds 	rtnl_lock();
6730a317a2f1SWANG Cong 	idev = ipv6_add_dev(init_net.loopback_dev);
67311da177e4SLinus Torvalds 	rtnl_unlock();
6732a317a2f1SWANG Cong 	if (IS_ERR(idev)) {
6733a317a2f1SWANG Cong 		err = PTR_ERR(idev);
6734e0da5a48SPavel Emelyanov 		goto errlo;
6735a317a2f1SWANG Cong 	}
67361da177e4SLinus Torvalds 
67372f460933SWANG Cong 	ip6_route_init_special_entries();
67382f460933SWANG Cong 
6739c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
6740c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6741c2e21293Sstephen hemminger 
67421da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
67431da177e4SLinus Torvalds 
6744c15b1ccaSHannes Frederic Sowa 	addrconf_verify();
6745c127ea2cSThomas Graf 
67463678a9d8Sstephen hemminger 	rtnl_af_register(&inet6_ops);
6747b382b191SThomas Graf 
674816feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
674916feebcfSFlorian Westphal 				   NULL, inet6_dump_ifinfo, 0);
6750c127ea2cSThomas Graf 	if (err < 0)
6751c127ea2cSThomas Graf 		goto errout;
6752c127ea2cSThomas Graf 
675316feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
675416feebcfSFlorian Westphal 				   inet6_rtm_newaddr, NULL, 0);
675516feebcfSFlorian Westphal 	if (err < 0)
675616feebcfSFlorian Westphal 		goto errout;
675716feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
675816feebcfSFlorian Westphal 				   inet6_rtm_deladdr, NULL, 0);
675916feebcfSFlorian Westphal 	if (err < 0)
676016feebcfSFlorian Westphal 		goto errout;
676116feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
676216feebcfSFlorian Westphal 				   inet6_rtm_getaddr, inet6_dump_ifaddr,
676316feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
676416feebcfSFlorian Westphal 	if (err < 0)
676516feebcfSFlorian Westphal 		goto errout;
676616feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
676716feebcfSFlorian Westphal 				   NULL, inet6_dump_ifmcaddr, 0);
676816feebcfSFlorian Westphal 	if (err < 0)
676916feebcfSFlorian Westphal 		goto errout;
677016feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
677116feebcfSFlorian Westphal 				   NULL, inet6_dump_ifacaddr, 0);
677216feebcfSFlorian Westphal 	if (err < 0)
677316feebcfSFlorian Westphal 		goto errout;
677416feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
677516feebcfSFlorian Westphal 				   inet6_netconf_get_devconf,
677616feebcfSFlorian Westphal 				   inet6_netconf_dump_devconf,
677716feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
677816feebcfSFlorian Westphal 	if (err < 0)
677916feebcfSFlorian Westphal 		goto errout;
6780a3fde2adSFlorian Westphal 	err = ipv6_addr_label_rtnl_register();
6781a3fde2adSFlorian Westphal 	if (err < 0)
6782a3fde2adSFlorian Westphal 		goto errout;
67832a8cc6c8SYOSHIFUJI Hideaki 
67841da177e4SLinus Torvalds 	return 0;
6785c127ea2cSThomas Graf errout:
678616feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
6787b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
6788c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
6789e0da5a48SPavel Emelyanov errlo:
6790c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
6791c15b1ccaSHannes Frederic Sowa out_nowq:
6792e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
67932cc6d2bfSNeil Horman out_addrlabel:
67942cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
67952cc6d2bfSNeil Horman out:
6796c127ea2cSThomas Graf 	return err;
67971da177e4SLinus Torvalds }
67981da177e4SLinus Torvalds 
679909f7709fSDaniel Lezcano void addrconf_cleanup(void)
68001da177e4SLinus Torvalds {
6801176c39afSDaniel Lezcano 	struct net_device *dev;
68021da177e4SLinus Torvalds 	int i;
68031da177e4SLinus Torvalds 
68041da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
6805e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
68062cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
68071da177e4SLinus Torvalds 
68085c45121dSFlorian Westphal 	rtnl_af_unregister(&inet6_ops);
68091da177e4SLinus Torvalds 
68105c45121dSFlorian Westphal 	rtnl_lock();
6811b382b191SThomas Graf 
6812176c39afSDaniel Lezcano 	/* clean dev list */
6813176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
6814176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
6815176c39afSDaniel Lezcano 			continue;
6816176c39afSDaniel Lezcano 		addrconf_ifdown(dev, 1);
6817176c39afSDaniel Lezcano 	}
6818176c39afSDaniel Lezcano 	addrconf_ifdown(init_net.loopback_dev, 2);
6819176c39afSDaniel Lezcano 
68201da177e4SLinus Torvalds 	/*
68211da177e4SLinus Torvalds 	 *	Check hash table.
68221da177e4SLinus Torvalds 	 */
68235c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
6824c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
6825c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
68265c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
6827c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
68281da177e4SLinus Torvalds 	rtnl_unlock();
6829c15b1ccaSHannes Frederic Sowa 
6830c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
68311da177e4SLinus Torvalds }
6832