xref: /openbmc/linux/net/ipv6/addrconf.c (revision 38fbeeeeccdb38d0635398e8e344d245f6d8dc52)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	IPv6 Address [auto]configuration
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
101da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
111da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
121da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds /*
161da177e4SLinus Torvalds  *	Changes:
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *	Janos Farkas			:	delete timer on ifdown
191da177e4SLinus Torvalds  *	<chexum@bankinf.banki.hu>
201da177e4SLinus Torvalds  *	Andi Kleen			:	kill double kfree on module
211da177e4SLinus Torvalds  *						unload.
221da177e4SLinus Torvalds  *	Maciej W. Rozycki		:	FDDI support
231da177e4SLinus Torvalds  *	sekiya@USAGI			:	Don't send too many RS
241da177e4SLinus Torvalds  *						packets.
251da177e4SLinus Torvalds  *	yoshfuji@USAGI			:       Fixed interval between DAD
261da177e4SLinus Torvalds  *						packets.
271da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	improved accuracy of
281da177e4SLinus Torvalds  *						address validation timer.
291da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	Privacy Extensions (RFC3041)
301da177e4SLinus Torvalds  *						support.
311da177e4SLinus Torvalds  *	Yuji SEKIYA @USAGI		:	Don't assign a same IPv6
321da177e4SLinus Torvalds  *						address on a same interface.
331da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	ARCnet support
341da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	convert /proc/net/if_inet6 to
351da177e4SLinus Torvalds  *						seq_file.
36b1cacb68SYOSHIFUJI Hideaki  *	YOSHIFUJI Hideaki @USAGI	:	improved source address
37b1cacb68SYOSHIFUJI Hideaki  *						selection; consider scope,
38b1cacb68SYOSHIFUJI Hideaki  *						status etc.
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
41f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
42f3213831SJoe Perches 
431da177e4SLinus Torvalds #include <linux/errno.h>
441da177e4SLinus Torvalds #include <linux/types.h>
45a0bffffcSIlpo Järvinen #include <linux/kernel.h>
46174cd4b1SIngo Molnar #include <linux/sched/signal.h>
471da177e4SLinus Torvalds #include <linux/socket.h>
481da177e4SLinus Torvalds #include <linux/sockios.h>
491da177e4SLinus Torvalds #include <linux/net.h>
503d1bec99SHannes Frederic Sowa #include <linux/inet.h>
511da177e4SLinus Torvalds #include <linux/in6.h>
521da177e4SLinus Torvalds #include <linux/netdevice.h>
531823730fSThomas Graf #include <linux/if_addr.h>
541da177e4SLinus Torvalds #include <linux/if_arp.h>
551da177e4SLinus Torvalds #include <linux/if_arcnet.h>
561da177e4SLinus Torvalds #include <linux/if_infiniband.h>
571da177e4SLinus Torvalds #include <linux/route.h>
581da177e4SLinus Torvalds #include <linux/inetdevice.h>
591da177e4SLinus Torvalds #include <linux/init.h>
605a0e3ad6STejun Heo #include <linux/slab.h>
611da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
621da177e4SLinus Torvalds #include <linux/sysctl.h>
631da177e4SLinus Torvalds #endif
644fc268d2SRandy Dunlap #include <linux/capability.h>
651da177e4SLinus Torvalds #include <linux/delay.h>
661da177e4SLinus Torvalds #include <linux/notifier.h>
67543537bdSPaulo Marques #include <linux/string.h>
68ddbe5032SEric Dumazet #include <linux/hash.h>
691da177e4SLinus Torvalds 
70457c4cbcSEric W. Biederman #include <net/net_namespace.h>
711da177e4SLinus Torvalds #include <net/sock.h>
721da177e4SLinus Torvalds #include <net/snmp.h>
731da177e4SLinus Torvalds 
745241c2d7SAlexander Aring #include <net/6lowpan.h>
75cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h>
761da177e4SLinus Torvalds #include <net/ipv6.h>
771da177e4SLinus Torvalds #include <net/protocol.h>
781da177e4SLinus Torvalds #include <net/ndisc.h>
791da177e4SLinus Torvalds #include <net/ip6_route.h>
801da177e4SLinus Torvalds #include <net/addrconf.h>
811da177e4SLinus Torvalds #include <net/tcp.h>
821da177e4SLinus Torvalds #include <net/ip.h>
835d620266SThomas Graf #include <net/netlink.h>
84f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
85ca254490SDavid Ahern #include <net/l3mdev.h>
861da177e4SLinus Torvalds #include <linux/if_tunnel.h>
871da177e4SLinus Torvalds #include <linux/rtnetlink.h>
88f3a1bfb1SNicolas Dichtel #include <linux/netconf.h>
891da177e4SLinus Torvalds #include <linux/random.h>
90e21e8467SStephen Hemminger #include <linux/uaccess.h>
917f7d9a6bSHerbert Xu #include <asm/unaligned.h>
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #include <linux/proc_fs.h>
941da177e4SLinus Torvalds #include <linux/seq_file.h>
95bc3b2d7fSPaul Gortmaker #include <linux/export.h>
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds /* Set to 3 to get tracing... */
981da177e4SLinus Torvalds #define ACONF_DEBUG 2
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
101ba3542e1Sdingtianhong #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
1021da177e4SLinus Torvalds #else
103ba3542e1Sdingtianhong #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
1041da177e4SLinus Torvalds #endif
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
10718a31e1eSThomas Graf 
1083d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \
1093d1bec99SHannes Frederic Sowa 	sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
1103d1bec99SHannes Frederic Sowa 
11118a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp)
11218a31e1eSThomas Graf {
11318a31e1eSThomas Graf 	return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
11418a31e1eSThomas Graf }
1151da177e4SLinus Torvalds 
116bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_init(s32 irt)
117bd11f074SMaciej Żenczykowski {
118bd11f074SMaciej Żenczykowski 	/* multiply 'initial retransmission time' by 0.9 .. 1.1 */
119bd11f074SMaciej Żenczykowski 	u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
120bd11f074SMaciej Żenczykowski 	do_div(tmp, 1000000);
121bd11f074SMaciej Żenczykowski 	return (s32)tmp;
122bd11f074SMaciej Żenczykowski }
123bd11f074SMaciej Żenczykowski 
124bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
125bd11f074SMaciej Żenczykowski {
126bd11f074SMaciej Żenczykowski 	/* multiply 'retransmission timeout' by 1.9 .. 2.1 */
127bd11f074SMaciej Żenczykowski 	u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
128bd11f074SMaciej Żenczykowski 	do_div(tmp, 1000000);
129bd11f074SMaciej Żenczykowski 	if ((s32)tmp > mrt) {
130bd11f074SMaciej Żenczykowski 		/* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
131bd11f074SMaciej Żenczykowski 		tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
132bd11f074SMaciej Żenczykowski 		do_div(tmp, 1000000);
133bd11f074SMaciej Żenczykowski 	}
134bd11f074SMaciej Żenczykowski 	return (s32)tmp;
135bd11f074SMaciej Żenczykowski }
136bd11f074SMaciej Żenczykowski 
1371da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
138a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev);
139408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
140408c4768SPavel Emelyanov #else
141a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev)
142408c4768SPavel Emelyanov {
143a317a2f1SWANG Cong 	return 0;
144408c4768SPavel Emelyanov }
145408c4768SPavel Emelyanov 
146408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
147408c4768SPavel Emelyanov {
148408c4768SPavel Emelyanov }
1491da177e4SLinus Torvalds #endif
1501da177e4SLinus Torvalds 
1519d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev);
1529d6280daSJiri Bohac static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1531da177e4SLinus Torvalds 
1541b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
1551da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev);
156622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr,
157622c81d5SHannes Frederic Sowa 					u8 dad_count,
158622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds /*
1611da177e4SLinus Torvalds  *	Configured unicast address hash table
1621da177e4SLinus Torvalds  */
163c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
1645c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock);
1651da177e4SLinus Torvalds 
166c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void);
167c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void);
168c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *);
1691da177e4SLinus Torvalds 
170c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq;
171c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1741da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1751da177e4SLinus Torvalds 
17693d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev,
17775c78500SMoni Shoua 				 unsigned long event);
1781da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1791da177e4SLinus Torvalds 
18021caa662SRomain Kuntz static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
18121caa662SRomain Kuntz 						  int plen,
18221caa662SRomain Kuntz 						  const struct net_device *dev,
18321caa662SRomain Kuntz 						  u32 flags, u32 noflags);
18421caa662SRomain Kuntz 
185cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp);
186c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w);
187764d3be6SPaolo Abeni static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id);
188c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1891da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1901da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1911da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1941da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1953e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
19606bfe655SDaniel Lezcano 			       struct net_device *dev);
1971da177e4SLinus Torvalds 
198888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1991da177e4SLinus Torvalds 	.forwarding		= 0,
2001da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2011da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2021da177e4SLinus Torvalds 	.accept_ra		= 1,
2031da177e4SLinus Torvalds 	.accept_redirects	= 1,
2041da177e4SLinus Torvalds 	.autoconf		= 1,
2051da177e4SLinus Torvalds 	.force_mld_version	= 0,
206fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
207fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2081da177e4SLinus Torvalds 	.dad_transmits		= 1,
2091da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2101da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
211bd11f074SMaciej Żenczykowski 	.rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
2121da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2131da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2141da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2151da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2161da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2171da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2181da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
21965f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
220d9333196SBen Greear 	.accept_ra_from_local	= 0,
2218013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
222c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
223930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
224930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
22552e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
22609c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
227bbea124bSJoel Scherpelz 	.accept_ra_rt_info_min_plen = 0,
22809c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
22909c884d4SYOSHIFUJI Hideaki #endif
230930d6ff2SYOSHIFUJI Hideaki #endif
231fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2320bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
233778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2341b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
235b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
236c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2373d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2383d1bec99SHannes Frederic Sowa 		.initialized = false,
2393985e8a3SErik Kline 	},
2403985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
24135103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
242f1705ec1SDavid Ahern 	.keep_addr_on_down	= 0,
2431ababebaSDavid Lebrun 	.seg6_enabled		= 0,
244bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
245bf355b8dSDavid Lebrun 	.seg6_require_hmac	= 0,
246bf355b8dSDavid Lebrun #endif
247adc176c5SErik Nordmark 	.enhanced_dad           = 1,
248d35a00b8SFelix Jia 	.addr_gen_mode		= IN6_ADDR_GEN_MODE_EUI64,
249df789fe7SDavid Forster 	.disable_policy		= 0,
2501da177e4SLinus Torvalds };
2511da177e4SLinus Torvalds 
252ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2531da177e4SLinus Torvalds 	.forwarding		= 0,
2541da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2551da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2561da177e4SLinus Torvalds 	.accept_ra		= 1,
2571da177e4SLinus Torvalds 	.accept_redirects	= 1,
2581da177e4SLinus Torvalds 	.autoconf		= 1,
259fc4eba58SHannes Frederic Sowa 	.force_mld_version	= 0,
260fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
261fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2621da177e4SLinus Torvalds 	.dad_transmits		= 1,
2631da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2641da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
265bd11f074SMaciej Żenczykowski 	.rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
2661da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2671da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2681da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2691da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2701da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2711da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2721da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
27365f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
274d9333196SBen Greear 	.accept_ra_from_local	= 0,
2758013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
276c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
277930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
278930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
27952e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
28009c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
281bbea124bSJoel Scherpelz 	.accept_ra_rt_info_min_plen = 0,
28209c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
28309c884d4SYOSHIFUJI Hideaki #endif
284930d6ff2SYOSHIFUJI Hideaki #endif
285fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2860bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
287778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2881b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
289b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
290c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2913d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2923d1bec99SHannes Frederic Sowa 		.initialized = false,
2933d1bec99SHannes Frederic Sowa 	},
2943985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
29535103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
296f1705ec1SDavid Ahern 	.keep_addr_on_down	= 0,
2971ababebaSDavid Lebrun 	.seg6_enabled		= 0,
298bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
299bf355b8dSDavid Lebrun 	.seg6_require_hmac	= 0,
300bf355b8dSDavid Lebrun #endif
301adc176c5SErik Nordmark 	.enhanced_dad           = 1,
302d35a00b8SFelix Jia 	.addr_gen_mode		= IN6_ADDR_GEN_MODE_EUI64,
303df789fe7SDavid Forster 	.disable_policy		= 0,
3041da177e4SLinus Torvalds };
3051da177e4SLinus Torvalds 
3061f372c7bSMike Manning /* Check if link is ready: is it up and is a valid qdisc available */
3071f372c7bSMike Manning static inline bool addrconf_link_ready(const struct net_device *dev)
308f24e3d65SMitsuru Chinen {
3091f372c7bSMike Manning 	return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
310f24e3d65SMitsuru Chinen }
311f24e3d65SMitsuru Chinen 
312b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev)
3131da177e4SLinus Torvalds {
314b7b1bfceSHannes Frederic Sowa 	if (del_timer(&idev->rs_timer))
315b7b1bfceSHannes Frederic Sowa 		__in6_dev_put(idev);
316b7b1bfceSHannes Frederic Sowa }
317b7b1bfceSHannes Frederic Sowa 
318c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
319b7b1bfceSHannes Frederic Sowa {
320c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
3211da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
324b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev,
3251da177e4SLinus Torvalds 				  unsigned long when)
3261da177e4SLinus Torvalds {
327b7b1bfceSHannes Frederic Sowa 	if (!timer_pending(&idev->rs_timer))
328b7b1bfceSHannes Frederic Sowa 		in6_dev_hold(idev);
329b7b1bfceSHannes Frederic Sowa 	mod_timer(&idev->rs_timer, jiffies + when);
3301da177e4SLinus Torvalds }
331b7b1bfceSHannes Frederic Sowa 
332c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
333c15b1ccaSHannes Frederic Sowa 				   unsigned long delay)
334b7b1bfceSHannes Frederic Sowa {
335b7b1bfceSHannes Frederic Sowa 	in6_ifa_hold(ifp);
336f8a894b2SXin Long 	if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
337f8a894b2SXin Long 		in6_ifa_put(ifp);
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3407f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
3417f7d9a6bSHerbert Xu {
342827da44cSJohn Stultz 	int i;
343827da44cSJohn Stultz 
344698365faSWANG Cong 	idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
345698365faSWANG Cong 	if (!idev->stats.ipv6)
3467f7d9a6bSHerbert Xu 		goto err_ip;
347827da44cSJohn Stultz 
348827da44cSJohn Stultz 	for_each_possible_cpu(i) {
349827da44cSJohn Stultz 		struct ipstats_mib *addrconf_stats;
350698365faSWANG Cong 		addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
351827da44cSJohn Stultz 		u64_stats_init(&addrconf_stats->syncp);
352827da44cSJohn Stultz 	}
353827da44cSJohn Stultz 
354827da44cSJohn Stultz 
355be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
356be281e55SEric Dumazet 					GFP_KERNEL);
357be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
3587f7d9a6bSHerbert Xu 		goto err_icmp;
359be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
360be281e55SEric Dumazet 					   GFP_KERNEL);
361be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
36214878f75SDavid L Stevens 		goto err_icmpmsg;
3637f7d9a6bSHerbert Xu 
3647f7d9a6bSHerbert Xu 	return 0;
3657f7d9a6bSHerbert Xu 
36614878f75SDavid L Stevens err_icmpmsg:
367be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3687f7d9a6bSHerbert Xu err_icmp:
369698365faSWANG Cong 	free_percpu(idev->stats.ipv6);
3707f7d9a6bSHerbert Xu err_ip:
371aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3727f7d9a6bSHerbert Xu }
3737f7d9a6bSHerbert Xu 
3741da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	struct inet6_dev *ndev;
377a317a2f1SWANG Cong 	int err = -ENOMEM;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	ASSERT_RTNL();
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
382a317a2f1SWANG Cong 		return ERR_PTR(-EINVAL);
3831da177e4SLinus Torvalds 
384322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
38563159f29SIan Morris 	if (!ndev)
386a317a2f1SWANG Cong 		return ERR_PTR(err);
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3891da177e4SLinus Torvalds 	ndev->dev = dev;
390502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
391b7b1bfceSHannes Frederic Sowa 	setup_timer(&ndev->rs_timer, addrconf_rs_timer,
392b7b1bfceSHannes Frederic Sowa 		    (unsigned long)ndev);
393c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3949b29c696SHannes Frederic Sowa 
3959b29c696SHannes Frederic Sowa 	if (ndev->cnf.stable_secret.initialized)
396d35a00b8SFelix Jia 		ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
3979b29c696SHannes Frederic Sowa 	else
398d35a00b8SFelix Jia 		ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode;
3999b29c696SHannes Frederic Sowa 
4001da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
4011da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
40263159f29SIan Morris 	if (!ndev->nd_parms) {
4031da177e4SLinus Torvalds 		kfree(ndev);
404a317a2f1SWANG Cong 		return ERR_PTR(err);
4051da177e4SLinus Torvalds 	}
4060187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
4070187bdfbSBen Hutchings 		dev_disable_lro(dev);
4081da177e4SLinus Torvalds 	/* We refer to the device */
4091da177e4SLinus Torvalds 	dev_hold(dev);
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
412ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
413f3213831SJoe Perches 			"%s: cannot allocate memory for statistics; dev=%s.\n",
414ba3542e1Sdingtianhong 			__func__, dev->name);
4151da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
4168603e33dSRoy Li 		dev_put(dev);
4178603e33dSRoy Li 		kfree(ndev);
418a317a2f1SWANG Cong 		return ERR_PTR(err);
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
422ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
423f3213831SJoe Perches 			"%s: cannot create /proc/net/dev_snmp6/%s\n",
424ba3542e1Sdingtianhong 			__func__, dev->name);
425a317a2f1SWANG Cong 		goto err_release;
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 
4289d6280daSJiri Bohac 	/* One reference from device. */
4291be92460SReshetova, Elena 	refcount_set(&ndev->refcnt, 1);
4301da177e4SLinus Torvalds 
4311b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
4321b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
4331b34be74SYOSHIFUJI Hideaki 
43407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
435c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
436f3213831SJoe Perches 		pr_info("%s: Disabled Multicast RS\n", dev->name);
437c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
438c7dc89c0SFred L. Templin 	}
439b077d7abSYOSHIFUJI Hideaki #endif
440b077d7abSYOSHIFUJI Hideaki 
441372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
44276506a98SJiri Bohac 	ndev->desync_factor = U32_MAX;
4431da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
4441da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4459625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4461da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4471da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4480be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
4499d6280daSJiri Bohac 	} else
4509d6280daSJiri Bohac 		ipv6_regen_rndid(ndev);
4515d9efa7eSDavid S. Miller 
452914faa14SDaniel Borkmann 	ndev->token = in6addr_any;
4531da177e4SLinus Torvalds 
4541f372c7bSMike Manning 	if (netif_running(dev) && addrconf_link_ready(dev))
45553aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
45653aadcc9SHerbert Xu 
4571da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4581da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
459a317a2f1SWANG Cong 	err = addrconf_sysctl_register(ndev);
460a317a2f1SWANG Cong 	if (err) {
461a317a2f1SWANG Cong 		ipv6_mc_destroy_dev(ndev);
4622a189f9eSSabrina Dubroca 		snmp6_unregister_dev(ndev);
463a317a2f1SWANG Cong 		goto err_release;
464a317a2f1SWANG Cong 	}
46530c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
466cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip6_ptr, ndev);
467d88ae4ccSYOSHIFUJI Hideaki 
4682c5e8933SHannes Frederic Sowa 	/* Join interface-local all-node multicast group */
4692c5e8933SHannes Frederic Sowa 	ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4702c5e8933SHannes Frederic Sowa 
471d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
472f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
473d88ae4ccSYOSHIFUJI Hideaki 
474d6ddef9eSLi Wei 	/* Join all-router multicast group if forwarding is set */
4758b2aaedeSLi Wei 	if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
476d6ddef9eSLi Wei 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
477d6ddef9eSLi Wei 
4781da177e4SLinus Torvalds 	return ndev;
479a317a2f1SWANG Cong 
480a317a2f1SWANG Cong err_release:
481a317a2f1SWANG Cong 	neigh_parms_release(&nd_tbl, ndev->nd_parms);
482a317a2f1SWANG Cong 	ndev->dead = 1;
483a317a2f1SWANG Cong 	in6_dev_finish_destroy(ndev);
484a317a2f1SWANG Cong 	return ERR_PTR(err);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
487cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	struct inet6_dev *idev;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	ASSERT_RTNL();
4921da177e4SLinus Torvalds 
493e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
494e21e8467SStephen Hemminger 	if (!idev) {
495e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
496a317a2f1SWANG Cong 		if (IS_ERR(idev))
4971da177e4SLinus Torvalds 			return NULL;
4981da177e4SLinus Torvalds 	}
499c5e33bddSYOSHIFUJI Hideaki 
5001da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
5011da177e4SLinus Torvalds 		ipv6_mc_up(idev);
5021da177e4SLinus Torvalds 	return idev;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
505f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
506f3a1bfb1SNicolas Dichtel {
507f3a1bfb1SNicolas Dichtel 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
508f3a1bfb1SNicolas Dichtel 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
509136ba622SZhang Shengju 	bool all = false;
510f3a1bfb1SNicolas Dichtel 
511136ba622SZhang Shengju 	if (type == NETCONFA_ALL)
512136ba622SZhang Shengju 		all = true;
513136ba622SZhang Shengju 
514136ba622SZhang Shengju 	if (all || type == NETCONFA_FORWARDING)
515f3a1bfb1SNicolas Dichtel 		size += nla_total_size(4);
516b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
517136ba622SZhang Shengju 	if (all || type == NETCONFA_MC_FORWARDING)
518d67b8c61SNicolas Dichtel 		size += nla_total_size(4);
519b1afce95SDavid S. Miller #endif
520136ba622SZhang Shengju 	if (all || type == NETCONFA_PROXY_NEIGH)
521c92d5491Sstephen hemminger 		size += nla_total_size(4);
522f3a1bfb1SNicolas Dichtel 
523136ba622SZhang Shengju 	if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
52435103d11SAndy Gospodarek 		size += nla_total_size(4);
52535103d11SAndy Gospodarek 
526f3a1bfb1SNicolas Dichtel 	return size;
527f3a1bfb1SNicolas Dichtel }
528f3a1bfb1SNicolas Dichtel 
529f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
530f3a1bfb1SNicolas Dichtel 				      struct ipv6_devconf *devconf, u32 portid,
531f3a1bfb1SNicolas Dichtel 				      u32 seq, int event, unsigned int flags,
532f3a1bfb1SNicolas Dichtel 				      int type)
533f3a1bfb1SNicolas Dichtel {
534f3a1bfb1SNicolas Dichtel 	struct nlmsghdr  *nlh;
535f3a1bfb1SNicolas Dichtel 	struct netconfmsg *ncm;
536136ba622SZhang Shengju 	bool all = false;
537f3a1bfb1SNicolas Dichtel 
538f3a1bfb1SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
539f3a1bfb1SNicolas Dichtel 			flags);
54063159f29SIan Morris 	if (!nlh)
541f3a1bfb1SNicolas Dichtel 		return -EMSGSIZE;
542f3a1bfb1SNicolas Dichtel 
543136ba622SZhang Shengju 	if (type == NETCONFA_ALL)
544136ba622SZhang Shengju 		all = true;
545136ba622SZhang Shengju 
546f3a1bfb1SNicolas Dichtel 	ncm = nlmsg_data(nlh);
547f3a1bfb1SNicolas Dichtel 	ncm->ncm_family = AF_INET6;
548f3a1bfb1SNicolas Dichtel 
549f3a1bfb1SNicolas Dichtel 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
550f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
551f3a1bfb1SNicolas Dichtel 
55223452170SDavid Ahern 	if (!devconf)
55323452170SDavid Ahern 		goto out;
55423452170SDavid Ahern 
555136ba622SZhang Shengju 	if ((all || type == NETCONFA_FORWARDING) &&
556f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
557f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
558b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
559136ba622SZhang Shengju 	if ((all || type == NETCONFA_MC_FORWARDING) &&
560d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
561d67b8c61SNicolas Dichtel 			devconf->mc_forwarding) < 0)
562d67b8c61SNicolas Dichtel 		goto nla_put_failure;
563b1afce95SDavid S. Miller #endif
564136ba622SZhang Shengju 	if ((all || type == NETCONFA_PROXY_NEIGH) &&
565c92d5491Sstephen hemminger 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
566c92d5491Sstephen hemminger 		goto nla_put_failure;
567c92d5491Sstephen hemminger 
568136ba622SZhang Shengju 	if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
56935103d11SAndy Gospodarek 	    nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
57035103d11SAndy Gospodarek 			devconf->ignore_routes_with_linkdown) < 0)
57135103d11SAndy Gospodarek 		goto nla_put_failure;
57235103d11SAndy Gospodarek 
57323452170SDavid Ahern out:
574053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
575053c095aSJohannes Berg 	return 0;
576f3a1bfb1SNicolas Dichtel 
577f3a1bfb1SNicolas Dichtel nla_put_failure:
578f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
579f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
580f3a1bfb1SNicolas Dichtel }
581f3a1bfb1SNicolas Dichtel 
58285b3daadSDavid Ahern void inet6_netconf_notify_devconf(struct net *net, int event, int type,
58385b3daadSDavid Ahern 				  int ifindex, struct ipv6_devconf *devconf)
584f3a1bfb1SNicolas Dichtel {
585f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
586f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
587f3a1bfb1SNicolas Dichtel 
588927265bcSEric Dumazet 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
58963159f29SIan Morris 	if (!skb)
590f3a1bfb1SNicolas Dichtel 		goto errout;
591f3a1bfb1SNicolas Dichtel 
592f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
59385b3daadSDavid Ahern 					 event, 0, type);
594f3a1bfb1SNicolas Dichtel 	if (err < 0) {
595f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
596f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
597f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
598f3a1bfb1SNicolas Dichtel 		goto errout;
599f3a1bfb1SNicolas Dichtel 	}
600927265bcSEric Dumazet 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
601f3a1bfb1SNicolas Dichtel 	return;
602f3a1bfb1SNicolas Dichtel errout:
603f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
604f3a1bfb1SNicolas Dichtel }
605f3a1bfb1SNicolas Dichtel 
60676f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
60776f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
60876f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
609c92d5491Sstephen hemminger 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
61035103d11SAndy Gospodarek 	[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]	= { .len = sizeof(int) },
61176f8f6cbSNicolas Dichtel };
61276f8f6cbSNicolas Dichtel 
61376f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
614c21ef3e3SDavid Ahern 				     struct nlmsghdr *nlh,
615c21ef3e3SDavid Ahern 				     struct netlink_ext_ack *extack)
61676f8f6cbSNicolas Dichtel {
61776f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
61876f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
61976f8f6cbSNicolas Dichtel 	struct netconfmsg *ncm;
62076f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
62176f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
62276f8f6cbSNicolas Dichtel 	struct inet6_dev *in6_dev;
62376f8f6cbSNicolas Dichtel 	struct net_device *dev;
62476f8f6cbSNicolas Dichtel 	int ifindex;
62576f8f6cbSNicolas Dichtel 	int err;
62676f8f6cbSNicolas Dichtel 
62776f8f6cbSNicolas Dichtel 	err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
628c21ef3e3SDavid Ahern 			  devconf_ipv6_policy, extack);
62976f8f6cbSNicolas Dichtel 	if (err < 0)
63076f8f6cbSNicolas Dichtel 		goto errout;
63176f8f6cbSNicolas Dichtel 
632a97eb33fSAnton Protopopov 	err = -EINVAL;
63376f8f6cbSNicolas Dichtel 	if (!tb[NETCONFA_IFINDEX])
63476f8f6cbSNicolas Dichtel 		goto errout;
63576f8f6cbSNicolas Dichtel 
63676f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
63776f8f6cbSNicolas Dichtel 	switch (ifindex) {
63876f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
63976f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
64076f8f6cbSNicolas Dichtel 		break;
64176f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
64276f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
64376f8f6cbSNicolas Dichtel 		break;
64476f8f6cbSNicolas Dichtel 	default:
64576f8f6cbSNicolas Dichtel 		dev = __dev_get_by_index(net, ifindex);
64663159f29SIan Morris 		if (!dev)
64776f8f6cbSNicolas Dichtel 			goto errout;
64876f8f6cbSNicolas Dichtel 		in6_dev = __in6_dev_get(dev);
64963159f29SIan Morris 		if (!in6_dev)
65076f8f6cbSNicolas Dichtel 			goto errout;
65176f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
65276f8f6cbSNicolas Dichtel 		break;
65376f8f6cbSNicolas Dichtel 	}
65476f8f6cbSNicolas Dichtel 
65576f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
656136ba622SZhang Shengju 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
65763159f29SIan Morris 	if (!skb)
65876f8f6cbSNicolas Dichtel 		goto errout;
65976f8f6cbSNicolas Dichtel 
66076f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
66176f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
66276f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
663136ba622SZhang Shengju 					 NETCONFA_ALL);
66476f8f6cbSNicolas Dichtel 	if (err < 0) {
66576f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
66676f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
66776f8f6cbSNicolas Dichtel 		kfree_skb(skb);
66876f8f6cbSNicolas Dichtel 		goto errout;
66976f8f6cbSNicolas Dichtel 	}
67076f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
67176f8f6cbSNicolas Dichtel errout:
67276f8f6cbSNicolas Dichtel 	return err;
67376f8f6cbSNicolas Dichtel }
67476f8f6cbSNicolas Dichtel 
6757a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
6767a674200SNicolas Dichtel 				      struct netlink_callback *cb)
6777a674200SNicolas Dichtel {
6787a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
6797a674200SNicolas Dichtel 	int h, s_h;
6807a674200SNicolas Dichtel 	int idx, s_idx;
6817a674200SNicolas Dichtel 	struct net_device *dev;
6827a674200SNicolas Dichtel 	struct inet6_dev *idev;
6837a674200SNicolas Dichtel 	struct hlist_head *head;
6847a674200SNicolas Dichtel 
6857a674200SNicolas Dichtel 	s_h = cb->args[0];
6867a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
6877a674200SNicolas Dichtel 
6887a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6897a674200SNicolas Dichtel 		idx = 0;
6907a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
6917a674200SNicolas Dichtel 		rcu_read_lock();
69263998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
69363998ac2SNicolas Dichtel 			  net->dev_base_seq;
6947a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
6957a674200SNicolas Dichtel 			if (idx < s_idx)
6967a674200SNicolas Dichtel 				goto cont;
6977a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
6987a674200SNicolas Dichtel 			if (!idev)
6997a674200SNicolas Dichtel 				goto cont;
7007a674200SNicolas Dichtel 
7017a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
7027a674200SNicolas Dichtel 						       &idev->cnf,
7037a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
7047a674200SNicolas Dichtel 						       cb->nlh->nlmsg_seq,
7057a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
7067a674200SNicolas Dichtel 						       NLM_F_MULTI,
707136ba622SZhang Shengju 						       NETCONFA_ALL) < 0) {
7087a674200SNicolas Dichtel 				rcu_read_unlock();
7097a674200SNicolas Dichtel 				goto done;
7107a674200SNicolas Dichtel 			}
71163998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7127a674200SNicolas Dichtel cont:
7137a674200SNicolas Dichtel 			idx++;
7147a674200SNicolas Dichtel 		}
7157a674200SNicolas Dichtel 		rcu_read_unlock();
7167a674200SNicolas Dichtel 	}
7177a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
7187a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
7197a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
7207a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
7217a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
7227a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
723136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
7247a674200SNicolas Dichtel 			goto done;
7257a674200SNicolas Dichtel 		else
7267a674200SNicolas Dichtel 			h++;
7277a674200SNicolas Dichtel 	}
7287a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
7297a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
7307a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
7317a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
7327a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
7337a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
734136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
7357a674200SNicolas Dichtel 			goto done;
7367a674200SNicolas Dichtel 		else
7377a674200SNicolas Dichtel 			h++;
7387a674200SNicolas Dichtel 	}
7397a674200SNicolas Dichtel done:
7407a674200SNicolas Dichtel 	cb->args[0] = h;
7417a674200SNicolas Dichtel 	cb->args[1] = idx;
7427a674200SNicolas Dichtel 
7437a674200SNicolas Dichtel 	return skb->len;
7447a674200SNicolas Dichtel }
7457a674200SNicolas Dichtel 
7461da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
7471da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	struct net_device *dev;
7501da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	if (!idev)
7531da177e4SLinus Torvalds 		return;
7541da177e4SLinus Torvalds 	dev = idev->dev;
7550187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
7560187bdfbSBen Hutchings 		dev_disable_lro(dev);
7571a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
7582c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
759f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
7602c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
7612c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
7622c5e8933SHannes Frederic Sowa 		} else {
763f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
7642c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
7652c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
7662c5e8933SHannes Frederic Sowa 		}
7671da177e4SLinus Torvalds 	}
768502a2ffdSstephen hemminger 
769502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
7702c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
7712c12a74cSMichal Wrobel 			continue;
7721da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
7731da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
7741da177e4SLinus Torvalds 		else
7751da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
7761da177e4SLinus Torvalds 	}
77785b3daadSDavid Ahern 	inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
77885b3daadSDavid Ahern 				     NETCONFA_FORWARDING,
779f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
7801da177e4SLinus Torvalds }
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 
783e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	struct net_device *dev;
7861da177e4SLinus Torvalds 	struct inet6_dev *idev;
7871da177e4SLinus Torvalds 
7884acd4945SBen Hutchings 	for_each_netdev(net, dev) {
7891da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
7901da177e4SLinus Torvalds 		if (idev) {
791e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
792e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
7931da177e4SLinus Torvalds 			if (changed)
7941da177e4SLinus Torvalds 				dev_forward_change(idev);
7951da177e4SLinus Torvalds 		}
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds }
798c8fecf22SPavel Emelyanov 
799013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
800c8fecf22SPavel Emelyanov {
801bff16c2fSPavel Emelyanov 	struct net *net;
802013d97e9SFrancesco Ruggeri 	int old;
803013d97e9SFrancesco Ruggeri 
804013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
805013d97e9SFrancesco Ruggeri 		return restart_syscall();
806bff16c2fSPavel Emelyanov 
807bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
808013d97e9SFrancesco Ruggeri 	old = *p;
809013d97e9SFrancesco Ruggeri 	*p = newf;
810c8fecf22SPavel Emelyanov 
811013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
812f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
81385b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
81485b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
815f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
816f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
817013d97e9SFrancesco Ruggeri 		rtnl_unlock();
818013d97e9SFrancesco Ruggeri 		return 0;
81988af182eSEric W. Biederman 	}
820b325fddbSStephen Hemminger 
821e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
822d26c638cSNicolas Dichtel 		int old_dflt = net->ipv6.devconf_dflt->forwarding;
823d26c638cSNicolas Dichtel 
824e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
825d26c638cSNicolas Dichtel 		if ((!newf) ^ (!old_dflt))
82685b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
82785b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
828d26c638cSNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
829d26c638cSNicolas Dichtel 						     net->ipv6.devconf_dflt);
830d26c638cSNicolas Dichtel 
831e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
832f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
83385b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
83485b3daadSDavid Ahern 						     NETCONFA_FORWARDING,
835f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
836f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
837013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
838c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
8390187bdfbSBen Hutchings 	rtnl_unlock();
840c8fecf22SPavel Emelyanov 
841013d97e9SFrancesco Ruggeri 	if (newf)
8427b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
843b325fddbSStephen Hemminger 	return 1;
844c8fecf22SPavel Emelyanov }
84535103d11SAndy Gospodarek 
84635103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf)
84735103d11SAndy Gospodarek {
84835103d11SAndy Gospodarek 	struct net_device *dev;
84935103d11SAndy Gospodarek 	struct inet6_dev *idev;
85035103d11SAndy Gospodarek 
85135103d11SAndy Gospodarek 	for_each_netdev(net, dev) {
85235103d11SAndy Gospodarek 		idev = __in6_dev_get(dev);
85335103d11SAndy Gospodarek 		if (idev) {
85435103d11SAndy Gospodarek 			int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
85535103d11SAndy Gospodarek 
85635103d11SAndy Gospodarek 			idev->cnf.ignore_routes_with_linkdown = newf;
85735103d11SAndy Gospodarek 			if (changed)
85835103d11SAndy Gospodarek 				inet6_netconf_notify_devconf(dev_net(dev),
85985b3daadSDavid Ahern 							     RTM_NEWNETCONF,
86035103d11SAndy Gospodarek 							     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
86135103d11SAndy Gospodarek 							     dev->ifindex,
86235103d11SAndy Gospodarek 							     &idev->cnf);
86335103d11SAndy Gospodarek 		}
86435103d11SAndy Gospodarek 	}
86535103d11SAndy Gospodarek }
86635103d11SAndy Gospodarek 
86735103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
86835103d11SAndy Gospodarek {
86935103d11SAndy Gospodarek 	struct net *net;
87035103d11SAndy Gospodarek 	int old;
87135103d11SAndy Gospodarek 
87235103d11SAndy Gospodarek 	if (!rtnl_trylock())
87335103d11SAndy Gospodarek 		return restart_syscall();
87435103d11SAndy Gospodarek 
87535103d11SAndy Gospodarek 	net = (struct net *)table->extra2;
87635103d11SAndy Gospodarek 	old = *p;
87735103d11SAndy Gospodarek 	*p = newf;
87835103d11SAndy Gospodarek 
87935103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
88035103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
88135103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
88285b3daadSDavid Ahern 						     RTM_NEWNETCONF,
88335103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
88435103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_DEFAULT,
88535103d11SAndy Gospodarek 						     net->ipv6.devconf_dflt);
88635103d11SAndy Gospodarek 		rtnl_unlock();
88735103d11SAndy Gospodarek 		return 0;
88835103d11SAndy Gospodarek 	}
88935103d11SAndy Gospodarek 
89035103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
89135103d11SAndy Gospodarek 		net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
89235103d11SAndy Gospodarek 		addrconf_linkdown_change(net, newf);
89335103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
89435103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
89585b3daadSDavid Ahern 						     RTM_NEWNETCONF,
89635103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
89735103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_ALL,
89835103d11SAndy Gospodarek 						     net->ipv6.devconf_all);
89935103d11SAndy Gospodarek 	}
90035103d11SAndy Gospodarek 	rtnl_unlock();
90135103d11SAndy Gospodarek 
90235103d11SAndy Gospodarek 	return 1;
90335103d11SAndy Gospodarek }
90435103d11SAndy Gospodarek 
9051da177e4SLinus Torvalds #endif
9061da177e4SLinus Torvalds 
9075c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
9081da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
9091da177e4SLinus Torvalds {
910c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
911547b792cSIlpo Järvinen 
9121da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
91391df42beSJoe Perches 	pr_debug("%s\n", __func__);
9141da177e4SLinus Torvalds #endif
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
9171da177e4SLinus Torvalds 
918c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
919c15b1ccaSHannes Frederic Sowa 		pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
920c15b1ccaSHannes Frederic Sowa 			  ifp);
9211da177e4SLinus Torvalds 
922e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
923f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
9241da177e4SLinus Torvalds 		return;
9251da177e4SLinus Torvalds 	}
92694e187c0SAmerigo Wang 	ip6_rt_put(ifp->rt);
9271da177e4SLinus Torvalds 
928e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
9291da177e4SLinus Torvalds }
9301da177e4SLinus Torvalds 
931e55ffac6SBrian Haley static void
932e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
933e55ffac6SBrian Haley {
934502a2ffdSstephen hemminger 	struct list_head *p;
9358a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
936e55ffac6SBrian Haley 
937e55ffac6SBrian Haley 	/*
938e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
939e55ffac6SBrian Haley 	 * global before linklocal.
940e55ffac6SBrian Haley 	 */
941502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
942502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
943502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
9448a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
945e55ffac6SBrian Haley 			break;
946e55ffac6SBrian Haley 	}
947e55ffac6SBrian Haley 
948b54c9b98SDavid S. Miller 	list_add_tail(&ifp->if_list, p);
949e55ffac6SBrian Haley }
950e55ffac6SBrian Haley 
951ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr)
9523eb84f49SYOSHIFUJI Hideaki {
953ddbe5032SEric Dumazet 	return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
9543eb84f49SYOSHIFUJI Hideaki }
9553eb84f49SYOSHIFUJI Hideaki 
9561da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds static struct inet6_ifaddr *
9593f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
9603f8f5298SJiri Pirko 	      const struct in6_addr *peer_addr, int pfxlen,
9618a226b2cSJiri Benc 	      int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
9621da177e4SLinus Torvalds {
963df789fe7SDavid Forster 	struct net *net = dev_net(idev->dev);
9641da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
9651da177e4SLinus Torvalds 	struct rt6_info *rt;
9663ad7d246SKrister Johansen 	struct in6_validator_info i6vi;
9673a88a81dSstephen hemminger 	unsigned int hash;
9681da177e4SLinus Torvalds 	int err = 0;
969d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
970d68b8270SYOSHIFUJI Hideaki 
971d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
972d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
973d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
974d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
975d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
9761da177e4SLinus Torvalds 
9778814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
9783ad7d246SKrister Johansen 
9793ad7d246SKrister Johansen 	in6_dev_hold(idev);
9803ad7d246SKrister Johansen 
9811da177e4SLinus Torvalds 	if (idev->dead) {
9821da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
9831da177e4SLinus Torvalds 		goto out2;
9841da177e4SLinus Torvalds 	}
9851da177e4SLinus Torvalds 
98656d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
9879bdd8d40SBrian Haley 		err = -EACCES;
9889bdd8d40SBrian Haley 		goto out2;
9899bdd8d40SBrian Haley 	}
9909bdd8d40SBrian Haley 
9913ad7d246SKrister Johansen 	i6vi.i6vi_addr = *addr;
9923ad7d246SKrister Johansen 	i6vi.i6vi_dev = idev;
9933ad7d246SKrister Johansen 	rcu_read_unlock_bh();
9943ad7d246SKrister Johansen 
9953ad7d246SKrister Johansen 	err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
9963ad7d246SKrister Johansen 
9973ad7d246SKrister Johansen 	rcu_read_lock_bh();
9983ad7d246SKrister Johansen 	err = notifier_to_errno(err);
9993ad7d246SKrister Johansen 	if (err)
10003ad7d246SKrister Johansen 		goto out2;
10013ad7d246SKrister Johansen 
10025c578aedSstephen hemminger 	spin_lock(&addrconf_hash_lock);
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
1005c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
1006ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: already assigned\n");
10071da177e4SLinus Torvalds 		err = -EEXIST;
10081da177e4SLinus Torvalds 		goto out;
10091da177e4SLinus Torvalds 	}
10101da177e4SLinus Torvalds 
1011322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
10121da177e4SLinus Torvalds 
101363159f29SIan Morris 	if (!ifa) {
1014ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: malloc failed\n");
10151da177e4SLinus Torvalds 		err = -ENOBUFS;
10161da177e4SLinus Torvalds 		goto out;
10171da177e4SLinus Torvalds 	}
10181da177e4SLinus Torvalds 
10198f031519SDavid S. Miller 	rt = addrconf_dst_alloc(idev, addr, false);
10201da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
10211da177e4SLinus Torvalds 		err = PTR_ERR(rt);
10221da177e4SLinus Torvalds 		goto out;
10231da177e4SLinus Torvalds 	}
10241da177e4SLinus Torvalds 
1025df789fe7SDavid Forster 	if (net->ipv6.devconf_all->disable_policy ||
1026df789fe7SDavid Forster 	    idev->cnf.disable_policy)
1027df789fe7SDavid Forster 		rt->dst.flags |= DST_NOPOLICY;
1028df789fe7SDavid Forster 
1029bba24896SJiri Pirko 	neigh_parms_data_state_setall(idev->nd_parms);
1030bba24896SJiri Pirko 
10314e3fd7a0SAlexey Dobriyan 	ifa->addr = *addr;
10323f8f5298SJiri Pirko 	if (peer_addr)
10333f8f5298SJiri Pirko 		ifa->peer_addr = *peer_addr;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
1036c15b1ccaSHannes Frederic Sowa 	INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1037c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
10381da177e4SLinus Torvalds 	ifa->scope = scope;
10391da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
104066eb9f86SMahesh Bandewar 	ifa->flags = flags;
104166eb9f86SMahesh Bandewar 	/* No need to add the TENTATIVE flag for addresses with NODAD */
104266eb9f86SMahesh Bandewar 	if (!(flags & IFA_F_NODAD))
104366eb9f86SMahesh Bandewar 		ifa->flags |= IFA_F_TENTATIVE;
10448a226b2cSJiri Benc 	ifa->valid_lft = valid_lft;
10458a226b2cSJiri Benc 	ifa->prefered_lft = prefered_lft;
10461da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
1047617fe29dSDaniel Borkmann 	ifa->tokenized = false;
10481da177e4SLinus Torvalds 
104957f5f544SKeir Fraser 	ifa->rt = rt;
105057f5f544SKeir Fraser 
10511da177e4SLinus Torvalds 	ifa->idev = idev;
10521da177e4SLinus Torvalds 	/* For caller */
1053271201c0SReshetova, Elena 	refcount_set(&ifa->refcnt, 1);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	/* Add to big hash table */
1056ddbe5032SEric Dumazet 	hash = inet6_addr_hash(addr);
10571da177e4SLinus Torvalds 
10585c578aedSstephen hemminger 	hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
10595c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	write_lock(&idev->lock);
10621da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
1063e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
1066372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
10671da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
10681da177e4SLinus Torvalds 	}
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
10711da177e4SLinus Torvalds 	write_unlock(&idev->lock);
10721da177e4SLinus Torvalds out2:
10738814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
10741da177e4SLinus Torvalds 
1075fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
1076f88c91ddSCong Wang 		inet6addr_notifier_call_chain(NETDEV_UP, ifa);
10771da177e4SLinus Torvalds 	else {
10781da177e4SLinus Torvalds 		kfree(ifa);
10793ad7d246SKrister Johansen 		in6_dev_put(idev);
10801da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
10811da177e4SLinus Torvalds 	}
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	return ifa;
10841da177e4SLinus Torvalds out:
10855c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
10861da177e4SLinus Torvalds 	goto out2;
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
10895b84efecSThomas Haller enum cleanup_prefix_rt_t {
10905b84efecSThomas Haller 	CLEANUP_PREFIX_RT_NOP,    /* no cleanup action for prefix route */
10915b84efecSThomas Haller 	CLEANUP_PREFIX_RT_DEL,    /* delete the prefix route */
10925b84efecSThomas Haller 	CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
10935b84efecSThomas Haller };
10945b84efecSThomas Haller 
10955b84efecSThomas Haller /*
10965b84efecSThomas Haller  * Check, whether the prefix for ifp would still need a prefix route
10975b84efecSThomas Haller  * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
10985b84efecSThomas Haller  * constants.
10995b84efecSThomas Haller  *
11005b84efecSThomas Haller  * 1) we don't purge prefix if address was not permanent.
11015b84efecSThomas Haller  *    prefix is managed by its own lifetime.
11025b84efecSThomas Haller  * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
11035b84efecSThomas Haller  * 3) if there are no addresses, delete prefix.
11045b84efecSThomas Haller  * 4) if there are still other permanent address(es),
11055b84efecSThomas Haller  *    corresponding prefix is still permanent.
11065b84efecSThomas Haller  * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
11075b84efecSThomas Haller  *    don't purge the prefix, assume user space is managing it.
11085b84efecSThomas Haller  * 6) otherwise, update prefix lifetime to the
11095b84efecSThomas Haller  *    longest valid lifetime among the corresponding
11105b84efecSThomas Haller  *    addresses on the device.
11115b84efecSThomas Haller  *    Note: subsequent RA will update lifetime.
11125b84efecSThomas Haller  **/
11135b84efecSThomas Haller static enum cleanup_prefix_rt_t
11145b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
11155b84efecSThomas Haller {
11165b84efecSThomas Haller 	struct inet6_ifaddr *ifa;
11175b84efecSThomas Haller 	struct inet6_dev *idev = ifp->idev;
11185b84efecSThomas Haller 	unsigned long lifetime;
11195b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
11205b84efecSThomas Haller 
11215b84efecSThomas Haller 	*expires = jiffies;
11225b84efecSThomas Haller 
11235b84efecSThomas Haller 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
11245b84efecSThomas Haller 		if (ifa == ifp)
11255b84efecSThomas Haller 			continue;
11265b84efecSThomas Haller 		if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
11275b84efecSThomas Haller 				       ifp->prefix_len))
11285b84efecSThomas Haller 			continue;
11295b84efecSThomas Haller 		if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
11305b84efecSThomas Haller 			return CLEANUP_PREFIX_RT_NOP;
11315b84efecSThomas Haller 
11325b84efecSThomas Haller 		action = CLEANUP_PREFIX_RT_EXPIRE;
11335b84efecSThomas Haller 
11345b84efecSThomas Haller 		spin_lock(&ifa->lock);
11355b84efecSThomas Haller 
11365b84efecSThomas Haller 		lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
11375b84efecSThomas Haller 		/*
11385b84efecSThomas Haller 		 * Note: Because this address is
11395b84efecSThomas Haller 		 * not permanent, lifetime <
11405b84efecSThomas Haller 		 * LONG_MAX / HZ here.
11415b84efecSThomas Haller 		 */
11425b84efecSThomas Haller 		if (time_before(*expires, ifa->tstamp + lifetime * HZ))
11435b84efecSThomas Haller 			*expires = ifa->tstamp + lifetime * HZ;
11445b84efecSThomas Haller 		spin_unlock(&ifa->lock);
11455b84efecSThomas Haller 	}
11465b84efecSThomas Haller 
11475b84efecSThomas Haller 	return action;
11485b84efecSThomas Haller }
11495b84efecSThomas Haller 
11505b84efecSThomas Haller static void
11515b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
11525b84efecSThomas Haller {
11535b84efecSThomas Haller 	struct rt6_info *rt;
11545b84efecSThomas Haller 
11555b84efecSThomas Haller 	rt = addrconf_get_prefix_route(&ifp->addr,
11565b84efecSThomas Haller 				       ifp->prefix_len,
11575b84efecSThomas Haller 				       ifp->idev->dev,
11585b84efecSThomas Haller 				       0, RTF_GATEWAY | RTF_DEFAULT);
11595b84efecSThomas Haller 	if (rt) {
11605b84efecSThomas Haller 		if (del_rt)
11615b84efecSThomas Haller 			ip6_del_rt(rt);
11625b84efecSThomas Haller 		else {
11635b84efecSThomas Haller 			if (!(rt->rt6i_flags & RTF_EXPIRES))
11645b84efecSThomas Haller 				rt6_set_expires(rt, expires);
11655b84efecSThomas Haller 			ip6_rt_put(rt);
11665b84efecSThomas Haller 		}
11675b84efecSThomas Haller 	}
11685b84efecSThomas Haller }
11695b84efecSThomas Haller 
11705b84efecSThomas Haller 
11711da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
11741da177e4SLinus Torvalds {
11754c5ff6a6SHerbert Xu 	int state;
11765b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
11775b84efecSThomas Haller 	unsigned long expires;
11781da177e4SLinus Torvalds 
1179c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
1180c15b1ccaSHannes Frederic Sowa 
11818e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
11824c5ff6a6SHerbert Xu 	state = ifp->state;
1183e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
11848e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
11854c5ff6a6SHerbert Xu 
11864c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
11874c5ff6a6SHerbert Xu 		goto out;
11881da177e4SLinus Torvalds 
11895c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
11905c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
11915c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
11921da177e4SLinus Torvalds 
11935b84efecSThomas Haller 	write_lock_bh(&ifp->idev->lock);
11945d9efa7eSDavid S. Miller 
11951da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
1196372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
11971da177e4SLinus Torvalds 		if (ifp->ifpub) {
11981da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
11991da177e4SLinus Torvalds 			ifp->ifpub = NULL;
12001da177e4SLinus Torvalds 		}
12011da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
12021da177e4SLinus Torvalds 	}
12031da177e4SLinus Torvalds 
12045b84efecSThomas Haller 	if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
12055b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &expires);
12065b84efecSThomas Haller 
1207502a2ffdSstephen hemminger 	list_del_init(&ifp->if_list);
12081da177e4SLinus Torvalds 	__in6_ifa_put(ifp);
1209502a2ffdSstephen hemminger 
12105b84efecSThomas Haller 	write_unlock_bh(&ifp->idev->lock);
12111da177e4SLinus Torvalds 
1212c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
1213b2238566SAndrey Vagin 
12141da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
12151da177e4SLinus Torvalds 
1216f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
12171da177e4SLinus Torvalds 
12185b84efecSThomas Haller 	if (action != CLEANUP_PREFIX_RT_NOP) {
12195b84efecSThomas Haller 		cleanup_prefix_route(ifp, expires,
12205b84efecSThomas Haller 			action == CLEANUP_PREFIX_RT_DEL);
12211da177e4SLinus Torvalds 	}
12221da177e4SLinus Torvalds 
1223c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1224c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
12254c5ff6a6SHerbert Xu out:
12261da177e4SLinus Torvalds 	in6_ifa_put(ifp);
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
12301da177e4SLinus Torvalds {
12311da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
12321da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
123376f793e3SLorenzo Colitti 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1234eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
12351da177e4SLinus Torvalds 	int tmp_plen;
12361da177e4SLinus Torvalds 	int ret = 0;
123795c385b4SNeil Horman 	u32 addr_flags;
123876f793e3SLorenzo Colitti 	unsigned long now = jiffies;
123976506a98SJiri Bohac 	long max_desync_factor;
12407aa8e63fSJiri Bohac 	s32 cnf_temp_preferred_lft;
12411da177e4SLinus Torvalds 
124253bd6749SJiri Pirko 	write_lock_bh(&idev->lock);
12431da177e4SLinus Torvalds 	if (ift) {
12441da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
12451da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
12461da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
12471da177e4SLinus Torvalds 		tmpaddr = &addr;
12481da177e4SLinus Torvalds 	} else {
12491da177e4SLinus Torvalds 		tmpaddr = NULL;
12501da177e4SLinus Torvalds 	}
12511da177e4SLinus Torvalds retry:
12521da177e4SLinus Torvalds 	in6_dev_hold(idev);
12531da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
125453bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1255f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
12561da177e4SLinus Torvalds 		in6_dev_put(idev);
12571da177e4SLinus Torvalds 		ret = -1;
12581da177e4SLinus Torvalds 		goto out;
12591da177e4SLinus Torvalds 	}
12601da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
12611da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
12621da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
12631da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
126453bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1265f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1266f3213831SJoe Perches 			__func__);
12671da177e4SLinus Torvalds 		in6_dev_put(idev);
12681da177e4SLinus Torvalds 		ret = -1;
12691da177e4SLinus Torvalds 		goto out;
12701da177e4SLinus Torvalds 	}
12711da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
12721da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
12739d6280daSJiri Bohac 	ipv6_try_regen_rndid(idev, tmpaddr);
12741da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
127576f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
12761da177e4SLinus Torvalds 
1277eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1278eac55bf9SBenoit Boissinot 			idev->cnf.dad_transmits *
12791f9248e5SJiri Pirko 			NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
128076506a98SJiri Bohac 
128176506a98SJiri Bohac 	/* recalculate max_desync_factor each time and update
128276506a98SJiri Bohac 	 * idev->desync_factor if it's larger
128376506a98SJiri Bohac 	 */
12847aa8e63fSJiri Bohac 	cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
128576506a98SJiri Bohac 	max_desync_factor = min_t(__u32,
128676506a98SJiri Bohac 				  idev->cnf.max_desync_factor,
12877aa8e63fSJiri Bohac 				  cnf_temp_preferred_lft - regen_advance);
128876506a98SJiri Bohac 
128976506a98SJiri Bohac 	if (unlikely(idev->desync_factor > max_desync_factor)) {
129076506a98SJiri Bohac 		if (max_desync_factor > 0) {
129176506a98SJiri Bohac 			get_random_bytes(&idev->desync_factor,
129276506a98SJiri Bohac 					 sizeof(idev->desync_factor));
129376506a98SJiri Bohac 			idev->desync_factor %= max_desync_factor;
129476506a98SJiri Bohac 		} else {
129576506a98SJiri Bohac 			idev->desync_factor = 0;
129676506a98SJiri Bohac 		}
129776506a98SJiri Bohac 	}
129876506a98SJiri Bohac 
129976506a98SJiri Bohac 	tmp_valid_lft = min_t(__u32,
130076506a98SJiri Bohac 			      ifp->valid_lft,
130176506a98SJiri Bohac 			      idev->cnf.temp_valid_lft + age);
13027aa8e63fSJiri Bohac 	tmp_prefered_lft = cnf_temp_preferred_lft + age -
130376506a98SJiri Bohac 			    idev->desync_factor;
130476506a98SJiri Bohac 	tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
130576506a98SJiri Bohac 	tmp_plen = ifp->prefix_len;
130676506a98SJiri Bohac 	tmp_tstamp = ifp->tstamp;
130776506a98SJiri Bohac 	spin_unlock_bh(&ifp->lock);
130876506a98SJiri Bohac 
130953bd6749SJiri Pirko 	write_unlock_bh(&idev->lock);
131095c385b4SNeil Horman 
1311eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1312eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1313eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1314eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1315ecab6701SHeiner Kallweit 	 * Use age calculation as in addrconf_verify to avoid unnecessary
1316ecab6701SHeiner Kallweit 	 * temporary addresses being generated.
1317eac55bf9SBenoit Boissinot 	 */
1318ecab6701SHeiner Kallweit 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1319ecab6701SHeiner Kallweit 	if (tmp_prefered_lft <= regen_advance + age) {
1320eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1321eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1322eac55bf9SBenoit Boissinot 		ret = -1;
1323eac55bf9SBenoit Boissinot 		goto out;
1324eac55bf9SBenoit Boissinot 	}
1325eac55bf9SBenoit Boissinot 
132695c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
132795c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
132895c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
132995c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
133095c385b4SNeil Horman 
13314b08a8f1SHannes Frederic Sowa 	ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
13328a226b2cSJiri Benc 			    ipv6_addr_scope(&addr), addr_flags,
13334b08a8f1SHannes Frederic Sowa 			    tmp_valid_lft, tmp_prefered_lft);
13344b08a8f1SHannes Frederic Sowa 	if (IS_ERR(ift)) {
13351da177e4SLinus Torvalds 		in6_ifa_put(ifp);
13361da177e4SLinus Torvalds 		in6_dev_put(idev);
1337f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
13381da177e4SLinus Torvalds 		tmpaddr = &addr;
133953bd6749SJiri Pirko 		write_lock_bh(&idev->lock);
13401da177e4SLinus Torvalds 		goto retry;
13411da177e4SLinus Torvalds 	}
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
13441da177e4SLinus Torvalds 	ift->ifpub = ifp;
134576f793e3SLorenzo Colitti 	ift->cstamp = now;
13461da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
13471da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
13481da177e4SLinus Torvalds 
1349cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
13501da177e4SLinus Torvalds 	in6_ifa_put(ift);
13511da177e4SLinus Torvalds 	in6_dev_put(idev);
13521da177e4SLinus Torvalds out:
13531da177e4SLinus Torvalds 	return ret;
13541da177e4SLinus Torvalds }
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds /*
1357072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
13581da177e4SLinus Torvalds  */
1359a9b05723SYOSHIFUJI Hideaki enum {
1360a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1361a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1362a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1363a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1364a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1365a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1366a9b05723SYOSHIFUJI Hideaki #endif
1367a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1368a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1369a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1370a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1371a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
13727fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
13737fd2561eSErik Kline 	IPV6_SADDR_RULE_NOT_OPTIMISTIC,
13747fd2561eSErik Kline #endif
1375a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1376072047e4SYOSHIFUJI Hideaki };
1377072047e4SYOSHIFUJI Hideaki 
1378a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1379a9b05723SYOSHIFUJI Hideaki 	int			rule;
1380a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1381a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1382a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1383a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1384a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1385a9b05723SYOSHIFUJI Hideaki };
1386a9b05723SYOSHIFUJI Hideaki 
1387a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
13889acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1389a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1390a9b05723SYOSHIFUJI Hideaki 	int scope;
1391a9b05723SYOSHIFUJI Hideaki 	int label;
13927cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1393a9b05723SYOSHIFUJI Hideaki };
1394072047e4SYOSHIFUJI Hideaki 
1395b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
13961da177e4SLinus Torvalds {
139745bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1398072047e4SYOSHIFUJI Hideaki 		return 1;
1399072047e4SYOSHIFUJI Hideaki 	return 0;
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
140235e015e1SMatteo Croce static bool ipv6_use_optimistic_addr(struct net *net,
140335e015e1SMatteo Croce 				     struct inet6_dev *idev)
14047fd2561eSErik Kline {
14057fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
140635e015e1SMatteo Croce 	if (!idev)
140735e015e1SMatteo Croce 		return false;
140835e015e1SMatteo Croce 	if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
140935e015e1SMatteo Croce 		return false;
141035e015e1SMatteo Croce 	if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
141135e015e1SMatteo Croce 		return false;
141235e015e1SMatteo Croce 
141335e015e1SMatteo Croce 	return true;
14147fd2561eSErik Kline #else
14157fd2561eSErik Kline 	return false;
14167fd2561eSErik Kline #endif
14177fd2561eSErik Kline }
14187fd2561eSErik Kline 
14193de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
14203de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1421a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1422a9b05723SYOSHIFUJI Hideaki 			       int i)
1423a9b05723SYOSHIFUJI Hideaki {
1424a9b05723SYOSHIFUJI Hideaki 	int ret;
1425a9b05723SYOSHIFUJI Hideaki 
1426a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1427a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1428a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1429a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1430a9b05723SYOSHIFUJI Hideaki 			break;
1431a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1432a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1433a9b05723SYOSHIFUJI Hideaki 			break;
1434a9b05723SYOSHIFUJI Hideaki 		default:
1435a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1436a9b05723SYOSHIFUJI Hideaki 		}
1437a9b05723SYOSHIFUJI Hideaki 		goto out;
1438a9b05723SYOSHIFUJI Hideaki 	}
1439a9b05723SYOSHIFUJI Hideaki 
1440a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1441a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1442a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1443a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1444a9b05723SYOSHIFUJI Hideaki 		break;
1445a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1446a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1447a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1448a9b05723SYOSHIFUJI Hideaki 		break;
1449a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1450a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1451a9b05723SYOSHIFUJI Hideaki 		 *
1452a9b05723SYOSHIFUJI Hideaki 		 *      ret
1453a9b05723SYOSHIFUJI Hideaki 		 *       ^
1454a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1455a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1456a9b05723SYOSHIFUJI Hideaki 		 *       |
1457a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1458a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1459a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1460db9c7c39Sstephen hemminger 		 *  B-15 |    \        if scope is enough for destination.
1461a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1462a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1463a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1464a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1465a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1466a9b05723SYOSHIFUJI Hideaki 		 *
1467a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1468a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1469a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1470a9b05723SYOSHIFUJI Hideaki 		 */
1471a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1472a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1473a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1474a9b05723SYOSHIFUJI Hideaki 		else
1475a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1476a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1477a9b05723SYOSHIFUJI Hideaki 		break;
1478a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
14797fd2561eSErik Kline 	    {
1480a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
14817fd2561eSErik Kline 		u8 avoid = IFA_F_DEPRECATED;
14827fd2561eSErik Kline 
148335e015e1SMatteo Croce 		if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
14847fd2561eSErik Kline 			avoid |= IFA_F_OPTIMISTIC;
1485a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
14867fd2561eSErik Kline 		      !(score->ifa->flags & avoid);
1487a9b05723SYOSHIFUJI Hideaki 		break;
14887fd2561eSErik Kline 	    }
1489a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1490a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
14917cbca67cSYOSHIFUJI Hideaki 	    {
1492a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
14937cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
14947cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1495a9b05723SYOSHIFUJI Hideaki 		break;
14967cbca67cSYOSHIFUJI Hideaki 	    }
1497a9b05723SYOSHIFUJI Hideaki #endif
1498a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1499a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1500a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1501a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1502a9b05723SYOSHIFUJI Hideaki 		break;
1503a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1504a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
15053de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
15063de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1507a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1508a9b05723SYOSHIFUJI Hideaki 		break;
1509a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
15107cbca67cSYOSHIFUJI Hideaki 	    {
1511a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
151225985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1513a9b05723SYOSHIFUJI Hideaki 		 */
15147cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
15157cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
15167cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
15177cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1518a9b05723SYOSHIFUJI Hideaki 		break;
15197cbca67cSYOSHIFUJI Hideaki 	    }
1520a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1521a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1522a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1523a9b05723SYOSHIFUJI Hideaki 		 */
1524a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1525a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1526a9b05723SYOSHIFUJI Hideaki 		break;
1527a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1528a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
152991b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
153091b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
153191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
153291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1533a9b05723SYOSHIFUJI Hideaki 		break;
15347fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
15357fd2561eSErik Kline 	case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
15367fd2561eSErik Kline 		/* Optimistic addresses still have lower precedence than other
15377fd2561eSErik Kline 		 * preferred addresses.
15387fd2561eSErik Kline 		 */
15397fd2561eSErik Kline 		ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
15407fd2561eSErik Kline 		break;
15417fd2561eSErik Kline #endif
1542a9b05723SYOSHIFUJI Hideaki 	default:
1543a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1544a9b05723SYOSHIFUJI Hideaki 	}
1545a9b05723SYOSHIFUJI Hideaki 
1546a9b05723SYOSHIFUJI Hideaki 	if (ret)
1547a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1548a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1549a9b05723SYOSHIFUJI Hideaki out:
1550a9b05723SYOSHIFUJI Hideaki 	return ret;
1551a9b05723SYOSHIFUJI Hideaki }
1552a9b05723SYOSHIFUJI Hideaki 
1553c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net,
15549131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_dst *dst,
15559131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct inet6_dev *idev,
1556c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_score *scores,
1557c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				int hiscore_idx)
15581da177e4SLinus Torvalds {
1559c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1560072047e4SYOSHIFUJI Hideaki 
15611da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1562502a2ffdSstephen hemminger 	list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1563a9b05723SYOSHIFUJI Hideaki 		int i;
1564072047e4SYOSHIFUJI Hideaki 
1565a9b05723SYOSHIFUJI Hideaki 		/*
15666b3ae80aSYOSHIFUJI Hideaki 		 * - Tentative Address (RFC2462 section 5.4)
15676b3ae80aSYOSHIFUJI Hideaki 		 *  - A tentative address is not considered
15686b3ae80aSYOSHIFUJI Hideaki 		 *    "assigned to an interface" in the traditional
156995c385b4SNeil Horman 		 *    sense, unless it is also flagged as optimistic.
15706b3ae80aSYOSHIFUJI Hideaki 		 * - Candidate Source Address (section 4)
1571072047e4SYOSHIFUJI Hideaki 		 *  - In any case, anycast addresses, multicast
1572072047e4SYOSHIFUJI Hideaki 		 *    addresses, and the unspecified address MUST
1573072047e4SYOSHIFUJI Hideaki 		 *    NOT be included in a candidate set.
1574072047e4SYOSHIFUJI Hideaki 		 */
1575a9b05723SYOSHIFUJI Hideaki 		if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1576a9b05723SYOSHIFUJI Hideaki 		    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
15776b3ae80aSYOSHIFUJI Hideaki 			continue;
1578a9b05723SYOSHIFUJI Hideaki 
1579a9b05723SYOSHIFUJI Hideaki 		score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1580a9b05723SYOSHIFUJI Hideaki 
1581a9b05723SYOSHIFUJI Hideaki 		if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1582a9b05723SYOSHIFUJI Hideaki 			     score->addr_type & IPV6_ADDR_MULTICAST)) {
1583ba7a46f1SJoe Perches 			net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
15849131f3deSYOSHIFUJI Hideaki/吉藤英明 					    idev->dev->name);
15851da177e4SLinus Torvalds 			continue;
1586072047e4SYOSHIFUJI Hideaki 		}
1587072047e4SYOSHIFUJI Hideaki 
1588a9b05723SYOSHIFUJI Hideaki 		score->rule = -1;
1589a9b05723SYOSHIFUJI Hideaki 		bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1590072047e4SYOSHIFUJI Hideaki 
1591a9b05723SYOSHIFUJI Hideaki 		for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1592a9b05723SYOSHIFUJI Hideaki 			int minihiscore, miniscore;
1593072047e4SYOSHIFUJI Hideaki 
15949131f3deSYOSHIFUJI Hideaki/吉藤英明 			minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
15959131f3deSYOSHIFUJI Hideaki/吉藤英明 			miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1596072047e4SYOSHIFUJI Hideaki 
1597a9b05723SYOSHIFUJI Hideaki 			if (minihiscore > miniscore) {
1598a9b05723SYOSHIFUJI Hideaki 				if (i == IPV6_SADDR_RULE_SCOPE &&
1599a9b05723SYOSHIFUJI Hideaki 				    score->scopedist > 0) {
1600a9b05723SYOSHIFUJI Hideaki 					/*
1601a9b05723SYOSHIFUJI Hideaki 					 * special case:
1602a9b05723SYOSHIFUJI Hideaki 					 * each remaining entry
1603a9b05723SYOSHIFUJI Hideaki 					 * has too small (not enough)
1604a9b05723SYOSHIFUJI Hideaki 					 * scope, because ifa entries
1605a9b05723SYOSHIFUJI Hideaki 					 * are sorted by their scope
1606a9b05723SYOSHIFUJI Hideaki 					 * values.
1607072047e4SYOSHIFUJI Hideaki 					 */
16089131f3deSYOSHIFUJI Hideaki/吉藤英明 					goto out;
1609072047e4SYOSHIFUJI Hideaki 				}
1610a9b05723SYOSHIFUJI Hideaki 				break;
1611a9b05723SYOSHIFUJI Hideaki 			} else if (minihiscore < miniscore) {
1612a9b05723SYOSHIFUJI Hideaki 				if (hiscore->ifa)
1613a9b05723SYOSHIFUJI Hideaki 					in6_ifa_put(hiscore->ifa);
161499cd07a5SJuha-Matti Tapio 
1615a9b05723SYOSHIFUJI Hideaki 				in6_ifa_hold(score->ifa);
1616072047e4SYOSHIFUJI Hideaki 
1617a0bffffcSIlpo Järvinen 				swap(hiscore, score);
1618c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				hiscore_idx = 1 - hiscore_idx;
1619a9b05723SYOSHIFUJI Hideaki 
1620a9b05723SYOSHIFUJI Hideaki 				/* restore our iterator */
1621a9b05723SYOSHIFUJI Hideaki 				score->ifa = hiscore->ifa;
1622a9b05723SYOSHIFUJI Hideaki 
1623a9b05723SYOSHIFUJI Hideaki 				break;
16241da177e4SLinus Torvalds 			}
1625a9b05723SYOSHIFUJI Hideaki 		}
1626a9b05723SYOSHIFUJI Hideaki 	}
16279131f3deSYOSHIFUJI Hideaki/吉藤英明 out:
16281da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
1629c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	return hiscore_idx;
16301da177e4SLinus Torvalds }
16319131f3deSYOSHIFUJI Hideaki/吉藤英明 
1632afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net,
1633afbac601SDavid Ahern 				 const struct net_device *dst_dev,
1634afbac601SDavid Ahern 				 const struct net_device *master,
1635afbac601SDavid Ahern 				 struct ipv6_saddr_dst *dst,
1636afbac601SDavid Ahern 				 struct ipv6_saddr_score *scores,
1637afbac601SDavid Ahern 				 int hiscore_idx)
1638afbac601SDavid Ahern {
1639afbac601SDavid Ahern 	struct inet6_dev *idev;
1640afbac601SDavid Ahern 
1641afbac601SDavid Ahern 	idev = __in6_dev_get(dst_dev);
1642afbac601SDavid Ahern 	if (idev)
1643afbac601SDavid Ahern 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1644afbac601SDavid Ahern 						   scores, hiscore_idx);
1645afbac601SDavid Ahern 
1646afbac601SDavid Ahern 	idev = __in6_dev_get(master);
1647afbac601SDavid Ahern 	if (idev)
1648afbac601SDavid Ahern 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1649afbac601SDavid Ahern 						   scores, hiscore_idx);
1650afbac601SDavid Ahern 
1651afbac601SDavid Ahern 	return hiscore_idx;
1652afbac601SDavid Ahern }
1653afbac601SDavid Ahern 
16549131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
16559131f3deSYOSHIFUJI Hideaki/吉藤英明 		       const struct in6_addr *daddr, unsigned int prefs,
16569131f3deSYOSHIFUJI Hideaki/吉藤英明 		       struct in6_addr *saddr)
16579131f3deSYOSHIFUJI Hideaki/吉藤英明 {
1658c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score scores[2], *hiscore;
16599131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_dst dst;
16609131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct inet6_dev *idev;
16619131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct net_device *dev;
16629131f3deSYOSHIFUJI Hideaki/吉藤英明 	int dst_type;
16639131f3deSYOSHIFUJI Hideaki/吉藤英明 	bool use_oif_addr = false;
1664c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	int hiscore_idx = 0;
16659131f3deSYOSHIFUJI Hideaki/吉藤英明 
16669131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst_type = __ipv6_addr_type(daddr);
16679131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.addr = daddr;
16689131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
16699131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.scope = __ipv6_addr_src_scope(dst_type);
16709131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
16719131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.prefs = prefs;
16729131f3deSYOSHIFUJI Hideaki/吉藤英明 
1673c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].rule = -1;
1674c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].ifa = NULL;
16759131f3deSYOSHIFUJI Hideaki/吉藤英明 
16769131f3deSYOSHIFUJI Hideaki/吉藤英明 	rcu_read_lock();
16779131f3deSYOSHIFUJI Hideaki/吉藤英明 
16789131f3deSYOSHIFUJI Hideaki/吉藤英明 	/* Candidate Source Address (section 4)
16799131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *  - multicast and link-local destination address,
16809131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    the set of candidate source address MUST only
16819131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
16829131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same link as the outgoing
16839131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.
16849131f3deSYOSHIFUJI Hideaki/吉藤英明 	 * (- For site-local destination addresses, the
16859131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    set of candidate source addresses MUST only
16869131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
16879131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same site as the outgoing
16889131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.)
16893985e8a3SErik Kline 	 *  - "It is RECOMMENDED that the candidate source addresses
16903985e8a3SErik Kline 	 *    be the set of unicast addresses assigned to the
16913985e8a3SErik Kline 	 *    interface that will be used to send to the destination
16923985e8a3SErik Kline 	 *    (the 'outgoing' interface)." (RFC 6724)
16939131f3deSYOSHIFUJI Hideaki/吉藤英明 	 */
16949131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (dst_dev) {
16959131f3deSYOSHIFUJI Hideaki/吉藤英明 		idev = __in6_dev_get(dst_dev);
16963985e8a3SErik Kline 		if ((dst_type & IPV6_ADDR_MULTICAST) ||
16973985e8a3SErik Kline 		    dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
16983985e8a3SErik Kline 		    (idev && idev->cnf.use_oif_addrs_only)) {
16999131f3deSYOSHIFUJI Hideaki/吉藤英明 			use_oif_addr = true;
17009131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
17019131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
17029131f3deSYOSHIFUJI Hideaki/吉藤英明 
17039131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (use_oif_addr) {
1704c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 		if (idev)
1705c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
17069131f3deSYOSHIFUJI Hideaki/吉藤英明 	} else {
1707afbac601SDavid Ahern 		const struct net_device *master;
1708afbac601SDavid Ahern 		int master_idx = 0;
1709afbac601SDavid Ahern 
1710afbac601SDavid Ahern 		/* if dst_dev exists and is enslaved to an L3 device, then
1711afbac601SDavid Ahern 		 * prefer addresses from dst_dev and then the master over
1712afbac601SDavid Ahern 		 * any other enslaved devices in the L3 domain.
1713afbac601SDavid Ahern 		 */
1714afbac601SDavid Ahern 		master = l3mdev_master_dev_rcu(dst_dev);
1715afbac601SDavid Ahern 		if (master) {
1716afbac601SDavid Ahern 			master_idx = master->ifindex;
1717afbac601SDavid Ahern 
1718afbac601SDavid Ahern 			hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1719afbac601SDavid Ahern 							    master, &dst,
1720afbac601SDavid Ahern 							    scores, hiscore_idx);
1721afbac601SDavid Ahern 
1722afbac601SDavid Ahern 			if (scores[hiscore_idx].ifa)
1723afbac601SDavid Ahern 				goto out;
1724afbac601SDavid Ahern 		}
1725afbac601SDavid Ahern 
17269131f3deSYOSHIFUJI Hideaki/吉藤英明 		for_each_netdev_rcu(net, dev) {
1727afbac601SDavid Ahern 			/* only consider addresses on devices in the
1728afbac601SDavid Ahern 			 * same L3 domain
1729afbac601SDavid Ahern 			 */
1730afbac601SDavid Ahern 			if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1731afbac601SDavid Ahern 				continue;
17329131f3deSYOSHIFUJI Hideaki/吉藤英明 			idev = __in6_dev_get(dev);
17339131f3deSYOSHIFUJI Hideaki/吉藤英明 			if (!idev)
17349131f3deSYOSHIFUJI Hideaki/吉藤英明 				continue;
1735c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
17369131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
17379131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
1738afbac601SDavid Ahern 
1739afbac601SDavid Ahern out:
17408814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
17411da177e4SLinus Torvalds 
1742c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	hiscore = &scores[hiscore_idx];
1743a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1744072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
17451da177e4SLinus Torvalds 
17464e3fd7a0SAlexey Dobriyan 	*saddr = hiscore->ifa->addr;
1747a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1748072047e4SYOSHIFUJI Hideaki 	return 0;
17491da177e4SLinus Torvalds }
17505e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
17511da177e4SLinus Torvalds 
17528965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1753479840ffSJiri Pirko 		      u32 banned_flags)
1754b7b1bfceSHannes Frederic Sowa {
1755b7b1bfceSHannes Frederic Sowa 	struct inet6_ifaddr *ifp;
1756b7b1bfceSHannes Frederic Sowa 	int err = -EADDRNOTAVAIL;
1757b7b1bfceSHannes Frederic Sowa 
1758602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1759602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1760602582caSHannes Frederic Sowa 			break;
1761b7b1bfceSHannes Frederic Sowa 		if (ifp->scope == IFA_LINK &&
1762b7b1bfceSHannes Frederic Sowa 		    !(ifp->flags & banned_flags)) {
1763b7b1bfceSHannes Frederic Sowa 			*addr = ifp->addr;
1764b7b1bfceSHannes Frederic Sowa 			err = 0;
1765b7b1bfceSHannes Frederic Sowa 			break;
1766b7b1bfceSHannes Frederic Sowa 		}
1767b7b1bfceSHannes Frederic Sowa 	}
1768b7b1bfceSHannes Frederic Sowa 	return err;
1769b7b1bfceSHannes Frederic Sowa }
1770b7b1bfceSHannes Frederic Sowa 
177195c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1772479840ffSJiri Pirko 		    u32 banned_flags)
17731da177e4SLinus Torvalds {
17741da177e4SLinus Torvalds 	struct inet6_dev *idev;
17751da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
17761da177e4SLinus Torvalds 
17778814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1778e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1779e21e8467SStephen Hemminger 	if (idev) {
17801da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1781b7b1bfceSHannes Frederic Sowa 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
17821da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
17831da177e4SLinus Torvalds 	}
17848814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
17851da177e4SLinus Torvalds 	return err;
17861da177e4SLinus Torvalds }
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
17891da177e4SLinus Torvalds {
17901da177e4SLinus Torvalds 	int cnt = 0;
17911da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1794502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list)
17951da177e4SLinus Torvalds 		cnt++;
17961da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
17971da177e4SLinus Torvalds 	return cnt;
17981da177e4SLinus Torvalds }
17991da177e4SLinus Torvalds 
1800b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
18012a7851bfSFlorian Westphal 		  const struct net_device *dev, int strict)
18021da177e4SLinus Torvalds {
1803c58da4c6SErik Kline 	return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1804c58da4c6SErik Kline }
1805c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr);
1806c58da4c6SErik Kline 
1807c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1808c58da4c6SErik Kline 			    const struct net_device *dev, int strict,
1809c58da4c6SErik Kline 			    u32 banned_flags)
1810c58da4c6SErik Kline {
1811eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1812ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
1813c58da4c6SErik Kline 	u32 ifp_flags;
18141da177e4SLinus Torvalds 
18155c578aedSstephen hemminger 	rcu_read_lock_bh();
1816b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1817878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1818bfeade08SDaniel Lezcano 			continue;
1819c58da4c6SErik Kline 		/* Decouple optimistic from tentative for evaluation here.
1820c58da4c6SErik Kline 		 * Ban optimistic addresses explicitly, when required.
1821c58da4c6SErik Kline 		 */
1822c58da4c6SErik Kline 		ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1823c58da4c6SErik Kline 			    ? (ifp->flags&~IFA_F_TENTATIVE)
1824c58da4c6SErik Kline 			    : ifp->flags;
18251da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1826c58da4c6SErik Kline 		    !(ifp_flags&banned_flags) &&
182763159f29SIan Morris 		    (!dev || ifp->idev->dev == dev ||
1828eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
18295c578aedSstephen hemminger 			rcu_read_unlock_bh();
1830eedf042aSStephen Hemminger 			return 1;
1831eedf042aSStephen Hemminger 		}
1832eedf042aSStephen Hemminger 	}
18335c578aedSstephen hemminger 
1834eedf042aSStephen Hemminger 	rcu_read_unlock_bh();
1835eedf042aSStephen Hemminger 	return 0;
18361da177e4SLinus Torvalds }
1837c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
18387159039aSYOSHIFUJI Hideaki 
18393e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
184006bfe655SDaniel Lezcano 			       struct net_device *dev)
18411da177e4SLinus Torvalds {
1842ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
18431da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
18441da177e4SLinus Torvalds 
1845b67bfe0dSSasha Levin 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1846878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
184706bfe655SDaniel Lezcano 			continue;
18481da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
184963159f29SIan Morris 			if (!dev || ifp->idev->dev == dev)
18503e81c6daSDavid S. Miller 				return true;
18511da177e4SLinus Torvalds 		}
18521da177e4SLinus Torvalds 	}
18533e81c6daSDavid S. Miller 	return false;
18541da177e4SLinus Torvalds }
18551da177e4SLinus Torvalds 
18567df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev.
18577df37ff3SCatalin\(ux\) M. BOIE  * If one is found it returns true.
18587df37ff3SCatalin\(ux\) M. BOIE  */
18597df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
18607df37ff3SCatalin\(ux\) M. BOIE 	const unsigned int prefix_len, struct net_device *dev)
18617df37ff3SCatalin\(ux\) M. BOIE {
18627df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_dev *idev;
18637df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_ifaddr *ifa;
18647df37ff3SCatalin\(ux\) M. BOIE 	bool ret = false;
18657df37ff3SCatalin\(ux\) M. BOIE 
18667df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_lock();
18677df37ff3SCatalin\(ux\) M. BOIE 	idev = __in6_dev_get(dev);
18687df37ff3SCatalin\(ux\) M. BOIE 	if (idev) {
18697df37ff3SCatalin\(ux\) M. BOIE 		read_lock_bh(&idev->lock);
18707df37ff3SCatalin\(ux\) M. BOIE 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
18717df37ff3SCatalin\(ux\) M. BOIE 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
18727df37ff3SCatalin\(ux\) M. BOIE 			if (ret)
18737df37ff3SCatalin\(ux\) M. BOIE 				break;
18747df37ff3SCatalin\(ux\) M. BOIE 		}
18757df37ff3SCatalin\(ux\) M. BOIE 		read_unlock_bh(&idev->lock);
18767df37ff3SCatalin\(ux\) M. BOIE 	}
18777df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_unlock();
18787df37ff3SCatalin\(ux\) M. BOIE 
18797df37ff3SCatalin\(ux\) M. BOIE 	return ret;
18807df37ff3SCatalin\(ux\) M. BOIE }
18817df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix);
18827df37ff3SCatalin\(ux\) M. BOIE 
1883b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
188452eeeb84SYOSHIFUJI Hideaki {
188552eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
188652eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
188752eeeb84SYOSHIFUJI Hideaki 	int	onlink;
188852eeeb84SYOSHIFUJI Hideaki 
188952eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
189052eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
189152eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
189252eeeb84SYOSHIFUJI Hideaki 	if (idev) {
189352eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
1894502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
189552eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
189652eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
189752eeeb84SYOSHIFUJI Hideaki 			if (onlink)
189852eeeb84SYOSHIFUJI Hideaki 				break;
189952eeeb84SYOSHIFUJI Hideaki 		}
190052eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
190152eeeb84SYOSHIFUJI Hideaki 	}
190252eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
190352eeeb84SYOSHIFUJI Hideaki 	return onlink;
190452eeeb84SYOSHIFUJI Hideaki }
190552eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
190652eeeb84SYOSHIFUJI Hideaki 
19079acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
19081cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
19091da177e4SLinus Torvalds {
1910b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
1911ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
19121da177e4SLinus Torvalds 
19135c578aedSstephen hemminger 	rcu_read_lock_bh();
1914b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1915878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
19161cab3da6SDaniel Lezcano 			continue;
19171da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
191863159f29SIan Morris 			if (!dev || ifp->idev->dev == dev ||
19191da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1920b79d1d54SDavid S. Miller 				result = ifp;
19211da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
19221da177e4SLinus Torvalds 				break;
19231da177e4SLinus Torvalds 			}
19241da177e4SLinus Torvalds 		}
19251da177e4SLinus Torvalds 	}
19265c578aedSstephen hemminger 	rcu_read_unlock_bh();
19271da177e4SLinus Torvalds 
1928b79d1d54SDavid S. Miller 	return result;
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
19321da177e4SLinus Torvalds 
1933cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
19341da177e4SLinus Torvalds {
19353d171f39SLubomir Rintel 	if (dad_failed)
19363d171f39SLubomir Rintel 		ifp->flags |= IFA_F_DADFAILED;
19373d171f39SLubomir Rintel 
1938ec8add2aSSabrina Dubroca 	if (ifp->flags&IFA_F_TEMPORARY) {
19391da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
19401da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
19411da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
19421da177e4SLinus Torvalds 		if (ifpub) {
19431da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
19441da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
19451da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
19461da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
19471da177e4SLinus Torvalds 		} else {
19481da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
19491da177e4SLinus Torvalds 		}
19501da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
1951ec8add2aSSabrina Dubroca 	} else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
1952ec8add2aSSabrina Dubroca 		spin_lock_bh(&ifp->lock);
1953ec8add2aSSabrina Dubroca 		addrconf_del_dad_work(ifp);
1954ec8add2aSSabrina Dubroca 		ifp->flags |= IFA_F_TENTATIVE;
1955ec8add2aSSabrina Dubroca 		spin_unlock_bh(&ifp->lock);
1956ec8add2aSSabrina Dubroca 		if (dad_failed)
1957ec8add2aSSabrina Dubroca 			ipv6_ifa_notify(0, ifp);
1958ec8add2aSSabrina Dubroca 		in6_ifa_put(ifp);
1959c15b1ccaSHannes Frederic Sowa 	} else {
19601da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
19611da177e4SLinus Torvalds 	}
1962c15b1ccaSHannes Frederic Sowa }
19631da177e4SLinus Torvalds 
1964f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1965f2344a13SHerbert Xu {
1966f2344a13SHerbert Xu 	int err = -ENOENT;
1967f2344a13SHerbert Xu 
19688e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
1969f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
1970f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
1971f2344a13SHerbert Xu 		err = 0;
1972f2344a13SHerbert Xu 	}
19738e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
1974f2344a13SHerbert Xu 
1975f2344a13SHerbert Xu 	return err;
1976f2344a13SHerbert Xu }
1977f2344a13SHerbert Xu 
19783c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
19793c21edbdSYOSHIFUJI Hideaki {
19801b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
19811855b7c3SHannes Frederic Sowa 	struct net *net = dev_net(ifp->idev->dev);
19829bdd8d40SBrian Haley 
1983853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
1984853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
1985f2344a13SHerbert Xu 		return;
1986853dc2e0SUrsula Braun 	}
1987f2344a13SHerbert Xu 
1988e87cc472SJoe Perches 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
19890522fea6SJens Rosenboom 			     ifp->idev->dev->name, &ifp->addr);
19909bdd8d40SBrian Haley 
19915f40ef77SHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
19921b34be74SYOSHIFUJI Hideaki 
19935f40ef77SHannes Frederic Sowa 	if (ifp->flags & IFA_F_STABLE_PRIVACY) {
19945f40ef77SHannes Frederic Sowa 		int scope = ifp->scope;
19955f40ef77SHannes Frederic Sowa 		u32 flags = ifp->flags;
19965f40ef77SHannes Frederic Sowa 		struct in6_addr new_addr;
19975f40ef77SHannes Frederic Sowa 		struct inet6_ifaddr *ifp2;
19985f40ef77SHannes Frederic Sowa 		u32 valid_lft, preferred_lft;
19995f40ef77SHannes Frederic Sowa 		int pfxlen = ifp->prefix_len;
20005f40ef77SHannes Frederic Sowa 		int retries = ifp->stable_privacy_retry + 1;
20015f40ef77SHannes Frederic Sowa 
20021855b7c3SHannes Frederic Sowa 		if (retries > net->ipv6.sysctl.idgen_retries) {
20035f40ef77SHannes Frederic Sowa 			net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
20045f40ef77SHannes Frederic Sowa 					     ifp->idev->dev->name);
20055f40ef77SHannes Frederic Sowa 			goto errdad;
20065f40ef77SHannes Frederic Sowa 		}
20075f40ef77SHannes Frederic Sowa 
20085f40ef77SHannes Frederic Sowa 		new_addr = ifp->addr;
20095f40ef77SHannes Frederic Sowa 		if (ipv6_generate_stable_address(&new_addr, retries,
20105f40ef77SHannes Frederic Sowa 						 idev))
20115f40ef77SHannes Frederic Sowa 			goto errdad;
20125f40ef77SHannes Frederic Sowa 
20135f40ef77SHannes Frederic Sowa 		valid_lft = ifp->valid_lft;
20145f40ef77SHannes Frederic Sowa 		preferred_lft = ifp->prefered_lft;
20155f40ef77SHannes Frederic Sowa 
20165f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp->lock);
20175f40ef77SHannes Frederic Sowa 
20185f40ef77SHannes Frederic Sowa 		if (idev->cnf.max_addresses &&
20195f40ef77SHannes Frederic Sowa 		    ipv6_count_addresses(idev) >=
20205f40ef77SHannes Frederic Sowa 		    idev->cnf.max_addresses)
20215f40ef77SHannes Frederic Sowa 			goto lock_errdad;
20225f40ef77SHannes Frederic Sowa 
20235f40ef77SHannes Frederic Sowa 		net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
20245f40ef77SHannes Frederic Sowa 				     ifp->idev->dev->name);
20255f40ef77SHannes Frederic Sowa 
20265f40ef77SHannes Frederic Sowa 		ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
20275f40ef77SHannes Frederic Sowa 				     scope, flags, valid_lft,
20285f40ef77SHannes Frederic Sowa 				     preferred_lft);
20295f40ef77SHannes Frederic Sowa 		if (IS_ERR(ifp2))
20305f40ef77SHannes Frederic Sowa 			goto lock_errdad;
20315f40ef77SHannes Frederic Sowa 
20325f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp2->lock);
20335f40ef77SHannes Frederic Sowa 		ifp2->stable_privacy_retry = retries;
20345f40ef77SHannes Frederic Sowa 		ifp2->state = INET6_IFADDR_STATE_PREDAD;
20355f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp2->lock);
20365f40ef77SHannes Frederic Sowa 
20371855b7c3SHannes Frederic Sowa 		addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
20385f40ef77SHannes Frederic Sowa 		in6_ifa_put(ifp2);
20395f40ef77SHannes Frederic Sowa lock_errdad:
20405f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp->lock);
20411b34be74SYOSHIFUJI Hideaki 	}
20421b34be74SYOSHIFUJI Hideaki 
20435f40ef77SHannes Frederic Sowa errdad:
2044c15b1ccaSHannes Frederic Sowa 	/* transition from _POSTDAD to _ERRDAD */
2045c15b1ccaSHannes Frederic Sowa 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
20468e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
2047c15b1ccaSHannes Frederic Sowa 
2048c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, 0);
2049751eb6b6SWei Yongjun 	in6_ifa_put(ifp);
20503c21edbdSYOSHIFUJI Hideaki }
20511da177e4SLinus Torvalds 
2052a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group.
2053a9ed4a29SSabrina Dubroca  * caller must hold RTNL */
2054b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
20551da177e4SLinus Torvalds {
20561da177e4SLinus Torvalds 	struct in6_addr maddr;
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
20591da177e4SLinus Torvalds 		return;
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
20621da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
20631da177e4SLinus Torvalds }
20641da177e4SLinus Torvalds 
2065a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
2066b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
20671da177e4SLinus Torvalds {
20681da177e4SLinus Torvalds 	struct in6_addr maddr;
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
20711da177e4SLinus Torvalds 		return;
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
20741da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
20751da177e4SLinus Torvalds }
20761da177e4SLinus Torvalds 
2077a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
207820380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
20791da177e4SLinus Torvalds {
20801da177e4SLinus Torvalds 	struct in6_addr addr;
2081c15b1ccaSHannes Frederic Sowa 
208288ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
20832bda8a0cSBjørn Mork 		return;
20841da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
20851da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
20861da177e4SLinus Torvalds 		return;
2087013b4d90SWANG Cong 	__ipv6_dev_ac_inc(ifp->idev, &addr);
20881da177e4SLinus Torvalds }
20891da177e4SLinus Torvalds 
2090a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
209120380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
20921da177e4SLinus Torvalds {
20931da177e4SLinus Torvalds 	struct in6_addr addr;
2094c15b1ccaSHannes Frederic Sowa 
209588ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
209632019e65SYOSHIFUJI Hideaki 		return;
20971da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
20981da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
20991da177e4SLinus Torvalds 		return;
21001da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
21011da177e4SLinus Torvalds }
21021da177e4SLinus Torvalds 
21038a7a4b47SAlexander Aring static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
210406a4c1c5Salex.bluesman.smirnov@gmail.com {
21058a7a4b47SAlexander Aring 	switch (dev->addr_len) {
21068a7a4b47SAlexander Aring 	case ETH_ALEN:
21079dae2e03SLuiz Augusto von Dentz 		memcpy(eui, dev->dev_addr, 3);
21089dae2e03SLuiz Augusto von Dentz 		eui[3] = 0xFF;
21099dae2e03SLuiz Augusto von Dentz 		eui[4] = 0xFE;
21109dae2e03SLuiz Augusto von Dentz 		memcpy(eui + 5, dev->dev_addr + 3, 3);
21119dae2e03SLuiz Augusto von Dentz 		break;
21128a7a4b47SAlexander Aring 	case EUI64_ADDR_LEN:
21135241c2d7SAlexander Aring 		memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
21145e98a36eSYOSHIFUJI Hideaki / 吉藤英明 		eui[0] ^= 2;
21158a7a4b47SAlexander Aring 		break;
21168a7a4b47SAlexander Aring 	default:
21178a7a4b47SAlexander Aring 		return -1;
21188a7a4b47SAlexander Aring 	}
21198a7a4b47SAlexander Aring 
212006a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
212106a4c1c5Salex.bluesman.smirnov@gmail.com }
212206a4c1c5Salex.bluesman.smirnov@gmail.com 
2123cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2124cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
2125cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	union fwnet_hwaddr *ha;
2126cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2127cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
2128cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
2129cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2130cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	ha = (union fwnet_hwaddr *)dev->dev_addr;
2131cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2132cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2133cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
2134cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
2135cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
2136cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2137073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2138073a8e0eSYOSHIFUJI Hideaki {
21391da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
21401da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
21411da177e4SLinus Torvalds 		return -1;
21421da177e4SLinus Torvalds 	memset(eui, 0, 7);
21431da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
21441da177e4SLinus Torvalds 	return 0;
2145073a8e0eSYOSHIFUJI Hideaki }
2146073a8e0eSYOSHIFUJI Hideaki 
2147073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2148073a8e0eSYOSHIFUJI Hideaki {
21491da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
21501da177e4SLinus Torvalds 		return -1;
21511da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
21521da177e4SLinus Torvalds 	eui[0] |= 2;
21531da177e4SLinus Torvalds 	return 0;
21541da177e4SLinus Torvalds }
2155073a8e0eSYOSHIFUJI Hideaki 
2156c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2157dfd982baSYOSHIFUJI Hideaki {
21589af28511SSascha Hlusiak 	if (addr == 0)
21599af28511SSascha Hlusiak 		return -1;
2160dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2161dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2162dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2163dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2164dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2165dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2166dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
2167dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
2168dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
2169dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
2170dfd982baSYOSHIFUJI Hideaki 	return 0;
2171dfd982baSYOSHIFUJI Hideaki }
2172dfd982baSYOSHIFUJI Hideaki 
2173dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2174dfd982baSYOSHIFUJI Hideaki {
2175dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
2176dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2177dfd982baSYOSHIFUJI Hideaki 	return -1;
2178dfd982baSYOSHIFUJI Hideaki }
2179dfd982baSYOSHIFUJI Hideaki 
2180aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2181aee80b54Sstephen hemminger {
2182aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2183aee80b54Sstephen hemminger }
2184aee80b54Sstephen hemminger 
2185e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2186e837735eSNicolas Dichtel {
2187e837735eSNicolas Dichtel 	memcpy(eui, dev->perm_addr, 3);
2188e837735eSNicolas Dichtel 	memcpy(eui + 5, dev->perm_addr + 3, 3);
2189e837735eSNicolas Dichtel 	eui[3] = 0xFF;
2190e837735eSNicolas Dichtel 	eui[4] = 0xFE;
2191e837735eSNicolas Dichtel 	eui[0] ^= 2;
2192e837735eSNicolas Dichtel 	return 0;
2193e837735eSNicolas Dichtel }
2194e837735eSNicolas Dichtel 
2195073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2196073a8e0eSYOSHIFUJI Hideaki {
2197073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
2198073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
2199073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
2200073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
2201073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
2202073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
2203073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
2204073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
2205c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
2206dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
2207aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
220845ce0fd1SFelix Jia 	case ARPHRD_TUNNEL:
2209aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
2210e74bccb8SJukka Rissanen 	case ARPHRD_6LOWPAN:
22118a7a4b47SAlexander Aring 		return addrconf_ifid_6lowpan(eui, dev);
2212cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
2213cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
2214e837735eSNicolas Dichtel 	case ARPHRD_TUNNEL6:
221545ce0fd1SFelix Jia 	case ARPHRD_IP6GRE:
2216e837735eSNicolas Dichtel 		return addrconf_ifid_ip6tnl(eui, dev);
2217073a8e0eSYOSHIFUJI Hideaki 	}
22181da177e4SLinus Torvalds 	return -1;
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
22221da177e4SLinus Torvalds {
22231da177e4SLinus Torvalds 	int err = -1;
22241da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2227602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2228602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
2229602582caSHannes Frederic Sowa 			break;
22301da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
22311da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
22321da177e4SLinus Torvalds 			err = 0;
22331da177e4SLinus Torvalds 			break;
22341da177e4SLinus Torvalds 		}
22351da177e4SLinus Torvalds 	}
22361da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
22371da177e4SLinus Torvalds 	return err;
22381da177e4SLinus Torvalds }
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
22419d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev)
22421da177e4SLinus Torvalds {
22431da177e4SLinus Torvalds regen:
2244955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
22451da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds 	/*
22481da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
22491da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
22501da177e4SLinus Torvalds 	 *
22511da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
22521da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
2253c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
22541da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
22551da177e4SLinus Torvalds 	 *  - value 0
22561da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
22571da177e4SLinus Torvalds 	 */
22581da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
22591da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
22601da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
22611da177e4SLinus Torvalds 		goto regen;
22621da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
22631da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
22641da177e4SLinus Torvalds 			goto regen;
22651da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
22661da177e4SLinus Torvalds 			goto regen;
22671da177e4SLinus Torvalds 	}
22681da177e4SLinus Torvalds }
22691da177e4SLinus Torvalds 
22709d6280daSJiri Bohac static void  ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
22718e5e8f30SEldad Zack {
22721da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
22739d6280daSJiri Bohac 		ipv6_regen_rndid(idev);
22741da177e4SLinus Torvalds }
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds /*
22771da177e4SLinus Torvalds  *	Add prefix route.
22781da177e4SLinus Torvalds  */
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds static void
22811da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2282e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
22831da177e4SLinus Torvalds {
228486872cb5SThomas Graf 	struct fib6_config cfg = {
2285ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
228686872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
228786872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
228886872cb5SThomas Graf 		.fc_expires = expires,
228986872cb5SThomas Graf 		.fc_dst_len = plen,
229086872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
2291c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
2292f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
229386872cb5SThomas Graf 	};
22941da177e4SLinus Torvalds 
22954e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
22961da177e4SLinus Torvalds 
22971da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
22981da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
22991da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
23001da177e4SLinus Torvalds 	 */
230107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
23021da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
230386872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
23040be669bbSJoerg Roedel #endif
23051da177e4SLinus Torvalds 
2306333c4301SDavid Ahern 	ip6_route_add(&cfg, NULL);
23071da177e4SLinus Torvalds }
23081da177e4SLinus Torvalds 
230914ef37b6SAndreas Hofmeister 
231014ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
231114ef37b6SAndreas Hofmeister 						  int plen,
231214ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
231314ef37b6SAndreas Hofmeister 						  u32 flags, u32 noflags)
231414ef37b6SAndreas Hofmeister {
231514ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
231614ef37b6SAndreas Hofmeister 	struct rt6_info *rt = NULL;
231714ef37b6SAndreas Hofmeister 	struct fib6_table *table;
2318ca254490SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
231914ef37b6SAndreas Hofmeister 
2320ca254490SDavid Ahern 	table = fib6_get_table(dev_net(dev), tb_id);
232163159f29SIan Morris 	if (!table)
232214ef37b6SAndreas Hofmeister 		return NULL;
232314ef37b6SAndreas Hofmeister 
23245744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
2325*38fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
232614ef37b6SAndreas Hofmeister 	if (!fn)
232714ef37b6SAndreas Hofmeister 		goto out;
23281f56a01fSMartin KaFai Lau 
23291f56a01fSMartin KaFai Lau 	noflags |= RTF_CACHE;
233014ef37b6SAndreas Hofmeister 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2331d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != dev->ifindex)
233214ef37b6SAndreas Hofmeister 			continue;
233314ef37b6SAndreas Hofmeister 		if ((rt->rt6i_flags & flags) != flags)
233414ef37b6SAndreas Hofmeister 			continue;
233585da53bfSRomain Kuntz 		if ((rt->rt6i_flags & noflags) != 0)
233614ef37b6SAndreas Hofmeister 			continue;
233714ef37b6SAndreas Hofmeister 		dst_hold(&rt->dst);
233814ef37b6SAndreas Hofmeister 		break;
233914ef37b6SAndreas Hofmeister 	}
234014ef37b6SAndreas Hofmeister out:
23415744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
234214ef37b6SAndreas Hofmeister 	return rt;
234314ef37b6SAndreas Hofmeister }
234414ef37b6SAndreas Hofmeister 
234514ef37b6SAndreas Hofmeister 
23461da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
23471da177e4SLinus Torvalds 
23481da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
23491da177e4SLinus Torvalds {
235086872cb5SThomas Graf 	struct fib6_config cfg = {
2351ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
235286872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
235386872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
235486872cb5SThomas Graf 		.fc_dst_len = 8,
235586872cb5SThomas Graf 		.fc_flags = RTF_UP,
2356c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
235786872cb5SThomas Graf 	};
23581da177e4SLinus Torvalds 
235986872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
236086872cb5SThomas Graf 
2361333c4301SDavid Ahern 	ip6_route_add(&cfg, NULL);
23621da177e4SLinus Torvalds }
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
23651da177e4SLinus Torvalds {
23661da177e4SLinus Torvalds 	struct inet6_dev *idev;
23671da177e4SLinus Torvalds 
23681da177e4SLinus Torvalds 	ASSERT_RTNL();
23691da177e4SLinus Torvalds 
2370e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2371e21e8467SStephen Hemminger 	if (!idev)
237264e724f6SBrian Haley 		return ERR_PTR(-ENOBUFS);
237364e724f6SBrian Haley 
237464e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
237564e724f6SBrian Haley 		return ERR_PTR(-EACCES);
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds 	/* Add default multicast route */
2378ba46ee4cSDavid Ahern 	if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
23791da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds 	return idev;
23821da177e4SLinus Torvalds }
23831da177e4SLinus Torvalds 
238453bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev,
238553bd6749SJiri Pirko 			     struct inet6_ifaddr *ifp,
238653bd6749SJiri Pirko 			     __u32 valid_lft, __u32 prefered_lft,
238753bd6749SJiri Pirko 			     bool create, unsigned long now)
238853bd6749SJiri Pirko {
238953bd6749SJiri Pirko 	u32 flags;
239053bd6749SJiri Pirko 	struct inet6_ifaddr *ift;
239153bd6749SJiri Pirko 
239253bd6749SJiri Pirko 	read_lock_bh(&idev->lock);
239353bd6749SJiri Pirko 	/* update all temporary addresses in the list */
239453bd6749SJiri Pirko 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
239553bd6749SJiri Pirko 		int age, max_valid, max_prefered;
239653bd6749SJiri Pirko 
239753bd6749SJiri Pirko 		if (ifp != ift->ifpub)
239853bd6749SJiri Pirko 			continue;
239953bd6749SJiri Pirko 
240053bd6749SJiri Pirko 		/* RFC 4941 section 3.3:
240153bd6749SJiri Pirko 		 * If a received option will extend the lifetime of a public
240253bd6749SJiri Pirko 		 * address, the lifetimes of temporary addresses should
240353bd6749SJiri Pirko 		 * be extended, subject to the overall constraint that no
240453bd6749SJiri Pirko 		 * temporary addresses should ever remain "valid" or "preferred"
240553bd6749SJiri Pirko 		 * for a time longer than (TEMP_VALID_LIFETIME) or
240653bd6749SJiri Pirko 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
240753bd6749SJiri Pirko 		 */
240853bd6749SJiri Pirko 		age = (now - ift->cstamp) / HZ;
240953bd6749SJiri Pirko 		max_valid = idev->cnf.temp_valid_lft - age;
241053bd6749SJiri Pirko 		if (max_valid < 0)
241153bd6749SJiri Pirko 			max_valid = 0;
241253bd6749SJiri Pirko 
241353bd6749SJiri Pirko 		max_prefered = idev->cnf.temp_prefered_lft -
241476506a98SJiri Bohac 			       idev->desync_factor - age;
241553bd6749SJiri Pirko 		if (max_prefered < 0)
241653bd6749SJiri Pirko 			max_prefered = 0;
241753bd6749SJiri Pirko 
241853bd6749SJiri Pirko 		if (valid_lft > max_valid)
241953bd6749SJiri Pirko 			valid_lft = max_valid;
242053bd6749SJiri Pirko 
242153bd6749SJiri Pirko 		if (prefered_lft > max_prefered)
242253bd6749SJiri Pirko 			prefered_lft = max_prefered;
242353bd6749SJiri Pirko 
242453bd6749SJiri Pirko 		spin_lock(&ift->lock);
242553bd6749SJiri Pirko 		flags = ift->flags;
242653bd6749SJiri Pirko 		ift->valid_lft = valid_lft;
242753bd6749SJiri Pirko 		ift->prefered_lft = prefered_lft;
242853bd6749SJiri Pirko 		ift->tstamp = now;
242953bd6749SJiri Pirko 		if (prefered_lft > 0)
243053bd6749SJiri Pirko 			ift->flags &= ~IFA_F_DEPRECATED;
243153bd6749SJiri Pirko 
243253bd6749SJiri Pirko 		spin_unlock(&ift->lock);
243353bd6749SJiri Pirko 		if (!(flags&IFA_F_TENTATIVE))
243453bd6749SJiri Pirko 			ipv6_ifa_notify(0, ift);
243553bd6749SJiri Pirko 	}
243653bd6749SJiri Pirko 
243753bd6749SJiri Pirko 	if ((create || list_empty(&idev->tempaddr_list)) &&
243853bd6749SJiri Pirko 	    idev->cnf.use_tempaddr > 0) {
243953bd6749SJiri Pirko 		/* When a new public address is created as described
244053bd6749SJiri Pirko 		 * in [ADDRCONF], also create a new temporary address.
244153bd6749SJiri Pirko 		 * Also create a temporary address if it's enabled but
244253bd6749SJiri Pirko 		 * no temporary address currently exists.
244353bd6749SJiri Pirko 		 */
244453bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
244553bd6749SJiri Pirko 		ipv6_create_tempaddr(ifp, NULL);
244653bd6749SJiri Pirko 	} else {
244753bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
244853bd6749SJiri Pirko 	}
244953bd6749SJiri Pirko }
245053bd6749SJiri Pirko 
2451cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2452cc9da6ccSBjørn Mork {
2453d35a00b8SFelix Jia 	return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2454d35a00b8SFelix Jia 	       idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2455cc9da6ccSBjørn Mork }
2456cc9da6ccSBjørn Mork 
2457cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
24584f672235SAlexander Aring 				 const struct prefix_info *pinfo,
24594f672235SAlexander Aring 				 struct inet6_dev *in6_dev,
2460cc84b3c6SAlexander Aring 				 const struct in6_addr *addr, int addr_type,
2461cc84b3c6SAlexander Aring 				 u32 addr_flags, bool sllao, bool tokenized,
24624f672235SAlexander Aring 				 __u32 valid_lft, u32 prefered_lft)
24634f672235SAlexander Aring {
24644f672235SAlexander Aring 	struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
24654f672235SAlexander Aring 	int create = 0, update_lft = 0;
24664f672235SAlexander Aring 
24674f672235SAlexander Aring 	if (!ifp && valid_lft) {
24684f672235SAlexander Aring 		int max_addresses = in6_dev->cnf.max_addresses;
24694f672235SAlexander Aring 
24704f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
247135e015e1SMatteo Croce 		if ((net->ipv6.devconf_all->optimistic_dad ||
247235e015e1SMatteo Croce 		     in6_dev->cnf.optimistic_dad) &&
24734f672235SAlexander Aring 		    !net->ipv6.devconf_all->forwarding && sllao)
24744f672235SAlexander Aring 			addr_flags |= IFA_F_OPTIMISTIC;
24754f672235SAlexander Aring #endif
24764f672235SAlexander Aring 
24774f672235SAlexander Aring 		/* Do not allow to create too much of autoconfigured
24784f672235SAlexander Aring 		 * addresses; this would be too easy way to crash kernel.
24794f672235SAlexander Aring 		 */
24804f672235SAlexander Aring 		if (!max_addresses ||
24814f672235SAlexander Aring 		    ipv6_count_addresses(in6_dev) < max_addresses)
24824f672235SAlexander Aring 			ifp = ipv6_add_addr(in6_dev, addr, NULL,
24834f672235SAlexander Aring 					    pinfo->prefix_len,
24844f672235SAlexander Aring 					    addr_type&IPV6_ADDR_SCOPE_MASK,
24854f672235SAlexander Aring 					    addr_flags, valid_lft,
24864f672235SAlexander Aring 					    prefered_lft);
24874f672235SAlexander Aring 
24884f672235SAlexander Aring 		if (IS_ERR_OR_NULL(ifp))
24894f672235SAlexander Aring 			return -1;
24904f672235SAlexander Aring 
24914f672235SAlexander Aring 		update_lft = 0;
24924f672235SAlexander Aring 		create = 1;
24934f672235SAlexander Aring 		spin_lock_bh(&ifp->lock);
24944f672235SAlexander Aring 		ifp->flags |= IFA_F_MANAGETEMPADDR;
24954f672235SAlexander Aring 		ifp->cstamp = jiffies;
24964f672235SAlexander Aring 		ifp->tokenized = tokenized;
24974f672235SAlexander Aring 		spin_unlock_bh(&ifp->lock);
24984f672235SAlexander Aring 		addrconf_dad_start(ifp);
24994f672235SAlexander Aring 	}
25004f672235SAlexander Aring 
25014f672235SAlexander Aring 	if (ifp) {
25024f672235SAlexander Aring 		u32 flags;
25034f672235SAlexander Aring 		unsigned long now;
25044f672235SAlexander Aring 		u32 stored_lft;
25054f672235SAlexander Aring 
25064f672235SAlexander Aring 		/* update lifetime (RFC2462 5.5.3 e) */
25074f672235SAlexander Aring 		spin_lock_bh(&ifp->lock);
25084f672235SAlexander Aring 		now = jiffies;
25094f672235SAlexander Aring 		if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
25104f672235SAlexander Aring 			stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
25114f672235SAlexander Aring 		else
25124f672235SAlexander Aring 			stored_lft = 0;
25134f672235SAlexander Aring 		if (!update_lft && !create && stored_lft) {
25144f672235SAlexander Aring 			const u32 minimum_lft = min_t(u32,
25154f672235SAlexander Aring 				stored_lft, MIN_VALID_LIFETIME);
25164f672235SAlexander Aring 			valid_lft = max(valid_lft, minimum_lft);
25174f672235SAlexander Aring 
25184f672235SAlexander Aring 			/* RFC4862 Section 5.5.3e:
25194f672235SAlexander Aring 			 * "Note that the preferred lifetime of the
25204f672235SAlexander Aring 			 *  corresponding address is always reset to
25214f672235SAlexander Aring 			 *  the Preferred Lifetime in the received
25224f672235SAlexander Aring 			 *  Prefix Information option, regardless of
25234f672235SAlexander Aring 			 *  whether the valid lifetime is also reset or
25244f672235SAlexander Aring 			 *  ignored."
25254f672235SAlexander Aring 			 *
25264f672235SAlexander Aring 			 * So we should always update prefered_lft here.
25274f672235SAlexander Aring 			 */
25284f672235SAlexander Aring 			update_lft = 1;
25294f672235SAlexander Aring 		}
25304f672235SAlexander Aring 
25314f672235SAlexander Aring 		if (update_lft) {
25324f672235SAlexander Aring 			ifp->valid_lft = valid_lft;
25334f672235SAlexander Aring 			ifp->prefered_lft = prefered_lft;
25344f672235SAlexander Aring 			ifp->tstamp = now;
25354f672235SAlexander Aring 			flags = ifp->flags;
25364f672235SAlexander Aring 			ifp->flags &= ~IFA_F_DEPRECATED;
25374f672235SAlexander Aring 			spin_unlock_bh(&ifp->lock);
25384f672235SAlexander Aring 
25394f672235SAlexander Aring 			if (!(flags&IFA_F_TENTATIVE))
25404f672235SAlexander Aring 				ipv6_ifa_notify(0, ifp);
25414f672235SAlexander Aring 		} else
25424f672235SAlexander Aring 			spin_unlock_bh(&ifp->lock);
25434f672235SAlexander Aring 
25444f672235SAlexander Aring 		manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
25454f672235SAlexander Aring 				 create, now);
25464f672235SAlexander Aring 
25474f672235SAlexander Aring 		in6_ifa_put(ifp);
25484f672235SAlexander Aring 		addrconf_verify();
25494f672235SAlexander Aring 	}
25504f672235SAlexander Aring 
25514f672235SAlexander Aring 	return 0;
25524f672235SAlexander Aring }
2553cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
25544f672235SAlexander Aring 
2555e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
25561da177e4SLinus Torvalds {
25571da177e4SLinus Torvalds 	struct prefix_info *pinfo;
25581da177e4SLinus Torvalds 	__u32 valid_lft;
25591da177e4SLinus Torvalds 	__u32 prefered_lft;
25604f672235SAlexander Aring 	int addr_type, err;
256164236f3fSHannes Frederic Sowa 	u32 addr_flags = 0;
25621da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
256356d417b1SBrian Haley 	struct net *net = dev_net(dev);
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
25661da177e4SLinus Torvalds 
25671da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
2568ba3542e1Sdingtianhong 		ADBG("addrconf: prefix option too short\n");
25691da177e4SLinus Torvalds 		return;
25701da177e4SLinus Torvalds 	}
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds 	/*
25731da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
25741da177e4SLinus Torvalds 	 */
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
25791da177e4SLinus Torvalds 		return;
25801da177e4SLinus Torvalds 
25811da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
25821da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2585e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
25861da177e4SLinus Torvalds 		return;
25871da177e4SLinus Torvalds 	}
25881da177e4SLinus Torvalds 
25891da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
25901da177e4SLinus Torvalds 
259163159f29SIan Morris 	if (!in6_dev) {
2592e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2593e87cc472SJoe Perches 				    dev->name);
25941da177e4SLinus Torvalds 		return;
25951da177e4SLinus Torvalds 	}
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds 	/*
25981da177e4SLinus Torvalds 	 *	Two things going on here:
25991da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
26001da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
26011da177e4SLinus Torvalds 	 */
26021da177e4SLinus Torvalds 
26034bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
26044bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
26054bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
26064bed72e4SYOSHIFUJI Hideaki 
26071da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
26086f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
26096f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
26106f704992SYOSHIFUJI Hideaki 		 * not good.
26111da177e4SLinus Torvalds 		 */
26124bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
26134bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
26144bed72e4SYOSHIFUJI Hideaki 		else
26154bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
26163dd4bc68SYOSHIFUJI Hideaki 
26174bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
26184bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
26191da177e4SLinus Torvalds 
262014ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
262114ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
262214ef37b6SAndreas Hofmeister 					       dev,
262314ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
262414ef37b6SAndreas Hofmeister 					       RTF_GATEWAY | RTF_DEFAULT);
26251da177e4SLinus Torvalds 
262614ef37b6SAndreas Hofmeister 		if (rt) {
26276f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
26281da177e4SLinus Torvalds 			if (valid_lft == 0) {
2629e0a1ad73SThomas Graf 				ip6_del_rt(rt);
26301da177e4SLinus Torvalds 				rt = NULL;
26314bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
26326f704992SYOSHIFUJI Hideaki 				/* not infinity */
26331716a961SGao feng 				rt6_set_expires(rt, jiffies + rt_expires);
26346f704992SYOSHIFUJI Hideaki 			} else {
26351716a961SGao feng 				rt6_clean_expires(rt);
26361da177e4SLinus Torvalds 			}
26371da177e4SLinus Torvalds 		} else if (valid_lft) {
26386f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
26394bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
26404bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
26416f704992SYOSHIFUJI Hideaki 				/* not infinity */
26426f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
26436f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
26446f704992SYOSHIFUJI Hideaki 			}
26451da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
26466f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
26471da177e4SLinus Torvalds 		}
264894e187c0SAmerigo Wang 		ip6_rt_put(rt);
26491da177e4SLinus Torvalds 	}
26501da177e4SLinus Torvalds 
26511da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
26521da177e4SLinus Torvalds 
26531da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
26541da177e4SLinus Torvalds 		struct in6_addr addr;
2655f997c55cSAlexander Aring 		bool tokenized = false, dev_addr_generated = false;
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
26581da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2659f53adae4SDaniel Borkmann 
2660f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2661f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2662f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2663f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2664f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
2665617fe29dSDaniel Borkmann 				tokenized = true;
2666cc9da6ccSBjørn Mork 			} else if (is_addr_mode_generate_stable(in6_dev) &&
2667622c81d5SHannes Frederic Sowa 				   !ipv6_generate_stable_address(&addr, 0,
2668622c81d5SHannes Frederic Sowa 								 in6_dev)) {
266964236f3fSHannes Frederic Sowa 				addr_flags |= IFA_F_STABLE_PRIVACY;
2670622c81d5SHannes Frederic Sowa 				goto ok;
2671f53adae4SDaniel Borkmann 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
26721da177e4SLinus Torvalds 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
26734f672235SAlexander Aring 				goto put;
2674f997c55cSAlexander Aring 			} else {
2675f997c55cSAlexander Aring 				dev_addr_generated = true;
26761da177e4SLinus Torvalds 			}
26771da177e4SLinus Torvalds 			goto ok;
26781da177e4SLinus Torvalds 		}
2679e87cc472SJoe Perches 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
26801da177e4SLinus Torvalds 				    pinfo->prefix_len);
26814f672235SAlexander Aring 		goto put;
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds ok:
26844f672235SAlexander Aring 		err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
26854f672235SAlexander Aring 						   &addr, addr_type,
26864f672235SAlexander Aring 						   addr_flags, sllao,
26874f672235SAlexander Aring 						   tokenized, valid_lft,
26888a226b2cSJiri Benc 						   prefered_lft);
26894f672235SAlexander Aring 		if (err)
26904f672235SAlexander Aring 			goto put;
2691f997c55cSAlexander Aring 
2692f997c55cSAlexander Aring 		/* Ignore error case here because previous prefix add addr was
2693f997c55cSAlexander Aring 		 * successful which will be notified.
2694f997c55cSAlexander Aring 		 */
2695f997c55cSAlexander Aring 		ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2696f997c55cSAlexander Aring 					      addr_type, addr_flags, sllao,
2697f997c55cSAlexander Aring 					      tokenized, valid_lft,
2698f997c55cSAlexander Aring 					      prefered_lft,
2699f997c55cSAlexander Aring 					      dev_addr_generated);
27001da177e4SLinus Torvalds 	}
27011da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
27024f672235SAlexander Aring put:
27031da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
27041da177e4SLinus Torvalds }
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds /*
27071da177e4SLinus Torvalds  *	Set destination address.
27081da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
27091da177e4SLinus Torvalds  *	device.
27101da177e4SLinus Torvalds  */
2711af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
27121da177e4SLinus Torvalds {
27131da177e4SLinus Torvalds 	struct in6_ifreq ireq;
27141da177e4SLinus Torvalds 	struct net_device *dev;
27151da177e4SLinus Torvalds 	int err = -EINVAL;
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds 	rtnl_lock();
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds 	err = -EFAULT;
27201da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
27211da177e4SLinus Torvalds 		goto err_exit;
27221da177e4SLinus Torvalds 
2723af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
27241da177e4SLinus Torvalds 
27251da177e4SLinus Torvalds 	err = -ENODEV;
272663159f29SIan Morris 	if (!dev)
27271da177e4SLinus Torvalds 		goto err_exit;
27281da177e4SLinus Torvalds 
272907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
27301da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
27315bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
27321da177e4SLinus Torvalds 		struct ifreq ifr;
27331da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
27341da177e4SLinus Torvalds 
27351da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
27361da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
27371da177e4SLinus Torvalds 			goto err_exit;
27381da177e4SLinus Torvalds 
27391da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
27401da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
27411da177e4SLinus Torvalds 		p.iph.saddr = 0;
27421da177e4SLinus Torvalds 		p.iph.version = 4;
27431da177e4SLinus Torvalds 		p.iph.ihl = 5;
27441da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
27451da177e4SLinus Torvalds 		p.iph.ttl = 64;
2746d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
27471da177e4SLinus Torvalds 
27485bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
27495bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
27505bc3eb7eSStephen Hemminger 
27515bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
27525bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
27531da177e4SLinus Torvalds 			set_fs(oldfs);
27545bc3eb7eSStephen Hemminger 		} else
27555bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
27561da177e4SLinus Torvalds 
27571da177e4SLinus Torvalds 		if (err == 0) {
27581da177e4SLinus Torvalds 			err = -ENOBUFS;
2759af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2760af284937SDaniel Lezcano 			if (!dev)
27611da177e4SLinus Torvalds 				goto err_exit;
27621da177e4SLinus Torvalds 			err = dev_open(dev);
27631da177e4SLinus Torvalds 		}
27641da177e4SLinus Torvalds 	}
27650be669bbSJoerg Roedel #endif
27661da177e4SLinus Torvalds 
27671da177e4SLinus Torvalds err_exit:
27681da177e4SLinus Torvalds 	rtnl_unlock();
27691da177e4SLinus Torvalds 	return err;
27701da177e4SLinus Torvalds }
27711da177e4SLinus Torvalds 
277293a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join,
277393a714d6SMadhu Challa 			  const struct in6_addr *addr, int ifindex)
277493a714d6SMadhu Challa {
277593a714d6SMadhu Challa 	int ret;
277693a714d6SMadhu Challa 
277793a714d6SMadhu Challa 	ASSERT_RTNL();
277893a714d6SMadhu Challa 
277993a714d6SMadhu Challa 	lock_sock(sk);
278093a714d6SMadhu Challa 	if (join)
278154ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_join(sk, ifindex, addr);
278293a714d6SMadhu Challa 	else
278354ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_drop(sk, ifindex, addr);
278493a714d6SMadhu Challa 	release_sock(sk);
278593a714d6SMadhu Challa 
278693a714d6SMadhu Challa 	return ret;
278793a714d6SMadhu Challa }
278893a714d6SMadhu Challa 
27891da177e4SLinus Torvalds /*
27901da177e4SLinus Torvalds  *	Manual configuration of address on an interface
27911da177e4SLinus Torvalds  */
2792479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex,
2793479840ffSJiri Pirko 			  const struct in6_addr *pfx,
2794caeaba79SNicolas Dichtel 			  const struct in6_addr *peer_pfx,
2795479840ffSJiri Pirko 			  unsigned int plen, __u32 ifa_flags,
2796479840ffSJiri Pirko 			  __u32 prefered_lft, __u32 valid_lft)
27971da177e4SLinus Torvalds {
27981da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
27991da177e4SLinus Torvalds 	struct inet6_dev *idev;
28001da177e4SLinus Torvalds 	struct net_device *dev;
280193a714d6SMadhu Challa 	unsigned long timeout;
280293a714d6SMadhu Challa 	clock_t expires;
28031da177e4SLinus Torvalds 	int scope;
28046f704992SYOSHIFUJI Hideaki 	u32 flags;
28051da177e4SLinus Torvalds 
28061da177e4SLinus Torvalds 	ASSERT_RTNL();
28071da177e4SLinus Torvalds 
280824ef0da7SThomas Graf 	if (plen > 128)
280924ef0da7SThomas Graf 		return -EINVAL;
281024ef0da7SThomas Graf 
28110778769dSNoriaki TAKAMIYA 	/* check the lifetime */
28120778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
28130778769dSNoriaki TAKAMIYA 		return -EINVAL;
28140778769dSNoriaki TAKAMIYA 
281553bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
281653bd6749SJiri Pirko 		return -EINVAL;
281753bd6749SJiri Pirko 
2818af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2819af284937SDaniel Lezcano 	if (!dev)
28201da177e4SLinus Torvalds 		return -ENODEV;
28211da177e4SLinus Torvalds 
282264e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
282364e724f6SBrian Haley 	if (IS_ERR(idev))
282464e724f6SBrian Haley 		return PTR_ERR(idev);
28251da177e4SLinus Torvalds 
282693a714d6SMadhu Challa 	if (ifa_flags & IFA_F_MCAUTOJOIN) {
282793a714d6SMadhu Challa 		int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
282893a714d6SMadhu Challa 					 true, pfx, ifindex);
282993a714d6SMadhu Challa 
283093a714d6SMadhu Challa 		if (ret < 0)
283193a714d6SMadhu Challa 			return ret;
283293a714d6SMadhu Challa 	}
283393a714d6SMadhu Challa 
28341da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
28351da177e4SLinus Torvalds 
28364bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
28374bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
28384bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
28394bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
28406f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
28414bed72e4SYOSHIFUJI Hideaki 	} else {
28424bed72e4SYOSHIFUJI Hideaki 		expires = 0;
28434bed72e4SYOSHIFUJI Hideaki 		flags = 0;
28444bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
28456f704992SYOSHIFUJI Hideaki 	}
28460778769dSNoriaki TAKAMIYA 
28474bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
28484bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
28494bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
28500778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
28514bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
28524bed72e4SYOSHIFUJI Hideaki 	}
28530778769dSNoriaki TAKAMIYA 
28548a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
28558a226b2cSJiri Benc 			    valid_lft, prefered_lft);
28560778769dSNoriaki TAKAMIYA 
28571da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
2858761aac73SThomas Haller 		if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
285946d48046SYOSHIFUJI Hideaki 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
28606f704992SYOSHIFUJI Hideaki 					      expires, flags);
2861761aac73SThomas Haller 		}
2862761aac73SThomas Haller 
286395c385b4SNeil Horman 		/*
286495c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
286595c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
286695c385b4SNeil Horman 		 * manually configured addresses
286795c385b4SNeil Horman 		 */
2868cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
286953bd6749SJiri Pirko 		if (ifa_flags & IFA_F_MANAGETEMPADDR)
287053bd6749SJiri Pirko 			manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
287153bd6749SJiri Pirko 					 true, jiffies);
28721da177e4SLinus Torvalds 		in6_ifa_put(ifp);
2873c15b1ccaSHannes Frederic Sowa 		addrconf_verify_rtnl();
28741da177e4SLinus Torvalds 		return 0;
287593a714d6SMadhu Challa 	} else if (ifa_flags & IFA_F_MCAUTOJOIN) {
287693a714d6SMadhu Challa 		ipv6_mc_config(net->ipv6.mc_autojoin_sk,
287793a714d6SMadhu Challa 			       false, pfx, ifindex);
28781da177e4SLinus Torvalds 	}
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds 	return PTR_ERR(ifp);
28811da177e4SLinus Torvalds }
28821da177e4SLinus Torvalds 
28836046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
28846046d5b4SHeiner Kallweit 			  const struct in6_addr *pfx, unsigned int plen)
28851da177e4SLinus Torvalds {
28861da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
28871da177e4SLinus Torvalds 	struct inet6_dev *idev;
28881da177e4SLinus Torvalds 	struct net_device *dev;
28891da177e4SLinus Torvalds 
289024ef0da7SThomas Graf 	if (plen > 128)
289124ef0da7SThomas Graf 		return -EINVAL;
289224ef0da7SThomas Graf 
2893af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2894af284937SDaniel Lezcano 	if (!dev)
28951da177e4SLinus Torvalds 		return -ENODEV;
28961da177e4SLinus Torvalds 
2897e5d08d71SIan Morris 	idev = __in6_dev_get(dev);
289863159f29SIan Morris 	if (!idev)
28991da177e4SLinus Torvalds 		return -ENXIO;
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2902502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
29031da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
29041da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
29051da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
29061da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
29071da177e4SLinus Torvalds 
29086046d5b4SHeiner Kallweit 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
29096046d5b4SHeiner Kallweit 			    (ifa_flags & IFA_F_MANAGETEMPADDR))
29106046d5b4SHeiner Kallweit 				manage_tempaddrs(idev, ifp, 0, 0, false,
29116046d5b4SHeiner Kallweit 						 jiffies);
29121da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
29136046d5b4SHeiner Kallweit 			addrconf_verify_rtnl();
291493a714d6SMadhu Challa 			if (ipv6_addr_is_multicast(pfx)) {
291593a714d6SMadhu Challa 				ipv6_mc_config(net->ipv6.mc_autojoin_sk,
291693a714d6SMadhu Challa 					       false, pfx, dev->ifindex);
291793a714d6SMadhu Challa 			}
29181da177e4SLinus Torvalds 			return 0;
29191da177e4SLinus Torvalds 		}
29201da177e4SLinus Torvalds 	}
29211da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
29221da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
29231da177e4SLinus Torvalds }
29241da177e4SLinus Torvalds 
29251da177e4SLinus Torvalds 
2926af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
29271da177e4SLinus Torvalds {
29281da177e4SLinus Torvalds 	struct in6_ifreq ireq;
29291da177e4SLinus Torvalds 	int err;
29301da177e4SLinus Torvalds 
2931af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
29321da177e4SLinus Torvalds 		return -EPERM;
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
29351da177e4SLinus Torvalds 		return -EFAULT;
29361da177e4SLinus Torvalds 
29371da177e4SLinus Torvalds 	rtnl_lock();
2938caeaba79SNicolas Dichtel 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2939af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2940af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
29411da177e4SLinus Torvalds 	rtnl_unlock();
29421da177e4SLinus Torvalds 	return err;
29431da177e4SLinus Torvalds }
29441da177e4SLinus Torvalds 
2945af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
29461da177e4SLinus Torvalds {
29471da177e4SLinus Torvalds 	struct in6_ifreq ireq;
29481da177e4SLinus Torvalds 	int err;
29491da177e4SLinus Torvalds 
2950af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
29511da177e4SLinus Torvalds 		return -EPERM;
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
29541da177e4SLinus Torvalds 		return -EFAULT;
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds 	rtnl_lock();
29576046d5b4SHeiner Kallweit 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2958af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
29591da177e4SLinus Torvalds 	rtnl_unlock();
29601da177e4SLinus Torvalds 	return err;
29611da177e4SLinus Torvalds }
29621da177e4SLinus Torvalds 
2963b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2964b5f348e5SIlpo Järvinen 		     int plen, int scope)
2965b5f348e5SIlpo Järvinen {
2966b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
2967b5f348e5SIlpo Järvinen 
29688a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, addr, NULL, plen,
296907edd741SHannes Frederic Sowa 			    scope, IFA_F_PERMANENT,
297007edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2971b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
2972b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
2973b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
2974b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
2975764d3be6SPaolo Abeni 		rt_genid_bump_ipv6(dev_net(idev->dev));
2976b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2977b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
2978b5f348e5SIlpo Järvinen 	}
2979b5f348e5SIlpo Järvinen }
2980b5f348e5SIlpo Järvinen 
298107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
29821da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
29831da177e4SLinus Torvalds {
29841da177e4SLinus Torvalds 	struct in6_addr addr;
29851da177e4SLinus Torvalds 	struct net_device *dev;
2986c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
2987929c9cf3SNicolas Dichtel 	int scope, plen;
2988f0e2acfaSNicolas Dichtel 	u32 pflags = 0;
29891da177e4SLinus Torvalds 
29901da177e4SLinus Torvalds 	ASSERT_RTNL();
29911da177e4SLinus Torvalds 
29921da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
29931da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
29941da177e4SLinus Torvalds 
29951da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
29961da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
29971da177e4SLinus Torvalds 		scope = IFA_LINK;
2998929c9cf3SNicolas Dichtel 		plen = 64;
29991da177e4SLinus Torvalds 	} else {
30001da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
3001929c9cf3SNicolas Dichtel 		plen = 96;
3002f0e2acfaSNicolas Dichtel 		pflags |= RTF_NONEXTHOP;
30031da177e4SLinus Torvalds 	}
30041da177e4SLinus Torvalds 
30051da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
3006929c9cf3SNicolas Dichtel 		add_addr(idev, &addr, plen, scope);
3007f0e2acfaSNicolas Dichtel 		addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
30081da177e4SLinus Torvalds 		return;
30091da177e4SLinus Torvalds 	}
30101da177e4SLinus Torvalds 
30116fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
3012e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
30131da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
30141da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds 			int flag = scope;
30171da177e4SLinus Torvalds 
30181da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
30191da177e4SLinus Torvalds 
30201da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
30211da177e4SLinus Torvalds 
30221da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
30231da177e4SLinus Torvalds 					continue;
30241da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
30251da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
30261da177e4SLinus Torvalds 						continue;
30271da177e4SLinus Torvalds 					flag |= IFA_HOST;
30281da177e4SLinus Torvalds 				}
30291da177e4SLinus Torvalds 
3030b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
3031f0e2acfaSNicolas Dichtel 				addrconf_prefix_route(&addr, plen, idev->dev, 0,
3032f0e2acfaSNicolas Dichtel 						      pflags);
30331da177e4SLinus Torvalds 			}
30341da177e4SLinus Torvalds 		}
30351da177e4SLinus Torvalds 	}
30361da177e4SLinus Torvalds }
30370be669bbSJoerg Roedel #endif
30381da177e4SLinus Torvalds 
30391da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
30401da177e4SLinus Torvalds {
30411da177e4SLinus Torvalds 	struct inet6_dev  *idev;
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds 	/* ::1 */
30441da177e4SLinus Torvalds 
30451da177e4SLinus Torvalds 	ASSERT_RTNL();
30461da177e4SLinus Torvalds 
3047e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
304863159f29SIan Morris 	if (!idev) {
304991df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
30501da177e4SLinus Torvalds 		return;
30511da177e4SLinus Torvalds 	}
30521da177e4SLinus Torvalds 
3053b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
30541da177e4SLinus Torvalds }
30551da177e4SLinus Torvalds 
30562ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev,
305764236f3fSHannes Frederic Sowa 			    const struct in6_addr *addr, u32 flags)
30581da177e4SLinus Torvalds {
30591da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
306064236f3fSHannes Frederic Sowa 	u32 addr_flags = flags | IFA_F_PERMANENT;
30611da177e4SLinus Torvalds 
306295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
306335e015e1SMatteo Croce 	if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
306435e015e1SMatteo Croce 	     idev->cnf.optimistic_dad) &&
3065702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
306695c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
306795c385b4SNeil Horman #endif
306895c385b4SNeil Horman 
306907edd741SHannes Frederic Sowa 	ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
307007edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
30711da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
307246d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
3073cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
30741da177e4SLinus Torvalds 		in6_ifa_put(ifp);
30751da177e4SLinus Torvalds 	}
30761da177e4SLinus Torvalds }
30772ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
30781da177e4SLinus Torvalds 
3079622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address)
3080622c81d5SHannes Frederic Sowa {
3081622c81d5SHannes Frederic Sowa 	if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3082622c81d5SHannes Frederic Sowa 		return true;
3083622c81d5SHannes Frederic Sowa 
3084622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0x02005eff) &&
3085622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3086622c81d5SHannes Frederic Sowa 		return true;
3087622c81d5SHannes Frederic Sowa 
3088622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3089622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3090622c81d5SHannes Frederic Sowa 		return true;
3091622c81d5SHannes Frederic Sowa 
3092622c81d5SHannes Frederic Sowa 	return false;
3093622c81d5SHannes Frederic Sowa }
3094622c81d5SHannes Frederic Sowa 
3095622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address,
3096622c81d5SHannes Frederic Sowa 					u8 dad_count,
3097622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev)
3098622c81d5SHannes Frederic Sowa {
3099622c81d5SHannes Frederic Sowa 	static DEFINE_SPINLOCK(lock);
3100622c81d5SHannes Frederic Sowa 	static __u32 digest[SHA_DIGEST_WORDS];
3101622c81d5SHannes Frederic Sowa 	static __u32 workspace[SHA_WORKSPACE_WORDS];
3102622c81d5SHannes Frederic Sowa 
3103622c81d5SHannes Frederic Sowa 	static union {
3104622c81d5SHannes Frederic Sowa 		char __data[SHA_MESSAGE_BYTES];
3105622c81d5SHannes Frederic Sowa 		struct {
3106622c81d5SHannes Frederic Sowa 			struct in6_addr secret;
3107ff40217eSHannes Frederic Sowa 			__be32 prefix[2];
3108622c81d5SHannes Frederic Sowa 			unsigned char hwaddr[MAX_ADDR_LEN];
3109622c81d5SHannes Frederic Sowa 			u8 dad_count;
3110622c81d5SHannes Frederic Sowa 		} __packed;
3111622c81d5SHannes Frederic Sowa 	} data;
3112622c81d5SHannes Frederic Sowa 
3113622c81d5SHannes Frederic Sowa 	struct in6_addr secret;
3114622c81d5SHannes Frederic Sowa 	struct in6_addr temp;
3115622c81d5SHannes Frederic Sowa 	struct net *net = dev_net(idev->dev);
3116622c81d5SHannes Frederic Sowa 
3117622c81d5SHannes Frederic Sowa 	BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3118622c81d5SHannes Frederic Sowa 
3119622c81d5SHannes Frederic Sowa 	if (idev->cnf.stable_secret.initialized)
3120622c81d5SHannes Frederic Sowa 		secret = idev->cnf.stable_secret.secret;
3121622c81d5SHannes Frederic Sowa 	else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3122622c81d5SHannes Frederic Sowa 		secret = net->ipv6.devconf_dflt->stable_secret.secret;
3123622c81d5SHannes Frederic Sowa 	else
3124622c81d5SHannes Frederic Sowa 		return -1;
3125622c81d5SHannes Frederic Sowa 
3126622c81d5SHannes Frederic Sowa retry:
3127622c81d5SHannes Frederic Sowa 	spin_lock_bh(&lock);
3128622c81d5SHannes Frederic Sowa 
3129622c81d5SHannes Frederic Sowa 	sha_init(digest);
3130622c81d5SHannes Frederic Sowa 	memset(&data, 0, sizeof(data));
3131622c81d5SHannes Frederic Sowa 	memset(workspace, 0, sizeof(workspace));
3132622c81d5SHannes Frederic Sowa 	memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3133ff40217eSHannes Frederic Sowa 	data.prefix[0] = address->s6_addr32[0];
3134ff40217eSHannes Frederic Sowa 	data.prefix[1] = address->s6_addr32[1];
3135622c81d5SHannes Frederic Sowa 	data.secret = secret;
3136622c81d5SHannes Frederic Sowa 	data.dad_count = dad_count;
3137622c81d5SHannes Frederic Sowa 
3138622c81d5SHannes Frederic Sowa 	sha_transform(digest, data.__data, workspace);
3139622c81d5SHannes Frederic Sowa 
3140622c81d5SHannes Frederic Sowa 	temp = *address;
3141ff40217eSHannes Frederic Sowa 	temp.s6_addr32[2] = (__force __be32)digest[0];
3142ff40217eSHannes Frederic Sowa 	temp.s6_addr32[3] = (__force __be32)digest[1];
3143622c81d5SHannes Frederic Sowa 
3144622c81d5SHannes Frederic Sowa 	spin_unlock_bh(&lock);
3145622c81d5SHannes Frederic Sowa 
3146622c81d5SHannes Frederic Sowa 	if (ipv6_reserved_interfaceid(temp)) {
3147622c81d5SHannes Frederic Sowa 		dad_count++;
31481855b7c3SHannes Frederic Sowa 		if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3149622c81d5SHannes Frederic Sowa 			return -1;
3150622c81d5SHannes Frederic Sowa 		goto retry;
3151622c81d5SHannes Frederic Sowa 	}
3152622c81d5SHannes Frederic Sowa 
3153622c81d5SHannes Frederic Sowa 	*address = temp;
3154622c81d5SHannes Frederic Sowa 	return 0;
3155622c81d5SHannes Frederic Sowa }
3156622c81d5SHannes Frederic Sowa 
3157cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3158cc9da6ccSBjørn Mork {
3159cc9da6ccSBjørn Mork 	struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3160cc9da6ccSBjørn Mork 
3161cc9da6ccSBjørn Mork 	if (s->initialized)
3162cc9da6ccSBjørn Mork 		return;
3163cc9da6ccSBjørn Mork 	s = &idev->cnf.stable_secret;
3164cc9da6ccSBjørn Mork 	get_random_bytes(&s->secret, sizeof(s->secret));
3165cc9da6ccSBjørn Mork 	s->initialized = true;
3166cc9da6ccSBjørn Mork }
3167cc9da6ccSBjørn Mork 
3168bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3169bc91b0f0SJiri Pirko {
3170bc91b0f0SJiri Pirko 	struct in6_addr addr;
3171bc91b0f0SJiri Pirko 
3172ca254490SDavid Ahern 	/* no link local addresses on L3 master devices */
3173ca254490SDavid Ahern 	if (netif_is_l3_master(idev->dev))
3174ca254490SDavid Ahern 		return;
3175ca254490SDavid Ahern 
3176bc91b0f0SJiri Pirko 	ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3177622c81d5SHannes Frederic Sowa 
3178d35a00b8SFelix Jia 	switch (idev->cnf.addr_gen_mode) {
3179cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_RANDOM:
3180cc9da6ccSBjørn Mork 		ipv6_gen_mode_random_init(idev);
3181cc9da6ccSBjørn Mork 		/* fallthrough */
3182cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3183622c81d5SHannes Frederic Sowa 		if (!ipv6_generate_stable_address(&addr, 0, idev))
318464236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr,
318564236f3fSHannes Frederic Sowa 					       IFA_F_STABLE_PRIVACY);
3186622c81d5SHannes Frederic Sowa 		else if (prefix_route)
3187622c81d5SHannes Frederic Sowa 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3188cc9da6ccSBjørn Mork 		break;
3189cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_EUI64:
3190bc91b0f0SJiri Pirko 		/* addrconf_add_linklocal also adds a prefix_route and we
3191bc91b0f0SJiri Pirko 		 * only need to care about prefix routes if ipv6_generate_eui64
3192bc91b0f0SJiri Pirko 		 * couldn't generate one.
3193bc91b0f0SJiri Pirko 		 */
3194bc91b0f0SJiri Pirko 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
319564236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr, 0);
3196bc91b0f0SJiri Pirko 		else if (prefix_route)
3197bc91b0f0SJiri Pirko 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3198cc9da6ccSBjørn Mork 		break;
3199cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_NONE:
3200cc9da6ccSBjørn Mork 	default:
3201cc9da6ccSBjørn Mork 		/* will not add any link local address */
3202cc9da6ccSBjørn Mork 		break;
3203bc91b0f0SJiri Pirko 	}
3204bc91b0f0SJiri Pirko }
3205bc91b0f0SJiri Pirko 
32061da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
32071da177e4SLinus Torvalds {
32081da177e4SLinus Torvalds 	struct inet6_dev *idev;
32091da177e4SLinus Torvalds 
32101da177e4SLinus Torvalds 	ASSERT_RTNL();
32111da177e4SLinus Torvalds 
321274235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
321374235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
321474235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
321506a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
3216e837735eSNicolas Dichtel 	    (dev->type != ARPHRD_IEEE1394) &&
3217e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_TUNNEL6) &&
3218cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_6LOWPAN) &&
321945ce0fd1SFelix Jia 	    (dev->type != ARPHRD_IP6GRE) &&
322045ce0fd1SFelix Jia 	    (dev->type != ARPHRD_IPGRE) &&
322145ce0fd1SFelix Jia 	    (dev->type != ARPHRD_TUNNEL) &&
3222cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_NONE)) {
322374235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
322474235a25SHerbert Xu 		return;
322574235a25SHerbert Xu 	}
322674235a25SHerbert Xu 
32271da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
322864e724f6SBrian Haley 	if (IS_ERR(idev))
32291da177e4SLinus Torvalds 		return;
32301da177e4SLinus Torvalds 
3231cc9da6ccSBjørn Mork 	/* this device type has no EUI support */
3232cc9da6ccSBjørn Mork 	if (dev->type == ARPHRD_NONE &&
3233d35a00b8SFelix Jia 	    idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3234d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3235cc9da6ccSBjørn Mork 
3236bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, false);
32371da177e4SLinus Torvalds }
32381da177e4SLinus Torvalds 
323907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
32401da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
32411da177e4SLinus Torvalds {
32421da177e4SLinus Torvalds 	struct inet6_dev *idev;
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 	ASSERT_RTNL();
32451da177e4SLinus Torvalds 
32461da177e4SLinus Torvalds 	/*
32471da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
32481da177e4SLinus Torvalds 	 * addresses... we should configure all of
32491da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
32501da177e4SLinus Torvalds 	 */
32511da177e4SLinus Torvalds 
3252e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
325363159f29SIan Morris 	if (!idev) {
325491df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
32551da177e4SLinus Torvalds 		return;
32561da177e4SLinus Torvalds 	}
32571da177e4SLinus Torvalds 
3258c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
3259bc91b0f0SJiri Pirko 		addrconf_addr_gen(idev, false);
3260c7dc89c0SFred L. Templin 		return;
3261c7dc89c0SFred L. Templin 	}
3262c7dc89c0SFred L. Templin 
32631da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
32641da177e4SLinus Torvalds 
326562b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
32661da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
32671da177e4SLinus Torvalds }
32680be669bbSJoerg Roedel #endif
32691da177e4SLinus Torvalds 
327007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3271aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
3272aee80b54Sstephen hemminger {
3273aee80b54Sstephen hemminger 	struct inet6_dev *idev;
3274aee80b54Sstephen hemminger 
3275aee80b54Sstephen hemminger 	ASSERT_RTNL();
3276aee80b54Sstephen hemminger 
3277e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
327863159f29SIan Morris 	if (!idev) {
327991df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
3280aee80b54Sstephen hemminger 		return;
3281aee80b54Sstephen hemminger 	}
3282aee80b54Sstephen hemminger 
3283bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, true);
3284d9e4ce65SHannes Frederic Sowa 	if (dev->flags & IFF_POINTOPOINT)
3285d9e4ce65SHannes Frederic Sowa 		addrconf_add_mroute(dev);
3286aee80b54Sstephen hemminger }
3287aee80b54Sstephen hemminger #endif
3288aee80b54Sstephen hemminger 
3289f1705ec1SDavid Ahern static int fixup_permanent_addr(struct inet6_dev *idev,
3290f1705ec1SDavid Ahern 				struct inet6_ifaddr *ifp)
3291f1705ec1SDavid Ahern {
3292fc882fcfSIdo Schimmel 	/* !rt6i_node means the host route was removed from the
32938048ced9SDavid Ahern 	 * FIB, for example, if 'lo' device is taken down. In that
32948048ced9SDavid Ahern 	 * case regenerate the host route.
32958048ced9SDavid Ahern 	 */
3296fc882fcfSIdo Schimmel 	if (!ifp->rt || !ifp->rt->rt6i_node) {
32978048ced9SDavid Ahern 		struct rt6_info *rt, *prev;
3298f1705ec1SDavid Ahern 
3299f1705ec1SDavid Ahern 		rt = addrconf_dst_alloc(idev, &ifp->addr, false);
330063a4e80bSTobias Klauser 		if (IS_ERR(rt))
3301f1705ec1SDavid Ahern 			return PTR_ERR(rt);
3302f1705ec1SDavid Ahern 
33038048ced9SDavid Ahern 		/* ifp->rt can be accessed outside of rtnl */
33048048ced9SDavid Ahern 		spin_lock(&ifp->lock);
33058048ced9SDavid Ahern 		prev = ifp->rt;
3306f1705ec1SDavid Ahern 		ifp->rt = rt;
33078048ced9SDavid Ahern 		spin_unlock(&ifp->lock);
33088048ced9SDavid Ahern 
33098048ced9SDavid Ahern 		ip6_rt_put(prev);
3310f1705ec1SDavid Ahern 	}
3311f1705ec1SDavid Ahern 
3312f1705ec1SDavid Ahern 	if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3313f1705ec1SDavid Ahern 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3314f1705ec1SDavid Ahern 				      idev->dev, 0, 0);
3315f1705ec1SDavid Ahern 	}
3316f1705ec1SDavid Ahern 
33176d717134SDavid Ahern 	if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3318f1705ec1SDavid Ahern 		addrconf_dad_start(ifp);
3319f1705ec1SDavid Ahern 
3320f1705ec1SDavid Ahern 	return 0;
3321f1705ec1SDavid Ahern }
3322f1705ec1SDavid Ahern 
3323f1705ec1SDavid Ahern static void addrconf_permanent_addr(struct net_device *dev)
3324f1705ec1SDavid Ahern {
3325f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifp, *tmp;
3326f1705ec1SDavid Ahern 	struct inet6_dev *idev;
3327f1705ec1SDavid Ahern 
3328f1705ec1SDavid Ahern 	idev = __in6_dev_get(dev);
3329f1705ec1SDavid Ahern 	if (!idev)
3330f1705ec1SDavid Ahern 		return;
3331f1705ec1SDavid Ahern 
3332f1705ec1SDavid Ahern 	write_lock_bh(&idev->lock);
3333f1705ec1SDavid Ahern 
3334f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3335f1705ec1SDavid Ahern 		if ((ifp->flags & IFA_F_PERMANENT) &&
3336f1705ec1SDavid Ahern 		    fixup_permanent_addr(idev, ifp) < 0) {
3337f1705ec1SDavid Ahern 			write_unlock_bh(&idev->lock);
3338f1705ec1SDavid Ahern 			ipv6_del_addr(ifp);
3339f1705ec1SDavid Ahern 			write_lock_bh(&idev->lock);
3340f1705ec1SDavid Ahern 
3341f1705ec1SDavid Ahern 			net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3342f1705ec1SDavid Ahern 					     idev->dev->name, &ifp->addr);
3343f1705ec1SDavid Ahern 		}
3344f1705ec1SDavid Ahern 	}
3345f1705ec1SDavid Ahern 
3346f1705ec1SDavid Ahern 	write_unlock_bh(&idev->lock);
3347f1705ec1SDavid Ahern }
3348f1705ec1SDavid Ahern 
33491da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
3350351638e7SJiri Pirko 			   void *ptr)
33511da177e4SLinus Torvalds {
3352351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
33534f7f34eaSDavid Ahern 	struct netdev_notifier_changeupper_info *info;
3354748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
335560abc0beSWANG Cong 	struct net *net = dev_net(dev);
3356c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
3357b217d616SHerbert Xu 	int err;
33581da177e4SLinus Torvalds 
33591da177e4SLinus Torvalds 	switch (event) {
336045ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
336174235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
336245ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
3363a317a2f1SWANG Cong 			if (IS_ERR(idev))
3364a317a2f1SWANG Cong 				return notifier_from_errno(PTR_ERR(idev));
336545ba9dd2SYOSHIFUJI Hideaki 		}
336645ba9dd2SYOSHIFUJI Hideaki 		break;
3367bcdd553fSStephen Hemminger 
3368b7b0b1d2SAlexander Duyck 	case NETDEV_CHANGEMTU:
3369b7b0b1d2SAlexander Duyck 		/* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3370b7b0b1d2SAlexander Duyck 		if (dev->mtu < IPV6_MIN_MTU) {
337160abc0beSWANG Cong 			addrconf_ifdown(dev, dev != net->loopback_dev);
3372b7b0b1d2SAlexander Duyck 			break;
3373b7b0b1d2SAlexander Duyck 		}
3374b7b0b1d2SAlexander Duyck 
3375b7b0b1d2SAlexander Duyck 		if (idev) {
3376b7b0b1d2SAlexander Duyck 			rt6_mtu_change(dev, dev->mtu);
3377b7b0b1d2SAlexander Duyck 			idev->cnf.mtu6 = dev->mtu;
3378b7b0b1d2SAlexander Duyck 			break;
3379b7b0b1d2SAlexander Duyck 		}
3380b7b0b1d2SAlexander Duyck 
3381b7b0b1d2SAlexander Duyck 		/* allocate new idev */
3382b7b0b1d2SAlexander Duyck 		idev = ipv6_add_dev(dev);
3383b7b0b1d2SAlexander Duyck 		if (IS_ERR(idev))
3384b7b0b1d2SAlexander Duyck 			break;
3385b7b0b1d2SAlexander Duyck 
3386b7b0b1d2SAlexander Duyck 		/* device is still not ready */
3387b7b0b1d2SAlexander Duyck 		if (!(idev->if_flags & IF_READY))
3388b7b0b1d2SAlexander Duyck 			break;
3389b7b0b1d2SAlexander Duyck 
3390b7b0b1d2SAlexander Duyck 		run_pending = 1;
3391b7b0b1d2SAlexander Duyck 
3392b7b0b1d2SAlexander Duyck 		/* fall through */
3393b7b0b1d2SAlexander Duyck 
33941da177e4SLinus Torvalds 	case NETDEV_UP:
33953c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
3396c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
3397c2edacf8SJay Vosburgh 			break;
3398c2edacf8SJay Vosburgh 
33993ce62a84SWANG Cong 		if (idev && idev->cnf.disable_ipv6)
34003ce62a84SWANG Cong 			break;
34013ce62a84SWANG Cong 
34023c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
340338bd10c4SDavid Ahern 			/* restore routes for permanent addresses */
340438bd10c4SDavid Ahern 			addrconf_permanent_addr(dev);
340538bd10c4SDavid Ahern 
34061f372c7bSMike Manning 			if (!addrconf_link_ready(dev)) {
34073c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
3408f3213831SJoe Perches 				pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
34093c21edbdSYOSHIFUJI Hideaki 					dev->name);
34103c21edbdSYOSHIFUJI Hideaki 				break;
34113c21edbdSYOSHIFUJI Hideaki 			}
341299081049SKristian Slavov 
3413d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
3414d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
3415d31c7b8fSEvgeniy Polyakov 
3416a317a2f1SWANG Cong 			if (!IS_ERR_OR_NULL(idev)) {
341799081049SKristian Slavov 				idev->if_flags |= IF_READY;
3418e3ec6cfcSBenjamin Thery 				run_pending = 1;
3419e3ec6cfcSBenjamin Thery 			}
3420b7b0b1d2SAlexander Duyck 		} else if (event == NETDEV_CHANGE) {
34211f372c7bSMike Manning 			if (!addrconf_link_ready(dev)) {
34223c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
34233c21edbdSYOSHIFUJI Hideaki 				break;
34243c21edbdSYOSHIFUJI Hideaki 			}
34253c21edbdSYOSHIFUJI Hideaki 
34263c21edbdSYOSHIFUJI Hideaki 			if (idev) {
3427a088d1d7SLinus Lüssing 				if (idev->if_flags & IF_READY) {
3428a088d1d7SLinus Lüssing 					/* device is already configured -
3429a088d1d7SLinus Lüssing 					 * but resend MLD reports, we might
3430a088d1d7SLinus Lüssing 					 * have roamed and need to update
3431a088d1d7SLinus Lüssing 					 * multicast snooping switches
3432a088d1d7SLinus Lüssing 					 */
3433a088d1d7SLinus Lüssing 					ipv6_mc_up(idev);
34343c21edbdSYOSHIFUJI Hideaki 					break;
3435a088d1d7SLinus Lüssing 				}
34363c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
34373c21edbdSYOSHIFUJI Hideaki 			}
34383c21edbdSYOSHIFUJI Hideaki 
3439f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
34403c21edbdSYOSHIFUJI Hideaki 				dev->name);
34413c21edbdSYOSHIFUJI Hideaki 
3442c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
34433c21edbdSYOSHIFUJI Hideaki 		}
34443c21edbdSYOSHIFUJI Hideaki 
34451da177e4SLinus Torvalds 		switch (dev->type) {
344607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
34471da177e4SLinus Torvalds 		case ARPHRD_SIT:
34481da177e4SLinus Torvalds 			addrconf_sit_config(dev);
34491da177e4SLinus Torvalds 			break;
34500be669bbSJoerg Roedel #endif
345107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3452aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
3453aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
3454aee80b54Sstephen hemminger 			break;
3455aee80b54Sstephen hemminger #endif
34561da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
34571da177e4SLinus Torvalds 			init_loopback(dev);
34581da177e4SLinus Torvalds 			break;
34591da177e4SLinus Torvalds 
34601da177e4SLinus Torvalds 		default:
34611da177e4SLinus Torvalds 			addrconf_dev_config(dev);
34621da177e4SLinus Torvalds 			break;
34633ff50b79SStephen Hemminger 		}
3464bcdd553fSStephen Hemminger 
3465a317a2f1SWANG Cong 		if (!IS_ERR_OR_NULL(idev)) {
3466c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
3467c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
3468c5e33bddSYOSHIFUJI Hideaki 
3469bcdd553fSStephen Hemminger 			/*
3470bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
3471bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
3472bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
34731da177e4SLinus Torvalds 			 */
3474bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
3475bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
34761da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
34771da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
34781da177e4SLinus Torvalds 			}
34791da177e4SLinus Torvalds 			idev->tstamp = jiffies;
34801da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
3481bcdd553fSStephen Hemminger 
3482bcdd553fSStephen Hemminger 			/*
3483bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
3484bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
34851da177e4SLinus Torvalds 			 */
34861da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
348760abc0beSWANG Cong 				addrconf_ifdown(dev, dev != net->loopback_dev);
34881da177e4SLinus Torvalds 		}
34891da177e4SLinus Torvalds 		break;
34901da177e4SLinus Torvalds 
34911da177e4SLinus Torvalds 	case NETDEV_DOWN:
34921da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
34931da177e4SLinus Torvalds 		/*
34941da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
34951da177e4SLinus Torvalds 		 */
34961da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
34971da177e4SLinus Torvalds 		break;
34983c21edbdSYOSHIFUJI Hideaki 
34991da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
35001da177e4SLinus Torvalds 		if (idev) {
35015632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
3502408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
3503a317a2f1SWANG Cong 			err = addrconf_sysctl_register(idev);
3504b217d616SHerbert Xu 			if (err)
3505b217d616SHerbert Xu 				return notifier_from_errno(err);
3506a317a2f1SWANG Cong 			err = snmp6_register_dev(idev);
3507a317a2f1SWANG Cong 			if (err) {
3508a317a2f1SWANG Cong 				addrconf_sysctl_unregister(idev);
3509a317a2f1SWANG Cong 				return notifier_from_errno(err);
3510a317a2f1SWANG Cong 			}
35115632c515SStephen Hemminger 		}
35121da177e4SLinus Torvalds 		break;
3513bcdd553fSStephen Hemminger 
351493d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
351593d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
35163ef0952cSAndrew Lunn 		if (idev)
351793d9b7d7SJiri Pirko 			addrconf_type_change(dev, event);
351875c78500SMoni Shoua 		break;
35194f7f34eaSDavid Ahern 
35204f7f34eaSDavid Ahern 	case NETDEV_CHANGEUPPER:
35214f7f34eaSDavid Ahern 		info = ptr;
35224f7f34eaSDavid Ahern 
35234f7f34eaSDavid Ahern 		/* flush all routes if dev is linked to or unlinked from
35244f7f34eaSDavid Ahern 		 * an L3 master device (e.g., VRF)
35254f7f34eaSDavid Ahern 		 */
35264f7f34eaSDavid Ahern 		if (info->upper_dev && netif_is_l3_master(info->upper_dev))
35274f7f34eaSDavid Ahern 			addrconf_ifdown(dev, 0);
35283ff50b79SStephen Hemminger 	}
35291da177e4SLinus Torvalds 
35301da177e4SLinus Torvalds 	return NOTIFY_OK;
35311da177e4SLinus Torvalds }
35321da177e4SLinus Torvalds 
35331da177e4SLinus Torvalds /*
35341da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
35351da177e4SLinus Torvalds  */
35361da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
35371da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
3538242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY,
35391da177e4SLinus Torvalds };
35401da177e4SLinus Torvalds 
354193d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
354275c78500SMoni Shoua {
354375c78500SMoni Shoua 	struct inet6_dev *idev;
354475c78500SMoni Shoua 	ASSERT_RTNL();
354575c78500SMoni Shoua 
354675c78500SMoni Shoua 	idev = __in6_dev_get(dev);
354775c78500SMoni Shoua 
354893d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
354975c78500SMoni Shoua 		ipv6_mc_remap(idev);
355093d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
355175c78500SMoni Shoua 		ipv6_mc_unmap(idev);
355275c78500SMoni Shoua }
355375c78500SMoni Shoua 
355470af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr)
355570af921dSDavid Ahern {
355670af921dSDavid Ahern 	return ipv6_addr_type(addr) &
355770af921dSDavid Ahern 		(IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
355870af921dSDavid Ahern }
355970af921dSDavid Ahern 
35601da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
35611da177e4SLinus Torvalds {
3562c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
3563502a2ffdSstephen hemminger 	struct inet6_dev *idev;
3564f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifa, *tmp;
3565f1705ec1SDavid Ahern 	struct list_head del_list;
3566f1705ec1SDavid Ahern 	int _keep_addr;
3567f1705ec1SDavid Ahern 	bool keep_addr;
356873a8bd74SDavid S. Miller 	int state, i;
35691da177e4SLinus Torvalds 
35701da177e4SLinus Torvalds 	ASSERT_RTNL();
35711da177e4SLinus Torvalds 
3572f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
357373a8bd74SDavid S. Miller 	neigh_ifdown(&nd_tbl, dev);
35741da177e4SLinus Torvalds 
35751da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
357663159f29SIan Morris 	if (!idev)
35771da177e4SLinus Torvalds 		return -ENODEV;
35781da177e4SLinus Torvalds 
3579bcdd553fSStephen Hemminger 	/*
3580bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3581bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
35821da177e4SLinus Torvalds 	 */
3583439e2385SDenis V. Lunev 	if (how) {
35841da177e4SLinus Torvalds 		idev->dead = 1;
35858814c4b5SYOSHIFUJI Hideaki 
35868814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3587a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
35881da177e4SLinus Torvalds 
35891da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
35901da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
35911da177e4SLinus Torvalds 
35921da177e4SLinus Torvalds 	}
35931da177e4SLinus Torvalds 
3594f1705ec1SDavid Ahern 	/* aggregate the system setting and interface setting */
3595f1705ec1SDavid Ahern 	_keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3596f1705ec1SDavid Ahern 	if (!_keep_addr)
3597f1705ec1SDavid Ahern 		_keep_addr = idev->cnf.keep_addr_on_down;
3598f1705ec1SDavid Ahern 
3599f1705ec1SDavid Ahern 	/* combine the user config with event to determine if permanent
3600f1705ec1SDavid Ahern 	 * addresses are to be removed from address hash table
3601f1705ec1SDavid Ahern 	 */
3602bc561632SMike Manning 	keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
3603f1705ec1SDavid Ahern 
360473a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
360573a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
360673a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
360773a8bd74SDavid S. Miller 
360873a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
360973a8bd74SDavid S. Miller restart:
3610b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
361173a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
3612c15b1ccaSHannes Frederic Sowa 				addrconf_del_dad_work(ifa);
3613f1705ec1SDavid Ahern 				/* combined flag + permanent flag decide if
3614f1705ec1SDavid Ahern 				 * address is retained on a down event
3615f1705ec1SDavid Ahern 				 */
3616f1705ec1SDavid Ahern 				if (!keep_addr ||
361770af921dSDavid Ahern 				    !(ifa->flags & IFA_F_PERMANENT) ||
361870af921dSDavid Ahern 				    addr_is_local(&ifa->addr)) {
3619f1705ec1SDavid Ahern 					hlist_del_init_rcu(&ifa->addr_lst);
362073a8bd74SDavid S. Miller 					goto restart;
362173a8bd74SDavid S. Miller 				}
362273a8bd74SDavid S. Miller 			}
3623f1705ec1SDavid Ahern 		}
362473a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
362573a8bd74SDavid S. Miller 	}
362673a8bd74SDavid S. Miller 
36271da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
36281da177e4SLinus Torvalds 
3629b7b1bfceSHannes Frederic Sowa 	addrconf_del_rs_timer(idev);
3630b7b1bfceSHannes Frederic Sowa 
3631bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3632439e2385SDenis V. Lunev 	if (!how)
36333c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
36341da177e4SLinus Torvalds 
3635bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3636372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3637372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3638372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3639372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
36401da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
36411da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
36421da177e4SLinus Torvalds 
36431da177e4SLinus Torvalds 		if (ifa->ifpub) {
36441da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
36451da177e4SLinus Torvalds 			ifa->ifpub = NULL;
36461da177e4SLinus Torvalds 		}
36471da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
36481da177e4SLinus Torvalds 		in6_ifa_put(ifa);
36491da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
36501da177e4SLinus Torvalds 	}
36518f37ada5Sstephen hemminger 
3652f1705ec1SDavid Ahern 	/* re-combine the user config with event to determine if permanent
3653f1705ec1SDavid Ahern 	 * addresses are to be removed from the interface list
3654f1705ec1SDavid Ahern 	 */
3655bc561632SMike Manning 	keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
3656f1705ec1SDavid Ahern 
3657f1705ec1SDavid Ahern 	INIT_LIST_HEAD(&del_list);
3658f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
365938bd10c4SDavid Ahern 		struct rt6_info *rt = NULL;
3660a2d6cbb0SRabin Vincent 		bool keep;
366138bd10c4SDavid Ahern 
3662c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifa);
366384e8b803Sstephen hemminger 
3664a2d6cbb0SRabin Vincent 		keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3665a2d6cbb0SRabin Vincent 			!addr_is_local(&ifa->addr);
3666a2d6cbb0SRabin Vincent 		if (!keep)
3667a2d6cbb0SRabin Vincent 			list_move(&ifa->if_list, &del_list);
3668a2d6cbb0SRabin Vincent 
36694c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
36708e8e676dSHannes Frederic Sowa 		spin_lock_bh(&ifa->lock);
3671f1705ec1SDavid Ahern 
3672a2d6cbb0SRabin Vincent 		if (keep) {
3673f1705ec1SDavid Ahern 			/* set state to skip the notifier below */
3674f1705ec1SDavid Ahern 			state = INET6_IFADDR_STATE_DEAD;
36756d717134SDavid Ahern 			ifa->state = INET6_IFADDR_STATE_PREDAD;
3676f1705ec1SDavid Ahern 			if (!(ifa->flags & IFA_F_NODAD))
3677f1705ec1SDavid Ahern 				ifa->flags |= IFA_F_TENTATIVE;
367838bd10c4SDavid Ahern 
367938bd10c4SDavid Ahern 			rt = ifa->rt;
368038bd10c4SDavid Ahern 			ifa->rt = NULL;
3681f1705ec1SDavid Ahern 		} else {
36824c5ff6a6SHerbert Xu 			state = ifa->state;
36834c5ff6a6SHerbert Xu 			ifa->state = INET6_IFADDR_STATE_DEAD;
3684f1705ec1SDavid Ahern 		}
3685f1705ec1SDavid Ahern 
36868e8e676dSHannes Frederic Sowa 		spin_unlock_bh(&ifa->lock);
36874c5ff6a6SHerbert Xu 
368838bd10c4SDavid Ahern 		if (rt)
368938bd10c4SDavid Ahern 			ip6_del_rt(rt);
369038bd10c4SDavid Ahern 
36919d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
36921da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
3693f88c91ddSCong Wang 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3694ea06f717SMike Manning 		} else {
3695ea06f717SMike Manning 			if (idev->cnf.forwarding)
3696ea06f717SMike Manning 				addrconf_leave_anycast(ifa);
3697ea06f717SMike Manning 			addrconf_leave_solict(ifa->idev, &ifa->addr);
36982de79570SLorenzo Colitti 		}
369973a8bd74SDavid S. Miller 
37001da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
37011da177e4SLinus Torvalds 	}
37028f37ada5Sstephen hemminger 
37031da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
37041da177e4SLinus Torvalds 
3705f1705ec1SDavid Ahern 	/* now clean up addresses to be removed */
3706f1705ec1SDavid Ahern 	while (!list_empty(&del_list)) {
3707f1705ec1SDavid Ahern 		ifa = list_first_entry(&del_list,
3708f1705ec1SDavid Ahern 				       struct inet6_ifaddr, if_list);
3709f1705ec1SDavid Ahern 		list_del(&ifa->if_list);
3710f1705ec1SDavid Ahern 
3711f1705ec1SDavid Ahern 		in6_ifa_put(ifa);
3712f1705ec1SDavid Ahern 	}
3713f1705ec1SDavid Ahern 
3714381f4dcaSSabrina Dubroca 	/* Step 5: Discard anycast and multicast list */
3715381f4dcaSSabrina Dubroca 	if (how) {
3716381f4dcaSSabrina Dubroca 		ipv6_ac_destroy_dev(idev);
37171da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
3718381f4dcaSSabrina Dubroca 	} else {
37191da177e4SLinus Torvalds 		ipv6_mc_down(idev);
3720381f4dcaSSabrina Dubroca 	}
37211da177e4SLinus Torvalds 
37221da177e4SLinus Torvalds 	idev->tstamp = jiffies;
37231da177e4SLinus Torvalds 
3724bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3725439e2385SDenis V. Lunev 	if (how) {
3726408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
37271da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
37281da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
37291da177e4SLinus Torvalds 		in6_dev_put(idev);
37301da177e4SLinus Torvalds 	}
37311da177e4SLinus Torvalds 	return 0;
37321da177e4SLinus Torvalds }
37331da177e4SLinus Torvalds 
37341da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
37351da177e4SLinus Torvalds {
3736b7b1bfceSHannes Frederic Sowa 	struct inet6_dev *idev = (struct inet6_dev *)data;
3737caf92bc4SCong Wang 	struct net_device *dev = idev->dev;
3738b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
37391da177e4SLinus Torvalds 
3740b7b1bfceSHannes Frederic Sowa 	write_lock(&idev->lock);
37415b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
37421da177e4SLinus Torvalds 		goto out;
37431da177e4SLinus Torvalds 
37449ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
37451da177e4SLinus Torvalds 		goto out;
37465b2a1953Sstephen hemminger 
37475b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
37485b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
37495b2a1953Sstephen hemminger 		goto out;
37501da177e4SLinus Torvalds 
3751bd11f074SMaciej Żenczykowski 	if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3752caf92bc4SCong Wang 		write_unlock(&idev->lock);
3753caf92bc4SCong Wang 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3754caf92bc4SCong Wang 			ndisc_send_rs(dev, &lladdr,
3755b7b1bfceSHannes Frederic Sowa 				      &in6addr_linklocal_allrouters);
3756b7b1bfceSHannes Frederic Sowa 		else
3757caf92bc4SCong Wang 			goto put;
3758b7b1bfceSHannes Frederic Sowa 
3759caf92bc4SCong Wang 		write_lock(&idev->lock);
3760bd11f074SMaciej Żenczykowski 		idev->rs_interval = rfc3315_s14_backoff_update(
3761bd11f074SMaciej Żenczykowski 			idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
37621da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
3763b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3764b7b1bfceSHannes Frederic Sowa 					     idev->cnf.rtr_solicits) ?
37655b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_delay :
3766bd11f074SMaciej Żenczykowski 				      idev->rs_interval);
37671da177e4SLinus Torvalds 	} else {
37681da177e4SLinus Torvalds 		/*
37691da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
37701da177e4SLinus Torvalds 		 * assumption any longer.
37711da177e4SLinus Torvalds 		 */
377291df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
37731da177e4SLinus Torvalds 	}
37741da177e4SLinus Torvalds 
37751da177e4SLinus Torvalds out:
3776b7b1bfceSHannes Frederic Sowa 	write_unlock(&idev->lock);
3777caf92bc4SCong Wang put:
3778b7b1bfceSHannes Frederic Sowa 	in6_dev_put(idev);
37791da177e4SLinus Torvalds }
37801da177e4SLinus Torvalds 
37811da177e4SLinus Torvalds /*
37821da177e4SLinus Torvalds  *	Duplicate Address Detection
37831da177e4SLinus Torvalds  */
37843c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
37853c21edbdSYOSHIFUJI Hideaki {
37863c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
37873c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
3788adc176c5SErik Nordmark 	u64 nonce;
37893c21edbdSYOSHIFUJI Hideaki 
379095c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
379195c385b4SNeil Horman 		rand_num = 0;
379295c385b4SNeil Horman 	else
379363862b5bSAruna-Hewapathirane 		rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
379495c385b4SNeil Horman 
3795adc176c5SErik Nordmark 	nonce = 0;
3796adc176c5SErik Nordmark 	if (idev->cnf.enhanced_dad ||
3797adc176c5SErik Nordmark 	    dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3798adc176c5SErik Nordmark 		do
3799adc176c5SErik Nordmark 			get_random_bytes(&nonce, 6);
3800adc176c5SErik Nordmark 		while (nonce == 0);
3801adc176c5SErik Nordmark 	}
3802adc176c5SErik Nordmark 	ifp->dad_nonce = nonce;
3803b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes = idev->cnf.dad_transmits;
3804c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, rand_num);
38053c21edbdSYOSHIFUJI Hideaki }
38063c21edbdSYOSHIFUJI Hideaki 
3807c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
38081da177e4SLinus Torvalds {
38091da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
38101da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3811764d3be6SPaolo Abeni 	bool bump_id, notify = false;
38121da177e4SLinus Torvalds 
38131da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
38141da177e4SLinus Torvalds 
381563862b5bSAruna-Hewapathirane 	prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
38161da177e4SLinus Torvalds 
38171da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3818622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3819e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
38201da177e4SLinus Torvalds 		goto out;
38211da177e4SLinus Torvalds 
38221da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
382335e015e1SMatteo Croce 	    dev_net(dev)->ipv6.devconf_all->accept_dad < 1 ||
38241b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
382555ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
382655ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3827764d3be6SPaolo Abeni 		bump_id = ifp->flags & IFA_F_TENTATIVE;
3828cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
382921809fafSstephen hemminger 		spin_unlock(&ifp->lock);
38301da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
38311da177e4SLinus Torvalds 
3832764d3be6SPaolo Abeni 		addrconf_dad_completed(ifp, bump_id);
38331da177e4SLinus Torvalds 		return;
38341da177e4SLinus Torvalds 	}
38351da177e4SLinus Torvalds 
38366732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
383721809fafSstephen hemminger 		spin_unlock(&ifp->lock);
38386732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
38393c21edbdSYOSHIFUJI Hideaki 		/*
3840590a9887SMasatake YAMATO 		 * If the device is not ready:
38413c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
38423c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
38433c21edbdSYOSHIFUJI Hideaki 		 */
38443c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
3845cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
38466732badeSYOSHIFUJI Hideaki 		return;
38473c21edbdSYOSHIFUJI Hideaki 	}
384895c385b4SNeil Horman 
384995c385b4SNeil Horman 	/*
385095c385b4SNeil Horman 	 * Optimistic nodes can start receiving
385195c385b4SNeil Horman 	 * Frames right away
385295c385b4SNeil Horman 	 */
38537fd2561eSErik Kline 	if (ifp->flags & IFA_F_OPTIMISTIC) {
385495c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
385535e015e1SMatteo Croce 		if (ipv6_use_optimistic_addr(dev_net(dev), idev)) {
38567fd2561eSErik Kline 			/* Because optimistic nodes can use this address,
38577fd2561eSErik Kline 			 * notify listeners. If DAD fails, RTM_DELADDR is sent.
38587fd2561eSErik Kline 			 */
385916186a82Ssubashab@codeaurora.org 			notify = true;
38607fd2561eSErik Kline 		}
38617fd2561eSErik Kline 	}
386295c385b4SNeil Horman 
38636732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
38641da177e4SLinus Torvalds out:
3865622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
38661da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
386716186a82Ssubashab@codeaurora.org 	if (notify)
386816186a82Ssubashab@codeaurora.org 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
38691da177e4SLinus Torvalds }
38701da177e4SLinus Torvalds 
3871c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp)
38721da177e4SLinus Torvalds {
3873c15b1ccaSHannes Frederic Sowa 	bool begin_dad = false;
3874c15b1ccaSHannes Frederic Sowa 
38758e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3876c15b1ccaSHannes Frederic Sowa 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3877c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_PREDAD;
3878c15b1ccaSHannes Frederic Sowa 		begin_dad = true;
3879c15b1ccaSHannes Frederic Sowa 	}
38808e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3881c15b1ccaSHannes Frederic Sowa 
3882c15b1ccaSHannes Frederic Sowa 	if (begin_dad)
3883c15b1ccaSHannes Frederic Sowa 		addrconf_mod_dad_work(ifp, 0);
3884c15b1ccaSHannes Frederic Sowa }
3885c15b1ccaSHannes Frederic Sowa 
3886c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w)
3887c15b1ccaSHannes Frederic Sowa {
3888c15b1ccaSHannes Frederic Sowa 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3889c15b1ccaSHannes Frederic Sowa 						struct inet6_ifaddr,
3890c15b1ccaSHannes Frederic Sowa 						dad_work);
38911da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
3892764d3be6SPaolo Abeni 	bool bump_id, disable_ipv6 = false;
38931da177e4SLinus Torvalds 	struct in6_addr mcaddr;
38941da177e4SLinus Torvalds 
3895c15b1ccaSHannes Frederic Sowa 	enum {
3896c15b1ccaSHannes Frederic Sowa 		DAD_PROCESS,
3897c15b1ccaSHannes Frederic Sowa 		DAD_BEGIN,
3898c15b1ccaSHannes Frederic Sowa 		DAD_ABORT,
3899c15b1ccaSHannes Frederic Sowa 	} action = DAD_PROCESS;
3900c15b1ccaSHannes Frederic Sowa 
3901c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
3902c15b1ccaSHannes Frederic Sowa 
39038e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3904c15b1ccaSHannes Frederic Sowa 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3905c15b1ccaSHannes Frederic Sowa 		action = DAD_BEGIN;
3906c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_DAD;
3907c15b1ccaSHannes Frederic Sowa 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3908c15b1ccaSHannes Frederic Sowa 		action = DAD_ABORT;
3909c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
391085b51b12SMike Manning 
391135e015e1SMatteo Croce 		if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
391235e015e1SMatteo Croce 		     idev->cnf.accept_dad > 1) &&
391335e015e1SMatteo Croce 		    !idev->cnf.disable_ipv6 &&
391485b51b12SMike Manning 		    !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
391585b51b12SMike Manning 			struct in6_addr addr;
391685b51b12SMike Manning 
391785b51b12SMike Manning 			addr.s6_addr32[0] = htonl(0xfe800000);
391885b51b12SMike Manning 			addr.s6_addr32[1] = 0;
391985b51b12SMike Manning 
392085b51b12SMike Manning 			if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
392185b51b12SMike Manning 			    ipv6_addr_equal(&ifp->addr, &addr)) {
392285b51b12SMike Manning 				/* DAD failed for link-local based on MAC */
392385b51b12SMike Manning 				idev->cnf.disable_ipv6 = 1;
392485b51b12SMike Manning 
392585b51b12SMike Manning 				pr_info("%s: IPv6 being disabled!\n",
392685b51b12SMike Manning 					ifp->idev->dev->name);
392785b51b12SMike Manning 				disable_ipv6 = true;
392885b51b12SMike Manning 			}
392985b51b12SMike Manning 		}
3930c15b1ccaSHannes Frederic Sowa 	}
39318e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3932c15b1ccaSHannes Frederic Sowa 
3933c15b1ccaSHannes Frederic Sowa 	if (action == DAD_BEGIN) {
3934c15b1ccaSHannes Frederic Sowa 		addrconf_dad_begin(ifp);
3935c15b1ccaSHannes Frederic Sowa 		goto out;
3936c15b1ccaSHannes Frederic Sowa 	} else if (action == DAD_ABORT) {
3937751eb6b6SWei Yongjun 		in6_ifa_hold(ifp);
3938c15b1ccaSHannes Frederic Sowa 		addrconf_dad_stop(ifp, 1);
393985b51b12SMike Manning 		if (disable_ipv6)
394085b51b12SMike Manning 			addrconf_ifdown(idev->dev, 0);
3941c15b1ccaSHannes Frederic Sowa 		goto out;
3942c15b1ccaSHannes Frederic Sowa 	}
3943c15b1ccaSHannes Frederic Sowa 
3944b7b1bfceSHannes Frederic Sowa 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
3945f2344a13SHerbert Xu 		goto out;
3946f2344a13SHerbert Xu 
3947c15b1ccaSHannes Frederic Sowa 	write_lock_bh(&idev->lock);
3948122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
3949c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
39501da177e4SLinus Torvalds 		goto out;
39511da177e4SLinus Torvalds 	}
395221809fafSstephen hemminger 
395321809fafSstephen hemminger 	spin_lock(&ifp->lock);
3954622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3955622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
3956c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
3957622ccdf1SHerbert Xu 		goto out;
3958622ccdf1SHerbert Xu 	}
3959622ccdf1SHerbert Xu 
3960b7b1bfceSHannes Frederic Sowa 	if (ifp->dad_probes == 0) {
39611da177e4SLinus Torvalds 		/*
39621da177e4SLinus Torvalds 		 * DAD was successful
39631da177e4SLinus Torvalds 		 */
39641da177e4SLinus Torvalds 
3965764d3be6SPaolo Abeni 		bump_id = ifp->flags & IFA_F_TENTATIVE;
3966cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
396721809fafSstephen hemminger 		spin_unlock(&ifp->lock);
3968c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
39691da177e4SLinus Torvalds 
3970764d3be6SPaolo Abeni 		addrconf_dad_completed(ifp, bump_id);
39711da177e4SLinus Torvalds 
39721da177e4SLinus Torvalds 		goto out;
39731da177e4SLinus Torvalds 	}
39741da177e4SLinus Torvalds 
3975b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes--;
3976c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp,
39771f9248e5SJiri Pirko 			      NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
397821809fafSstephen hemminger 	spin_unlock(&ifp->lock);
3979c15b1ccaSHannes Frederic Sowa 	write_unlock_bh(&idev->lock);
39801da177e4SLinus Torvalds 
39811da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
39821da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3983adc176c5SErik Nordmark 	ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
3984adc176c5SErik Nordmark 		      ifp->dad_nonce);
39851da177e4SLinus Torvalds out:
39861da177e4SLinus Torvalds 	in6_ifa_put(ifp);
3987c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
39881da177e4SLinus Torvalds }
39891da177e4SLinus Torvalds 
399011ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */
399111ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
399211ffff75SHannes Frederic Sowa {
399311ffff75SHannes Frederic Sowa 	struct inet6_ifaddr *ifpiter;
399411ffff75SHannes Frederic Sowa 	struct inet6_dev *idev = ifp->idev;
399511ffff75SHannes Frederic Sowa 
3996602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3997602582caSHannes Frederic Sowa 		if (ifpiter->scope > IFA_LINK)
3998602582caSHannes Frederic Sowa 			break;
399911ffff75SHannes Frederic Sowa 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
400011ffff75SHannes Frederic Sowa 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
400111ffff75SHannes Frederic Sowa 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
400211ffff75SHannes Frederic Sowa 		    IFA_F_PERMANENT)
400311ffff75SHannes Frederic Sowa 			return false;
400411ffff75SHannes Frederic Sowa 	}
400511ffff75SHannes Frederic Sowa 	return true;
400611ffff75SHannes Frederic Sowa }
400711ffff75SHannes Frederic Sowa 
4008764d3be6SPaolo Abeni static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
40091da177e4SLinus Torvalds {
40101da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
4011b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
4012b173ee48SHannes Frederic Sowa 	bool send_rs, send_mld;
4013b7b1bfceSHannes Frederic Sowa 
4014c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
40151da177e4SLinus Torvalds 
40161da177e4SLinus Torvalds 	/*
40171da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
40181da177e4SLinus Torvalds 	 */
40191da177e4SLinus Torvalds 
40201da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
40211da177e4SLinus Torvalds 
4022026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
4023026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
40241da177e4SLinus Torvalds 	 */
40251da177e4SLinus Torvalds 
40261ec047ebSHannes Frederic Sowa 	read_lock_bh(&ifp->idev->lock);
402711ffff75SHannes Frederic Sowa 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4028b173ee48SHannes Frederic Sowa 	send_rs = send_mld &&
4029b173ee48SHannes Frederic Sowa 		  ipv6_accept_ra(ifp->idev) &&
4030bd11f074SMaciej Żenczykowski 		  ifp->idev->cnf.rtr_solicits != 0 &&
4031b173ee48SHannes Frederic Sowa 		  (dev->flags&IFF_LOOPBACK) == 0;
40321ec047ebSHannes Frederic Sowa 	read_unlock_bh(&ifp->idev->lock);
40331ec047ebSHannes Frederic Sowa 
4034b173ee48SHannes Frederic Sowa 	/* While dad is in progress mld report's source address is in6_addrany.
4035b173ee48SHannes Frederic Sowa 	 * Resend with proper ll now.
4036b173ee48SHannes Frederic Sowa 	 */
4037b173ee48SHannes Frederic Sowa 	if (send_mld)
4038b173ee48SHannes Frederic Sowa 		ipv6_mc_dad_complete(ifp->idev);
4039b173ee48SHannes Frederic Sowa 
40401ec047ebSHannes Frederic Sowa 	if (send_rs) {
40411da177e4SLinus Torvalds 		/*
40421da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
40431da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
40441da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
40451da177e4SLinus Torvalds 		 */
40461ec047ebSHannes Frederic Sowa 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4047b7b1bfceSHannes Frederic Sowa 			return;
40481ec047ebSHannes Frederic Sowa 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
40491da177e4SLinus Torvalds 
4050b7b1bfceSHannes Frederic Sowa 		write_lock_bh(&ifp->idev->lock);
4051b7b1bfceSHannes Frederic Sowa 		spin_lock(&ifp->lock);
4052bd11f074SMaciej Żenczykowski 		ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4053bd11f074SMaciej Żenczykowski 			ifp->idev->cnf.rtr_solicit_interval);
4054b7b1bfceSHannes Frederic Sowa 		ifp->idev->rs_probes = 1;
40551da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
4056bd11f074SMaciej Żenczykowski 		addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4057b7b1bfceSHannes Frederic Sowa 		spin_unlock(&ifp->lock);
4058b7b1bfceSHannes Frederic Sowa 		write_unlock_bh(&ifp->idev->lock);
40591da177e4SLinus Torvalds 	}
4060764d3be6SPaolo Abeni 
4061764d3be6SPaolo Abeni 	if (bump_id)
4062764d3be6SPaolo Abeni 		rt_genid_bump_ipv6(dev_net(dev));
4063a11a7f71SMarcus Huewe 
4064a11a7f71SMarcus Huewe 	/* Make sure that a new temporary address will be created
4065a11a7f71SMarcus Huewe 	 * before this temporary address becomes deprecated.
4066a11a7f71SMarcus Huewe 	 */
4067a11a7f71SMarcus Huewe 	if (ifp->flags & IFA_F_TEMPORARY)
4068a11a7f71SMarcus Huewe 		addrconf_verify_rtnl();
40691da177e4SLinus Torvalds }
40701da177e4SLinus Torvalds 
4071e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev)
4072e21e8467SStephen Hemminger {
4073c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
4074c5e33bddSYOSHIFUJI Hideaki 
4075c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
4076502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
407721809fafSstephen hemminger 		spin_lock(&ifp->lock);
4078f2344a13SHerbert Xu 		if (ifp->flags & IFA_F_TENTATIVE &&
4079f2344a13SHerbert Xu 		    ifp->state == INET6_IFADDR_STATE_DAD)
4080c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
4081f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
4082c5e33bddSYOSHIFUJI Hideaki 	}
4083c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
4084c5e33bddSYOSHIFUJI Hideaki }
4085c5e33bddSYOSHIFUJI Hideaki 
40861da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
40871da177e4SLinus Torvalds struct if6_iter_state {
40883c40090aSDaniel Lezcano 	struct seq_net_private p;
40891da177e4SLinus Torvalds 	int bucket;
40901d578303SMihai Maruseac 	int offset;
40911da177e4SLinus Torvalds };
40921da177e4SLinus Torvalds 
40931d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
40941da177e4SLinus Torvalds {
40951da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
40961da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
40971218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
40981d578303SMihai Maruseac 	int p = 0;
40991da177e4SLinus Torvalds 
41001d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
41011d578303SMihai Maruseac 	if (pos == 0) {
41021d578303SMihai Maruseac 		state->bucket = 0;
41031d578303SMihai Maruseac 		state->offset = 0;
41041d578303SMihai Maruseac 	}
41051d578303SMihai Maruseac 
41061d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4107b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
41081d578303SMihai Maruseac 					 addr_lst) {
41099f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
41109f0d3c27SEric Dumazet 				continue;
41111d578303SMihai Maruseac 			/* sync with offset */
41121d578303SMihai Maruseac 			if (p < state->offset) {
41131d578303SMihai Maruseac 				p++;
41141d578303SMihai Maruseac 				continue;
41151d578303SMihai Maruseac 			}
41161d578303SMihai Maruseac 			state->offset++;
41171da177e4SLinus Torvalds 			return ifa;
41181da177e4SLinus Torvalds 		}
41191d578303SMihai Maruseac 
41201d578303SMihai Maruseac 		/* prepare for next bucket */
41211d578303SMihai Maruseac 		state->offset = 0;
41221d578303SMihai Maruseac 		p = 0;
41231d578303SMihai Maruseac 	}
4124c2e21293Sstephen hemminger 	return NULL;
4125c2e21293Sstephen hemminger }
41261da177e4SLinus Torvalds 
4127c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4128c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
41291da177e4SLinus Torvalds {
41301da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
41311218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
41321da177e4SLinus Torvalds 
4133b67bfe0dSSasha Levin 	hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
41349f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
41359f0d3c27SEric Dumazet 			continue;
41361d578303SMihai Maruseac 		state->offset++;
41371da177e4SLinus Torvalds 		return ifa;
41381d578303SMihai Maruseac 	}
41391da177e4SLinus Torvalds 
4140c2e21293Sstephen hemminger 	while (++state->bucket < IN6_ADDR_HSIZE) {
41411d578303SMihai Maruseac 		state->offset = 0;
4142b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa,
4143c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
41449f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
41459f0d3c27SEric Dumazet 				continue;
41461d578303SMihai Maruseac 			state->offset++;
4147c2e21293Sstephen hemminger 			return ifa;
4148c2e21293Sstephen hemminger 		}
4149c2e21293Sstephen hemminger 	}
4150c2e21293Sstephen hemminger 
4151c2e21293Sstephen hemminger 	return NULL;
4152c2e21293Sstephen hemminger }
4153c2e21293Sstephen hemminger 
41541da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
41554f70eccaSEric Dumazet 	__acquires(rcu_bh)
41561da177e4SLinus Torvalds {
41575c578aedSstephen hemminger 	rcu_read_lock_bh();
41581d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
41591da177e4SLinus Torvalds }
41601da177e4SLinus Torvalds 
41611da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
41621da177e4SLinus Torvalds {
41631da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
41641da177e4SLinus Torvalds 
41651da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
41661da177e4SLinus Torvalds 	++*pos;
41671da177e4SLinus Torvalds 	return ifa;
41681da177e4SLinus Torvalds }
41691da177e4SLinus Torvalds 
41701da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
41714f70eccaSEric Dumazet 	__releases(rcu_bh)
41721da177e4SLinus Torvalds {
41735c578aedSstephen hemminger 	rcu_read_unlock_bh();
41741da177e4SLinus Torvalds }
41751da177e4SLinus Torvalds 
41761da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
41771da177e4SLinus Torvalds {
41781da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4179971a351cSJiri Pirko 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4180b071195dSHarvey Harrison 		   &ifp->addr,
41811da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
41821da177e4SLinus Torvalds 		   ifp->prefix_len,
41831da177e4SLinus Torvalds 		   ifp->scope,
4184971a351cSJiri Pirko 		   (u8) ifp->flags,
41851da177e4SLinus Torvalds 		   ifp->idev->dev->name);
41861da177e4SLinus Torvalds 	return 0;
41871da177e4SLinus Torvalds }
41881da177e4SLinus Torvalds 
418956b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
41901da177e4SLinus Torvalds 	.start	= if6_seq_start,
41911da177e4SLinus Torvalds 	.next	= if6_seq_next,
41921da177e4SLinus Torvalds 	.show	= if6_seq_show,
41931da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
41941da177e4SLinus Torvalds };
41951da177e4SLinus Torvalds 
41961da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
41971da177e4SLinus Torvalds {
41983c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
4199cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
42001da177e4SLinus Torvalds }
42011da177e4SLinus Torvalds 
42029a32144eSArjan van de Ven static const struct file_operations if6_fops = {
42031da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
42041da177e4SLinus Torvalds 	.open		= if6_seq_open,
42051da177e4SLinus Torvalds 	.read		= seq_read,
42061da177e4SLinus Torvalds 	.llseek		= seq_lseek,
42073c40090aSDaniel Lezcano 	.release	= seq_release_net,
42081da177e4SLinus Torvalds };
42091da177e4SLinus Torvalds 
42102c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
42111da177e4SLinus Torvalds {
4212d4beaa66SGao feng 	if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
42131da177e4SLinus Torvalds 		return -ENOMEM;
42141da177e4SLinus Torvalds 	return 0;
42151da177e4SLinus Torvalds }
42161da177e4SLinus Torvalds 
42172c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
42183c40090aSDaniel Lezcano {
4219ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
42203c40090aSDaniel Lezcano }
42213c40090aSDaniel Lezcano 
42223c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
42233c40090aSDaniel Lezcano 	.init = if6_proc_net_init,
42243c40090aSDaniel Lezcano 	.exit = if6_proc_net_exit,
42253c40090aSDaniel Lezcano };
42263c40090aSDaniel Lezcano 
42273c40090aSDaniel Lezcano int __init if6_proc_init(void)
42283c40090aSDaniel Lezcano {
42293c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
42303c40090aSDaniel Lezcano }
42313c40090aSDaniel Lezcano 
42321da177e4SLinus Torvalds void if6_proc_exit(void)
42331da177e4SLinus Torvalds {
42343c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
42351da177e4SLinus Torvalds }
42361da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
42371da177e4SLinus Torvalds 
423807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
42393b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
4240b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
42413b9f9a1cSNoriaki TAKAMIYA {
42423b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
4243c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
4244ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
4245c2e21293Sstephen hemminger 
42465c578aedSstephen hemminger 	rcu_read_lock_bh();
4247b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
4248878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
4249389f6612SDaniel Lezcano 			continue;
4250caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
42513b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
42523b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
42533b9f9a1cSNoriaki TAKAMIYA 			break;
42543b9f9a1cSNoriaki TAKAMIYA 		}
42553b9f9a1cSNoriaki TAKAMIYA 	}
42565c578aedSstephen hemminger 	rcu_read_unlock_bh();
42573b9f9a1cSNoriaki TAKAMIYA 	return ret;
42583b9f9a1cSNoriaki TAKAMIYA }
42593b9f9a1cSNoriaki TAKAMIYA #endif
42603b9f9a1cSNoriaki TAKAMIYA 
42611da177e4SLinus Torvalds /*
42621da177e4SLinus Torvalds  *	Periodic address status verification
42631da177e4SLinus Torvalds  */
42641da177e4SLinus Torvalds 
4265c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void)
42661da177e4SLinus Torvalds {
4267b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
42681da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
42691da177e4SLinus Torvalds 	int i;
42701da177e4SLinus Torvalds 
4271c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4272c15b1ccaSHannes Frederic Sowa 
42735c578aedSstephen hemminger 	rcu_read_lock_bh();
42741da177e4SLinus Torvalds 	now = jiffies;
4275b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
42761da177e4SLinus Torvalds 
4277c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
42781da177e4SLinus Torvalds 
42791da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
42801da177e4SLinus Torvalds restart:
4281c15b1ccaSHannes Frederic Sowa 		hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
42821da177e4SLinus Torvalds 			unsigned long age;
42831da177e4SLinus Torvalds 
4284fad8da3eSYasushi Asano 			/* When setting preferred_lft to a value not zero or
4285fad8da3eSYasushi Asano 			 * infinity, while valid_lft is infinity
4286fad8da3eSYasushi Asano 			 * IFA_F_PERMANENT has a non-infinity life time.
4287fad8da3eSYasushi Asano 			 */
4288fad8da3eSYasushi Asano 			if ((ifp->flags & IFA_F_PERMANENT) &&
4289fad8da3eSYasushi Asano 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
42901da177e4SLinus Torvalds 				continue;
42911da177e4SLinus Torvalds 
42921da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
4293b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
4294b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
42951da177e4SLinus Torvalds 
42968f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
42978f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
42981da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
42991da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
43001da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
43011da177e4SLinus Torvalds 				goto restart;
43028f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
43038f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
43048f27ebb9SYOSHIFUJI Hideaki 				continue;
43051da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
4306a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
43071da177e4SLinus Torvalds 				int deprecate = 0;
43081da177e4SLinus Torvalds 
43091da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
43101da177e4SLinus Torvalds 					deprecate = 1;
43111da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
43121da177e4SLinus Torvalds 				}
43131da177e4SLinus Torvalds 
4314fad8da3eSYasushi Asano 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4315fad8da3eSYasushi Asano 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
43161da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
43171da177e4SLinus Torvalds 
43181da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
43191da177e4SLinus Torvalds 
43201da177e4SLinus Torvalds 				if (deprecate) {
43211da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
43221da177e4SLinus Torvalds 
43231da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
43241da177e4SLinus Torvalds 					in6_ifa_put(ifp);
43251da177e4SLinus Torvalds 					goto restart;
43261da177e4SLinus Torvalds 				}
43271da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
43281da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
432988949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
433088949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
43311f9248e5SJiri Pirko 					NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
433288949cf4Sstephen hemminger 
43331da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
43341da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
43351da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
43361da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
43371da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
43381da177e4SLinus Torvalds 						ifp->regen_count++;
43391da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
43401da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
43411da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
43425c578aedSstephen hemminger 
4343291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
4344291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
4345291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
43461da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
43471da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
43481da177e4SLinus Torvalds 						in6_ifa_put(ifp);
43491da177e4SLinus Torvalds 						goto restart;
43501da177e4SLinus Torvalds 					}
43511da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
43521da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
43531da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
43541da177e4SLinus Torvalds 			} else {
43551da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
43561da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
43571da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
43581da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
43591da177e4SLinus Torvalds 			}
43601da177e4SLinus Torvalds 		}
43611da177e4SLinus Torvalds 	}
43621da177e4SLinus Torvalds 
4363b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
4364b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
4365b2db7564SYOSHIFUJI Hideaki 
4366b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
4367b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4368b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
4369b2db7564SYOSHIFUJI Hideaki 
4370b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4371b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4372b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4373b2db7564SYOSHIFUJI Hideaki 
4374ba3542e1Sdingtianhong 	ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4375ba3542e1Sdingtianhong 	      now, next, next_sec, next_sched);
4376c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
43775c578aedSstephen hemminger 	rcu_read_unlock_bh();
43781da177e4SLinus Torvalds }
43791da177e4SLinus Torvalds 
4380c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w)
4381c15b1ccaSHannes Frederic Sowa {
4382c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
4383c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4384c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
4385c15b1ccaSHannes Frederic Sowa }
4386c15b1ccaSHannes Frederic Sowa 
4387c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void)
4388c15b1ccaSHannes Frederic Sowa {
4389c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4390c15b1ccaSHannes Frederic Sowa }
4391c15b1ccaSHannes Frederic Sowa 
4392caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4393caeaba79SNicolas Dichtel 				     struct in6_addr **peer_pfx)
4394461d8837SThomas Graf {
4395461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
4396461d8837SThomas Graf 
4397caeaba79SNicolas Dichtel 	*peer_pfx = NULL;
4398caeaba79SNicolas Dichtel 
4399461d8837SThomas Graf 	if (addr)
4400461d8837SThomas Graf 		pfx = nla_data(addr);
4401461d8837SThomas Graf 
4402461d8837SThomas Graf 	if (local) {
4403461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4404caeaba79SNicolas Dichtel 			*peer_pfx = pfx;
4405461d8837SThomas Graf 		pfx = nla_data(local);
4406461d8837SThomas Graf 	}
4407461d8837SThomas Graf 
4408461d8837SThomas Graf 	return pfx;
4409461d8837SThomas Graf }
4410461d8837SThomas Graf 
4411ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4412461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
4413461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
4414461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
4415479840ffSJiri Pirko 	[IFA_FLAGS]		= { .len = sizeof(u32) },
4416461d8837SThomas Graf };
4417461d8837SThomas Graf 
44181da177e4SLinus Torvalds static int
4419c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4420c21ef3e3SDavid Ahern 		  struct netlink_ext_ack *extack)
44211da177e4SLinus Torvalds {
44223b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4423b933f716SThomas Graf 	struct ifaddrmsg *ifm;
4424b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4425caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
44266046d5b4SHeiner Kallweit 	u32 ifa_flags;
4427b933f716SThomas Graf 	int err;
44281da177e4SLinus Torvalds 
4429fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4430c21ef3e3SDavid Ahern 			  extack);
4431b933f716SThomas Graf 	if (err < 0)
4432b933f716SThomas Graf 		return err;
4433b933f716SThomas Graf 
4434b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
4435caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
443663159f29SIan Morris 	if (!pfx)
44371da177e4SLinus Torvalds 		return -EINVAL;
44381da177e4SLinus Torvalds 
44396046d5b4SHeiner Kallweit 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
44406046d5b4SHeiner Kallweit 
44416046d5b4SHeiner Kallweit 	/* We ignore other flags so far. */
44426046d5b4SHeiner Kallweit 	ifa_flags &= IFA_F_MANAGETEMPADDR;
44436046d5b4SHeiner Kallweit 
44446046d5b4SHeiner Kallweit 	return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
44456046d5b4SHeiner Kallweit 			      ifm->ifa_prefixlen);
44461da177e4SLinus Torvalds }
44471da177e4SLinus Torvalds 
4448479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
444955ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
4450081bba5bSNoriaki TAKAMIYA {
44516f704992SYOSHIFUJI Hideaki 	u32 flags;
44526f704992SYOSHIFUJI Hideaki 	clock_t expires;
44534bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
445453bd6749SJiri Pirko 	bool was_managetempaddr;
44555b84efecSThomas Haller 	bool had_prefixroute;
445646d48046SYOSHIFUJI Hideaki 
4457c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4458c15b1ccaSHannes Frederic Sowa 
4459081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
4460081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
4461081bba5bSNoriaki TAKAMIYA 
446253bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR &&
446353bd6749SJiri Pirko 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
446453bd6749SJiri Pirko 		return -EINVAL;
446553bd6749SJiri Pirko 
44664bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
44674bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
44684bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
44694bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
44706f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
44714bed72e4SYOSHIFUJI Hideaki 	} else {
44724bed72e4SYOSHIFUJI Hideaki 		expires = 0;
44734bed72e4SYOSHIFUJI Hideaki 		flags = 0;
44744bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
44756f704992SYOSHIFUJI Hideaki 	}
4476081bba5bSNoriaki TAKAMIYA 
44774bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
44784bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
44794bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
448055ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
44814bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
44824bed72e4SYOSHIFUJI Hideaki 	}
4483081bba5bSNoriaki TAKAMIYA 
4484081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
448553bd6749SJiri Pirko 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
44865b84efecSThomas Haller 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
44875b84efecSThomas Haller 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
448853bd6749SJiri Pirko 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4489761aac73SThomas Haller 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4490761aac73SThomas Haller 			IFA_F_NOPREFIXROUTE);
449153bd6749SJiri Pirko 	ifp->flags |= ifa_flags;
4492081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
4493081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
4494081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
4495081bba5bSNoriaki TAKAMIYA 
4496081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
4497081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
4498081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
4499081bba5bSNoriaki TAKAMIYA 
4500761aac73SThomas Haller 	if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
450146d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
45026f704992SYOSHIFUJI Hideaki 				      expires, flags);
45035b84efecSThomas Haller 	} else if (had_prefixroute) {
45045b84efecSThomas Haller 		enum cleanup_prefix_rt_t action;
45055b84efecSThomas Haller 		unsigned long rt_expires;
45065b84efecSThomas Haller 
45075b84efecSThomas Haller 		write_lock_bh(&ifp->idev->lock);
45085b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &rt_expires);
45095b84efecSThomas Haller 		write_unlock_bh(&ifp->idev->lock);
45105b84efecSThomas Haller 
45115b84efecSThomas Haller 		if (action != CLEANUP_PREFIX_RT_NOP) {
45125b84efecSThomas Haller 			cleanup_prefix_route(ifp, rt_expires,
45135b84efecSThomas Haller 				action == CLEANUP_PREFIX_RT_DEL);
45145b84efecSThomas Haller 		}
4515761aac73SThomas Haller 	}
451653bd6749SJiri Pirko 
451753bd6749SJiri Pirko 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
451853bd6749SJiri Pirko 		if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
451953bd6749SJiri Pirko 			valid_lft = prefered_lft = 0;
452053bd6749SJiri Pirko 		manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
452153bd6749SJiri Pirko 				 !was_managetempaddr, jiffies);
452253bd6749SJiri Pirko 	}
452353bd6749SJiri Pirko 
4524c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4525081bba5bSNoriaki TAKAMIYA 
4526081bba5bSNoriaki TAKAMIYA 	return 0;
4527081bba5bSNoriaki TAKAMIYA }
4528081bba5bSNoriaki TAKAMIYA 
4529081bba5bSNoriaki TAKAMIYA static int
4530c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4531c21ef3e3SDavid Ahern 		  struct netlink_ext_ack *extack)
45321da177e4SLinus Torvalds {
45333b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4534461d8837SThomas Graf 	struct ifaddrmsg *ifm;
4535461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4536caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
45377198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
45387198f8ceSThomas Graf 	struct net_device *dev;
453955ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4540479840ffSJiri Pirko 	u32 ifa_flags;
4541461d8837SThomas Graf 	int err;
45421da177e4SLinus Torvalds 
4543fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4544c21ef3e3SDavid Ahern 			  extack);
4545461d8837SThomas Graf 	if (err < 0)
4546461d8837SThomas Graf 		return err;
4547461d8837SThomas Graf 
4548461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
4549caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
455063159f29SIan Morris 	if (!pfx)
45511da177e4SLinus Torvalds 		return -EINVAL;
45521da177e4SLinus Torvalds 
4553461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
45540778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
4555461d8837SThomas Graf 
4556461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
45570778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
4558461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
4559461d8837SThomas Graf 	} else {
4560461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
4561461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
45620778769dSNoriaki TAKAMIYA 	}
45630778769dSNoriaki TAKAMIYA 
4564af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
456563159f29SIan Morris 	if (!dev)
45667198f8ceSThomas Graf 		return -ENODEV;
4567081bba5bSNoriaki TAKAMIYA 
4568479840ffSJiri Pirko 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4569479840ffSJiri Pirko 
457055ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
4571761aac73SThomas Haller 	ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
457293a714d6SMadhu Challa 		     IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
457355ebaef1SNoriaki TAKAMIYA 
45741cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
457563159f29SIan Morris 	if (!ifa) {
45767198f8ceSThomas Graf 		/*
45777198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
4578db9c7c39Sstephen hemminger 		 * userspace already relies on not having to provide this.
45797198f8ceSThomas Graf 		 */
4580caeaba79SNicolas Dichtel 		return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4581af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
4582af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
45831da177e4SLinus Torvalds 	}
45841da177e4SLinus Torvalds 
45857198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
45867198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
45877198f8ceSThomas Graf 		err = -EEXIST;
45887198f8ceSThomas Graf 	else
458955ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
45907198f8ceSThomas Graf 
45917198f8ceSThomas Graf 	in6_ifa_put(ifa);
45927198f8ceSThomas Graf 
45937198f8ceSThomas Graf 	return err;
45947198f8ceSThomas Graf }
45957198f8ceSThomas Graf 
4596479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4597101bb229SThomas Graf 			  u8 scope, int ifindex)
4598101bb229SThomas Graf {
4599101bb229SThomas Graf 	struct ifaddrmsg *ifm;
4600101bb229SThomas Graf 
4601101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
4602101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
4603101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
4604101bb229SThomas Graf 	ifm->ifa_flags = flags;
4605101bb229SThomas Graf 	ifm->ifa_scope = scope;
4606101bb229SThomas Graf 	ifm->ifa_index = ifindex;
4607101bb229SThomas Graf }
4608101bb229SThomas Graf 
460985486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
461085486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
461185486af0SThomas Graf {
461285486af0SThomas Graf 	struct ifa_cacheinfo ci;
461385486af0SThomas Graf 
461418a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
461518a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
461685486af0SThomas Graf 	ci.ifa_prefered = preferred;
461785486af0SThomas Graf 	ci.ifa_valid = valid;
461885486af0SThomas Graf 
461985486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
462085486af0SThomas Graf }
462185486af0SThomas Graf 
4622101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
4623101bb229SThomas Graf {
4624101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
4625101bb229SThomas Graf 		return RT_SCOPE_HOST;
4626101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
4627101bb229SThomas Graf 		return RT_SCOPE_LINK;
4628101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
4629101bb229SThomas Graf 		return RT_SCOPE_SITE;
4630101bb229SThomas Graf 	else
4631101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
4632101bb229SThomas Graf }
4633101bb229SThomas Graf 
46340ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
46350ab6803bSThomas Graf {
4636339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4637caeaba79SNicolas Dichtel 	       + nla_total_size(16) /* IFA_LOCAL */
4638339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
4639479840ffSJiri Pirko 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
4640479840ffSJiri Pirko 	       + nla_total_size(4)  /* IFA_FLAGS */;
46410ab6803bSThomas Graf }
4642c5396a31SYOSHIFUJI Hideaki 
46431da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
464415e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
46451da177e4SLinus Torvalds {
46461da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
464785486af0SThomas Graf 	u32 preferred, valid;
46481da177e4SLinus Torvalds 
464915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
465063159f29SIan Morris 	if (!nlh)
465126932566SPatrick McHardy 		return -EMSGSIZE;
46520ab6803bSThomas Graf 
4653101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4654101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
4655101bb229SThomas Graf 
4656fad8da3eSYasushi Asano 	if (!((ifa->flags&IFA_F_PERMANENT) &&
4657fad8da3eSYasushi Asano 	      (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
465885486af0SThomas Graf 		preferred = ifa->prefered_lft;
465985486af0SThomas Graf 		valid = ifa->valid_lft;
466085486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
46611da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
4662a1faa698SJens Rosenboom 			if (preferred > tval)
466385486af0SThomas Graf 				preferred -= tval;
4664a1faa698SJens Rosenboom 			else
4665a1faa698SJens Rosenboom 				preferred = 0;
4666f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
4667f56619fcSBen Hutchings 				if (valid > tval)
466885486af0SThomas Graf 					valid -= tval;
4669f56619fcSBen Hutchings 				else
4670f56619fcSBen Hutchings 					valid = 0;
4671f56619fcSBen Hutchings 			}
46721da177e4SLinus Torvalds 		}
46731da177e4SLinus Torvalds 	} else {
467485486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
467585486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
46761da177e4SLinus Torvalds 	}
467785486af0SThomas Graf 
46787996c799SCong Wang 	if (!ipv6_addr_any(&ifa->peer_addr)) {
4679930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4680930345eaSJiri Benc 		    nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4681caeaba79SNicolas Dichtel 			goto error;
4682caeaba79SNicolas Dichtel 	} else
4683930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4684caeaba79SNicolas Dichtel 			goto error;
4685caeaba79SNicolas Dichtel 
4686caeaba79SNicolas Dichtel 	if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4687caeaba79SNicolas Dichtel 		goto error;
468885486af0SThomas Graf 
4689479840ffSJiri Pirko 	if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4690479840ffSJiri Pirko 		goto error;
4691479840ffSJiri Pirko 
4692053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4693053c095aSJohannes Berg 	return 0;
4694caeaba79SNicolas Dichtel 
4695caeaba79SNicolas Dichtel error:
4696caeaba79SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
4697caeaba79SNicolas Dichtel 	return -EMSGSIZE;
46981da177e4SLinus Torvalds }
46991da177e4SLinus Torvalds 
47001da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
470115e47304SEric W. Biederman 				u32 portid, u32 seq, int event, u16 flags)
47021da177e4SLinus Torvalds {
47031da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4704101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4705101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
47061da177e4SLinus Torvalds 
47071da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4708101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4709101bb229SThomas Graf 
471015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
471163159f29SIan Morris 	if (!nlh)
471226932566SPatrick McHardy 		return -EMSGSIZE;
47130ab6803bSThomas Graf 
4714101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4715930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
47160ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
471726932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
471826932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
471926932566SPatrick McHardy 		return -EMSGSIZE;
472026932566SPatrick McHardy 	}
472185486af0SThomas Graf 
4722053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4723053c095aSJohannes Berg 	return 0;
47241da177e4SLinus Torvalds }
47251da177e4SLinus Torvalds 
47261da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
472715e47304SEric W. Biederman 				u32 portid, u32 seq, int event, unsigned int flags)
47281da177e4SLinus Torvalds {
47291da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4730101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4731101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
47321da177e4SLinus Torvalds 
47331da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4734101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4735101bb229SThomas Graf 
473615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
473763159f29SIan Morris 	if (!nlh)
473826932566SPatrick McHardy 		return -EMSGSIZE;
47390ab6803bSThomas Graf 
4740101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4741930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
47420ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
474326932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
474426932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
474526932566SPatrick McHardy 		return -EMSGSIZE;
474626932566SPatrick McHardy 	}
474785486af0SThomas Graf 
4748053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4749053c095aSJohannes Berg 	return 0;
47501da177e4SLinus Torvalds }
47511da177e4SLinus Torvalds 
4752e21e8467SStephen Hemminger enum addr_type_t {
47531da177e4SLinus Torvalds 	UNICAST_ADDR,
47541da177e4SLinus Torvalds 	MULTICAST_ADDR,
47551da177e4SLinus Torvalds 	ANYCAST_ADDR,
47561da177e4SLinus Torvalds };
47571da177e4SLinus Torvalds 
4758234b27c3SEric Dumazet /* called with rcu_read_lock() */
4759234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4760234b27c3SEric Dumazet 			  struct netlink_callback *cb, enum addr_type_t type,
4761234b27c3SEric Dumazet 			  int s_ip_idx, int *p_ip_idx)
47621da177e4SLinus Torvalds {
47631da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
47641da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
4765234b27c3SEric Dumazet 	int err = 1;
4766234b27c3SEric Dumazet 	int ip_idx = *p_ip_idx;
47671da177e4SLinus Torvalds 
47681da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
47691da177e4SLinus Torvalds 	switch (type) {
4770502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
4771502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
4772502a2ffdSstephen hemminger 
4773ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
4774502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
4775502a2ffdSstephen hemminger 			if (++ip_idx < s_ip_idx)
47761da177e4SLinus Torvalds 				continue;
47775d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifaddr(skb, ifa,
477815e47304SEric W. Biederman 						NETLINK_CB(cb->skb).portid,
47795d5619b4SYOSHIFUJI Hideaki 						cb->nlh->nlmsg_seq,
47805d5619b4SYOSHIFUJI Hideaki 						RTM_NEWADDR,
47815d5619b4SYOSHIFUJI Hideaki 						NLM_F_MULTI);
4782053c095aSJohannes Berg 			if (err < 0)
4783bcd32326SEric Dumazet 				break;
478463998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
47851da177e4SLinus Torvalds 		}
47861da177e4SLinus Torvalds 		break;
4787502a2ffdSstephen hemminger 	}
47881da177e4SLinus Torvalds 	case MULTICAST_ADDR:
47891da177e4SLinus Torvalds 		/* multicast address */
47901da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
47911da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
47921da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
47931da177e4SLinus Torvalds 				continue;
47945d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifmcaddr(skb, ifmca,
479515e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
47965d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
47975d5619b4SYOSHIFUJI Hideaki 						  RTM_GETMULTICAST,
47985d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4799053c095aSJohannes Berg 			if (err < 0)
4800bcd32326SEric Dumazet 				break;
48011da177e4SLinus Torvalds 		}
48021da177e4SLinus Torvalds 		break;
48031da177e4SLinus Torvalds 	case ANYCAST_ADDR:
48041da177e4SLinus Torvalds 		/* anycast address */
48051da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
48061da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
48071da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
48081da177e4SLinus Torvalds 				continue;
48095d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifacaddr(skb, ifaca,
481015e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
48115d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
48125d5619b4SYOSHIFUJI Hideaki 						  RTM_GETANYCAST,
48135d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4814053c095aSJohannes Berg 			if (err < 0)
4815bcd32326SEric Dumazet 				break;
48161da177e4SLinus Torvalds 		}
48171da177e4SLinus Torvalds 		break;
48181da177e4SLinus Torvalds 	default:
48191da177e4SLinus Torvalds 		break;
48201da177e4SLinus Torvalds 	}
48211da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
4822234b27c3SEric Dumazet 	*p_ip_idx = ip_idx;
4823234b27c3SEric Dumazet 	return err;
4824234b27c3SEric Dumazet }
48255d5619b4SYOSHIFUJI Hideaki 
4826234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4827234b27c3SEric Dumazet 			   enum addr_type_t type)
4828234b27c3SEric Dumazet {
4829234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
4830234b27c3SEric Dumazet 	int h, s_h;
4831234b27c3SEric Dumazet 	int idx, ip_idx;
4832234b27c3SEric Dumazet 	int s_idx, s_ip_idx;
4833234b27c3SEric Dumazet 	struct net_device *dev;
4834234b27c3SEric Dumazet 	struct inet6_dev *idev;
4835234b27c3SEric Dumazet 	struct hlist_head *head;
4836234b27c3SEric Dumazet 
4837234b27c3SEric Dumazet 	s_h = cb->args[0];
4838234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
4839234b27c3SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
4840234b27c3SEric Dumazet 
4841234b27c3SEric Dumazet 	rcu_read_lock();
484263998ac2SNicolas Dichtel 	cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4843234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4844234b27c3SEric Dumazet 		idx = 0;
4845234b27c3SEric Dumazet 		head = &net->dev_index_head[h];
4846b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
4847234b27c3SEric Dumazet 			if (idx < s_idx)
4848234b27c3SEric Dumazet 				goto cont;
48494b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
4850234b27c3SEric Dumazet 				s_ip_idx = 0;
4851234b27c3SEric Dumazet 			ip_idx = 0;
4852e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
4853e21e8467SStephen Hemminger 			if (!idev)
4854234b27c3SEric Dumazet 				goto cont;
4855234b27c3SEric Dumazet 
4856234b27c3SEric Dumazet 			if (in6_dump_addrs(idev, skb, cb, type,
48577b46a644SDavid S. Miller 					   s_ip_idx, &ip_idx) < 0)
4858234b27c3SEric Dumazet 				goto done;
48597562f876SPavel Emelianov cont:
48607562f876SPavel Emelianov 			idx++;
48611da177e4SLinus Torvalds 		}
4862234b27c3SEric Dumazet 	}
4863234b27c3SEric Dumazet done:
4864234b27c3SEric Dumazet 	rcu_read_unlock();
4865234b27c3SEric Dumazet 	cb->args[0] = h;
4866234b27c3SEric Dumazet 	cb->args[1] = idx;
4867234b27c3SEric Dumazet 	cb->args[2] = ip_idx;
4868234b27c3SEric Dumazet 
48691da177e4SLinus Torvalds 	return skb->len;
48701da177e4SLinus Torvalds }
48711da177e4SLinus Torvalds 
48721da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
48731da177e4SLinus Torvalds {
48741da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
4875b854272bSDenis V. Lunev 
48761da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
48771da177e4SLinus Torvalds }
48781da177e4SLinus Torvalds 
48791da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
48801da177e4SLinus Torvalds {
48811da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
4882b854272bSDenis V. Lunev 
48831da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
48841da177e4SLinus Torvalds }
48851da177e4SLinus Torvalds 
48861da177e4SLinus Torvalds 
48871da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
48881da177e4SLinus Torvalds {
48891da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
4890b854272bSDenis V. Lunev 
48911da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
48921da177e4SLinus Torvalds }
48931da177e4SLinus Torvalds 
4894c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4895c21ef3e3SDavid Ahern 			     struct netlink_ext_ack *extack)
48966c223828SNoriaki TAKAMIYA {
48973b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
48981b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
48991b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4900caeaba79SNicolas Dichtel 	struct in6_addr *addr = NULL, *peer;
49016c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
49026c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
49036c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
49046c223828SNoriaki TAKAMIYA 	int err;
49056c223828SNoriaki TAKAMIYA 
4906fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4907c21ef3e3SDavid Ahern 			  extack);
49081b29fc2cSThomas Graf 	if (err < 0)
49091b29fc2cSThomas Graf 		goto errout;
49106c223828SNoriaki TAKAMIYA 
4911caeaba79SNicolas Dichtel 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
491263159f29SIan Morris 	if (!addr) {
49131b29fc2cSThomas Graf 		err = -EINVAL;
49141b29fc2cSThomas Graf 		goto errout;
49151b29fc2cSThomas Graf 	}
49161b29fc2cSThomas Graf 
49171b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
49186c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
49196fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
49206c223828SNoriaki TAKAMIYA 
4921e21e8467SStephen Hemminger 	ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4922e21e8467SStephen Hemminger 	if (!ifa) {
49231b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
49241b29fc2cSThomas Graf 		goto errout;
49256c223828SNoriaki TAKAMIYA 	}
49266c223828SNoriaki TAKAMIYA 
4927e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4928e21e8467SStephen Hemminger 	if (!skb) {
49291b29fc2cSThomas Graf 		err = -ENOBUFS;
49301b29fc2cSThomas Graf 		goto errout_ifa;
49311b29fc2cSThomas Graf 	}
49321b29fc2cSThomas Graf 
493315e47304SEric W. Biederman 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
49346c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
493526932566SPatrick McHardy 	if (err < 0) {
493626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
493726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
493826932566SPatrick McHardy 		kfree_skb(skb);
493926932566SPatrick McHardy 		goto errout_ifa;
494026932566SPatrick McHardy 	}
494115e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
49421b29fc2cSThomas Graf errout_ifa:
49436c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
49441b29fc2cSThomas Graf errout:
49456c223828SNoriaki TAKAMIYA 	return err;
49466c223828SNoriaki TAKAMIYA }
49476c223828SNoriaki TAKAMIYA 
49481da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
49491da177e4SLinus Torvalds {
49501da177e4SLinus Torvalds 	struct sk_buff *skb;
4951c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
49525d620266SThomas Graf 	int err = -ENOBUFS;
49531da177e4SLinus Torvalds 
4954f784ad3dSMahesh Bandewar 	/* Don't send DELADDR notification for TENTATIVE address,
4955f784ad3dSMahesh Bandewar 	 * since NEWADDR notification is sent only after removing
49566819a14eSMike Manning 	 * TENTATIVE flag, if DAD has not failed.
4957f784ad3dSMahesh Bandewar 	 */
49586819a14eSMike Manning 	if (ifa->flags & IFA_F_TENTATIVE && !(ifa->flags & IFA_F_DADFAILED) &&
49596819a14eSMike Manning 	    event == RTM_DELADDR)
4960f784ad3dSMahesh Bandewar 		return;
4961f784ad3dSMahesh Bandewar 
49620ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
496363159f29SIan Morris 	if (!skb)
49645d620266SThomas Graf 		goto errout;
49655d620266SThomas Graf 
49665d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
496726932566SPatrick McHardy 	if (err < 0) {
496826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
496926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
497026932566SPatrick McHardy 		kfree_skb(skb);
497126932566SPatrick McHardy 		goto errout;
497226932566SPatrick McHardy 	}
49731ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
49741ce85fe4SPablo Neira Ayuso 	return;
49755d620266SThomas Graf errout:
49765d620266SThomas Graf 	if (err < 0)
49776fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
49781da177e4SLinus Torvalds }
49791da177e4SLinus Torvalds 
4980b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
49811da177e4SLinus Torvalds 				__s32 *array, int bytes)
49821da177e4SLinus Torvalds {
498304561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
498404561c1fSThomas Graf 
49851da177e4SLinus Torvalds 	memset(array, 0, bytes);
49861da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
49871da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
49881da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
49891da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
49901da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
49911da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
49921da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
49931da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
499493908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
499593908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
4996bd11f074SMaciej Żenczykowski 	array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4997bd11f074SMaciej Żenczykowski 		jiffies_to_msecs(cnf->rtr_solicit_max_interval);
499893908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
499993908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
50001da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
5001fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5002fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5003fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5004fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
50051da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
50061da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
50071da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
50081da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
50091da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
50101da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
501165f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
50128013d1d7SHangbin Liu 	array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
5013c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
5014930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
5015930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
501693908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
501793908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
5018fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
5019bbea124bSJoel Scherpelz 	array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
502009c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
502109c884d4SYOSHIFUJI Hideaki #endif
5022930d6ff2SYOSHIFUJI Hideaki #endif
5023fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
50240bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
502595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
502695c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
50277fd2561eSErik Kline 	array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
502895c385b4SNeil Horman #endif
50297bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
50307bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
50317bc570c8SYOSHIFUJI Hideaki #endif
5032778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
50331b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
5034c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
50355cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5036b800c3b9SHannes Frederic Sowa 	array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5037d9333196SBen Greear 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5038c2943f14SHarout Hedeshian 	array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
503935103d11SAndy Gospodarek 	array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
50403d1bec99SHannes Frederic Sowa 	/* we omit DEVCONF_STABLE_SECRET for now */
50413985e8a3SErik Kline 	array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5042abbc3043SJohannes Berg 	array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
50437a02bf89SJohannes Berg 	array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5044f1705ec1SDavid Ahern 	array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
50451ababebaSDavid Lebrun 	array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5046bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
5047bf355b8dSDavid Lebrun 	array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5048bf355b8dSDavid Lebrun #endif
5049adc176c5SErik Nordmark 	array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5050d35a00b8SFelix Jia 	array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5051df789fe7SDavid Forster 	array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
50521da177e4SLinus Torvalds }
50531da177e4SLinus Torvalds 
5054b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
5055b382b191SThomas Graf {
5056b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
5057b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
5058b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5059b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5060f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5061f53adae4SDaniel Borkmann 	     + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
5062b382b191SThomas Graf }
5063b382b191SThomas Graf 
5064339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
5065339bf98fSThomas Graf {
5066339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5067339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5068339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5069339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
5070339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
50710344338bSAndy Gospodarek 	       + nla_total_size(1) /* IFLA_OPERSTATE */
5072b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5073339bf98fSThomas Graf }
5074c5396a31SYOSHIFUJI Hideaki 
5075be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5076aca05671SJia He 					int bytes)
50777f7d9a6bSHerbert Xu {
50787f7d9a6bSHerbert Xu 	int i;
5079aca05671SJia He 	int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
50807f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
50817f7d9a6bSHerbert Xu 
50827f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
5083aca05671SJia He 	put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5084aca05671SJia He 	for (i = 1; i < ICMP6_MIB_MAX; i++)
5085be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
50867f7d9a6bSHerbert Xu 
5087aca05671SJia He 	memset(&stats[ICMP6_MIB_MAX], 0, pad);
50887f7d9a6bSHerbert Xu }
50897f7d9a6bSHerbert Xu 
5090698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5091a3a77372SRaghavendra K T 					int bytes, size_t syncpoff)
50924ce3c183SEric Dumazet {
5093a3a77372SRaghavendra K T 	int i, c;
5094a3a77372SRaghavendra K T 	u64 buff[IPSTATS_MIB_MAX];
5095a3a77372SRaghavendra K T 	int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5096a3a77372SRaghavendra K T 
50974ce3c183SEric Dumazet 	BUG_ON(pad < 0);
50984ce3c183SEric Dumazet 
5099a3a77372SRaghavendra K T 	memset(buff, 0, sizeof(buff));
5100a3a77372SRaghavendra K T 	buff[0] = IPSTATS_MIB_MAX;
51014ce3c183SEric Dumazet 
5102a3a77372SRaghavendra K T 	for_each_possible_cpu(c) {
5103a3a77372SRaghavendra K T 		for (i = 1; i < IPSTATS_MIB_MAX; i++)
5104a3a77372SRaghavendra K T 			buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5105a3a77372SRaghavendra K T 	}
5106a3a77372SRaghavendra K T 
5107a3a77372SRaghavendra K T 	memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5108a3a77372SRaghavendra K T 	memset(&stats[IPSTATS_MIB_MAX], 0, pad);
51094ce3c183SEric Dumazet }
51104ce3c183SEric Dumazet 
51117f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
51127f7d9a6bSHerbert Xu 			     int bytes)
51137f7d9a6bSHerbert Xu {
51147f7d9a6bSHerbert Xu 	switch (attrtype) {
51157f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
5116a3a77372SRaghavendra K T 		__snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5117a3a77372SRaghavendra K T 				     offsetof(struct ipstats_mib, syncp));
51187f7d9a6bSHerbert Xu 		break;
51197f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
5120aca05671SJia He 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
51217f7d9a6bSHerbert Xu 		break;
51227f7d9a6bSHerbert Xu 	}
51237f7d9a6bSHerbert Xu }
51247f7d9a6bSHerbert Xu 
5125d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5126d5566fd7SSowmini Varadhan 				  u32 ext_filter_mask)
51271da177e4SLinus Torvalds {
5128bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
51291da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
51301da177e4SLinus Torvalds 
5131c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5132c78679e8SDavid S. Miller 		goto nla_put_failure;
51331da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
513424912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
513524912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
51361f9248e5SJiri Pirko 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5137c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5138c78679e8SDavid S. Miller 		goto nla_put_failure;
5139bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
514063159f29SIan Morris 	if (!nla)
514104561c1fSThomas Graf 		goto nla_put_failure;
5142bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
51431da177e4SLinus Torvalds 
5144bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
5145bf99f1bdSYOSHIFUJI Hideaki 
5146d5566fd7SSowmini Varadhan 	if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5147d5566fd7SSowmini Varadhan 		return 0;
5148d5566fd7SSowmini Varadhan 
5149bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
515063159f29SIan Morris 	if (!nla)
5151bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
5152bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5153bf99f1bdSYOSHIFUJI Hideaki 
5154bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
515563159f29SIan Morris 	if (!nla)
5156bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
5157bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
51581da177e4SLinus Torvalds 
5159f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
516063159f29SIan Morris 	if (!nla)
5161f53adae4SDaniel Borkmann 		goto nla_put_failure;
5162bc91b0f0SJiri Pirko 
5163d35a00b8SFelix Jia 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5164bc91b0f0SJiri Pirko 		goto nla_put_failure;
5165bc91b0f0SJiri Pirko 
5166f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
5167f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5168f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
5169f53adae4SDaniel Borkmann 
5170b382b191SThomas Graf 	return 0;
5171b382b191SThomas Graf 
5172b382b191SThomas Graf nla_put_failure:
5173b382b191SThomas Graf 	return -EMSGSIZE;
5174b382b191SThomas Graf }
5175b382b191SThomas Graf 
5176b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev,
5177b1974ed0SArad, Ronen 				     u32 ext_filter_mask)
5178b382b191SThomas Graf {
5179b382b191SThomas Graf 	if (!__in6_dev_get(dev))
5180b382b191SThomas Graf 		return 0;
5181b382b191SThomas Graf 
5182b382b191SThomas Graf 	return inet6_ifla6_size();
5183b382b191SThomas Graf }
5184b382b191SThomas Graf 
5185d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5186d5566fd7SSowmini Varadhan 			      u32 ext_filter_mask)
5187b382b191SThomas Graf {
5188b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
5189b382b191SThomas Graf 
5190b382b191SThomas Graf 	if (!idev)
5191b382b191SThomas Graf 		return -ENODATA;
5192b382b191SThomas Graf 
5193d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5194b382b191SThomas Graf 		return -EMSGSIZE;
5195b382b191SThomas Graf 
5196b382b191SThomas Graf 	return 0;
5197b382b191SThomas Graf }
5198b382b191SThomas Graf 
5199f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5200f53adae4SDaniel Borkmann {
5201f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
5202f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
520347e27d5eSDaniel Borkmann 	bool clear_token, update_rs = false;
5204dc848292SHannes Frederic Sowa 	struct in6_addr ll_addr;
5205f53adae4SDaniel Borkmann 
5206c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
5207c15b1ccaSHannes Frederic Sowa 
520863159f29SIan Morris 	if (!token)
5209f53adae4SDaniel Borkmann 		return -EINVAL;
5210f53adae4SDaniel Borkmann 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5211f53adae4SDaniel Borkmann 		return -EINVAL;
5212f53adae4SDaniel Borkmann 	if (!ipv6_accept_ra(idev))
5213f53adae4SDaniel Borkmann 		return -EINVAL;
5214bd11f074SMaciej Żenczykowski 	if (idev->cnf.rtr_solicits == 0)
5215f53adae4SDaniel Borkmann 		return -EINVAL;
5216f53adae4SDaniel Borkmann 
5217f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5218f53adae4SDaniel Borkmann 
5219f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5220f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5221f53adae4SDaniel Borkmann 
5222f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5223f53adae4SDaniel Borkmann 
522447e27d5eSDaniel Borkmann 	clear_token = ipv6_addr_any(token);
522547e27d5eSDaniel Borkmann 	if (clear_token)
522647e27d5eSDaniel Borkmann 		goto update_lft;
522747e27d5eSDaniel Borkmann 
5228dc848292SHannes Frederic Sowa 	if (!idev->dead && (idev->if_flags & IF_READY) &&
5229dc848292SHannes Frederic Sowa 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5230dc848292SHannes Frederic Sowa 			     IFA_F_OPTIMISTIC)) {
5231fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
5232fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
5233fc403832SDaniel Borkmann 		 */
5234f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5235fc403832SDaniel Borkmann 		update_rs = true;
5236fc403832SDaniel Borkmann 	}
5237f53adae4SDaniel Borkmann 
523847e27d5eSDaniel Borkmann update_lft:
5239f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5240fc403832SDaniel Borkmann 
524177ecaaceSHannes Frederic Sowa 	if (update_rs) {
5242f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
5243bd11f074SMaciej Żenczykowski 		idev->rs_interval = rfc3315_s14_backoff_init(
5244bd11f074SMaciej Żenczykowski 			idev->cnf.rtr_solicit_interval);
524577ecaaceSHannes Frederic Sowa 		idev->rs_probes = 1;
5246bd11f074SMaciej Żenczykowski 		addrconf_mod_rs_timer(idev, idev->rs_interval);
524777ecaaceSHannes Frederic Sowa 	}
5248f53adae4SDaniel Borkmann 
5249f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
5250f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
5251f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
5252617fe29dSDaniel Borkmann 		if (ifp->tokenized) {
5253f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
5254f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
5255f53adae4SDaniel Borkmann 		}
5256f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
5257f53adae4SDaniel Borkmann 	}
5258f53adae4SDaniel Borkmann 
5259f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5260b2ed64a9SLubomir Rintel 	inet6_ifinfo_notify(RTM_NEWLINK, idev);
5261c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
5262f53adae4SDaniel Borkmann 	return 0;
5263f53adae4SDaniel Borkmann }
5264f53adae4SDaniel Borkmann 
526511b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
526611b1f828SDaniel Borkmann 	[IFLA_INET6_ADDR_GEN_MODE]	= { .type = NLA_U8 },
526711b1f828SDaniel Borkmann 	[IFLA_INET6_TOKEN]		= { .len = sizeof(struct in6_addr) },
526811b1f828SDaniel Borkmann };
526911b1f828SDaniel Borkmann 
527011b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev,
527111b1f828SDaniel Borkmann 				  const struct nlattr *nla)
527211b1f828SDaniel Borkmann {
527311b1f828SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
527411b1f828SDaniel Borkmann 
527511b1f828SDaniel Borkmann 	if (dev && !__in6_dev_get(dev))
527611b1f828SDaniel Borkmann 		return -EAFNOSUPPORT;
527711b1f828SDaniel Borkmann 
5278fceb6435SJohannes Berg 	return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy,
5279fceb6435SJohannes Berg 				NULL);
528011b1f828SDaniel Borkmann }
528111b1f828SDaniel Borkmann 
5282d35a00b8SFelix Jia static int check_addr_gen_mode(int mode)
5283d35a00b8SFelix Jia {
5284d35a00b8SFelix Jia 	if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5285d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_NONE &&
5286d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5287d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_RANDOM)
5288d35a00b8SFelix Jia 		return -EINVAL;
5289d35a00b8SFelix Jia 	return 1;
5290d35a00b8SFelix Jia }
5291d35a00b8SFelix Jia 
5292d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5293d35a00b8SFelix Jia 				int mode)
5294d35a00b8SFelix Jia {
5295d35a00b8SFelix Jia 	if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5296d35a00b8SFelix Jia 	    !idev->cnf.stable_secret.initialized &&
5297d35a00b8SFelix Jia 	    !net->ipv6.devconf_dflt->stable_secret.initialized)
5298d35a00b8SFelix Jia 		return -EINVAL;
5299d35a00b8SFelix Jia 	return 1;
5300d35a00b8SFelix Jia }
5301d35a00b8SFelix Jia 
5302f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5303f53adae4SDaniel Borkmann {
5304f53adae4SDaniel Borkmann 	int err = -EINVAL;
5305f53adae4SDaniel Borkmann 	struct inet6_dev *idev = __in6_dev_get(dev);
5306f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
5307f53adae4SDaniel Borkmann 
5308f53adae4SDaniel Borkmann 	if (!idev)
5309f53adae4SDaniel Borkmann 		return -EAFNOSUPPORT;
5310f53adae4SDaniel Borkmann 
5311fceb6435SJohannes Berg 	if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
5312f53adae4SDaniel Borkmann 		BUG();
5313f53adae4SDaniel Borkmann 
5314bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_TOKEN]) {
5315f53adae4SDaniel Borkmann 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5316bc91b0f0SJiri Pirko 		if (err)
5317bc91b0f0SJiri Pirko 			return err;
5318bc91b0f0SJiri Pirko 	}
5319bc91b0f0SJiri Pirko 
5320bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5321bc91b0f0SJiri Pirko 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5322bc91b0f0SJiri Pirko 
5323d35a00b8SFelix Jia 		if (check_addr_gen_mode(mode) < 0 ||
5324d35a00b8SFelix Jia 		    check_stable_privacy(idev, dev_net(dev), mode) < 0)
5325bc91b0f0SJiri Pirko 			return -EINVAL;
5326622c81d5SHannes Frederic Sowa 
5327d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = mode;
5328bc91b0f0SJiri Pirko 		err = 0;
5329bc91b0f0SJiri Pirko 	}
5330f53adae4SDaniel Borkmann 
5331f53adae4SDaniel Borkmann 	return err;
5332f53adae4SDaniel Borkmann }
5333f53adae4SDaniel Borkmann 
5334b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
533515e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
5336b382b191SThomas Graf {
5337b382b191SThomas Graf 	struct net_device *dev = idev->dev;
5338b382b191SThomas Graf 	struct ifinfomsg *hdr;
5339b382b191SThomas Graf 	struct nlmsghdr *nlh;
5340b382b191SThomas Graf 	void *protoinfo;
5341b382b191SThomas Graf 
534215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
534363159f29SIan Morris 	if (!nlh)
5344b382b191SThomas Graf 		return -EMSGSIZE;
5345b382b191SThomas Graf 
5346b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
5347b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
5348b382b191SThomas Graf 	hdr->__ifi_pad = 0;
5349b382b191SThomas Graf 	hdr->ifi_type = dev->type;
5350b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
5351b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
5352b382b191SThomas Graf 	hdr->ifi_change = 0;
5353b382b191SThomas Graf 
5354c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5355c78679e8SDavid S. Miller 	    (dev->addr_len &&
5356c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5357c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5358a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
53590344338bSAndy Gospodarek 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
53600344338bSAndy Gospodarek 	    nla_put_u8(skb, IFLA_OPERSTATE,
53610344338bSAndy Gospodarek 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5362c78679e8SDavid S. Miller 		goto nla_put_failure;
5363b382b191SThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
536463159f29SIan Morris 	if (!protoinfo)
5365b382b191SThomas Graf 		goto nla_put_failure;
5366b382b191SThomas Graf 
5367d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5368b382b191SThomas Graf 		goto nla_put_failure;
5369b382b191SThomas Graf 
537004561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
5371053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5372053c095aSJohannes Berg 	return 0;
53731da177e4SLinus Torvalds 
537404561c1fSThomas Graf nla_put_failure:
537526932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
537626932566SPatrick McHardy 	return -EMSGSIZE;
53771da177e4SLinus Torvalds }
53781da177e4SLinus Torvalds 
53791da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
53801da177e4SLinus Torvalds {
53813b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
538284d2697dSEric Dumazet 	int h, s_h;
5383434a8a58SDavid S. Miller 	int idx = 0, s_idx;
53841da177e4SLinus Torvalds 	struct net_device *dev;
53851da177e4SLinus Torvalds 	struct inet6_dev *idev;
538684d2697dSEric Dumazet 	struct hlist_head *head;
53871da177e4SLinus Torvalds 
538884d2697dSEric Dumazet 	s_h = cb->args[0];
538984d2697dSEric Dumazet 	s_idx = cb->args[1];
539084d2697dSEric Dumazet 
539184d2697dSEric Dumazet 	rcu_read_lock();
539284d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
53937562f876SPavel Emelianov 		idx = 0;
539484d2697dSEric Dumazet 		head = &net->dev_index_head[h];
5395b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
53961da177e4SLinus Torvalds 			if (idx < s_idx)
53977562f876SPavel Emelianov 				goto cont;
539884d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
539984d2697dSEric Dumazet 			if (!idev)
54007562f876SPavel Emelianov 				goto cont;
540184d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
540215e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
540384d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
5404053c095aSJohannes Berg 					      RTM_NEWLINK, NLM_F_MULTI) < 0)
540584d2697dSEric Dumazet 				goto out;
54067562f876SPavel Emelianov cont:
54077562f876SPavel Emelianov 			idx++;
54081da177e4SLinus Torvalds 		}
540984d2697dSEric Dumazet 	}
541084d2697dSEric Dumazet out:
541184d2697dSEric Dumazet 	rcu_read_unlock();
541284d2697dSEric Dumazet 	cb->args[1] = idx;
541384d2697dSEric Dumazet 	cb->args[0] = h;
54141da177e4SLinus Torvalds 
54151da177e4SLinus Torvalds 	return skb->len;
54161da177e4SLinus Torvalds }
54171da177e4SLinus Torvalds 
54181da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
54191da177e4SLinus Torvalds {
54201da177e4SLinus Torvalds 	struct sk_buff *skb;
5421c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
54228d7a76c9SThomas Graf 	int err = -ENOBUFS;
54231da177e4SLinus Torvalds 
5424339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
542563159f29SIan Morris 	if (!skb)
54268d7a76c9SThomas Graf 		goto errout;
54278d7a76c9SThomas Graf 
54288d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
542926932566SPatrick McHardy 	if (err < 0) {
543026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
543126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
543226932566SPatrick McHardy 		kfree_skb(skb);
543326932566SPatrick McHardy 		goto errout;
543426932566SPatrick McHardy 	}
54355f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
54361ce85fe4SPablo Neira Ayuso 	return;
54378d7a76c9SThomas Graf errout:
54388d7a76c9SThomas Graf 	if (err < 0)
54395f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
54401da177e4SLinus Torvalds }
54411da177e4SLinus Torvalds 
5442339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
5443339bf98fSThomas Graf {
5444339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
5445339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
5446339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
5447339bf98fSThomas Graf }
5448c5396a31SYOSHIFUJI Hideaki 
54491da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
545015e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
5451b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
54521da177e4SLinus Torvalds {
54531da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
54541da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
54551da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
54561da177e4SLinus Torvalds 
545715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
545863159f29SIan Morris 	if (!nlh)
545926932566SPatrick McHardy 		return -EMSGSIZE;
54606051e2f4SThomas Graf 
54616051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
54621da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
54638a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
54648a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
54651da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
54661da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
54671da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
54688a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
54691da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
54701da177e4SLinus Torvalds 	if (pinfo->onlink)
54711da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
54721da177e4SLinus Torvalds 	if (pinfo->autoconf)
54731da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
54741da177e4SLinus Torvalds 
5475c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5476c78679e8SDavid S. Miller 		goto nla_put_failure;
54771da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
54781da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
5479c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5480c78679e8SDavid S. Miller 		goto nla_put_failure;
5481053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5482053c095aSJohannes Berg 	return 0;
54831da177e4SLinus Torvalds 
54846051e2f4SThomas Graf nla_put_failure:
548526932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
548626932566SPatrick McHardy 	return -EMSGSIZE;
54871da177e4SLinus Torvalds }
54881da177e4SLinus Torvalds 
54891da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
54901da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
54911da177e4SLinus Torvalds {
54921da177e4SLinus Torvalds 	struct sk_buff *skb;
5493c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
54948c384bfaSThomas Graf 	int err = -ENOBUFS;
54951da177e4SLinus Torvalds 
5496339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
549763159f29SIan Morris 	if (!skb)
54988c384bfaSThomas Graf 		goto errout;
54998c384bfaSThomas Graf 
55008c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
550126932566SPatrick McHardy 	if (err < 0) {
550226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
550326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
550426932566SPatrick McHardy 		kfree_skb(skb);
550526932566SPatrick McHardy 		goto errout;
550626932566SPatrick McHardy 	}
55071ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
55081ce85fe4SPablo Neira Ayuso 	return;
55098c384bfaSThomas Graf errout:
55108c384bfaSThomas Graf 	if (err < 0)
55116fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
55121da177e4SLinus Torvalds }
55131da177e4SLinus Torvalds 
55141da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
55151da177e4SLinus Torvalds {
551663998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
551763998ac2SNicolas Dichtel 
5518c15b1ccaSHannes Frederic Sowa 	if (event)
5519c15b1ccaSHannes Frederic Sowa 		ASSERT_RTNL();
5520c15b1ccaSHannes Frederic Sowa 
55211da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
55221da177e4SLinus Torvalds 
55231da177e4SLinus Torvalds 	switch (event) {
55241da177e4SLinus Torvalds 	case RTM_NEWADDR:
552595c385b4SNeil Horman 		/*
552695c385b4SNeil Horman 		 * If the address was optimistic
552795c385b4SNeil Horman 		 * we inserted the route at the start of
552895c385b4SNeil Horman 		 * our DAD process, so we don't need
552995c385b4SNeil Horman 		 * to do it again
553095c385b4SNeil Horman 		 */
55314e587ea7SWei Wang 		if (!rcu_access_pointer(ifp->rt->rt6i_node))
553240e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
55331da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
55341da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
55357996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr))
5536caeaba79SNicolas Dichtel 			addrconf_prefix_route(&ifp->peer_addr, 128,
5537caeaba79SNicolas Dichtel 					      ifp->idev->dev, 0, 0);
55381da177e4SLinus Torvalds 		break;
55391da177e4SLinus Torvalds 	case RTM_DELADDR:
55401da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
55411da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
55421da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
55437996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr)) {
5544caeaba79SNicolas Dichtel 			struct rt6_info *rt;
5545caeaba79SNicolas Dichtel 
5546e7478dfcSNicolas Dichtel 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5547e7478dfcSNicolas Dichtel 						       ifp->idev->dev, 0, 0);
55488e3d5be7SMartin KaFai Lau 			if (rt)
55498e3d5be7SMartin KaFai Lau 				ip6_del_rt(rt);
5550caeaba79SNicolas Dichtel 		}
555138bd10c4SDavid Ahern 		if (ifp->rt) {
5552ad65a2f0SWei Wang 			if (dst_hold_safe(&ifp->rt->dst))
55538e3d5be7SMartin KaFai Lau 				ip6_del_rt(ifp->rt);
555438bd10c4SDavid Ahern 		}
5555705f1c86SHannes Frederic Sowa 		rt_genid_bump_ipv6(net);
55561da177e4SLinus Torvalds 		break;
55571da177e4SLinus Torvalds 	}
555863998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
55591da177e4SLinus Torvalds }
55601da177e4SLinus Torvalds 
55611da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
55621da177e4SLinus Torvalds {
55638814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
55641da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
55651da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
55668814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
55671da177e4SLinus Torvalds }
55681da177e4SLinus Torvalds 
55691da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
55701da177e4SLinus Torvalds 
55711da177e4SLinus Torvalds static
5572fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
55731da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
55741da177e4SLinus Torvalds {
55751da177e4SLinus Torvalds 	int *valp = ctl->data;
55761da177e4SLinus Torvalds 	int val = *valp;
557788af182eSEric W. Biederman 	loff_t pos = *ppos;
5578fe2c6338SJoe Perches 	struct ctl_table lctl;
55791da177e4SLinus Torvalds 	int ret;
55801da177e4SLinus Torvalds 
5581013d97e9SFrancesco Ruggeri 	/*
5582013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
5583013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5584013d97e9SFrancesco Ruggeri 	 */
5585013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5586013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5587013d97e9SFrancesco Ruggeri 
5588013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
55891da177e4SLinus Torvalds 
5590c8fecf22SPavel Emelyanov 	if (write)
5591b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
559288af182eSEric W. Biederman 	if (ret)
559388af182eSEric W. Biederman 		*ppos = pos;
55941da177e4SLinus Torvalds 	return ret;
55951da177e4SLinus Torvalds }
55961da177e4SLinus Torvalds 
559777751427SMarcelo Leitner static
559877751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
559977751427SMarcelo Leitner 			void __user *buffer, size_t *lenp, loff_t *ppos)
560077751427SMarcelo Leitner {
560177751427SMarcelo Leitner 	struct inet6_dev *idev = ctl->extra1;
560277751427SMarcelo Leitner 	int min_mtu = IPV6_MIN_MTU;
560377751427SMarcelo Leitner 	struct ctl_table lctl;
560477751427SMarcelo Leitner 
560577751427SMarcelo Leitner 	lctl = *ctl;
560677751427SMarcelo Leitner 	lctl.extra1 = &min_mtu;
560777751427SMarcelo Leitner 	lctl.extra2 = idev ? &idev->dev->mtu : NULL;
560877751427SMarcelo Leitner 
560977751427SMarcelo Leitner 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
561077751427SMarcelo Leitner }
561177751427SMarcelo Leitner 
561256d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
561356d417b1SBrian Haley {
561475538c2bSCong Wang 	struct netdev_notifier_info info;
561575538c2bSCong Wang 
561656d417b1SBrian Haley 	if (!idev || !idev->dev)
561756d417b1SBrian Haley 		return;
561856d417b1SBrian Haley 
561975538c2bSCong Wang 	netdev_notifier_info_init(&info, idev->dev);
562056d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
562175538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_DOWN, &info);
562256d417b1SBrian Haley 	else
562375538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_UP, &info);
562456d417b1SBrian Haley }
562556d417b1SBrian Haley 
562656d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
562756d417b1SBrian Haley {
562856d417b1SBrian Haley 	struct net_device *dev;
562956d417b1SBrian Haley 	struct inet6_dev *idev;
563056d417b1SBrian Haley 
563103e4deffSKefeng Wang 	for_each_netdev(net, dev) {
563256d417b1SBrian Haley 		idev = __in6_dev_get(dev);
563356d417b1SBrian Haley 		if (idev) {
563456d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
563556d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
563656d417b1SBrian Haley 			if (changed)
563756d417b1SBrian Haley 				dev_disable_change(idev);
563856d417b1SBrian Haley 		}
563956d417b1SBrian Haley 	}
564056d417b1SBrian Haley }
564156d417b1SBrian Haley 
5642013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
564356d417b1SBrian Haley {
564456d417b1SBrian Haley 	struct net *net;
5645013d97e9SFrancesco Ruggeri 	int old;
5646013d97e9SFrancesco Ruggeri 
5647013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
5648013d97e9SFrancesco Ruggeri 		return restart_syscall();
564956d417b1SBrian Haley 
565056d417b1SBrian Haley 	net = (struct net *)table->extra2;
5651013d97e9SFrancesco Ruggeri 	old = *p;
5652013d97e9SFrancesco Ruggeri 	*p = newf;
565356d417b1SBrian Haley 
5654013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5655013d97e9SFrancesco Ruggeri 		rtnl_unlock();
565656d417b1SBrian Haley 		return 0;
565788af182eSEric W. Biederman 	}
565856d417b1SBrian Haley 
565956d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
566056d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
566156d417b1SBrian Haley 		addrconf_disable_change(net, newf);
5662013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
566356d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
566456d417b1SBrian Haley 
566556d417b1SBrian Haley 	rtnl_unlock();
566656d417b1SBrian Haley 	return 0;
566756d417b1SBrian Haley }
566856d417b1SBrian Haley 
566956d417b1SBrian Haley static
5670fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
567156d417b1SBrian Haley 			    void __user *buffer, size_t *lenp, loff_t *ppos)
567256d417b1SBrian Haley {
567356d417b1SBrian Haley 	int *valp = ctl->data;
567456d417b1SBrian Haley 	int val = *valp;
567588af182eSEric W. Biederman 	loff_t pos = *ppos;
5676fe2c6338SJoe Perches 	struct ctl_table lctl;
567756d417b1SBrian Haley 	int ret;
567856d417b1SBrian Haley 
5679013d97e9SFrancesco Ruggeri 	/*
5680013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
5681013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5682013d97e9SFrancesco Ruggeri 	 */
5683013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5684013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5685013d97e9SFrancesco Ruggeri 
5686013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
568756d417b1SBrian Haley 
568856d417b1SBrian Haley 	if (write)
568956d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
569088af182eSEric W. Biederman 	if (ret)
569188af182eSEric W. Biederman 		*ppos = pos;
569256d417b1SBrian Haley 	return ret;
569356d417b1SBrian Haley }
569456d417b1SBrian Haley 
5695c92d5491Sstephen hemminger static
5696c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5697c92d5491Sstephen hemminger 			      void __user *buffer, size_t *lenp, loff_t *ppos)
5698c92d5491Sstephen hemminger {
5699c92d5491Sstephen hemminger 	int *valp = ctl->data;
5700c92d5491Sstephen hemminger 	int ret;
5701c92d5491Sstephen hemminger 	int old, new;
5702c92d5491Sstephen hemminger 
5703c92d5491Sstephen hemminger 	old = *valp;
5704c92d5491Sstephen hemminger 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5705c92d5491Sstephen hemminger 	new = *valp;
5706c92d5491Sstephen hemminger 
5707c92d5491Sstephen hemminger 	if (write && old != new) {
5708c92d5491Sstephen hemminger 		struct net *net = ctl->extra2;
5709c92d5491Sstephen hemminger 
5710c92d5491Sstephen hemminger 		if (!rtnl_trylock())
5711c92d5491Sstephen hemminger 			return restart_syscall();
5712c92d5491Sstephen hemminger 
5713c92d5491Sstephen hemminger 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
571485b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
571585b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
5716c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_DEFAULT,
5717c92d5491Sstephen hemminger 						     net->ipv6.devconf_dflt);
5718c92d5491Sstephen hemminger 		else if (valp == &net->ipv6.devconf_all->proxy_ndp)
571985b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
572085b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
5721c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_ALL,
5722c92d5491Sstephen hemminger 						     net->ipv6.devconf_all);
5723c92d5491Sstephen hemminger 		else {
5724c92d5491Sstephen hemminger 			struct inet6_dev *idev = ctl->extra1;
5725c92d5491Sstephen hemminger 
572685b3daadSDavid Ahern 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
572785b3daadSDavid Ahern 						     NETCONFA_PROXY_NEIGH,
5728c92d5491Sstephen hemminger 						     idev->dev->ifindex,
5729c92d5491Sstephen hemminger 						     &idev->cnf);
5730c92d5491Sstephen hemminger 		}
5731c92d5491Sstephen hemminger 		rtnl_unlock();
5732c92d5491Sstephen hemminger 	}
5733c92d5491Sstephen hemminger 
5734c92d5491Sstephen hemminger 	return ret;
5735c92d5491Sstephen hemminger }
5736c92d5491Sstephen hemminger 
5737d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5738d35a00b8SFelix Jia 					 void __user *buffer, size_t *lenp,
5739d35a00b8SFelix Jia 					 loff_t *ppos)
5740d35a00b8SFelix Jia {
5741d35a00b8SFelix Jia 	int ret = 0;
5742d35a00b8SFelix Jia 	int new_val;
5743d35a00b8SFelix Jia 	struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5744d35a00b8SFelix Jia 	struct net *net = (struct net *)ctl->extra2;
5745d35a00b8SFelix Jia 
57468c171d6cSFelix Jia 	if (!rtnl_trylock())
57478c171d6cSFelix Jia 		return restart_syscall();
57488c171d6cSFelix Jia 
5749d35a00b8SFelix Jia 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5750d35a00b8SFelix Jia 
5751d35a00b8SFelix Jia 	if (write) {
5752d35a00b8SFelix Jia 		new_val = *((int *)ctl->data);
5753d35a00b8SFelix Jia 
57548c171d6cSFelix Jia 		if (check_addr_gen_mode(new_val) < 0) {
57558c171d6cSFelix Jia 			ret = -EINVAL;
57568c171d6cSFelix Jia 			goto out;
57578c171d6cSFelix Jia 		}
5758d35a00b8SFelix Jia 
5759d35a00b8SFelix Jia 		/* request for default */
5760d35a00b8SFelix Jia 		if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5761d35a00b8SFelix Jia 			ipv6_devconf_dflt.addr_gen_mode = new_val;
5762d35a00b8SFelix Jia 
5763d35a00b8SFelix Jia 		/* request for individual net device */
5764d35a00b8SFelix Jia 		} else {
5765d35a00b8SFelix Jia 			if (!idev)
57668c171d6cSFelix Jia 				goto out;
5767d35a00b8SFelix Jia 
57688c171d6cSFelix Jia 			if (check_stable_privacy(idev, net, new_val) < 0) {
57698c171d6cSFelix Jia 				ret = -EINVAL;
57708c171d6cSFelix Jia 				goto out;
57718c171d6cSFelix Jia 			}
5772d35a00b8SFelix Jia 
5773d35a00b8SFelix Jia 			if (idev->cnf.addr_gen_mode != new_val) {
5774d35a00b8SFelix Jia 				idev->cnf.addr_gen_mode = new_val;
5775d35a00b8SFelix Jia 				addrconf_dev_config(idev->dev);
57768c171d6cSFelix Jia 			}
57778c171d6cSFelix Jia 		}
57788c171d6cSFelix Jia 	}
57798c171d6cSFelix Jia 
57808c171d6cSFelix Jia out:
5781d35a00b8SFelix Jia 	rtnl_unlock();
5782d35a00b8SFelix Jia 
5783d35a00b8SFelix Jia 	return ret;
5784d35a00b8SFelix Jia }
5785d35a00b8SFelix Jia 
57863d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
57873d1bec99SHannes Frederic Sowa 					 void __user *buffer, size_t *lenp,
57883d1bec99SHannes Frederic Sowa 					 loff_t *ppos)
57893d1bec99SHannes Frederic Sowa {
57903d1bec99SHannes Frederic Sowa 	int err;
57913d1bec99SHannes Frederic Sowa 	struct in6_addr addr;
57923d1bec99SHannes Frederic Sowa 	char str[IPV6_MAX_STRLEN];
57933d1bec99SHannes Frederic Sowa 	struct ctl_table lctl = *ctl;
5794622c81d5SHannes Frederic Sowa 	struct net *net = ctl->extra2;
57953d1bec99SHannes Frederic Sowa 	struct ipv6_stable_secret *secret = ctl->data;
57963d1bec99SHannes Frederic Sowa 
5797622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5798622c81d5SHannes Frederic Sowa 		return -EIO;
5799622c81d5SHannes Frederic Sowa 
58003d1bec99SHannes Frederic Sowa 	lctl.maxlen = IPV6_MAX_STRLEN;
58013d1bec99SHannes Frederic Sowa 	lctl.data = str;
58023d1bec99SHannes Frederic Sowa 
58033d1bec99SHannes Frederic Sowa 	if (!rtnl_trylock())
58043d1bec99SHannes Frederic Sowa 		return restart_syscall();
58053d1bec99SHannes Frederic Sowa 
58063d1bec99SHannes Frederic Sowa 	if (!write && !secret->initialized) {
58073d1bec99SHannes Frederic Sowa 		err = -EIO;
58083d1bec99SHannes Frederic Sowa 		goto out;
58093d1bec99SHannes Frederic Sowa 	}
58103d1bec99SHannes Frederic Sowa 
58115449a5caSWANG Cong 	err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
58123d1bec99SHannes Frederic Sowa 	if (err >= sizeof(str)) {
58133d1bec99SHannes Frederic Sowa 		err = -EIO;
58143d1bec99SHannes Frederic Sowa 		goto out;
58153d1bec99SHannes Frederic Sowa 	}
58163d1bec99SHannes Frederic Sowa 
58173d1bec99SHannes Frederic Sowa 	err = proc_dostring(&lctl, write, buffer, lenp, ppos);
58183d1bec99SHannes Frederic Sowa 	if (err || !write)
58193d1bec99SHannes Frederic Sowa 		goto out;
58203d1bec99SHannes Frederic Sowa 
58213d1bec99SHannes Frederic Sowa 	if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
58223d1bec99SHannes Frederic Sowa 		err = -EIO;
58233d1bec99SHannes Frederic Sowa 		goto out;
58243d1bec99SHannes Frederic Sowa 	}
58253d1bec99SHannes Frederic Sowa 
58263d1bec99SHannes Frederic Sowa 	secret->initialized = true;
58273d1bec99SHannes Frederic Sowa 	secret->secret = addr;
58283d1bec99SHannes Frederic Sowa 
5829622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5830622c81d5SHannes Frederic Sowa 		struct net_device *dev;
5831622c81d5SHannes Frederic Sowa 
5832622c81d5SHannes Frederic Sowa 		for_each_netdev(net, dev) {
5833622c81d5SHannes Frederic Sowa 			struct inet6_dev *idev = __in6_dev_get(dev);
5834622c81d5SHannes Frederic Sowa 
5835622c81d5SHannes Frederic Sowa 			if (idev) {
5836d35a00b8SFelix Jia 				idev->cnf.addr_gen_mode =
5837622c81d5SHannes Frederic Sowa 					IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5838622c81d5SHannes Frederic Sowa 			}
5839622c81d5SHannes Frederic Sowa 		}
5840622c81d5SHannes Frederic Sowa 	} else {
5841622c81d5SHannes Frederic Sowa 		struct inet6_dev *idev = ctl->extra1;
5842622c81d5SHannes Frederic Sowa 
5843d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5844622c81d5SHannes Frederic Sowa 	}
5845622c81d5SHannes Frederic Sowa 
58463d1bec99SHannes Frederic Sowa out:
58473d1bec99SHannes Frederic Sowa 	rtnl_unlock();
58483d1bec99SHannes Frederic Sowa 
58493d1bec99SHannes Frederic Sowa 	return err;
58503d1bec99SHannes Frederic Sowa }
5851c92d5491Sstephen hemminger 
585235103d11SAndy Gospodarek static
585335103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
585435103d11SAndy Gospodarek 						int write,
585535103d11SAndy Gospodarek 						void __user *buffer,
585635103d11SAndy Gospodarek 						size_t *lenp,
585735103d11SAndy Gospodarek 						loff_t *ppos)
585835103d11SAndy Gospodarek {
585935103d11SAndy Gospodarek 	int *valp = ctl->data;
586035103d11SAndy Gospodarek 	int val = *valp;
586135103d11SAndy Gospodarek 	loff_t pos = *ppos;
586235103d11SAndy Gospodarek 	struct ctl_table lctl;
586335103d11SAndy Gospodarek 	int ret;
586435103d11SAndy Gospodarek 
586535103d11SAndy Gospodarek 	/* ctl->data points to idev->cnf.ignore_routes_when_linkdown
586635103d11SAndy Gospodarek 	 * we should not modify it until we get the rtnl lock.
586735103d11SAndy Gospodarek 	 */
586835103d11SAndy Gospodarek 	lctl = *ctl;
586935103d11SAndy Gospodarek 	lctl.data = &val;
587035103d11SAndy Gospodarek 
587135103d11SAndy Gospodarek 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
587235103d11SAndy Gospodarek 
587335103d11SAndy Gospodarek 	if (write)
587435103d11SAndy Gospodarek 		ret = addrconf_fixup_linkdown(ctl, valp, val);
587535103d11SAndy Gospodarek 	if (ret)
587635103d11SAndy Gospodarek 		*ppos = pos;
587735103d11SAndy Gospodarek 	return ret;
587835103d11SAndy Gospodarek }
587935103d11SAndy Gospodarek 
5880df789fe7SDavid Forster static
5881df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action)
5882df789fe7SDavid Forster {
5883df789fe7SDavid Forster 	if (rt) {
5884df789fe7SDavid Forster 		if (action)
5885df789fe7SDavid Forster 			rt->dst.flags |= DST_NOPOLICY;
5886df789fe7SDavid Forster 		else
5887df789fe7SDavid Forster 			rt->dst.flags &= ~DST_NOPOLICY;
5888df789fe7SDavid Forster 	}
5889df789fe7SDavid Forster }
5890df789fe7SDavid Forster 
5891df789fe7SDavid Forster static
5892df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
5893df789fe7SDavid Forster {
5894df789fe7SDavid Forster 	struct inet6_ifaddr *ifa;
5895df789fe7SDavid Forster 
5896df789fe7SDavid Forster 	read_lock_bh(&idev->lock);
5897df789fe7SDavid Forster 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
5898df789fe7SDavid Forster 		spin_lock(&ifa->lock);
5899df789fe7SDavid Forster 		if (ifa->rt) {
5900df789fe7SDavid Forster 			struct rt6_info *rt = ifa->rt;
5901df789fe7SDavid Forster 			struct fib6_table *table = rt->rt6i_table;
5902df789fe7SDavid Forster 			int cpu;
5903df789fe7SDavid Forster 
5904df789fe7SDavid Forster 			read_lock(&table->tb6_lock);
5905df789fe7SDavid Forster 			addrconf_set_nopolicy(ifa->rt, val);
5906df789fe7SDavid Forster 			if (rt->rt6i_pcpu) {
5907df789fe7SDavid Forster 				for_each_possible_cpu(cpu) {
5908df789fe7SDavid Forster 					struct rt6_info **rtp;
5909df789fe7SDavid Forster 
5910df789fe7SDavid Forster 					rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
5911df789fe7SDavid Forster 					addrconf_set_nopolicy(*rtp, val);
5912df789fe7SDavid Forster 				}
5913df789fe7SDavid Forster 			}
5914df789fe7SDavid Forster 			read_unlock(&table->tb6_lock);
5915df789fe7SDavid Forster 		}
5916df789fe7SDavid Forster 		spin_unlock(&ifa->lock);
5917df789fe7SDavid Forster 	}
5918df789fe7SDavid Forster 	read_unlock_bh(&idev->lock);
5919df789fe7SDavid Forster }
5920df789fe7SDavid Forster 
5921df789fe7SDavid Forster static
5922df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
5923df789fe7SDavid Forster {
5924df789fe7SDavid Forster 	struct inet6_dev *idev;
5925df789fe7SDavid Forster 	struct net *net;
5926df789fe7SDavid Forster 
5927df789fe7SDavid Forster 	if (!rtnl_trylock())
5928df789fe7SDavid Forster 		return restart_syscall();
5929df789fe7SDavid Forster 
5930df789fe7SDavid Forster 	*valp = val;
5931df789fe7SDavid Forster 
5932df789fe7SDavid Forster 	net = (struct net *)ctl->extra2;
5933df789fe7SDavid Forster 	if (valp == &net->ipv6.devconf_dflt->disable_policy) {
5934df789fe7SDavid Forster 		rtnl_unlock();
5935df789fe7SDavid Forster 		return 0;
5936df789fe7SDavid Forster 	}
5937df789fe7SDavid Forster 
5938df789fe7SDavid Forster 	if (valp == &net->ipv6.devconf_all->disable_policy)  {
5939df789fe7SDavid Forster 		struct net_device *dev;
5940df789fe7SDavid Forster 
5941df789fe7SDavid Forster 		for_each_netdev(net, dev) {
5942df789fe7SDavid Forster 			idev = __in6_dev_get(dev);
5943df789fe7SDavid Forster 			if (idev)
5944df789fe7SDavid Forster 				addrconf_disable_policy_idev(idev, val);
5945df789fe7SDavid Forster 		}
5946df789fe7SDavid Forster 	} else {
5947df789fe7SDavid Forster 		idev = (struct inet6_dev *)ctl->extra1;
5948df789fe7SDavid Forster 		addrconf_disable_policy_idev(idev, val);
5949df789fe7SDavid Forster 	}
5950df789fe7SDavid Forster 
5951df789fe7SDavid Forster 	rtnl_unlock();
5952df789fe7SDavid Forster 	return 0;
5953df789fe7SDavid Forster }
5954df789fe7SDavid Forster 
5955df789fe7SDavid Forster static
5956df789fe7SDavid Forster int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
5957df789fe7SDavid Forster 				   void __user *buffer, size_t *lenp,
5958df789fe7SDavid Forster 				   loff_t *ppos)
5959df789fe7SDavid Forster {
5960df789fe7SDavid Forster 	int *valp = ctl->data;
5961df789fe7SDavid Forster 	int val = *valp;
5962df789fe7SDavid Forster 	loff_t pos = *ppos;
5963df789fe7SDavid Forster 	struct ctl_table lctl;
5964df789fe7SDavid Forster 	int ret;
5965df789fe7SDavid Forster 
5966df789fe7SDavid Forster 	lctl = *ctl;
5967df789fe7SDavid Forster 	lctl.data = &val;
5968df789fe7SDavid Forster 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5969df789fe7SDavid Forster 
5970df789fe7SDavid Forster 	if (write && (*valp != val))
5971df789fe7SDavid Forster 		ret = addrconf_disable_policy(ctl, valp, val);
5972df789fe7SDavid Forster 
5973df789fe7SDavid Forster 	if (ret)
5974df789fe7SDavid Forster 		*ppos = pos;
5975df789fe7SDavid Forster 
5976df789fe7SDavid Forster 	return ret;
5977df789fe7SDavid Forster }
5978df789fe7SDavid Forster 
5979cb4a4c69SMaciej Żenczykowski static int minus_one = -1;
5980cb9e684eSMaciej Żenczykowski static const int one = 1;
5981cb9e684eSMaciej Żenczykowski static const int two_five_five = 255;
5982cb9e684eSMaciej Żenczykowski 
5983607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = {
59841da177e4SLinus Torvalds 	{
59851da177e4SLinus Torvalds 		.procname	= "forwarding",
59861da177e4SLinus Torvalds 		.data		= &ipv6_devconf.forwarding,
59871da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59881da177e4SLinus Torvalds 		.mode		= 0644,
59896d9f239aSAlexey Dobriyan 		.proc_handler	= addrconf_sysctl_forward,
59901da177e4SLinus Torvalds 	},
59911da177e4SLinus Torvalds 	{
59921da177e4SLinus Torvalds 		.procname	= "hop_limit",
59931da177e4SLinus Torvalds 		.data		= &ipv6_devconf.hop_limit,
59941da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59951da177e4SLinus Torvalds 		.mode		= 0644,
5996cb9e684eSMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
5997cb9e684eSMaciej Żenczykowski 		.extra1		= (void *)&one,
5998cb9e684eSMaciej Żenczykowski 		.extra2		= (void *)&two_five_five,
59991da177e4SLinus Torvalds 	},
60001da177e4SLinus Torvalds 	{
60011da177e4SLinus Torvalds 		.procname	= "mtu",
60021da177e4SLinus Torvalds 		.data		= &ipv6_devconf.mtu6,
60031da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60041da177e4SLinus Torvalds 		.mode		= 0644,
600577751427SMarcelo Leitner 		.proc_handler	= addrconf_sysctl_mtu,
60061da177e4SLinus Torvalds 	},
60071da177e4SLinus Torvalds 	{
60081da177e4SLinus Torvalds 		.procname	= "accept_ra",
60091da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_ra,
60101da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60111da177e4SLinus Torvalds 		.mode		= 0644,
60126d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60131da177e4SLinus Torvalds 	},
60141da177e4SLinus Torvalds 	{
60151da177e4SLinus Torvalds 		.procname	= "accept_redirects",
60161da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_redirects,
60171da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60181da177e4SLinus Torvalds 		.mode		= 0644,
60196d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60201da177e4SLinus Torvalds 	},
60211da177e4SLinus Torvalds 	{
60221da177e4SLinus Torvalds 		.procname	= "autoconf",
60231da177e4SLinus Torvalds 		.data		= &ipv6_devconf.autoconf,
60241da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60251da177e4SLinus Torvalds 		.mode		= 0644,
60266d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60271da177e4SLinus Torvalds 	},
60281da177e4SLinus Torvalds 	{
60291da177e4SLinus Torvalds 		.procname	= "dad_transmits",
60301da177e4SLinus Torvalds 		.data		= &ipv6_devconf.dad_transmits,
60311da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60321da177e4SLinus Torvalds 		.mode		= 0644,
60336d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60341da177e4SLinus Torvalds 	},
60351da177e4SLinus Torvalds 	{
60361da177e4SLinus Torvalds 		.procname	= "router_solicitations",
60371da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicits,
60381da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60391da177e4SLinus Torvalds 		.mode		= 0644,
6040cb4a4c69SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
6041cb4a4c69SMaciej Żenczykowski 		.extra1		= &minus_one,
60421da177e4SLinus Torvalds 	},
60431da177e4SLinus Torvalds 	{
60441da177e4SLinus Torvalds 		.procname	= "router_solicitation_interval",
60451da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_interval,
60461da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60471da177e4SLinus Torvalds 		.mode		= 0644,
60486d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
60491da177e4SLinus Torvalds 	},
60501da177e4SLinus Torvalds 	{
6051bd11f074SMaciej Żenczykowski 		.procname	= "router_solicitation_max_interval",
6052bd11f074SMaciej Żenczykowski 		.data		= &ipv6_devconf.rtr_solicit_max_interval,
6053bd11f074SMaciej Żenczykowski 		.maxlen		= sizeof(int),
6054bd11f074SMaciej Żenczykowski 		.mode		= 0644,
6055bd11f074SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_jiffies,
6056bd11f074SMaciej Żenczykowski 	},
6057bd11f074SMaciej Żenczykowski 	{
60581da177e4SLinus Torvalds 		.procname	= "router_solicitation_delay",
60591da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_delay,
60601da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60611da177e4SLinus Torvalds 		.mode		= 0644,
60626d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
60631da177e4SLinus Torvalds 	},
60641da177e4SLinus Torvalds 	{
60651da177e4SLinus Torvalds 		.procname	= "force_mld_version",
60661da177e4SLinus Torvalds 		.data		= &ipv6_devconf.force_mld_version,
60671da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60681da177e4SLinus Torvalds 		.mode		= 0644,
60696d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60701da177e4SLinus Torvalds 	},
6071fc4eba58SHannes Frederic Sowa 	{
6072fc4eba58SHannes Frederic Sowa 		.procname	= "mldv1_unsolicited_report_interval",
6073fc4eba58SHannes Frederic Sowa 		.data		=
6074fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv1_unsolicited_report_interval,
6075fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6076fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
6077fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
6078fc4eba58SHannes Frederic Sowa 	},
6079fc4eba58SHannes Frederic Sowa 	{
6080fc4eba58SHannes Frederic Sowa 		.procname	= "mldv2_unsolicited_report_interval",
6081fc4eba58SHannes Frederic Sowa 		.data		=
6082fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv2_unsolicited_report_interval,
6083fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6084fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
6085fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
6086fc4eba58SHannes Frederic Sowa 	},
60871da177e4SLinus Torvalds 	{
60881da177e4SLinus Torvalds 		.procname	= "use_tempaddr",
60891da177e4SLinus Torvalds 		.data		= &ipv6_devconf.use_tempaddr,
60901da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60911da177e4SLinus Torvalds 		.mode		= 0644,
60926d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60931da177e4SLinus Torvalds 	},
60941da177e4SLinus Torvalds 	{
60951da177e4SLinus Torvalds 		.procname	= "temp_valid_lft",
60961da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_valid_lft,
60971da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60981da177e4SLinus Torvalds 		.mode		= 0644,
60996d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61001da177e4SLinus Torvalds 	},
61011da177e4SLinus Torvalds 	{
61021da177e4SLinus Torvalds 		.procname	= "temp_prefered_lft",
61031da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_prefered_lft,
61041da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61051da177e4SLinus Torvalds 		.mode		= 0644,
61066d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61071da177e4SLinus Torvalds 	},
61081da177e4SLinus Torvalds 	{
61091da177e4SLinus Torvalds 		.procname	= "regen_max_retry",
61101da177e4SLinus Torvalds 		.data		= &ipv6_devconf.regen_max_retry,
61111da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61121da177e4SLinus Torvalds 		.mode		= 0644,
61136d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61141da177e4SLinus Torvalds 	},
61151da177e4SLinus Torvalds 	{
61161da177e4SLinus Torvalds 		.procname	= "max_desync_factor",
61171da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_desync_factor,
61181da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61191da177e4SLinus Torvalds 		.mode		= 0644,
61206d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61211da177e4SLinus Torvalds 	},
61221da177e4SLinus Torvalds 	{
61231da177e4SLinus Torvalds 		.procname	= "max_addresses",
61241da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_addresses,
61251da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
61261da177e4SLinus Torvalds 		.mode		= 0644,
61276d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61281da177e4SLinus Torvalds 	},
61291da177e4SLinus Torvalds 	{
613065f5c7c1SYOSHIFUJI Hideaki 		.procname	= "accept_ra_defrtr",
613165f5c7c1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_defrtr,
613265f5c7c1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
613365f5c7c1SYOSHIFUJI Hideaki 		.mode		= 0644,
61346d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
613565f5c7c1SYOSHIFUJI Hideaki 	},
613665f5c7c1SYOSHIFUJI Hideaki 	{
61378013d1d7SHangbin Liu 		.procname	= "accept_ra_min_hop_limit",
61388013d1d7SHangbin Liu 		.data		= &ipv6_devconf.accept_ra_min_hop_limit,
61398013d1d7SHangbin Liu 		.maxlen		= sizeof(int),
61408013d1d7SHangbin Liu 		.mode		= 0644,
61418013d1d7SHangbin Liu 		.proc_handler	= proc_dointvec,
61428013d1d7SHangbin Liu 	},
61438013d1d7SHangbin Liu 	{
6144c4fd30ebSYOSHIFUJI Hideaki 		.procname	= "accept_ra_pinfo",
6145c4fd30ebSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_pinfo,
6146c4fd30ebSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6147c4fd30ebSYOSHIFUJI Hideaki 		.mode		= 0644,
61486d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
6149c4fd30ebSYOSHIFUJI Hideaki 	},
6150930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
6151930d6ff2SYOSHIFUJI Hideaki 	{
6152930d6ff2SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rtr_pref",
6153930d6ff2SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rtr_pref,
6154930d6ff2SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6155930d6ff2SYOSHIFUJI Hideaki 		.mode		= 0644,
61566d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
6157930d6ff2SYOSHIFUJI Hideaki 	},
615852e16356SYOSHIFUJI Hideaki 	{
615952e16356SYOSHIFUJI Hideaki 		.procname	= "router_probe_interval",
616052e16356SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.rtr_probe_interval,
616152e16356SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
616252e16356SYOSHIFUJI Hideaki 		.mode		= 0644,
61636d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
616452e16356SYOSHIFUJI Hideaki 	},
6165fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
616609c884d4SYOSHIFUJI Hideaki 	{
6167bbea124bSJoel Scherpelz 		.procname	= "accept_ra_rt_info_min_plen",
6168bbea124bSJoel Scherpelz 		.data		= &ipv6_devconf.accept_ra_rt_info_min_plen,
6169bbea124bSJoel Scherpelz 		.maxlen		= sizeof(int),
6170bbea124bSJoel Scherpelz 		.mode		= 0644,
6171bbea124bSJoel Scherpelz 		.proc_handler	= proc_dointvec,
6172bbea124bSJoel Scherpelz 	},
6173bbea124bSJoel Scherpelz 	{
617409c884d4SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rt_info_max_plen",
617509c884d4SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
617609c884d4SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
617709c884d4SYOSHIFUJI Hideaki 		.mode		= 0644,
61786d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
617909c884d4SYOSHIFUJI Hideaki 	},
618009c884d4SYOSHIFUJI Hideaki #endif
6181930d6ff2SYOSHIFUJI Hideaki #endif
6182c4fd30ebSYOSHIFUJI Hideaki 	{
6183fbea49e1SYOSHIFUJI Hideaki 		.procname	= "proxy_ndp",
6184fbea49e1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.proxy_ndp,
6185fbea49e1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6186fbea49e1SYOSHIFUJI Hideaki 		.mode		= 0644,
6187c92d5491Sstephen hemminger 		.proc_handler	= addrconf_sysctl_proxy_ndp,
6188fbea49e1SYOSHIFUJI Hideaki 	},
6189fbea49e1SYOSHIFUJI Hideaki 	{
61900bcbc926SYOSHIFUJI Hideaki 		.procname	= "accept_source_route",
61910bcbc926SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_source_route,
61920bcbc926SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
61930bcbc926SYOSHIFUJI Hideaki 		.mode		= 0644,
61946d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61950bcbc926SYOSHIFUJI Hideaki 	},
619695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
619795c385b4SNeil Horman 	{
619895c385b4SNeil Horman 		.procname	= "optimistic_dad",
619995c385b4SNeil Horman 		.data		= &ipv6_devconf.optimistic_dad,
620095c385b4SNeil Horman 		.maxlen		= sizeof(int),
620195c385b4SNeil Horman 		.mode		= 0644,
62026d9f239aSAlexey Dobriyan 		.proc_handler   = proc_dointvec,
620395c385b4SNeil Horman 	},
62047fd2561eSErik Kline 	{
62057fd2561eSErik Kline 		.procname	= "use_optimistic",
62067fd2561eSErik Kline 		.data		= &ipv6_devconf.use_optimistic,
62077fd2561eSErik Kline 		.maxlen		= sizeof(int),
62087fd2561eSErik Kline 		.mode		= 0644,
62097fd2561eSErik Kline 		.proc_handler	= proc_dointvec,
62107fd2561eSErik Kline 	},
621195c385b4SNeil Horman #endif
62127bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
62137bc570c8SYOSHIFUJI Hideaki 	{
62147bc570c8SYOSHIFUJI Hideaki 		.procname	= "mc_forwarding",
62157bc570c8SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.mc_forwarding,
62167bc570c8SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6217a4e6db07SDavid S. Miller 		.mode		= 0444,
62186d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
62197bc570c8SYOSHIFUJI Hideaki 	},
62207bc570c8SYOSHIFUJI Hideaki #endif
62210bcbc926SYOSHIFUJI Hideaki 	{
6222778d80beSYOSHIFUJI Hideaki 		.procname	= "disable_ipv6",
6223778d80beSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.disable_ipv6,
6224778d80beSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6225778d80beSYOSHIFUJI Hideaki 		.mode		= 0644,
622656d417b1SBrian Haley 		.proc_handler	= addrconf_sysctl_disable,
6227778d80beSYOSHIFUJI Hideaki 	},
6228778d80beSYOSHIFUJI Hideaki 	{
62291b34be74SYOSHIFUJI Hideaki 		.procname	= "accept_dad",
62301b34be74SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_dad,
62311b34be74SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
62321b34be74SYOSHIFUJI Hideaki 		.mode		= 0644,
62336d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
62341b34be74SYOSHIFUJI Hideaki 	},
62351b34be74SYOSHIFUJI Hideaki 	{
6236f7734fdfSOctavian Purdila 		.procname	= "force_tllao",
6237f7734fdfSOctavian Purdila 		.data		= &ipv6_devconf.force_tllao,
6238f7734fdfSOctavian Purdila 		.maxlen		= sizeof(int),
6239f7734fdfSOctavian Purdila 		.mode		= 0644,
6240f7734fdfSOctavian Purdila 		.proc_handler	= proc_dointvec
6241f7734fdfSOctavian Purdila 	},
6242f7734fdfSOctavian Purdila 	{
62435cb04436SHannes Frederic Sowa 		.procname	= "ndisc_notify",
62445cb04436SHannes Frederic Sowa 		.data		= &ipv6_devconf.ndisc_notify,
62455cb04436SHannes Frederic Sowa 		.maxlen		= sizeof(int),
62465cb04436SHannes Frederic Sowa 		.mode		= 0644,
62475cb04436SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
62485cb04436SHannes Frederic Sowa 	},
62495cb04436SHannes Frederic Sowa 	{
6250b800c3b9SHannes Frederic Sowa 		.procname	= "suppress_frag_ndisc",
6251b800c3b9SHannes Frederic Sowa 		.data		= &ipv6_devconf.suppress_frag_ndisc,
6252b800c3b9SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6253b800c3b9SHannes Frederic Sowa 		.mode		= 0644,
6254b800c3b9SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
6255b800c3b9SHannes Frederic Sowa 	},
6256b800c3b9SHannes Frederic Sowa 	{
6257d9333196SBen Greear 		.procname	= "accept_ra_from_local",
6258d9333196SBen Greear 		.data		= &ipv6_devconf.accept_ra_from_local,
6259d9333196SBen Greear 		.maxlen		= sizeof(int),
6260d9333196SBen Greear 		.mode		= 0644,
6261d9333196SBen Greear 		.proc_handler	= proc_dointvec,
6262d9333196SBen Greear 	},
6263d9333196SBen Greear 	{
6264c2943f14SHarout Hedeshian 		.procname	= "accept_ra_mtu",
6265c2943f14SHarout Hedeshian 		.data		= &ipv6_devconf.accept_ra_mtu,
6266c2943f14SHarout Hedeshian 		.maxlen		= sizeof(int),
6267c2943f14SHarout Hedeshian 		.mode		= 0644,
6268c2943f14SHarout Hedeshian 		.proc_handler	= proc_dointvec,
6269c2943f14SHarout Hedeshian 	},
6270c2943f14SHarout Hedeshian 	{
62713d1bec99SHannes Frederic Sowa 		.procname	= "stable_secret",
62723d1bec99SHannes Frederic Sowa 		.data		= &ipv6_devconf.stable_secret,
62733d1bec99SHannes Frederic Sowa 		.maxlen		= IPV6_MAX_STRLEN,
62743d1bec99SHannes Frederic Sowa 		.mode		= 0600,
62753d1bec99SHannes Frederic Sowa 		.proc_handler	= addrconf_sysctl_stable_secret,
62763d1bec99SHannes Frederic Sowa 	},
62773d1bec99SHannes Frederic Sowa 	{
62783985e8a3SErik Kline 		.procname	= "use_oif_addrs_only",
62793985e8a3SErik Kline 		.data		= &ipv6_devconf.use_oif_addrs_only,
62803985e8a3SErik Kline 		.maxlen		= sizeof(int),
62813985e8a3SErik Kline 		.mode		= 0644,
62823985e8a3SErik Kline 		.proc_handler	= proc_dointvec,
628335103d11SAndy Gospodarek 	},
628435103d11SAndy Gospodarek 	{
628535103d11SAndy Gospodarek 		.procname	= "ignore_routes_with_linkdown",
628635103d11SAndy Gospodarek 		.data		= &ipv6_devconf.ignore_routes_with_linkdown,
628735103d11SAndy Gospodarek 		.maxlen		= sizeof(int),
628835103d11SAndy Gospodarek 		.mode		= 0644,
628935103d11SAndy Gospodarek 		.proc_handler	= addrconf_sysctl_ignore_routes_with_linkdown,
62903985e8a3SErik Kline 	},
62913985e8a3SErik Kline 	{
6292abbc3043SJohannes Berg 		.procname	= "drop_unicast_in_l2_multicast",
6293abbc3043SJohannes Berg 		.data		= &ipv6_devconf.drop_unicast_in_l2_multicast,
6294abbc3043SJohannes Berg 		.maxlen		= sizeof(int),
6295abbc3043SJohannes Berg 		.mode		= 0644,
6296abbc3043SJohannes Berg 		.proc_handler	= proc_dointvec,
6297abbc3043SJohannes Berg 	},
6298abbc3043SJohannes Berg 	{
62997a02bf89SJohannes Berg 		.procname	= "drop_unsolicited_na",
63007a02bf89SJohannes Berg 		.data		= &ipv6_devconf.drop_unsolicited_na,
63017a02bf89SJohannes Berg 		.maxlen		= sizeof(int),
63027a02bf89SJohannes Berg 		.mode		= 0644,
63037a02bf89SJohannes Berg 		.proc_handler	= proc_dointvec,
63047a02bf89SJohannes Berg 	},
63057a02bf89SJohannes Berg 	{
6306f1705ec1SDavid Ahern 		.procname	= "keep_addr_on_down",
6307f1705ec1SDavid Ahern 		.data		= &ipv6_devconf.keep_addr_on_down,
6308f1705ec1SDavid Ahern 		.maxlen		= sizeof(int),
6309f1705ec1SDavid Ahern 		.mode		= 0644,
6310f1705ec1SDavid Ahern 		.proc_handler	= proc_dointvec,
6311f1705ec1SDavid Ahern 
6312f1705ec1SDavid Ahern 	},
6313f1705ec1SDavid Ahern 	{
63141ababebaSDavid Lebrun 		.procname	= "seg6_enabled",
63151ababebaSDavid Lebrun 		.data		= &ipv6_devconf.seg6_enabled,
63161ababebaSDavid Lebrun 		.maxlen		= sizeof(int),
63171ababebaSDavid Lebrun 		.mode		= 0644,
63181ababebaSDavid Lebrun 		.proc_handler	= proc_dointvec,
63191ababebaSDavid Lebrun 	},
6320bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
6321bf355b8dSDavid Lebrun 	{
6322bf355b8dSDavid Lebrun 		.procname	= "seg6_require_hmac",
6323bf355b8dSDavid Lebrun 		.data		= &ipv6_devconf.seg6_require_hmac,
6324bf355b8dSDavid Lebrun 		.maxlen		= sizeof(int),
6325bf355b8dSDavid Lebrun 		.mode		= 0644,
6326bf355b8dSDavid Lebrun 		.proc_handler	= proc_dointvec,
6327bf355b8dSDavid Lebrun 	},
6328bf355b8dSDavid Lebrun #endif
63291ababebaSDavid Lebrun 	{
6330adc176c5SErik Nordmark 		.procname       = "enhanced_dad",
6331adc176c5SErik Nordmark 		.data           = &ipv6_devconf.enhanced_dad,
6332adc176c5SErik Nordmark 		.maxlen         = sizeof(int),
6333adc176c5SErik Nordmark 		.mode           = 0644,
6334adc176c5SErik Nordmark 		.proc_handler   = proc_dointvec,
6335adc176c5SErik Nordmark 	},
6336adc176c5SErik Nordmark 	{
6337d35a00b8SFelix Jia 		.procname		= "addr_gen_mode",
6338d35a00b8SFelix Jia 		.data			= &ipv6_devconf.addr_gen_mode,
6339d35a00b8SFelix Jia 		.maxlen			= sizeof(int),
6340d35a00b8SFelix Jia 		.mode			= 0644,
6341d35a00b8SFelix Jia 		.proc_handler	= addrconf_sysctl_addr_gen_mode,
6342d35a00b8SFelix Jia 	},
6343d35a00b8SFelix Jia 	{
6344df789fe7SDavid Forster 		.procname       = "disable_policy",
6345df789fe7SDavid Forster 		.data           = &ipv6_devconf.disable_policy,
6346df789fe7SDavid Forster 		.maxlen         = sizeof(int),
6347df789fe7SDavid Forster 		.mode           = 0644,
6348df789fe7SDavid Forster 		.proc_handler   = addrconf_sysctl_disable_policy,
6349df789fe7SDavid Forster 	},
6350df789fe7SDavid Forster 	{
6351f8572d8fSEric W. Biederman 		/* sentinel */
63521da177e4SLinus Torvalds 	}
63531da177e4SLinus Torvalds };
63541da177e4SLinus Torvalds 
6355bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6356f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
63571da177e4SLinus Torvalds {
635829c994e3SNicolas Dichtel 	int i, ifindex;
6359607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
63606105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
63611dab6222SPavel Emelyanov 
6362607ea7cdSKonstantin Khlebnikov 	table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6363607ea7cdSKonstantin Khlebnikov 	if (!table)
6364f68635e6SPavel Emelyanov 		goto out;
6365f68635e6SPavel Emelyanov 
6366607ea7cdSKonstantin Khlebnikov 	for (i = 0; table[i].data; i++) {
6367607ea7cdSKonstantin Khlebnikov 		table[i].data += (char *)p - (char *)&ipv6_devconf;
6368cb9e684eSMaciej Żenczykowski 		/* If one of these is already set, then it is not safe to
6369cb9e684eSMaciej Żenczykowski 		 * overwrite either of them: this makes proc_dointvec_minmax
6370cb9e684eSMaciej Żenczykowski 		 * usable.
6371cb9e684eSMaciej Żenczykowski 		 */
6372cb9e684eSMaciej Żenczykowski 		if (!table[i].extra1 && !table[i].extra2) {
6373607ea7cdSKonstantin Khlebnikov 			table[i].extra1 = idev; /* embedded; no ref */
6374607ea7cdSKonstantin Khlebnikov 			table[i].extra2 = net;
63751da177e4SLinus Torvalds 		}
6376cb9e684eSMaciej Żenczykowski 	}
63771da177e4SLinus Torvalds 
63786105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
63791da177e4SLinus Torvalds 
6380607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = register_net_sysctl(net, path, table);
6381607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
63826105e293SEric W. Biederman 		goto free;
6383f68635e6SPavel Emelyanov 
638429c994e3SNicolas Dichtel 	if (!strcmp(dev_name, "all"))
638529c994e3SNicolas Dichtel 		ifindex = NETCONFA_IFINDEX_ALL;
638629c994e3SNicolas Dichtel 	else if (!strcmp(dev_name, "default"))
638729c994e3SNicolas Dichtel 		ifindex = NETCONFA_IFINDEX_DEFAULT;
638829c994e3SNicolas Dichtel 	else
638929c994e3SNicolas Dichtel 		ifindex = idev->dev->ifindex;
639085b3daadSDavid Ahern 	inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
639185b3daadSDavid Ahern 				     ifindex, p);
639295897312SPavel Emelyanov 	return 0;
63931da177e4SLinus Torvalds 
63941da177e4SLinus Torvalds free:
6395607ea7cdSKonstantin Khlebnikov 	kfree(table);
6396f68635e6SPavel Emelyanov out:
639795897312SPavel Emelyanov 	return -ENOBUFS;
63981da177e4SLinus Torvalds }
63991da177e4SLinus Torvalds 
640023452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net,
640123452170SDavid Ahern 					 struct ipv6_devconf *p, int ifindex)
6402f52295a9SPavel Emelyanov {
6403607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
6404f52295a9SPavel Emelyanov 
6405607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
6406408c4768SPavel Emelyanov 		return;
6407408c4768SPavel Emelyanov 
6408607ea7cdSKonstantin Khlebnikov 	table = p->sysctl_header->ctl_table_arg;
6409607ea7cdSKonstantin Khlebnikov 	unregister_net_sysctl_table(p->sysctl_header);
6410607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = NULL;
6411607ea7cdSKonstantin Khlebnikov 	kfree(table);
641223452170SDavid Ahern 
641323452170SDavid Ahern 	inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
64141da177e4SLinus Torvalds }
6415408c4768SPavel Emelyanov 
6416a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev)
6417408c4768SPavel Emelyanov {
6418a317a2f1SWANG Cong 	int err;
6419a317a2f1SWANG Cong 
6420a317a2f1SWANG Cong 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
6421a317a2f1SWANG Cong 		return -EINVAL;
6422a317a2f1SWANG Cong 
6423a317a2f1SWANG Cong 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6424f8572d8fSEric W. Biederman 				    &ndisc_ifinfo_sysctl_change);
6425a317a2f1SWANG Cong 	if (err)
6426a317a2f1SWANG Cong 		return err;
6427a317a2f1SWANG Cong 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6428f8572d8fSEric W. Biederman 					 idev, &idev->cnf);
6429a317a2f1SWANG Cong 	if (err)
6430a317a2f1SWANG Cong 		neigh_sysctl_unregister(idev->nd_parms);
6431a317a2f1SWANG Cong 
6432a317a2f1SWANG Cong 	return err;
6433408c4768SPavel Emelyanov }
6434408c4768SPavel Emelyanov 
6435408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6436408c4768SPavel Emelyanov {
643723452170SDavid Ahern 	__addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
643823452170SDavid Ahern 				     idev->dev->ifindex);
6439408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
64401da177e4SLinus Torvalds }
64411da177e4SLinus Torvalds 
64421da177e4SLinus Torvalds 
64431da177e4SLinus Torvalds #endif
64441da177e4SLinus Torvalds 
64452c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
6446e0da5a48SPavel Emelyanov {
6447a79ca223SHong Zhiguo 	int err = -ENOMEM;
6448e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
6449e0da5a48SPavel Emelyanov 
6450a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
645163159f29SIan Morris 	if (!all)
6452e0da5a48SPavel Emelyanov 		goto err_alloc_all;
6453e0da5a48SPavel Emelyanov 
6454a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
645563159f29SIan Morris 	if (!dflt)
6456e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
6457a79ca223SHong Zhiguo 
645856d417b1SBrian Haley 	/* these will be inherited by all namespaces */
645956d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
646056d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6461e0da5a48SPavel Emelyanov 
64623d1bec99SHannes Frederic Sowa 	dflt->stable_secret.initialized = false;
64633d1bec99SHannes Frederic Sowa 	all->stable_secret.initialized = false;
64643d1bec99SHannes Frederic Sowa 
6465e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
6466e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
6467e0da5a48SPavel Emelyanov 
6468e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
6469f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
6470e0da5a48SPavel Emelyanov 	if (err < 0)
6471e0da5a48SPavel Emelyanov 		goto err_reg_all;
6472e0da5a48SPavel Emelyanov 
6473f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6474e0da5a48SPavel Emelyanov 	if (err < 0)
6475e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
6476e0da5a48SPavel Emelyanov #endif
6477e0da5a48SPavel Emelyanov 	return 0;
6478e0da5a48SPavel Emelyanov 
6479e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
6480e0da5a48SPavel Emelyanov err_reg_dflt:
648123452170SDavid Ahern 	__addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
6482e0da5a48SPavel Emelyanov err_reg_all:
6483e0da5a48SPavel Emelyanov 	kfree(dflt);
6484e0da5a48SPavel Emelyanov #endif
6485e0da5a48SPavel Emelyanov err_alloc_dflt:
6486e0da5a48SPavel Emelyanov 	kfree(all);
6487e0da5a48SPavel Emelyanov err_alloc_all:
6488e0da5a48SPavel Emelyanov 	return err;
6489e0da5a48SPavel Emelyanov }
6490e0da5a48SPavel Emelyanov 
64912c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
6492e0da5a48SPavel Emelyanov {
6493e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
649423452170SDavid Ahern 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
649523452170SDavid Ahern 				     NETCONFA_IFINDEX_DEFAULT);
649623452170SDavid Ahern 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
649723452170SDavid Ahern 				     NETCONFA_IFINDEX_ALL);
6498e0da5a48SPavel Emelyanov #endif
6499e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_dflt);
6500e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_all);
6501e0da5a48SPavel Emelyanov }
6502e0da5a48SPavel Emelyanov 
6503e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
6504e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
6505e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
6506e0da5a48SPavel Emelyanov };
6507e0da5a48SPavel Emelyanov 
6508207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = {
6509b382b191SThomas Graf 	.family		  = AF_INET6,
6510b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
6511b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
651211b1f828SDaniel Borkmann 	.validate_link_af = inet6_validate_link_af,
6513f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
6514b382b191SThomas Graf };
6515b382b191SThomas Graf 
65161da177e4SLinus Torvalds /*
65171da177e4SLinus Torvalds  *	Init / cleanup code
65181da177e4SLinus Torvalds  */
65191da177e4SLinus Torvalds 
65201da177e4SLinus Torvalds int __init addrconf_init(void)
65211da177e4SLinus Torvalds {
6522a317a2f1SWANG Cong 	struct inet6_dev *idev;
6523c2e21293Sstephen hemminger 	int i, err;
65242a8cc6c8SYOSHIFUJI Hideaki 
6525e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
6526e21e8467SStephen Hemminger 	if (err < 0) {
6527f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
6528f3213831SJoe Perches 			__func__, err);
65292cc6d2bfSNeil Horman 		goto out;
65302a8cc6c8SYOSHIFUJI Hideaki 	}
65311da177e4SLinus Torvalds 
65322cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
65332cc6d2bfSNeil Horman 	if (err < 0)
65342cc6d2bfSNeil Horman 		goto out_addrlabel;
6535e0da5a48SPavel Emelyanov 
6536c15b1ccaSHannes Frederic Sowa 	addrconf_wq = create_workqueue("ipv6_addrconf");
6537c15b1ccaSHannes Frederic Sowa 	if (!addrconf_wq) {
6538c15b1ccaSHannes Frederic Sowa 		err = -ENOMEM;
6539c15b1ccaSHannes Frederic Sowa 		goto out_nowq;
6540c15b1ccaSHannes Frederic Sowa 	}
6541c15b1ccaSHannes Frederic Sowa 
65421da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
65431da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
65441da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
65451da177e4SLinus Torvalds 	 * to other devices which are up.
65461da177e4SLinus Torvalds 	 *
65471da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
65481da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
65491da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
65501da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
65511da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
65521da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
65531da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
65541da177e4SLinus Torvalds 	 *
65551da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
65561da177e4SLinus Torvalds 	 * loopback_dev first by hand.
65571da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
65581da177e4SLinus Torvalds 	 * device and it being up should be removed.
65591da177e4SLinus Torvalds 	 */
65601da177e4SLinus Torvalds 	rtnl_lock();
6561a317a2f1SWANG Cong 	idev = ipv6_add_dev(init_net.loopback_dev);
65621da177e4SLinus Torvalds 	rtnl_unlock();
6563a317a2f1SWANG Cong 	if (IS_ERR(idev)) {
6564a317a2f1SWANG Cong 		err = PTR_ERR(idev);
6565e0da5a48SPavel Emelyanov 		goto errlo;
6566a317a2f1SWANG Cong 	}
65671da177e4SLinus Torvalds 
65682f460933SWANG Cong 	ip6_route_init_special_entries();
65692f460933SWANG Cong 
6570c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
6571c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6572c2e21293Sstephen hemminger 
65731da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
65741da177e4SLinus Torvalds 
6575c15b1ccaSHannes Frederic Sowa 	addrconf_verify();
6576c127ea2cSThomas Graf 
65773678a9d8Sstephen hemminger 	rtnl_af_register(&inet6_ops);
6578b382b191SThomas Graf 
6579c7ac8679SGreg Rose 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6580b97bac64SFlorian Westphal 			      0);
6581c127ea2cSThomas Graf 	if (err < 0)
6582c127ea2cSThomas Graf 		goto errout;
6583c127ea2cSThomas Graf 
6584c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
6585b97bac64SFlorian Westphal 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, 0);
6586b97bac64SFlorian Westphal 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, 0);
6587c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6588b97bac64SFlorian Westphal 			inet6_dump_ifaddr, 0);
6589c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6590b97bac64SFlorian Westphal 			inet6_dump_ifmcaddr, 0);
6591c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6592b97bac64SFlorian Westphal 			inet6_dump_ifacaddr, 0);
659376f8f6cbSNicolas Dichtel 	__rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
6594b97bac64SFlorian Westphal 			inet6_netconf_dump_devconf, 0);
6595c127ea2cSThomas Graf 
65962a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
65972a8cc6c8SYOSHIFUJI Hideaki 
65981da177e4SLinus Torvalds 	return 0;
6599c127ea2cSThomas Graf errout:
6600b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
6601c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
6602e0da5a48SPavel Emelyanov errlo:
6603c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
6604c15b1ccaSHannes Frederic Sowa out_nowq:
6605e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
66062cc6d2bfSNeil Horman out_addrlabel:
66072cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
66082cc6d2bfSNeil Horman out:
6609c127ea2cSThomas Graf 	return err;
66101da177e4SLinus Torvalds }
66111da177e4SLinus Torvalds 
661209f7709fSDaniel Lezcano void addrconf_cleanup(void)
66131da177e4SLinus Torvalds {
6614176c39afSDaniel Lezcano 	struct net_device *dev;
66151da177e4SLinus Torvalds 	int i;
66161da177e4SLinus Torvalds 
66171da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
6618e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
66192cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
66201da177e4SLinus Torvalds 
66215c45121dSFlorian Westphal 	rtnl_af_unregister(&inet6_ops);
66221da177e4SLinus Torvalds 
66235c45121dSFlorian Westphal 	rtnl_lock();
6624b382b191SThomas Graf 
6625176c39afSDaniel Lezcano 	/* clean dev list */
6626176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
6627176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
6628176c39afSDaniel Lezcano 			continue;
6629176c39afSDaniel Lezcano 		addrconf_ifdown(dev, 1);
6630176c39afSDaniel Lezcano 	}
6631176c39afSDaniel Lezcano 	addrconf_ifdown(init_net.loopback_dev, 2);
6632176c39afSDaniel Lezcano 
66331da177e4SLinus Torvalds 	/*
66341da177e4SLinus Torvalds 	 *	Check hash table.
66351da177e4SLinus Torvalds 	 */
66365c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
6637c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
6638c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
66395c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
6640c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
66411da177e4SLinus Torvalds 	rtnl_unlock();
6642c15b1ccaSHannes Frederic Sowa 
6643c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
66441da177e4SLinus Torvalds }
6645