xref: /openbmc/linux/net/ipv6/addrconf.c (revision 21a216a8fc630161e69eaf02cbebdd1816ff1a13)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	IPv6 Address [auto]configuration
41da177e4SLinus Torvalds  *	Linux INET6 implementation
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	Authors:
71da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
81da177e4SLinus Torvalds  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /*
121da177e4SLinus Torvalds  *	Changes:
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *	Janos Farkas			:	delete timer on ifdown
151da177e4SLinus Torvalds  *	<chexum@bankinf.banki.hu>
161da177e4SLinus Torvalds  *	Andi Kleen			:	kill double kfree on module
171da177e4SLinus Torvalds  *						unload.
181da177e4SLinus Torvalds  *	Maciej W. Rozycki		:	FDDI support
191da177e4SLinus Torvalds  *	sekiya@USAGI			:	Don't send too many RS
201da177e4SLinus Torvalds  *						packets.
211da177e4SLinus Torvalds  *	yoshfuji@USAGI			:       Fixed interval between DAD
221da177e4SLinus Torvalds  *						packets.
231da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	improved accuracy of
241da177e4SLinus Torvalds  *						address validation timer.
251da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	Privacy Extensions (RFC3041)
261da177e4SLinus Torvalds  *						support.
271da177e4SLinus Torvalds  *	Yuji SEKIYA @USAGI		:	Don't assign a same IPv6
281da177e4SLinus Torvalds  *						address on a same interface.
291da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	ARCnet support
301da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	convert /proc/net/if_inet6 to
311da177e4SLinus Torvalds  *						seq_file.
32b1cacb68SYOSHIFUJI Hideaki  *	YOSHIFUJI Hideaki @USAGI	:	improved source address
33b1cacb68SYOSHIFUJI Hideaki  *						selection; consider scope,
34b1cacb68SYOSHIFUJI Hideaki  *						status etc.
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
37f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
38f3213831SJoe Perches 
391da177e4SLinus Torvalds #include <linux/errno.h>
401da177e4SLinus Torvalds #include <linux/types.h>
41a0bffffcSIlpo Järvinen #include <linux/kernel.h>
42174cd4b1SIngo Molnar #include <linux/sched/signal.h>
431da177e4SLinus Torvalds #include <linux/socket.h>
441da177e4SLinus Torvalds #include <linux/sockios.h>
451da177e4SLinus Torvalds #include <linux/net.h>
463d1bec99SHannes Frederic Sowa #include <linux/inet.h>
471da177e4SLinus Torvalds #include <linux/in6.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
491823730fSThomas Graf #include <linux/if_addr.h>
501da177e4SLinus Torvalds #include <linux/if_arp.h>
511da177e4SLinus Torvalds #include <linux/if_arcnet.h>
521da177e4SLinus Torvalds #include <linux/if_infiniband.h>
531da177e4SLinus Torvalds #include <linux/route.h>
541da177e4SLinus Torvalds #include <linux/inetdevice.h>
551da177e4SLinus Torvalds #include <linux/init.h>
565a0e3ad6STejun Heo #include <linux/slab.h>
571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
581da177e4SLinus Torvalds #include <linux/sysctl.h>
591da177e4SLinus Torvalds #endif
604fc268d2SRandy Dunlap #include <linux/capability.h>
611da177e4SLinus Torvalds #include <linux/delay.h>
621da177e4SLinus Torvalds #include <linux/notifier.h>
63543537bdSPaulo Marques #include <linux/string.h>
64ddbe5032SEric Dumazet #include <linux/hash.h>
651da177e4SLinus Torvalds 
66457c4cbcSEric W. Biederman #include <net/net_namespace.h>
671da177e4SLinus Torvalds #include <net/sock.h>
681da177e4SLinus Torvalds #include <net/snmp.h>
691da177e4SLinus Torvalds 
705241c2d7SAlexander Aring #include <net/6lowpan.h>
71cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h>
721da177e4SLinus Torvalds #include <net/ipv6.h>
731da177e4SLinus Torvalds #include <net/protocol.h>
741da177e4SLinus Torvalds #include <net/ndisc.h>
751da177e4SLinus Torvalds #include <net/ip6_route.h>
761da177e4SLinus Torvalds #include <net/addrconf.h>
771da177e4SLinus Torvalds #include <net/tcp.h>
781da177e4SLinus Torvalds #include <net/ip.h>
795d620266SThomas Graf #include <net/netlink.h>
80f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
81ca254490SDavid Ahern #include <net/l3mdev.h>
821da177e4SLinus Torvalds #include <linux/if_tunnel.h>
831da177e4SLinus Torvalds #include <linux/rtnetlink.h>
84f3a1bfb1SNicolas Dichtel #include <linux/netconf.h>
851da177e4SLinus Torvalds #include <linux/random.h>
86e21e8467SStephen Hemminger #include <linux/uaccess.h>
877f7d9a6bSHerbert Xu #include <asm/unaligned.h>
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds #include <linux/proc_fs.h>
901da177e4SLinus Torvalds #include <linux/seq_file.h>
91bc3b2d7fSPaul Gortmaker #include <linux/export.h>
929ee11f0fSJustin Iurman #include <linux/ioam6.h>
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
9518a31e1eSThomas Graf 
963d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \
973d1bec99SHannes Frederic Sowa 	sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
983d1bec99SHannes Frederic Sowa 
9918a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp)
10018a31e1eSThomas Graf {
10118a31e1eSThomas Graf 	return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
10218a31e1eSThomas Graf }
1031da177e4SLinus Torvalds 
104bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_init(s32 irt)
105bd11f074SMaciej Żenczykowski {
106bd11f074SMaciej Żenczykowski 	/* multiply 'initial retransmission time' by 0.9 .. 1.1 */
107bd11f074SMaciej Żenczykowski 	u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
108bd11f074SMaciej Żenczykowski 	do_div(tmp, 1000000);
109bd11f074SMaciej Żenczykowski 	return (s32)tmp;
110bd11f074SMaciej Żenczykowski }
111bd11f074SMaciej Żenczykowski 
112bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
113bd11f074SMaciej Żenczykowski {
114bd11f074SMaciej Żenczykowski 	/* multiply 'retransmission timeout' by 1.9 .. 2.1 */
115bd11f074SMaciej Żenczykowski 	u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
116bd11f074SMaciej Żenczykowski 	do_div(tmp, 1000000);
117bd11f074SMaciej Żenczykowski 	if ((s32)tmp > mrt) {
118bd11f074SMaciej Żenczykowski 		/* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
119bd11f074SMaciej Żenczykowski 		tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
120bd11f074SMaciej Żenczykowski 		do_div(tmp, 1000000);
121bd11f074SMaciej Żenczykowski 	}
122bd11f074SMaciej Żenczykowski 	return (s32)tmp;
123bd11f074SMaciej Żenczykowski }
124bd11f074SMaciej Żenczykowski 
1251da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
126a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev);
127408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
128408c4768SPavel Emelyanov #else
129a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev)
130408c4768SPavel Emelyanov {
131a317a2f1SWANG Cong 	return 0;
132408c4768SPavel Emelyanov }
133408c4768SPavel Emelyanov 
134408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
135408c4768SPavel Emelyanov {
136408c4768SPavel Emelyanov }
1371da177e4SLinus Torvalds #endif
1381da177e4SLinus Torvalds 
139969c5464SFernando Gont static void ipv6_gen_rnd_iid(struct in6_addr *addr);
1401da177e4SLinus Torvalds 
1411b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
142d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev);
143622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr,
144622c81d5SHannes Frederic Sowa 					u8 dad_count,
145622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev);
1461da177e4SLinus Torvalds 
14727c565aeSEric Dumazet #define IN6_ADDR_HSIZE_SHIFT	8
14827c565aeSEric Dumazet #define IN6_ADDR_HSIZE		(1 << IN6_ADDR_HSIZE_SHIFT)
1491da177e4SLinus Torvalds /*
1501da177e4SLinus Torvalds  *	Configured unicast address hash table
1511da177e4SLinus Torvalds  */
152c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
1535c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock);
1541da177e4SLinus Torvalds 
155c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void);
156c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void);
157c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *);
1581da177e4SLinus Torvalds 
159c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq;
160c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1631da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1641da177e4SLinus Torvalds 
16593d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev,
16675c78500SMoni Shoua 				 unsigned long event);
167ae79dbf6SFlorent Fourcot static int addrconf_ifdown(struct net_device *dev, bool unregister);
1681da177e4SLinus Torvalds 
1698d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
17021caa662SRomain Kuntz 						  int plen,
17121caa662SRomain Kuntz 						  const struct net_device *dev,
1722b2450caSDavid Ahern 						  u32 flags, u32 noflags,
1732b2450caSDavid Ahern 						  bool no_gw);
17421caa662SRomain Kuntz 
175cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp);
176c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w);
177c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
178c76fe2d9SDavid Ahern 				   bool send_na);
179896585d4SHangbin Liu static void addrconf_dad_run(struct inet6_dev *idev, bool restart);
180e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t);
1811da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1821da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1851da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1861da177e4SLinus Torvalds 
187888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1881da177e4SLinus Torvalds 	.forwarding		= 0,
1891da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1901da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1911da177e4SLinus Torvalds 	.accept_ra		= 1,
1921da177e4SLinus Torvalds 	.accept_redirects	= 1,
1931da177e4SLinus Torvalds 	.autoconf		= 1,
1941da177e4SLinus Torvalds 	.force_mld_version	= 0,
195fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
196fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
1971da177e4SLinus Torvalds 	.dad_transmits		= 1,
1981da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1991da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
200bd11f074SMaciej Żenczykowski 	.rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
2011da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2021da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2031da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2041da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2051da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2061da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2071da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
20865f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
2096b2e04bcSPraveen Chaudhary 	.ra_defrtr_metric	= IP6_RT_PRIO_USER,
210d9333196SBen Greear 	.accept_ra_from_local	= 0,
2118013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
212c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
213930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
214930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
21552e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
21609c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
217bbea124bSJoel Scherpelz 	.accept_ra_rt_info_min_plen = 0,
21809c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
21909c884d4SYOSHIFUJI Hideaki #endif
220930d6ff2SYOSHIFUJI Hideaki #endif
221fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2220bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
223778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
22409400953SNicolas Dichtel 	.accept_dad		= 0,
225b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
226c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2273d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2283d1bec99SHannes Frederic Sowa 		.initialized = false,
2293985e8a3SErik Kline 	},
2303985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
23135103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
232f1705ec1SDavid Ahern 	.keep_addr_on_down	= 0,
2331ababebaSDavid Lebrun 	.seg6_enabled		= 0,
234bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
235bf355b8dSDavid Lebrun 	.seg6_require_hmac	= 0,
236bf355b8dSDavid Lebrun #endif
237adc176c5SErik Nordmark 	.enhanced_dad           = 1,
238d35a00b8SFelix Jia 	.addr_gen_mode		= IN6_ADDR_GEN_MODE_EUI64,
239df789fe7SDavid Forster 	.disable_policy		= 0,
2408610c7c6SAlexander Aring 	.rpl_seg_enabled	= 0,
2419ee11f0fSJustin Iurman 	.ioam6_enabled		= 0,
2429ee11f0fSJustin Iurman 	.ioam6_id               = IOAM6_DEFAULT_IF_ID,
2439ee11f0fSJustin Iurman 	.ioam6_id_wide		= IOAM6_DEFAULT_IF_ID_WIDE,
24418ac597aSJames Prestwood 	.ndisc_evict_nocarrier	= 1,
2451da177e4SLinus Torvalds };
2461da177e4SLinus Torvalds 
247ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2481da177e4SLinus Torvalds 	.forwarding		= 0,
2491da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2501da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2511da177e4SLinus Torvalds 	.accept_ra		= 1,
2521da177e4SLinus Torvalds 	.accept_redirects	= 1,
2531da177e4SLinus Torvalds 	.autoconf		= 1,
254fc4eba58SHannes Frederic Sowa 	.force_mld_version	= 0,
255fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
256fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2571da177e4SLinus Torvalds 	.dad_transmits		= 1,
2581da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2591da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
260bd11f074SMaciej Żenczykowski 	.rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
2611da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2621da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2631da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2641da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2651da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2661da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2671da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
26865f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
2696b2e04bcSPraveen Chaudhary 	.ra_defrtr_metric	= IP6_RT_PRIO_USER,
270d9333196SBen Greear 	.accept_ra_from_local	= 0,
2718013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
272c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
273930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
274930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
27552e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
27609c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
277bbea124bSJoel Scherpelz 	.accept_ra_rt_info_min_plen = 0,
27809c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
27909c884d4SYOSHIFUJI Hideaki #endif
280930d6ff2SYOSHIFUJI Hideaki #endif
281fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2820bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
283778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2841b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
285b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
286c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2873d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2883d1bec99SHannes Frederic Sowa 		.initialized = false,
2893d1bec99SHannes Frederic Sowa 	},
2903985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
29135103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
292f1705ec1SDavid Ahern 	.keep_addr_on_down	= 0,
2931ababebaSDavid Lebrun 	.seg6_enabled		= 0,
294bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
295bf355b8dSDavid Lebrun 	.seg6_require_hmac	= 0,
296bf355b8dSDavid Lebrun #endif
297adc176c5SErik Nordmark 	.enhanced_dad           = 1,
298d35a00b8SFelix Jia 	.addr_gen_mode		= IN6_ADDR_GEN_MODE_EUI64,
299df789fe7SDavid Forster 	.disable_policy		= 0,
3008610c7c6SAlexander Aring 	.rpl_seg_enabled	= 0,
3019ee11f0fSJustin Iurman 	.ioam6_enabled		= 0,
3029ee11f0fSJustin Iurman 	.ioam6_id               = IOAM6_DEFAULT_IF_ID,
3039ee11f0fSJustin Iurman 	.ioam6_id_wide		= IOAM6_DEFAULT_IF_ID_WIDE,
30418ac597aSJames Prestwood 	.ndisc_evict_nocarrier	= 1,
3051da177e4SLinus Torvalds };
3061da177e4SLinus Torvalds 
3071f372c7bSMike Manning /* Check if link is ready: is it up and is a valid qdisc available */
3081f372c7bSMike Manning static inline bool addrconf_link_ready(const struct net_device *dev)
309f24e3d65SMitsuru Chinen {
3101f372c7bSMike Manning 	return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
311f24e3d65SMitsuru Chinen }
312f24e3d65SMitsuru Chinen 
313b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev)
3141da177e4SLinus Torvalds {
315b7b1bfceSHannes Frederic Sowa 	if (del_timer(&idev->rs_timer))
316b7b1bfceSHannes Frederic Sowa 		__in6_dev_put(idev);
317b7b1bfceSHannes Frederic Sowa }
318b7b1bfceSHannes Frederic Sowa 
319c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
320b7b1bfceSHannes Frederic Sowa {
321c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
3221da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
325b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev,
3261da177e4SLinus Torvalds 				  unsigned long when)
3271da177e4SLinus Torvalds {
328b7b1bfceSHannes Frederic Sowa 	if (!timer_pending(&idev->rs_timer))
329b7b1bfceSHannes Frederic Sowa 		in6_dev_hold(idev);
330b7b1bfceSHannes Frederic Sowa 	mod_timer(&idev->rs_timer, jiffies + when);
3311da177e4SLinus Torvalds }
332b7b1bfceSHannes Frederic Sowa 
333c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
334c15b1ccaSHannes Frederic Sowa 				   unsigned long delay)
335b7b1bfceSHannes Frederic Sowa {
336b7b1bfceSHannes Frederic Sowa 	in6_ifa_hold(ifp);
337f8a894b2SXin Long 	if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
338f8a894b2SXin Long 		in6_ifa_put(ifp);
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3417f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
3427f7d9a6bSHerbert Xu {
343827da44cSJohn Stultz 	int i;
344827da44cSJohn Stultz 
345698365faSWANG Cong 	idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
346698365faSWANG Cong 	if (!idev->stats.ipv6)
3477f7d9a6bSHerbert Xu 		goto err_ip;
348827da44cSJohn Stultz 
349827da44cSJohn Stultz 	for_each_possible_cpu(i) {
350827da44cSJohn Stultz 		struct ipstats_mib *addrconf_stats;
351698365faSWANG Cong 		addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
352827da44cSJohn Stultz 		u64_stats_init(&addrconf_stats->syncp);
353827da44cSJohn Stultz 	}
354827da44cSJohn Stultz 
355827da44cSJohn Stultz 
356be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
357be281e55SEric Dumazet 					GFP_KERNEL);
358be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
3597f7d9a6bSHerbert Xu 		goto err_icmp;
360be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
361be281e55SEric Dumazet 					   GFP_KERNEL);
362be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
36314878f75SDavid L Stevens 		goto err_icmpmsg;
3647f7d9a6bSHerbert Xu 
3657f7d9a6bSHerbert Xu 	return 0;
3667f7d9a6bSHerbert Xu 
36714878f75SDavid L Stevens err_icmpmsg:
368be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3697f7d9a6bSHerbert Xu err_icmp:
370698365faSWANG Cong 	free_percpu(idev->stats.ipv6);
3717f7d9a6bSHerbert Xu err_ip:
372aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3737f7d9a6bSHerbert Xu }
3747f7d9a6bSHerbert Xu 
3751da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	struct inet6_dev *ndev;
378a317a2f1SWANG Cong 	int err = -ENOMEM;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	ASSERT_RTNL();
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
383a317a2f1SWANG Cong 		return ERR_PTR(-EINVAL);
3841da177e4SLinus Torvalds 
385322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
38663159f29SIan Morris 	if (!ndev)
387a317a2f1SWANG Cong 		return ERR_PTR(err);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3901da177e4SLinus Torvalds 	ndev->dev = dev;
391502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
392e99e88a9SKees Cook 	timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0);
393c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3949b29c696SHannes Frederic Sowa 
3959b29c696SHannes Frederic Sowa 	if (ndev->cnf.stable_secret.initialized)
396d35a00b8SFelix Jia 		ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
3979b29c696SHannes Frederic Sowa 
3981da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
39949b99da2SRocco Yue 	ndev->ra_mtu = 0;
4001da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
40163159f29SIan Morris 	if (!ndev->nd_parms) {
4021da177e4SLinus Torvalds 		kfree(ndev);
403a317a2f1SWANG Cong 		return ERR_PTR(err);
4041da177e4SLinus Torvalds 	}
4050187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
4060187bdfbSBen Hutchings 		dev_disable_lro(dev);
4071da177e4SLinus Torvalds 	/* We refer to the device */
4088c727003SEric Dumazet 	dev_hold_track(dev, &ndev->dev_tracker, GFP_KERNEL);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
411e32ac250SJoe Perches 		netdev_dbg(dev, "%s: cannot allocate memory for statistics\n",
412e32ac250SJoe Perches 			   __func__);
4131da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
4148c727003SEric Dumazet 		dev_put_track(dev, &ndev->dev_tracker);
4158603e33dSRoy Li 		kfree(ndev);
416a317a2f1SWANG Cong 		return ERR_PTR(err);
4171da177e4SLinus Torvalds 	}
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
420e32ac250SJoe Perches 		netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n",
421ba3542e1Sdingtianhong 			   __func__, dev->name);
422a317a2f1SWANG Cong 		goto err_release;
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 
4259d6280daSJiri Bohac 	/* One reference from device. */
4261be92460SReshetova, Elena 	refcount_set(&ndev->refcnt, 1);
4271da177e4SLinus Torvalds 
4281b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
4291b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
4301b34be74SYOSHIFUJI Hideaki 
43107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
432c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
433f3213831SJoe Perches 		pr_info("%s: Disabled Multicast RS\n", dev->name);
434c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
435c7dc89c0SFred L. Templin 	}
436b077d7abSYOSHIFUJI Hideaki #endif
437b077d7abSYOSHIFUJI Hideaki 
438372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
43976506a98SJiri Bohac 	ndev->desync_factor = U32_MAX;
4401da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
4411da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4429625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4431da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4441da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4450be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
446969c5464SFernando Gont 	}
4475d9efa7eSDavid S. Miller 
448914faa14SDaniel Borkmann 	ndev->token = in6addr_any;
4491da177e4SLinus Torvalds 
4501f372c7bSMike Manning 	if (netif_running(dev) && addrconf_link_ready(dev))
45153aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
45253aadcc9SHerbert Xu 
4531da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4541da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
455a317a2f1SWANG Cong 	err = addrconf_sysctl_register(ndev);
456a317a2f1SWANG Cong 	if (err) {
457a317a2f1SWANG Cong 		ipv6_mc_destroy_dev(ndev);
4582a189f9eSSabrina Dubroca 		snmp6_unregister_dev(ndev);
459a317a2f1SWANG Cong 		goto err_release;
460a317a2f1SWANG Cong 	}
46130c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
462cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip6_ptr, ndev);
463d88ae4ccSYOSHIFUJI Hideaki 
4642c5e8933SHannes Frederic Sowa 	/* Join interface-local all-node multicast group */
4652c5e8933SHannes Frederic Sowa 	ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4662c5e8933SHannes Frederic Sowa 
467d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
468f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
469d88ae4ccSYOSHIFUJI Hideaki 
470d6ddef9eSLi Wei 	/* Join all-router multicast group if forwarding is set */
4718b2aaedeSLi Wei 	if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
472d6ddef9eSLi Wei 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
473d6ddef9eSLi Wei 
4741da177e4SLinus Torvalds 	return ndev;
475a317a2f1SWANG Cong 
476a317a2f1SWANG Cong err_release:
477a317a2f1SWANG Cong 	neigh_parms_release(&nd_tbl, ndev->nd_parms);
478a317a2f1SWANG Cong 	ndev->dead = 1;
479a317a2f1SWANG Cong 	in6_dev_finish_destroy(ndev);
480a317a2f1SWANG Cong 	return ERR_PTR(err);
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
483cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4841da177e4SLinus Torvalds {
4851da177e4SLinus Torvalds 	struct inet6_dev *idev;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	ASSERT_RTNL();
4881da177e4SLinus Torvalds 
489e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
490e21e8467SStephen Hemminger 	if (!idev) {
491e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
492a317a2f1SWANG Cong 		if (IS_ERR(idev))
493db0b99f5SSabrina Dubroca 			return idev;
4941da177e4SLinus Torvalds 	}
495c5e33bddSYOSHIFUJI Hideaki 
4961da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4971da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4981da177e4SLinus Torvalds 	return idev;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
501f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
502f3a1bfb1SNicolas Dichtel {
503f3a1bfb1SNicolas Dichtel 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
504f3a1bfb1SNicolas Dichtel 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
505136ba622SZhang Shengju 	bool all = false;
506f3a1bfb1SNicolas Dichtel 
507136ba622SZhang Shengju 	if (type == NETCONFA_ALL)
508136ba622SZhang Shengju 		all = true;
509136ba622SZhang Shengju 
510136ba622SZhang Shengju 	if (all || type == NETCONFA_FORWARDING)
511f3a1bfb1SNicolas Dichtel 		size += nla_total_size(4);
512b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
513136ba622SZhang Shengju 	if (all || type == NETCONFA_MC_FORWARDING)
514d67b8c61SNicolas Dichtel 		size += nla_total_size(4);
515b1afce95SDavid S. Miller #endif
516136ba622SZhang Shengju 	if (all || type == NETCONFA_PROXY_NEIGH)
517c92d5491Sstephen hemminger 		size += nla_total_size(4);
518f3a1bfb1SNicolas Dichtel 
519136ba622SZhang Shengju 	if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
52035103d11SAndy Gospodarek 		size += nla_total_size(4);
52135103d11SAndy Gospodarek 
522f3a1bfb1SNicolas Dichtel 	return size;
523f3a1bfb1SNicolas Dichtel }
524f3a1bfb1SNicolas Dichtel 
525f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
526f3a1bfb1SNicolas Dichtel 				      struct ipv6_devconf *devconf, u32 portid,
527f3a1bfb1SNicolas Dichtel 				      u32 seq, int event, unsigned int flags,
528f3a1bfb1SNicolas Dichtel 				      int type)
529f3a1bfb1SNicolas Dichtel {
530f3a1bfb1SNicolas Dichtel 	struct nlmsghdr  *nlh;
531f3a1bfb1SNicolas Dichtel 	struct netconfmsg *ncm;
532136ba622SZhang Shengju 	bool all = false;
533f3a1bfb1SNicolas Dichtel 
534f3a1bfb1SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
535f3a1bfb1SNicolas Dichtel 			flags);
53663159f29SIan Morris 	if (!nlh)
537f3a1bfb1SNicolas Dichtel 		return -EMSGSIZE;
538f3a1bfb1SNicolas Dichtel 
539136ba622SZhang Shengju 	if (type == NETCONFA_ALL)
540136ba622SZhang Shengju 		all = true;
541136ba622SZhang Shengju 
542f3a1bfb1SNicolas Dichtel 	ncm = nlmsg_data(nlh);
543f3a1bfb1SNicolas Dichtel 	ncm->ncm_family = AF_INET6;
544f3a1bfb1SNicolas Dichtel 
545f3a1bfb1SNicolas Dichtel 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
546f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
547f3a1bfb1SNicolas Dichtel 
54823452170SDavid Ahern 	if (!devconf)
54923452170SDavid Ahern 		goto out;
55023452170SDavid Ahern 
551136ba622SZhang Shengju 	if ((all || type == NETCONFA_FORWARDING) &&
552f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
553f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
554b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
555136ba622SZhang Shengju 	if ((all || type == NETCONFA_MC_FORWARDING) &&
556d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
557145c7a79SEric Dumazet 			atomic_read(&devconf->mc_forwarding)) < 0)
558d67b8c61SNicolas Dichtel 		goto nla_put_failure;
559b1afce95SDavid S. Miller #endif
560136ba622SZhang Shengju 	if ((all || type == NETCONFA_PROXY_NEIGH) &&
561c92d5491Sstephen hemminger 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
562c92d5491Sstephen hemminger 		goto nla_put_failure;
563c92d5491Sstephen hemminger 
564136ba622SZhang Shengju 	if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
56535103d11SAndy Gospodarek 	    nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
56635103d11SAndy Gospodarek 			devconf->ignore_routes_with_linkdown) < 0)
56735103d11SAndy Gospodarek 		goto nla_put_failure;
56835103d11SAndy Gospodarek 
56923452170SDavid Ahern out:
570053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
571053c095aSJohannes Berg 	return 0;
572f3a1bfb1SNicolas Dichtel 
573f3a1bfb1SNicolas Dichtel nla_put_failure:
574f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
575f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
576f3a1bfb1SNicolas Dichtel }
577f3a1bfb1SNicolas Dichtel 
57885b3daadSDavid Ahern void inet6_netconf_notify_devconf(struct net *net, int event, int type,
57985b3daadSDavid Ahern 				  int ifindex, struct ipv6_devconf *devconf)
580f3a1bfb1SNicolas Dichtel {
581f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
582f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
583f3a1bfb1SNicolas Dichtel 
584927265bcSEric Dumazet 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
58563159f29SIan Morris 	if (!skb)
586f3a1bfb1SNicolas Dichtel 		goto errout;
587f3a1bfb1SNicolas Dichtel 
588f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
58985b3daadSDavid Ahern 					 event, 0, type);
590f3a1bfb1SNicolas Dichtel 	if (err < 0) {
591f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
592f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
593f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
594f3a1bfb1SNicolas Dichtel 		goto errout;
595f3a1bfb1SNicolas Dichtel 	}
596927265bcSEric Dumazet 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
597f3a1bfb1SNicolas Dichtel 	return;
598f3a1bfb1SNicolas Dichtel errout:
599f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
600f3a1bfb1SNicolas Dichtel }
601f3a1bfb1SNicolas Dichtel 
60276f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
60376f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
60476f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
605c92d5491Sstephen hemminger 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
60635103d11SAndy Gospodarek 	[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]	= { .len = sizeof(int) },
60776f8f6cbSNicolas Dichtel };
60876f8f6cbSNicolas Dichtel 
60938d51810SJakub Kicinski static int inet6_netconf_valid_get_req(struct sk_buff *skb,
61038d51810SJakub Kicinski 				       const struct nlmsghdr *nlh,
61138d51810SJakub Kicinski 				       struct nlattr **tb,
61238d51810SJakub Kicinski 				       struct netlink_ext_ack *extack)
61338d51810SJakub Kicinski {
61438d51810SJakub Kicinski 	int i, err;
61538d51810SJakub Kicinski 
61638d51810SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
61738d51810SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request");
61838d51810SJakub Kicinski 		return -EINVAL;
61938d51810SJakub Kicinski 	}
62038d51810SJakub Kicinski 
62138d51810SJakub Kicinski 	if (!netlink_strict_get_check(skb))
6228cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
6238cb08174SJohannes Berg 					      tb, NETCONFA_MAX,
6248cb08174SJohannes Berg 					      devconf_ipv6_policy, extack);
62538d51810SJakub Kicinski 
6268cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
6278cb08174SJohannes Berg 					    tb, NETCONFA_MAX,
6288cb08174SJohannes Berg 					    devconf_ipv6_policy, extack);
62938d51810SJakub Kicinski 	if (err)
63038d51810SJakub Kicinski 		return err;
63138d51810SJakub Kicinski 
63238d51810SJakub Kicinski 	for (i = 0; i <= NETCONFA_MAX; i++) {
63338d51810SJakub Kicinski 		if (!tb[i])
63438d51810SJakub Kicinski 			continue;
63538d51810SJakub Kicinski 
63638d51810SJakub Kicinski 		switch (i) {
63738d51810SJakub Kicinski 		case NETCONFA_IFINDEX:
63838d51810SJakub Kicinski 			break;
63938d51810SJakub Kicinski 		default:
64038d51810SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
64138d51810SJakub Kicinski 			return -EINVAL;
64238d51810SJakub Kicinski 		}
64338d51810SJakub Kicinski 	}
64438d51810SJakub Kicinski 
64538d51810SJakub Kicinski 	return 0;
64638d51810SJakub Kicinski }
64738d51810SJakub Kicinski 
64876f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
649c21ef3e3SDavid Ahern 				     struct nlmsghdr *nlh,
650c21ef3e3SDavid Ahern 				     struct netlink_ext_ack *extack)
65176f8f6cbSNicolas Dichtel {
65276f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
65376f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
6544ea2607fSFlorian Westphal 	struct inet6_dev *in6_dev = NULL;
6554ea2607fSFlorian Westphal 	struct net_device *dev = NULL;
65676f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
65776f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
65876f8f6cbSNicolas Dichtel 	int ifindex;
65976f8f6cbSNicolas Dichtel 	int err;
66076f8f6cbSNicolas Dichtel 
66138d51810SJakub Kicinski 	err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack);
66276f8f6cbSNicolas Dichtel 	if (err < 0)
6634ea2607fSFlorian Westphal 		return err;
6644ea2607fSFlorian Westphal 
6654ea2607fSFlorian Westphal 	if (!tb[NETCONFA_IFINDEX])
6664ea2607fSFlorian Westphal 		return -EINVAL;
66776f8f6cbSNicolas Dichtel 
668a97eb33fSAnton Protopopov 	err = -EINVAL;
66976f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
67076f8f6cbSNicolas Dichtel 	switch (ifindex) {
67176f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
67276f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
67376f8f6cbSNicolas Dichtel 		break;
67476f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
67576f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
67676f8f6cbSNicolas Dichtel 		break;
67776f8f6cbSNicolas Dichtel 	default:
6784ea2607fSFlorian Westphal 		dev = dev_get_by_index(net, ifindex);
67963159f29SIan Morris 		if (!dev)
6804ea2607fSFlorian Westphal 			return -EINVAL;
6814ea2607fSFlorian Westphal 		in6_dev = in6_dev_get(dev);
68263159f29SIan Morris 		if (!in6_dev)
68376f8f6cbSNicolas Dichtel 			goto errout;
68476f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
68576f8f6cbSNicolas Dichtel 		break;
68676f8f6cbSNicolas Dichtel 	}
68776f8f6cbSNicolas Dichtel 
68876f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
6894ea2607fSFlorian Westphal 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
69063159f29SIan Morris 	if (!skb)
69176f8f6cbSNicolas Dichtel 		goto errout;
69276f8f6cbSNicolas Dichtel 
69376f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
69476f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
69576f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
696136ba622SZhang Shengju 					 NETCONFA_ALL);
69776f8f6cbSNicolas Dichtel 	if (err < 0) {
69876f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
69976f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
70076f8f6cbSNicolas Dichtel 		kfree_skb(skb);
70176f8f6cbSNicolas Dichtel 		goto errout;
70276f8f6cbSNicolas Dichtel 	}
70376f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
70476f8f6cbSNicolas Dichtel errout:
7054ea2607fSFlorian Westphal 	if (in6_dev)
7064ea2607fSFlorian Westphal 		in6_dev_put(in6_dev);
7074ea2607fSFlorian Westphal 	dev_put(dev);
70876f8f6cbSNicolas Dichtel 	return err;
70976f8f6cbSNicolas Dichtel }
71076f8f6cbSNicolas Dichtel 
7117a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
7127a674200SNicolas Dichtel 				      struct netlink_callback *cb)
7137a674200SNicolas Dichtel {
714addd383fSDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
7157a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
7167a674200SNicolas Dichtel 	int h, s_h;
7177a674200SNicolas Dichtel 	int idx, s_idx;
7187a674200SNicolas Dichtel 	struct net_device *dev;
7197a674200SNicolas Dichtel 	struct inet6_dev *idev;
7207a674200SNicolas Dichtel 	struct hlist_head *head;
7217a674200SNicolas Dichtel 
722addd383fSDavid Ahern 	if (cb->strict_check) {
723addd383fSDavid Ahern 		struct netlink_ext_ack *extack = cb->extack;
724addd383fSDavid Ahern 		struct netconfmsg *ncm;
725addd383fSDavid Ahern 
726addd383fSDavid Ahern 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
727addd383fSDavid Ahern 			NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
728addd383fSDavid Ahern 			return -EINVAL;
729addd383fSDavid Ahern 		}
730addd383fSDavid Ahern 
731addd383fSDavid Ahern 		if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
732addd383fSDavid Ahern 			NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
733addd383fSDavid Ahern 			return -EINVAL;
734addd383fSDavid Ahern 		}
735addd383fSDavid Ahern 	}
736addd383fSDavid Ahern 
7377a674200SNicolas Dichtel 	s_h = cb->args[0];
7387a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
7397a674200SNicolas Dichtel 
7407a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
7417a674200SNicolas Dichtel 		idx = 0;
7427a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
7437a674200SNicolas Dichtel 		rcu_read_lock();
74463998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
74563998ac2SNicolas Dichtel 			  net->dev_base_seq;
7467a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
7477a674200SNicolas Dichtel 			if (idx < s_idx)
7487a674200SNicolas Dichtel 				goto cont;
7497a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
7507a674200SNicolas Dichtel 			if (!idev)
7517a674200SNicolas Dichtel 				goto cont;
7527a674200SNicolas Dichtel 
7537a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
7547a674200SNicolas Dichtel 						       &idev->cnf,
7557a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
756addd383fSDavid Ahern 						       nlh->nlmsg_seq,
7577a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
7587a674200SNicolas Dichtel 						       NLM_F_MULTI,
759136ba622SZhang Shengju 						       NETCONFA_ALL) < 0) {
7607a674200SNicolas Dichtel 				rcu_read_unlock();
7617a674200SNicolas Dichtel 				goto done;
7627a674200SNicolas Dichtel 			}
76363998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7647a674200SNicolas Dichtel cont:
7657a674200SNicolas Dichtel 			idx++;
7667a674200SNicolas Dichtel 		}
7677a674200SNicolas Dichtel 		rcu_read_unlock();
7687a674200SNicolas Dichtel 	}
7697a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
7707a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
7717a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
7727a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
773addd383fSDavid Ahern 					       nlh->nlmsg_seq,
7747a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
775136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
7767a674200SNicolas Dichtel 			goto done;
7777a674200SNicolas Dichtel 		else
7787a674200SNicolas Dichtel 			h++;
7797a674200SNicolas Dichtel 	}
7807a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
7817a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
7827a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
7837a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
784addd383fSDavid Ahern 					       nlh->nlmsg_seq,
7857a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
786136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
7877a674200SNicolas Dichtel 			goto done;
7887a674200SNicolas Dichtel 		else
7897a674200SNicolas Dichtel 			h++;
7907a674200SNicolas Dichtel 	}
7917a674200SNicolas Dichtel done:
7927a674200SNicolas Dichtel 	cb->args[0] = h;
7937a674200SNicolas Dichtel 	cb->args[1] = idx;
7947a674200SNicolas Dichtel 
7957a674200SNicolas Dichtel 	return skb->len;
7967a674200SNicolas Dichtel }
7977a674200SNicolas Dichtel 
7981da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
7991da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	struct net_device *dev;
8021da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	if (!idev)
8051da177e4SLinus Torvalds 		return;
8061da177e4SLinus Torvalds 	dev = idev->dev;
8070187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
8080187bdfbSBen Hutchings 		dev_disable_lro(dev);
8091a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
8102c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
811f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
8122c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
8132c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
8142c5e8933SHannes Frederic Sowa 		} else {
815f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
8162c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
8172c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
8182c5e8933SHannes Frederic Sowa 		}
8191da177e4SLinus Torvalds 	}
820502a2ffdSstephen hemminger 
821502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
8222c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
8232c12a74cSMichal Wrobel 			continue;
8241da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
8251da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
8261da177e4SLinus Torvalds 		else
8271da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
8281da177e4SLinus Torvalds 	}
82985b3daadSDavid Ahern 	inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
83085b3daadSDavid Ahern 				     NETCONFA_FORWARDING,
831f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 
835e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
8361da177e4SLinus Torvalds {
8371da177e4SLinus Torvalds 	struct net_device *dev;
8381da177e4SLinus Torvalds 	struct inet6_dev *idev;
8391da177e4SLinus Torvalds 
8404acd4945SBen Hutchings 	for_each_netdev(net, dev) {
8411da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
8421da177e4SLinus Torvalds 		if (idev) {
843e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
844e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
8451da177e4SLinus Torvalds 			if (changed)
8461da177e4SLinus Torvalds 				dev_forward_change(idev);
8471da177e4SLinus Torvalds 		}
8481da177e4SLinus Torvalds 	}
8491da177e4SLinus Torvalds }
850c8fecf22SPavel Emelyanov 
851013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
852c8fecf22SPavel Emelyanov {
853bff16c2fSPavel Emelyanov 	struct net *net;
854013d97e9SFrancesco Ruggeri 	int old;
855013d97e9SFrancesco Ruggeri 
856013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
857013d97e9SFrancesco Ruggeri 		return restart_syscall();
858bff16c2fSPavel Emelyanov 
859bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
860013d97e9SFrancesco Ruggeri 	old = *p;
861013d97e9SFrancesco Ruggeri 	*p = newf;
862c8fecf22SPavel Emelyanov 
863013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
864f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
86585b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
86685b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
867f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
868f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
869013d97e9SFrancesco Ruggeri 		rtnl_unlock();
870013d97e9SFrancesco Ruggeri 		return 0;
87188af182eSEric W. Biederman 	}
872b325fddbSStephen Hemminger 
873e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
874d26c638cSNicolas Dichtel 		int old_dflt = net->ipv6.devconf_dflt->forwarding;
875d26c638cSNicolas Dichtel 
876e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
877d26c638cSNicolas Dichtel 		if ((!newf) ^ (!old_dflt))
87885b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
87985b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
880d26c638cSNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
881d26c638cSNicolas Dichtel 						     net->ipv6.devconf_dflt);
882d26c638cSNicolas Dichtel 
883e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
884f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
88585b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
88685b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
887f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
888f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
889013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
890c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
8910187bdfbSBen Hutchings 	rtnl_unlock();
892c8fecf22SPavel Emelyanov 
893013d97e9SFrancesco Ruggeri 	if (newf)
8947b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
895b325fddbSStephen Hemminger 	return 1;
896c8fecf22SPavel Emelyanov }
89735103d11SAndy Gospodarek 
89835103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf)
89935103d11SAndy Gospodarek {
90035103d11SAndy Gospodarek 	struct net_device *dev;
90135103d11SAndy Gospodarek 	struct inet6_dev *idev;
90235103d11SAndy Gospodarek 
90335103d11SAndy Gospodarek 	for_each_netdev(net, dev) {
90435103d11SAndy Gospodarek 		idev = __in6_dev_get(dev);
90535103d11SAndy Gospodarek 		if (idev) {
90635103d11SAndy Gospodarek 			int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
90735103d11SAndy Gospodarek 
90835103d11SAndy Gospodarek 			idev->cnf.ignore_routes_with_linkdown = newf;
90935103d11SAndy Gospodarek 			if (changed)
91035103d11SAndy Gospodarek 				inet6_netconf_notify_devconf(dev_net(dev),
91185b3daadSDavid Ahern 							     RTM_NEWNETCONF,
91235103d11SAndy Gospodarek 							     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
91335103d11SAndy Gospodarek 							     dev->ifindex,
91435103d11SAndy Gospodarek 							     &idev->cnf);
91535103d11SAndy Gospodarek 		}
91635103d11SAndy Gospodarek 	}
91735103d11SAndy Gospodarek }
91835103d11SAndy Gospodarek 
91935103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
92035103d11SAndy Gospodarek {
92135103d11SAndy Gospodarek 	struct net *net;
92235103d11SAndy Gospodarek 	int old;
92335103d11SAndy Gospodarek 
92435103d11SAndy Gospodarek 	if (!rtnl_trylock())
92535103d11SAndy Gospodarek 		return restart_syscall();
92635103d11SAndy Gospodarek 
92735103d11SAndy Gospodarek 	net = (struct net *)table->extra2;
92835103d11SAndy Gospodarek 	old = *p;
92935103d11SAndy Gospodarek 	*p = newf;
93035103d11SAndy Gospodarek 
93135103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
93235103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
93335103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
93485b3daadSDavid Ahern 						     RTM_NEWNETCONF,
93535103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
93635103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_DEFAULT,
93735103d11SAndy Gospodarek 						     net->ipv6.devconf_dflt);
93835103d11SAndy Gospodarek 		rtnl_unlock();
93935103d11SAndy Gospodarek 		return 0;
94035103d11SAndy Gospodarek 	}
94135103d11SAndy Gospodarek 
94235103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
94335103d11SAndy Gospodarek 		net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
94435103d11SAndy Gospodarek 		addrconf_linkdown_change(net, newf);
94535103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
94635103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
94785b3daadSDavid Ahern 						     RTM_NEWNETCONF,
94835103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
94935103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_ALL,
95035103d11SAndy Gospodarek 						     net->ipv6.devconf_all);
95135103d11SAndy Gospodarek 	}
95235103d11SAndy Gospodarek 	rtnl_unlock();
95335103d11SAndy Gospodarek 
95435103d11SAndy Gospodarek 	return 1;
95535103d11SAndy Gospodarek }
95635103d11SAndy Gospodarek 
9571da177e4SLinus Torvalds #endif
9581da177e4SLinus Torvalds 
9595c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
9601da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
9611da177e4SLinus Torvalds {
962c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
963547b792cSIlpo Järvinen 
9641da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
96591df42beSJoe Perches 	pr_debug("%s\n", __func__);
9661da177e4SLinus Torvalds #endif
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
9691da177e4SLinus Torvalds 
970c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
971c15b1ccaSHannes Frederic Sowa 		pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
972c15b1ccaSHannes Frederic Sowa 			  ifp);
9731da177e4SLinus Torvalds 
974e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
975f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
9761da177e4SLinus Torvalds 		return;
9771da177e4SLinus Torvalds 	}
9781da177e4SLinus Torvalds 
979e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
982e55ffac6SBrian Haley static void
983e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
984e55ffac6SBrian Haley {
985502a2ffdSstephen hemminger 	struct list_head *p;
9868a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
987e55ffac6SBrian Haley 
988e55ffac6SBrian Haley 	/*
989e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
990e55ffac6SBrian Haley 	 * global before linklocal.
991e55ffac6SBrian Haley 	 */
992502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
993502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
994502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
9958a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
996e55ffac6SBrian Haley 			break;
997e55ffac6SBrian Haley 	}
998e55ffac6SBrian Haley 
9998ef802aaSEric Dumazet 	list_add_tail_rcu(&ifp->if_list, p);
1000e55ffac6SBrian Haley }
1001e55ffac6SBrian Haley 
10023f27fb23SEric Dumazet static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
10033eb84f49SYOSHIFUJI Hideaki {
10043f27fb23SEric Dumazet 	u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
10053f27fb23SEric Dumazet 
10063f27fb23SEric Dumazet 	return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
10073eb84f49SYOSHIFUJI Hideaki }
10083eb84f49SYOSHIFUJI Hideaki 
100956fc709bSEric Dumazet static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1010752a9292SEric Dumazet 			       struct net_device *dev, unsigned int hash)
101156fc709bSEric Dumazet {
101256fc709bSEric Dumazet 	struct inet6_ifaddr *ifp;
101356fc709bSEric Dumazet 
101456fc709bSEric Dumazet 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
101556fc709bSEric Dumazet 		if (!net_eq(dev_net(ifp->idev->dev), net))
101656fc709bSEric Dumazet 			continue;
101756fc709bSEric Dumazet 		if (ipv6_addr_equal(&ifp->addr, addr)) {
101856fc709bSEric Dumazet 			if (!dev || ifp->idev->dev == dev)
101956fc709bSEric Dumazet 				return true;
102056fc709bSEric Dumazet 		}
102156fc709bSEric Dumazet 	}
102256fc709bSEric Dumazet 	return false;
102356fc709bSEric Dumazet }
102456fc709bSEric Dumazet 
1025f3d9832eSDavid Ahern static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
1026f3d9832eSDavid Ahern {
10273f27fb23SEric Dumazet 	unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr);
1028f3d9832eSDavid Ahern 	int err = 0;
1029f3d9832eSDavid Ahern 
1030f3d9832eSDavid Ahern 	spin_lock(&addrconf_hash_lock);
1031f3d9832eSDavid Ahern 
1032f3d9832eSDavid Ahern 	/* Ignore adding duplicate addresses on an interface */
1033752a9292SEric Dumazet 	if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) {
1034e32ac250SJoe Perches 		netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
1035f3d9832eSDavid Ahern 		err = -EEXIST;
1036752a9292SEric Dumazet 	} else {
1037752a9292SEric Dumazet 		hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
1038f3d9832eSDavid Ahern 	}
1039f3d9832eSDavid Ahern 
1040f3d9832eSDavid Ahern 	spin_unlock(&addrconf_hash_lock);
1041f3d9832eSDavid Ahern 
1042f3d9832eSDavid Ahern 	return err;
1043f3d9832eSDavid Ahern }
1044f3d9832eSDavid Ahern 
10451da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds static struct inet6_ifaddr *
1048e6464b8cSDavid Ahern ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
1049de95e047SDavid Ahern 	      bool can_block, struct netlink_ext_ack *extack)
10501da177e4SLinus Torvalds {
1051f3d9832eSDavid Ahern 	gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
1052e6464b8cSDavid Ahern 	int addr_type = ipv6_addr_type(cfg->pfx);
1053df789fe7SDavid Forster 	struct net *net = dev_net(idev->dev);
10541da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
1055360a9887SDavid Ahern 	struct fib6_info *f6i = NULL;
10561da177e4SLinus Torvalds 	int err = 0;
1057d68b8270SYOSHIFUJI Hideaki 
1058d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
1059f17f7648SHangbin Liu 	    (addr_type & IPV6_ADDR_MULTICAST &&
1060f17f7648SHangbin Liu 	     !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) ||
1061d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
10623ede0bbcSRobert Shearman 	     !netif_is_l3_master(idev->dev) &&
1063d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
1064d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	if (idev->dead) {
10671da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
1068f3d9832eSDavid Ahern 		goto out;
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds 
107156d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
10729bdd8d40SBrian Haley 		err = -EACCES;
1073f3d9832eSDavid Ahern 		goto out;
10749bdd8d40SBrian Haley 	}
10759bdd8d40SBrian Haley 
1076ff7883eaSDavid Ahern 	/* validator notifier needs to be blocking;
1077ff7883eaSDavid Ahern 	 * do not call in atomic context
1078ff7883eaSDavid Ahern 	 */
1079ff7883eaSDavid Ahern 	if (can_block) {
1080ff7883eaSDavid Ahern 		struct in6_validator_info i6vi = {
1081e6464b8cSDavid Ahern 			.i6vi_addr = *cfg->pfx,
1082ff7883eaSDavid Ahern 			.i6vi_dev = idev,
1083de95e047SDavid Ahern 			.extack = extack,
1084ff7883eaSDavid Ahern 		};
1085ff7883eaSDavid Ahern 
10863ad7d246SKrister Johansen 		err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
10873ad7d246SKrister Johansen 		err = notifier_to_errno(err);
1088f3d9832eSDavid Ahern 		if (err < 0)
10891da177e4SLinus Torvalds 			goto out;
1090ff7883eaSDavid Ahern 	}
10911da177e4SLinus Torvalds 
10926126891cSVasily Averin 	ifa = kzalloc(sizeof(*ifa), gfp_flags | __GFP_ACCOUNT);
109363159f29SIan Morris 	if (!ifa) {
10941da177e4SLinus Torvalds 		err = -ENOBUFS;
10951da177e4SLinus Torvalds 		goto out;
10961da177e4SLinus Torvalds 	}
10971da177e4SLinus Torvalds 
1098e6464b8cSDavid Ahern 	f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags);
1099360a9887SDavid Ahern 	if (IS_ERR(f6i)) {
1100360a9887SDavid Ahern 		err = PTR_ERR(f6i);
1101360a9887SDavid Ahern 		f6i = NULL;
11021da177e4SLinus Torvalds 		goto out;
11031da177e4SLinus Torvalds 	}
11041da177e4SLinus Torvalds 
1105df789fe7SDavid Forster 	if (net->ipv6.devconf_all->disable_policy ||
1106df789fe7SDavid Forster 	    idev->cnf.disable_policy)
1107360a9887SDavid Ahern 		f6i->dst_nopolicy = true;
1108df789fe7SDavid Forster 
1109bba24896SJiri Pirko 	neigh_parms_data_state_setall(idev->nd_parms);
1110bba24896SJiri Pirko 
1111e6464b8cSDavid Ahern 	ifa->addr = *cfg->pfx;
1112e6464b8cSDavid Ahern 	if (cfg->peer_pfx)
1113e6464b8cSDavid Ahern 		ifa->peer_addr = *cfg->peer_pfx;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
1116c15b1ccaSHannes Frederic Sowa 	INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1117c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
1118e6464b8cSDavid Ahern 	ifa->scope = cfg->scope;
1119e6464b8cSDavid Ahern 	ifa->prefix_len = cfg->plen;
11208308f3ffSDavid Ahern 	ifa->rt_priority = cfg->rt_priority;
1121e6464b8cSDavid Ahern 	ifa->flags = cfg->ifa_flags;
112266eb9f86SMahesh Bandewar 	/* No need to add the TENTATIVE flag for addresses with NODAD */
1123e6464b8cSDavid Ahern 	if (!(cfg->ifa_flags & IFA_F_NODAD))
112466eb9f86SMahesh Bandewar 		ifa->flags |= IFA_F_TENTATIVE;
1125e6464b8cSDavid Ahern 	ifa->valid_lft = cfg->valid_lft;
1126e6464b8cSDavid Ahern 	ifa->prefered_lft = cfg->preferred_lft;
11271da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
1128617fe29dSDaniel Borkmann 	ifa->tokenized = false;
11291da177e4SLinus Torvalds 
1130360a9887SDavid Ahern 	ifa->rt = f6i;
113157f5f544SKeir Fraser 
11321da177e4SLinus Torvalds 	ifa->idev = idev;
1133f3d9832eSDavid Ahern 	in6_dev_hold(idev);
1134f3d9832eSDavid Ahern 
11351da177e4SLinus Torvalds 	/* For caller */
1136271201c0SReshetova, Elena 	refcount_set(&ifa->refcnt, 1);
11371da177e4SLinus Torvalds 
1138f3d9832eSDavid Ahern 	rcu_read_lock_bh();
11391da177e4SLinus Torvalds 
1140f3d9832eSDavid Ahern 	err = ipv6_add_addr_hash(idev->dev, ifa);
1141f3d9832eSDavid Ahern 	if (err < 0) {
1142f3d9832eSDavid Ahern 		rcu_read_unlock_bh();
1143f3d9832eSDavid Ahern 		goto out;
1144f3d9832eSDavid Ahern 	}
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	write_lock(&idev->lock);
1147f3d9832eSDavid Ahern 
11481da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
1149e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
1152372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
11531da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
11541da177e4SLinus Torvalds 	}
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
11571da177e4SLinus Torvalds 	write_unlock(&idev->lock);
1158f3d9832eSDavid Ahern 
11598814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
11601da177e4SLinus Torvalds 
1161f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1162f3d9832eSDavid Ahern out:
1163f3d9832eSDavid Ahern 	if (unlikely(err < 0)) {
1164360a9887SDavid Ahern 		fib6_info_release(f6i);
116593531c67SDavid Ahern 
1166f3d9832eSDavid Ahern 		if (ifa) {
1167f3d9832eSDavid Ahern 			if (ifa->idev)
1168f3d9832eSDavid Ahern 				in6_dev_put(ifa->idev);
11691da177e4SLinus Torvalds 			kfree(ifa);
1170f3d9832eSDavid Ahern 		}
11711da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	return ifa;
11751da177e4SLinus Torvalds }
11761da177e4SLinus Torvalds 
11775b84efecSThomas Haller enum cleanup_prefix_rt_t {
11785b84efecSThomas Haller 	CLEANUP_PREFIX_RT_NOP,    /* no cleanup action for prefix route */
11795b84efecSThomas Haller 	CLEANUP_PREFIX_RT_DEL,    /* delete the prefix route */
11805b84efecSThomas Haller 	CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
11815b84efecSThomas Haller };
11825b84efecSThomas Haller 
11835b84efecSThomas Haller /*
11845b84efecSThomas Haller  * Check, whether the prefix for ifp would still need a prefix route
11855b84efecSThomas Haller  * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
11865b84efecSThomas Haller  * constants.
11875b84efecSThomas Haller  *
11885b84efecSThomas Haller  * 1) we don't purge prefix if address was not permanent.
11895b84efecSThomas Haller  *    prefix is managed by its own lifetime.
11905b84efecSThomas Haller  * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
11915b84efecSThomas Haller  * 3) if there are no addresses, delete prefix.
11925b84efecSThomas Haller  * 4) if there are still other permanent address(es),
11935b84efecSThomas Haller  *    corresponding prefix is still permanent.
11945b84efecSThomas Haller  * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
11955b84efecSThomas Haller  *    don't purge the prefix, assume user space is managing it.
11965b84efecSThomas Haller  * 6) otherwise, update prefix lifetime to the
11975b84efecSThomas Haller  *    longest valid lifetime among the corresponding
11985b84efecSThomas Haller  *    addresses on the device.
11995b84efecSThomas Haller  *    Note: subsequent RA will update lifetime.
12005b84efecSThomas Haller  **/
12015b84efecSThomas Haller static enum cleanup_prefix_rt_t
12025b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
12035b84efecSThomas Haller {
12045b84efecSThomas Haller 	struct inet6_ifaddr *ifa;
12055b84efecSThomas Haller 	struct inet6_dev *idev = ifp->idev;
12065b84efecSThomas Haller 	unsigned long lifetime;
12075b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
12085b84efecSThomas Haller 
12095b84efecSThomas Haller 	*expires = jiffies;
12105b84efecSThomas Haller 
12115b84efecSThomas Haller 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
12125b84efecSThomas Haller 		if (ifa == ifp)
12135b84efecSThomas Haller 			continue;
1214e75913c9SZhiqiang Liu 		if (ifa->prefix_len != ifp->prefix_len ||
1215e75913c9SZhiqiang Liu 		    !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
12165b84efecSThomas Haller 				       ifp->prefix_len))
12175b84efecSThomas Haller 			continue;
12185b84efecSThomas Haller 		if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
12195b84efecSThomas Haller 			return CLEANUP_PREFIX_RT_NOP;
12205b84efecSThomas Haller 
12215b84efecSThomas Haller 		action = CLEANUP_PREFIX_RT_EXPIRE;
12225b84efecSThomas Haller 
12235b84efecSThomas Haller 		spin_lock(&ifa->lock);
12245b84efecSThomas Haller 
12255b84efecSThomas Haller 		lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
12265b84efecSThomas Haller 		/*
12275b84efecSThomas Haller 		 * Note: Because this address is
12285b84efecSThomas Haller 		 * not permanent, lifetime <
12295b84efecSThomas Haller 		 * LONG_MAX / HZ here.
12305b84efecSThomas Haller 		 */
12315b84efecSThomas Haller 		if (time_before(*expires, ifa->tstamp + lifetime * HZ))
12325b84efecSThomas Haller 			*expires = ifa->tstamp + lifetime * HZ;
12335b84efecSThomas Haller 		spin_unlock(&ifa->lock);
12345b84efecSThomas Haller 	}
12355b84efecSThomas Haller 
12365b84efecSThomas Haller 	return action;
12375b84efecSThomas Haller }
12385b84efecSThomas Haller 
12395b84efecSThomas Haller static void
1240d0098e4cSHangbin Liu cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires,
1241d0098e4cSHangbin Liu 		     bool del_rt, bool del_peer)
12425b84efecSThomas Haller {
124393c2fb25SDavid Ahern 	struct fib6_info *f6i;
12445b84efecSThomas Haller 
1245d0098e4cSHangbin Liu 	f6i = addrconf_get_prefix_route(del_peer ? &ifp->peer_addr : &ifp->addr,
1246d0098e4cSHangbin Liu 					ifp->prefix_len,
12472b2450caSDavid Ahern 					ifp->idev->dev, 0, RTF_DEFAULT, true);
124893c2fb25SDavid Ahern 	if (f6i) {
12495b84efecSThomas Haller 		if (del_rt)
125011dd74b3SRoopa Prabhu 			ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
12515b84efecSThomas Haller 		else {
125293c2fb25SDavid Ahern 			if (!(f6i->fib6_flags & RTF_EXPIRES))
125393c2fb25SDavid Ahern 				fib6_set_expires(f6i, expires);
125493c2fb25SDavid Ahern 			fib6_info_release(f6i);
12555b84efecSThomas Haller 		}
12565b84efecSThomas Haller 	}
12575b84efecSThomas Haller }
12585b84efecSThomas Haller 
12595b84efecSThomas Haller 
12601da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
12631da177e4SLinus Torvalds {
12644c5ff6a6SHerbert Xu 	int state;
12655b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
12665b84efecSThomas Haller 	unsigned long expires;
12671da177e4SLinus Torvalds 
1268c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
1269c15b1ccaSHannes Frederic Sowa 
12708e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
12714c5ff6a6SHerbert Xu 	state = ifp->state;
1272e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
12738e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
12744c5ff6a6SHerbert Xu 
12754c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
12764c5ff6a6SHerbert Xu 		goto out;
12771da177e4SLinus Torvalds 
12785c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
12795c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
12805c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
12811da177e4SLinus Torvalds 
12825b84efecSThomas Haller 	write_lock_bh(&ifp->idev->lock);
12835d9efa7eSDavid S. Miller 
12841da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
1285372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
12861da177e4SLinus Torvalds 		if (ifp->ifpub) {
12871da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
12881da177e4SLinus Torvalds 			ifp->ifpub = NULL;
12891da177e4SLinus Torvalds 		}
12901da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
12911da177e4SLinus Torvalds 	}
12921da177e4SLinus Torvalds 
12935b84efecSThomas Haller 	if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
12945b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &expires);
12955b84efecSThomas Haller 
12968ef802aaSEric Dumazet 	list_del_rcu(&ifp->if_list);
12971da177e4SLinus Torvalds 	__in6_ifa_put(ifp);
1298502a2ffdSstephen hemminger 
12995b84efecSThomas Haller 	write_unlock_bh(&ifp->idev->lock);
13001da177e4SLinus Torvalds 
1301c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
1302b2238566SAndrey Vagin 
13031da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
13041da177e4SLinus Torvalds 
1305f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
13061da177e4SLinus Torvalds 
13075b84efecSThomas Haller 	if (action != CLEANUP_PREFIX_RT_NOP) {
13085b84efecSThomas Haller 		cleanup_prefix_route(ifp, expires,
1309d0098e4cSHangbin Liu 			action == CLEANUP_PREFIX_RT_DEL, false);
13101da177e4SLinus Torvalds 	}
13111da177e4SLinus Torvalds 
1312c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1313c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
13144c5ff6a6SHerbert Xu out:
13151da177e4SLinus Torvalds 	in6_ifa_put(ifp);
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
1318969c5464SFernando Gont static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, bool block)
13191da177e4SLinus Torvalds {
13201da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
1321e6464b8cSDavid Ahern 	unsigned long tmp_tstamp, age;
1322eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
132376f793e3SLorenzo Colitti 	unsigned long now = jiffies;
13247aa8e63fSJiri Bohac 	s32 cnf_temp_preferred_lft;
1325969c5464SFernando Gont 	struct inet6_ifaddr *ift;
1326969c5464SFernando Gont 	struct ifa6_config cfg;
1327969c5464SFernando Gont 	long max_desync_factor;
1328969c5464SFernando Gont 	struct in6_addr addr;
1329969c5464SFernando Gont 	int ret = 0;
13301da177e4SLinus Torvalds 
133153bd6749SJiri Pirko 	write_lock_bh(&idev->lock);
1332969c5464SFernando Gont 
13331da177e4SLinus Torvalds retry:
13341da177e4SLinus Torvalds 	in6_dev_hold(idev);
13351da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
133653bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1337f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
13381da177e4SLinus Torvalds 		in6_dev_put(idev);
13391da177e4SLinus Torvalds 		ret = -1;
13401da177e4SLinus Torvalds 		goto out;
13411da177e4SLinus Torvalds 	}
13421da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
13431da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
13441da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
13451da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
134653bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1347f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1348f3213831SJoe Perches 			__func__);
13491da177e4SLinus Torvalds 		in6_dev_put(idev);
13501da177e4SLinus Torvalds 		ret = -1;
13511da177e4SLinus Torvalds 		goto out;
13521da177e4SLinus Torvalds 	}
13531da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
13541da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1355969c5464SFernando Gont 	ipv6_gen_rnd_iid(&addr);
1356969c5464SFernando Gont 
135776f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
13581da177e4SLinus Torvalds 
1359eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1360eac55bf9SBenoit Boissinot 			idev->cnf.dad_transmits *
136119e16d22SHangbin Liu 			max(NEIGH_VAR(idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
136276506a98SJiri Bohac 
136376506a98SJiri Bohac 	/* recalculate max_desync_factor each time and update
136476506a98SJiri Bohac 	 * idev->desync_factor if it's larger
136576506a98SJiri Bohac 	 */
13667aa8e63fSJiri Bohac 	cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
136776506a98SJiri Bohac 	max_desync_factor = min_t(__u32,
136876506a98SJiri Bohac 				  idev->cnf.max_desync_factor,
13697aa8e63fSJiri Bohac 				  cnf_temp_preferred_lft - regen_advance);
137076506a98SJiri Bohac 
137176506a98SJiri Bohac 	if (unlikely(idev->desync_factor > max_desync_factor)) {
137276506a98SJiri Bohac 		if (max_desync_factor > 0) {
137376506a98SJiri Bohac 			get_random_bytes(&idev->desync_factor,
137476506a98SJiri Bohac 					 sizeof(idev->desync_factor));
137576506a98SJiri Bohac 			idev->desync_factor %= max_desync_factor;
137676506a98SJiri Bohac 		} else {
137776506a98SJiri Bohac 			idev->desync_factor = 0;
137876506a98SJiri Bohac 		}
137976506a98SJiri Bohac 	}
138076506a98SJiri Bohac 
13813f2d67b6SDavid Ahern 	memset(&cfg, 0, sizeof(cfg));
1382e6464b8cSDavid Ahern 	cfg.valid_lft = min_t(__u32, ifp->valid_lft,
138376506a98SJiri Bohac 			      idev->cnf.temp_valid_lft + age);
1384e6464b8cSDavid Ahern 	cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1385e6464b8cSDavid Ahern 	cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
1386e6464b8cSDavid Ahern 
1387e6464b8cSDavid Ahern 	cfg.plen = ifp->prefix_len;
138876506a98SJiri Bohac 	tmp_tstamp = ifp->tstamp;
138976506a98SJiri Bohac 	spin_unlock_bh(&ifp->lock);
139076506a98SJiri Bohac 
139153bd6749SJiri Pirko 	write_unlock_bh(&idev->lock);
139295c385b4SNeil Horman 
1393eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1394eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1395eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1396eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1397ecab6701SHeiner Kallweit 	 * Use age calculation as in addrconf_verify to avoid unnecessary
1398ecab6701SHeiner Kallweit 	 * temporary addresses being generated.
1399eac55bf9SBenoit Boissinot 	 */
1400ecab6701SHeiner Kallweit 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1401e6464b8cSDavid Ahern 	if (cfg.preferred_lft <= regen_advance + age) {
1402eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1403eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1404eac55bf9SBenoit Boissinot 		ret = -1;
1405eac55bf9SBenoit Boissinot 		goto out;
1406eac55bf9SBenoit Boissinot 	}
1407eac55bf9SBenoit Boissinot 
1408e6464b8cSDavid Ahern 	cfg.ifa_flags = IFA_F_TEMPORARY;
140995c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
141095c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
1411e6464b8cSDavid Ahern 		cfg.ifa_flags |= IFA_F_OPTIMISTIC;
141295c385b4SNeil Horman 
1413e6464b8cSDavid Ahern 	cfg.pfx = &addr;
1414e6464b8cSDavid Ahern 	cfg.scope = ipv6_addr_scope(cfg.pfx);
1415e6464b8cSDavid Ahern 
1416e6464b8cSDavid Ahern 	ift = ipv6_add_addr(idev, &cfg, block, NULL);
14174b08a8f1SHannes Frederic Sowa 	if (IS_ERR(ift)) {
14181da177e4SLinus Torvalds 		in6_ifa_put(ifp);
14191da177e4SLinus Torvalds 		in6_dev_put(idev);
1420f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
142153bd6749SJiri Pirko 		write_lock_bh(&idev->lock);
14221da177e4SLinus Torvalds 		goto retry;
14231da177e4SLinus Torvalds 	}
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
14261da177e4SLinus Torvalds 	ift->ifpub = ifp;
142776f793e3SLorenzo Colitti 	ift->cstamp = now;
14281da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
14291da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
14301da177e4SLinus Torvalds 
1431cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
14321da177e4SLinus Torvalds 	in6_ifa_put(ift);
14331da177e4SLinus Torvalds 	in6_dev_put(idev);
14341da177e4SLinus Torvalds out:
14351da177e4SLinus Torvalds 	return ret;
14361da177e4SLinus Torvalds }
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds /*
1439072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
14401da177e4SLinus Torvalds  */
1441a9b05723SYOSHIFUJI Hideaki enum {
1442a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1443a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1444a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1445a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1446a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1447a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1448a9b05723SYOSHIFUJI Hideaki #endif
1449a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1450a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1451a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1452a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1453a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
14547fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
14557fd2561eSErik Kline 	IPV6_SADDR_RULE_NOT_OPTIMISTIC,
14567fd2561eSErik Kline #endif
1457a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1458072047e4SYOSHIFUJI Hideaki };
1459072047e4SYOSHIFUJI Hideaki 
1460a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1461a9b05723SYOSHIFUJI Hideaki 	int			rule;
1462a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1463a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1464a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1465a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1466a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1467a9b05723SYOSHIFUJI Hideaki };
1468a9b05723SYOSHIFUJI Hideaki 
1469a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
14709acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1471a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1472a9b05723SYOSHIFUJI Hideaki 	int scope;
1473a9b05723SYOSHIFUJI Hideaki 	int label;
14747cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1475a9b05723SYOSHIFUJI Hideaki };
1476072047e4SYOSHIFUJI Hideaki 
1477b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
14781da177e4SLinus Torvalds {
147945bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1480072047e4SYOSHIFUJI Hideaki 		return 1;
1481072047e4SYOSHIFUJI Hideaki 	return 0;
14821da177e4SLinus Torvalds }
14831da177e4SLinus Torvalds 
148435e015e1SMatteo Croce static bool ipv6_use_optimistic_addr(struct net *net,
148535e015e1SMatteo Croce 				     struct inet6_dev *idev)
14867fd2561eSErik Kline {
14877fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
148835e015e1SMatteo Croce 	if (!idev)
148935e015e1SMatteo Croce 		return false;
149035e015e1SMatteo Croce 	if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
149135e015e1SMatteo Croce 		return false;
149235e015e1SMatteo Croce 	if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
149335e015e1SMatteo Croce 		return false;
149435e015e1SMatteo Croce 
149535e015e1SMatteo Croce 	return true;
14967fd2561eSErik Kline #else
14977fd2561eSErik Kline 	return false;
14987fd2561eSErik Kline #endif
14997fd2561eSErik Kline }
15007fd2561eSErik Kline 
1501f1c02cfbSSabrina Dubroca static bool ipv6_allow_optimistic_dad(struct net *net,
1502f1c02cfbSSabrina Dubroca 				      struct inet6_dev *idev)
1503f1c02cfbSSabrina Dubroca {
1504f1c02cfbSSabrina Dubroca #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1505f1c02cfbSSabrina Dubroca 	if (!idev)
1506f1c02cfbSSabrina Dubroca 		return false;
1507f1c02cfbSSabrina Dubroca 	if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1508f1c02cfbSSabrina Dubroca 		return false;
1509f1c02cfbSSabrina Dubroca 
1510f1c02cfbSSabrina Dubroca 	return true;
1511f1c02cfbSSabrina Dubroca #else
1512f1c02cfbSSabrina Dubroca 	return false;
1513f1c02cfbSSabrina Dubroca #endif
1514f1c02cfbSSabrina Dubroca }
1515f1c02cfbSSabrina Dubroca 
15163de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
15173de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1518a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1519a9b05723SYOSHIFUJI Hideaki 			       int i)
1520a9b05723SYOSHIFUJI Hideaki {
1521a9b05723SYOSHIFUJI Hideaki 	int ret;
1522a9b05723SYOSHIFUJI Hideaki 
1523a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1524a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1525a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1526a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1527a9b05723SYOSHIFUJI Hideaki 			break;
1528a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1529a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1530a9b05723SYOSHIFUJI Hideaki 			break;
1531a9b05723SYOSHIFUJI Hideaki 		default:
1532a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1533a9b05723SYOSHIFUJI Hideaki 		}
1534a9b05723SYOSHIFUJI Hideaki 		goto out;
1535a9b05723SYOSHIFUJI Hideaki 	}
1536a9b05723SYOSHIFUJI Hideaki 
1537a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1538a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1539a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1540a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1541a9b05723SYOSHIFUJI Hideaki 		break;
1542a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1543a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1544a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1545a9b05723SYOSHIFUJI Hideaki 		break;
1546a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1547a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1548a9b05723SYOSHIFUJI Hideaki 		 *
1549a9b05723SYOSHIFUJI Hideaki 		 *      ret
1550a9b05723SYOSHIFUJI Hideaki 		 *       ^
1551a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1552a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1553a9b05723SYOSHIFUJI Hideaki 		 *       |
1554a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1555a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1556a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1557db9c7c39Sstephen hemminger 		 *  B-15 |    \        if scope is enough for destination.
1558a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1559a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1560a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1561a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1562a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1563a9b05723SYOSHIFUJI Hideaki 		 *
1564a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1565a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1566a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1567a9b05723SYOSHIFUJI Hideaki 		 */
1568a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1569a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1570a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1571a9b05723SYOSHIFUJI Hideaki 		else
1572a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1573a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1574a9b05723SYOSHIFUJI Hideaki 		break;
1575a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
15767fd2561eSErik Kline 	    {
1577a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
15787fd2561eSErik Kline 		u8 avoid = IFA_F_DEPRECATED;
15797fd2561eSErik Kline 
158035e015e1SMatteo Croce 		if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
15817fd2561eSErik Kline 			avoid |= IFA_F_OPTIMISTIC;
1582a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
15837fd2561eSErik Kline 		      !(score->ifa->flags & avoid);
1584a9b05723SYOSHIFUJI Hideaki 		break;
15857fd2561eSErik Kline 	    }
1586a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1587a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
15887cbca67cSYOSHIFUJI Hideaki 	    {
1589a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
15907cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
15917cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1592a9b05723SYOSHIFUJI Hideaki 		break;
15937cbca67cSYOSHIFUJI Hideaki 	    }
1594a9b05723SYOSHIFUJI Hideaki #endif
1595a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1596a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1597a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1598a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1599a9b05723SYOSHIFUJI Hideaki 		break;
1600a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1601a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
16023de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
16033de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1604a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1605a9b05723SYOSHIFUJI Hideaki 		break;
1606a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
16077cbca67cSYOSHIFUJI Hideaki 	    {
1608a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
160925985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1610a9b05723SYOSHIFUJI Hideaki 		 */
16117cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
16127cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
16137cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
16147cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1615a9b05723SYOSHIFUJI Hideaki 		break;
16167cbca67cSYOSHIFUJI Hideaki 	    }
1617a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1618a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1619a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1620a9b05723SYOSHIFUJI Hideaki 		 */
1621a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1622a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1623a9b05723SYOSHIFUJI Hideaki 		break;
1624a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1625a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
162691b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
162791b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
162891b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
162991b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1630a9b05723SYOSHIFUJI Hideaki 		break;
16317fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
16327fd2561eSErik Kline 	case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
16337fd2561eSErik Kline 		/* Optimistic addresses still have lower precedence than other
16347fd2561eSErik Kline 		 * preferred addresses.
16357fd2561eSErik Kline 		 */
16367fd2561eSErik Kline 		ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
16377fd2561eSErik Kline 		break;
16387fd2561eSErik Kline #endif
1639a9b05723SYOSHIFUJI Hideaki 	default:
1640a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1641a9b05723SYOSHIFUJI Hideaki 	}
1642a9b05723SYOSHIFUJI Hideaki 
1643a9b05723SYOSHIFUJI Hideaki 	if (ret)
1644a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1645a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1646a9b05723SYOSHIFUJI Hideaki out:
1647a9b05723SYOSHIFUJI Hideaki 	return ret;
1648a9b05723SYOSHIFUJI Hideaki }
1649a9b05723SYOSHIFUJI Hideaki 
1650c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net,
16519131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_dst *dst,
16529131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct inet6_dev *idev,
1653c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_score *scores,
1654c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				int hiscore_idx)
16551da177e4SLinus Torvalds {
1656c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1657072047e4SYOSHIFUJI Hideaki 
1658f59c031eSEric Dumazet 	list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
1659a9b05723SYOSHIFUJI Hideaki 		int i;
1660072047e4SYOSHIFUJI Hideaki 
1661a9b05723SYOSHIFUJI Hideaki 		/*
16626b3ae80aSYOSHIFUJI Hideaki 		 * - Tentative Address (RFC2462 section 5.4)
16636b3ae80aSYOSHIFUJI Hideaki 		 *  - A tentative address is not considered
16646b3ae80aSYOSHIFUJI Hideaki 		 *    "assigned to an interface" in the traditional
166595c385b4SNeil Horman 		 *    sense, unless it is also flagged as optimistic.
16666b3ae80aSYOSHIFUJI Hideaki 		 * - Candidate Source Address (section 4)
1667072047e4SYOSHIFUJI Hideaki 		 *  - In any case, anycast addresses, multicast
1668072047e4SYOSHIFUJI Hideaki 		 *    addresses, and the unspecified address MUST
1669072047e4SYOSHIFUJI Hideaki 		 *    NOT be included in a candidate set.
1670072047e4SYOSHIFUJI Hideaki 		 */
1671a9b05723SYOSHIFUJI Hideaki 		if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1672a9b05723SYOSHIFUJI Hideaki 		    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
16736b3ae80aSYOSHIFUJI Hideaki 			continue;
1674a9b05723SYOSHIFUJI Hideaki 
1675a9b05723SYOSHIFUJI Hideaki 		score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1676a9b05723SYOSHIFUJI Hideaki 
1677a9b05723SYOSHIFUJI Hideaki 		if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1678a9b05723SYOSHIFUJI Hideaki 			     score->addr_type & IPV6_ADDR_MULTICAST)) {
1679ba7a46f1SJoe Perches 			net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
16809131f3deSYOSHIFUJI Hideaki/吉藤英明 					    idev->dev->name);
16811da177e4SLinus Torvalds 			continue;
1682072047e4SYOSHIFUJI Hideaki 		}
1683072047e4SYOSHIFUJI Hideaki 
1684a9b05723SYOSHIFUJI Hideaki 		score->rule = -1;
1685a9b05723SYOSHIFUJI Hideaki 		bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1686072047e4SYOSHIFUJI Hideaki 
1687a9b05723SYOSHIFUJI Hideaki 		for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1688a9b05723SYOSHIFUJI Hideaki 			int minihiscore, miniscore;
1689072047e4SYOSHIFUJI Hideaki 
16909131f3deSYOSHIFUJI Hideaki/吉藤英明 			minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
16919131f3deSYOSHIFUJI Hideaki/吉藤英明 			miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1692072047e4SYOSHIFUJI Hideaki 
1693a9b05723SYOSHIFUJI Hideaki 			if (minihiscore > miniscore) {
1694a9b05723SYOSHIFUJI Hideaki 				if (i == IPV6_SADDR_RULE_SCOPE &&
1695a9b05723SYOSHIFUJI Hideaki 				    score->scopedist > 0) {
1696a9b05723SYOSHIFUJI Hideaki 					/*
1697a9b05723SYOSHIFUJI Hideaki 					 * special case:
1698a9b05723SYOSHIFUJI Hideaki 					 * each remaining entry
1699a9b05723SYOSHIFUJI Hideaki 					 * has too small (not enough)
1700a9b05723SYOSHIFUJI Hideaki 					 * scope, because ifa entries
1701a9b05723SYOSHIFUJI Hideaki 					 * are sorted by their scope
1702a9b05723SYOSHIFUJI Hideaki 					 * values.
1703072047e4SYOSHIFUJI Hideaki 					 */
17049131f3deSYOSHIFUJI Hideaki/吉藤英明 					goto out;
1705072047e4SYOSHIFUJI Hideaki 				}
1706a9b05723SYOSHIFUJI Hideaki 				break;
1707a9b05723SYOSHIFUJI Hideaki 			} else if (minihiscore < miniscore) {
1708a0bffffcSIlpo Järvinen 				swap(hiscore, score);
1709c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				hiscore_idx = 1 - hiscore_idx;
1710a9b05723SYOSHIFUJI Hideaki 
1711a9b05723SYOSHIFUJI Hideaki 				/* restore our iterator */
1712a9b05723SYOSHIFUJI Hideaki 				score->ifa = hiscore->ifa;
1713a9b05723SYOSHIFUJI Hideaki 
1714a9b05723SYOSHIFUJI Hideaki 				break;
17151da177e4SLinus Torvalds 			}
1716a9b05723SYOSHIFUJI Hideaki 		}
1717a9b05723SYOSHIFUJI Hideaki 	}
17189131f3deSYOSHIFUJI Hideaki/吉藤英明 out:
1719c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	return hiscore_idx;
17201da177e4SLinus Torvalds }
17219131f3deSYOSHIFUJI Hideaki/吉藤英明 
1722afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net,
1723afbac601SDavid Ahern 				 const struct net_device *dst_dev,
1724afbac601SDavid Ahern 				 const struct net_device *master,
1725afbac601SDavid Ahern 				 struct ipv6_saddr_dst *dst,
1726afbac601SDavid Ahern 				 struct ipv6_saddr_score *scores,
1727afbac601SDavid Ahern 				 int hiscore_idx)
1728afbac601SDavid Ahern {
1729afbac601SDavid Ahern 	struct inet6_dev *idev;
1730afbac601SDavid Ahern 
1731afbac601SDavid Ahern 	idev = __in6_dev_get(dst_dev);
1732afbac601SDavid Ahern 	if (idev)
1733afbac601SDavid Ahern 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1734afbac601SDavid Ahern 						   scores, hiscore_idx);
1735afbac601SDavid Ahern 
1736afbac601SDavid Ahern 	idev = __in6_dev_get(master);
1737afbac601SDavid Ahern 	if (idev)
1738afbac601SDavid Ahern 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1739afbac601SDavid Ahern 						   scores, hiscore_idx);
1740afbac601SDavid Ahern 
1741afbac601SDavid Ahern 	return hiscore_idx;
1742afbac601SDavid Ahern }
1743afbac601SDavid Ahern 
17449131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
17459131f3deSYOSHIFUJI Hideaki/吉藤英明 		       const struct in6_addr *daddr, unsigned int prefs,
17469131f3deSYOSHIFUJI Hideaki/吉藤英明 		       struct in6_addr *saddr)
17479131f3deSYOSHIFUJI Hideaki/吉藤英明 {
1748c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score scores[2], *hiscore;
17499131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_dst dst;
17509131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct inet6_dev *idev;
17519131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct net_device *dev;
17529131f3deSYOSHIFUJI Hideaki/吉藤英明 	int dst_type;
17539131f3deSYOSHIFUJI Hideaki/吉藤英明 	bool use_oif_addr = false;
1754c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	int hiscore_idx = 0;
1755cc429c8fSEric Dumazet 	int ret = 0;
17569131f3deSYOSHIFUJI Hideaki/吉藤英明 
17579131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst_type = __ipv6_addr_type(daddr);
17589131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.addr = daddr;
17599131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
17609131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.scope = __ipv6_addr_src_scope(dst_type);
17619131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
17629131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.prefs = prefs;
17639131f3deSYOSHIFUJI Hideaki/吉藤英明 
1764c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].rule = -1;
1765c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].ifa = NULL;
17669131f3deSYOSHIFUJI Hideaki/吉藤英明 
17679131f3deSYOSHIFUJI Hideaki/吉藤英明 	rcu_read_lock();
17689131f3deSYOSHIFUJI Hideaki/吉藤英明 
17699131f3deSYOSHIFUJI Hideaki/吉藤英明 	/* Candidate Source Address (section 4)
17709131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *  - multicast and link-local destination address,
17719131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    the set of candidate source address MUST only
17729131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
17739131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same link as the outgoing
17749131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.
17759131f3deSYOSHIFUJI Hideaki/吉藤英明 	 * (- For site-local destination addresses, the
17769131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    set of candidate source addresses MUST only
17779131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
17789131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same site as the outgoing
17799131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.)
17803985e8a3SErik Kline 	 *  - "It is RECOMMENDED that the candidate source addresses
17813985e8a3SErik Kline 	 *    be the set of unicast addresses assigned to the
17823985e8a3SErik Kline 	 *    interface that will be used to send to the destination
17833985e8a3SErik Kline 	 *    (the 'outgoing' interface)." (RFC 6724)
17849131f3deSYOSHIFUJI Hideaki/吉藤英明 	 */
17859131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (dst_dev) {
17869131f3deSYOSHIFUJI Hideaki/吉藤英明 		idev = __in6_dev_get(dst_dev);
17873985e8a3SErik Kline 		if ((dst_type & IPV6_ADDR_MULTICAST) ||
17883985e8a3SErik Kline 		    dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
17893985e8a3SErik Kline 		    (idev && idev->cnf.use_oif_addrs_only)) {
17909131f3deSYOSHIFUJI Hideaki/吉藤英明 			use_oif_addr = true;
17919131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
17929131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
17939131f3deSYOSHIFUJI Hideaki/吉藤英明 
17949131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (use_oif_addr) {
1795c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 		if (idev)
1796c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
17979131f3deSYOSHIFUJI Hideaki/吉藤英明 	} else {
1798afbac601SDavid Ahern 		const struct net_device *master;
1799afbac601SDavid Ahern 		int master_idx = 0;
1800afbac601SDavid Ahern 
1801afbac601SDavid Ahern 		/* if dst_dev exists and is enslaved to an L3 device, then
1802afbac601SDavid Ahern 		 * prefer addresses from dst_dev and then the master over
1803afbac601SDavid Ahern 		 * any other enslaved devices in the L3 domain.
1804afbac601SDavid Ahern 		 */
1805afbac601SDavid Ahern 		master = l3mdev_master_dev_rcu(dst_dev);
1806afbac601SDavid Ahern 		if (master) {
1807afbac601SDavid Ahern 			master_idx = master->ifindex;
1808afbac601SDavid Ahern 
1809afbac601SDavid Ahern 			hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1810afbac601SDavid Ahern 							    master, &dst,
1811afbac601SDavid Ahern 							    scores, hiscore_idx);
1812afbac601SDavid Ahern 
1813afbac601SDavid Ahern 			if (scores[hiscore_idx].ifa)
1814afbac601SDavid Ahern 				goto out;
1815afbac601SDavid Ahern 		}
1816afbac601SDavid Ahern 
18179131f3deSYOSHIFUJI Hideaki/吉藤英明 		for_each_netdev_rcu(net, dev) {
1818afbac601SDavid Ahern 			/* only consider addresses on devices in the
1819afbac601SDavid Ahern 			 * same L3 domain
1820afbac601SDavid Ahern 			 */
1821afbac601SDavid Ahern 			if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1822afbac601SDavid Ahern 				continue;
18239131f3deSYOSHIFUJI Hideaki/吉藤英明 			idev = __in6_dev_get(dev);
18249131f3deSYOSHIFUJI Hideaki/吉藤英明 			if (!idev)
18259131f3deSYOSHIFUJI Hideaki/吉藤英明 				continue;
1826c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
18279131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
18289131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
1829afbac601SDavid Ahern 
1830afbac601SDavid Ahern out:
1831c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	hiscore = &scores[hiscore_idx];
1832a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1833cc429c8fSEric Dumazet 		ret = -EADDRNOTAVAIL;
1834cc429c8fSEric Dumazet 	else
18354e3fd7a0SAlexey Dobriyan 		*saddr = hiscore->ifa->addr;
1836cc429c8fSEric Dumazet 
1837cc429c8fSEric Dumazet 	rcu_read_unlock();
1838cc429c8fSEric Dumazet 	return ret;
18391da177e4SLinus Torvalds }
18405e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
18411da177e4SLinus Torvalds 
18428965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1843479840ffSJiri Pirko 		      u32 banned_flags)
1844b7b1bfceSHannes Frederic Sowa {
1845b7b1bfceSHannes Frederic Sowa 	struct inet6_ifaddr *ifp;
1846b7b1bfceSHannes Frederic Sowa 	int err = -EADDRNOTAVAIL;
1847b7b1bfceSHannes Frederic Sowa 
1848602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1849602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1850602582caSHannes Frederic Sowa 			break;
1851b7b1bfceSHannes Frederic Sowa 		if (ifp->scope == IFA_LINK &&
1852b7b1bfceSHannes Frederic Sowa 		    !(ifp->flags & banned_flags)) {
1853b7b1bfceSHannes Frederic Sowa 			*addr = ifp->addr;
1854b7b1bfceSHannes Frederic Sowa 			err = 0;
1855b7b1bfceSHannes Frederic Sowa 			break;
1856b7b1bfceSHannes Frederic Sowa 		}
1857b7b1bfceSHannes Frederic Sowa 	}
1858b7b1bfceSHannes Frederic Sowa 	return err;
1859b7b1bfceSHannes Frederic Sowa }
1860b7b1bfceSHannes Frederic Sowa 
186195c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1862479840ffSJiri Pirko 		    u32 banned_flags)
18631da177e4SLinus Torvalds {
18641da177e4SLinus Torvalds 	struct inet6_dev *idev;
18651da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
18661da177e4SLinus Torvalds 
18678814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1868e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1869e21e8467SStephen Hemminger 	if (idev) {
18701da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1871b7b1bfceSHannes Frederic Sowa 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
18721da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
18731da177e4SLinus Torvalds 	}
18748814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
18751da177e4SLinus Torvalds 	return err;
18761da177e4SLinus Torvalds }
18771da177e4SLinus Torvalds 
1878d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev)
18791da177e4SLinus Torvalds {
1880d9bf82c2SEric Dumazet 	const struct inet6_ifaddr *ifp;
18811da177e4SLinus Torvalds 	int cnt = 0;
18821da177e4SLinus Torvalds 
1883d9bf82c2SEric Dumazet 	rcu_read_lock();
1884d9bf82c2SEric Dumazet 	list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
18851da177e4SLinus Torvalds 		cnt++;
1886d9bf82c2SEric Dumazet 	rcu_read_unlock();
18871da177e4SLinus Torvalds 	return cnt;
18881da177e4SLinus Torvalds }
18891da177e4SLinus Torvalds 
1890b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
18912a7851bfSFlorian Westphal 		  const struct net_device *dev, int strict)
18921da177e4SLinus Torvalds {
1893232378e8SDavid Ahern 	return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1894232378e8SDavid Ahern 				       strict, IFA_F_TENTATIVE);
1895c58da4c6SErik Kline }
1896c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr);
1897c58da4c6SErik Kline 
18981893ff20SDavid Ahern /* device argument is used to find the L3 domain of interest. If
18991893ff20SDavid Ahern  * skip_dev_check is set, then the ifp device is not checked against
19001893ff20SDavid Ahern  * the passed in dev argument. So the 2 cases for addresses checks are:
19011893ff20SDavid Ahern  *   1. does the address exist in the L3 domain that dev is part of
19021893ff20SDavid Ahern  *      (skip_dev_check = true), or
19031893ff20SDavid Ahern  *
19041893ff20SDavid Ahern  *   2. does the address exist on the specific device
19051893ff20SDavid Ahern  *      (skip_dev_check = false)
19061893ff20SDavid Ahern  */
19074ef1a7cbSXin Long static struct net_device *
19084ef1a7cbSXin Long __ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1909232378e8SDavid Ahern 			  const struct net_device *dev, bool skip_dev_check,
1910232378e8SDavid Ahern 			  int strict, u32 banned_flags)
1911c58da4c6SErik Kline {
19123f27fb23SEric Dumazet 	unsigned int hash = inet6_addr_hash(net, addr);
19134ef1a7cbSXin Long 	struct net_device *l3mdev, *ndev;
1914eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1915c58da4c6SErik Kline 	u32 ifp_flags;
19161da177e4SLinus Torvalds 
1917480318a0SEric Dumazet 	rcu_read_lock();
1918232378e8SDavid Ahern 
19191893ff20SDavid Ahern 	l3mdev = l3mdev_master_dev_rcu(dev);
1920232378e8SDavid Ahern 	if (skip_dev_check)
1921232378e8SDavid Ahern 		dev = NULL;
1922232378e8SDavid Ahern 
1923b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
19244ef1a7cbSXin Long 		ndev = ifp->idev->dev;
19254ef1a7cbSXin Long 		if (!net_eq(dev_net(ndev), net))
1926bfeade08SDaniel Lezcano 			continue;
19271893ff20SDavid Ahern 
19284ef1a7cbSXin Long 		if (l3mdev_master_dev_rcu(ndev) != l3mdev)
19291893ff20SDavid Ahern 			continue;
19301893ff20SDavid Ahern 
1931c58da4c6SErik Kline 		/* Decouple optimistic from tentative for evaluation here.
1932c58da4c6SErik Kline 		 * Ban optimistic addresses explicitly, when required.
1933c58da4c6SErik Kline 		 */
1934c58da4c6SErik Kline 		ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1935c58da4c6SErik Kline 			    ? (ifp->flags&~IFA_F_TENTATIVE)
1936c58da4c6SErik Kline 			    : ifp->flags;
19371da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1938c58da4c6SErik Kline 		    !(ifp_flags&banned_flags) &&
19394ef1a7cbSXin Long 		    (!dev || ndev == dev ||
1940eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1941480318a0SEric Dumazet 			rcu_read_unlock();
19424ef1a7cbSXin Long 			return ndev;
1943eedf042aSStephen Hemminger 		}
1944eedf042aSStephen Hemminger 	}
19455c578aedSstephen hemminger 
1946480318a0SEric Dumazet 	rcu_read_unlock();
19474ef1a7cbSXin Long 	return NULL;
19484ef1a7cbSXin Long }
19494ef1a7cbSXin Long 
19504ef1a7cbSXin Long int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
19514ef1a7cbSXin Long 			    const struct net_device *dev, bool skip_dev_check,
19524ef1a7cbSXin Long 			    int strict, u32 banned_flags)
19534ef1a7cbSXin Long {
19544ef1a7cbSXin Long 	return __ipv6_chk_addr_and_flags(net, addr, dev, skip_dev_check,
19554ef1a7cbSXin Long 					 strict, banned_flags) ? 1 : 0;
19561da177e4SLinus Torvalds }
1957c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
19587159039aSYOSHIFUJI Hideaki 
19591da177e4SLinus Torvalds 
19607df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev.
19617df37ff3SCatalin\(ux\) M. BOIE  * If one is found it returns true.
19627df37ff3SCatalin\(ux\) M. BOIE  */
19637df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
19647df37ff3SCatalin\(ux\) M. BOIE 	const unsigned int prefix_len, struct net_device *dev)
19657df37ff3SCatalin\(ux\) M. BOIE {
196647e26941SEric Dumazet 	const struct inet6_ifaddr *ifa;
196747e26941SEric Dumazet 	const struct inet6_dev *idev;
19687df37ff3SCatalin\(ux\) M. BOIE 	bool ret = false;
19697df37ff3SCatalin\(ux\) M. BOIE 
19707df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_lock();
19717df37ff3SCatalin\(ux\) M. BOIE 	idev = __in6_dev_get(dev);
19727df37ff3SCatalin\(ux\) M. BOIE 	if (idev) {
197347e26941SEric Dumazet 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
19747df37ff3SCatalin\(ux\) M. BOIE 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
19757df37ff3SCatalin\(ux\) M. BOIE 			if (ret)
19767df37ff3SCatalin\(ux\) M. BOIE 				break;
19777df37ff3SCatalin\(ux\) M. BOIE 		}
19787df37ff3SCatalin\(ux\) M. BOIE 	}
19797df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_unlock();
19807df37ff3SCatalin\(ux\) M. BOIE 
19817df37ff3SCatalin\(ux\) M. BOIE 	return ret;
19827df37ff3SCatalin\(ux\) M. BOIE }
19837df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix);
19847df37ff3SCatalin\(ux\) M. BOIE 
1985b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
198652eeeb84SYOSHIFUJI Hideaki {
198724ba333bSEric Dumazet 	const struct inet6_ifaddr *ifa;
198824ba333bSEric Dumazet 	const struct inet6_dev *idev;
198952eeeb84SYOSHIFUJI Hideaki 	int	onlink;
199052eeeb84SYOSHIFUJI Hideaki 
199152eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
199252eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
199352eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
199452eeeb84SYOSHIFUJI Hideaki 	if (idev) {
199524ba333bSEric Dumazet 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
199652eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
199752eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
199852eeeb84SYOSHIFUJI Hideaki 			if (onlink)
199952eeeb84SYOSHIFUJI Hideaki 				break;
200052eeeb84SYOSHIFUJI Hideaki 		}
200152eeeb84SYOSHIFUJI Hideaki 	}
200252eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
200352eeeb84SYOSHIFUJI Hideaki 	return onlink;
200452eeeb84SYOSHIFUJI Hideaki }
200552eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
200652eeeb84SYOSHIFUJI Hideaki 
200781f6cb31SXin Long /**
200881f6cb31SXin Long  * ipv6_dev_find - find the first device with a given source address.
200981f6cb31SXin Long  * @net: the net namespace
201081f6cb31SXin Long  * @addr: the source address
20112c4de211SXin Long  * @dev: used to find the L3 domain of interest
201281f6cb31SXin Long  *
201381f6cb31SXin Long  * The caller should be protected by RCU, or RTNL.
201481f6cb31SXin Long  */
20154ef1a7cbSXin Long struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr,
20164ef1a7cbSXin Long 				 struct net_device *dev)
201781f6cb31SXin Long {
20184ef1a7cbSXin Long 	return __ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1,
20194ef1a7cbSXin Long 					 IFA_F_TENTATIVE);
202081f6cb31SXin Long }
202181f6cb31SXin Long EXPORT_SYMBOL(ipv6_dev_find);
202281f6cb31SXin Long 
20239acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
20241cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
20251da177e4SLinus Torvalds {
20263f27fb23SEric Dumazet 	unsigned int hash = inet6_addr_hash(net, addr);
2027b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
20281da177e4SLinus Torvalds 
202924f226daSEric Dumazet 	rcu_read_lock();
203024f226daSEric Dumazet 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
2031878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
20321cab3da6SDaniel Lezcano 			continue;
20331da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
203463159f29SIan Morris 			if (!dev || ifp->idev->dev == dev ||
20351da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
2036b79d1d54SDavid S. Miller 				result = ifp;
20371da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
20381da177e4SLinus Torvalds 				break;
20391da177e4SLinus Torvalds 			}
20401da177e4SLinus Torvalds 		}
20411da177e4SLinus Torvalds 	}
204224f226daSEric Dumazet 	rcu_read_unlock();
20431da177e4SLinus Torvalds 
2044b79d1d54SDavid S. Miller 	return result;
20451da177e4SLinus Torvalds }
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
20481da177e4SLinus Torvalds 
2049cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
20501da177e4SLinus Torvalds {
20513d171f39SLubomir Rintel 	if (dad_failed)
20523d171f39SLubomir Rintel 		ifp->flags |= IFA_F_DADFAILED;
20533d171f39SLubomir Rintel 
2054ec8add2aSSabrina Dubroca 	if (ifp->flags&IFA_F_TEMPORARY) {
20551da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
20561da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
20571da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
20581da177e4SLinus Torvalds 		if (ifpub) {
20591da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
20601da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
2061969c5464SFernando Gont 			ipv6_create_tempaddr(ifpub, true);
20621da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
20631da177e4SLinus Torvalds 		} else {
20641da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
20651da177e4SLinus Torvalds 		}
20661da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
2067ec8add2aSSabrina Dubroca 	} else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2068ec8add2aSSabrina Dubroca 		spin_lock_bh(&ifp->lock);
2069ec8add2aSSabrina Dubroca 		addrconf_del_dad_work(ifp);
2070ec8add2aSSabrina Dubroca 		ifp->flags |= IFA_F_TENTATIVE;
2071f1c02cfbSSabrina Dubroca 		if (dad_failed)
2072f1c02cfbSSabrina Dubroca 			ifp->flags &= ~IFA_F_OPTIMISTIC;
2073ec8add2aSSabrina Dubroca 		spin_unlock_bh(&ifp->lock);
2074ec8add2aSSabrina Dubroca 		if (dad_failed)
2075ec8add2aSSabrina Dubroca 			ipv6_ifa_notify(0, ifp);
2076ec8add2aSSabrina Dubroca 		in6_ifa_put(ifp);
2077c15b1ccaSHannes Frederic Sowa 	} else {
20781da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
20791da177e4SLinus Torvalds 	}
2080c15b1ccaSHannes Frederic Sowa }
20811da177e4SLinus Torvalds 
2082f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2083f2344a13SHerbert Xu {
2084f2344a13SHerbert Xu 	int err = -ENOENT;
2085f2344a13SHerbert Xu 
20868e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
2087f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
2088f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
2089f2344a13SHerbert Xu 		err = 0;
2090f2344a13SHerbert Xu 	}
20918e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
2092f2344a13SHerbert Xu 
2093f2344a13SHerbert Xu 	return err;
2094f2344a13SHerbert Xu }
2095f2344a13SHerbert Xu 
2096da13c59bSVishwanath Pai void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
20973c21edbdSYOSHIFUJI Hideaki {
20981b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
20991855b7c3SHannes Frederic Sowa 	struct net *net = dev_net(ifp->idev->dev);
21009bdd8d40SBrian Haley 
2101853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
2102853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
2103f2344a13SHerbert Xu 		return;
2104853dc2e0SUrsula Braun 	}
2105f2344a13SHerbert Xu 
2106da13c59bSVishwanath Pai 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2107da13c59bSVishwanath Pai 			     ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
21089bdd8d40SBrian Haley 
21095f40ef77SHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
21101b34be74SYOSHIFUJI Hideaki 
21115f40ef77SHannes Frederic Sowa 	if (ifp->flags & IFA_F_STABLE_PRIVACY) {
21125f40ef77SHannes Frederic Sowa 		struct in6_addr new_addr;
21135f40ef77SHannes Frederic Sowa 		struct inet6_ifaddr *ifp2;
21145f40ef77SHannes Frederic Sowa 		int retries = ifp->stable_privacy_retry + 1;
2115e6464b8cSDavid Ahern 		struct ifa6_config cfg = {
2116e6464b8cSDavid Ahern 			.pfx = &new_addr,
2117e6464b8cSDavid Ahern 			.plen = ifp->prefix_len,
2118e6464b8cSDavid Ahern 			.ifa_flags = ifp->flags,
2119e6464b8cSDavid Ahern 			.valid_lft = ifp->valid_lft,
2120e6464b8cSDavid Ahern 			.preferred_lft = ifp->prefered_lft,
2121e6464b8cSDavid Ahern 			.scope = ifp->scope,
2122e6464b8cSDavid Ahern 		};
21235f40ef77SHannes Frederic Sowa 
21241855b7c3SHannes Frederic Sowa 		if (retries > net->ipv6.sysctl.idgen_retries) {
21255f40ef77SHannes Frederic Sowa 			net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
21265f40ef77SHannes Frederic Sowa 					     ifp->idev->dev->name);
21275f40ef77SHannes Frederic Sowa 			goto errdad;
21285f40ef77SHannes Frederic Sowa 		}
21295f40ef77SHannes Frederic Sowa 
21305f40ef77SHannes Frederic Sowa 		new_addr = ifp->addr;
21315f40ef77SHannes Frederic Sowa 		if (ipv6_generate_stable_address(&new_addr, retries,
21325f40ef77SHannes Frederic Sowa 						 idev))
21335f40ef77SHannes Frederic Sowa 			goto errdad;
21345f40ef77SHannes Frederic Sowa 
21355f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp->lock);
21365f40ef77SHannes Frederic Sowa 
21375f40ef77SHannes Frederic Sowa 		if (idev->cnf.max_addresses &&
21385f40ef77SHannes Frederic Sowa 		    ipv6_count_addresses(idev) >=
21395f40ef77SHannes Frederic Sowa 		    idev->cnf.max_addresses)
21405f40ef77SHannes Frederic Sowa 			goto lock_errdad;
21415f40ef77SHannes Frederic Sowa 
21425f40ef77SHannes Frederic Sowa 		net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
21435f40ef77SHannes Frederic Sowa 				     ifp->idev->dev->name);
21445f40ef77SHannes Frederic Sowa 
2145e6464b8cSDavid Ahern 		ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
21465f40ef77SHannes Frederic Sowa 		if (IS_ERR(ifp2))
21475f40ef77SHannes Frederic Sowa 			goto lock_errdad;
21485f40ef77SHannes Frederic Sowa 
21495f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp2->lock);
21505f40ef77SHannes Frederic Sowa 		ifp2->stable_privacy_retry = retries;
21515f40ef77SHannes Frederic Sowa 		ifp2->state = INET6_IFADDR_STATE_PREDAD;
21525f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp2->lock);
21535f40ef77SHannes Frederic Sowa 
21541855b7c3SHannes Frederic Sowa 		addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
21555f40ef77SHannes Frederic Sowa 		in6_ifa_put(ifp2);
21565f40ef77SHannes Frederic Sowa lock_errdad:
21575f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp->lock);
21581b34be74SYOSHIFUJI Hideaki 	}
21591b34be74SYOSHIFUJI Hideaki 
21605f40ef77SHannes Frederic Sowa errdad:
2161c15b1ccaSHannes Frederic Sowa 	/* transition from _POSTDAD to _ERRDAD */
2162c15b1ccaSHannes Frederic Sowa 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
21638e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
2164c15b1ccaSHannes Frederic Sowa 
2165c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, 0);
2166751eb6b6SWei Yongjun 	in6_ifa_put(ifp);
21673c21edbdSYOSHIFUJI Hideaki }
21681da177e4SLinus Torvalds 
2169a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group.
2170a9ed4a29SSabrina Dubroca  * caller must hold RTNL */
2171b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
21721da177e4SLinus Torvalds {
21731da177e4SLinus Torvalds 	struct in6_addr maddr;
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
21761da177e4SLinus Torvalds 		return;
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
21791da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
21801da177e4SLinus Torvalds }
21811da177e4SLinus Torvalds 
2182a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
2183b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
21841da177e4SLinus Torvalds {
21851da177e4SLinus Torvalds 	struct in6_addr maddr;
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
21881da177e4SLinus Torvalds 		return;
21891da177e4SLinus Torvalds 
21901da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
21911da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
21921da177e4SLinus Torvalds }
21931da177e4SLinus Torvalds 
2194a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
219520380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
21961da177e4SLinus Torvalds {
21971da177e4SLinus Torvalds 	struct in6_addr addr;
2198c15b1ccaSHannes Frederic Sowa 
219988ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
22002bda8a0cSBjørn Mork 		return;
22011da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
22021da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
22031da177e4SLinus Torvalds 		return;
2204013b4d90SWANG Cong 	__ipv6_dev_ac_inc(ifp->idev, &addr);
22051da177e4SLinus Torvalds }
22061da177e4SLinus Torvalds 
2207a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
220820380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
22091da177e4SLinus Torvalds {
22101da177e4SLinus Torvalds 	struct in6_addr addr;
2211c15b1ccaSHannes Frederic Sowa 
221288ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
221332019e65SYOSHIFUJI Hideaki 		return;
22141da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
22151da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
22161da177e4SLinus Torvalds 		return;
22171da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
22181da177e4SLinus Torvalds }
22191da177e4SLinus Torvalds 
22208a7a4b47SAlexander Aring static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
222106a4c1c5Salex.bluesman.smirnov@gmail.com {
22228a7a4b47SAlexander Aring 	switch (dev->addr_len) {
22238a7a4b47SAlexander Aring 	case ETH_ALEN:
22249dae2e03SLuiz Augusto von Dentz 		memcpy(eui, dev->dev_addr, 3);
22259dae2e03SLuiz Augusto von Dentz 		eui[3] = 0xFF;
22269dae2e03SLuiz Augusto von Dentz 		eui[4] = 0xFE;
22279dae2e03SLuiz Augusto von Dentz 		memcpy(eui + 5, dev->dev_addr + 3, 3);
22289dae2e03SLuiz Augusto von Dentz 		break;
22298a7a4b47SAlexander Aring 	case EUI64_ADDR_LEN:
22305241c2d7SAlexander Aring 		memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
22315e98a36eSYOSHIFUJI Hideaki / 吉藤英明 		eui[0] ^= 2;
22328a7a4b47SAlexander Aring 		break;
22338a7a4b47SAlexander Aring 	default:
22348a7a4b47SAlexander Aring 		return -1;
22358a7a4b47SAlexander Aring 	}
22368a7a4b47SAlexander Aring 
223706a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
223806a4c1c5Salex.bluesman.smirnov@gmail.com }
223906a4c1c5Salex.bluesman.smirnov@gmail.com 
2240cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2241cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
22421a8a23d2SJakub Kicinski 	const union fwnet_hwaddr *ha;
2243cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2244cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
2245cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
2246cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
22471a8a23d2SJakub Kicinski 	ha = (const union fwnet_hwaddr *)dev->dev_addr;
2248cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2249cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2250cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
2251cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
2252cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
2253cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2254073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2255073a8e0eSYOSHIFUJI Hideaki {
22561da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
22571da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
22581da177e4SLinus Torvalds 		return -1;
22591da177e4SLinus Torvalds 	memset(eui, 0, 7);
22601da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
22611da177e4SLinus Torvalds 	return 0;
2262073a8e0eSYOSHIFUJI Hideaki }
2263073a8e0eSYOSHIFUJI Hideaki 
2264073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2265073a8e0eSYOSHIFUJI Hideaki {
22661da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
22671da177e4SLinus Torvalds 		return -1;
22681da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
22691da177e4SLinus Torvalds 	eui[0] |= 2;
22701da177e4SLinus Torvalds 	return 0;
22711da177e4SLinus Torvalds }
2272073a8e0eSYOSHIFUJI Hideaki 
2273c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2274dfd982baSYOSHIFUJI Hideaki {
22759af28511SSascha Hlusiak 	if (addr == 0)
22769af28511SSascha Hlusiak 		return -1;
2277dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2278dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2279dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2280dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2281dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2282dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2283dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
2284dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
2285dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
2286dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
2287dfd982baSYOSHIFUJI Hideaki 	return 0;
2288dfd982baSYOSHIFUJI Hideaki }
2289dfd982baSYOSHIFUJI Hideaki 
2290dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2291dfd982baSYOSHIFUJI Hideaki {
2292dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
2293dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2294dfd982baSYOSHIFUJI Hideaki 	return -1;
2295dfd982baSYOSHIFUJI Hideaki }
2296dfd982baSYOSHIFUJI Hideaki 
2297aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2298aee80b54Sstephen hemminger {
2299aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2300aee80b54Sstephen hemminger }
2301aee80b54Sstephen hemminger 
2302e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2303e837735eSNicolas Dichtel {
2304e837735eSNicolas Dichtel 	memcpy(eui, dev->perm_addr, 3);
2305e837735eSNicolas Dichtel 	memcpy(eui + 5, dev->perm_addr + 3, 3);
2306e837735eSNicolas Dichtel 	eui[3] = 0xFF;
2307e837735eSNicolas Dichtel 	eui[4] = 0xFE;
2308e837735eSNicolas Dichtel 	eui[0] ^= 2;
2309e837735eSNicolas Dichtel 	return 0;
2310e837735eSNicolas Dichtel }
2311e837735eSNicolas Dichtel 
2312073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2313073a8e0eSYOSHIFUJI Hideaki {
2314073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
2315073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
2316073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
2317073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
2318073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
2319073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
2320073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
2321073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
2322c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
2323dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
2324aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
232545ce0fd1SFelix Jia 	case ARPHRD_TUNNEL:
2326aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
2327e74bccb8SJukka Rissanen 	case ARPHRD_6LOWPAN:
23288a7a4b47SAlexander Aring 		return addrconf_ifid_6lowpan(eui, dev);
2329cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
2330cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
2331e837735eSNicolas Dichtel 	case ARPHRD_TUNNEL6:
233245ce0fd1SFelix Jia 	case ARPHRD_IP6GRE:
23339deb441cSSubash Abhinov Kasiviswanathan 	case ARPHRD_RAWIP:
2334e837735eSNicolas Dichtel 		return addrconf_ifid_ip6tnl(eui, dev);
2335073a8e0eSYOSHIFUJI Hideaki 	}
23361da177e4SLinus Torvalds 	return -1;
23371da177e4SLinus Torvalds }
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
23401da177e4SLinus Torvalds {
23411da177e4SLinus Torvalds 	int err = -1;
23421da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
23431da177e4SLinus Torvalds 
23441da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2345602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2346602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
2347602582caSHannes Frederic Sowa 			break;
23481da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
23491da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
23501da177e4SLinus Torvalds 			err = 0;
23511da177e4SLinus Torvalds 			break;
23521da177e4SLinus Torvalds 		}
23531da177e4SLinus Torvalds 	}
23541da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
23551da177e4SLinus Torvalds 	return err;
23561da177e4SLinus Torvalds }
23571da177e4SLinus Torvalds 
2358969c5464SFernando Gont /* Generation of a randomized Interface Identifier
2359969c5464SFernando Gont  * draft-ietf-6man-rfc4941bis, Section 3.3.1
2360969c5464SFernando Gont  */
2361969c5464SFernando Gont 
2362969c5464SFernando Gont static void ipv6_gen_rnd_iid(struct in6_addr *addr)
23631da177e4SLinus Torvalds {
23641da177e4SLinus Torvalds regen:
2365969c5464SFernando Gont 	get_random_bytes(&addr->s6_addr[8], 8);
23661da177e4SLinus Torvalds 
2367969c5464SFernando Gont 	/* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1:
2368969c5464SFernando Gont 	 * check if generated address is not inappropriate:
23691da177e4SLinus Torvalds 	 *
2370912b519aSBhaskar Chowdhury 	 * - Reserved IPv6 Interface Identifiers
23711da177e4SLinus Torvalds 	 * - XXX: already assigned to an address on the device
23721da177e4SLinus Torvalds 	 */
23731da177e4SLinus Torvalds 
2374969c5464SFernando Gont 	/* Subnet-router anycast: 0000:0000:0000:0000 */
2375969c5464SFernando Gont 	if (!(addr->s6_addr32[2] | addr->s6_addr32[3]))
2376969c5464SFernando Gont 		goto regen;
2377969c5464SFernando Gont 
2378969c5464SFernando Gont 	/* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212
2379969c5464SFernando Gont 	 * Proxy Mobile IPv6:   0200:5EFF:FE00:5213
2380969c5464SFernando Gont 	 * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF
2381969c5464SFernando Gont 	 */
2382969c5464SFernando Gont 	if (ntohl(addr->s6_addr32[2]) == 0x02005eff &&
2383969c5464SFernando Gont 	    (ntohl(addr->s6_addr32[3]) & 0Xff000000) == 0xfe000000)
2384969c5464SFernando Gont 		goto regen;
2385969c5464SFernando Gont 
2386969c5464SFernando Gont 	/* Reserved subnet anycast addresses */
2387969c5464SFernando Gont 	if (ntohl(addr->s6_addr32[2]) == 0xfdffffff &&
2388969c5464SFernando Gont 	    ntohl(addr->s6_addr32[3]) >= 0Xffffff80)
2389969c5464SFernando Gont 		goto regen;
23901da177e4SLinus Torvalds }
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds /*
23931da177e4SLinus Torvalds  *	Add prefix route.
23941da177e4SLinus Torvalds  */
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds static void
23978308f3ffSDavid Ahern addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
23988308f3ffSDavid Ahern 		      struct net_device *dev, unsigned long expires,
23998308f3ffSDavid Ahern 		      u32 flags, gfp_t gfp_flags)
24001da177e4SLinus Torvalds {
240186872cb5SThomas Graf 	struct fib6_config cfg = {
2402ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
24038308f3ffSDavid Ahern 		.fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
240486872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
240586872cb5SThomas Graf 		.fc_expires = expires,
240686872cb5SThomas Graf 		.fc_dst_len = plen,
240786872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
2408c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
2409f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
2410e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
241186872cb5SThomas Graf 	};
24121da177e4SLinus Torvalds 
24134e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
24161da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
24171da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
24181da177e4SLinus Torvalds 	 */
241907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
24201da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
242186872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
24220be669bbSJoerg Roedel #endif
24231da177e4SLinus Torvalds 
2424acb54e3cSDavid Ahern 	ip6_route_add(&cfg, gfp_flags, NULL);
24251da177e4SLinus Torvalds }
24261da177e4SLinus Torvalds 
242714ef37b6SAndreas Hofmeister 
24288d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
242914ef37b6SAndreas Hofmeister 						  int plen,
243014ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
24312b2450caSDavid Ahern 						  u32 flags, u32 noflags,
24322b2450caSDavid Ahern 						  bool no_gw)
243314ef37b6SAndreas Hofmeister {
243414ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
24358d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
243614ef37b6SAndreas Hofmeister 	struct fib6_table *table;
2437ca254490SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
243814ef37b6SAndreas Hofmeister 
2439ca254490SDavid Ahern 	table = fib6_get_table(dev_net(dev), tb_id);
244063159f29SIan Morris 	if (!table)
244114ef37b6SAndreas Hofmeister 		return NULL;
244214ef37b6SAndreas Hofmeister 
244366f5d6ceSWei Wang 	rcu_read_lock();
244438fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
244514ef37b6SAndreas Hofmeister 	if (!fn)
244614ef37b6SAndreas Hofmeister 		goto out;
24471f56a01fSMartin KaFai Lau 
244866f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
2449f88d8ea6SDavid Ahern 		/* prefix routes only use builtin fib6_nh */
2450f88d8ea6SDavid Ahern 		if (rt->nh)
2451f88d8ea6SDavid Ahern 			continue;
2452f88d8ea6SDavid Ahern 
24531cf844c7SDavid Ahern 		if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
245414ef37b6SAndreas Hofmeister 			continue;
24551cf844c7SDavid Ahern 		if (no_gw && rt->fib6_nh->fib_nh_gw_family)
24562b2450caSDavid Ahern 			continue;
245793c2fb25SDavid Ahern 		if ((rt->fib6_flags & flags) != flags)
245814ef37b6SAndreas Hofmeister 			continue;
245993c2fb25SDavid Ahern 		if ((rt->fib6_flags & noflags) != 0)
246014ef37b6SAndreas Hofmeister 			continue;
2461e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
2462e873e4b9SWei Wang 			continue;
246314ef37b6SAndreas Hofmeister 		break;
246414ef37b6SAndreas Hofmeister 	}
246514ef37b6SAndreas Hofmeister out:
246666f5d6ceSWei Wang 	rcu_read_unlock();
246714ef37b6SAndreas Hofmeister 	return rt;
246814ef37b6SAndreas Hofmeister }
246914ef37b6SAndreas Hofmeister 
247014ef37b6SAndreas Hofmeister 
24711da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
24721da177e4SLinus Torvalds 
24731da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
24741da177e4SLinus Torvalds {
247586872cb5SThomas Graf 	struct fib6_config cfg = {
2476ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
247786872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
247886872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
247986872cb5SThomas Graf 		.fc_dst_len = 8,
248086872cb5SThomas Graf 		.fc_flags = RTF_UP,
2481ceed9038SMatteo Croce 		.fc_type = RTN_MULTICAST,
2482c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
2483a826b043SMatteo Croce 		.fc_protocol = RTPROT_KERNEL,
248486872cb5SThomas Graf 	};
24851da177e4SLinus Torvalds 
248686872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
248786872cb5SThomas Graf 
2488e500c6d3SCong Wang 	ip6_route_add(&cfg, GFP_KERNEL, NULL);
24891da177e4SLinus Torvalds }
24901da177e4SLinus Torvalds 
24911da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
24921da177e4SLinus Torvalds {
24931da177e4SLinus Torvalds 	struct inet6_dev *idev;
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds 	ASSERT_RTNL();
24961da177e4SLinus Torvalds 
2497e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2498db0b99f5SSabrina Dubroca 	if (IS_ERR(idev))
2499db0b99f5SSabrina Dubroca 		return idev;
250064e724f6SBrian Haley 
250164e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
250264e724f6SBrian Haley 		return ERR_PTR(-EACCES);
25031da177e4SLinus Torvalds 
25041da177e4SLinus Torvalds 	/* Add default multicast route */
2505ba46ee4cSDavid Ahern 	if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
25061da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds 	return idev;
25091da177e4SLinus Torvalds }
25101da177e4SLinus Torvalds 
251153bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev,
251253bd6749SJiri Pirko 			     struct inet6_ifaddr *ifp,
251353bd6749SJiri Pirko 			     __u32 valid_lft, __u32 prefered_lft,
251453bd6749SJiri Pirko 			     bool create, unsigned long now)
251553bd6749SJiri Pirko {
251653bd6749SJiri Pirko 	u32 flags;
251753bd6749SJiri Pirko 	struct inet6_ifaddr *ift;
251853bd6749SJiri Pirko 
251953bd6749SJiri Pirko 	read_lock_bh(&idev->lock);
252053bd6749SJiri Pirko 	/* update all temporary addresses in the list */
252153bd6749SJiri Pirko 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
252253bd6749SJiri Pirko 		int age, max_valid, max_prefered;
252353bd6749SJiri Pirko 
252453bd6749SJiri Pirko 		if (ifp != ift->ifpub)
252553bd6749SJiri Pirko 			continue;
252653bd6749SJiri Pirko 
252753bd6749SJiri Pirko 		/* RFC 4941 section 3.3:
252853bd6749SJiri Pirko 		 * If a received option will extend the lifetime of a public
252953bd6749SJiri Pirko 		 * address, the lifetimes of temporary addresses should
253053bd6749SJiri Pirko 		 * be extended, subject to the overall constraint that no
253153bd6749SJiri Pirko 		 * temporary addresses should ever remain "valid" or "preferred"
253253bd6749SJiri Pirko 		 * for a time longer than (TEMP_VALID_LIFETIME) or
253353bd6749SJiri Pirko 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
253453bd6749SJiri Pirko 		 */
253553bd6749SJiri Pirko 		age = (now - ift->cstamp) / HZ;
253653bd6749SJiri Pirko 		max_valid = idev->cnf.temp_valid_lft - age;
253753bd6749SJiri Pirko 		if (max_valid < 0)
253853bd6749SJiri Pirko 			max_valid = 0;
253953bd6749SJiri Pirko 
254053bd6749SJiri Pirko 		max_prefered = idev->cnf.temp_prefered_lft -
254176506a98SJiri Bohac 			       idev->desync_factor - age;
254253bd6749SJiri Pirko 		if (max_prefered < 0)
254353bd6749SJiri Pirko 			max_prefered = 0;
254453bd6749SJiri Pirko 
254553bd6749SJiri Pirko 		if (valid_lft > max_valid)
254653bd6749SJiri Pirko 			valid_lft = max_valid;
254753bd6749SJiri Pirko 
254853bd6749SJiri Pirko 		if (prefered_lft > max_prefered)
254953bd6749SJiri Pirko 			prefered_lft = max_prefered;
255053bd6749SJiri Pirko 
255153bd6749SJiri Pirko 		spin_lock(&ift->lock);
255253bd6749SJiri Pirko 		flags = ift->flags;
255353bd6749SJiri Pirko 		ift->valid_lft = valid_lft;
255453bd6749SJiri Pirko 		ift->prefered_lft = prefered_lft;
255553bd6749SJiri Pirko 		ift->tstamp = now;
255653bd6749SJiri Pirko 		if (prefered_lft > 0)
255753bd6749SJiri Pirko 			ift->flags &= ~IFA_F_DEPRECATED;
255853bd6749SJiri Pirko 
255953bd6749SJiri Pirko 		spin_unlock(&ift->lock);
256053bd6749SJiri Pirko 		if (!(flags&IFA_F_TENTATIVE))
256153bd6749SJiri Pirko 			ipv6_ifa_notify(0, ift);
256253bd6749SJiri Pirko 	}
256353bd6749SJiri Pirko 
256453bd6749SJiri Pirko 	if ((create || list_empty(&idev->tempaddr_list)) &&
256553bd6749SJiri Pirko 	    idev->cnf.use_tempaddr > 0) {
256653bd6749SJiri Pirko 		/* When a new public address is created as described
256753bd6749SJiri Pirko 		 * in [ADDRCONF], also create a new temporary address.
256853bd6749SJiri Pirko 		 * Also create a temporary address if it's enabled but
256953bd6749SJiri Pirko 		 * no temporary address currently exists.
257053bd6749SJiri Pirko 		 */
257153bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
2572969c5464SFernando Gont 		ipv6_create_tempaddr(ifp, false);
257353bd6749SJiri Pirko 	} else {
257453bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
257553bd6749SJiri Pirko 	}
257653bd6749SJiri Pirko }
257753bd6749SJiri Pirko 
2578cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2579cc9da6ccSBjørn Mork {
2580d35a00b8SFelix Jia 	return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2581d35a00b8SFelix Jia 	       idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2582cc9da6ccSBjørn Mork }
2583cc9da6ccSBjørn Mork 
2584cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
25854f672235SAlexander Aring 				 const struct prefix_info *pinfo,
25864f672235SAlexander Aring 				 struct inet6_dev *in6_dev,
2587cc84b3c6SAlexander Aring 				 const struct in6_addr *addr, int addr_type,
2588cc84b3c6SAlexander Aring 				 u32 addr_flags, bool sllao, bool tokenized,
25894f672235SAlexander Aring 				 __u32 valid_lft, u32 prefered_lft)
25904f672235SAlexander Aring {
25914f672235SAlexander Aring 	struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
259236268983SGuillaume Nault 	int create = 0, update_lft = 0;
25934f672235SAlexander Aring 
25944f672235SAlexander Aring 	if (!ifp && valid_lft) {
25954f672235SAlexander Aring 		int max_addresses = in6_dev->cnf.max_addresses;
2596e6464b8cSDavid Ahern 		struct ifa6_config cfg = {
2597e6464b8cSDavid Ahern 			.pfx = addr,
2598e6464b8cSDavid Ahern 			.plen = pinfo->prefix_len,
2599e6464b8cSDavid Ahern 			.ifa_flags = addr_flags,
2600e6464b8cSDavid Ahern 			.valid_lft = valid_lft,
2601e6464b8cSDavid Ahern 			.preferred_lft = prefered_lft,
2602e6464b8cSDavid Ahern 			.scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2603e6464b8cSDavid Ahern 		};
26044f672235SAlexander Aring 
26054f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
260635e015e1SMatteo Croce 		if ((net->ipv6.devconf_all->optimistic_dad ||
260735e015e1SMatteo Croce 		     in6_dev->cnf.optimistic_dad) &&
26084f672235SAlexander Aring 		    !net->ipv6.devconf_all->forwarding && sllao)
2609e6464b8cSDavid Ahern 			cfg.ifa_flags |= IFA_F_OPTIMISTIC;
26104f672235SAlexander Aring #endif
26114f672235SAlexander Aring 
26124f672235SAlexander Aring 		/* Do not allow to create too much of autoconfigured
26134f672235SAlexander Aring 		 * addresses; this would be too easy way to crash kernel.
26144f672235SAlexander Aring 		 */
26154f672235SAlexander Aring 		if (!max_addresses ||
26164f672235SAlexander Aring 		    ipv6_count_addresses(in6_dev) < max_addresses)
2617e6464b8cSDavid Ahern 			ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
26184f672235SAlexander Aring 
26194f672235SAlexander Aring 		if (IS_ERR_OR_NULL(ifp))
26204f672235SAlexander Aring 			return -1;
26214f672235SAlexander Aring 
26224f672235SAlexander Aring 		create = 1;
26234f672235SAlexander Aring 		spin_lock_bh(&ifp->lock);
26244f672235SAlexander Aring 		ifp->flags |= IFA_F_MANAGETEMPADDR;
26254f672235SAlexander Aring 		ifp->cstamp = jiffies;
26264f672235SAlexander Aring 		ifp->tokenized = tokenized;
26274f672235SAlexander Aring 		spin_unlock_bh(&ifp->lock);
26284f672235SAlexander Aring 		addrconf_dad_start(ifp);
26294f672235SAlexander Aring 	}
26304f672235SAlexander Aring 
26314f672235SAlexander Aring 	if (ifp) {
26324f672235SAlexander Aring 		u32 flags;
26334f672235SAlexander Aring 		unsigned long now;
26344f672235SAlexander Aring 		u32 stored_lft;
26354f672235SAlexander Aring 
263636268983SGuillaume Nault 		/* update lifetime (RFC2462 5.5.3 e) */
26374f672235SAlexander Aring 		spin_lock_bh(&ifp->lock);
26384f672235SAlexander Aring 		now = jiffies;
26394f672235SAlexander Aring 		if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
26404f672235SAlexander Aring 			stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
26414f672235SAlexander Aring 		else
26424f672235SAlexander Aring 			stored_lft = 0;
2643f85f94b8SLorenzo Bianconi 		if (!create && stored_lft) {
264436268983SGuillaume Nault 			const u32 minimum_lft = min_t(u32,
264536268983SGuillaume Nault 				stored_lft, MIN_VALID_LIFETIME);
264636268983SGuillaume Nault 			valid_lft = max(valid_lft, minimum_lft);
264736268983SGuillaume Nault 
264836268983SGuillaume Nault 			/* RFC4862 Section 5.5.3e:
264936268983SGuillaume Nault 			 * "Note that the preferred lifetime of the
265036268983SGuillaume Nault 			 *  corresponding address is always reset to
265136268983SGuillaume Nault 			 *  the Preferred Lifetime in the received
265236268983SGuillaume Nault 			 *  Prefix Information option, regardless of
265336268983SGuillaume Nault 			 *  whether the valid lifetime is also reset or
265436268983SGuillaume Nault 			 *  ignored."
265536268983SGuillaume Nault 			 *
265636268983SGuillaume Nault 			 * So we should always update prefered_lft here.
265736268983SGuillaume Nault 			 */
265836268983SGuillaume Nault 			update_lft = 1;
265936268983SGuillaume Nault 		}
266036268983SGuillaume Nault 
266136268983SGuillaume Nault 		if (update_lft) {
26624f672235SAlexander Aring 			ifp->valid_lft = valid_lft;
26634f672235SAlexander Aring 			ifp->prefered_lft = prefered_lft;
26644f672235SAlexander Aring 			ifp->tstamp = now;
26654f672235SAlexander Aring 			flags = ifp->flags;
26664f672235SAlexander Aring 			ifp->flags &= ~IFA_F_DEPRECATED;
26674f672235SAlexander Aring 			spin_unlock_bh(&ifp->lock);
26684f672235SAlexander Aring 
26694f672235SAlexander Aring 			if (!(flags&IFA_F_TENTATIVE))
26704f672235SAlexander Aring 				ipv6_ifa_notify(0, ifp);
26714f672235SAlexander Aring 		} else
26724f672235SAlexander Aring 			spin_unlock_bh(&ifp->lock);
26734f672235SAlexander Aring 
26744f672235SAlexander Aring 		manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
26754f672235SAlexander Aring 				 create, now);
26764f672235SAlexander Aring 
26774f672235SAlexander Aring 		in6_ifa_put(ifp);
26784f672235SAlexander Aring 		addrconf_verify();
26794f672235SAlexander Aring 	}
26804f672235SAlexander Aring 
26814f672235SAlexander Aring 	return 0;
26824f672235SAlexander Aring }
2683cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
26844f672235SAlexander Aring 
2685e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
26861da177e4SLinus Torvalds {
26871da177e4SLinus Torvalds 	struct prefix_info *pinfo;
26881da177e4SLinus Torvalds 	__u32 valid_lft;
26891da177e4SLinus Torvalds 	__u32 prefered_lft;
26904f672235SAlexander Aring 	int addr_type, err;
269164236f3fSHannes Frederic Sowa 	u32 addr_flags = 0;
26921da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
269356d417b1SBrian Haley 	struct net *net = dev_net(dev);
26941da177e4SLinus Torvalds 
26951da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
2698e32ac250SJoe Perches 		netdev_dbg(dev, "addrconf: prefix option too short\n");
26991da177e4SLinus Torvalds 		return;
27001da177e4SLinus Torvalds 	}
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds 	/*
27031da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
27041da177e4SLinus Torvalds 	 */
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
27091da177e4SLinus Torvalds 		return;
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
27121da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2715e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
27161da177e4SLinus Torvalds 		return;
27171da177e4SLinus Torvalds 	}
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
27201da177e4SLinus Torvalds 
272163159f29SIan Morris 	if (!in6_dev) {
2722e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2723e87cc472SJoe Perches 				    dev->name);
27241da177e4SLinus Torvalds 		return;
27251da177e4SLinus Torvalds 	}
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds 	/*
27281da177e4SLinus Torvalds 	 *	Two things going on here:
27291da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
27301da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
27311da177e4SLinus Torvalds 	 */
27321da177e4SLinus Torvalds 
27334bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
27348d1c802bSDavid Ahern 		struct fib6_info *rt;
27354bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
27364bed72e4SYOSHIFUJI Hideaki 
27371da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
27386f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
27396f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
27406f704992SYOSHIFUJI Hideaki 		 * not good.
27411da177e4SLinus Torvalds 		 */
27424bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
27434bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
27444bed72e4SYOSHIFUJI Hideaki 		else
27454bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
27463dd4bc68SYOSHIFUJI Hideaki 
27474bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
27484bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
27491da177e4SLinus Torvalds 
275014ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
275114ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
275214ef37b6SAndreas Hofmeister 					       dev,
275314ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
27542b2450caSDavid Ahern 					       RTF_DEFAULT, true);
27551da177e4SLinus Torvalds 
275614ef37b6SAndreas Hofmeister 		if (rt) {
27576f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
27581da177e4SLinus Torvalds 			if (valid_lft == 0) {
275911dd74b3SRoopa Prabhu 				ip6_del_rt(net, rt, false);
27601da177e4SLinus Torvalds 				rt = NULL;
27614bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
27626f704992SYOSHIFUJI Hideaki 				/* not infinity */
276314895687SDavid Ahern 				fib6_set_expires(rt, jiffies + rt_expires);
27646f704992SYOSHIFUJI Hideaki 			} else {
276514895687SDavid Ahern 				fib6_clean_expires(rt);
27661da177e4SLinus Torvalds 			}
27671da177e4SLinus Torvalds 		} else if (valid_lft) {
27686f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
27694bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
27704bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
27716f704992SYOSHIFUJI Hideaki 				/* not infinity */
27726f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
27736f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
27746f704992SYOSHIFUJI Hideaki 			}
27751da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
27768308f3ffSDavid Ahern 					      0, dev, expires, flags,
27778308f3ffSDavid Ahern 					      GFP_ATOMIC);
27781da177e4SLinus Torvalds 		}
277993531c67SDavid Ahern 		fib6_info_release(rt);
27801da177e4SLinus Torvalds 	}
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
27831da177e4SLinus Torvalds 
27841da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
27851da177e4SLinus Torvalds 		struct in6_addr addr;
2786f997c55cSAlexander Aring 		bool tokenized = false, dev_addr_generated = false;
27871da177e4SLinus Torvalds 
27881da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
27891da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2790f53adae4SDaniel Borkmann 
2791f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2792f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2793f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2794f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2795f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
2796617fe29dSDaniel Borkmann 				tokenized = true;
2797cc9da6ccSBjørn Mork 			} else if (is_addr_mode_generate_stable(in6_dev) &&
2798622c81d5SHannes Frederic Sowa 				   !ipv6_generate_stable_address(&addr, 0,
2799622c81d5SHannes Frederic Sowa 								 in6_dev)) {
280064236f3fSHannes Frederic Sowa 				addr_flags |= IFA_F_STABLE_PRIVACY;
2801622c81d5SHannes Frederic Sowa 				goto ok;
2802f53adae4SDaniel Borkmann 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
28031da177e4SLinus Torvalds 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
28044f672235SAlexander Aring 				goto put;
2805f997c55cSAlexander Aring 			} else {
2806f997c55cSAlexander Aring 				dev_addr_generated = true;
28071da177e4SLinus Torvalds 			}
28081da177e4SLinus Torvalds 			goto ok;
28091da177e4SLinus Torvalds 		}
2810e87cc472SJoe Perches 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
28111da177e4SLinus Torvalds 				    pinfo->prefix_len);
28124f672235SAlexander Aring 		goto put;
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds ok:
28154f672235SAlexander Aring 		err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
28164f672235SAlexander Aring 						   &addr, addr_type,
28174f672235SAlexander Aring 						   addr_flags, sllao,
28184f672235SAlexander Aring 						   tokenized, valid_lft,
28198a226b2cSJiri Benc 						   prefered_lft);
28204f672235SAlexander Aring 		if (err)
28214f672235SAlexander Aring 			goto put;
2822f997c55cSAlexander Aring 
2823f997c55cSAlexander Aring 		/* Ignore error case here because previous prefix add addr was
2824f997c55cSAlexander Aring 		 * successful which will be notified.
2825f997c55cSAlexander Aring 		 */
2826f997c55cSAlexander Aring 		ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2827f997c55cSAlexander Aring 					      addr_type, addr_flags, sllao,
2828f997c55cSAlexander Aring 					      tokenized, valid_lft,
2829f997c55cSAlexander Aring 					      prefered_lft,
2830f997c55cSAlexander Aring 					      dev_addr_generated);
28311da177e4SLinus Torvalds 	}
28321da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
28334f672235SAlexander Aring put:
28341da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
28351da177e4SLinus Torvalds }
28361da177e4SLinus Torvalds 
283768ad6886SChristoph Hellwig static int addrconf_set_sit_dstaddr(struct net *net, struct net_device *dev,
283868ad6886SChristoph Hellwig 		struct in6_ifreq *ireq)
283968ad6886SChristoph Hellwig {
284068ad6886SChristoph Hellwig 	struct ip_tunnel_parm p = { };
284168ad6886SChristoph Hellwig 	int err;
284268ad6886SChristoph Hellwig 
284368ad6886SChristoph Hellwig 	if (!(ipv6_addr_type(&ireq->ifr6_addr) & IPV6_ADDR_COMPATv4))
284468ad6886SChristoph Hellwig 		return -EADDRNOTAVAIL;
284568ad6886SChristoph Hellwig 
284668ad6886SChristoph Hellwig 	p.iph.daddr = ireq->ifr6_addr.s6_addr32[3];
284768ad6886SChristoph Hellwig 	p.iph.version = 4;
284868ad6886SChristoph Hellwig 	p.iph.ihl = 5;
284968ad6886SChristoph Hellwig 	p.iph.protocol = IPPROTO_IPV6;
285068ad6886SChristoph Hellwig 	p.iph.ttl = 64;
285168ad6886SChristoph Hellwig 
28528e3db0bbSChristoph Hellwig 	if (!dev->netdev_ops->ndo_tunnel_ctl)
285368ad6886SChristoph Hellwig 		return -EOPNOTSUPP;
28548e3db0bbSChristoph Hellwig 	err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, SIOCADDTUNNEL);
285568ad6886SChristoph Hellwig 	if (err)
285668ad6886SChristoph Hellwig 		return err;
285768ad6886SChristoph Hellwig 
285868ad6886SChristoph Hellwig 	dev = __dev_get_by_name(net, p.name);
285968ad6886SChristoph Hellwig 	if (!dev)
286068ad6886SChristoph Hellwig 		return -ENOBUFS;
286168ad6886SChristoph Hellwig 	return dev_open(dev, NULL);
286268ad6886SChristoph Hellwig }
286368ad6886SChristoph Hellwig 
28641da177e4SLinus Torvalds /*
28651da177e4SLinus Torvalds  *	Set destination address.
28661da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
28671da177e4SLinus Torvalds  *	device.
28681da177e4SLinus Torvalds  */
2869af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
28701da177e4SLinus Torvalds {
28711da177e4SLinus Torvalds 	struct net_device *dev;
287268ad6886SChristoph Hellwig 	struct in6_ifreq ireq;
287368ad6886SChristoph Hellwig 	int err = -ENODEV;
28741da177e4SLinus Torvalds 
2875f0988460SChristoph Hellwig 	if (!IS_ENABLED(CONFIG_IPV6_SIT))
2876f0988460SChristoph Hellwig 		return -ENODEV;
287768ad6886SChristoph Hellwig 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
287868ad6886SChristoph Hellwig 		return -EFAULT;
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds 	rtnl_lock();
2881af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
288268ad6886SChristoph Hellwig 	if (dev && dev->type == ARPHRD_SIT)
288368ad6886SChristoph Hellwig 		err = addrconf_set_sit_dstaddr(net, dev, &ireq);
28841da177e4SLinus Torvalds 	rtnl_unlock();
28851da177e4SLinus Torvalds 	return err;
28861da177e4SLinus Torvalds }
28871da177e4SLinus Torvalds 
288893a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join,
288993a714d6SMadhu Challa 			  const struct in6_addr *addr, int ifindex)
289093a714d6SMadhu Challa {
289193a714d6SMadhu Challa 	int ret;
289293a714d6SMadhu Challa 
289393a714d6SMadhu Challa 	ASSERT_RTNL();
289493a714d6SMadhu Challa 
289593a714d6SMadhu Challa 	lock_sock(sk);
289693a714d6SMadhu Challa 	if (join)
289754ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_join(sk, ifindex, addr);
289893a714d6SMadhu Challa 	else
289954ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_drop(sk, ifindex, addr);
290093a714d6SMadhu Challa 	release_sock(sk);
290193a714d6SMadhu Challa 
290293a714d6SMadhu Challa 	return ret;
290393a714d6SMadhu Challa }
290493a714d6SMadhu Challa 
29051da177e4SLinus Torvalds /*
29061da177e4SLinus Torvalds  *	Manual configuration of address on an interface
29071da177e4SLinus Torvalds  */
2908479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex,
290919b1518cSDavid Ahern 			  struct ifa6_config *cfg,
2910de95e047SDavid Ahern 			  struct netlink_ext_ack *extack)
29111da177e4SLinus Torvalds {
29121da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
29131da177e4SLinus Torvalds 	struct inet6_dev *idev;
29141da177e4SLinus Torvalds 	struct net_device *dev;
291593a714d6SMadhu Challa 	unsigned long timeout;
291693a714d6SMadhu Challa 	clock_t expires;
29176f704992SYOSHIFUJI Hideaki 	u32 flags;
29181da177e4SLinus Torvalds 
29191da177e4SLinus Torvalds 	ASSERT_RTNL();
29201da177e4SLinus Torvalds 
292119b1518cSDavid Ahern 	if (cfg->plen > 128)
292224ef0da7SThomas Graf 		return -EINVAL;
292324ef0da7SThomas Graf 
29240778769dSNoriaki TAKAMIYA 	/* check the lifetime */
292519b1518cSDavid Ahern 	if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
29260778769dSNoriaki TAKAMIYA 		return -EINVAL;
29270778769dSNoriaki TAKAMIYA 
292819b1518cSDavid Ahern 	if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64)
292953bd6749SJiri Pirko 		return -EINVAL;
293053bd6749SJiri Pirko 
2931af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2932af284937SDaniel Lezcano 	if (!dev)
29331da177e4SLinus Torvalds 		return -ENODEV;
29341da177e4SLinus Torvalds 
293564e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
293664e724f6SBrian Haley 	if (IS_ERR(idev))
293764e724f6SBrian Haley 		return PTR_ERR(idev);
29381da177e4SLinus Torvalds 
293919b1518cSDavid Ahern 	if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
294093a714d6SMadhu Challa 		int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
294119b1518cSDavid Ahern 					 true, cfg->pfx, ifindex);
294293a714d6SMadhu Challa 
294393a714d6SMadhu Challa 		if (ret < 0)
294493a714d6SMadhu Challa 			return ret;
294593a714d6SMadhu Challa 	}
294693a714d6SMadhu Challa 
294719b1518cSDavid Ahern 	cfg->scope = ipv6_addr_scope(cfg->pfx);
29481da177e4SLinus Torvalds 
294919b1518cSDavid Ahern 	timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
29504bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
29514bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
295219b1518cSDavid Ahern 		cfg->valid_lft = timeout;
29536f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
29544bed72e4SYOSHIFUJI Hideaki 	} else {
29554bed72e4SYOSHIFUJI Hideaki 		expires = 0;
29564bed72e4SYOSHIFUJI Hideaki 		flags = 0;
295719b1518cSDavid Ahern 		cfg->ifa_flags |= IFA_F_PERMANENT;
29586f704992SYOSHIFUJI Hideaki 	}
29590778769dSNoriaki TAKAMIYA 
296019b1518cSDavid Ahern 	timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
29614bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
29624bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
296319b1518cSDavid Ahern 			cfg->ifa_flags |= IFA_F_DEPRECATED;
296419b1518cSDavid Ahern 		cfg->preferred_lft = timeout;
29654bed72e4SYOSHIFUJI Hideaki 	}
29660778769dSNoriaki TAKAMIYA 
296719b1518cSDavid Ahern 	ifp = ipv6_add_addr(idev, cfg, true, extack);
29681da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
296919b1518cSDavid Ahern 		if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
29708308f3ffSDavid Ahern 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
29718308f3ffSDavid Ahern 					      ifp->rt_priority, dev, expires,
29728308f3ffSDavid Ahern 					      flags, GFP_KERNEL);
2973761aac73SThomas Haller 		}
2974761aac73SThomas Haller 
2975a2d481b3SLorenzo Bianconi 		/* Send a netlink notification if DAD is enabled and
2976a2d481b3SLorenzo Bianconi 		 * optimistic flag is not set
2977a2d481b3SLorenzo Bianconi 		 */
2978a2d481b3SLorenzo Bianconi 		if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
2979a2d481b3SLorenzo Bianconi 			ipv6_ifa_notify(0, ifp);
298095c385b4SNeil Horman 		/*
298195c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
298295c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
298395c385b4SNeil Horman 		 * manually configured addresses
298495c385b4SNeil Horman 		 */
2985cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
298619b1518cSDavid Ahern 		if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
298719b1518cSDavid Ahern 			manage_tempaddrs(idev, ifp, cfg->valid_lft,
298819b1518cSDavid Ahern 					 cfg->preferred_lft, true, jiffies);
29891da177e4SLinus Torvalds 		in6_ifa_put(ifp);
2990c15b1ccaSHannes Frederic Sowa 		addrconf_verify_rtnl();
29911da177e4SLinus Torvalds 		return 0;
299219b1518cSDavid Ahern 	} else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
299319b1518cSDavid Ahern 		ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
299419b1518cSDavid Ahern 			       cfg->pfx, ifindex);
29951da177e4SLinus Torvalds 	}
29961da177e4SLinus Torvalds 
29971da177e4SLinus Torvalds 	return PTR_ERR(ifp);
29981da177e4SLinus Torvalds }
29991da177e4SLinus Torvalds 
30006046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
30016046d5b4SHeiner Kallweit 			  const struct in6_addr *pfx, unsigned int plen)
30021da177e4SLinus Torvalds {
30031da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
30041da177e4SLinus Torvalds 	struct inet6_dev *idev;
30051da177e4SLinus Torvalds 	struct net_device *dev;
30061da177e4SLinus Torvalds 
300724ef0da7SThomas Graf 	if (plen > 128)
300824ef0da7SThomas Graf 		return -EINVAL;
300924ef0da7SThomas Graf 
3010af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
3011af284937SDaniel Lezcano 	if (!dev)
30121da177e4SLinus Torvalds 		return -ENODEV;
30131da177e4SLinus Torvalds 
3014e5d08d71SIan Morris 	idev = __in6_dev_get(dev);
301563159f29SIan Morris 	if (!idev)
30161da177e4SLinus Torvalds 		return -ENXIO;
30171da177e4SLinus Torvalds 
30181da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3019502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
30201da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
30211da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
30221da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
30231da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
30241da177e4SLinus Torvalds 
30256046d5b4SHeiner Kallweit 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
30266046d5b4SHeiner Kallweit 			    (ifa_flags & IFA_F_MANAGETEMPADDR))
30276046d5b4SHeiner Kallweit 				manage_tempaddrs(idev, ifp, 0, 0, false,
30286046d5b4SHeiner Kallweit 						 jiffies);
30291da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
30306046d5b4SHeiner Kallweit 			addrconf_verify_rtnl();
303193a714d6SMadhu Challa 			if (ipv6_addr_is_multicast(pfx)) {
303293a714d6SMadhu Challa 				ipv6_mc_config(net->ipv6.mc_autojoin_sk,
303393a714d6SMadhu Challa 					       false, pfx, dev->ifindex);
303493a714d6SMadhu Challa 			}
30351da177e4SLinus Torvalds 			return 0;
30361da177e4SLinus Torvalds 		}
30371da177e4SLinus Torvalds 	}
30381da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
30391da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
30401da177e4SLinus Torvalds }
30411da177e4SLinus Torvalds 
30421da177e4SLinus Torvalds 
3043af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
30441da177e4SLinus Torvalds {
304519b1518cSDavid Ahern 	struct ifa6_config cfg = {
304619b1518cSDavid Ahern 		.ifa_flags = IFA_F_PERMANENT,
304719b1518cSDavid Ahern 		.preferred_lft = INFINITY_LIFE_TIME,
304819b1518cSDavid Ahern 		.valid_lft = INFINITY_LIFE_TIME,
304919b1518cSDavid Ahern 	};
30501da177e4SLinus Torvalds 	struct in6_ifreq ireq;
30511da177e4SLinus Torvalds 	int err;
30521da177e4SLinus Torvalds 
3053af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
30541da177e4SLinus Torvalds 		return -EPERM;
30551da177e4SLinus Torvalds 
30561da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
30571da177e4SLinus Torvalds 		return -EFAULT;
30581da177e4SLinus Torvalds 
305919b1518cSDavid Ahern 	cfg.pfx = &ireq.ifr6_addr;
306019b1518cSDavid Ahern 	cfg.plen = ireq.ifr6_prefixlen;
306119b1518cSDavid Ahern 
30621da177e4SLinus Torvalds 	rtnl_lock();
306319b1518cSDavid Ahern 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
30641da177e4SLinus Torvalds 	rtnl_unlock();
30651da177e4SLinus Torvalds 	return err;
30661da177e4SLinus Torvalds }
30671da177e4SLinus Torvalds 
3068af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
30691da177e4SLinus Torvalds {
30701da177e4SLinus Torvalds 	struct in6_ifreq ireq;
30711da177e4SLinus Torvalds 	int err;
30721da177e4SLinus Torvalds 
3073af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
30741da177e4SLinus Torvalds 		return -EPERM;
30751da177e4SLinus Torvalds 
30761da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
30771da177e4SLinus Torvalds 		return -EFAULT;
30781da177e4SLinus Torvalds 
30791da177e4SLinus Torvalds 	rtnl_lock();
30806046d5b4SHeiner Kallweit 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
3081af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
30821da177e4SLinus Torvalds 	rtnl_unlock();
30831da177e4SLinus Torvalds 	return err;
30841da177e4SLinus Torvalds }
30851da177e4SLinus Torvalds 
3086b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3087b5f348e5SIlpo Järvinen 		     int plen, int scope)
3088b5f348e5SIlpo Järvinen {
3089b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
3090e6464b8cSDavid Ahern 	struct ifa6_config cfg = {
3091e6464b8cSDavid Ahern 		.pfx = addr,
3092e6464b8cSDavid Ahern 		.plen = plen,
3093e6464b8cSDavid Ahern 		.ifa_flags = IFA_F_PERMANENT,
3094e6464b8cSDavid Ahern 		.valid_lft = INFINITY_LIFE_TIME,
3095e6464b8cSDavid Ahern 		.preferred_lft = INFINITY_LIFE_TIME,
3096e6464b8cSDavid Ahern 		.scope = scope
3097e6464b8cSDavid Ahern 	};
3098b5f348e5SIlpo Järvinen 
3099e6464b8cSDavid Ahern 	ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3100b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
3101b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
3102b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
3103b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
3104764d3be6SPaolo Abeni 		rt_genid_bump_ipv6(dev_net(idev->dev));
3105b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
3106b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
3107b5f348e5SIlpo Järvinen 	}
3108b5f348e5SIlpo Järvinen }
3109b5f348e5SIlpo Järvinen 
3110e5dd7294SAntonio Quartulli #if IS_ENABLED(CONFIG_IPV6_SIT) || IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3111e5dd7294SAntonio Quartulli static void add_v4_addrs(struct inet6_dev *idev)
31121da177e4SLinus Torvalds {
31131da177e4SLinus Torvalds 	struct in6_addr addr;
31141da177e4SLinus Torvalds 	struct net_device *dev;
3115c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
3116e5dd7294SAntonio Quartulli 	int scope, plen, offset = 0;
3117f0e2acfaSNicolas Dichtel 	u32 pflags = 0;
31181da177e4SLinus Torvalds 
31191da177e4SLinus Torvalds 	ASSERT_RTNL();
31201da177e4SLinus Torvalds 
31211da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
3122e5dd7294SAntonio Quartulli 	/* in case of IP6GRE the dev_addr is an IPv6 and therefore we use only the last 4 bytes */
3123e5dd7294SAntonio Quartulli 	if (idev->dev->addr_len == sizeof(struct in6_addr))
3124e5dd7294SAntonio Quartulli 		offset = sizeof(struct in6_addr) - 4;
3125e5dd7294SAntonio Quartulli 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr + offset, 4);
31261da177e4SLinus Torvalds 
31271da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
312861e18ce7SStephen Suryaputra 		if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE)
312961e18ce7SStephen Suryaputra 			return;
313061e18ce7SStephen Suryaputra 
31311da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
31321da177e4SLinus Torvalds 		scope = IFA_LINK;
3133929c9cf3SNicolas Dichtel 		plen = 64;
31341da177e4SLinus Torvalds 	} else {
31351da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
3136929c9cf3SNicolas Dichtel 		plen = 96;
3137f0e2acfaSNicolas Dichtel 		pflags |= RTF_NONEXTHOP;
31381da177e4SLinus Torvalds 	}
31391da177e4SLinus Torvalds 
31401da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
3141929c9cf3SNicolas Dichtel 		add_addr(idev, &addr, plen, scope);
31428308f3ffSDavid Ahern 		addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
3143e500c6d3SCong Wang 				      GFP_KERNEL);
31441da177e4SLinus Torvalds 		return;
31451da177e4SLinus Torvalds 	}
31461da177e4SLinus Torvalds 
31476fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
3148e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
31491da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
31501da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
31511da177e4SLinus Torvalds 			int flag = scope;
31521da177e4SLinus Torvalds 
3153cd5a411dSFlorian Westphal 			in_dev_for_each_ifa_rtnl(ifa, in_dev) {
31541da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
31551da177e4SLinus Torvalds 
31561da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
31571da177e4SLinus Torvalds 					continue;
31581da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
31591da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
31601da177e4SLinus Torvalds 						continue;
31611da177e4SLinus Torvalds 					flag |= IFA_HOST;
31621da177e4SLinus Torvalds 				}
31631da177e4SLinus Torvalds 
3164b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
31658308f3ffSDavid Ahern 				addrconf_prefix_route(&addr, plen, 0, idev->dev,
3166e500c6d3SCong Wang 						      0, pflags, GFP_KERNEL);
31671da177e4SLinus Torvalds 			}
31681da177e4SLinus Torvalds 		}
31691da177e4SLinus Torvalds 	}
31701da177e4SLinus Torvalds }
31710be669bbSJoerg Roedel #endif
31721da177e4SLinus Torvalds 
31731da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
31741da177e4SLinus Torvalds {
31751da177e4SLinus Torvalds 	struct inet6_dev  *idev;
31761da177e4SLinus Torvalds 
31771da177e4SLinus Torvalds 	/* ::1 */
31781da177e4SLinus Torvalds 
31791da177e4SLinus Torvalds 	ASSERT_RTNL();
31801da177e4SLinus Torvalds 
3181e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
3182db0b99f5SSabrina Dubroca 	if (IS_ERR(idev)) {
318391df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
31841da177e4SLinus Torvalds 		return;
31851da177e4SLinus Torvalds 	}
31861da177e4SLinus Torvalds 
3187b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
31881da177e4SLinus Torvalds }
31891da177e4SLinus Torvalds 
31902ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev,
319164236f3fSHannes Frederic Sowa 			    const struct in6_addr *addr, u32 flags)
31921da177e4SLinus Torvalds {
3193e6464b8cSDavid Ahern 	struct ifa6_config cfg = {
3194e6464b8cSDavid Ahern 		.pfx = addr,
3195e6464b8cSDavid Ahern 		.plen = 64,
3196e6464b8cSDavid Ahern 		.ifa_flags = flags | IFA_F_PERMANENT,
3197e6464b8cSDavid Ahern 		.valid_lft = INFINITY_LIFE_TIME,
3198e6464b8cSDavid Ahern 		.preferred_lft = INFINITY_LIFE_TIME,
3199e6464b8cSDavid Ahern 		.scope = IFA_LINK
3200e6464b8cSDavid Ahern 	};
32011da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
32021da177e4SLinus Torvalds 
320395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
320435e015e1SMatteo Croce 	if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
320535e015e1SMatteo Croce 	     idev->cnf.optimistic_dad) &&
3206702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3207e6464b8cSDavid Ahern 		cfg.ifa_flags |= IFA_F_OPTIMISTIC;
320895c385b4SNeil Horman #endif
320995c385b4SNeil Horman 
3210e6464b8cSDavid Ahern 	ifp = ipv6_add_addr(idev, &cfg, true, NULL);
32111da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
32128308f3ffSDavid Ahern 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
3213acb54e3cSDavid Ahern 				      0, 0, GFP_ATOMIC);
3214cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
32151da177e4SLinus Torvalds 		in6_ifa_put(ifp);
32161da177e4SLinus Torvalds 	}
32171da177e4SLinus Torvalds }
32182ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
32191da177e4SLinus Torvalds 
3220622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address)
3221622c81d5SHannes Frederic Sowa {
3222622c81d5SHannes Frederic Sowa 	if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3223622c81d5SHannes Frederic Sowa 		return true;
3224622c81d5SHannes Frederic Sowa 
3225622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0x02005eff) &&
3226622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3227622c81d5SHannes Frederic Sowa 		return true;
3228622c81d5SHannes Frederic Sowa 
3229622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3230622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3231622c81d5SHannes Frederic Sowa 		return true;
3232622c81d5SHannes Frederic Sowa 
3233622c81d5SHannes Frederic Sowa 	return false;
3234622c81d5SHannes Frederic Sowa }
3235622c81d5SHannes Frederic Sowa 
3236622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address,
3237622c81d5SHannes Frederic Sowa 					u8 dad_count,
3238622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev)
3239622c81d5SHannes Frederic Sowa {
3240622c81d5SHannes Frederic Sowa 	static DEFINE_SPINLOCK(lock);
32416b0b0fa2SEric Biggers 	static __u32 digest[SHA1_DIGEST_WORDS];
32426b0b0fa2SEric Biggers 	static __u32 workspace[SHA1_WORKSPACE_WORDS];
3243622c81d5SHannes Frederic Sowa 
3244622c81d5SHannes Frederic Sowa 	static union {
32456b0b0fa2SEric Biggers 		char __data[SHA1_BLOCK_SIZE];
3246622c81d5SHannes Frederic Sowa 		struct {
3247622c81d5SHannes Frederic Sowa 			struct in6_addr secret;
3248ff40217eSHannes Frederic Sowa 			__be32 prefix[2];
3249622c81d5SHannes Frederic Sowa 			unsigned char hwaddr[MAX_ADDR_LEN];
3250622c81d5SHannes Frederic Sowa 			u8 dad_count;
3251622c81d5SHannes Frederic Sowa 		} __packed;
3252622c81d5SHannes Frederic Sowa 	} data;
3253622c81d5SHannes Frederic Sowa 
3254622c81d5SHannes Frederic Sowa 	struct in6_addr secret;
3255622c81d5SHannes Frederic Sowa 	struct in6_addr temp;
3256622c81d5SHannes Frederic Sowa 	struct net *net = dev_net(idev->dev);
3257622c81d5SHannes Frederic Sowa 
3258622c81d5SHannes Frederic Sowa 	BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3259622c81d5SHannes Frederic Sowa 
3260622c81d5SHannes Frederic Sowa 	if (idev->cnf.stable_secret.initialized)
3261622c81d5SHannes Frederic Sowa 		secret = idev->cnf.stable_secret.secret;
3262622c81d5SHannes Frederic Sowa 	else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3263622c81d5SHannes Frederic Sowa 		secret = net->ipv6.devconf_dflt->stable_secret.secret;
3264622c81d5SHannes Frederic Sowa 	else
3265622c81d5SHannes Frederic Sowa 		return -1;
3266622c81d5SHannes Frederic Sowa 
3267622c81d5SHannes Frederic Sowa retry:
3268622c81d5SHannes Frederic Sowa 	spin_lock_bh(&lock);
3269622c81d5SHannes Frederic Sowa 
32706b0b0fa2SEric Biggers 	sha1_init(digest);
3271622c81d5SHannes Frederic Sowa 	memset(&data, 0, sizeof(data));
3272622c81d5SHannes Frederic Sowa 	memset(workspace, 0, sizeof(workspace));
3273622c81d5SHannes Frederic Sowa 	memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3274ff40217eSHannes Frederic Sowa 	data.prefix[0] = address->s6_addr32[0];
3275ff40217eSHannes Frederic Sowa 	data.prefix[1] = address->s6_addr32[1];
3276622c81d5SHannes Frederic Sowa 	data.secret = secret;
3277622c81d5SHannes Frederic Sowa 	data.dad_count = dad_count;
3278622c81d5SHannes Frederic Sowa 
32796b0b0fa2SEric Biggers 	sha1_transform(digest, data.__data, workspace);
3280622c81d5SHannes Frederic Sowa 
3281622c81d5SHannes Frederic Sowa 	temp = *address;
3282ff40217eSHannes Frederic Sowa 	temp.s6_addr32[2] = (__force __be32)digest[0];
3283ff40217eSHannes Frederic Sowa 	temp.s6_addr32[3] = (__force __be32)digest[1];
3284622c81d5SHannes Frederic Sowa 
3285622c81d5SHannes Frederic Sowa 	spin_unlock_bh(&lock);
3286622c81d5SHannes Frederic Sowa 
3287622c81d5SHannes Frederic Sowa 	if (ipv6_reserved_interfaceid(temp)) {
3288622c81d5SHannes Frederic Sowa 		dad_count++;
32891855b7c3SHannes Frederic Sowa 		if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3290622c81d5SHannes Frederic Sowa 			return -1;
3291622c81d5SHannes Frederic Sowa 		goto retry;
3292622c81d5SHannes Frederic Sowa 	}
3293622c81d5SHannes Frederic Sowa 
3294622c81d5SHannes Frederic Sowa 	*address = temp;
3295622c81d5SHannes Frederic Sowa 	return 0;
3296622c81d5SHannes Frederic Sowa }
3297622c81d5SHannes Frederic Sowa 
3298cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3299cc9da6ccSBjørn Mork {
3300cc9da6ccSBjørn Mork 	struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3301cc9da6ccSBjørn Mork 
3302cc9da6ccSBjørn Mork 	if (s->initialized)
3303cc9da6ccSBjørn Mork 		return;
3304cc9da6ccSBjørn Mork 	s = &idev->cnf.stable_secret;
3305cc9da6ccSBjørn Mork 	get_random_bytes(&s->secret, sizeof(s->secret));
3306cc9da6ccSBjørn Mork 	s->initialized = true;
3307cc9da6ccSBjørn Mork }
3308cc9da6ccSBjørn Mork 
3309bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3310bc91b0f0SJiri Pirko {
3311bc91b0f0SJiri Pirko 	struct in6_addr addr;
3312bc91b0f0SJiri Pirko 
3313ca254490SDavid Ahern 	/* no link local addresses on L3 master devices */
3314ca254490SDavid Ahern 	if (netif_is_l3_master(idev->dev))
3315ca254490SDavid Ahern 		return;
3316ca254490SDavid Ahern 
3317744fdc82SJarod Wilson 	/* no link local addresses on devices flagged as slaves */
3318744fdc82SJarod Wilson 	if (idev->dev->flags & IFF_SLAVE)
3319744fdc82SJarod Wilson 		return;
3320744fdc82SJarod Wilson 
3321bc91b0f0SJiri Pirko 	ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3322622c81d5SHannes Frederic Sowa 
3323d35a00b8SFelix Jia 	switch (idev->cnf.addr_gen_mode) {
3324cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_RANDOM:
3325cc9da6ccSBjørn Mork 		ipv6_gen_mode_random_init(idev);
3326a8eceea8SJoe Perches 		fallthrough;
3327cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3328622c81d5SHannes Frederic Sowa 		if (!ipv6_generate_stable_address(&addr, 0, idev))
332964236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr,
333064236f3fSHannes Frederic Sowa 					       IFA_F_STABLE_PRIVACY);
3331622c81d5SHannes Frederic Sowa 		else if (prefix_route)
33328308f3ffSDavid Ahern 			addrconf_prefix_route(&addr, 64, 0, idev->dev,
3333acb54e3cSDavid Ahern 					      0, 0, GFP_KERNEL);
3334cc9da6ccSBjørn Mork 		break;
3335cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_EUI64:
3336bc91b0f0SJiri Pirko 		/* addrconf_add_linklocal also adds a prefix_route and we
3337bc91b0f0SJiri Pirko 		 * only need to care about prefix routes if ipv6_generate_eui64
3338bc91b0f0SJiri Pirko 		 * couldn't generate one.
3339bc91b0f0SJiri Pirko 		 */
3340bc91b0f0SJiri Pirko 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
334164236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr, 0);
3342bc91b0f0SJiri Pirko 		else if (prefix_route)
33438308f3ffSDavid Ahern 			addrconf_prefix_route(&addr, 64, 0, idev->dev,
334427b10608SDavid Ahern 					      0, 0, GFP_KERNEL);
3345cc9da6ccSBjørn Mork 		break;
3346cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_NONE:
3347cc9da6ccSBjørn Mork 	default:
3348cc9da6ccSBjørn Mork 		/* will not add any link local address */
3349cc9da6ccSBjørn Mork 		break;
3350bc91b0f0SJiri Pirko 	}
3351bc91b0f0SJiri Pirko }
3352bc91b0f0SJiri Pirko 
33531da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
33541da177e4SLinus Torvalds {
33551da177e4SLinus Torvalds 	struct inet6_dev *idev;
33561da177e4SLinus Torvalds 
33571da177e4SLinus Torvalds 	ASSERT_RTNL();
33581da177e4SLinus Torvalds 
335974235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
336074235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
336174235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
336206a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
3363e837735eSNicolas Dichtel 	    (dev->type != ARPHRD_IEEE1394) &&
3364e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_TUNNEL6) &&
3365cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_6LOWPAN) &&
336645ce0fd1SFelix Jia 	    (dev->type != ARPHRD_TUNNEL) &&
33679deb441cSSubash Abhinov Kasiviswanathan 	    (dev->type != ARPHRD_NONE) &&
33689deb441cSSubash Abhinov Kasiviswanathan 	    (dev->type != ARPHRD_RAWIP)) {
336974235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
337060380488SHangbin Liu 		idev = __in6_dev_get(dev);
337160380488SHangbin Liu 		if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
337260380488SHangbin Liu 		    dev->flags & IFF_MULTICAST)
337360380488SHangbin Liu 			ipv6_mc_up(idev);
337474235a25SHerbert Xu 		return;
337574235a25SHerbert Xu 	}
337674235a25SHerbert Xu 
33771da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
337864e724f6SBrian Haley 	if (IS_ERR(idev))
33791da177e4SLinus Torvalds 		return;
33801da177e4SLinus Torvalds 
3381cc9da6ccSBjørn Mork 	/* this device type has no EUI support */
3382cc9da6ccSBjørn Mork 	if (dev->type == ARPHRD_NONE &&
3383d35a00b8SFelix Jia 	    idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3384d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3385cc9da6ccSBjørn Mork 
3386bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, false);
33871da177e4SLinus Torvalds }
33881da177e4SLinus Torvalds 
338907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
33901da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
33911da177e4SLinus Torvalds {
33921da177e4SLinus Torvalds 	struct inet6_dev *idev;
33931da177e4SLinus Torvalds 
33941da177e4SLinus Torvalds 	ASSERT_RTNL();
33951da177e4SLinus Torvalds 
33961da177e4SLinus Torvalds 	/*
33971da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
33981da177e4SLinus Torvalds 	 * addresses... we should configure all of
33991da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
34001da177e4SLinus Torvalds 	 */
34011da177e4SLinus Torvalds 
3402e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
3403db0b99f5SSabrina Dubroca 	if (IS_ERR(idev)) {
340491df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
34051da177e4SLinus Torvalds 		return;
34061da177e4SLinus Torvalds 	}
34071da177e4SLinus Torvalds 
3408c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
3409bc91b0f0SJiri Pirko 		addrconf_addr_gen(idev, false);
3410c7dc89c0SFred L. Templin 		return;
3411c7dc89c0SFred L. Templin 	}
3412c7dc89c0SFred L. Templin 
3413e5dd7294SAntonio Quartulli 	add_v4_addrs(idev);
34141da177e4SLinus Torvalds 
341562b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
34161da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
34171da177e4SLinus Torvalds }
34180be669bbSJoerg Roedel #endif
34191da177e4SLinus Torvalds 
3420e5dd7294SAntonio Quartulli #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3421aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
3422aee80b54Sstephen hemminger {
3423aee80b54Sstephen hemminger 	struct inet6_dev *idev;
3424aee80b54Sstephen hemminger 
3425aee80b54Sstephen hemminger 	ASSERT_RTNL();
3426aee80b54Sstephen hemminger 
3427e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
3428db0b99f5SSabrina Dubroca 	if (IS_ERR(idev)) {
342991df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
3430aee80b54Sstephen hemminger 		return;
3431aee80b54Sstephen hemminger 	}
3432aee80b54Sstephen hemminger 
3433e5dd7294SAntonio Quartulli 	if (dev->type == ARPHRD_ETHER) {
3434bc91b0f0SJiri Pirko 		addrconf_addr_gen(idev, true);
3435e5dd7294SAntonio Quartulli 		return;
3436e5dd7294SAntonio Quartulli 	}
3437e5dd7294SAntonio Quartulli 
3438e5dd7294SAntonio Quartulli 	add_v4_addrs(idev);
3439e5dd7294SAntonio Quartulli 
3440d9e4ce65SHannes Frederic Sowa 	if (dev->flags & IFF_POINTOPOINT)
3441d9e4ce65SHannes Frederic Sowa 		addrconf_add_mroute(dev);
3442aee80b54Sstephen hemminger }
3443aee80b54Sstephen hemminger #endif
3444aee80b54Sstephen hemminger 
3445afb1d4b5SDavid Ahern static int fixup_permanent_addr(struct net *net,
3446afb1d4b5SDavid Ahern 				struct inet6_dev *idev,
3447f1705ec1SDavid Ahern 				struct inet6_ifaddr *ifp)
3448f1705ec1SDavid Ahern {
344993c2fb25SDavid Ahern 	/* !fib6_node means the host route was removed from the
34508048ced9SDavid Ahern 	 * FIB, for example, if 'lo' device is taken down. In that
34518048ced9SDavid Ahern 	 * case regenerate the host route.
34528048ced9SDavid Ahern 	 */
345393c2fb25SDavid Ahern 	if (!ifp->rt || !ifp->rt->fib6_node) {
3454360a9887SDavid Ahern 		struct fib6_info *f6i, *prev;
3455f1705ec1SDavid Ahern 
3456360a9887SDavid Ahern 		f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3457acb54e3cSDavid Ahern 					 GFP_ATOMIC);
3458360a9887SDavid Ahern 		if (IS_ERR(f6i))
3459360a9887SDavid Ahern 			return PTR_ERR(f6i);
3460f1705ec1SDavid Ahern 
34618048ced9SDavid Ahern 		/* ifp->rt can be accessed outside of rtnl */
34628048ced9SDavid Ahern 		spin_lock(&ifp->lock);
34638048ced9SDavid Ahern 		prev = ifp->rt;
3464360a9887SDavid Ahern 		ifp->rt = f6i;
34658048ced9SDavid Ahern 		spin_unlock(&ifp->lock);
34668048ced9SDavid Ahern 
346793531c67SDavid Ahern 		fib6_info_release(prev);
3468f1705ec1SDavid Ahern 	}
3469f1705ec1SDavid Ahern 
3470f1705ec1SDavid Ahern 	if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3471f1705ec1SDavid Ahern 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
34728308f3ffSDavid Ahern 				      ifp->rt_priority, idev->dev, 0, 0,
34738308f3ffSDavid Ahern 				      GFP_ATOMIC);
3474f1705ec1SDavid Ahern 	}
3475f1705ec1SDavid Ahern 
34766d717134SDavid Ahern 	if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3477f1705ec1SDavid Ahern 		addrconf_dad_start(ifp);
3478f1705ec1SDavid Ahern 
3479f1705ec1SDavid Ahern 	return 0;
3480f1705ec1SDavid Ahern }
3481f1705ec1SDavid Ahern 
3482afb1d4b5SDavid Ahern static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
3483f1705ec1SDavid Ahern {
3484f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifp, *tmp;
3485f1705ec1SDavid Ahern 	struct inet6_dev *idev;
3486f1705ec1SDavid Ahern 
3487f1705ec1SDavid Ahern 	idev = __in6_dev_get(dev);
3488f1705ec1SDavid Ahern 	if (!idev)
3489f1705ec1SDavid Ahern 		return;
3490f1705ec1SDavid Ahern 
3491f1705ec1SDavid Ahern 	write_lock_bh(&idev->lock);
3492f1705ec1SDavid Ahern 
3493f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3494f1705ec1SDavid Ahern 		if ((ifp->flags & IFA_F_PERMANENT) &&
3495afb1d4b5SDavid Ahern 		    fixup_permanent_addr(net, idev, ifp) < 0) {
3496f1705ec1SDavid Ahern 			write_unlock_bh(&idev->lock);
3497e669b869SEric Dumazet 			in6_ifa_hold(ifp);
3498f1705ec1SDavid Ahern 			ipv6_del_addr(ifp);
3499f1705ec1SDavid Ahern 			write_lock_bh(&idev->lock);
3500f1705ec1SDavid Ahern 
3501f1705ec1SDavid Ahern 			net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3502f1705ec1SDavid Ahern 					     idev->dev->name, &ifp->addr);
3503f1705ec1SDavid Ahern 		}
3504f1705ec1SDavid Ahern 	}
3505f1705ec1SDavid Ahern 
3506f1705ec1SDavid Ahern 	write_unlock_bh(&idev->lock);
3507f1705ec1SDavid Ahern }
3508f1705ec1SDavid Ahern 
35091da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
3510351638e7SJiri Pirko 			   void *ptr)
35111da177e4SLinus Torvalds {
3512351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3513896585d4SHangbin Liu 	struct netdev_notifier_change_info *change_info;
35144f7f34eaSDavid Ahern 	struct netdev_notifier_changeupper_info *info;
3515748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
351660abc0beSWANG Cong 	struct net *net = dev_net(dev);
3517c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
3518b217d616SHerbert Xu 	int err;
35191da177e4SLinus Torvalds 
35201da177e4SLinus Torvalds 	switch (event) {
352145ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
352274235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
352345ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
3524a317a2f1SWANG Cong 			if (IS_ERR(idev))
3525a317a2f1SWANG Cong 				return notifier_from_errno(PTR_ERR(idev));
352645ba9dd2SYOSHIFUJI Hideaki 		}
352745ba9dd2SYOSHIFUJI Hideaki 		break;
3528bcdd553fSStephen Hemminger 
3529b7b0b1d2SAlexander Duyck 	case NETDEV_CHANGEMTU:
3530b7b0b1d2SAlexander Duyck 		/* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3531b7b0b1d2SAlexander Duyck 		if (dev->mtu < IPV6_MIN_MTU) {
353260abc0beSWANG Cong 			addrconf_ifdown(dev, dev != net->loopback_dev);
3533b7b0b1d2SAlexander Duyck 			break;
3534b7b0b1d2SAlexander Duyck 		}
3535b7b0b1d2SAlexander Duyck 
3536b7b0b1d2SAlexander Duyck 		if (idev) {
3537b7b0b1d2SAlexander Duyck 			rt6_mtu_change(dev, dev->mtu);
3538b7b0b1d2SAlexander Duyck 			idev->cnf.mtu6 = dev->mtu;
3539b7b0b1d2SAlexander Duyck 			break;
3540b7b0b1d2SAlexander Duyck 		}
3541b7b0b1d2SAlexander Duyck 
3542b7b0b1d2SAlexander Duyck 		/* allocate new idev */
3543b7b0b1d2SAlexander Duyck 		idev = ipv6_add_dev(dev);
3544b7b0b1d2SAlexander Duyck 		if (IS_ERR(idev))
3545b7b0b1d2SAlexander Duyck 			break;
3546b7b0b1d2SAlexander Duyck 
3547b7b0b1d2SAlexander Duyck 		/* device is still not ready */
3548b7b0b1d2SAlexander Duyck 		if (!(idev->if_flags & IF_READY))
3549b7b0b1d2SAlexander Duyck 			break;
3550b7b0b1d2SAlexander Duyck 
3551b7b0b1d2SAlexander Duyck 		run_pending = 1;
3552a8eceea8SJoe Perches 		fallthrough;
35531da177e4SLinus Torvalds 	case NETDEV_UP:
35543c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
3555c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
3556c2edacf8SJay Vosburgh 			break;
3557c2edacf8SJay Vosburgh 
35583ce62a84SWANG Cong 		if (idev && idev->cnf.disable_ipv6)
35593ce62a84SWANG Cong 			break;
35603ce62a84SWANG Cong 
35613c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
356238bd10c4SDavid Ahern 			/* restore routes for permanent addresses */
3563afb1d4b5SDavid Ahern 			addrconf_permanent_addr(net, dev);
356438bd10c4SDavid Ahern 
35651f372c7bSMike Manning 			if (!addrconf_link_ready(dev)) {
35663c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
35677c62b8ddSLubomir Rintel 				pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
35683c21edbdSYOSHIFUJI Hideaki 					 dev->name);
35693c21edbdSYOSHIFUJI Hideaki 				break;
35703c21edbdSYOSHIFUJI Hideaki 			}
357199081049SKristian Slavov 
3572d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
3573d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
3574d31c7b8fSEvgeniy Polyakov 
3575a317a2f1SWANG Cong 			if (!IS_ERR_OR_NULL(idev)) {
357699081049SKristian Slavov 				idev->if_flags |= IF_READY;
3577e3ec6cfcSBenjamin Thery 				run_pending = 1;
3578e3ec6cfcSBenjamin Thery 			}
3579b7b0b1d2SAlexander Duyck 		} else if (event == NETDEV_CHANGE) {
35801f372c7bSMike Manning 			if (!addrconf_link_ready(dev)) {
35813c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
358227c6fa73SIdo Schimmel 				rt6_sync_down_dev(dev, event);
35833c21edbdSYOSHIFUJI Hideaki 				break;
35843c21edbdSYOSHIFUJI Hideaki 			}
35853c21edbdSYOSHIFUJI Hideaki 
3586896585d4SHangbin Liu 			if (!IS_ERR_OR_NULL(idev)) {
3587a088d1d7SLinus Lüssing 				if (idev->if_flags & IF_READY) {
3588a088d1d7SLinus Lüssing 					/* device is already configured -
3589a088d1d7SLinus Lüssing 					 * but resend MLD reports, we might
3590a088d1d7SLinus Lüssing 					 * have roamed and need to update
3591a088d1d7SLinus Lüssing 					 * multicast snooping switches
3592a088d1d7SLinus Lüssing 					 */
3593a088d1d7SLinus Lüssing 					ipv6_mc_up(idev);
3594896585d4SHangbin Liu 					change_info = ptr;
3595896585d4SHangbin Liu 					if (change_info->flags_changed & IFF_NOARP)
3596896585d4SHangbin Liu 						addrconf_dad_run(idev, true);
359727c6fa73SIdo Schimmel 					rt6_sync_up(dev, RTNH_F_LINKDOWN);
35983c21edbdSYOSHIFUJI Hideaki 					break;
3599a088d1d7SLinus Lüssing 				}
36003c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
36013c21edbdSYOSHIFUJI Hideaki 			}
36023c21edbdSYOSHIFUJI Hideaki 
3603f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
36043c21edbdSYOSHIFUJI Hideaki 				dev->name);
36053c21edbdSYOSHIFUJI Hideaki 
3606c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
36073c21edbdSYOSHIFUJI Hideaki 		}
36083c21edbdSYOSHIFUJI Hideaki 
36091da177e4SLinus Torvalds 		switch (dev->type) {
361007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
36111da177e4SLinus Torvalds 		case ARPHRD_SIT:
36121da177e4SLinus Torvalds 			addrconf_sit_config(dev);
36131da177e4SLinus Torvalds 			break;
36140be669bbSJoerg Roedel #endif
3615e5dd7294SAntonio Quartulli #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3616e5dd7294SAntonio Quartulli 		case ARPHRD_IP6GRE:
3617aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
3618aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
3619aee80b54Sstephen hemminger 			break;
3620aee80b54Sstephen hemminger #endif
36211da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
36221da177e4SLinus Torvalds 			init_loopback(dev);
36231da177e4SLinus Torvalds 			break;
36241da177e4SLinus Torvalds 
36251da177e4SLinus Torvalds 		default:
36261da177e4SLinus Torvalds 			addrconf_dev_config(dev);
36271da177e4SLinus Torvalds 			break;
36283ff50b79SStephen Hemminger 		}
3629bcdd553fSStephen Hemminger 
3630a317a2f1SWANG Cong 		if (!IS_ERR_OR_NULL(idev)) {
3631c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
3632896585d4SHangbin Liu 				addrconf_dad_run(idev, false);
3633c5e33bddSYOSHIFUJI Hideaki 
36342127d95aSIdo Schimmel 			/* Device has an address by now */
36352127d95aSIdo Schimmel 			rt6_sync_up(dev, RTNH_F_DEAD);
36362127d95aSIdo Schimmel 
3637bcdd553fSStephen Hemminger 			/*
3638bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
3639bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
3640bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
36411da177e4SLinus Torvalds 			 */
3642bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
3643bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
36441da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
36451da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
36461da177e4SLinus Torvalds 			}
36471da177e4SLinus Torvalds 			idev->tstamp = jiffies;
36481da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
3649bcdd553fSStephen Hemminger 
3650bcdd553fSStephen Hemminger 			/*
3651bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
3652bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
36531da177e4SLinus Torvalds 			 */
36541da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
365560abc0beSWANG Cong 				addrconf_ifdown(dev, dev != net->loopback_dev);
36561da177e4SLinus Torvalds 		}
36571da177e4SLinus Torvalds 		break;
36581da177e4SLinus Torvalds 
36591da177e4SLinus Torvalds 	case NETDEV_DOWN:
36601da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
36611da177e4SLinus Torvalds 		/*
36621da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
36631da177e4SLinus Torvalds 		 */
36641da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
36651da177e4SLinus Torvalds 		break;
36663c21edbdSYOSHIFUJI Hideaki 
36671da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
36681da177e4SLinus Torvalds 		if (idev) {
36695632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
3670408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
3671a317a2f1SWANG Cong 			err = addrconf_sysctl_register(idev);
3672b217d616SHerbert Xu 			if (err)
3673b217d616SHerbert Xu 				return notifier_from_errno(err);
3674a317a2f1SWANG Cong 			err = snmp6_register_dev(idev);
3675a317a2f1SWANG Cong 			if (err) {
3676a317a2f1SWANG Cong 				addrconf_sysctl_unregister(idev);
3677a317a2f1SWANG Cong 				return notifier_from_errno(err);
3678a317a2f1SWANG Cong 			}
36795632c515SStephen Hemminger 		}
36801da177e4SLinus Torvalds 		break;
3681bcdd553fSStephen Hemminger 
368293d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
368393d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
36843ef0952cSAndrew Lunn 		if (idev)
368593d9b7d7SJiri Pirko 			addrconf_type_change(dev, event);
368675c78500SMoni Shoua 		break;
36874f7f34eaSDavid Ahern 
36884f7f34eaSDavid Ahern 	case NETDEV_CHANGEUPPER:
36894f7f34eaSDavid Ahern 		info = ptr;
36904f7f34eaSDavid Ahern 
36914f7f34eaSDavid Ahern 		/* flush all routes if dev is linked to or unlinked from
36924f7f34eaSDavid Ahern 		 * an L3 master device (e.g., VRF)
36934f7f34eaSDavid Ahern 		 */
36944f7f34eaSDavid Ahern 		if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3695ae79dbf6SFlorent Fourcot 			addrconf_ifdown(dev, false);
36963ff50b79SStephen Hemminger 	}
36971da177e4SLinus Torvalds 
36981da177e4SLinus Torvalds 	return NOTIFY_OK;
36991da177e4SLinus Torvalds }
37001da177e4SLinus Torvalds 
37011da177e4SLinus Torvalds /*
37021da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
37031da177e4SLinus Torvalds  */
37041da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
37051da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
3706242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY,
37071da177e4SLinus Torvalds };
37081da177e4SLinus Torvalds 
370993d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
371075c78500SMoni Shoua {
371175c78500SMoni Shoua 	struct inet6_dev *idev;
371275c78500SMoni Shoua 	ASSERT_RTNL();
371375c78500SMoni Shoua 
371475c78500SMoni Shoua 	idev = __in6_dev_get(dev);
371575c78500SMoni Shoua 
371693d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
371775c78500SMoni Shoua 		ipv6_mc_remap(idev);
371893d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
371975c78500SMoni Shoua 		ipv6_mc_unmap(idev);
372075c78500SMoni Shoua }
372175c78500SMoni Shoua 
372270af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr)
372370af921dSDavid Ahern {
372470af921dSDavid Ahern 	return ipv6_addr_type(addr) &
372570af921dSDavid Ahern 		(IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
372670af921dSDavid Ahern }
372770af921dSDavid Ahern 
3728ae79dbf6SFlorent Fourcot static int addrconf_ifdown(struct net_device *dev, bool unregister)
37291da177e4SLinus Torvalds {
3730ae79dbf6SFlorent Fourcot 	unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN;
3731c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
3732502a2ffdSstephen hemminger 	struct inet6_dev *idev;
3733f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifa, *tmp;
37340aef78aaSIvan Vecera 	bool keep_addr = false;
373573a8bd74SDavid S. Miller 	int state, i;
37361da177e4SLinus Torvalds 
37371da177e4SLinus Torvalds 	ASSERT_RTNL();
37381da177e4SLinus Torvalds 
37394c981e28SIdo Schimmel 	rt6_disable_ip(dev, event);
37401da177e4SLinus Torvalds 
37411da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
374263159f29SIan Morris 	if (!idev)
37431da177e4SLinus Torvalds 		return -ENODEV;
37441da177e4SLinus Torvalds 
3745bcdd553fSStephen Hemminger 	/*
3746bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3747bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
37481da177e4SLinus Torvalds 	 */
3749ae79dbf6SFlorent Fourcot 	if (unregister) {
37501da177e4SLinus Torvalds 		idev->dead = 1;
37518814c4b5SYOSHIFUJI Hideaki 
37528814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3753a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
37541da177e4SLinus Torvalds 
37551da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
37561da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
37571da177e4SLinus Torvalds 
37581da177e4SLinus Torvalds 	}
37591da177e4SLinus Torvalds 
3760f1705ec1SDavid Ahern 	/* combine the user config with event to determine if permanent
3761f1705ec1SDavid Ahern 	 * addresses are to be removed from address hash table
3762f1705ec1SDavid Ahern 	 */
3763ae79dbf6SFlorent Fourcot 	if (!unregister && !idev->cnf.disable_ipv6) {
37640aef78aaSIvan Vecera 		/* aggregate the system setting and interface setting */
37650aef78aaSIvan Vecera 		int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
37660aef78aaSIvan Vecera 
37670aef78aaSIvan Vecera 		if (!_keep_addr)
37680aef78aaSIvan Vecera 			_keep_addr = idev->cnf.keep_addr_on_down;
37690aef78aaSIvan Vecera 
37700aef78aaSIvan Vecera 		keep_addr = (_keep_addr > 0);
37710aef78aaSIvan Vecera 	}
3772f1705ec1SDavid Ahern 
377373a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
377473a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
377573a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
377673a8bd74SDavid S. Miller 
377773a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
377873a8bd74SDavid S. Miller restart:
3779b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
378073a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
3781c15b1ccaSHannes Frederic Sowa 				addrconf_del_dad_work(ifa);
3782f1705ec1SDavid Ahern 				/* combined flag + permanent flag decide if
3783f1705ec1SDavid Ahern 				 * address is retained on a down event
3784f1705ec1SDavid Ahern 				 */
3785f1705ec1SDavid Ahern 				if (!keep_addr ||
378670af921dSDavid Ahern 				    !(ifa->flags & IFA_F_PERMANENT) ||
378770af921dSDavid Ahern 				    addr_is_local(&ifa->addr)) {
3788f1705ec1SDavid Ahern 					hlist_del_init_rcu(&ifa->addr_lst);
378973a8bd74SDavid S. Miller 					goto restart;
379073a8bd74SDavid S. Miller 				}
379173a8bd74SDavid S. Miller 			}
3792f1705ec1SDavid Ahern 		}
379373a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
379473a8bd74SDavid S. Miller 	}
379573a8bd74SDavid S. Miller 
37961da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
37971da177e4SLinus Torvalds 
3798b7b1bfceSHannes Frederic Sowa 	addrconf_del_rs_timer(idev);
3799b7b1bfceSHannes Frederic Sowa 
3800bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3801ae79dbf6SFlorent Fourcot 	if (!unregister)
38023c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
38031da177e4SLinus Torvalds 
3804bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3805372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3806372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3807372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3808372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
38091da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
38101da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
38111da177e4SLinus Torvalds 
38121da177e4SLinus Torvalds 		if (ifa->ifpub) {
38131da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
38141da177e4SLinus Torvalds 			ifa->ifpub = NULL;
38151da177e4SLinus Torvalds 		}
38161da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
38171da177e4SLinus Torvalds 		in6_ifa_put(ifa);
38181da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
38191da177e4SLinus Torvalds 	}
38208f37ada5Sstephen hemminger 
3821f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
38228d1c802bSDavid Ahern 		struct fib6_info *rt = NULL;
3823a2d6cbb0SRabin Vincent 		bool keep;
382438bd10c4SDavid Ahern 
3825c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifa);
382684e8b803Sstephen hemminger 
3827a2d6cbb0SRabin Vincent 		keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3828a2d6cbb0SRabin Vincent 			!addr_is_local(&ifa->addr);
3829a2d6cbb0SRabin Vincent 
38304c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
38318e8e676dSHannes Frederic Sowa 		spin_lock_bh(&ifa->lock);
3832f1705ec1SDavid Ahern 
3833a2d6cbb0SRabin Vincent 		if (keep) {
3834f1705ec1SDavid Ahern 			/* set state to skip the notifier below */
3835f1705ec1SDavid Ahern 			state = INET6_IFADDR_STATE_DEAD;
38366d717134SDavid Ahern 			ifa->state = INET6_IFADDR_STATE_PREDAD;
3837f1705ec1SDavid Ahern 			if (!(ifa->flags & IFA_F_NODAD))
3838f1705ec1SDavid Ahern 				ifa->flags |= IFA_F_TENTATIVE;
383938bd10c4SDavid Ahern 
384038bd10c4SDavid Ahern 			rt = ifa->rt;
384138bd10c4SDavid Ahern 			ifa->rt = NULL;
3842f1705ec1SDavid Ahern 		} else {
38434c5ff6a6SHerbert Xu 			state = ifa->state;
38444c5ff6a6SHerbert Xu 			ifa->state = INET6_IFADDR_STATE_DEAD;
3845f1705ec1SDavid Ahern 		}
3846f1705ec1SDavid Ahern 
38478e8e676dSHannes Frederic Sowa 		spin_unlock_bh(&ifa->lock);
38484c5ff6a6SHerbert Xu 
384938bd10c4SDavid Ahern 		if (rt)
385011dd74b3SRoopa Prabhu 			ip6_del_rt(net, rt, false);
385138bd10c4SDavid Ahern 
38529d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
38531da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
3854f88c91ddSCong Wang 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3855ea06f717SMike Manning 		} else {
3856ea06f717SMike Manning 			if (idev->cnf.forwarding)
3857ea06f717SMike Manning 				addrconf_leave_anycast(ifa);
3858ea06f717SMike Manning 			addrconf_leave_solict(ifa->idev, &ifa->addr);
38592de79570SLorenzo Colitti 		}
386073a8bd74SDavid S. Miller 
38611da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
38628ef802aaSEric Dumazet 		if (!keep) {
38638ef802aaSEric Dumazet 			list_del_rcu(&ifa->if_list);
38648ef802aaSEric Dumazet 			in6_ifa_put(ifa);
38658ef802aaSEric Dumazet 		}
38661da177e4SLinus Torvalds 	}
38678f37ada5Sstephen hemminger 
38681da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
38691da177e4SLinus Torvalds 
3870381f4dcaSSabrina Dubroca 	/* Step 5: Discard anycast and multicast list */
3871ae79dbf6SFlorent Fourcot 	if (unregister) {
3872381f4dcaSSabrina Dubroca 		ipv6_ac_destroy_dev(idev);
38731da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
3874381f4dcaSSabrina Dubroca 	} else {
38751da177e4SLinus Torvalds 		ipv6_mc_down(idev);
3876381f4dcaSSabrina Dubroca 	}
38771da177e4SLinus Torvalds 
38781da177e4SLinus Torvalds 	idev->tstamp = jiffies;
387949b99da2SRocco Yue 	idev->ra_mtu = 0;
38801da177e4SLinus Torvalds 
3881bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3882ae79dbf6SFlorent Fourcot 	if (unregister) {
3883408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
38841da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
38851da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
38861da177e4SLinus Torvalds 		in6_dev_put(idev);
38871da177e4SLinus Torvalds 	}
38881da177e4SLinus Torvalds 	return 0;
38891da177e4SLinus Torvalds }
38901da177e4SLinus Torvalds 
3891e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t)
38921da177e4SLinus Torvalds {
3893e99e88a9SKees Cook 	struct inet6_dev *idev = from_timer(idev, t, rs_timer);
3894caf92bc4SCong Wang 	struct net_device *dev = idev->dev;
3895b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
38961da177e4SLinus Torvalds 
3897b7b1bfceSHannes Frederic Sowa 	write_lock(&idev->lock);
38985b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
38991da177e4SLinus Torvalds 		goto out;
39001da177e4SLinus Torvalds 
39019ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
39021da177e4SLinus Torvalds 		goto out;
39035b2a1953Sstephen hemminger 
39045b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
39055b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
39065b2a1953Sstephen hemminger 		goto out;
39071da177e4SLinus Torvalds 
3908bd11f074SMaciej Żenczykowski 	if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3909caf92bc4SCong Wang 		write_unlock(&idev->lock);
3910caf92bc4SCong Wang 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3911caf92bc4SCong Wang 			ndisc_send_rs(dev, &lladdr,
3912b7b1bfceSHannes Frederic Sowa 				      &in6addr_linklocal_allrouters);
3913b7b1bfceSHannes Frederic Sowa 		else
3914caf92bc4SCong Wang 			goto put;
3915b7b1bfceSHannes Frederic Sowa 
3916caf92bc4SCong Wang 		write_lock(&idev->lock);
3917bd11f074SMaciej Żenczykowski 		idev->rs_interval = rfc3315_s14_backoff_update(
3918bd11f074SMaciej Żenczykowski 			idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
39191da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
3920b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3921b7b1bfceSHannes Frederic Sowa 					     idev->cnf.rtr_solicits) ?
39225b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_delay :
3923bd11f074SMaciej Żenczykowski 				      idev->rs_interval);
39241da177e4SLinus Torvalds 	} else {
39251da177e4SLinus Torvalds 		/*
39261da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
39271da177e4SLinus Torvalds 		 * assumption any longer.
39281da177e4SLinus Torvalds 		 */
392991df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
39301da177e4SLinus Torvalds 	}
39311da177e4SLinus Torvalds 
39321da177e4SLinus Torvalds out:
3933b7b1bfceSHannes Frederic Sowa 	write_unlock(&idev->lock);
3934caf92bc4SCong Wang put:
3935b7b1bfceSHannes Frederic Sowa 	in6_dev_put(idev);
39361da177e4SLinus Torvalds }
39371da177e4SLinus Torvalds 
39381da177e4SLinus Torvalds /*
39391da177e4SLinus Torvalds  *	Duplicate Address Detection
39401da177e4SLinus Torvalds  */
39413c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
39423c21edbdSYOSHIFUJI Hideaki {
39433c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
39443c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
3945adc176c5SErik Nordmark 	u64 nonce;
39463c21edbdSYOSHIFUJI Hideaki 
394795c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
394895c385b4SNeil Horman 		rand_num = 0;
394995c385b4SNeil Horman 	else
395063862b5bSAruna-Hewapathirane 		rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
395195c385b4SNeil Horman 
3952adc176c5SErik Nordmark 	nonce = 0;
3953adc176c5SErik Nordmark 	if (idev->cnf.enhanced_dad ||
3954adc176c5SErik Nordmark 	    dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3955adc176c5SErik Nordmark 		do
3956adc176c5SErik Nordmark 			get_random_bytes(&nonce, 6);
3957adc176c5SErik Nordmark 		while (nonce == 0);
3958adc176c5SErik Nordmark 	}
3959adc176c5SErik Nordmark 	ifp->dad_nonce = nonce;
3960b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes = idev->cnf.dad_transmits;
3961c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, rand_num);
39623c21edbdSYOSHIFUJI Hideaki }
39633c21edbdSYOSHIFUJI Hideaki 
3964c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
39651da177e4SLinus Torvalds {
39661da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
39671da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3968764d3be6SPaolo Abeni 	bool bump_id, notify = false;
3969afb1d4b5SDavid Ahern 	struct net *net;
39701da177e4SLinus Torvalds 
39711da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
39721da177e4SLinus Torvalds 
397363862b5bSAruna-Hewapathirane 	prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
39741da177e4SLinus Torvalds 
39751da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3976622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3977e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
39781da177e4SLinus Torvalds 		goto out;
39791da177e4SLinus Torvalds 
3980afb1d4b5SDavid Ahern 	net = dev_net(dev);
39811da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3982afb1d4b5SDavid Ahern 	    (net->ipv6.devconf_all->accept_dad < 1 &&
3983a2d3f3e3SMatteo Croce 	     idev->cnf.accept_dad < 1) ||
398455ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
398555ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3986c76fe2d9SDavid Ahern 		bool send_na = false;
3987c76fe2d9SDavid Ahern 
3988c76fe2d9SDavid Ahern 		if (ifp->flags & IFA_F_TENTATIVE &&
3989c76fe2d9SDavid Ahern 		    !(ifp->flags & IFA_F_OPTIMISTIC))
3990c76fe2d9SDavid Ahern 			send_na = true;
3991764d3be6SPaolo Abeni 		bump_id = ifp->flags & IFA_F_TENTATIVE;
3992cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
399321809fafSstephen hemminger 		spin_unlock(&ifp->lock);
39941da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
39951da177e4SLinus Torvalds 
3996c76fe2d9SDavid Ahern 		addrconf_dad_completed(ifp, bump_id, send_na);
39971da177e4SLinus Torvalds 		return;
39981da177e4SLinus Torvalds 	}
39991da177e4SLinus Torvalds 
40006732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
400121809fafSstephen hemminger 		spin_unlock(&ifp->lock);
40026732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
40033c21edbdSYOSHIFUJI Hideaki 		/*
4004590a9887SMasatake YAMATO 		 * If the device is not ready:
40053c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
40063c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
40073c21edbdSYOSHIFUJI Hideaki 		 */
40083c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
4009cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
40106732badeSYOSHIFUJI Hideaki 		return;
40113c21edbdSYOSHIFUJI Hideaki 	}
401295c385b4SNeil Horman 
401395c385b4SNeil Horman 	/*
401495c385b4SNeil Horman 	 * Optimistic nodes can start receiving
401595c385b4SNeil Horman 	 * Frames right away
401695c385b4SNeil Horman 	 */
40177fd2561eSErik Kline 	if (ifp->flags & IFA_F_OPTIMISTIC) {
4018afb1d4b5SDavid Ahern 		ip6_ins_rt(net, ifp->rt);
4019afb1d4b5SDavid Ahern 		if (ipv6_use_optimistic_addr(net, idev)) {
40207fd2561eSErik Kline 			/* Because optimistic nodes can use this address,
40217fd2561eSErik Kline 			 * notify listeners. If DAD fails, RTM_DELADDR is sent.
40227fd2561eSErik Kline 			 */
402316186a82Ssubashab@codeaurora.org 			notify = true;
40247fd2561eSErik Kline 		}
40257fd2561eSErik Kline 	}
402695c385b4SNeil Horman 
40276732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
40281da177e4SLinus Torvalds out:
4029622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
40301da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
403116186a82Ssubashab@codeaurora.org 	if (notify)
403216186a82Ssubashab@codeaurora.org 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
40331da177e4SLinus Torvalds }
40341da177e4SLinus Torvalds 
4035c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp)
40361da177e4SLinus Torvalds {
4037c15b1ccaSHannes Frederic Sowa 	bool begin_dad = false;
4038c15b1ccaSHannes Frederic Sowa 
40398e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
4040c15b1ccaSHannes Frederic Sowa 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4041c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_PREDAD;
4042c15b1ccaSHannes Frederic Sowa 		begin_dad = true;
4043c15b1ccaSHannes Frederic Sowa 	}
40448e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
4045c15b1ccaSHannes Frederic Sowa 
4046c15b1ccaSHannes Frederic Sowa 	if (begin_dad)
4047c15b1ccaSHannes Frederic Sowa 		addrconf_mod_dad_work(ifp, 0);
4048c15b1ccaSHannes Frederic Sowa }
4049c15b1ccaSHannes Frederic Sowa 
4050c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w)
4051c15b1ccaSHannes Frederic Sowa {
4052c15b1ccaSHannes Frederic Sowa 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4053c15b1ccaSHannes Frederic Sowa 						struct inet6_ifaddr,
4054c15b1ccaSHannes Frederic Sowa 						dad_work);
40551da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
4056764d3be6SPaolo Abeni 	bool bump_id, disable_ipv6 = false;
40571da177e4SLinus Torvalds 	struct in6_addr mcaddr;
40581da177e4SLinus Torvalds 
4059c15b1ccaSHannes Frederic Sowa 	enum {
4060c15b1ccaSHannes Frederic Sowa 		DAD_PROCESS,
4061c15b1ccaSHannes Frederic Sowa 		DAD_BEGIN,
4062c15b1ccaSHannes Frederic Sowa 		DAD_ABORT,
4063c15b1ccaSHannes Frederic Sowa 	} action = DAD_PROCESS;
4064c15b1ccaSHannes Frederic Sowa 
4065c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
4066c15b1ccaSHannes Frederic Sowa 
40678e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
4068c15b1ccaSHannes Frederic Sowa 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4069c15b1ccaSHannes Frederic Sowa 		action = DAD_BEGIN;
4070c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_DAD;
4071c15b1ccaSHannes Frederic Sowa 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4072c15b1ccaSHannes Frederic Sowa 		action = DAD_ABORT;
4073c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
407485b51b12SMike Manning 
407535e015e1SMatteo Croce 		if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
407635e015e1SMatteo Croce 		     idev->cnf.accept_dad > 1) &&
407735e015e1SMatteo Croce 		    !idev->cnf.disable_ipv6 &&
407885b51b12SMike Manning 		    !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
407985b51b12SMike Manning 			struct in6_addr addr;
408085b51b12SMike Manning 
408185b51b12SMike Manning 			addr.s6_addr32[0] = htonl(0xfe800000);
408285b51b12SMike Manning 			addr.s6_addr32[1] = 0;
408385b51b12SMike Manning 
408485b51b12SMike Manning 			if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
408585b51b12SMike Manning 			    ipv6_addr_equal(&ifp->addr, &addr)) {
408685b51b12SMike Manning 				/* DAD failed for link-local based on MAC */
408785b51b12SMike Manning 				idev->cnf.disable_ipv6 = 1;
408885b51b12SMike Manning 
408985b51b12SMike Manning 				pr_info("%s: IPv6 being disabled!\n",
409085b51b12SMike Manning 					ifp->idev->dev->name);
409185b51b12SMike Manning 				disable_ipv6 = true;
409285b51b12SMike Manning 			}
409385b51b12SMike Manning 		}
4094c15b1ccaSHannes Frederic Sowa 	}
40958e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
4096c15b1ccaSHannes Frederic Sowa 
4097c15b1ccaSHannes Frederic Sowa 	if (action == DAD_BEGIN) {
4098c15b1ccaSHannes Frederic Sowa 		addrconf_dad_begin(ifp);
4099c15b1ccaSHannes Frederic Sowa 		goto out;
4100c15b1ccaSHannes Frederic Sowa 	} else if (action == DAD_ABORT) {
4101751eb6b6SWei Yongjun 		in6_ifa_hold(ifp);
4102c15b1ccaSHannes Frederic Sowa 		addrconf_dad_stop(ifp, 1);
410385b51b12SMike Manning 		if (disable_ipv6)
4104ae79dbf6SFlorent Fourcot 			addrconf_ifdown(idev->dev, false);
4105c15b1ccaSHannes Frederic Sowa 		goto out;
4106c15b1ccaSHannes Frederic Sowa 	}
4107c15b1ccaSHannes Frederic Sowa 
4108b7b1bfceSHannes Frederic Sowa 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
4109f2344a13SHerbert Xu 		goto out;
4110f2344a13SHerbert Xu 
4111c15b1ccaSHannes Frederic Sowa 	write_lock_bh(&idev->lock);
4112122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
4113c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
41141da177e4SLinus Torvalds 		goto out;
41151da177e4SLinus Torvalds 	}
411621809fafSstephen hemminger 
411721809fafSstephen hemminger 	spin_lock(&ifp->lock);
4118622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4119622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
4120c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
4121622ccdf1SHerbert Xu 		goto out;
4122622ccdf1SHerbert Xu 	}
4123622ccdf1SHerbert Xu 
4124b7b1bfceSHannes Frederic Sowa 	if (ifp->dad_probes == 0) {
4125c76fe2d9SDavid Ahern 		bool send_na = false;
4126c76fe2d9SDavid Ahern 
41271da177e4SLinus Torvalds 		/*
41281da177e4SLinus Torvalds 		 * DAD was successful
41291da177e4SLinus Torvalds 		 */
41301da177e4SLinus Torvalds 
4131c76fe2d9SDavid Ahern 		if (ifp->flags & IFA_F_TENTATIVE &&
4132c76fe2d9SDavid Ahern 		    !(ifp->flags & IFA_F_OPTIMISTIC))
4133c76fe2d9SDavid Ahern 			send_na = true;
4134764d3be6SPaolo Abeni 		bump_id = ifp->flags & IFA_F_TENTATIVE;
4135cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
413621809fafSstephen hemminger 		spin_unlock(&ifp->lock);
4137c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
41381da177e4SLinus Torvalds 
4139c76fe2d9SDavid Ahern 		addrconf_dad_completed(ifp, bump_id, send_na);
41401da177e4SLinus Torvalds 
41411da177e4SLinus Torvalds 		goto out;
41421da177e4SLinus Torvalds 	}
41431da177e4SLinus Torvalds 
4144b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes--;
4145c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp,
414619e16d22SHangbin Liu 			      max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME),
414719e16d22SHangbin Liu 				  HZ/100));
414821809fafSstephen hemminger 	spin_unlock(&ifp->lock);
4149c15b1ccaSHannes Frederic Sowa 	write_unlock_bh(&idev->lock);
41501da177e4SLinus Torvalds 
41511da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
41521da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
4153adc176c5SErik Nordmark 	ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4154adc176c5SErik Nordmark 		      ifp->dad_nonce);
41551da177e4SLinus Torvalds out:
41561da177e4SLinus Torvalds 	in6_ifa_put(ifp);
4157c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
41581da177e4SLinus Torvalds }
41591da177e4SLinus Torvalds 
416011ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */
416111ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
416211ffff75SHannes Frederic Sowa {
416311ffff75SHannes Frederic Sowa 	struct inet6_ifaddr *ifpiter;
416411ffff75SHannes Frederic Sowa 	struct inet6_dev *idev = ifp->idev;
416511ffff75SHannes Frederic Sowa 
4166602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4167602582caSHannes Frederic Sowa 		if (ifpiter->scope > IFA_LINK)
4168602582caSHannes Frederic Sowa 			break;
416911ffff75SHannes Frederic Sowa 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
417011ffff75SHannes Frederic Sowa 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
417111ffff75SHannes Frederic Sowa 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
417211ffff75SHannes Frederic Sowa 		    IFA_F_PERMANENT)
417311ffff75SHannes Frederic Sowa 			return false;
417411ffff75SHannes Frederic Sowa 	}
417511ffff75SHannes Frederic Sowa 	return true;
417611ffff75SHannes Frederic Sowa }
417711ffff75SHannes Frederic Sowa 
4178c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4179c76fe2d9SDavid Ahern 				   bool send_na)
41801da177e4SLinus Torvalds {
41811da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
4182b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
4183b173ee48SHannes Frederic Sowa 	bool send_rs, send_mld;
4184b7b1bfceSHannes Frederic Sowa 
4185c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
41861da177e4SLinus Torvalds 
41871da177e4SLinus Torvalds 	/*
41881da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
41891da177e4SLinus Torvalds 	 */
41901da177e4SLinus Torvalds 
41911da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
41921da177e4SLinus Torvalds 
4193026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
4194026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
41951da177e4SLinus Torvalds 	 */
41961da177e4SLinus Torvalds 
41971ec047ebSHannes Frederic Sowa 	read_lock_bh(&ifp->idev->lock);
419811ffff75SHannes Frederic Sowa 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4199b173ee48SHannes Frederic Sowa 	send_rs = send_mld &&
4200b173ee48SHannes Frederic Sowa 		  ipv6_accept_ra(ifp->idev) &&
4201bd11f074SMaciej Żenczykowski 		  ifp->idev->cnf.rtr_solicits != 0 &&
4202b173ee48SHannes Frederic Sowa 		  (dev->flags&IFF_LOOPBACK) == 0;
42031ec047ebSHannes Frederic Sowa 	read_unlock_bh(&ifp->idev->lock);
42041ec047ebSHannes Frederic Sowa 
4205b173ee48SHannes Frederic Sowa 	/* While dad is in progress mld report's source address is in6_addrany.
4206b173ee48SHannes Frederic Sowa 	 * Resend with proper ll now.
4207b173ee48SHannes Frederic Sowa 	 */
4208b173ee48SHannes Frederic Sowa 	if (send_mld)
4209b173ee48SHannes Frederic Sowa 		ipv6_mc_dad_complete(ifp->idev);
4210b173ee48SHannes Frederic Sowa 
4211c76fe2d9SDavid Ahern 	/* send unsolicited NA if enabled */
4212c76fe2d9SDavid Ahern 	if (send_na &&
4213c76fe2d9SDavid Ahern 	    (ifp->idev->cnf.ndisc_notify ||
4214c76fe2d9SDavid Ahern 	     dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
4215c76fe2d9SDavid Ahern 		ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4216c76fe2d9SDavid Ahern 			      /*router=*/ !!ifp->idev->cnf.forwarding,
4217c76fe2d9SDavid Ahern 			      /*solicited=*/ false, /*override=*/ true,
4218c76fe2d9SDavid Ahern 			      /*inc_opt=*/ true);
4219c76fe2d9SDavid Ahern 	}
4220c76fe2d9SDavid Ahern 
42211ec047ebSHannes Frederic Sowa 	if (send_rs) {
42221da177e4SLinus Torvalds 		/*
42231da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
42241da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
42251da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
42261da177e4SLinus Torvalds 		 */
42271ec047ebSHannes Frederic Sowa 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4228b7b1bfceSHannes Frederic Sowa 			return;
42291ec047ebSHannes Frederic Sowa 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
42301da177e4SLinus Torvalds 
4231b7b1bfceSHannes Frederic Sowa 		write_lock_bh(&ifp->idev->lock);
4232b7b1bfceSHannes Frederic Sowa 		spin_lock(&ifp->lock);
4233bd11f074SMaciej Żenczykowski 		ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4234bd11f074SMaciej Żenczykowski 			ifp->idev->cnf.rtr_solicit_interval);
4235b7b1bfceSHannes Frederic Sowa 		ifp->idev->rs_probes = 1;
42361da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
4237bd11f074SMaciej Żenczykowski 		addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4238b7b1bfceSHannes Frederic Sowa 		spin_unlock(&ifp->lock);
4239b7b1bfceSHannes Frederic Sowa 		write_unlock_bh(&ifp->idev->lock);
42401da177e4SLinus Torvalds 	}
4241764d3be6SPaolo Abeni 
4242764d3be6SPaolo Abeni 	if (bump_id)
4243764d3be6SPaolo Abeni 		rt_genid_bump_ipv6(dev_net(dev));
4244a11a7f71SMarcus Huewe 
4245a11a7f71SMarcus Huewe 	/* Make sure that a new temporary address will be created
4246a11a7f71SMarcus Huewe 	 * before this temporary address becomes deprecated.
4247a11a7f71SMarcus Huewe 	 */
4248a11a7f71SMarcus Huewe 	if (ifp->flags & IFA_F_TEMPORARY)
4249a11a7f71SMarcus Huewe 		addrconf_verify_rtnl();
42501da177e4SLinus Torvalds }
42511da177e4SLinus Torvalds 
4252896585d4SHangbin Liu static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
4253e21e8467SStephen Hemminger {
4254c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
4255c5e33bddSYOSHIFUJI Hideaki 
4256c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
4257502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
425821809fafSstephen hemminger 		spin_lock(&ifp->lock);
4259896585d4SHangbin Liu 		if ((ifp->flags & IFA_F_TENTATIVE &&
4260896585d4SHangbin Liu 		     ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4261896585d4SHangbin Liu 			if (restart)
4262896585d4SHangbin Liu 				ifp->state = INET6_IFADDR_STATE_PREDAD;
4263c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
4264896585d4SHangbin Liu 		}
4265f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
4266c5e33bddSYOSHIFUJI Hideaki 	}
4267c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
4268c5e33bddSYOSHIFUJI Hideaki }
4269c5e33bddSYOSHIFUJI Hideaki 
42701da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
42711da177e4SLinus Torvalds struct if6_iter_state {
42723c40090aSDaniel Lezcano 	struct seq_net_private p;
42731da177e4SLinus Torvalds 	int bucket;
42741d578303SMihai Maruseac 	int offset;
42751da177e4SLinus Torvalds };
42761da177e4SLinus Torvalds 
42771d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
42781da177e4SLinus Torvalds {
42791da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
42801218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
4281a5c1d98fSEric Dumazet 	struct inet6_ifaddr *ifa = NULL;
42821d578303SMihai Maruseac 	int p = 0;
42831da177e4SLinus Torvalds 
42841d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
42851d578303SMihai Maruseac 	if (pos == 0) {
42861d578303SMihai Maruseac 		state->bucket = 0;
42871d578303SMihai Maruseac 		state->offset = 0;
42881d578303SMihai Maruseac 	}
42891d578303SMihai Maruseac 
42901d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4291a5c1d98fSEric Dumazet 		hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket],
42921d578303SMihai Maruseac 					 addr_lst) {
42939f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
42949f0d3c27SEric Dumazet 				continue;
42951d578303SMihai Maruseac 			/* sync with offset */
42961d578303SMihai Maruseac 			if (p < state->offset) {
42971d578303SMihai Maruseac 				p++;
42981d578303SMihai Maruseac 				continue;
42991d578303SMihai Maruseac 			}
43001da177e4SLinus Torvalds 			return ifa;
43011da177e4SLinus Torvalds 		}
43021d578303SMihai Maruseac 
43031d578303SMihai Maruseac 		/* prepare for next bucket */
43041d578303SMihai Maruseac 		state->offset = 0;
43051d578303SMihai Maruseac 		p = 0;
43061d578303SMihai Maruseac 	}
4307c2e21293Sstephen hemminger 	return NULL;
4308c2e21293Sstephen hemminger }
43091da177e4SLinus Torvalds 
4310c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4311c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
43121da177e4SLinus Torvalds {
43131da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
43141218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
43151da177e4SLinus Torvalds 
4316a5c1d98fSEric Dumazet 	hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
43179f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
43189f0d3c27SEric Dumazet 			continue;
43191d578303SMihai Maruseac 		state->offset++;
43201da177e4SLinus Torvalds 		return ifa;
43211d578303SMihai Maruseac 	}
43221da177e4SLinus Torvalds 
43231d578303SMihai Maruseac 	state->offset = 0;
432486f9bd1fSJeff Barnhill 	while (++state->bucket < IN6_ADDR_HSIZE) {
4325a5c1d98fSEric Dumazet 		hlist_for_each_entry_rcu(ifa,
4326c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
43279f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
43289f0d3c27SEric Dumazet 				continue;
4329c2e21293Sstephen hemminger 			return ifa;
4330c2e21293Sstephen hemminger 		}
4331c2e21293Sstephen hemminger 	}
4332c2e21293Sstephen hemminger 
4333c2e21293Sstephen hemminger 	return NULL;
4334c2e21293Sstephen hemminger }
4335c2e21293Sstephen hemminger 
43361da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4337a5c1d98fSEric Dumazet 	__acquires(rcu)
43381da177e4SLinus Torvalds {
4339a5c1d98fSEric Dumazet 	rcu_read_lock();
43401d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
43411da177e4SLinus Torvalds }
43421da177e4SLinus Torvalds 
43431da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
43441da177e4SLinus Torvalds {
43451da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
43461da177e4SLinus Torvalds 
43471da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
43481da177e4SLinus Torvalds 	++*pos;
43491da177e4SLinus Torvalds 	return ifa;
43501da177e4SLinus Torvalds }
43511da177e4SLinus Torvalds 
43521da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
4353a5c1d98fSEric Dumazet 	__releases(rcu)
43541da177e4SLinus Torvalds {
4355a5c1d98fSEric Dumazet 	rcu_read_unlock();
43561da177e4SLinus Torvalds }
43571da177e4SLinus Torvalds 
43581da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
43591da177e4SLinus Torvalds {
43601da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4361971a351cSJiri Pirko 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4362b071195dSHarvey Harrison 		   &ifp->addr,
43631da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
43641da177e4SLinus Torvalds 		   ifp->prefix_len,
43651da177e4SLinus Torvalds 		   ifp->scope,
4366971a351cSJiri Pirko 		   (u8) ifp->flags,
43671da177e4SLinus Torvalds 		   ifp->idev->dev->name);
43681da177e4SLinus Torvalds 	return 0;
43691da177e4SLinus Torvalds }
43701da177e4SLinus Torvalds 
437156b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
43721da177e4SLinus Torvalds 	.start	= if6_seq_start,
43731da177e4SLinus Torvalds 	.next	= if6_seq_next,
43741da177e4SLinus Torvalds 	.show	= if6_seq_show,
43751da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
43761da177e4SLinus Torvalds };
43771da177e4SLinus Torvalds 
43782c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
43791da177e4SLinus Torvalds {
4380c3506372SChristoph Hellwig 	if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4381c3506372SChristoph Hellwig 			sizeof(struct if6_iter_state)))
43821da177e4SLinus Torvalds 		return -ENOMEM;
43831da177e4SLinus Torvalds 	return 0;
43841da177e4SLinus Torvalds }
43851da177e4SLinus Torvalds 
43862c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
43873c40090aSDaniel Lezcano {
4388ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
43893c40090aSDaniel Lezcano }
43903c40090aSDaniel Lezcano 
43913c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
43923c40090aSDaniel Lezcano 	.init = if6_proc_net_init,
43933c40090aSDaniel Lezcano 	.exit = if6_proc_net_exit,
43943c40090aSDaniel Lezcano };
43953c40090aSDaniel Lezcano 
43963c40090aSDaniel Lezcano int __init if6_proc_init(void)
43973c40090aSDaniel Lezcano {
43983c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
43993c40090aSDaniel Lezcano }
44003c40090aSDaniel Lezcano 
44011da177e4SLinus Torvalds void if6_proc_exit(void)
44021da177e4SLinus Torvalds {
44033c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
44041da177e4SLinus Torvalds }
44051da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
44061da177e4SLinus Torvalds 
440707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
44083b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
4409b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
44103b9f9a1cSNoriaki TAKAMIYA {
44113f27fb23SEric Dumazet 	unsigned int hash = inet6_addr_hash(net, addr);
4412c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
44133f27fb23SEric Dumazet 	int ret = 0;
4414c2e21293Sstephen hemminger 
44154e5f47abSEric Dumazet 	rcu_read_lock();
44164e5f47abSEric Dumazet 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
4417878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
4418389f6612SDaniel Lezcano 			continue;
4419caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
44203b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
44213b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
44223b9f9a1cSNoriaki TAKAMIYA 			break;
44233b9f9a1cSNoriaki TAKAMIYA 		}
44243b9f9a1cSNoriaki TAKAMIYA 	}
44254e5f47abSEric Dumazet 	rcu_read_unlock();
44263b9f9a1cSNoriaki TAKAMIYA 	return ret;
44273b9f9a1cSNoriaki TAKAMIYA }
44283b9f9a1cSNoriaki TAKAMIYA #endif
44293b9f9a1cSNoriaki TAKAMIYA 
4430f37c6059SAlexander Aring /* RFC6554 has some algorithm to avoid loops in segment routing by
4431f37c6059SAlexander Aring  * checking if the segments contains any of a local interface address.
4432f37c6059SAlexander Aring  *
4433f37c6059SAlexander Aring  * Quote:
4434f37c6059SAlexander Aring  *
4435f37c6059SAlexander Aring  * To detect loops in the SRH, a router MUST determine if the SRH
4436f37c6059SAlexander Aring  * includes multiple addresses assigned to any interface on that router.
4437f37c6059SAlexander Aring  * If such addresses appear more than once and are separated by at least
4438f37c6059SAlexander Aring  * one address not assigned to that router.
4439f37c6059SAlexander Aring  */
4440f37c6059SAlexander Aring int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
4441f37c6059SAlexander Aring 			  unsigned char nsegs)
4442f37c6059SAlexander Aring {
4443f37c6059SAlexander Aring 	const struct in6_addr *addr;
4444f37c6059SAlexander Aring 	int i, ret = 0, found = 0;
4445f37c6059SAlexander Aring 	struct inet6_ifaddr *ifp;
4446f37c6059SAlexander Aring 	bool separated = false;
4447f37c6059SAlexander Aring 	unsigned int hash;
4448f37c6059SAlexander Aring 	bool hash_found;
4449f37c6059SAlexander Aring 
4450f37c6059SAlexander Aring 	rcu_read_lock();
4451f37c6059SAlexander Aring 	for (i = 0; i < nsegs; i++) {
4452f37c6059SAlexander Aring 		addr = &segs[i];
4453f37c6059SAlexander Aring 		hash = inet6_addr_hash(net, addr);
4454f37c6059SAlexander Aring 
4455f37c6059SAlexander Aring 		hash_found = false;
4456f37c6059SAlexander Aring 		hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
4457f37c6059SAlexander Aring 			if (!net_eq(dev_net(ifp->idev->dev), net))
4458f37c6059SAlexander Aring 				continue;
4459f37c6059SAlexander Aring 
4460f37c6059SAlexander Aring 			if (ipv6_addr_equal(&ifp->addr, addr)) {
4461f37c6059SAlexander Aring 				hash_found = true;
4462f37c6059SAlexander Aring 				break;
4463f37c6059SAlexander Aring 			}
4464f37c6059SAlexander Aring 		}
4465f37c6059SAlexander Aring 
4466f37c6059SAlexander Aring 		if (hash_found) {
4467f37c6059SAlexander Aring 			if (found > 1 && separated) {
4468f37c6059SAlexander Aring 				ret = 1;
4469f37c6059SAlexander Aring 				break;
4470f37c6059SAlexander Aring 			}
4471f37c6059SAlexander Aring 
4472f37c6059SAlexander Aring 			separated = false;
4473f37c6059SAlexander Aring 			found++;
4474f37c6059SAlexander Aring 		} else {
4475f37c6059SAlexander Aring 			separated = true;
4476f37c6059SAlexander Aring 		}
4477f37c6059SAlexander Aring 	}
4478f37c6059SAlexander Aring 	rcu_read_unlock();
4479f37c6059SAlexander Aring 
4480f37c6059SAlexander Aring 	return ret;
4481f37c6059SAlexander Aring }
4482f37c6059SAlexander Aring 
44831da177e4SLinus Torvalds /*
44841da177e4SLinus Torvalds  *	Periodic address status verification
44851da177e4SLinus Torvalds  */
44861da177e4SLinus Torvalds 
4487c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void)
44881da177e4SLinus Torvalds {
4489b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
44901da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
44911da177e4SLinus Torvalds 	int i;
44921da177e4SLinus Torvalds 
4493c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4494c15b1ccaSHannes Frederic Sowa 
44955c578aedSstephen hemminger 	rcu_read_lock_bh();
44961da177e4SLinus Torvalds 	now = jiffies;
4497b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
44981da177e4SLinus Torvalds 
4499c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
45001da177e4SLinus Torvalds 
45011da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
45021da177e4SLinus Torvalds restart:
4503c15b1ccaSHannes Frederic Sowa 		hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
45041da177e4SLinus Torvalds 			unsigned long age;
45051da177e4SLinus Torvalds 
4506fad8da3eSYasushi Asano 			/* When setting preferred_lft to a value not zero or
4507fad8da3eSYasushi Asano 			 * infinity, while valid_lft is infinity
4508fad8da3eSYasushi Asano 			 * IFA_F_PERMANENT has a non-infinity life time.
4509fad8da3eSYasushi Asano 			 */
4510fad8da3eSYasushi Asano 			if ((ifp->flags & IFA_F_PERMANENT) &&
4511fad8da3eSYasushi Asano 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
45121da177e4SLinus Torvalds 				continue;
45131da177e4SLinus Torvalds 
45141da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
4515b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
4516b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
45171da177e4SLinus Torvalds 
45188f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
45198f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
45201da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
45211da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
4522aa8caa76STaehee Yoo 				rcu_read_unlock_bh();
45231da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
4524aa8caa76STaehee Yoo 				rcu_read_lock_bh();
45251da177e4SLinus Torvalds 				goto restart;
45268f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
45278f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
45288f27ebb9SYOSHIFUJI Hideaki 				continue;
45291da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
4530a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
45311da177e4SLinus Torvalds 				int deprecate = 0;
45321da177e4SLinus Torvalds 
45331da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
45341da177e4SLinus Torvalds 					deprecate = 1;
45351da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
45361da177e4SLinus Torvalds 				}
45371da177e4SLinus Torvalds 
4538fad8da3eSYasushi Asano 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4539fad8da3eSYasushi Asano 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
45401da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
45411da177e4SLinus Torvalds 
45421da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
45431da177e4SLinus Torvalds 
45441da177e4SLinus Torvalds 				if (deprecate) {
45451da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
45461da177e4SLinus Torvalds 
45471da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
45481da177e4SLinus Torvalds 					in6_ifa_put(ifp);
45491da177e4SLinus Torvalds 					goto restart;
45501da177e4SLinus Torvalds 				}
45511da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
45521da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
455388949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
455488949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
455519e16d22SHangbin Liu 					max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
455688949cf4Sstephen hemminger 
45571da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
45581da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
45591da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
45601da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
45611da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
45621da177e4SLinus Torvalds 						ifp->regen_count++;
45631da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
45641da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
45651da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
45665c578aedSstephen hemminger 
4567291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
4568291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
4569291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
4570e64e469bSEric Dumazet 						rcu_read_unlock_bh();
4571969c5464SFernando Gont 						ipv6_create_tempaddr(ifpub, true);
45721da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
45731da177e4SLinus Torvalds 						in6_ifa_put(ifp);
4574e64e469bSEric Dumazet 						rcu_read_lock_bh();
45751da177e4SLinus Torvalds 						goto restart;
45761da177e4SLinus Torvalds 					}
45771da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
45781da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
45791da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
45801da177e4SLinus Torvalds 			} else {
45811da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
45821da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
45831da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
45841da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
45851da177e4SLinus Torvalds 			}
45861da177e4SLinus Torvalds 		}
45871da177e4SLinus Torvalds 	}
45881da177e4SLinus Torvalds 
4589b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
4590b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
4591b2db7564SYOSHIFUJI Hideaki 
4592b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
4593b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4594b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
4595b2db7564SYOSHIFUJI Hideaki 
4596b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4597b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4598b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4599b2db7564SYOSHIFUJI Hideaki 
4600e32ac250SJoe Perches 	pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4601ba3542e1Sdingtianhong 		 now, next, next_sec, next_sched);
4602c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
46035c578aedSstephen hemminger 	rcu_read_unlock_bh();
46041da177e4SLinus Torvalds }
46051da177e4SLinus Torvalds 
4606c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w)
4607c15b1ccaSHannes Frederic Sowa {
4608c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
4609c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4610c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
4611c15b1ccaSHannes Frederic Sowa }
4612c15b1ccaSHannes Frederic Sowa 
4613c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void)
4614c15b1ccaSHannes Frederic Sowa {
4615c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4616c15b1ccaSHannes Frederic Sowa }
4617c15b1ccaSHannes Frederic Sowa 
4618caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4619caeaba79SNicolas Dichtel 				     struct in6_addr **peer_pfx)
4620461d8837SThomas Graf {
4621461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
4622461d8837SThomas Graf 
4623caeaba79SNicolas Dichtel 	*peer_pfx = NULL;
4624caeaba79SNicolas Dichtel 
4625461d8837SThomas Graf 	if (addr)
4626461d8837SThomas Graf 		pfx = nla_data(addr);
4627461d8837SThomas Graf 
4628461d8837SThomas Graf 	if (local) {
4629461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4630caeaba79SNicolas Dichtel 			*peer_pfx = pfx;
4631461d8837SThomas Graf 		pfx = nla_data(local);
4632461d8837SThomas Graf 	}
4633461d8837SThomas Graf 
4634461d8837SThomas Graf 	return pfx;
4635461d8837SThomas Graf }
4636461d8837SThomas Graf 
4637ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4638461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
4639461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
4640461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
4641479840ffSJiri Pirko 	[IFA_FLAGS]		= { .len = sizeof(u32) },
46428308f3ffSDavid Ahern 	[IFA_RT_PRIORITY]	= { .len = sizeof(u32) },
46436ecf4c37SChristian Brauner 	[IFA_TARGET_NETNSID]	= { .type = NLA_S32 },
4644461d8837SThomas Graf };
4645461d8837SThomas Graf 
46461da177e4SLinus Torvalds static int
4647c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4648c21ef3e3SDavid Ahern 		  struct netlink_ext_ack *extack)
46491da177e4SLinus Torvalds {
46503b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4651b933f716SThomas Graf 	struct ifaddrmsg *ifm;
4652b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4653caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
46546046d5b4SHeiner Kallweit 	u32 ifa_flags;
4655b933f716SThomas Graf 	int err;
46561da177e4SLinus Torvalds 
46578cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
46588cb08174SJohannes Berg 				     ifa_ipv6_policy, extack);
4659b933f716SThomas Graf 	if (err < 0)
4660b933f716SThomas Graf 		return err;
4661b933f716SThomas Graf 
4662b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
4663caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
466463159f29SIan Morris 	if (!pfx)
46651da177e4SLinus Torvalds 		return -EINVAL;
46661da177e4SLinus Torvalds 
46676046d5b4SHeiner Kallweit 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
46686046d5b4SHeiner Kallweit 
46696046d5b4SHeiner Kallweit 	/* We ignore other flags so far. */
46706046d5b4SHeiner Kallweit 	ifa_flags &= IFA_F_MANAGETEMPADDR;
46716046d5b4SHeiner Kallweit 
46726046d5b4SHeiner Kallweit 	return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
46736046d5b4SHeiner Kallweit 			      ifm->ifa_prefixlen);
46741da177e4SLinus Torvalds }
46751da177e4SLinus Torvalds 
46768308f3ffSDavid Ahern static int modify_prefix_route(struct inet6_ifaddr *ifp,
467761794012SHangbin Liu 			       unsigned long expires, u32 flags,
467861794012SHangbin Liu 			       bool modify_peer)
46798308f3ffSDavid Ahern {
46808308f3ffSDavid Ahern 	struct fib6_info *f6i;
4681e7c7faa9SDavid Ahern 	u32 prio;
46828308f3ffSDavid Ahern 
468361794012SHangbin Liu 	f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
468461794012SHangbin Liu 					ifp->prefix_len,
46852b2450caSDavid Ahern 					ifp->idev->dev, 0, RTF_DEFAULT, true);
46868308f3ffSDavid Ahern 	if (!f6i)
46878308f3ffSDavid Ahern 		return -ENOENT;
46888308f3ffSDavid Ahern 
4689e7c7faa9SDavid Ahern 	prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4690e7c7faa9SDavid Ahern 	if (f6i->fib6_metric != prio) {
4691e7c7faa9SDavid Ahern 		/* delete old one */
469211dd74b3SRoopa Prabhu 		ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
4693e7c7faa9SDavid Ahern 
46948308f3ffSDavid Ahern 		/* add new one */
469561794012SHangbin Liu 		addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
469661794012SHangbin Liu 				      ifp->prefix_len,
46978308f3ffSDavid Ahern 				      ifp->rt_priority, ifp->idev->dev,
46988308f3ffSDavid Ahern 				      expires, flags, GFP_KERNEL);
46998308f3ffSDavid Ahern 	} else {
47008308f3ffSDavid Ahern 		if (!expires)
47018308f3ffSDavid Ahern 			fib6_clean_expires(f6i);
47028308f3ffSDavid Ahern 		else
47038308f3ffSDavid Ahern 			fib6_set_expires(f6i, expires);
47048308f3ffSDavid Ahern 
47058308f3ffSDavid Ahern 		fib6_info_release(f6i);
47068308f3ffSDavid Ahern 	}
47078308f3ffSDavid Ahern 
47088308f3ffSDavid Ahern 	return 0;
47098308f3ffSDavid Ahern }
47108308f3ffSDavid Ahern 
4711d169a1f8SDavid Ahern static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg)
4712081bba5bSNoriaki TAKAMIYA {
47136f704992SYOSHIFUJI Hideaki 	u32 flags;
47146f704992SYOSHIFUJI Hideaki 	clock_t expires;
47154bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
471653bd6749SJiri Pirko 	bool was_managetempaddr;
47175b84efecSThomas Haller 	bool had_prefixroute;
4718d0098e4cSHangbin Liu 	bool new_peer = false;
471946d48046SYOSHIFUJI Hideaki 
4720c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4721c15b1ccaSHannes Frederic Sowa 
4722d169a1f8SDavid Ahern 	if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
4723081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
4724081bba5bSNoriaki TAKAMIYA 
4725d169a1f8SDavid Ahern 	if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
472653bd6749SJiri Pirko 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
472753bd6749SJiri Pirko 		return -EINVAL;
472853bd6749SJiri Pirko 
4729f1c02cfbSSabrina Dubroca 	if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
4730d169a1f8SDavid Ahern 		cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
4731f1c02cfbSSabrina Dubroca 
4732d169a1f8SDavid Ahern 	timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
47334bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
47344bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
4735d169a1f8SDavid Ahern 		cfg->valid_lft = timeout;
47366f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
47374bed72e4SYOSHIFUJI Hideaki 	} else {
47384bed72e4SYOSHIFUJI Hideaki 		expires = 0;
47394bed72e4SYOSHIFUJI Hideaki 		flags = 0;
4740d169a1f8SDavid Ahern 		cfg->ifa_flags |= IFA_F_PERMANENT;
47416f704992SYOSHIFUJI Hideaki 	}
4742081bba5bSNoriaki TAKAMIYA 
4743d169a1f8SDavid Ahern 	timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
47444bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
47454bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
4746d169a1f8SDavid Ahern 			cfg->ifa_flags |= IFA_F_DEPRECATED;
4747d169a1f8SDavid Ahern 		cfg->preferred_lft = timeout;
47484bed72e4SYOSHIFUJI Hideaki 	}
4749081bba5bSNoriaki TAKAMIYA 
4750d0098e4cSHangbin Liu 	if (cfg->peer_pfx &&
4751d0098e4cSHangbin Liu 	    memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
4752d0098e4cSHangbin Liu 		if (!ipv6_addr_any(&ifp->peer_addr))
4753d0098e4cSHangbin Liu 			cleanup_prefix_route(ifp, expires, true, true);
4754d0098e4cSHangbin Liu 		new_peer = true;
4755d0098e4cSHangbin Liu 	}
4756d0098e4cSHangbin Liu 
4757081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
475853bd6749SJiri Pirko 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
47595b84efecSThomas Haller 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
47605b84efecSThomas Haller 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
476153bd6749SJiri Pirko 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4762761aac73SThomas Haller 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4763761aac73SThomas Haller 			IFA_F_NOPREFIXROUTE);
4764d169a1f8SDavid Ahern 	ifp->flags |= cfg->ifa_flags;
4765081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
4766d169a1f8SDavid Ahern 	ifp->valid_lft = cfg->valid_lft;
4767d169a1f8SDavid Ahern 	ifp->prefered_lft = cfg->preferred_lft;
4768081bba5bSNoriaki TAKAMIYA 
47698308f3ffSDavid Ahern 	if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
47708308f3ffSDavid Ahern 		ifp->rt_priority = cfg->rt_priority;
4771081bba5bSNoriaki TAKAMIYA 
4772d0098e4cSHangbin Liu 	if (new_peer)
4773d0098e4cSHangbin Liu 		ifp->peer_addr = *cfg->peer_pfx;
4774d0098e4cSHangbin Liu 
4775081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
4776081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
4777081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
4778081bba5bSNoriaki TAKAMIYA 
4779d169a1f8SDavid Ahern 	if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
47808308f3ffSDavid Ahern 		int rc = -ENOENT;
47818308f3ffSDavid Ahern 
47828308f3ffSDavid Ahern 		if (had_prefixroute)
478361794012SHangbin Liu 			rc = modify_prefix_route(ifp, expires, flags, false);
47848308f3ffSDavid Ahern 
47858308f3ffSDavid Ahern 		/* prefix route could have been deleted; if so restore it */
47868308f3ffSDavid Ahern 		if (rc == -ENOENT) {
4787acb54e3cSDavid Ahern 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
47888308f3ffSDavid Ahern 					      ifp->rt_priority, ifp->idev->dev,
47898308f3ffSDavid Ahern 					      expires, flags, GFP_KERNEL);
47908308f3ffSDavid Ahern 		}
479161794012SHangbin Liu 
479261794012SHangbin Liu 		if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
479361794012SHangbin Liu 			rc = modify_prefix_route(ifp, expires, flags, true);
479461794012SHangbin Liu 
479561794012SHangbin Liu 		if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
479661794012SHangbin Liu 			addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
479761794012SHangbin Liu 					      ifp->rt_priority, ifp->idev->dev,
479861794012SHangbin Liu 					      expires, flags, GFP_KERNEL);
479961794012SHangbin Liu 		}
48005b84efecSThomas Haller 	} else if (had_prefixroute) {
48015b84efecSThomas Haller 		enum cleanup_prefix_rt_t action;
48025b84efecSThomas Haller 		unsigned long rt_expires;
48035b84efecSThomas Haller 
48045b84efecSThomas Haller 		write_lock_bh(&ifp->idev->lock);
48055b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &rt_expires);
48065b84efecSThomas Haller 		write_unlock_bh(&ifp->idev->lock);
48075b84efecSThomas Haller 
48085b84efecSThomas Haller 		if (action != CLEANUP_PREFIX_RT_NOP) {
48095b84efecSThomas Haller 			cleanup_prefix_route(ifp, rt_expires,
4810d0098e4cSHangbin Liu 				action == CLEANUP_PREFIX_RT_DEL, false);
48115b84efecSThomas Haller 		}
4812761aac73SThomas Haller 	}
481353bd6749SJiri Pirko 
481453bd6749SJiri Pirko 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4815d169a1f8SDavid Ahern 		if (was_managetempaddr &&
4816d169a1f8SDavid Ahern 		    !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
4817d169a1f8SDavid Ahern 			cfg->valid_lft = 0;
4818d169a1f8SDavid Ahern 			cfg->preferred_lft = 0;
4819d169a1f8SDavid Ahern 		}
4820d169a1f8SDavid Ahern 		manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4821d169a1f8SDavid Ahern 				 cfg->preferred_lft, !was_managetempaddr,
4822d169a1f8SDavid Ahern 				 jiffies);
482353bd6749SJiri Pirko 	}
482453bd6749SJiri Pirko 
4825c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4826081bba5bSNoriaki TAKAMIYA 
4827081bba5bSNoriaki TAKAMIYA 	return 0;
4828081bba5bSNoriaki TAKAMIYA }
4829081bba5bSNoriaki TAKAMIYA 
4830081bba5bSNoriaki TAKAMIYA static int
4831c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4832c21ef3e3SDavid Ahern 		  struct netlink_ext_ack *extack)
48331da177e4SLinus Torvalds {
48343b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4835461d8837SThomas Graf 	struct ifaddrmsg *ifm;
4836461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
483719b1518cSDavid Ahern 	struct in6_addr *peer_pfx;
48387198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
48397198f8ceSThomas Graf 	struct net_device *dev;
4840f1c02cfbSSabrina Dubroca 	struct inet6_dev *idev;
484119b1518cSDavid Ahern 	struct ifa6_config cfg;
4842461d8837SThomas Graf 	int err;
48431da177e4SLinus Torvalds 
48448cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
48458cb08174SJohannes Berg 				     ifa_ipv6_policy, extack);
4846461d8837SThomas Graf 	if (err < 0)
4847461d8837SThomas Graf 		return err;
4848461d8837SThomas Graf 
484919b1518cSDavid Ahern 	memset(&cfg, 0, sizeof(cfg));
485019b1518cSDavid Ahern 
4851461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
485219b1518cSDavid Ahern 	cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
485319b1518cSDavid Ahern 	if (!cfg.pfx)
48541da177e4SLinus Torvalds 		return -EINVAL;
48551da177e4SLinus Torvalds 
485619b1518cSDavid Ahern 	cfg.peer_pfx = peer_pfx;
485719b1518cSDavid Ahern 	cfg.plen = ifm->ifa_prefixlen;
48588308f3ffSDavid Ahern 	if (tb[IFA_RT_PRIORITY])
48598308f3ffSDavid Ahern 		cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
48608308f3ffSDavid Ahern 
486119b1518cSDavid Ahern 	cfg.valid_lft = INFINITY_LIFE_TIME;
486219b1518cSDavid Ahern 	cfg.preferred_lft = INFINITY_LIFE_TIME;
486319b1518cSDavid Ahern 
4864461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
48650778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
4866461d8837SThomas Graf 
4867461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
486819b1518cSDavid Ahern 		cfg.valid_lft = ci->ifa_valid;
486919b1518cSDavid Ahern 		cfg.preferred_lft = ci->ifa_prefered;
48700778769dSNoriaki TAKAMIYA 	}
48710778769dSNoriaki TAKAMIYA 
4872af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
487363159f29SIan Morris 	if (!dev)
48747198f8ceSThomas Graf 		return -ENODEV;
4875081bba5bSNoriaki TAKAMIYA 
487619b1518cSDavid Ahern 	if (tb[IFA_FLAGS])
487719b1518cSDavid Ahern 		cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
487819b1518cSDavid Ahern 	else
487919b1518cSDavid Ahern 		cfg.ifa_flags = ifm->ifa_flags;
4880479840ffSJiri Pirko 
488155ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
488219b1518cSDavid Ahern 	cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
488319b1518cSDavid Ahern 			 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
488419b1518cSDavid Ahern 			 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
4885f1c02cfbSSabrina Dubroca 
4886f1c02cfbSSabrina Dubroca 	idev = ipv6_find_idev(dev);
4887db0b99f5SSabrina Dubroca 	if (IS_ERR(idev))
4888db0b99f5SSabrina Dubroca 		return PTR_ERR(idev);
4889f1c02cfbSSabrina Dubroca 
4890f1c02cfbSSabrina Dubroca 	if (!ipv6_allow_optimistic_dad(net, idev))
489119b1518cSDavid Ahern 		cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
4892f1c02cfbSSabrina Dubroca 
489319b1518cSDavid Ahern 	if (cfg.ifa_flags & IFA_F_NODAD &&
489419b1518cSDavid Ahern 	    cfg.ifa_flags & IFA_F_OPTIMISTIC) {
4895f1c02cfbSSabrina Dubroca 		NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4896f1c02cfbSSabrina Dubroca 		return -EINVAL;
4897f1c02cfbSSabrina Dubroca 	}
489855ebaef1SNoriaki TAKAMIYA 
489919b1518cSDavid Ahern 	ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
490063159f29SIan Morris 	if (!ifa) {
49017198f8ceSThomas Graf 		/*
49027198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
4903db9c7c39Sstephen hemminger 		 * userspace already relies on not having to provide this.
49047198f8ceSThomas Graf 		 */
490519b1518cSDavid Ahern 		return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
49061da177e4SLinus Torvalds 	}
49071da177e4SLinus Torvalds 
49087198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
49097198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
49107198f8ceSThomas Graf 		err = -EEXIST;
49117198f8ceSThomas Graf 	else
4912d169a1f8SDavid Ahern 		err = inet6_addr_modify(ifa, &cfg);
49137198f8ceSThomas Graf 
49147198f8ceSThomas Graf 	in6_ifa_put(ifa);
49157198f8ceSThomas Graf 
49167198f8ceSThomas Graf 	return err;
49177198f8ceSThomas Graf }
49187198f8ceSThomas Graf 
4919479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4920101bb229SThomas Graf 			  u8 scope, int ifindex)
4921101bb229SThomas Graf {
4922101bb229SThomas Graf 	struct ifaddrmsg *ifm;
4923101bb229SThomas Graf 
4924101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
4925101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
4926101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
4927101bb229SThomas Graf 	ifm->ifa_flags = flags;
4928101bb229SThomas Graf 	ifm->ifa_scope = scope;
4929101bb229SThomas Graf 	ifm->ifa_index = ifindex;
4930101bb229SThomas Graf }
4931101bb229SThomas Graf 
493285486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
493385486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
493485486af0SThomas Graf {
493585486af0SThomas Graf 	struct ifa_cacheinfo ci;
493685486af0SThomas Graf 
493718a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
493818a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
493985486af0SThomas Graf 	ci.ifa_prefered = preferred;
494085486af0SThomas Graf 	ci.ifa_valid = valid;
494185486af0SThomas Graf 
494285486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
494385486af0SThomas Graf }
494485486af0SThomas Graf 
4945101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
4946101bb229SThomas Graf {
4947101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
4948101bb229SThomas Graf 		return RT_SCOPE_HOST;
4949101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
4950101bb229SThomas Graf 		return RT_SCOPE_LINK;
4951101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
4952101bb229SThomas Graf 		return RT_SCOPE_SITE;
4953101bb229SThomas Graf 	else
4954101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
4955101bb229SThomas Graf }
4956101bb229SThomas Graf 
49570ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
49580ab6803bSThomas Graf {
4959339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4960caeaba79SNicolas Dichtel 	       + nla_total_size(16) /* IFA_LOCAL */
4961339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
4962479840ffSJiri Pirko 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
49638308f3ffSDavid Ahern 	       + nla_total_size(4)  /* IFA_FLAGS */
49648308f3ffSDavid Ahern 	       + nla_total_size(4)  /* IFA_RT_PRIORITY */;
49650ab6803bSThomas Graf }
4966c5396a31SYOSHIFUJI Hideaki 
49676ba1e6e8SDavid Ahern enum addr_type_t {
49686ba1e6e8SDavid Ahern 	UNICAST_ADDR,
49696ba1e6e8SDavid Ahern 	MULTICAST_ADDR,
49706ba1e6e8SDavid Ahern 	ANYCAST_ADDR,
49716ba1e6e8SDavid Ahern };
49726ba1e6e8SDavid Ahern 
4973203651b6SChristian Brauner struct inet6_fill_args {
4974203651b6SChristian Brauner 	u32 portid;
4975203651b6SChristian Brauner 	u32 seq;
4976203651b6SChristian Brauner 	int event;
4977203651b6SChristian Brauner 	unsigned int flags;
4978203651b6SChristian Brauner 	int netnsid;
49796371a71fSDavid Ahern 	int ifindex;
49806ba1e6e8SDavid Ahern 	enum addr_type_t type;
4981203651b6SChristian Brauner };
4982203651b6SChristian Brauner 
49831da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4984203651b6SChristian Brauner 			     struct inet6_fill_args *args)
49851da177e4SLinus Torvalds {
49861da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
498785486af0SThomas Graf 	u32 preferred, valid;
49881da177e4SLinus Torvalds 
4989203651b6SChristian Brauner 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4990203651b6SChristian Brauner 			sizeof(struct ifaddrmsg), args->flags);
499163159f29SIan Morris 	if (!nlh)
499226932566SPatrick McHardy 		return -EMSGSIZE;
49930ab6803bSThomas Graf 
4994101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4995101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
4996101bb229SThomas Graf 
4997203651b6SChristian Brauner 	if (args->netnsid >= 0 &&
4998203651b6SChristian Brauner 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
49996ecf4c37SChristian Brauner 		goto error;
50006ecf4c37SChristian Brauner 
5001fad8da3eSYasushi Asano 	if (!((ifa->flags&IFA_F_PERMANENT) &&
5002fad8da3eSYasushi Asano 	      (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
500385486af0SThomas Graf 		preferred = ifa->prefered_lft;
500485486af0SThomas Graf 		valid = ifa->valid_lft;
500585486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
50061da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
5007a1faa698SJens Rosenboom 			if (preferred > tval)
500885486af0SThomas Graf 				preferred -= tval;
5009a1faa698SJens Rosenboom 			else
5010a1faa698SJens Rosenboom 				preferred = 0;
5011f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
5012f56619fcSBen Hutchings 				if (valid > tval)
501385486af0SThomas Graf 					valid -= tval;
5014f56619fcSBen Hutchings 				else
5015f56619fcSBen Hutchings 					valid = 0;
5016f56619fcSBen Hutchings 			}
50171da177e4SLinus Torvalds 		}
50181da177e4SLinus Torvalds 	} else {
501985486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
502085486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
50211da177e4SLinus Torvalds 	}
502285486af0SThomas Graf 
50237996c799SCong Wang 	if (!ipv6_addr_any(&ifa->peer_addr)) {
5024930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
5025930345eaSJiri Benc 		    nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
5026caeaba79SNicolas Dichtel 			goto error;
5027caeaba79SNicolas Dichtel 	} else
5028930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
5029caeaba79SNicolas Dichtel 			goto error;
5030caeaba79SNicolas Dichtel 
50318308f3ffSDavid Ahern 	if (ifa->rt_priority &&
50328308f3ffSDavid Ahern 	    nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
50338308f3ffSDavid Ahern 		goto error;
50348308f3ffSDavid Ahern 
5035caeaba79SNicolas Dichtel 	if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
5036caeaba79SNicolas Dichtel 		goto error;
503785486af0SThomas Graf 
5038479840ffSJiri Pirko 	if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
5039479840ffSJiri Pirko 		goto error;
5040479840ffSJiri Pirko 
5041053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5042053c095aSJohannes Berg 	return 0;
5043caeaba79SNicolas Dichtel 
5044caeaba79SNicolas Dichtel error:
5045caeaba79SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
5046caeaba79SNicolas Dichtel 	return -EMSGSIZE;
50471da177e4SLinus Torvalds }
50481da177e4SLinus Torvalds 
50491da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
5050203651b6SChristian Brauner 			       struct inet6_fill_args *args)
50511da177e4SLinus Torvalds {
50521da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
5053101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
5054101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
50551da177e4SLinus Torvalds 
50561da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
5057101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
5058101bb229SThomas Graf 
5059203651b6SChristian Brauner 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5060203651b6SChristian Brauner 			sizeof(struct ifaddrmsg), args->flags);
506163159f29SIan Morris 	if (!nlh)
506226932566SPatrick McHardy 		return -EMSGSIZE;
50630ab6803bSThomas Graf 
5064203651b6SChristian Brauner 	if (args->netnsid >= 0 &&
5065ceb736e1SZhang Qilong 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5066ceb736e1SZhang Qilong 		nlmsg_cancel(skb, nlh);
50676ecf4c37SChristian Brauner 		return -EMSGSIZE;
5068ceb736e1SZhang Qilong 	}
50696ecf4c37SChristian Brauner 
5070101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5071930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
50720ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
507326932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
507426932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
507526932566SPatrick McHardy 		return -EMSGSIZE;
507626932566SPatrick McHardy 	}
507785486af0SThomas Graf 
5078053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5079053c095aSJohannes Berg 	return 0;
50801da177e4SLinus Torvalds }
50811da177e4SLinus Torvalds 
50821da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
5083203651b6SChristian Brauner 			       struct inet6_fill_args *args)
50841da177e4SLinus Torvalds {
50859ee8cbb2SDavid Ahern 	struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
50869ee8cbb2SDavid Ahern 	int ifindex = dev ? dev->ifindex : 1;
50871da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
5088101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
50891da177e4SLinus Torvalds 
50901da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
5091101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
5092101bb229SThomas Graf 
5093203651b6SChristian Brauner 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5094203651b6SChristian Brauner 			sizeof(struct ifaddrmsg), args->flags);
509563159f29SIan Morris 	if (!nlh)
509626932566SPatrick McHardy 		return -EMSGSIZE;
50970ab6803bSThomas Graf 
5098203651b6SChristian Brauner 	if (args->netnsid >= 0 &&
5099ceb736e1SZhang Qilong 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5100ceb736e1SZhang Qilong 		nlmsg_cancel(skb, nlh);
51016ecf4c37SChristian Brauner 		return -EMSGSIZE;
5102ceb736e1SZhang Qilong 	}
51036ecf4c37SChristian Brauner 
5104101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5105930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
51060ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
510726932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
510826932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
510926932566SPatrick McHardy 		return -EMSGSIZE;
511026932566SPatrick McHardy 	}
511185486af0SThomas Graf 
5112053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5113053c095aSJohannes Berg 	return 0;
51141da177e4SLinus Torvalds }
51151da177e4SLinus Torvalds 
5116234b27c3SEric Dumazet /* called with rcu_read_lock() */
5117234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
5118fe884c2bSDavid Ahern 			  struct netlink_callback *cb, int s_ip_idx,
51196ba1e6e8SDavid Ahern 			  struct inet6_fill_args *fillargs)
51201da177e4SLinus Torvalds {
51211da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
51221da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
5123fe884c2bSDavid Ahern 	int ip_idx = 0;
5124234b27c3SEric Dumazet 	int err = 1;
51251da177e4SLinus Torvalds 
51261da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
51276ba1e6e8SDavid Ahern 	switch (fillargs->type) {
5128502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
5129502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
51306ba1e6e8SDavid Ahern 		fillargs->event = RTM_NEWADDR;
5131502a2ffdSstephen hemminger 
5132ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
5133502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
51344ba4c566SDavid Ahern 			if (ip_idx < s_ip_idx)
51354ba4c566SDavid Ahern 				goto next;
51366ba1e6e8SDavid Ahern 			err = inet6_fill_ifaddr(skb, ifa, fillargs);
5137053c095aSJohannes Berg 			if (err < 0)
5138bcd32326SEric Dumazet 				break;
513963998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
51404ba4c566SDavid Ahern next:
51414ba4c566SDavid Ahern 			ip_idx++;
51421da177e4SLinus Torvalds 		}
51431da177e4SLinus Torvalds 		break;
5144502a2ffdSstephen hemminger 	}
51451da177e4SLinus Torvalds 	case MULTICAST_ADDR:
514688e2ca30STaehee Yoo 		read_unlock_bh(&idev->lock);
51476ba1e6e8SDavid Ahern 		fillargs->event = RTM_GETMULTICAST;
5148203651b6SChristian Brauner 
51491da177e4SLinus Torvalds 		/* multicast address */
515088e2ca30STaehee Yoo 		for (ifmca = rcu_dereference(idev->mc_list);
515188e2ca30STaehee Yoo 		     ifmca;
515288e2ca30STaehee Yoo 		     ifmca = rcu_dereference(ifmca->next), ip_idx++) {
51531da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
51541da177e4SLinus Torvalds 				continue;
51556ba1e6e8SDavid Ahern 			err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
5156053c095aSJohannes Berg 			if (err < 0)
5157bcd32326SEric Dumazet 				break;
51581da177e4SLinus Torvalds 		}
515988e2ca30STaehee Yoo 		read_lock_bh(&idev->lock);
51601da177e4SLinus Torvalds 		break;
51611da177e4SLinus Torvalds 	case ANYCAST_ADDR:
51626ba1e6e8SDavid Ahern 		fillargs->event = RTM_GETANYCAST;
51631da177e4SLinus Torvalds 		/* anycast address */
51641da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
51651da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
51661da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
51671da177e4SLinus Torvalds 				continue;
51686ba1e6e8SDavid Ahern 			err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
5169053c095aSJohannes Berg 			if (err < 0)
5170bcd32326SEric Dumazet 				break;
51711da177e4SLinus Torvalds 		}
51721da177e4SLinus Torvalds 		break;
51731da177e4SLinus Torvalds 	default:
51741da177e4SLinus Torvalds 		break;
51751da177e4SLinus Torvalds 	}
51761da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
5177fe884c2bSDavid Ahern 	cb->args[2] = ip_idx;
5178234b27c3SEric Dumazet 	return err;
5179234b27c3SEric Dumazet }
51805d5619b4SYOSHIFUJI Hideaki 
5181ed6eff11SDavid Ahern static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5182ed6eff11SDavid Ahern 				       struct inet6_fill_args *fillargs,
5183ed6eff11SDavid Ahern 				       struct net **tgt_net, struct sock *sk,
51846371a71fSDavid Ahern 				       struct netlink_callback *cb)
5185ed6eff11SDavid Ahern {
51866371a71fSDavid Ahern 	struct netlink_ext_ack *extack = cb->extack;
5187ed6eff11SDavid Ahern 	struct nlattr *tb[IFA_MAX+1];
5188ed6eff11SDavid Ahern 	struct ifaddrmsg *ifm;
5189ed6eff11SDavid Ahern 	int err, i;
5190ed6eff11SDavid Ahern 
5191ed6eff11SDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5192ed6eff11SDavid Ahern 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5193ed6eff11SDavid Ahern 		return -EINVAL;
5194ed6eff11SDavid Ahern 	}
5195ed6eff11SDavid Ahern 
5196ed6eff11SDavid Ahern 	ifm = nlmsg_data(nlh);
5197ed6eff11SDavid Ahern 	if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5198ed6eff11SDavid Ahern 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5199ed6eff11SDavid Ahern 		return -EINVAL;
5200ed6eff11SDavid Ahern 	}
52016371a71fSDavid Ahern 
52026371a71fSDavid Ahern 	fillargs->ifindex = ifm->ifa_index;
52036371a71fSDavid Ahern 	if (fillargs->ifindex) {
52046371a71fSDavid Ahern 		cb->answer_flags |= NLM_F_DUMP_FILTERED;
52056371a71fSDavid Ahern 		fillargs->flags |= NLM_F_DUMP_FILTERED;
5206ed6eff11SDavid Ahern 	}
5207ed6eff11SDavid Ahern 
52088cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5209ed6eff11SDavid Ahern 					    ifa_ipv6_policy, extack);
5210ed6eff11SDavid Ahern 	if (err < 0)
5211ed6eff11SDavid Ahern 		return err;
5212ed6eff11SDavid Ahern 
5213ed6eff11SDavid Ahern 	for (i = 0; i <= IFA_MAX; ++i) {
5214ed6eff11SDavid Ahern 		if (!tb[i])
5215ed6eff11SDavid Ahern 			continue;
5216ed6eff11SDavid Ahern 
5217ed6eff11SDavid Ahern 		if (i == IFA_TARGET_NETNSID) {
5218ed6eff11SDavid Ahern 			struct net *net;
5219ed6eff11SDavid Ahern 
5220ed6eff11SDavid Ahern 			fillargs->netnsid = nla_get_s32(tb[i]);
5221ed6eff11SDavid Ahern 			net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5222ed6eff11SDavid Ahern 			if (IS_ERR(net)) {
5223bf4cc40eSBjørn Mork 				fillargs->netnsid = -1;
5224ed6eff11SDavid Ahern 				NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5225ed6eff11SDavid Ahern 				return PTR_ERR(net);
5226ed6eff11SDavid Ahern 			}
5227ed6eff11SDavid Ahern 			*tgt_net = net;
5228ed6eff11SDavid Ahern 		} else {
5229ed6eff11SDavid Ahern 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5230ed6eff11SDavid Ahern 			return -EINVAL;
5231ed6eff11SDavid Ahern 		}
5232ed6eff11SDavid Ahern 	}
5233ed6eff11SDavid Ahern 
5234ed6eff11SDavid Ahern 	return 0;
5235ed6eff11SDavid Ahern }
5236ed6eff11SDavid Ahern 
5237234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5238234b27c3SEric Dumazet 			   enum addr_type_t type)
5239234b27c3SEric Dumazet {
5240ed6eff11SDavid Ahern 	const struct nlmsghdr *nlh = cb->nlh;
52416ba1e6e8SDavid Ahern 	struct inet6_fill_args fillargs = {
52426ba1e6e8SDavid Ahern 		.portid = NETLINK_CB(cb->skb).portid,
52436ba1e6e8SDavid Ahern 		.seq = cb->nlh->nlmsg_seq,
52446ba1e6e8SDavid Ahern 		.flags = NLM_F_MULTI,
52456ba1e6e8SDavid Ahern 		.netnsid = -1,
52466ba1e6e8SDavid Ahern 		.type = type,
52476ba1e6e8SDavid Ahern 	};
524887117bafSRocco Yue 	struct net *tgt_net = sock_net(skb->sk);
5249fe884c2bSDavid Ahern 	int idx, s_idx, s_ip_idx;
5250234b27c3SEric Dumazet 	int h, s_h;
5251234b27c3SEric Dumazet 	struct net_device *dev;
5252234b27c3SEric Dumazet 	struct inet6_dev *idev;
5253234b27c3SEric Dumazet 	struct hlist_head *head;
5254242afaa6SDavid Ahern 	int err = 0;
5255234b27c3SEric Dumazet 
5256234b27c3SEric Dumazet 	s_h = cb->args[0];
5257234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
5258fe884c2bSDavid Ahern 	s_ip_idx = cb->args[2];
5259234b27c3SEric Dumazet 
5260ed6eff11SDavid Ahern 	if (cb->strict_check) {
5261ed6eff11SDavid Ahern 		err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
52626371a71fSDavid Ahern 						  skb->sk, cb);
5263ed6eff11SDavid Ahern 		if (err < 0)
5264242afaa6SDavid Ahern 			goto put_tgt_net;
52656371a71fSDavid Ahern 
5266242afaa6SDavid Ahern 		err = 0;
52676371a71fSDavid Ahern 		if (fillargs.ifindex) {
52686371a71fSDavid Ahern 			dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
5269242afaa6SDavid Ahern 			if (!dev) {
5270242afaa6SDavid Ahern 				err = -ENODEV;
5271242afaa6SDavid Ahern 				goto put_tgt_net;
5272242afaa6SDavid Ahern 			}
52736371a71fSDavid Ahern 			idev = __in6_dev_get(dev);
52746371a71fSDavid Ahern 			if (idev) {
52756371a71fSDavid Ahern 				err = in6_dump_addrs(idev, skb, cb, s_ip_idx,
52766371a71fSDavid Ahern 						     &fillargs);
52771518039fSJakub Kicinski 				if (err > 0)
52781518039fSJakub Kicinski 					err = 0;
52796371a71fSDavid Ahern 			}
52806371a71fSDavid Ahern 			goto put_tgt_net;
52816371a71fSDavid Ahern 		}
52826ecf4c37SChristian Brauner 	}
52836ecf4c37SChristian Brauner 
5284234b27c3SEric Dumazet 	rcu_read_lock();
52856ecf4c37SChristian Brauner 	cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq;
5286234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5287234b27c3SEric Dumazet 		idx = 0;
52886ecf4c37SChristian Brauner 		head = &tgt_net->dev_index_head[h];
5289b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
5290234b27c3SEric Dumazet 			if (idx < s_idx)
5291234b27c3SEric Dumazet 				goto cont;
52924b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
5293234b27c3SEric Dumazet 				s_ip_idx = 0;
5294e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
5295e21e8467SStephen Hemminger 			if (!idev)
5296234b27c3SEric Dumazet 				goto cont;
5297234b27c3SEric Dumazet 
5298fe884c2bSDavid Ahern 			if (in6_dump_addrs(idev, skb, cb, s_ip_idx,
52996ba1e6e8SDavid Ahern 					   &fillargs) < 0)
5300234b27c3SEric Dumazet 				goto done;
53017562f876SPavel Emelianov cont:
53027562f876SPavel Emelianov 			idx++;
53031da177e4SLinus Torvalds 		}
5304234b27c3SEric Dumazet 	}
5305234b27c3SEric Dumazet done:
5306234b27c3SEric Dumazet 	rcu_read_unlock();
5307234b27c3SEric Dumazet 	cb->args[0] = h;
5308234b27c3SEric Dumazet 	cb->args[1] = idx;
53096371a71fSDavid Ahern put_tgt_net:
53106ba1e6e8SDavid Ahern 	if (fillargs.netnsid >= 0)
53116ecf4c37SChristian Brauner 		put_net(tgt_net);
5312234b27c3SEric Dumazet 
53137c1e8a38SArthur Gautier 	return skb->len ? : err;
53141da177e4SLinus Torvalds }
53151da177e4SLinus Torvalds 
53161da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
53171da177e4SLinus Torvalds {
53181da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
5319b854272bSDenis V. Lunev 
53201da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
53211da177e4SLinus Torvalds }
53221da177e4SLinus Torvalds 
53231da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
53241da177e4SLinus Torvalds {
53251da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
5326b854272bSDenis V. Lunev 
53271da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
53281da177e4SLinus Torvalds }
53291da177e4SLinus Torvalds 
53301da177e4SLinus Torvalds 
53311da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
53321da177e4SLinus Torvalds {
53331da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
5334b854272bSDenis V. Lunev 
53351da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
53361da177e4SLinus Torvalds }
53371da177e4SLinus Torvalds 
53384b1373deSJakub Kicinski static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
53394b1373deSJakub Kicinski 				       const struct nlmsghdr *nlh,
53404b1373deSJakub Kicinski 				       struct nlattr **tb,
53414b1373deSJakub Kicinski 				       struct netlink_ext_ack *extack)
53424b1373deSJakub Kicinski {
53434b1373deSJakub Kicinski 	struct ifaddrmsg *ifm;
53444b1373deSJakub Kicinski 	int i, err;
53454b1373deSJakub Kicinski 
53464b1373deSJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
53474b1373deSJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
53484b1373deSJakub Kicinski 		return -EINVAL;
53494b1373deSJakub Kicinski 	}
53504b1373deSJakub Kicinski 
53512beb6d29SHangbin Liu 	if (!netlink_strict_get_check(skb))
53522beb6d29SHangbin Liu 		return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
53532beb6d29SHangbin Liu 					      ifa_ipv6_policy, extack);
53542beb6d29SHangbin Liu 
53554b1373deSJakub Kicinski 	ifm = nlmsg_data(nlh);
53564b1373deSJakub Kicinski 	if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
53574b1373deSJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
53584b1373deSJakub Kicinski 		return -EINVAL;
53594b1373deSJakub Kicinski 	}
53604b1373deSJakub Kicinski 
53618cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
53624b1373deSJakub Kicinski 					    ifa_ipv6_policy, extack);
53634b1373deSJakub Kicinski 	if (err)
53644b1373deSJakub Kicinski 		return err;
53654b1373deSJakub Kicinski 
53664b1373deSJakub Kicinski 	for (i = 0; i <= IFA_MAX; i++) {
53674b1373deSJakub Kicinski 		if (!tb[i])
53684b1373deSJakub Kicinski 			continue;
53694b1373deSJakub Kicinski 
53704b1373deSJakub Kicinski 		switch (i) {
53714b1373deSJakub Kicinski 		case IFA_TARGET_NETNSID:
53724b1373deSJakub Kicinski 		case IFA_ADDRESS:
53734b1373deSJakub Kicinski 		case IFA_LOCAL:
53744b1373deSJakub Kicinski 			break;
53754b1373deSJakub Kicinski 		default:
53764b1373deSJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
53774b1373deSJakub Kicinski 			return -EINVAL;
53784b1373deSJakub Kicinski 		}
53794b1373deSJakub Kicinski 	}
53804b1373deSJakub Kicinski 
53814b1373deSJakub Kicinski 	return 0;
53824b1373deSJakub Kicinski }
53834b1373deSJakub Kicinski 
5384c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5385c21ef3e3SDavid Ahern 			     struct netlink_ext_ack *extack)
53866c223828SNoriaki TAKAMIYA {
538787117bafSRocco Yue 	struct net *tgt_net = sock_net(in_skb->sk);
5388203651b6SChristian Brauner 	struct inet6_fill_args fillargs = {
5389203651b6SChristian Brauner 		.portid = NETLINK_CB(in_skb).portid,
5390203651b6SChristian Brauner 		.seq = nlh->nlmsg_seq,
5391203651b6SChristian Brauner 		.event = RTM_NEWADDR,
5392203651b6SChristian Brauner 		.flags = 0,
5393203651b6SChristian Brauner 		.netnsid = -1,
5394203651b6SChristian Brauner 	};
53951b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
53961b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
5397caeaba79SNicolas Dichtel 	struct in6_addr *addr = NULL, *peer;
53986c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
53996c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
54006c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
54016c223828SNoriaki TAKAMIYA 	int err;
54026c223828SNoriaki TAKAMIYA 
54034b1373deSJakub Kicinski 	err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
54041b29fc2cSThomas Graf 	if (err < 0)
5405c24675f8SFlorian Westphal 		return err;
54066c223828SNoriaki TAKAMIYA 
54076ecf4c37SChristian Brauner 	if (tb[IFA_TARGET_NETNSID]) {
5408203651b6SChristian Brauner 		fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
54096ecf4c37SChristian Brauner 
54106ecf4c37SChristian Brauner 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
5411203651b6SChristian Brauner 						  fillargs.netnsid);
54126ecf4c37SChristian Brauner 		if (IS_ERR(tgt_net))
54136ecf4c37SChristian Brauner 			return PTR_ERR(tgt_net);
54146ecf4c37SChristian Brauner 	}
54156ecf4c37SChristian Brauner 
5416caeaba79SNicolas Dichtel 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
5417c24675f8SFlorian Westphal 	if (!addr)
5418c24675f8SFlorian Westphal 		return -EINVAL;
54191b29fc2cSThomas Graf 
54201b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
54216c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
54226ecf4c37SChristian Brauner 		dev = dev_get_by_index(tgt_net, ifm->ifa_index);
54236c223828SNoriaki TAKAMIYA 
54246ecf4c37SChristian Brauner 	ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
5425e21e8467SStephen Hemminger 	if (!ifa) {
54261b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
54271b29fc2cSThomas Graf 		goto errout;
54286c223828SNoriaki TAKAMIYA 	}
54296c223828SNoriaki TAKAMIYA 
5430e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5431e21e8467SStephen Hemminger 	if (!skb) {
54321b29fc2cSThomas Graf 		err = -ENOBUFS;
54331b29fc2cSThomas Graf 		goto errout_ifa;
54341b29fc2cSThomas Graf 	}
54351b29fc2cSThomas Graf 
5436203651b6SChristian Brauner 	err = inet6_fill_ifaddr(skb, ifa, &fillargs);
543726932566SPatrick McHardy 	if (err < 0) {
543826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
543926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
544026932566SPatrick McHardy 		kfree_skb(skb);
544126932566SPatrick McHardy 		goto errout_ifa;
544226932566SPatrick McHardy 	}
54436ecf4c37SChristian Brauner 	err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
54441b29fc2cSThomas Graf errout_ifa:
54456c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
54461b29fc2cSThomas Graf errout:
5447c24675f8SFlorian Westphal 	dev_put(dev);
5448203651b6SChristian Brauner 	if (fillargs.netnsid >= 0)
54496ecf4c37SChristian Brauner 		put_net(tgt_net);
54506ecf4c37SChristian Brauner 
54516c223828SNoriaki TAKAMIYA 	return err;
54526c223828SNoriaki TAKAMIYA }
54536c223828SNoriaki TAKAMIYA 
54541da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
54551da177e4SLinus Torvalds {
54561da177e4SLinus Torvalds 	struct sk_buff *skb;
5457c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
5458203651b6SChristian Brauner 	struct inet6_fill_args fillargs = {
5459203651b6SChristian Brauner 		.portid = 0,
5460203651b6SChristian Brauner 		.seq = 0,
5461203651b6SChristian Brauner 		.event = event,
5462203651b6SChristian Brauner 		.flags = 0,
5463203651b6SChristian Brauner 		.netnsid = -1,
5464203651b6SChristian Brauner 	};
54655d620266SThomas Graf 	int err = -ENOBUFS;
54661da177e4SLinus Torvalds 
54670ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
546863159f29SIan Morris 	if (!skb)
54695d620266SThomas Graf 		goto errout;
54705d620266SThomas Graf 
5471203651b6SChristian Brauner 	err = inet6_fill_ifaddr(skb, ifa, &fillargs);
547226932566SPatrick McHardy 	if (err < 0) {
547326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
547426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
547526932566SPatrick McHardy 		kfree_skb(skb);
547626932566SPatrick McHardy 		goto errout;
547726932566SPatrick McHardy 	}
54781ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
54791ce85fe4SPablo Neira Ayuso 	return;
54805d620266SThomas Graf errout:
54815d620266SThomas Graf 	if (err < 0)
54826fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
54831da177e4SLinus Torvalds }
54841da177e4SLinus Torvalds 
5485b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
54861da177e4SLinus Torvalds 				__s32 *array, int bytes)
54871da177e4SLinus Torvalds {
548804561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
548904561c1fSThomas Graf 
54901da177e4SLinus Torvalds 	memset(array, 0, bytes);
54911da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
54921da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
54931da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
54941da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
54951da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
54961da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
54971da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
54981da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
549993908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
550093908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
5501bd11f074SMaciej Żenczykowski 	array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5502bd11f074SMaciej Żenczykowski 		jiffies_to_msecs(cnf->rtr_solicit_max_interval);
550393908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
550493908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
55051da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
5506fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5507fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5508fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5509fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
55101da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
55111da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
55121da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
55131da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
55141da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
55151da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
551665f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
55176b2e04bcSPraveen Chaudhary 	array[DEVCONF_RA_DEFRTR_METRIC] = cnf->ra_defrtr_metric;
55188013d1d7SHangbin Liu 	array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
5519c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
5520930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
5521930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
552293908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
552393908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
5524fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
5525bbea124bSJoel Scherpelz 	array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
552609c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
552709c884d4SYOSHIFUJI Hideaki #endif
5528930d6ff2SYOSHIFUJI Hideaki #endif
5529fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
55300bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
553195c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
553295c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
55337fd2561eSErik Kline 	array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
553495c385b4SNeil Horman #endif
55357bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
5536145c7a79SEric Dumazet 	array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding);
55377bc570c8SYOSHIFUJI Hideaki #endif
5538778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
55391b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
5540c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
55415cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5542b800c3b9SHannes Frederic Sowa 	array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5543d9333196SBen Greear 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5544c2943f14SHarout Hedeshian 	array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
554535103d11SAndy Gospodarek 	array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
55463d1bec99SHannes Frederic Sowa 	/* we omit DEVCONF_STABLE_SECRET for now */
55473985e8a3SErik Kline 	array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5548abbc3043SJohannes Berg 	array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
55497a02bf89SJohannes Berg 	array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5550f1705ec1SDavid Ahern 	array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
55511ababebaSDavid Lebrun 	array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5552bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
5553bf355b8dSDavid Lebrun 	array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5554bf355b8dSDavid Lebrun #endif
5555adc176c5SErik Nordmark 	array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5556d35a00b8SFelix Jia 	array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5557df789fe7SDavid Forster 	array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
55582210d6b2SMaciej Żenczykowski 	array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
55598610c7c6SAlexander Aring 	array[DEVCONF_RPL_SEG_ENABLED] = cnf->rpl_seg_enabled;
55609ee11f0fSJustin Iurman 	array[DEVCONF_IOAM6_ENABLED] = cnf->ioam6_enabled;
55619ee11f0fSJustin Iurman 	array[DEVCONF_IOAM6_ID] = cnf->ioam6_id;
55629ee11f0fSJustin Iurman 	array[DEVCONF_IOAM6_ID_WIDE] = cnf->ioam6_id_wide;
556318ac597aSJames Prestwood 	array[DEVCONF_NDISC_EVICT_NOCARRIER] = cnf->ndisc_evict_nocarrier;
55641da177e4SLinus Torvalds }
55651da177e4SLinus Torvalds 
5566b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
5567b382b191SThomas Graf {
5568b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
5569b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
5570b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5571b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5572f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5573bdd72f41SSabrina Dubroca 	     + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5574bdd72f41SSabrina Dubroca 	     + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
557549b99da2SRocco Yue 	     + nla_total_size(4) /* IFLA_INET6_RA_MTU */
5576bdd72f41SSabrina Dubroca 	     + 0;
5577b382b191SThomas Graf }
5578b382b191SThomas Graf 
5579339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
5580339bf98fSThomas Graf {
5581339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5582339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5583339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5584339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
5585339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
55860344338bSAndy Gospodarek 	       + nla_total_size(1) /* IFLA_OPERSTATE */
5587b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5588339bf98fSThomas Graf }
5589c5396a31SYOSHIFUJI Hideaki 
5590be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5591aca05671SJia He 					int bytes)
55927f7d9a6bSHerbert Xu {
55937f7d9a6bSHerbert Xu 	int i;
5594aca05671SJia He 	int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
55957f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
55967f7d9a6bSHerbert Xu 
55977f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
5598aca05671SJia He 	put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5599aca05671SJia He 	for (i = 1; i < ICMP6_MIB_MAX; i++)
5600be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
56017f7d9a6bSHerbert Xu 
5602aca05671SJia He 	memset(&stats[ICMP6_MIB_MAX], 0, pad);
56037f7d9a6bSHerbert Xu }
56047f7d9a6bSHerbert Xu 
5605698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5606a3a77372SRaghavendra K T 					int bytes, size_t syncpoff)
56074ce3c183SEric Dumazet {
5608a3a77372SRaghavendra K T 	int i, c;
5609a3a77372SRaghavendra K T 	u64 buff[IPSTATS_MIB_MAX];
5610a3a77372SRaghavendra K T 	int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5611a3a77372SRaghavendra K T 
56124ce3c183SEric Dumazet 	BUG_ON(pad < 0);
56134ce3c183SEric Dumazet 
5614a3a77372SRaghavendra K T 	memset(buff, 0, sizeof(buff));
5615a3a77372SRaghavendra K T 	buff[0] = IPSTATS_MIB_MAX;
56164ce3c183SEric Dumazet 
5617a3a77372SRaghavendra K T 	for_each_possible_cpu(c) {
5618a3a77372SRaghavendra K T 		for (i = 1; i < IPSTATS_MIB_MAX; i++)
5619a3a77372SRaghavendra K T 			buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5620a3a77372SRaghavendra K T 	}
5621a3a77372SRaghavendra K T 
5622a3a77372SRaghavendra K T 	memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5623a3a77372SRaghavendra K T 	memset(&stats[IPSTATS_MIB_MAX], 0, pad);
56244ce3c183SEric Dumazet }
56254ce3c183SEric Dumazet 
56267f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
56277f7d9a6bSHerbert Xu 			     int bytes)
56287f7d9a6bSHerbert Xu {
56297f7d9a6bSHerbert Xu 	switch (attrtype) {
56307f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
5631a3a77372SRaghavendra K T 		__snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5632a3a77372SRaghavendra K T 				     offsetof(struct ipstats_mib, syncp));
56337f7d9a6bSHerbert Xu 		break;
56347f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
5635aca05671SJia He 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
56367f7d9a6bSHerbert Xu 		break;
56377f7d9a6bSHerbert Xu 	}
56387f7d9a6bSHerbert Xu }
56397f7d9a6bSHerbert Xu 
5640d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5641d5566fd7SSowmini Varadhan 				  u32 ext_filter_mask)
56421da177e4SLinus Torvalds {
5643bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
56441da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
56451da177e4SLinus Torvalds 
5646c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5647c78679e8SDavid S. Miller 		goto nla_put_failure;
56481da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
564924912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
565024912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
56511f9248e5SJiri Pirko 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5652c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5653c78679e8SDavid S. Miller 		goto nla_put_failure;
5654bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
565563159f29SIan Morris 	if (!nla)
565604561c1fSThomas Graf 		goto nla_put_failure;
5657bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
56581da177e4SLinus Torvalds 
5659bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
5660bf99f1bdSYOSHIFUJI Hideaki 
5661d5566fd7SSowmini Varadhan 	if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5662d5566fd7SSowmini Varadhan 		return 0;
5663d5566fd7SSowmini Varadhan 
5664bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
566563159f29SIan Morris 	if (!nla)
5666bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
5667bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5668bf99f1bdSYOSHIFUJI Hideaki 
5669bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
567063159f29SIan Morris 	if (!nla)
5671bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
5672bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
56731da177e4SLinus Torvalds 
5674f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
567563159f29SIan Morris 	if (!nla)
5676f53adae4SDaniel Borkmann 		goto nla_put_failure;
5677f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
5678f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5679f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
5680f53adae4SDaniel Borkmann 
56810d7982ceSNicolas Dichtel 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
56820d7982ceSNicolas Dichtel 		goto nla_put_failure;
56830d7982ceSNicolas Dichtel 
568449b99da2SRocco Yue 	if (idev->ra_mtu &&
568549b99da2SRocco Yue 	    nla_put_u32(skb, IFLA_INET6_RA_MTU, idev->ra_mtu))
568649b99da2SRocco Yue 		goto nla_put_failure;
568749b99da2SRocco Yue 
5688b382b191SThomas Graf 	return 0;
5689b382b191SThomas Graf 
5690b382b191SThomas Graf nla_put_failure:
5691b382b191SThomas Graf 	return -EMSGSIZE;
5692b382b191SThomas Graf }
5693b382b191SThomas Graf 
5694b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev,
5695b1974ed0SArad, Ronen 				     u32 ext_filter_mask)
5696b382b191SThomas Graf {
5697b382b191SThomas Graf 	if (!__in6_dev_get(dev))
5698b382b191SThomas Graf 		return 0;
5699b382b191SThomas Graf 
5700b382b191SThomas Graf 	return inet6_ifla6_size();
5701b382b191SThomas Graf }
5702b382b191SThomas Graf 
5703d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5704d5566fd7SSowmini Varadhan 			      u32 ext_filter_mask)
5705b382b191SThomas Graf {
5706b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
5707b382b191SThomas Graf 
5708b382b191SThomas Graf 	if (!idev)
5709b382b191SThomas Graf 		return -ENODATA;
5710b382b191SThomas Graf 
5711d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5712b382b191SThomas Graf 		return -EMSGSIZE;
5713b382b191SThomas Graf 
5714b382b191SThomas Graf 	return 0;
5715b382b191SThomas Graf }
5716b382b191SThomas Graf 
57173583a4e8SStephen Hemminger static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token,
57183583a4e8SStephen Hemminger 			     struct netlink_ext_ack *extack)
5719f53adae4SDaniel Borkmann {
5720f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
5721f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
572247e27d5eSDaniel Borkmann 	bool clear_token, update_rs = false;
5723dc848292SHannes Frederic Sowa 	struct in6_addr ll_addr;
5724f53adae4SDaniel Borkmann 
5725c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
5726c15b1ccaSHannes Frederic Sowa 
572763159f29SIan Morris 	if (!token)
5728f53adae4SDaniel Borkmann 		return -EINVAL;
57293583a4e8SStephen Hemminger 
57303583a4e8SStephen Hemminger 	if (dev->flags & IFF_LOOPBACK) {
57313583a4e8SStephen Hemminger 		NL_SET_ERR_MSG_MOD(extack, "Device is loopback");
5732f53adae4SDaniel Borkmann 		return -EINVAL;
57333583a4e8SStephen Hemminger 	}
57343583a4e8SStephen Hemminger 
57353583a4e8SStephen Hemminger 	if (dev->flags & IFF_NOARP) {
57363583a4e8SStephen Hemminger 		NL_SET_ERR_MSG_MOD(extack,
57373583a4e8SStephen Hemminger 				   "Device does not do neighbour discovery");
5738f53adae4SDaniel Borkmann 		return -EINVAL;
57393583a4e8SStephen Hemminger 	}
57403583a4e8SStephen Hemminger 
57413583a4e8SStephen Hemminger 	if (!ipv6_accept_ra(idev)) {
57423583a4e8SStephen Hemminger 		NL_SET_ERR_MSG_MOD(extack,
57433583a4e8SStephen Hemminger 				   "Router advertisement is disabled on device");
5744f53adae4SDaniel Borkmann 		return -EINVAL;
57453583a4e8SStephen Hemminger 	}
57463583a4e8SStephen Hemminger 
57473583a4e8SStephen Hemminger 	if (idev->cnf.rtr_solicits == 0) {
57483583a4e8SStephen Hemminger 		NL_SET_ERR_MSG(extack,
57493583a4e8SStephen Hemminger 			       "Router solicitation is disabled on device");
57503583a4e8SStephen Hemminger 		return -EINVAL;
57513583a4e8SStephen Hemminger 	}
5752f53adae4SDaniel Borkmann 
5753f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5754f53adae4SDaniel Borkmann 
5755f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5756f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5757f53adae4SDaniel Borkmann 
5758f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5759f53adae4SDaniel Borkmann 
576047e27d5eSDaniel Borkmann 	clear_token = ipv6_addr_any(token);
576147e27d5eSDaniel Borkmann 	if (clear_token)
576247e27d5eSDaniel Borkmann 		goto update_lft;
576347e27d5eSDaniel Borkmann 
5764dc848292SHannes Frederic Sowa 	if (!idev->dead && (idev->if_flags & IF_READY) &&
5765dc848292SHannes Frederic Sowa 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5766dc848292SHannes Frederic Sowa 			     IFA_F_OPTIMISTIC)) {
5767fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
5768fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
5769fc403832SDaniel Borkmann 		 */
5770f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5771fc403832SDaniel Borkmann 		update_rs = true;
5772fc403832SDaniel Borkmann 	}
5773f53adae4SDaniel Borkmann 
577447e27d5eSDaniel Borkmann update_lft:
5775f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5776fc403832SDaniel Borkmann 
577777ecaaceSHannes Frederic Sowa 	if (update_rs) {
5778f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
5779bd11f074SMaciej Żenczykowski 		idev->rs_interval = rfc3315_s14_backoff_init(
5780bd11f074SMaciej Żenczykowski 			idev->cnf.rtr_solicit_interval);
578177ecaaceSHannes Frederic Sowa 		idev->rs_probes = 1;
5782bd11f074SMaciej Żenczykowski 		addrconf_mod_rs_timer(idev, idev->rs_interval);
578377ecaaceSHannes Frederic Sowa 	}
5784f53adae4SDaniel Borkmann 
5785f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
5786f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
5787f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
5788617fe29dSDaniel Borkmann 		if (ifp->tokenized) {
5789f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
5790f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
5791f53adae4SDaniel Borkmann 		}
5792f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
5793f53adae4SDaniel Borkmann 	}
5794f53adae4SDaniel Borkmann 
5795f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5796b2ed64a9SLubomir Rintel 	inet6_ifinfo_notify(RTM_NEWLINK, idev);
5797c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
5798f53adae4SDaniel Borkmann 	return 0;
5799f53adae4SDaniel Borkmann }
5800f53adae4SDaniel Borkmann 
580111b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
580211b1f828SDaniel Borkmann 	[IFLA_INET6_ADDR_GEN_MODE]	= { .type = NLA_U8 },
580311b1f828SDaniel Borkmann 	[IFLA_INET6_TOKEN]		= { .len = sizeof(struct in6_addr) },
580449b99da2SRocco Yue 	[IFLA_INET6_RA_MTU]		= { .type = NLA_REJECT,
580549b99da2SRocco Yue 					    .reject_message =
580649b99da2SRocco Yue 						"IFLA_INET6_RA_MTU can not be set" },
580711b1f828SDaniel Borkmann };
580811b1f828SDaniel Borkmann 
5809d35a00b8SFelix Jia static int check_addr_gen_mode(int mode)
5810d35a00b8SFelix Jia {
5811d35a00b8SFelix Jia 	if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5812d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_NONE &&
5813d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5814d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_RANDOM)
5815d35a00b8SFelix Jia 		return -EINVAL;
5816d35a00b8SFelix Jia 	return 1;
5817d35a00b8SFelix Jia }
5818d35a00b8SFelix Jia 
5819d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5820d35a00b8SFelix Jia 				int mode)
5821d35a00b8SFelix Jia {
5822d35a00b8SFelix Jia 	if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5823d35a00b8SFelix Jia 	    !idev->cnf.stable_secret.initialized &&
5824d35a00b8SFelix Jia 	    !net->ipv6.devconf_dflt->stable_secret.initialized)
5825d35a00b8SFelix Jia 		return -EINVAL;
5826d35a00b8SFelix Jia 	return 1;
5827d35a00b8SFelix Jia }
5828d35a00b8SFelix Jia 
58297dc2bccaSMaxim Mikityanskiy static int inet6_validate_link_af(const struct net_device *dev,
58308679c31eSRocco Yue 				  const struct nlattr *nla,
58318679c31eSRocco Yue 				  struct netlink_ext_ack *extack)
5832f53adae4SDaniel Borkmann {
5833f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
58347dc2bccaSMaxim Mikityanskiy 	struct inet6_dev *idev = NULL;
58357dc2bccaSMaxim Mikityanskiy 	int err;
5836f53adae4SDaniel Borkmann 
58377dc2bccaSMaxim Mikityanskiy 	if (dev) {
58387dc2bccaSMaxim Mikityanskiy 		idev = __in6_dev_get(dev);
5839f53adae4SDaniel Borkmann 		if (!idev)
5840f53adae4SDaniel Borkmann 			return -EAFNOSUPPORT;
58417dc2bccaSMaxim Mikityanskiy 	}
58427dc2bccaSMaxim Mikityanskiy 
58437dc2bccaSMaxim Mikityanskiy 	err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
58448679c31eSRocco Yue 					  inet6_af_policy, extack);
58457dc2bccaSMaxim Mikityanskiy 	if (err)
58467dc2bccaSMaxim Mikityanskiy 		return err;
58477dc2bccaSMaxim Mikityanskiy 
58487dc2bccaSMaxim Mikityanskiy 	if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
58497dc2bccaSMaxim Mikityanskiy 		return -EINVAL;
58507dc2bccaSMaxim Mikityanskiy 
58517dc2bccaSMaxim Mikityanskiy 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
58527dc2bccaSMaxim Mikityanskiy 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
58537dc2bccaSMaxim Mikityanskiy 
58547dc2bccaSMaxim Mikityanskiy 		if (check_addr_gen_mode(mode) < 0)
58557dc2bccaSMaxim Mikityanskiy 			return -EINVAL;
58567dc2bccaSMaxim Mikityanskiy 		if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
58577dc2bccaSMaxim Mikityanskiy 			return -EINVAL;
58587dc2bccaSMaxim Mikityanskiy 	}
58597dc2bccaSMaxim Mikityanskiy 
58607dc2bccaSMaxim Mikityanskiy 	return 0;
58617dc2bccaSMaxim Mikityanskiy }
58627dc2bccaSMaxim Mikityanskiy 
58633583a4e8SStephen Hemminger static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla,
58643583a4e8SStephen Hemminger 			     struct netlink_ext_ack *extack)
58657dc2bccaSMaxim Mikityanskiy {
58667dc2bccaSMaxim Mikityanskiy 	struct inet6_dev *idev = __in6_dev_get(dev);
58677dc2bccaSMaxim Mikityanskiy 	struct nlattr *tb[IFLA_INET6_MAX + 1];
58687dc2bccaSMaxim Mikityanskiy 	int err;
5869f53adae4SDaniel Borkmann 
5870db3fa271SEric Dumazet 	if (!idev)
5871db3fa271SEric Dumazet 		return -EAFNOSUPPORT;
5872db3fa271SEric Dumazet 
58738cb08174SJohannes Berg 	if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
58745ac6b198SZheng Yongjun 		return -EINVAL;
5875f53adae4SDaniel Borkmann 
5876bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_TOKEN]) {
58773583a4e8SStephen Hemminger 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]),
58783583a4e8SStephen Hemminger 					extack);
5879bc91b0f0SJiri Pirko 		if (err)
5880bc91b0f0SJiri Pirko 			return err;
5881bc91b0f0SJiri Pirko 	}
5882bc91b0f0SJiri Pirko 
5883bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5884bc91b0f0SJiri Pirko 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5885bc91b0f0SJiri Pirko 
5886d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = mode;
5887bc91b0f0SJiri Pirko 	}
5888f53adae4SDaniel Borkmann 
58897dc2bccaSMaxim Mikityanskiy 	return 0;
5890f53adae4SDaniel Borkmann }
5891f53adae4SDaniel Borkmann 
5892b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
589315e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
5894b382b191SThomas Graf {
5895b382b191SThomas Graf 	struct net_device *dev = idev->dev;
5896b382b191SThomas Graf 	struct ifinfomsg *hdr;
5897b382b191SThomas Graf 	struct nlmsghdr *nlh;
5898b382b191SThomas Graf 	void *protoinfo;
5899b382b191SThomas Graf 
590015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
590163159f29SIan Morris 	if (!nlh)
5902b382b191SThomas Graf 		return -EMSGSIZE;
5903b382b191SThomas Graf 
5904b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
5905b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
5906b382b191SThomas Graf 	hdr->__ifi_pad = 0;
5907b382b191SThomas Graf 	hdr->ifi_type = dev->type;
5908b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
5909b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
5910b382b191SThomas Graf 	hdr->ifi_change = 0;
5911b382b191SThomas Graf 
5912c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5913c78679e8SDavid S. Miller 	    (dev->addr_len &&
5914c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5915c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5916a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
59170344338bSAndy Gospodarek 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
59180344338bSAndy Gospodarek 	    nla_put_u8(skb, IFLA_OPERSTATE,
59190344338bSAndy Gospodarek 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5920c78679e8SDavid S. Miller 		goto nla_put_failure;
5921ae0be8deSMichal Kubecek 	protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
592263159f29SIan Morris 	if (!protoinfo)
5923b382b191SThomas Graf 		goto nla_put_failure;
5924b382b191SThomas Graf 
5925d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5926b382b191SThomas Graf 		goto nla_put_failure;
5927b382b191SThomas Graf 
592804561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
5929053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5930053c095aSJohannes Berg 	return 0;
59311da177e4SLinus Torvalds 
593204561c1fSThomas Graf nla_put_failure:
593326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
593426932566SPatrick McHardy 	return -EMSGSIZE;
59351da177e4SLinus Torvalds }
59361da177e4SLinus Torvalds 
5937786e0007SDavid Ahern static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
5938786e0007SDavid Ahern 				   struct netlink_ext_ack *extack)
5939786e0007SDavid Ahern {
5940786e0007SDavid Ahern 	struct ifinfomsg *ifm;
5941786e0007SDavid Ahern 
5942786e0007SDavid Ahern 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5943786e0007SDavid Ahern 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
5944786e0007SDavid Ahern 		return -EINVAL;
5945786e0007SDavid Ahern 	}
5946786e0007SDavid Ahern 
5947786e0007SDavid Ahern 	if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
5948786e0007SDavid Ahern 		NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
5949786e0007SDavid Ahern 		return -EINVAL;
5950786e0007SDavid Ahern 	}
5951786e0007SDavid Ahern 
5952786e0007SDavid Ahern 	ifm = nlmsg_data(nlh);
5953786e0007SDavid Ahern 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
5954786e0007SDavid Ahern 	    ifm->ifi_change || ifm->ifi_index) {
5955786e0007SDavid Ahern 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
5956786e0007SDavid Ahern 		return -EINVAL;
5957786e0007SDavid Ahern 	}
5958786e0007SDavid Ahern 
5959786e0007SDavid Ahern 	return 0;
5960786e0007SDavid Ahern }
5961786e0007SDavid Ahern 
59621da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
59631da177e4SLinus Torvalds {
59643b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
596584d2697dSEric Dumazet 	int h, s_h;
5966434a8a58SDavid S. Miller 	int idx = 0, s_idx;
59671da177e4SLinus Torvalds 	struct net_device *dev;
59681da177e4SLinus Torvalds 	struct inet6_dev *idev;
596984d2697dSEric Dumazet 	struct hlist_head *head;
59701da177e4SLinus Torvalds 
5971786e0007SDavid Ahern 	/* only requests using strict checking can pass data to
5972786e0007SDavid Ahern 	 * influence the dump
5973786e0007SDavid Ahern 	 */
5974786e0007SDavid Ahern 	if (cb->strict_check) {
5975786e0007SDavid Ahern 		int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
5976786e0007SDavid Ahern 
5977786e0007SDavid Ahern 		if (err < 0)
5978786e0007SDavid Ahern 			return err;
5979786e0007SDavid Ahern 	}
5980786e0007SDavid Ahern 
598184d2697dSEric Dumazet 	s_h = cb->args[0];
598284d2697dSEric Dumazet 	s_idx = cb->args[1];
598384d2697dSEric Dumazet 
598484d2697dSEric Dumazet 	rcu_read_lock();
598584d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
59867562f876SPavel Emelianov 		idx = 0;
598784d2697dSEric Dumazet 		head = &net->dev_index_head[h];
5988b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
59891da177e4SLinus Torvalds 			if (idx < s_idx)
59907562f876SPavel Emelianov 				goto cont;
599184d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
599284d2697dSEric Dumazet 			if (!idev)
59937562f876SPavel Emelianov 				goto cont;
599484d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
599515e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
599684d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
5997053c095aSJohannes Berg 					      RTM_NEWLINK, NLM_F_MULTI) < 0)
599884d2697dSEric Dumazet 				goto out;
59997562f876SPavel Emelianov cont:
60007562f876SPavel Emelianov 			idx++;
60011da177e4SLinus Torvalds 		}
600284d2697dSEric Dumazet 	}
600384d2697dSEric Dumazet out:
600484d2697dSEric Dumazet 	rcu_read_unlock();
600584d2697dSEric Dumazet 	cb->args[1] = idx;
600684d2697dSEric Dumazet 	cb->args[0] = h;
60071da177e4SLinus Torvalds 
60081da177e4SLinus Torvalds 	return skb->len;
60091da177e4SLinus Torvalds }
60101da177e4SLinus Torvalds 
60111da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
60121da177e4SLinus Torvalds {
60131da177e4SLinus Torvalds 	struct sk_buff *skb;
6014c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
60158d7a76c9SThomas Graf 	int err = -ENOBUFS;
60161da177e4SLinus Torvalds 
6017339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
601863159f29SIan Morris 	if (!skb)
60198d7a76c9SThomas Graf 		goto errout;
60208d7a76c9SThomas Graf 
60218d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
602226932566SPatrick McHardy 	if (err < 0) {
602326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
602426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
602526932566SPatrick McHardy 		kfree_skb(skb);
602626932566SPatrick McHardy 		goto errout;
602726932566SPatrick McHardy 	}
60285f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
60291ce85fe4SPablo Neira Ayuso 	return;
60308d7a76c9SThomas Graf errout:
60318d7a76c9SThomas Graf 	if (err < 0)
60325f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
60331da177e4SLinus Torvalds }
60341da177e4SLinus Torvalds 
6035339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
6036339bf98fSThomas Graf {
6037339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
6038339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
6039339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
6040339bf98fSThomas Graf }
6041c5396a31SYOSHIFUJI Hideaki 
60421da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
604315e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
6044b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
60451da177e4SLinus Torvalds {
60461da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
60471da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
60481da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
60491da177e4SLinus Torvalds 
605015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
605163159f29SIan Morris 	if (!nlh)
605226932566SPatrick McHardy 		return -EMSGSIZE;
60536051e2f4SThomas Graf 
60546051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
60551da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
60568a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
60578a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
60581da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
60591da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
60601da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
60618a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
60621da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
60631da177e4SLinus Torvalds 	if (pinfo->onlink)
60641da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
60651da177e4SLinus Torvalds 	if (pinfo->autoconf)
60661da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
60671da177e4SLinus Torvalds 
6068c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
6069c78679e8SDavid S. Miller 		goto nla_put_failure;
60701da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
60711da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
6072c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
6073c78679e8SDavid S. Miller 		goto nla_put_failure;
6074053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
6075053c095aSJohannes Berg 	return 0;
60761da177e4SLinus Torvalds 
60776051e2f4SThomas Graf nla_put_failure:
607826932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
607926932566SPatrick McHardy 	return -EMSGSIZE;
60801da177e4SLinus Torvalds }
60811da177e4SLinus Torvalds 
60821da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
60831da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
60841da177e4SLinus Torvalds {
60851da177e4SLinus Torvalds 	struct sk_buff *skb;
6086c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
60878c384bfaSThomas Graf 	int err = -ENOBUFS;
60881da177e4SLinus Torvalds 
6089339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
609063159f29SIan Morris 	if (!skb)
60918c384bfaSThomas Graf 		goto errout;
60928c384bfaSThomas Graf 
60938c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
609426932566SPatrick McHardy 	if (err < 0) {
609526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
609626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
609726932566SPatrick McHardy 		kfree_skb(skb);
609826932566SPatrick McHardy 		goto errout;
609926932566SPatrick McHardy 	}
61001ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
61011ce85fe4SPablo Neira Ayuso 	return;
61028c384bfaSThomas Graf errout:
61038c384bfaSThomas Graf 	if (err < 0)
61046fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
61051da177e4SLinus Torvalds }
61061da177e4SLinus Torvalds 
61071da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
61081da177e4SLinus Torvalds {
610963998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
611063998ac2SNicolas Dichtel 
6111c15b1ccaSHannes Frederic Sowa 	if (event)
6112c15b1ccaSHannes Frederic Sowa 		ASSERT_RTNL();
6113c15b1ccaSHannes Frederic Sowa 
61141da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
61151da177e4SLinus Torvalds 
61161da177e4SLinus Torvalds 	switch (event) {
61171da177e4SLinus Torvalds 	case RTM_NEWADDR:
611895c385b4SNeil Horman 		/*
61192d819d25SDavid Ahern 		 * If the address was optimistic we inserted the route at the
61202d819d25SDavid Ahern 		 * start of our DAD process, so we don't need to do it again.
61212d819d25SDavid Ahern 		 * If the device was taken down in the middle of the DAD
61222d819d25SDavid Ahern 		 * cycle there is a race where we could get here without a
61232d819d25SDavid Ahern 		 * host route, so nothing to insert. That will be fixed when
61242d819d25SDavid Ahern 		 * the device is brought up.
612595c385b4SNeil Horman 		 */
61262d819d25SDavid Ahern 		if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
6127afb1d4b5SDavid Ahern 			ip6_ins_rt(net, ifp->rt);
61282d819d25SDavid Ahern 		} else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
61292d819d25SDavid Ahern 			pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
61302d819d25SDavid Ahern 				&ifp->addr, ifp->idev->dev->name);
61312d819d25SDavid Ahern 		}
61322d819d25SDavid Ahern 
61331da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
61341da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
61357996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr))
613607758eb9SHangbin Liu 			addrconf_prefix_route(&ifp->peer_addr, 128,
613707758eb9SHangbin Liu 					      ifp->rt_priority, ifp->idev->dev,
613807758eb9SHangbin Liu 					      0, 0, GFP_ATOMIC);
61391da177e4SLinus Torvalds 		break;
61401da177e4SLinus Torvalds 	case RTM_DELADDR:
61411da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
61421da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
61431da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
61447996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr)) {
61458d1c802bSDavid Ahern 			struct fib6_info *rt;
6146caeaba79SNicolas Dichtel 
6147e7478dfcSNicolas Dichtel 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
61482b2450caSDavid Ahern 						       ifp->idev->dev, 0, 0,
61492b2450caSDavid Ahern 						       false);
61508e3d5be7SMartin KaFai Lau 			if (rt)
615111dd74b3SRoopa Prabhu 				ip6_del_rt(net, rt, false);
6152caeaba79SNicolas Dichtel 		}
615338bd10c4SDavid Ahern 		if (ifp->rt) {
615411dd74b3SRoopa Prabhu 			ip6_del_rt(net, ifp->rt, false);
615593531c67SDavid Ahern 			ifp->rt = NULL;
615638bd10c4SDavid Ahern 		}
6157705f1c86SHannes Frederic Sowa 		rt_genid_bump_ipv6(net);
61581da177e4SLinus Torvalds 		break;
61591da177e4SLinus Torvalds 	}
616063998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
61611da177e4SLinus Torvalds }
61621da177e4SLinus Torvalds 
61631da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
61641da177e4SLinus Torvalds {
61651da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
61661da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
61671da177e4SLinus Torvalds }
61681da177e4SLinus Torvalds 
61691da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
61701da177e4SLinus Torvalds 
617132927393SChristoph Hellwig static int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
617232927393SChristoph Hellwig 		void *buffer, size_t *lenp, loff_t *ppos)
61731da177e4SLinus Torvalds {
61741da177e4SLinus Torvalds 	int *valp = ctl->data;
61751da177e4SLinus Torvalds 	int val = *valp;
617688af182eSEric W. Biederman 	loff_t pos = *ppos;
6177fe2c6338SJoe Perches 	struct ctl_table lctl;
61781da177e4SLinus Torvalds 	int ret;
61791da177e4SLinus Torvalds 
6180013d97e9SFrancesco Ruggeri 	/*
6181013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
6182013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
6183013d97e9SFrancesco Ruggeri 	 */
6184013d97e9SFrancesco Ruggeri 	lctl = *ctl;
6185013d97e9SFrancesco Ruggeri 	lctl.data = &val;
6186013d97e9SFrancesco Ruggeri 
6187013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
61881da177e4SLinus Torvalds 
6189c8fecf22SPavel Emelyanov 	if (write)
6190b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
619188af182eSEric W. Biederman 	if (ret)
619288af182eSEric W. Biederman 		*ppos = pos;
61931da177e4SLinus Torvalds 	return ret;
61941da177e4SLinus Torvalds }
61951da177e4SLinus Torvalds 
619632927393SChristoph Hellwig static int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
619732927393SChristoph Hellwig 		void *buffer, size_t *lenp, loff_t *ppos)
619877751427SMarcelo Leitner {
619977751427SMarcelo Leitner 	struct inet6_dev *idev = ctl->extra1;
620077751427SMarcelo Leitner 	int min_mtu = IPV6_MIN_MTU;
620177751427SMarcelo Leitner 	struct ctl_table lctl;
620277751427SMarcelo Leitner 
620377751427SMarcelo Leitner 	lctl = *ctl;
620477751427SMarcelo Leitner 	lctl.extra1 = &min_mtu;
620577751427SMarcelo Leitner 	lctl.extra2 = idev ? &idev->dev->mtu : NULL;
620677751427SMarcelo Leitner 
620777751427SMarcelo Leitner 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
620877751427SMarcelo Leitner }
620977751427SMarcelo Leitner 
621056d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
621156d417b1SBrian Haley {
621275538c2bSCong Wang 	struct netdev_notifier_info info;
621375538c2bSCong Wang 
621456d417b1SBrian Haley 	if (!idev || !idev->dev)
621556d417b1SBrian Haley 		return;
621656d417b1SBrian Haley 
621775538c2bSCong Wang 	netdev_notifier_info_init(&info, idev->dev);
621856d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
621975538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_DOWN, &info);
622056d417b1SBrian Haley 	else
622175538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_UP, &info);
622256d417b1SBrian Haley }
622356d417b1SBrian Haley 
622456d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
622556d417b1SBrian Haley {
622656d417b1SBrian Haley 	struct net_device *dev;
622756d417b1SBrian Haley 	struct inet6_dev *idev;
622856d417b1SBrian Haley 
622903e4deffSKefeng Wang 	for_each_netdev(net, dev) {
623056d417b1SBrian Haley 		idev = __in6_dev_get(dev);
623156d417b1SBrian Haley 		if (idev) {
623256d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
623356d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
623456d417b1SBrian Haley 			if (changed)
623556d417b1SBrian Haley 				dev_disable_change(idev);
623656d417b1SBrian Haley 		}
623756d417b1SBrian Haley 	}
623856d417b1SBrian Haley }
623956d417b1SBrian Haley 
6240013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
624156d417b1SBrian Haley {
624256d417b1SBrian Haley 	struct net *net;
6243013d97e9SFrancesco Ruggeri 	int old;
6244013d97e9SFrancesco Ruggeri 
6245013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
6246013d97e9SFrancesco Ruggeri 		return restart_syscall();
624756d417b1SBrian Haley 
624856d417b1SBrian Haley 	net = (struct net *)table->extra2;
6249013d97e9SFrancesco Ruggeri 	old = *p;
6250013d97e9SFrancesco Ruggeri 	*p = newf;
625156d417b1SBrian Haley 
6252013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6253013d97e9SFrancesco Ruggeri 		rtnl_unlock();
625456d417b1SBrian Haley 		return 0;
625588af182eSEric W. Biederman 	}
625656d417b1SBrian Haley 
625756d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
625856d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
625956d417b1SBrian Haley 		addrconf_disable_change(net, newf);
6260013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
626156d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
626256d417b1SBrian Haley 
626356d417b1SBrian Haley 	rtnl_unlock();
626456d417b1SBrian Haley 	return 0;
626556d417b1SBrian Haley }
626656d417b1SBrian Haley 
626732927393SChristoph Hellwig static int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
626832927393SChristoph Hellwig 		void *buffer, size_t *lenp, loff_t *ppos)
626956d417b1SBrian Haley {
627056d417b1SBrian Haley 	int *valp = ctl->data;
627156d417b1SBrian Haley 	int val = *valp;
627288af182eSEric W. Biederman 	loff_t pos = *ppos;
6273fe2c6338SJoe Perches 	struct ctl_table lctl;
627456d417b1SBrian Haley 	int ret;
627556d417b1SBrian Haley 
6276013d97e9SFrancesco Ruggeri 	/*
6277013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
6278013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
6279013d97e9SFrancesco Ruggeri 	 */
6280013d97e9SFrancesco Ruggeri 	lctl = *ctl;
6281013d97e9SFrancesco Ruggeri 	lctl.data = &val;
6282013d97e9SFrancesco Ruggeri 
6283013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
628456d417b1SBrian Haley 
628556d417b1SBrian Haley 	if (write)
628656d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
628788af182eSEric W. Biederman 	if (ret)
628888af182eSEric W. Biederman 		*ppos = pos;
628956d417b1SBrian Haley 	return ret;
629056d417b1SBrian Haley }
629156d417b1SBrian Haley 
629232927393SChristoph Hellwig static int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
629332927393SChristoph Hellwig 		void *buffer, size_t *lenp, loff_t *ppos)
6294c92d5491Sstephen hemminger {
6295c92d5491Sstephen hemminger 	int *valp = ctl->data;
6296c92d5491Sstephen hemminger 	int ret;
6297c92d5491Sstephen hemminger 	int old, new;
6298c92d5491Sstephen hemminger 
6299c92d5491Sstephen hemminger 	old = *valp;
6300c92d5491Sstephen hemminger 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6301c92d5491Sstephen hemminger 	new = *valp;
6302c92d5491Sstephen hemminger 
6303c92d5491Sstephen hemminger 	if (write && old != new) {
6304c92d5491Sstephen hemminger 		struct net *net = ctl->extra2;
6305c92d5491Sstephen hemminger 
6306c92d5491Sstephen hemminger 		if (!rtnl_trylock())
6307c92d5491Sstephen hemminger 			return restart_syscall();
6308c92d5491Sstephen hemminger 
6309c92d5491Sstephen hemminger 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
631085b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
631185b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
6312c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_DEFAULT,
6313c92d5491Sstephen hemminger 						     net->ipv6.devconf_dflt);
6314c92d5491Sstephen hemminger 		else if (valp == &net->ipv6.devconf_all->proxy_ndp)
631585b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
631685b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
6317c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_ALL,
6318c92d5491Sstephen hemminger 						     net->ipv6.devconf_all);
6319c92d5491Sstephen hemminger 		else {
6320c92d5491Sstephen hemminger 			struct inet6_dev *idev = ctl->extra1;
6321c92d5491Sstephen hemminger 
632285b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
632385b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
6324c92d5491Sstephen hemminger 						     idev->dev->ifindex,
6325c92d5491Sstephen hemminger 						     &idev->cnf);
6326c92d5491Sstephen hemminger 		}
6327c92d5491Sstephen hemminger 		rtnl_unlock();
6328c92d5491Sstephen hemminger 	}
6329c92d5491Sstephen hemminger 
6330c92d5491Sstephen hemminger 	return ret;
6331c92d5491Sstephen hemminger }
6332c92d5491Sstephen hemminger 
6333d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
633432927393SChristoph Hellwig 					 void *buffer, size_t *lenp,
6335d35a00b8SFelix Jia 					 loff_t *ppos)
6336d35a00b8SFelix Jia {
6337d35a00b8SFelix Jia 	int ret = 0;
6338c6dbf7aaSSabrina Dubroca 	u32 new_val;
6339d35a00b8SFelix Jia 	struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6340d35a00b8SFelix Jia 	struct net *net = (struct net *)ctl->extra2;
6341c6dbf7aaSSabrina Dubroca 	struct ctl_table tmp = {
6342c6dbf7aaSSabrina Dubroca 		.data = &new_val,
6343c6dbf7aaSSabrina Dubroca 		.maxlen = sizeof(new_val),
6344c6dbf7aaSSabrina Dubroca 		.mode = ctl->mode,
6345c6dbf7aaSSabrina Dubroca 	};
6346d35a00b8SFelix Jia 
63478c171d6cSFelix Jia 	if (!rtnl_trylock())
63488c171d6cSFelix Jia 		return restart_syscall();
63498c171d6cSFelix Jia 
6350c6dbf7aaSSabrina Dubroca 	new_val = *((u32 *)ctl->data);
6351c6dbf7aaSSabrina Dubroca 
6352c6dbf7aaSSabrina Dubroca 	ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6353c6dbf7aaSSabrina Dubroca 	if (ret != 0)
6354c6dbf7aaSSabrina Dubroca 		goto out;
6355d35a00b8SFelix Jia 
6356d35a00b8SFelix Jia 	if (write) {
63578c171d6cSFelix Jia 		if (check_addr_gen_mode(new_val) < 0) {
63588c171d6cSFelix Jia 			ret = -EINVAL;
63598c171d6cSFelix Jia 			goto out;
63608c171d6cSFelix Jia 		}
6361d35a00b8SFelix Jia 
6362c6dbf7aaSSabrina Dubroca 		if (idev) {
63638c171d6cSFelix Jia 			if (check_stable_privacy(idev, net, new_val) < 0) {
63648c171d6cSFelix Jia 				ret = -EINVAL;
63658c171d6cSFelix Jia 				goto out;
63668c171d6cSFelix Jia 			}
6367d35a00b8SFelix Jia 
6368d35a00b8SFelix Jia 			if (idev->cnf.addr_gen_mode != new_val) {
6369d35a00b8SFelix Jia 				idev->cnf.addr_gen_mode = new_val;
6370d35a00b8SFelix Jia 				addrconf_dev_config(idev->dev);
63718c171d6cSFelix Jia 			}
6372f24c5987SSabrina Dubroca 		} else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6373f24c5987SSabrina Dubroca 			struct net_device *dev;
6374f24c5987SSabrina Dubroca 
6375f24c5987SSabrina Dubroca 			net->ipv6.devconf_dflt->addr_gen_mode = new_val;
6376f24c5987SSabrina Dubroca 			for_each_netdev(net, dev) {
6377f24c5987SSabrina Dubroca 				idev = __in6_dev_get(dev);
6378f24c5987SSabrina Dubroca 				if (idev &&
6379f24c5987SSabrina Dubroca 				    idev->cnf.addr_gen_mode != new_val) {
6380f24c5987SSabrina Dubroca 					idev->cnf.addr_gen_mode = new_val;
6381f24c5987SSabrina Dubroca 					addrconf_dev_config(idev->dev);
6382f24c5987SSabrina Dubroca 				}
6383f24c5987SSabrina Dubroca 			}
63848c171d6cSFelix Jia 		}
6385c6dbf7aaSSabrina Dubroca 
6386c6dbf7aaSSabrina Dubroca 		*((u32 *)ctl->data) = new_val;
63878c171d6cSFelix Jia 	}
63888c171d6cSFelix Jia 
63898c171d6cSFelix Jia out:
6390d35a00b8SFelix Jia 	rtnl_unlock();
6391d35a00b8SFelix Jia 
6392d35a00b8SFelix Jia 	return ret;
6393d35a00b8SFelix Jia }
6394d35a00b8SFelix Jia 
63953d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
639632927393SChristoph Hellwig 					 void *buffer, size_t *lenp,
63973d1bec99SHannes Frederic Sowa 					 loff_t *ppos)
63983d1bec99SHannes Frederic Sowa {
63993d1bec99SHannes Frederic Sowa 	int err;
64003d1bec99SHannes Frederic Sowa 	struct in6_addr addr;
64013d1bec99SHannes Frederic Sowa 	char str[IPV6_MAX_STRLEN];
64023d1bec99SHannes Frederic Sowa 	struct ctl_table lctl = *ctl;
6403622c81d5SHannes Frederic Sowa 	struct net *net = ctl->extra2;
64043d1bec99SHannes Frederic Sowa 	struct ipv6_stable_secret *secret = ctl->data;
64053d1bec99SHannes Frederic Sowa 
6406622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6407622c81d5SHannes Frederic Sowa 		return -EIO;
6408622c81d5SHannes Frederic Sowa 
64093d1bec99SHannes Frederic Sowa 	lctl.maxlen = IPV6_MAX_STRLEN;
64103d1bec99SHannes Frederic Sowa 	lctl.data = str;
64113d1bec99SHannes Frederic Sowa 
64123d1bec99SHannes Frederic Sowa 	if (!rtnl_trylock())
64133d1bec99SHannes Frederic Sowa 		return restart_syscall();
64143d1bec99SHannes Frederic Sowa 
64153d1bec99SHannes Frederic Sowa 	if (!write && !secret->initialized) {
64163d1bec99SHannes Frederic Sowa 		err = -EIO;
64173d1bec99SHannes Frederic Sowa 		goto out;
64183d1bec99SHannes Frederic Sowa 	}
64193d1bec99SHannes Frederic Sowa 
64205449a5caSWANG Cong 	err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
64213d1bec99SHannes Frederic Sowa 	if (err >= sizeof(str)) {
64223d1bec99SHannes Frederic Sowa 		err = -EIO;
64233d1bec99SHannes Frederic Sowa 		goto out;
64243d1bec99SHannes Frederic Sowa 	}
64253d1bec99SHannes Frederic Sowa 
64263d1bec99SHannes Frederic Sowa 	err = proc_dostring(&lctl, write, buffer, lenp, ppos);
64273d1bec99SHannes Frederic Sowa 	if (err || !write)
64283d1bec99SHannes Frederic Sowa 		goto out;
64293d1bec99SHannes Frederic Sowa 
64303d1bec99SHannes Frederic Sowa 	if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
64313d1bec99SHannes Frederic Sowa 		err = -EIO;
64323d1bec99SHannes Frederic Sowa 		goto out;
64333d1bec99SHannes Frederic Sowa 	}
64343d1bec99SHannes Frederic Sowa 
64353d1bec99SHannes Frederic Sowa 	secret->initialized = true;
64363d1bec99SHannes Frederic Sowa 	secret->secret = addr;
64373d1bec99SHannes Frederic Sowa 
6438622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6439622c81d5SHannes Frederic Sowa 		struct net_device *dev;
6440622c81d5SHannes Frederic Sowa 
6441622c81d5SHannes Frederic Sowa 		for_each_netdev(net, dev) {
6442622c81d5SHannes Frederic Sowa 			struct inet6_dev *idev = __in6_dev_get(dev);
6443622c81d5SHannes Frederic Sowa 
6444622c81d5SHannes Frederic Sowa 			if (idev) {
6445d35a00b8SFelix Jia 				idev->cnf.addr_gen_mode =
6446622c81d5SHannes Frederic Sowa 					IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6447622c81d5SHannes Frederic Sowa 			}
6448622c81d5SHannes Frederic Sowa 		}
6449622c81d5SHannes Frederic Sowa 	} else {
6450622c81d5SHannes Frederic Sowa 		struct inet6_dev *idev = ctl->extra1;
6451622c81d5SHannes Frederic Sowa 
6452d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6453622c81d5SHannes Frederic Sowa 	}
6454622c81d5SHannes Frederic Sowa 
64553d1bec99SHannes Frederic Sowa out:
64563d1bec99SHannes Frederic Sowa 	rtnl_unlock();
64573d1bec99SHannes Frederic Sowa 
64583d1bec99SHannes Frederic Sowa 	return err;
64593d1bec99SHannes Frederic Sowa }
6460c92d5491Sstephen hemminger 
646135103d11SAndy Gospodarek static
646235103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
646332927393SChristoph Hellwig 						int write, void *buffer,
646435103d11SAndy Gospodarek 						size_t *lenp,
646535103d11SAndy Gospodarek 						loff_t *ppos)
646635103d11SAndy Gospodarek {
646735103d11SAndy Gospodarek 	int *valp = ctl->data;
646835103d11SAndy Gospodarek 	int val = *valp;
646935103d11SAndy Gospodarek 	loff_t pos = *ppos;
647035103d11SAndy Gospodarek 	struct ctl_table lctl;
647135103d11SAndy Gospodarek 	int ret;
647235103d11SAndy Gospodarek 
647335103d11SAndy Gospodarek 	/* ctl->data points to idev->cnf.ignore_routes_when_linkdown
647435103d11SAndy Gospodarek 	 * we should not modify it until we get the rtnl lock.
647535103d11SAndy Gospodarek 	 */
647635103d11SAndy Gospodarek 	lctl = *ctl;
647735103d11SAndy Gospodarek 	lctl.data = &val;
647835103d11SAndy Gospodarek 
647935103d11SAndy Gospodarek 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
648035103d11SAndy Gospodarek 
648135103d11SAndy Gospodarek 	if (write)
648235103d11SAndy Gospodarek 		ret = addrconf_fixup_linkdown(ctl, valp, val);
648335103d11SAndy Gospodarek 	if (ret)
648435103d11SAndy Gospodarek 		*ppos = pos;
648535103d11SAndy Gospodarek 	return ret;
648635103d11SAndy Gospodarek }
648735103d11SAndy Gospodarek 
6488df789fe7SDavid Forster static
6489df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6490df789fe7SDavid Forster {
6491df789fe7SDavid Forster 	if (rt) {
6492df789fe7SDavid Forster 		if (action)
6493df789fe7SDavid Forster 			rt->dst.flags |= DST_NOPOLICY;
6494df789fe7SDavid Forster 		else
6495df789fe7SDavid Forster 			rt->dst.flags &= ~DST_NOPOLICY;
6496df789fe7SDavid Forster 	}
6497df789fe7SDavid Forster }
6498df789fe7SDavid Forster 
6499df789fe7SDavid Forster static
6500df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6501df789fe7SDavid Forster {
6502df789fe7SDavid Forster 	struct inet6_ifaddr *ifa;
6503df789fe7SDavid Forster 
6504df789fe7SDavid Forster 	read_lock_bh(&idev->lock);
6505df789fe7SDavid Forster 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
6506df789fe7SDavid Forster 		spin_lock(&ifa->lock);
6507df789fe7SDavid Forster 		if (ifa->rt) {
6508f88d8ea6SDavid Ahern 			/* host routes only use builtin fib6_nh */
65091cf844c7SDavid Ahern 			struct fib6_nh *nh = ifa->rt->fib6_nh;
6510df789fe7SDavid Forster 			int cpu;
6511df789fe7SDavid Forster 
651266f5d6ceSWei Wang 			rcu_read_lock();
65133b6761d1SDavid Ahern 			ifa->rt->dst_nopolicy = val ? true : false;
6514f40b6ae2SDavid Ahern 			if (nh->rt6i_pcpu) {
6515df789fe7SDavid Forster 				for_each_possible_cpu(cpu) {
6516df789fe7SDavid Forster 					struct rt6_info **rtp;
6517df789fe7SDavid Forster 
6518f40b6ae2SDavid Ahern 					rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
6519df789fe7SDavid Forster 					addrconf_set_nopolicy(*rtp, val);
6520df789fe7SDavid Forster 				}
6521df789fe7SDavid Forster 			}
652266f5d6ceSWei Wang 			rcu_read_unlock();
6523df789fe7SDavid Forster 		}
6524df789fe7SDavid Forster 		spin_unlock(&ifa->lock);
6525df789fe7SDavid Forster 	}
6526df789fe7SDavid Forster 	read_unlock_bh(&idev->lock);
6527df789fe7SDavid Forster }
6528df789fe7SDavid Forster 
6529df789fe7SDavid Forster static
6530df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
6531df789fe7SDavid Forster {
6532df789fe7SDavid Forster 	struct inet6_dev *idev;
6533df789fe7SDavid Forster 	struct net *net;
6534df789fe7SDavid Forster 
6535df789fe7SDavid Forster 	if (!rtnl_trylock())
6536df789fe7SDavid Forster 		return restart_syscall();
6537df789fe7SDavid Forster 
6538df789fe7SDavid Forster 	*valp = val;
6539df789fe7SDavid Forster 
6540df789fe7SDavid Forster 	net = (struct net *)ctl->extra2;
6541df789fe7SDavid Forster 	if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6542df789fe7SDavid Forster 		rtnl_unlock();
6543df789fe7SDavid Forster 		return 0;
6544df789fe7SDavid Forster 	}
6545df789fe7SDavid Forster 
6546df789fe7SDavid Forster 	if (valp == &net->ipv6.devconf_all->disable_policy)  {
6547df789fe7SDavid Forster 		struct net_device *dev;
6548df789fe7SDavid Forster 
6549df789fe7SDavid Forster 		for_each_netdev(net, dev) {
6550df789fe7SDavid Forster 			idev = __in6_dev_get(dev);
6551df789fe7SDavid Forster 			if (idev)
6552df789fe7SDavid Forster 				addrconf_disable_policy_idev(idev, val);
6553df789fe7SDavid Forster 		}
6554df789fe7SDavid Forster 	} else {
6555df789fe7SDavid Forster 		idev = (struct inet6_dev *)ctl->extra1;
6556df789fe7SDavid Forster 		addrconf_disable_policy_idev(idev, val);
6557df789fe7SDavid Forster 	}
6558df789fe7SDavid Forster 
6559df789fe7SDavid Forster 	rtnl_unlock();
6560df789fe7SDavid Forster 	return 0;
6561df789fe7SDavid Forster }
6562df789fe7SDavid Forster 
656332927393SChristoph Hellwig static int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
656432927393SChristoph Hellwig 				   void *buffer, size_t *lenp, loff_t *ppos)
6565df789fe7SDavid Forster {
6566df789fe7SDavid Forster 	int *valp = ctl->data;
6567df789fe7SDavid Forster 	int val = *valp;
6568df789fe7SDavid Forster 	loff_t pos = *ppos;
6569df789fe7SDavid Forster 	struct ctl_table lctl;
6570df789fe7SDavid Forster 	int ret;
6571df789fe7SDavid Forster 
6572df789fe7SDavid Forster 	lctl = *ctl;
6573df789fe7SDavid Forster 	lctl.data = &val;
6574df789fe7SDavid Forster 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6575df789fe7SDavid Forster 
6576df789fe7SDavid Forster 	if (write && (*valp != val))
6577df789fe7SDavid Forster 		ret = addrconf_disable_policy(ctl, valp, val);
6578df789fe7SDavid Forster 
6579df789fe7SDavid Forster 	if (ret)
6580df789fe7SDavid Forster 		*ppos = pos;
6581df789fe7SDavid Forster 
6582df789fe7SDavid Forster 	return ret;
6583df789fe7SDavid Forster }
6584df789fe7SDavid Forster 
6585cb4a4c69SMaciej Żenczykowski static int minus_one = -1;
6586cb9e684eSMaciej Żenczykowski static const int two_five_five = 255;
6587176f716cSMatthieu Baerts static u32 ioam6_if_id_max = U16_MAX;
6588cb9e684eSMaciej Żenczykowski 
6589607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = {
65901da177e4SLinus Torvalds 	{
65911da177e4SLinus Torvalds 		.procname	= "forwarding",
65921da177e4SLinus Torvalds 		.data		= &ipv6_devconf.forwarding,
65931da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
65941da177e4SLinus Torvalds 		.mode		= 0644,
65956d9f239aSAlexey Dobriyan 		.proc_handler	= addrconf_sysctl_forward,
65961da177e4SLinus Torvalds 	},
65971da177e4SLinus Torvalds 	{
65981da177e4SLinus Torvalds 		.procname	= "hop_limit",
65991da177e4SLinus Torvalds 		.data		= &ipv6_devconf.hop_limit,
66001da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66011da177e4SLinus Torvalds 		.mode		= 0644,
6602cb9e684eSMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
6603eec4844fSMatteo Croce 		.extra1		= (void *)SYSCTL_ONE,
6604cb9e684eSMaciej Żenczykowski 		.extra2		= (void *)&two_five_five,
66051da177e4SLinus Torvalds 	},
66061da177e4SLinus Torvalds 	{
66071da177e4SLinus Torvalds 		.procname	= "mtu",
66081da177e4SLinus Torvalds 		.data		= &ipv6_devconf.mtu6,
66091da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66101da177e4SLinus Torvalds 		.mode		= 0644,
661177751427SMarcelo Leitner 		.proc_handler	= addrconf_sysctl_mtu,
66121da177e4SLinus Torvalds 	},
66131da177e4SLinus Torvalds 	{
66141da177e4SLinus Torvalds 		.procname	= "accept_ra",
66151da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_ra,
66161da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66171da177e4SLinus Torvalds 		.mode		= 0644,
66186d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66191da177e4SLinus Torvalds 	},
66201da177e4SLinus Torvalds 	{
66211da177e4SLinus Torvalds 		.procname	= "accept_redirects",
66221da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_redirects,
66231da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66241da177e4SLinus Torvalds 		.mode		= 0644,
66256d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66261da177e4SLinus Torvalds 	},
66271da177e4SLinus Torvalds 	{
66281da177e4SLinus Torvalds 		.procname	= "autoconf",
66291da177e4SLinus Torvalds 		.data		= &ipv6_devconf.autoconf,
66301da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66311da177e4SLinus Torvalds 		.mode		= 0644,
66326d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66331da177e4SLinus Torvalds 	},
66341da177e4SLinus Torvalds 	{
66351da177e4SLinus Torvalds 		.procname	= "dad_transmits",
66361da177e4SLinus Torvalds 		.data		= &ipv6_devconf.dad_transmits,
66371da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66381da177e4SLinus Torvalds 		.mode		= 0644,
66396d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66401da177e4SLinus Torvalds 	},
66411da177e4SLinus Torvalds 	{
66421da177e4SLinus Torvalds 		.procname	= "router_solicitations",
66431da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicits,
66441da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66451da177e4SLinus Torvalds 		.mode		= 0644,
6646cb4a4c69SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
6647cb4a4c69SMaciej Żenczykowski 		.extra1		= &minus_one,
66481da177e4SLinus Torvalds 	},
66491da177e4SLinus Torvalds 	{
66501da177e4SLinus Torvalds 		.procname	= "router_solicitation_interval",
66511da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_interval,
66521da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66531da177e4SLinus Torvalds 		.mode		= 0644,
66546d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
66551da177e4SLinus Torvalds 	},
66561da177e4SLinus Torvalds 	{
6657bd11f074SMaciej Żenczykowski 		.procname	= "router_solicitation_max_interval",
6658bd11f074SMaciej Żenczykowski 		.data		= &ipv6_devconf.rtr_solicit_max_interval,
6659bd11f074SMaciej Żenczykowski 		.maxlen		= sizeof(int),
6660bd11f074SMaciej Żenczykowski 		.mode		= 0644,
6661bd11f074SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_jiffies,
6662bd11f074SMaciej Żenczykowski 	},
6663bd11f074SMaciej Żenczykowski 	{
66641da177e4SLinus Torvalds 		.procname	= "router_solicitation_delay",
66651da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_delay,
66661da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66671da177e4SLinus Torvalds 		.mode		= 0644,
66686d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
66691da177e4SLinus Torvalds 	},
66701da177e4SLinus Torvalds 	{
66711da177e4SLinus Torvalds 		.procname	= "force_mld_version",
66721da177e4SLinus Torvalds 		.data		= &ipv6_devconf.force_mld_version,
66731da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66741da177e4SLinus Torvalds 		.mode		= 0644,
66756d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66761da177e4SLinus Torvalds 	},
6677fc4eba58SHannes Frederic Sowa 	{
6678fc4eba58SHannes Frederic Sowa 		.procname	= "mldv1_unsolicited_report_interval",
6679fc4eba58SHannes Frederic Sowa 		.data		=
6680fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv1_unsolicited_report_interval,
6681fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6682fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
6683fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
6684fc4eba58SHannes Frederic Sowa 	},
6685fc4eba58SHannes Frederic Sowa 	{
6686fc4eba58SHannes Frederic Sowa 		.procname	= "mldv2_unsolicited_report_interval",
6687fc4eba58SHannes Frederic Sowa 		.data		=
6688fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv2_unsolicited_report_interval,
6689fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6690fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
6691fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
6692fc4eba58SHannes Frederic Sowa 	},
66931da177e4SLinus Torvalds 	{
66941da177e4SLinus Torvalds 		.procname	= "use_tempaddr",
66951da177e4SLinus Torvalds 		.data		= &ipv6_devconf.use_tempaddr,
66961da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
66971da177e4SLinus Torvalds 		.mode		= 0644,
66986d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
66991da177e4SLinus Torvalds 	},
67001da177e4SLinus Torvalds 	{
67011da177e4SLinus Torvalds 		.procname	= "temp_valid_lft",
67021da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_valid_lft,
67031da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
67041da177e4SLinus Torvalds 		.mode		= 0644,
67056d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
67061da177e4SLinus Torvalds 	},
67071da177e4SLinus Torvalds 	{
67081da177e4SLinus Torvalds 		.procname	= "temp_prefered_lft",
67091da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_prefered_lft,
67101da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
67111da177e4SLinus Torvalds 		.mode		= 0644,
67126d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
67131da177e4SLinus Torvalds 	},
67141da177e4SLinus Torvalds 	{
67151da177e4SLinus Torvalds 		.procname	= "regen_max_retry",
67161da177e4SLinus Torvalds 		.data		= &ipv6_devconf.regen_max_retry,
67171da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
67181da177e4SLinus Torvalds 		.mode		= 0644,
67196d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
67201da177e4SLinus Torvalds 	},
67211da177e4SLinus Torvalds 	{
67221da177e4SLinus Torvalds 		.procname	= "max_desync_factor",
67231da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_desync_factor,
67241da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
67251da177e4SLinus Torvalds 		.mode		= 0644,
67266d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
67271da177e4SLinus Torvalds 	},
67281da177e4SLinus Torvalds 	{
67291da177e4SLinus Torvalds 		.procname	= "max_addresses",
67301da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_addresses,
67311da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
67321da177e4SLinus Torvalds 		.mode		= 0644,
67336d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
67341da177e4SLinus Torvalds 	},
67351da177e4SLinus Torvalds 	{
673665f5c7c1SYOSHIFUJI Hideaki 		.procname	= "accept_ra_defrtr",
673765f5c7c1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_defrtr,
673865f5c7c1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
673965f5c7c1SYOSHIFUJI Hideaki 		.mode		= 0644,
67406d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
674165f5c7c1SYOSHIFUJI Hideaki 	},
674265f5c7c1SYOSHIFUJI Hideaki 	{
67436b2e04bcSPraveen Chaudhary 		.procname	= "ra_defrtr_metric",
67446b2e04bcSPraveen Chaudhary 		.data		= &ipv6_devconf.ra_defrtr_metric,
67456b2e04bcSPraveen Chaudhary 		.maxlen		= sizeof(u32),
67466b2e04bcSPraveen Chaudhary 		.mode		= 0644,
67476b2e04bcSPraveen Chaudhary 		.proc_handler	= proc_douintvec_minmax,
67486b2e04bcSPraveen Chaudhary 		.extra1		= (void *)SYSCTL_ONE,
67496b2e04bcSPraveen Chaudhary 	},
67506b2e04bcSPraveen Chaudhary 	{
67518013d1d7SHangbin Liu 		.procname	= "accept_ra_min_hop_limit",
67528013d1d7SHangbin Liu 		.data		= &ipv6_devconf.accept_ra_min_hop_limit,
67538013d1d7SHangbin Liu 		.maxlen		= sizeof(int),
67548013d1d7SHangbin Liu 		.mode		= 0644,
67558013d1d7SHangbin Liu 		.proc_handler	= proc_dointvec,
67568013d1d7SHangbin Liu 	},
67578013d1d7SHangbin Liu 	{
6758c4fd30ebSYOSHIFUJI Hideaki 		.procname	= "accept_ra_pinfo",
6759c4fd30ebSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_pinfo,
6760c4fd30ebSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6761c4fd30ebSYOSHIFUJI Hideaki 		.mode		= 0644,
67626d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
6763c4fd30ebSYOSHIFUJI Hideaki 	},
6764930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
6765930d6ff2SYOSHIFUJI Hideaki 	{
6766930d6ff2SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rtr_pref",
6767930d6ff2SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rtr_pref,
6768930d6ff2SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6769930d6ff2SYOSHIFUJI Hideaki 		.mode		= 0644,
67706d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
6771930d6ff2SYOSHIFUJI Hideaki 	},
677252e16356SYOSHIFUJI Hideaki 	{
677352e16356SYOSHIFUJI Hideaki 		.procname	= "router_probe_interval",
677452e16356SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.rtr_probe_interval,
677552e16356SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
677652e16356SYOSHIFUJI Hideaki 		.mode		= 0644,
67776d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
677852e16356SYOSHIFUJI Hideaki 	},
6779fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
678009c884d4SYOSHIFUJI Hideaki 	{
6781bbea124bSJoel Scherpelz 		.procname	= "accept_ra_rt_info_min_plen",
6782bbea124bSJoel Scherpelz 		.data		= &ipv6_devconf.accept_ra_rt_info_min_plen,
6783bbea124bSJoel Scherpelz 		.maxlen		= sizeof(int),
6784bbea124bSJoel Scherpelz 		.mode		= 0644,
6785bbea124bSJoel Scherpelz 		.proc_handler	= proc_dointvec,
6786bbea124bSJoel Scherpelz 	},
6787bbea124bSJoel Scherpelz 	{
678809c884d4SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rt_info_max_plen",
678909c884d4SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
679009c884d4SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
679109c884d4SYOSHIFUJI Hideaki 		.mode		= 0644,
67926d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
679309c884d4SYOSHIFUJI Hideaki 	},
679409c884d4SYOSHIFUJI Hideaki #endif
6795930d6ff2SYOSHIFUJI Hideaki #endif
6796c4fd30ebSYOSHIFUJI Hideaki 	{
6797fbea49e1SYOSHIFUJI Hideaki 		.procname	= "proxy_ndp",
6798fbea49e1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.proxy_ndp,
6799fbea49e1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6800fbea49e1SYOSHIFUJI Hideaki 		.mode		= 0644,
6801c92d5491Sstephen hemminger 		.proc_handler	= addrconf_sysctl_proxy_ndp,
6802fbea49e1SYOSHIFUJI Hideaki 	},
6803fbea49e1SYOSHIFUJI Hideaki 	{
68040bcbc926SYOSHIFUJI Hideaki 		.procname	= "accept_source_route",
68050bcbc926SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_source_route,
68060bcbc926SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
68070bcbc926SYOSHIFUJI Hideaki 		.mode		= 0644,
68086d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
68090bcbc926SYOSHIFUJI Hideaki 	},
681095c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
681195c385b4SNeil Horman 	{
681295c385b4SNeil Horman 		.procname	= "optimistic_dad",
681395c385b4SNeil Horman 		.data		= &ipv6_devconf.optimistic_dad,
681495c385b4SNeil Horman 		.maxlen		= sizeof(int),
681595c385b4SNeil Horman 		.mode		= 0644,
68166d9f239aSAlexey Dobriyan 		.proc_handler   = proc_dointvec,
681795c385b4SNeil Horman 	},
68187fd2561eSErik Kline 	{
68197fd2561eSErik Kline 		.procname	= "use_optimistic",
68207fd2561eSErik Kline 		.data		= &ipv6_devconf.use_optimistic,
68217fd2561eSErik Kline 		.maxlen		= sizeof(int),
68227fd2561eSErik Kline 		.mode		= 0644,
68237fd2561eSErik Kline 		.proc_handler	= proc_dointvec,
68247fd2561eSErik Kline 	},
682595c385b4SNeil Horman #endif
68267bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
68277bc570c8SYOSHIFUJI Hideaki 	{
68287bc570c8SYOSHIFUJI Hideaki 		.procname	= "mc_forwarding",
68297bc570c8SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.mc_forwarding,
68307bc570c8SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6831a4e6db07SDavid S. Miller 		.mode		= 0444,
68326d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
68337bc570c8SYOSHIFUJI Hideaki 	},
68347bc570c8SYOSHIFUJI Hideaki #endif
68350bcbc926SYOSHIFUJI Hideaki 	{
6836778d80beSYOSHIFUJI Hideaki 		.procname	= "disable_ipv6",
6837778d80beSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.disable_ipv6,
6838778d80beSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6839778d80beSYOSHIFUJI Hideaki 		.mode		= 0644,
684056d417b1SBrian Haley 		.proc_handler	= addrconf_sysctl_disable,
6841778d80beSYOSHIFUJI Hideaki 	},
6842778d80beSYOSHIFUJI Hideaki 	{
68431b34be74SYOSHIFUJI Hideaki 		.procname	= "accept_dad",
68441b34be74SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_dad,
68451b34be74SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
68461b34be74SYOSHIFUJI Hideaki 		.mode		= 0644,
68476d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
68481b34be74SYOSHIFUJI Hideaki 	},
68491b34be74SYOSHIFUJI Hideaki 	{
6850f7734fdfSOctavian Purdila 		.procname	= "force_tllao",
6851f7734fdfSOctavian Purdila 		.data		= &ipv6_devconf.force_tllao,
6852f7734fdfSOctavian Purdila 		.maxlen		= sizeof(int),
6853f7734fdfSOctavian Purdila 		.mode		= 0644,
6854f7734fdfSOctavian Purdila 		.proc_handler	= proc_dointvec
6855f7734fdfSOctavian Purdila 	},
6856f7734fdfSOctavian Purdila 	{
68575cb04436SHannes Frederic Sowa 		.procname	= "ndisc_notify",
68585cb04436SHannes Frederic Sowa 		.data		= &ipv6_devconf.ndisc_notify,
68595cb04436SHannes Frederic Sowa 		.maxlen		= sizeof(int),
68605cb04436SHannes Frederic Sowa 		.mode		= 0644,
68615cb04436SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
68625cb04436SHannes Frederic Sowa 	},
68635cb04436SHannes Frederic Sowa 	{
6864b800c3b9SHannes Frederic Sowa 		.procname	= "suppress_frag_ndisc",
6865b800c3b9SHannes Frederic Sowa 		.data		= &ipv6_devconf.suppress_frag_ndisc,
6866b800c3b9SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6867b800c3b9SHannes Frederic Sowa 		.mode		= 0644,
6868b800c3b9SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
6869b800c3b9SHannes Frederic Sowa 	},
6870b800c3b9SHannes Frederic Sowa 	{
6871d9333196SBen Greear 		.procname	= "accept_ra_from_local",
6872d9333196SBen Greear 		.data		= &ipv6_devconf.accept_ra_from_local,
6873d9333196SBen Greear 		.maxlen		= sizeof(int),
6874d9333196SBen Greear 		.mode		= 0644,
6875d9333196SBen Greear 		.proc_handler	= proc_dointvec,
6876d9333196SBen Greear 	},
6877d9333196SBen Greear 	{
6878c2943f14SHarout Hedeshian 		.procname	= "accept_ra_mtu",
6879c2943f14SHarout Hedeshian 		.data		= &ipv6_devconf.accept_ra_mtu,
6880c2943f14SHarout Hedeshian 		.maxlen		= sizeof(int),
6881c2943f14SHarout Hedeshian 		.mode		= 0644,
6882c2943f14SHarout Hedeshian 		.proc_handler	= proc_dointvec,
6883c2943f14SHarout Hedeshian 	},
6884c2943f14SHarout Hedeshian 	{
68853d1bec99SHannes Frederic Sowa 		.procname	= "stable_secret",
68863d1bec99SHannes Frederic Sowa 		.data		= &ipv6_devconf.stable_secret,
68873d1bec99SHannes Frederic Sowa 		.maxlen		= IPV6_MAX_STRLEN,
68883d1bec99SHannes Frederic Sowa 		.mode		= 0600,
68893d1bec99SHannes Frederic Sowa 		.proc_handler	= addrconf_sysctl_stable_secret,
68903d1bec99SHannes Frederic Sowa 	},
68913d1bec99SHannes Frederic Sowa 	{
68923985e8a3SErik Kline 		.procname	= "use_oif_addrs_only",
68933985e8a3SErik Kline 		.data		= &ipv6_devconf.use_oif_addrs_only,
68943985e8a3SErik Kline 		.maxlen		= sizeof(int),
68953985e8a3SErik Kline 		.mode		= 0644,
68963985e8a3SErik Kline 		.proc_handler	= proc_dointvec,
689735103d11SAndy Gospodarek 	},
689835103d11SAndy Gospodarek 	{
689935103d11SAndy Gospodarek 		.procname	= "ignore_routes_with_linkdown",
690035103d11SAndy Gospodarek 		.data		= &ipv6_devconf.ignore_routes_with_linkdown,
690135103d11SAndy Gospodarek 		.maxlen		= sizeof(int),
690235103d11SAndy Gospodarek 		.mode		= 0644,
690335103d11SAndy Gospodarek 		.proc_handler	= addrconf_sysctl_ignore_routes_with_linkdown,
69043985e8a3SErik Kline 	},
69053985e8a3SErik Kline 	{
6906abbc3043SJohannes Berg 		.procname	= "drop_unicast_in_l2_multicast",
6907abbc3043SJohannes Berg 		.data		= &ipv6_devconf.drop_unicast_in_l2_multicast,
6908abbc3043SJohannes Berg 		.maxlen		= sizeof(int),
6909abbc3043SJohannes Berg 		.mode		= 0644,
6910abbc3043SJohannes Berg 		.proc_handler	= proc_dointvec,
6911abbc3043SJohannes Berg 	},
6912abbc3043SJohannes Berg 	{
69137a02bf89SJohannes Berg 		.procname	= "drop_unsolicited_na",
69147a02bf89SJohannes Berg 		.data		= &ipv6_devconf.drop_unsolicited_na,
69157a02bf89SJohannes Berg 		.maxlen		= sizeof(int),
69167a02bf89SJohannes Berg 		.mode		= 0644,
69177a02bf89SJohannes Berg 		.proc_handler	= proc_dointvec,
69187a02bf89SJohannes Berg 	},
69197a02bf89SJohannes Berg 	{
6920f1705ec1SDavid Ahern 		.procname	= "keep_addr_on_down",
6921f1705ec1SDavid Ahern 		.data		= &ipv6_devconf.keep_addr_on_down,
6922f1705ec1SDavid Ahern 		.maxlen		= sizeof(int),
6923f1705ec1SDavid Ahern 		.mode		= 0644,
6924f1705ec1SDavid Ahern 		.proc_handler	= proc_dointvec,
6925f1705ec1SDavid Ahern 
6926f1705ec1SDavid Ahern 	},
6927f1705ec1SDavid Ahern 	{
69281ababebaSDavid Lebrun 		.procname	= "seg6_enabled",
69291ababebaSDavid Lebrun 		.data		= &ipv6_devconf.seg6_enabled,
69301ababebaSDavid Lebrun 		.maxlen		= sizeof(int),
69311ababebaSDavid Lebrun 		.mode		= 0644,
69321ababebaSDavid Lebrun 		.proc_handler	= proc_dointvec,
69331ababebaSDavid Lebrun 	},
6934bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
6935bf355b8dSDavid Lebrun 	{
6936bf355b8dSDavid Lebrun 		.procname	= "seg6_require_hmac",
6937bf355b8dSDavid Lebrun 		.data		= &ipv6_devconf.seg6_require_hmac,
6938bf355b8dSDavid Lebrun 		.maxlen		= sizeof(int),
6939bf355b8dSDavid Lebrun 		.mode		= 0644,
6940bf355b8dSDavid Lebrun 		.proc_handler	= proc_dointvec,
6941bf355b8dSDavid Lebrun 	},
6942bf355b8dSDavid Lebrun #endif
69431ababebaSDavid Lebrun 	{
6944adc176c5SErik Nordmark 		.procname       = "enhanced_dad",
6945adc176c5SErik Nordmark 		.data           = &ipv6_devconf.enhanced_dad,
6946adc176c5SErik Nordmark 		.maxlen         = sizeof(int),
6947adc176c5SErik Nordmark 		.mode           = 0644,
6948adc176c5SErik Nordmark 		.proc_handler   = proc_dointvec,
6949adc176c5SErik Nordmark 	},
6950adc176c5SErik Nordmark 	{
6951d35a00b8SFelix Jia 		.procname	= "addr_gen_mode",
6952d35a00b8SFelix Jia 		.data		= &ipv6_devconf.addr_gen_mode,
6953d35a00b8SFelix Jia 		.maxlen		= sizeof(int),
6954d35a00b8SFelix Jia 		.mode		= 0644,
6955d35a00b8SFelix Jia 		.proc_handler	= addrconf_sysctl_addr_gen_mode,
6956d35a00b8SFelix Jia 	},
6957d35a00b8SFelix Jia 	{
6958df789fe7SDavid Forster 		.procname       = "disable_policy",
6959df789fe7SDavid Forster 		.data           = &ipv6_devconf.disable_policy,
6960df789fe7SDavid Forster 		.maxlen         = sizeof(int),
6961df789fe7SDavid Forster 		.mode           = 0644,
6962df789fe7SDavid Forster 		.proc_handler   = addrconf_sysctl_disable_policy,
6963df789fe7SDavid Forster 	},
6964df789fe7SDavid Forster 	{
69652210d6b2SMaciej Żenczykowski 		.procname	= "ndisc_tclass",
69662210d6b2SMaciej Żenczykowski 		.data		= &ipv6_devconf.ndisc_tclass,
69672210d6b2SMaciej Żenczykowski 		.maxlen		= sizeof(int),
69682210d6b2SMaciej Żenczykowski 		.mode		= 0644,
69692210d6b2SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
6970eec4844fSMatteo Croce 		.extra1		= (void *)SYSCTL_ZERO,
69712210d6b2SMaciej Żenczykowski 		.extra2		= (void *)&two_five_five,
69722210d6b2SMaciej Żenczykowski 	},
69732210d6b2SMaciej Żenczykowski 	{
69748610c7c6SAlexander Aring 		.procname	= "rpl_seg_enabled",
69758610c7c6SAlexander Aring 		.data		= &ipv6_devconf.rpl_seg_enabled,
69768610c7c6SAlexander Aring 		.maxlen		= sizeof(int),
69778610c7c6SAlexander Aring 		.mode		= 0644,
69788610c7c6SAlexander Aring 		.proc_handler	= proc_dointvec,
69798610c7c6SAlexander Aring 	},
69808610c7c6SAlexander Aring 	{
69819ee11f0fSJustin Iurman 		.procname	= "ioam6_enabled",
69829ee11f0fSJustin Iurman 		.data		= &ipv6_devconf.ioam6_enabled,
69839ee11f0fSJustin Iurman 		.maxlen		= sizeof(u8),
69849ee11f0fSJustin Iurman 		.mode		= 0644,
69859ee11f0fSJustin Iurman 		.proc_handler	= proc_dou8vec_minmax,
69869ee11f0fSJustin Iurman 		.extra1		= (void *)SYSCTL_ZERO,
69879ee11f0fSJustin Iurman 		.extra2		= (void *)SYSCTL_ONE,
69889ee11f0fSJustin Iurman 	},
69899ee11f0fSJustin Iurman 	{
69909ee11f0fSJustin Iurman 		.procname	= "ioam6_id",
69919ee11f0fSJustin Iurman 		.data		= &ipv6_devconf.ioam6_id,
69929ee11f0fSJustin Iurman 		.maxlen		= sizeof(u32),
69939ee11f0fSJustin Iurman 		.mode		= 0644,
69949ee11f0fSJustin Iurman 		.proc_handler	= proc_douintvec_minmax,
69959ee11f0fSJustin Iurman 		.extra1		= (void *)SYSCTL_ZERO,
69969ee11f0fSJustin Iurman 		.extra2		= (void *)&ioam6_if_id_max,
69979ee11f0fSJustin Iurman 	},
69989ee11f0fSJustin Iurman 	{
69999ee11f0fSJustin Iurman 		.procname	= "ioam6_id_wide",
70009ee11f0fSJustin Iurman 		.data		= &ipv6_devconf.ioam6_id_wide,
70019ee11f0fSJustin Iurman 		.maxlen		= sizeof(u32),
70029ee11f0fSJustin Iurman 		.mode		= 0644,
70039ee11f0fSJustin Iurman 		.proc_handler	= proc_douintvec,
70049ee11f0fSJustin Iurman 	},
70059ee11f0fSJustin Iurman 	{
700618ac597aSJames Prestwood 		.procname	= "ndisc_evict_nocarrier",
700718ac597aSJames Prestwood 		.data		= &ipv6_devconf.ndisc_evict_nocarrier,
700818ac597aSJames Prestwood 		.maxlen		= sizeof(u8),
700918ac597aSJames Prestwood 		.mode		= 0644,
701018ac597aSJames Prestwood 		.proc_handler	= proc_dou8vec_minmax,
701118ac597aSJames Prestwood 		.extra1		= (void *)SYSCTL_ZERO,
701218ac597aSJames Prestwood 		.extra2		= (void *)SYSCTL_ONE,
701318ac597aSJames Prestwood 	},
701418ac597aSJames Prestwood 	{
7015f8572d8fSEric W. Biederman 		/* sentinel */
70161da177e4SLinus Torvalds 	}
70171da177e4SLinus Torvalds };
70181da177e4SLinus Torvalds 
7019bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
7020f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
70211da177e4SLinus Torvalds {
702229c994e3SNicolas Dichtel 	int i, ifindex;
7023607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
70246105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
70251dab6222SPavel Emelyanov 
7026607ea7cdSKonstantin Khlebnikov 	table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
7027607ea7cdSKonstantin Khlebnikov 	if (!table)
7028f68635e6SPavel Emelyanov 		goto out;
7029f68635e6SPavel Emelyanov 
7030607ea7cdSKonstantin Khlebnikov 	for (i = 0; table[i].data; i++) {
7031607ea7cdSKonstantin Khlebnikov 		table[i].data += (char *)p - (char *)&ipv6_devconf;
7032cb9e684eSMaciej Żenczykowski 		/* If one of these is already set, then it is not safe to
7033cb9e684eSMaciej Żenczykowski 		 * overwrite either of them: this makes proc_dointvec_minmax
7034cb9e684eSMaciej Żenczykowski 		 * usable.
7035cb9e684eSMaciej Żenczykowski 		 */
7036cb9e684eSMaciej Żenczykowski 		if (!table[i].extra1 && !table[i].extra2) {
7037607ea7cdSKonstantin Khlebnikov 			table[i].extra1 = idev; /* embedded; no ref */
7038607ea7cdSKonstantin Khlebnikov 			table[i].extra2 = net;
70391da177e4SLinus Torvalds 		}
7040cb9e684eSMaciej Żenczykowski 	}
70411da177e4SLinus Torvalds 
70426105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
70431da177e4SLinus Torvalds 
7044607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = register_net_sysctl(net, path, table);
7045607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
70466105e293SEric W. Biederman 		goto free;
7047f68635e6SPavel Emelyanov 
704829c994e3SNicolas Dichtel 	if (!strcmp(dev_name, "all"))
704929c994e3SNicolas Dichtel 		ifindex = NETCONFA_IFINDEX_ALL;
705029c994e3SNicolas Dichtel 	else if (!strcmp(dev_name, "default"))
705129c994e3SNicolas Dichtel 		ifindex = NETCONFA_IFINDEX_DEFAULT;
705229c994e3SNicolas Dichtel 	else
705329c994e3SNicolas Dichtel 		ifindex = idev->dev->ifindex;
705485b3daadSDavid Ahern 	inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
705585b3daadSDavid Ahern 				     ifindex, p);
705695897312SPavel Emelyanov 	return 0;
70571da177e4SLinus Torvalds 
70581da177e4SLinus Torvalds free:
7059607ea7cdSKonstantin Khlebnikov 	kfree(table);
7060f68635e6SPavel Emelyanov out:
706195897312SPavel Emelyanov 	return -ENOBUFS;
70621da177e4SLinus Torvalds }
70631da177e4SLinus Torvalds 
706423452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net,
706523452170SDavid Ahern 					 struct ipv6_devconf *p, int ifindex)
7066f52295a9SPavel Emelyanov {
7067607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
7068f52295a9SPavel Emelyanov 
7069607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
7070408c4768SPavel Emelyanov 		return;
7071408c4768SPavel Emelyanov 
7072607ea7cdSKonstantin Khlebnikov 	table = p->sysctl_header->ctl_table_arg;
7073607ea7cdSKonstantin Khlebnikov 	unregister_net_sysctl_table(p->sysctl_header);
7074607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = NULL;
7075607ea7cdSKonstantin Khlebnikov 	kfree(table);
707623452170SDavid Ahern 
707723452170SDavid Ahern 	inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
70781da177e4SLinus Torvalds }
7079408c4768SPavel Emelyanov 
7080a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev)
7081408c4768SPavel Emelyanov {
7082a317a2f1SWANG Cong 	int err;
7083a317a2f1SWANG Cong 
7084a317a2f1SWANG Cong 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
7085a317a2f1SWANG Cong 		return -EINVAL;
7086a317a2f1SWANG Cong 
7087a317a2f1SWANG Cong 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
7088f8572d8fSEric W. Biederman 				    &ndisc_ifinfo_sysctl_change);
7089a317a2f1SWANG Cong 	if (err)
7090a317a2f1SWANG Cong 		return err;
7091a317a2f1SWANG Cong 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
7092f8572d8fSEric W. Biederman 					 idev, &idev->cnf);
7093a317a2f1SWANG Cong 	if (err)
7094a317a2f1SWANG Cong 		neigh_sysctl_unregister(idev->nd_parms);
7095a317a2f1SWANG Cong 
7096a317a2f1SWANG Cong 	return err;
7097408c4768SPavel Emelyanov }
7098408c4768SPavel Emelyanov 
7099408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
7100408c4768SPavel Emelyanov {
710123452170SDavid Ahern 	__addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
710223452170SDavid Ahern 				     idev->dev->ifindex);
7103408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
71041da177e4SLinus Torvalds }
71051da177e4SLinus Torvalds 
71061da177e4SLinus Torvalds 
71071da177e4SLinus Torvalds #endif
71081da177e4SLinus Torvalds 
71092c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
7110e0da5a48SPavel Emelyanov {
7111a79ca223SHong Zhiguo 	int err = -ENOMEM;
7112e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
7113e0da5a48SPavel Emelyanov 
7114*21a216a8SEric Dumazet 	spin_lock_init(&net->ipv6.addrconf_hash_lock);
7115*21a216a8SEric Dumazet 	net->ipv6.inet6_addr_lst = kcalloc(IN6_ADDR_HSIZE,
7116*21a216a8SEric Dumazet 					   sizeof(struct hlist_head),
7117*21a216a8SEric Dumazet 					   GFP_KERNEL);
7118*21a216a8SEric Dumazet 	if (!net->ipv6.inet6_addr_lst)
7119*21a216a8SEric Dumazet 		goto err_alloc_addr;
7120*21a216a8SEric Dumazet 
7121a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
712263159f29SIan Morris 	if (!all)
7123e0da5a48SPavel Emelyanov 		goto err_alloc_all;
7124e0da5a48SPavel Emelyanov 
7125a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
712663159f29SIan Morris 	if (!dflt)
7127e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
7128a79ca223SHong Zhiguo 
7129a154d5d8SArnd Bergmann 	if (IS_ENABLED(CONFIG_SYSCTL) &&
71309efd6a3cSNicolas Dichtel 	    !net_eq(net, &init_net)) {
71319efd6a3cSNicolas Dichtel 		switch (sysctl_devconf_inherit_init_net) {
71329efd6a3cSNicolas Dichtel 		case 1:  /* copy from init_net */
71339efd6a3cSNicolas Dichtel 			memcpy(all, init_net.ipv6.devconf_all,
71349efd6a3cSNicolas Dichtel 			       sizeof(ipv6_devconf));
71359efd6a3cSNicolas Dichtel 			memcpy(dflt, init_net.ipv6.devconf_dflt,
71369efd6a3cSNicolas Dichtel 			       sizeof(ipv6_devconf_dflt));
71379efd6a3cSNicolas Dichtel 			break;
71389efd6a3cSNicolas Dichtel 		case 3: /* copy from the current netns */
71399efd6a3cSNicolas Dichtel 			memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all,
71409efd6a3cSNicolas Dichtel 			       sizeof(ipv6_devconf));
71419efd6a3cSNicolas Dichtel 			memcpy(dflt,
71429efd6a3cSNicolas Dichtel 			       current->nsproxy->net_ns->ipv6.devconf_dflt,
71439efd6a3cSNicolas Dichtel 			       sizeof(ipv6_devconf_dflt));
71449efd6a3cSNicolas Dichtel 			break;
71459efd6a3cSNicolas Dichtel 		case 0:
71469efd6a3cSNicolas Dichtel 		case 2:
71479efd6a3cSNicolas Dichtel 			/* use compiled values */
71489efd6a3cSNicolas Dichtel 			break;
71499efd6a3cSNicolas Dichtel 		}
7150856c395cSCong Wang 	}
7151856c395cSCong Wang 
715256d417b1SBrian Haley 	/* these will be inherited by all namespaces */
715356d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
715456d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
7155e0da5a48SPavel Emelyanov 
71563d1bec99SHannes Frederic Sowa 	dflt->stable_secret.initialized = false;
71573d1bec99SHannes Frederic Sowa 	all->stable_secret.initialized = false;
71583d1bec99SHannes Frederic Sowa 
7159e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
7160e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
7161e0da5a48SPavel Emelyanov 
7162e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
7163f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
7164e0da5a48SPavel Emelyanov 	if (err < 0)
7165e0da5a48SPavel Emelyanov 		goto err_reg_all;
7166e0da5a48SPavel Emelyanov 
7167f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
7168e0da5a48SPavel Emelyanov 	if (err < 0)
7169e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
7170e0da5a48SPavel Emelyanov #endif
7171e0da5a48SPavel Emelyanov 	return 0;
7172e0da5a48SPavel Emelyanov 
7173e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
7174e0da5a48SPavel Emelyanov err_reg_dflt:
717523452170SDavid Ahern 	__addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
7176e0da5a48SPavel Emelyanov err_reg_all:
7177e0da5a48SPavel Emelyanov 	kfree(dflt);
7178e0da5a48SPavel Emelyanov #endif
7179e0da5a48SPavel Emelyanov err_alloc_dflt:
7180e0da5a48SPavel Emelyanov 	kfree(all);
7181e0da5a48SPavel Emelyanov err_alloc_all:
7182*21a216a8SEric Dumazet 	kfree(net->ipv6.inet6_addr_lst);
7183*21a216a8SEric Dumazet err_alloc_addr:
7184e0da5a48SPavel Emelyanov 	return err;
7185e0da5a48SPavel Emelyanov }
7186e0da5a48SPavel Emelyanov 
71872c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
7188e0da5a48SPavel Emelyanov {
7189*21a216a8SEric Dumazet 	int i;
7190*21a216a8SEric Dumazet 
7191e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
719223452170SDavid Ahern 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
719323452170SDavid Ahern 				     NETCONFA_IFINDEX_DEFAULT);
719423452170SDavid Ahern 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
719523452170SDavid Ahern 				     NETCONFA_IFINDEX_ALL);
7196e0da5a48SPavel Emelyanov #endif
7197e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_dflt);
71987d9b1b57SEric Dumazet 	net->ipv6.devconf_dflt = NULL;
7199e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_all);
72007d9b1b57SEric Dumazet 	net->ipv6.devconf_all = NULL;
7201*21a216a8SEric Dumazet 
7202*21a216a8SEric Dumazet 	/*
7203*21a216a8SEric Dumazet 	 *	Check hash table, then free it.
7204*21a216a8SEric Dumazet 	 */
7205*21a216a8SEric Dumazet 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
7206*21a216a8SEric Dumazet 		WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i]));
7207*21a216a8SEric Dumazet 
7208*21a216a8SEric Dumazet 	kfree(net->ipv6.inet6_addr_lst);
7209*21a216a8SEric Dumazet 	net->ipv6.inet6_addr_lst = NULL;
7210e0da5a48SPavel Emelyanov }
7211e0da5a48SPavel Emelyanov 
7212e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
7213e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
7214e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
7215e0da5a48SPavel Emelyanov };
7216e0da5a48SPavel Emelyanov 
7217207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = {
7218b382b191SThomas Graf 	.family		  = AF_INET6,
7219b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
7220b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
722111b1f828SDaniel Borkmann 	.validate_link_af = inet6_validate_link_af,
7222f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
7223b382b191SThomas Graf };
7224b382b191SThomas Graf 
72251da177e4SLinus Torvalds /*
72261da177e4SLinus Torvalds  *	Init / cleanup code
72271da177e4SLinus Torvalds  */
72281da177e4SLinus Torvalds 
72291da177e4SLinus Torvalds int __init addrconf_init(void)
72301da177e4SLinus Torvalds {
7231a317a2f1SWANG Cong 	struct inet6_dev *idev;
7232c2e21293Sstephen hemminger 	int i, err;
72332a8cc6c8SYOSHIFUJI Hideaki 
7234e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
7235e21e8467SStephen Hemminger 	if (err < 0) {
7236f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
7237f3213831SJoe Perches 			__func__, err);
72382cc6d2bfSNeil Horman 		goto out;
72392a8cc6c8SYOSHIFUJI Hideaki 	}
72401da177e4SLinus Torvalds 
72412cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
72422cc6d2bfSNeil Horman 	if (err < 0)
72432cc6d2bfSNeil Horman 		goto out_addrlabel;
7244e0da5a48SPavel Emelyanov 
7245c15b1ccaSHannes Frederic Sowa 	addrconf_wq = create_workqueue("ipv6_addrconf");
7246c15b1ccaSHannes Frederic Sowa 	if (!addrconf_wq) {
7247c15b1ccaSHannes Frederic Sowa 		err = -ENOMEM;
7248c15b1ccaSHannes Frederic Sowa 		goto out_nowq;
7249c15b1ccaSHannes Frederic Sowa 	}
7250c15b1ccaSHannes Frederic Sowa 
72511da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
72521da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
72531da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
72541da177e4SLinus Torvalds 	 * to other devices which are up.
72551da177e4SLinus Torvalds 	 *
72561da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
72571da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
72581da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
72591da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
72601da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
72611da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
72621da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
72631da177e4SLinus Torvalds 	 *
72641da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
72651da177e4SLinus Torvalds 	 * loopback_dev first by hand.
72661da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
72671da177e4SLinus Torvalds 	 * device and it being up should be removed.
72681da177e4SLinus Torvalds 	 */
72691da177e4SLinus Torvalds 	rtnl_lock();
7270a317a2f1SWANG Cong 	idev = ipv6_add_dev(init_net.loopback_dev);
72711da177e4SLinus Torvalds 	rtnl_unlock();
7272a317a2f1SWANG Cong 	if (IS_ERR(idev)) {
7273a317a2f1SWANG Cong 		err = PTR_ERR(idev);
7274e0da5a48SPavel Emelyanov 		goto errlo;
7275a317a2f1SWANG Cong 	}
72761da177e4SLinus Torvalds 
72772f460933SWANG Cong 	ip6_route_init_special_entries();
72782f460933SWANG Cong 
7279c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
7280c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
7281c2e21293Sstephen hemminger 
72821da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
72831da177e4SLinus Torvalds 
7284c15b1ccaSHannes Frederic Sowa 	addrconf_verify();
7285c127ea2cSThomas Graf 
72863678a9d8Sstephen hemminger 	rtnl_af_register(&inet6_ops);
7287b382b191SThomas Graf 
728816feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
728916feebcfSFlorian Westphal 				   NULL, inet6_dump_ifinfo, 0);
7290c127ea2cSThomas Graf 	if (err < 0)
7291c127ea2cSThomas Graf 		goto errout;
7292c127ea2cSThomas Graf 
729316feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
729416feebcfSFlorian Westphal 				   inet6_rtm_newaddr, NULL, 0);
729516feebcfSFlorian Westphal 	if (err < 0)
729616feebcfSFlorian Westphal 		goto errout;
729716feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
729816feebcfSFlorian Westphal 				   inet6_rtm_deladdr, NULL, 0);
729916feebcfSFlorian Westphal 	if (err < 0)
730016feebcfSFlorian Westphal 		goto errout;
730116feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
730216feebcfSFlorian Westphal 				   inet6_rtm_getaddr, inet6_dump_ifaddr,
730316feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
730416feebcfSFlorian Westphal 	if (err < 0)
730516feebcfSFlorian Westphal 		goto errout;
730616feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
730716feebcfSFlorian Westphal 				   NULL, inet6_dump_ifmcaddr, 0);
730816feebcfSFlorian Westphal 	if (err < 0)
730916feebcfSFlorian Westphal 		goto errout;
731016feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
731116feebcfSFlorian Westphal 				   NULL, inet6_dump_ifacaddr, 0);
731216feebcfSFlorian Westphal 	if (err < 0)
731316feebcfSFlorian Westphal 		goto errout;
731416feebcfSFlorian Westphal 	err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
731516feebcfSFlorian Westphal 				   inet6_netconf_get_devconf,
731616feebcfSFlorian Westphal 				   inet6_netconf_dump_devconf,
731716feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
731816feebcfSFlorian Westphal 	if (err < 0)
731916feebcfSFlorian Westphal 		goto errout;
7320a3fde2adSFlorian Westphal 	err = ipv6_addr_label_rtnl_register();
7321a3fde2adSFlorian Westphal 	if (err < 0)
7322a3fde2adSFlorian Westphal 		goto errout;
73232a8cc6c8SYOSHIFUJI Hideaki 
73241da177e4SLinus Torvalds 	return 0;
7325c127ea2cSThomas Graf errout:
732616feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
7327b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
7328c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
7329e0da5a48SPavel Emelyanov errlo:
7330c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
7331c15b1ccaSHannes Frederic Sowa out_nowq:
7332e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
73332cc6d2bfSNeil Horman out_addrlabel:
73342cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
73352cc6d2bfSNeil Horman out:
7336c127ea2cSThomas Graf 	return err;
73371da177e4SLinus Torvalds }
73381da177e4SLinus Torvalds 
733909f7709fSDaniel Lezcano void addrconf_cleanup(void)
73401da177e4SLinus Torvalds {
7341176c39afSDaniel Lezcano 	struct net_device *dev;
73421da177e4SLinus Torvalds 	int i;
73431da177e4SLinus Torvalds 
73441da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
7345e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
73462cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
73471da177e4SLinus Torvalds 
73485c45121dSFlorian Westphal 	rtnl_af_unregister(&inet6_ops);
73491da177e4SLinus Torvalds 
73505c45121dSFlorian Westphal 	rtnl_lock();
7351b382b191SThomas Graf 
7352176c39afSDaniel Lezcano 	/* clean dev list */
7353176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
7354176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
7355176c39afSDaniel Lezcano 			continue;
7356ae79dbf6SFlorent Fourcot 		addrconf_ifdown(dev, true);
7357176c39afSDaniel Lezcano 	}
7358ae79dbf6SFlorent Fourcot 	addrconf_ifdown(init_net.loopback_dev, true);
7359176c39afSDaniel Lezcano 
73601da177e4SLinus Torvalds 	/*
73611da177e4SLinus Torvalds 	 *	Check hash table.
73621da177e4SLinus Torvalds 	 */
73635c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
7364c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
7365c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
73665c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
7367c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
73681da177e4SLinus Torvalds 	rtnl_unlock();
7369c15b1ccaSHannes Frederic Sowa 
7370c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
73711da177e4SLinus Torvalds }
7372