xref: /openbmc/linux/net/ipv6/addrconf.c (revision bbea124bc99df968011e76eba105fe964a4eceab)
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
227*bbea124bSJoel 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
281*bbea124bSJoel 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 
306f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
30705297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev)
308f24e3d65SMitsuru Chinen {
30905297949SDavid S. Miller 	return !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 {
335c15b1ccaSHannes Frederic Sowa 	if (!delayed_work_pending(&ifp->dad_work))
336b7b1bfceSHannes Frederic Sowa 		in6_ifa_hold(ifp);
337c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
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. */
4291da177e4SLinus Torvalds 	in6_dev_hold(ndev);
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 
454f24e3d65SMitsuru Chinen 	if (netif_running(dev) && addrconf_qdisc_ok(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 
552136ba622SZhang Shengju 	if ((all || type == NETCONFA_FORWARDING) &&
553f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
554f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
555b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
556136ba622SZhang Shengju 	if ((all || type == NETCONFA_MC_FORWARDING) &&
557d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
558d67b8c61SNicolas Dichtel 			devconf->mc_forwarding) < 0)
559d67b8c61SNicolas Dichtel 		goto nla_put_failure;
560b1afce95SDavid S. Miller #endif
561136ba622SZhang Shengju 	if ((all || type == NETCONFA_PROXY_NEIGH) &&
562c92d5491Sstephen hemminger 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
563c92d5491Sstephen hemminger 		goto nla_put_failure;
564c92d5491Sstephen hemminger 
565136ba622SZhang Shengju 	if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
56635103d11SAndy Gospodarek 	    nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
56735103d11SAndy Gospodarek 			devconf->ignore_routes_with_linkdown) < 0)
56835103d11SAndy Gospodarek 		goto nla_put_failure;
56935103d11SAndy Gospodarek 
570053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
571053c095aSJohannes Berg 	return 0;
572f3a1bfb1SNicolas Dichtel 
573f3a1bfb1SNicolas Dichtel nla_put_failure:
574f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
575f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
576f3a1bfb1SNicolas Dichtel }
577f3a1bfb1SNicolas Dichtel 
578d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
579f3a1bfb1SNicolas Dichtel 				  struct ipv6_devconf *devconf)
580f3a1bfb1SNicolas Dichtel {
581f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
582f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
583f3a1bfb1SNicolas Dichtel 
584927265bcSEric Dumazet 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
58563159f29SIan Morris 	if (!skb)
586f3a1bfb1SNicolas Dichtel 		goto errout;
587f3a1bfb1SNicolas Dichtel 
588f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
589f3a1bfb1SNicolas Dichtel 					 RTM_NEWNETCONF, 0, type);
590f3a1bfb1SNicolas Dichtel 	if (err < 0) {
591f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
592f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
593f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
594f3a1bfb1SNicolas Dichtel 		goto errout;
595f3a1bfb1SNicolas Dichtel 	}
596927265bcSEric Dumazet 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
597f3a1bfb1SNicolas Dichtel 	return;
598f3a1bfb1SNicolas Dichtel errout:
599f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
600f3a1bfb1SNicolas Dichtel }
601f3a1bfb1SNicolas Dichtel 
60276f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
60376f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
60476f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
605c92d5491Sstephen hemminger 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
60635103d11SAndy Gospodarek 	[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]	= { .len = sizeof(int) },
60776f8f6cbSNicolas Dichtel };
60876f8f6cbSNicolas Dichtel 
60976f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
610661d2967SThomas Graf 				     struct nlmsghdr *nlh)
61176f8f6cbSNicolas Dichtel {
61276f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
61376f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
61476f8f6cbSNicolas Dichtel 	struct netconfmsg *ncm;
61576f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
61676f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
61776f8f6cbSNicolas Dichtel 	struct inet6_dev *in6_dev;
61876f8f6cbSNicolas Dichtel 	struct net_device *dev;
61976f8f6cbSNicolas Dichtel 	int ifindex;
62076f8f6cbSNicolas Dichtel 	int err;
62176f8f6cbSNicolas Dichtel 
62276f8f6cbSNicolas Dichtel 	err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
62376f8f6cbSNicolas Dichtel 			  devconf_ipv6_policy);
62476f8f6cbSNicolas Dichtel 	if (err < 0)
62576f8f6cbSNicolas Dichtel 		goto errout;
62676f8f6cbSNicolas Dichtel 
627a97eb33fSAnton Protopopov 	err = -EINVAL;
62876f8f6cbSNicolas Dichtel 	if (!tb[NETCONFA_IFINDEX])
62976f8f6cbSNicolas Dichtel 		goto errout;
63076f8f6cbSNicolas Dichtel 
63176f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
63276f8f6cbSNicolas Dichtel 	switch (ifindex) {
63376f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
63476f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
63576f8f6cbSNicolas Dichtel 		break;
63676f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
63776f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
63876f8f6cbSNicolas Dichtel 		break;
63976f8f6cbSNicolas Dichtel 	default:
64076f8f6cbSNicolas Dichtel 		dev = __dev_get_by_index(net, ifindex);
64163159f29SIan Morris 		if (!dev)
64276f8f6cbSNicolas Dichtel 			goto errout;
64376f8f6cbSNicolas Dichtel 		in6_dev = __in6_dev_get(dev);
64463159f29SIan Morris 		if (!in6_dev)
64576f8f6cbSNicolas Dichtel 			goto errout;
64676f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
64776f8f6cbSNicolas Dichtel 		break;
64876f8f6cbSNicolas Dichtel 	}
64976f8f6cbSNicolas Dichtel 
65076f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
651136ba622SZhang Shengju 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
65263159f29SIan Morris 	if (!skb)
65376f8f6cbSNicolas Dichtel 		goto errout;
65476f8f6cbSNicolas Dichtel 
65576f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
65676f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
65776f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
658136ba622SZhang Shengju 					 NETCONFA_ALL);
65976f8f6cbSNicolas Dichtel 	if (err < 0) {
66076f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
66176f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
66276f8f6cbSNicolas Dichtel 		kfree_skb(skb);
66376f8f6cbSNicolas Dichtel 		goto errout;
66476f8f6cbSNicolas Dichtel 	}
66576f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
66676f8f6cbSNicolas Dichtel errout:
66776f8f6cbSNicolas Dichtel 	return err;
66876f8f6cbSNicolas Dichtel }
66976f8f6cbSNicolas Dichtel 
6707a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
6717a674200SNicolas Dichtel 				      struct netlink_callback *cb)
6727a674200SNicolas Dichtel {
6737a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
6747a674200SNicolas Dichtel 	int h, s_h;
6757a674200SNicolas Dichtel 	int idx, s_idx;
6767a674200SNicolas Dichtel 	struct net_device *dev;
6777a674200SNicolas Dichtel 	struct inet6_dev *idev;
6787a674200SNicolas Dichtel 	struct hlist_head *head;
6797a674200SNicolas Dichtel 
6807a674200SNicolas Dichtel 	s_h = cb->args[0];
6817a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
6827a674200SNicolas Dichtel 
6837a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6847a674200SNicolas Dichtel 		idx = 0;
6857a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
6867a674200SNicolas Dichtel 		rcu_read_lock();
68763998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
68863998ac2SNicolas Dichtel 			  net->dev_base_seq;
6897a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
6907a674200SNicolas Dichtel 			if (idx < s_idx)
6917a674200SNicolas Dichtel 				goto cont;
6927a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
6937a674200SNicolas Dichtel 			if (!idev)
6947a674200SNicolas Dichtel 				goto cont;
6957a674200SNicolas Dichtel 
6967a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
6977a674200SNicolas Dichtel 						       &idev->cnf,
6987a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
6997a674200SNicolas Dichtel 						       cb->nlh->nlmsg_seq,
7007a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
7017a674200SNicolas Dichtel 						       NLM_F_MULTI,
702136ba622SZhang Shengju 						       NETCONFA_ALL) < 0) {
7037a674200SNicolas Dichtel 				rcu_read_unlock();
7047a674200SNicolas Dichtel 				goto done;
7057a674200SNicolas Dichtel 			}
70663998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7077a674200SNicolas Dichtel cont:
7087a674200SNicolas Dichtel 			idx++;
7097a674200SNicolas Dichtel 		}
7107a674200SNicolas Dichtel 		rcu_read_unlock();
7117a674200SNicolas Dichtel 	}
7127a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
7137a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
7147a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
7157a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
7167a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
7177a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
718136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
7197a674200SNicolas Dichtel 			goto done;
7207a674200SNicolas Dichtel 		else
7217a674200SNicolas Dichtel 			h++;
7227a674200SNicolas Dichtel 	}
7237a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
7247a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
7257a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
7267a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
7277a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
7287a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
729136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
7307a674200SNicolas Dichtel 			goto done;
7317a674200SNicolas Dichtel 		else
7327a674200SNicolas Dichtel 			h++;
7337a674200SNicolas Dichtel 	}
7347a674200SNicolas Dichtel done:
7357a674200SNicolas Dichtel 	cb->args[0] = h;
7367a674200SNicolas Dichtel 	cb->args[1] = idx;
7377a674200SNicolas Dichtel 
7387a674200SNicolas Dichtel 	return skb->len;
7397a674200SNicolas Dichtel }
7407a674200SNicolas Dichtel 
7411da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
7421da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
7431da177e4SLinus Torvalds {
7441da177e4SLinus Torvalds 	struct net_device *dev;
7451da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	if (!idev)
7481da177e4SLinus Torvalds 		return;
7491da177e4SLinus Torvalds 	dev = idev->dev;
7500187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
7510187bdfbSBen Hutchings 		dev_disable_lro(dev);
7521a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
7532c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
754f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
7552c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
7562c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
7572c5e8933SHannes Frederic Sowa 		} else {
758f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
7592c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
7602c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
7612c5e8933SHannes Frederic Sowa 		}
7621da177e4SLinus Torvalds 	}
763502a2ffdSstephen hemminger 
764502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
7652c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
7662c12a74cSMichal Wrobel 			continue;
7671da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
7681da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
7691da177e4SLinus Torvalds 		else
7701da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
7711da177e4SLinus Torvalds 	}
772f3a1bfb1SNicolas Dichtel 	inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
773f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 
777e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
7781da177e4SLinus Torvalds {
7791da177e4SLinus Torvalds 	struct net_device *dev;
7801da177e4SLinus Torvalds 	struct inet6_dev *idev;
7811da177e4SLinus Torvalds 
7824acd4945SBen Hutchings 	for_each_netdev(net, dev) {
7831da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
7841da177e4SLinus Torvalds 		if (idev) {
785e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
786e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
7871da177e4SLinus Torvalds 			if (changed)
7881da177e4SLinus Torvalds 				dev_forward_change(idev);
7891da177e4SLinus Torvalds 		}
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds }
792c8fecf22SPavel Emelyanov 
793013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
794c8fecf22SPavel Emelyanov {
795bff16c2fSPavel Emelyanov 	struct net *net;
796013d97e9SFrancesco Ruggeri 	int old;
797013d97e9SFrancesco Ruggeri 
798013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
799013d97e9SFrancesco Ruggeri 		return restart_syscall();
800bff16c2fSPavel Emelyanov 
801bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
802013d97e9SFrancesco Ruggeri 	old = *p;
803013d97e9SFrancesco Ruggeri 	*p = newf;
804c8fecf22SPavel Emelyanov 
805013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
806f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
807f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
808f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
809f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
810013d97e9SFrancesco Ruggeri 		rtnl_unlock();
811013d97e9SFrancesco Ruggeri 		return 0;
81288af182eSEric W. Biederman 	}
813b325fddbSStephen Hemminger 
814e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
815d26c638cSNicolas Dichtel 		int old_dflt = net->ipv6.devconf_dflt->forwarding;
816d26c638cSNicolas Dichtel 
817e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
818d26c638cSNicolas Dichtel 		if ((!newf) ^ (!old_dflt))
819d26c638cSNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
820d26c638cSNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
821d26c638cSNicolas Dichtel 						     net->ipv6.devconf_dflt);
822d26c638cSNicolas Dichtel 
823e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
824f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
825f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
826f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
827f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
828013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
829c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
8300187bdfbSBen Hutchings 	rtnl_unlock();
831c8fecf22SPavel Emelyanov 
832013d97e9SFrancesco Ruggeri 	if (newf)
8337b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
834b325fddbSStephen Hemminger 	return 1;
835c8fecf22SPavel Emelyanov }
83635103d11SAndy Gospodarek 
83735103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf)
83835103d11SAndy Gospodarek {
83935103d11SAndy Gospodarek 	struct net_device *dev;
84035103d11SAndy Gospodarek 	struct inet6_dev *idev;
84135103d11SAndy Gospodarek 
84235103d11SAndy Gospodarek 	for_each_netdev(net, dev) {
84335103d11SAndy Gospodarek 		idev = __in6_dev_get(dev);
84435103d11SAndy Gospodarek 		if (idev) {
84535103d11SAndy Gospodarek 			int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
84635103d11SAndy Gospodarek 
84735103d11SAndy Gospodarek 			idev->cnf.ignore_routes_with_linkdown = newf;
84835103d11SAndy Gospodarek 			if (changed)
84935103d11SAndy Gospodarek 				inet6_netconf_notify_devconf(dev_net(dev),
85035103d11SAndy Gospodarek 							     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
85135103d11SAndy Gospodarek 							     dev->ifindex,
85235103d11SAndy Gospodarek 							     &idev->cnf);
85335103d11SAndy Gospodarek 		}
85435103d11SAndy Gospodarek 	}
85535103d11SAndy Gospodarek }
85635103d11SAndy Gospodarek 
85735103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
85835103d11SAndy Gospodarek {
85935103d11SAndy Gospodarek 	struct net *net;
86035103d11SAndy Gospodarek 	int old;
86135103d11SAndy Gospodarek 
86235103d11SAndy Gospodarek 	if (!rtnl_trylock())
86335103d11SAndy Gospodarek 		return restart_syscall();
86435103d11SAndy Gospodarek 
86535103d11SAndy Gospodarek 	net = (struct net *)table->extra2;
86635103d11SAndy Gospodarek 	old = *p;
86735103d11SAndy Gospodarek 	*p = newf;
86835103d11SAndy Gospodarek 
86935103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
87035103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
87135103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
87235103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
87335103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_DEFAULT,
87435103d11SAndy Gospodarek 						     net->ipv6.devconf_dflt);
87535103d11SAndy Gospodarek 		rtnl_unlock();
87635103d11SAndy Gospodarek 		return 0;
87735103d11SAndy Gospodarek 	}
87835103d11SAndy Gospodarek 
87935103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
88035103d11SAndy Gospodarek 		net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
88135103d11SAndy Gospodarek 		addrconf_linkdown_change(net, newf);
88235103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
88335103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
88435103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
88535103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_ALL,
88635103d11SAndy Gospodarek 						     net->ipv6.devconf_all);
88735103d11SAndy Gospodarek 	}
88835103d11SAndy Gospodarek 	rtnl_unlock();
88935103d11SAndy Gospodarek 
89035103d11SAndy Gospodarek 	return 1;
89135103d11SAndy Gospodarek }
89235103d11SAndy Gospodarek 
8931da177e4SLinus Torvalds #endif
8941da177e4SLinus Torvalds 
8955c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
8961da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
8971da177e4SLinus Torvalds {
898c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
899547b792cSIlpo Järvinen 
9001da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
90191df42beSJoe Perches 	pr_debug("%s\n", __func__);
9021da177e4SLinus Torvalds #endif
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
9051da177e4SLinus Torvalds 
906c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
907c15b1ccaSHannes Frederic Sowa 		pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
908c15b1ccaSHannes Frederic Sowa 			  ifp);
9091da177e4SLinus Torvalds 
910e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
911f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
9121da177e4SLinus Torvalds 		return;
9131da177e4SLinus Torvalds 	}
91494e187c0SAmerigo Wang 	ip6_rt_put(ifp->rt);
9151da177e4SLinus Torvalds 
916e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds 
919e55ffac6SBrian Haley static void
920e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
921e55ffac6SBrian Haley {
922502a2ffdSstephen hemminger 	struct list_head *p;
9238a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
924e55ffac6SBrian Haley 
925e55ffac6SBrian Haley 	/*
926e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
927e55ffac6SBrian Haley 	 * global before linklocal.
928e55ffac6SBrian Haley 	 */
929502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
930502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
931502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
9328a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
933e55ffac6SBrian Haley 			break;
934e55ffac6SBrian Haley 	}
935e55ffac6SBrian Haley 
936b54c9b98SDavid S. Miller 	list_add_tail(&ifp->if_list, p);
937e55ffac6SBrian Haley }
938e55ffac6SBrian Haley 
939ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr)
9403eb84f49SYOSHIFUJI Hideaki {
941ddbe5032SEric Dumazet 	return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
9423eb84f49SYOSHIFUJI Hideaki }
9433eb84f49SYOSHIFUJI Hideaki 
9441da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds static struct inet6_ifaddr *
9473f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
9483f8f5298SJiri Pirko 	      const struct in6_addr *peer_addr, int pfxlen,
9498a226b2cSJiri Benc 	      int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
9501da177e4SLinus Torvalds {
951df789fe7SDavid Forster 	struct net *net = dev_net(idev->dev);
9521da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
9531da177e4SLinus Torvalds 	struct rt6_info *rt;
9543a88a81dSstephen hemminger 	unsigned int hash;
9551da177e4SLinus Torvalds 	int err = 0;
956d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
957d68b8270SYOSHIFUJI Hideaki 
958d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
959d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
960d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
961d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
962d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
9631da177e4SLinus Torvalds 
9648814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
9651da177e4SLinus Torvalds 	if (idev->dead) {
9661da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
9671da177e4SLinus Torvalds 		goto out2;
9681da177e4SLinus Torvalds 	}
9691da177e4SLinus Torvalds 
97056d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
9719bdd8d40SBrian Haley 		err = -EACCES;
9729bdd8d40SBrian Haley 		goto out2;
9739bdd8d40SBrian Haley 	}
9749bdd8d40SBrian Haley 
9755c578aedSstephen hemminger 	spin_lock(&addrconf_hash_lock);
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
978c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
979ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: already assigned\n");
9801da177e4SLinus Torvalds 		err = -EEXIST;
9811da177e4SLinus Torvalds 		goto out;
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 
984322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
9851da177e4SLinus Torvalds 
98663159f29SIan Morris 	if (!ifa) {
987ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: malloc failed\n");
9881da177e4SLinus Torvalds 		err = -ENOBUFS;
9891da177e4SLinus Torvalds 		goto out;
9901da177e4SLinus Torvalds 	}
9911da177e4SLinus Torvalds 
9928f031519SDavid S. Miller 	rt = addrconf_dst_alloc(idev, addr, false);
9931da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
9941da177e4SLinus Torvalds 		err = PTR_ERR(rt);
9951da177e4SLinus Torvalds 		goto out;
9961da177e4SLinus Torvalds 	}
9971da177e4SLinus Torvalds 
998df789fe7SDavid Forster 	if (net->ipv6.devconf_all->disable_policy ||
999df789fe7SDavid Forster 	    idev->cnf.disable_policy)
1000df789fe7SDavid Forster 		rt->dst.flags |= DST_NOPOLICY;
1001df789fe7SDavid Forster 
1002bba24896SJiri Pirko 	neigh_parms_data_state_setall(idev->nd_parms);
1003bba24896SJiri Pirko 
10044e3fd7a0SAlexey Dobriyan 	ifa->addr = *addr;
10053f8f5298SJiri Pirko 	if (peer_addr)
10063f8f5298SJiri Pirko 		ifa->peer_addr = *peer_addr;
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
1009c15b1ccaSHannes Frederic Sowa 	INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1010c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
10111da177e4SLinus Torvalds 	ifa->scope = scope;
10121da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
10131da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
10148a226b2cSJiri Benc 	ifa->valid_lft = valid_lft;
10158a226b2cSJiri Benc 	ifa->prefered_lft = prefered_lft;
10161da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
1017617fe29dSDaniel Borkmann 	ifa->tokenized = false;
10181da177e4SLinus Torvalds 
101957f5f544SKeir Fraser 	ifa->rt = rt;
102057f5f544SKeir Fraser 
10211da177e4SLinus Torvalds 	ifa->idev = idev;
10221da177e4SLinus Torvalds 	in6_dev_hold(idev);
10231da177e4SLinus Torvalds 	/* For caller */
10241da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	/* Add to big hash table */
1027ddbe5032SEric Dumazet 	hash = inet6_addr_hash(addr);
10281da177e4SLinus Torvalds 
10295c578aedSstephen hemminger 	hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
10305c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	write_lock(&idev->lock);
10331da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
1034e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
1037372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
10381da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
10391da177e4SLinus Torvalds 	}
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
10421da177e4SLinus Torvalds 	write_unlock(&idev->lock);
10431da177e4SLinus Torvalds out2:
10448814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
10451da177e4SLinus Torvalds 
1046fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
1047f88c91ddSCong Wang 		inet6addr_notifier_call_chain(NETDEV_UP, ifa);
10481da177e4SLinus Torvalds 	else {
10491da177e4SLinus Torvalds 		kfree(ifa);
10501da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
10511da177e4SLinus Torvalds 	}
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	return ifa;
10541da177e4SLinus Torvalds out:
10555c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
10561da177e4SLinus Torvalds 	goto out2;
10571da177e4SLinus Torvalds }
10581da177e4SLinus Torvalds 
10595b84efecSThomas Haller enum cleanup_prefix_rt_t {
10605b84efecSThomas Haller 	CLEANUP_PREFIX_RT_NOP,    /* no cleanup action for prefix route */
10615b84efecSThomas Haller 	CLEANUP_PREFIX_RT_DEL,    /* delete the prefix route */
10625b84efecSThomas Haller 	CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
10635b84efecSThomas Haller };
10645b84efecSThomas Haller 
10655b84efecSThomas Haller /*
10665b84efecSThomas Haller  * Check, whether the prefix for ifp would still need a prefix route
10675b84efecSThomas Haller  * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
10685b84efecSThomas Haller  * constants.
10695b84efecSThomas Haller  *
10705b84efecSThomas Haller  * 1) we don't purge prefix if address was not permanent.
10715b84efecSThomas Haller  *    prefix is managed by its own lifetime.
10725b84efecSThomas Haller  * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
10735b84efecSThomas Haller  * 3) if there are no addresses, delete prefix.
10745b84efecSThomas Haller  * 4) if there are still other permanent address(es),
10755b84efecSThomas Haller  *    corresponding prefix is still permanent.
10765b84efecSThomas Haller  * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
10775b84efecSThomas Haller  *    don't purge the prefix, assume user space is managing it.
10785b84efecSThomas Haller  * 6) otherwise, update prefix lifetime to the
10795b84efecSThomas Haller  *    longest valid lifetime among the corresponding
10805b84efecSThomas Haller  *    addresses on the device.
10815b84efecSThomas Haller  *    Note: subsequent RA will update lifetime.
10825b84efecSThomas Haller  **/
10835b84efecSThomas Haller static enum cleanup_prefix_rt_t
10845b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
10855b84efecSThomas Haller {
10865b84efecSThomas Haller 	struct inet6_ifaddr *ifa;
10875b84efecSThomas Haller 	struct inet6_dev *idev = ifp->idev;
10885b84efecSThomas Haller 	unsigned long lifetime;
10895b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
10905b84efecSThomas Haller 
10915b84efecSThomas Haller 	*expires = jiffies;
10925b84efecSThomas Haller 
10935b84efecSThomas Haller 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
10945b84efecSThomas Haller 		if (ifa == ifp)
10955b84efecSThomas Haller 			continue;
10965b84efecSThomas Haller 		if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
10975b84efecSThomas Haller 				       ifp->prefix_len))
10985b84efecSThomas Haller 			continue;
10995b84efecSThomas Haller 		if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
11005b84efecSThomas Haller 			return CLEANUP_PREFIX_RT_NOP;
11015b84efecSThomas Haller 
11025b84efecSThomas Haller 		action = CLEANUP_PREFIX_RT_EXPIRE;
11035b84efecSThomas Haller 
11045b84efecSThomas Haller 		spin_lock(&ifa->lock);
11055b84efecSThomas Haller 
11065b84efecSThomas Haller 		lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
11075b84efecSThomas Haller 		/*
11085b84efecSThomas Haller 		 * Note: Because this address is
11095b84efecSThomas Haller 		 * not permanent, lifetime <
11105b84efecSThomas Haller 		 * LONG_MAX / HZ here.
11115b84efecSThomas Haller 		 */
11125b84efecSThomas Haller 		if (time_before(*expires, ifa->tstamp + lifetime * HZ))
11135b84efecSThomas Haller 			*expires = ifa->tstamp + lifetime * HZ;
11145b84efecSThomas Haller 		spin_unlock(&ifa->lock);
11155b84efecSThomas Haller 	}
11165b84efecSThomas Haller 
11175b84efecSThomas Haller 	return action;
11185b84efecSThomas Haller }
11195b84efecSThomas Haller 
11205b84efecSThomas Haller static void
11215b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
11225b84efecSThomas Haller {
11235b84efecSThomas Haller 	struct rt6_info *rt;
11245b84efecSThomas Haller 
11255b84efecSThomas Haller 	rt = addrconf_get_prefix_route(&ifp->addr,
11265b84efecSThomas Haller 				       ifp->prefix_len,
11275b84efecSThomas Haller 				       ifp->idev->dev,
11285b84efecSThomas Haller 				       0, RTF_GATEWAY | RTF_DEFAULT);
11295b84efecSThomas Haller 	if (rt) {
11305b84efecSThomas Haller 		if (del_rt)
11315b84efecSThomas Haller 			ip6_del_rt(rt);
11325b84efecSThomas Haller 		else {
11335b84efecSThomas Haller 			if (!(rt->rt6i_flags & RTF_EXPIRES))
11345b84efecSThomas Haller 				rt6_set_expires(rt, expires);
11355b84efecSThomas Haller 			ip6_rt_put(rt);
11365b84efecSThomas Haller 		}
11375b84efecSThomas Haller 	}
11385b84efecSThomas Haller }
11395b84efecSThomas Haller 
11405b84efecSThomas Haller 
11411da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
11441da177e4SLinus Torvalds {
11454c5ff6a6SHerbert Xu 	int state;
11465b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
11475b84efecSThomas Haller 	unsigned long expires;
11481da177e4SLinus Torvalds 
1149c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
1150c15b1ccaSHannes Frederic Sowa 
11518e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
11524c5ff6a6SHerbert Xu 	state = ifp->state;
1153e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
11548e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
11554c5ff6a6SHerbert Xu 
11564c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
11574c5ff6a6SHerbert Xu 		goto out;
11581da177e4SLinus Torvalds 
11595c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
11605c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
11615c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
11621da177e4SLinus Torvalds 
11635b84efecSThomas Haller 	write_lock_bh(&ifp->idev->lock);
11645d9efa7eSDavid S. Miller 
11651da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
1166372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
11671da177e4SLinus Torvalds 		if (ifp->ifpub) {
11681da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
11691da177e4SLinus Torvalds 			ifp->ifpub = NULL;
11701da177e4SLinus Torvalds 		}
11711da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
11745b84efecSThomas Haller 	if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
11755b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &expires);
11765b84efecSThomas Haller 
1177502a2ffdSstephen hemminger 	list_del_init(&ifp->if_list);
11781da177e4SLinus Torvalds 	__in6_ifa_put(ifp);
1179502a2ffdSstephen hemminger 
11805b84efecSThomas Haller 	write_unlock_bh(&ifp->idev->lock);
11811da177e4SLinus Torvalds 
1182c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
1183b2238566SAndrey Vagin 
11841da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
11851da177e4SLinus Torvalds 
1186f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
11871da177e4SLinus Torvalds 
11885b84efecSThomas Haller 	if (action != CLEANUP_PREFIX_RT_NOP) {
11895b84efecSThomas Haller 		cleanup_prefix_route(ifp, expires,
11905b84efecSThomas Haller 			action == CLEANUP_PREFIX_RT_DEL);
11911da177e4SLinus Torvalds 	}
11921da177e4SLinus Torvalds 
1193c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1194c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
11954c5ff6a6SHerbert Xu out:
11961da177e4SLinus Torvalds 	in6_ifa_put(ifp);
11971da177e4SLinus Torvalds }
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
12001da177e4SLinus Torvalds {
12011da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
12021da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
120376f793e3SLorenzo Colitti 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1204eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
12051da177e4SLinus Torvalds 	int tmp_plen;
12061da177e4SLinus Torvalds 	int ret = 0;
120795c385b4SNeil Horman 	u32 addr_flags;
120876f793e3SLorenzo Colitti 	unsigned long now = jiffies;
120976506a98SJiri Bohac 	long max_desync_factor;
12107aa8e63fSJiri Bohac 	s32 cnf_temp_preferred_lft;
12111da177e4SLinus Torvalds 
121253bd6749SJiri Pirko 	write_lock_bh(&idev->lock);
12131da177e4SLinus Torvalds 	if (ift) {
12141da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
12151da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
12161da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
12171da177e4SLinus Torvalds 		tmpaddr = &addr;
12181da177e4SLinus Torvalds 	} else {
12191da177e4SLinus Torvalds 		tmpaddr = NULL;
12201da177e4SLinus Torvalds 	}
12211da177e4SLinus Torvalds retry:
12221da177e4SLinus Torvalds 	in6_dev_hold(idev);
12231da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
122453bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1225f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
12261da177e4SLinus Torvalds 		in6_dev_put(idev);
12271da177e4SLinus Torvalds 		ret = -1;
12281da177e4SLinus Torvalds 		goto out;
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
12311da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
12321da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
12331da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
123453bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1235f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1236f3213831SJoe Perches 			__func__);
12371da177e4SLinus Torvalds 		in6_dev_put(idev);
12381da177e4SLinus Torvalds 		ret = -1;
12391da177e4SLinus Torvalds 		goto out;
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
12421da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
12439d6280daSJiri Bohac 	ipv6_try_regen_rndid(idev, tmpaddr);
12441da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
124576f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
12461da177e4SLinus Torvalds 
1247eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1248eac55bf9SBenoit Boissinot 			idev->cnf.dad_transmits *
12491f9248e5SJiri Pirko 			NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
125076506a98SJiri Bohac 
125176506a98SJiri Bohac 	/* recalculate max_desync_factor each time and update
125276506a98SJiri Bohac 	 * idev->desync_factor if it's larger
125376506a98SJiri Bohac 	 */
12547aa8e63fSJiri Bohac 	cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
125576506a98SJiri Bohac 	max_desync_factor = min_t(__u32,
125676506a98SJiri Bohac 				  idev->cnf.max_desync_factor,
12577aa8e63fSJiri Bohac 				  cnf_temp_preferred_lft - regen_advance);
125876506a98SJiri Bohac 
125976506a98SJiri Bohac 	if (unlikely(idev->desync_factor > max_desync_factor)) {
126076506a98SJiri Bohac 		if (max_desync_factor > 0) {
126176506a98SJiri Bohac 			get_random_bytes(&idev->desync_factor,
126276506a98SJiri Bohac 					 sizeof(idev->desync_factor));
126376506a98SJiri Bohac 			idev->desync_factor %= max_desync_factor;
126476506a98SJiri Bohac 		} else {
126576506a98SJiri Bohac 			idev->desync_factor = 0;
126676506a98SJiri Bohac 		}
126776506a98SJiri Bohac 	}
126876506a98SJiri Bohac 
126976506a98SJiri Bohac 	tmp_valid_lft = min_t(__u32,
127076506a98SJiri Bohac 			      ifp->valid_lft,
127176506a98SJiri Bohac 			      idev->cnf.temp_valid_lft + age);
12727aa8e63fSJiri Bohac 	tmp_prefered_lft = cnf_temp_preferred_lft + age -
127376506a98SJiri Bohac 			    idev->desync_factor;
127476506a98SJiri Bohac 	tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
127576506a98SJiri Bohac 	tmp_plen = ifp->prefix_len;
127676506a98SJiri Bohac 	tmp_tstamp = ifp->tstamp;
127776506a98SJiri Bohac 	spin_unlock_bh(&ifp->lock);
127876506a98SJiri Bohac 
127953bd6749SJiri Pirko 	write_unlock_bh(&idev->lock);
128095c385b4SNeil Horman 
1281eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1282eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1283eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1284eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1285ecab6701SHeiner Kallweit 	 * Use age calculation as in addrconf_verify to avoid unnecessary
1286ecab6701SHeiner Kallweit 	 * temporary addresses being generated.
1287eac55bf9SBenoit Boissinot 	 */
1288ecab6701SHeiner Kallweit 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1289ecab6701SHeiner Kallweit 	if (tmp_prefered_lft <= regen_advance + age) {
1290eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1291eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1292eac55bf9SBenoit Boissinot 		ret = -1;
1293eac55bf9SBenoit Boissinot 		goto out;
1294eac55bf9SBenoit Boissinot 	}
1295eac55bf9SBenoit Boissinot 
129695c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
129795c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
129895c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
129995c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
130095c385b4SNeil Horman 
13014b08a8f1SHannes Frederic Sowa 	ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
13028a226b2cSJiri Benc 			    ipv6_addr_scope(&addr), addr_flags,
13034b08a8f1SHannes Frederic Sowa 			    tmp_valid_lft, tmp_prefered_lft);
13044b08a8f1SHannes Frederic Sowa 	if (IS_ERR(ift)) {
13051da177e4SLinus Torvalds 		in6_ifa_put(ifp);
13061da177e4SLinus Torvalds 		in6_dev_put(idev);
1307f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
13081da177e4SLinus Torvalds 		tmpaddr = &addr;
130953bd6749SJiri Pirko 		write_lock_bh(&idev->lock);
13101da177e4SLinus Torvalds 		goto retry;
13111da177e4SLinus Torvalds 	}
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
13141da177e4SLinus Torvalds 	ift->ifpub = ifp;
131576f793e3SLorenzo Colitti 	ift->cstamp = now;
13161da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
13171da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
13181da177e4SLinus Torvalds 
1319cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
13201da177e4SLinus Torvalds 	in6_ifa_put(ift);
13211da177e4SLinus Torvalds 	in6_dev_put(idev);
13221da177e4SLinus Torvalds out:
13231da177e4SLinus Torvalds 	return ret;
13241da177e4SLinus Torvalds }
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds /*
1327072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
13281da177e4SLinus Torvalds  */
1329a9b05723SYOSHIFUJI Hideaki enum {
1330a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1331a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1332a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1333a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1334a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1335a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1336a9b05723SYOSHIFUJI Hideaki #endif
1337a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1338a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1339a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1340a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1341a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
13427fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
13437fd2561eSErik Kline 	IPV6_SADDR_RULE_NOT_OPTIMISTIC,
13447fd2561eSErik Kline #endif
1345a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1346072047e4SYOSHIFUJI Hideaki };
1347072047e4SYOSHIFUJI Hideaki 
1348a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1349a9b05723SYOSHIFUJI Hideaki 	int			rule;
1350a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1351a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1352a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1353a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1354a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1355a9b05723SYOSHIFUJI Hideaki };
1356a9b05723SYOSHIFUJI Hideaki 
1357a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
13589acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1359a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1360a9b05723SYOSHIFUJI Hideaki 	int scope;
1361a9b05723SYOSHIFUJI Hideaki 	int label;
13627cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1363a9b05723SYOSHIFUJI Hideaki };
1364072047e4SYOSHIFUJI Hideaki 
1365b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
13661da177e4SLinus Torvalds {
136745bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1368072047e4SYOSHIFUJI Hideaki 		return 1;
1369072047e4SYOSHIFUJI Hideaki 	return 0;
13701da177e4SLinus Torvalds }
13711da177e4SLinus Torvalds 
13727fd2561eSErik Kline static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
13737fd2561eSErik Kline {
13747fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
13757fd2561eSErik Kline 	return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
13767fd2561eSErik Kline #else
13777fd2561eSErik Kline 	return false;
13787fd2561eSErik Kline #endif
13797fd2561eSErik Kline }
13807fd2561eSErik Kline 
13813de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
13823de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1383a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1384a9b05723SYOSHIFUJI Hideaki 			       int i)
1385a9b05723SYOSHIFUJI Hideaki {
1386a9b05723SYOSHIFUJI Hideaki 	int ret;
1387a9b05723SYOSHIFUJI Hideaki 
1388a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1389a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1390a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1391a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1392a9b05723SYOSHIFUJI Hideaki 			break;
1393a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1394a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1395a9b05723SYOSHIFUJI Hideaki 			break;
1396a9b05723SYOSHIFUJI Hideaki 		default:
1397a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1398a9b05723SYOSHIFUJI Hideaki 		}
1399a9b05723SYOSHIFUJI Hideaki 		goto out;
1400a9b05723SYOSHIFUJI Hideaki 	}
1401a9b05723SYOSHIFUJI Hideaki 
1402a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1403a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1404a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1405a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1406a9b05723SYOSHIFUJI Hideaki 		break;
1407a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1408a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1409a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1410a9b05723SYOSHIFUJI Hideaki 		break;
1411a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1412a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1413a9b05723SYOSHIFUJI Hideaki 		 *
1414a9b05723SYOSHIFUJI Hideaki 		 *      ret
1415a9b05723SYOSHIFUJI Hideaki 		 *       ^
1416a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1417a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1418a9b05723SYOSHIFUJI Hideaki 		 *       |
1419a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1420a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1421a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1422db9c7c39Sstephen hemminger 		 *  B-15 |    \        if scope is enough for destination.
1423a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1424a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1425a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1426a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1427a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1428a9b05723SYOSHIFUJI Hideaki 		 *
1429a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1430a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1431a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1432a9b05723SYOSHIFUJI Hideaki 		 */
1433a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1434a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1435a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1436a9b05723SYOSHIFUJI Hideaki 		else
1437a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1438a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1439a9b05723SYOSHIFUJI Hideaki 		break;
1440a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
14417fd2561eSErik Kline 	    {
1442a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
14437fd2561eSErik Kline 		u8 avoid = IFA_F_DEPRECATED;
14447fd2561eSErik Kline 
14457fd2561eSErik Kline 		if (!ipv6_use_optimistic_addr(score->ifa->idev))
14467fd2561eSErik Kline 			avoid |= IFA_F_OPTIMISTIC;
1447a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
14487fd2561eSErik Kline 		      !(score->ifa->flags & avoid);
1449a9b05723SYOSHIFUJI Hideaki 		break;
14507fd2561eSErik Kline 	    }
1451a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1452a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
14537cbca67cSYOSHIFUJI Hideaki 	    {
1454a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
14557cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
14567cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1457a9b05723SYOSHIFUJI Hideaki 		break;
14587cbca67cSYOSHIFUJI Hideaki 	    }
1459a9b05723SYOSHIFUJI Hideaki #endif
1460a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1461a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1462a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1463a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1464a9b05723SYOSHIFUJI Hideaki 		break;
1465a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1466a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
14673de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
14683de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1469a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1470a9b05723SYOSHIFUJI Hideaki 		break;
1471a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
14727cbca67cSYOSHIFUJI Hideaki 	    {
1473a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
147425985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1475a9b05723SYOSHIFUJI Hideaki 		 */
14767cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
14777cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
14787cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
14797cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1480a9b05723SYOSHIFUJI Hideaki 		break;
14817cbca67cSYOSHIFUJI Hideaki 	    }
1482a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1483a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1484a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1485a9b05723SYOSHIFUJI Hideaki 		 */
1486a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1487a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1488a9b05723SYOSHIFUJI Hideaki 		break;
1489a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1490a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
149191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
149291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
149391b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
149491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1495a9b05723SYOSHIFUJI Hideaki 		break;
14967fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
14977fd2561eSErik Kline 	case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
14987fd2561eSErik Kline 		/* Optimistic addresses still have lower precedence than other
14997fd2561eSErik Kline 		 * preferred addresses.
15007fd2561eSErik Kline 		 */
15017fd2561eSErik Kline 		ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
15027fd2561eSErik Kline 		break;
15037fd2561eSErik Kline #endif
1504a9b05723SYOSHIFUJI Hideaki 	default:
1505a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1506a9b05723SYOSHIFUJI Hideaki 	}
1507a9b05723SYOSHIFUJI Hideaki 
1508a9b05723SYOSHIFUJI Hideaki 	if (ret)
1509a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1510a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1511a9b05723SYOSHIFUJI Hideaki out:
1512a9b05723SYOSHIFUJI Hideaki 	return ret;
1513a9b05723SYOSHIFUJI Hideaki }
1514a9b05723SYOSHIFUJI Hideaki 
1515c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net,
15169131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_dst *dst,
15179131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct inet6_dev *idev,
1518c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_score *scores,
1519c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				int hiscore_idx)
15201da177e4SLinus Torvalds {
1521c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1522072047e4SYOSHIFUJI Hideaki 
15231da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1524502a2ffdSstephen hemminger 	list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1525a9b05723SYOSHIFUJI Hideaki 		int i;
1526072047e4SYOSHIFUJI Hideaki 
1527a9b05723SYOSHIFUJI Hideaki 		/*
15286b3ae80aSYOSHIFUJI Hideaki 		 * - Tentative Address (RFC2462 section 5.4)
15296b3ae80aSYOSHIFUJI Hideaki 		 *  - A tentative address is not considered
15306b3ae80aSYOSHIFUJI Hideaki 		 *    "assigned to an interface" in the traditional
153195c385b4SNeil Horman 		 *    sense, unless it is also flagged as optimistic.
15326b3ae80aSYOSHIFUJI Hideaki 		 * - Candidate Source Address (section 4)
1533072047e4SYOSHIFUJI Hideaki 		 *  - In any case, anycast addresses, multicast
1534072047e4SYOSHIFUJI Hideaki 		 *    addresses, and the unspecified address MUST
1535072047e4SYOSHIFUJI Hideaki 		 *    NOT be included in a candidate set.
1536072047e4SYOSHIFUJI Hideaki 		 */
1537a9b05723SYOSHIFUJI Hideaki 		if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1538a9b05723SYOSHIFUJI Hideaki 		    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
15396b3ae80aSYOSHIFUJI Hideaki 			continue;
1540a9b05723SYOSHIFUJI Hideaki 
1541a9b05723SYOSHIFUJI Hideaki 		score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1542a9b05723SYOSHIFUJI Hideaki 
1543a9b05723SYOSHIFUJI Hideaki 		if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1544a9b05723SYOSHIFUJI Hideaki 			     score->addr_type & IPV6_ADDR_MULTICAST)) {
1545ba7a46f1SJoe Perches 			net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
15469131f3deSYOSHIFUJI Hideaki/吉藤英明 					    idev->dev->name);
15471da177e4SLinus Torvalds 			continue;
1548072047e4SYOSHIFUJI Hideaki 		}
1549072047e4SYOSHIFUJI Hideaki 
1550a9b05723SYOSHIFUJI Hideaki 		score->rule = -1;
1551a9b05723SYOSHIFUJI Hideaki 		bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1552072047e4SYOSHIFUJI Hideaki 
1553a9b05723SYOSHIFUJI Hideaki 		for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1554a9b05723SYOSHIFUJI Hideaki 			int minihiscore, miniscore;
1555072047e4SYOSHIFUJI Hideaki 
15569131f3deSYOSHIFUJI Hideaki/吉藤英明 			minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
15579131f3deSYOSHIFUJI Hideaki/吉藤英明 			miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1558072047e4SYOSHIFUJI Hideaki 
1559a9b05723SYOSHIFUJI Hideaki 			if (minihiscore > miniscore) {
1560a9b05723SYOSHIFUJI Hideaki 				if (i == IPV6_SADDR_RULE_SCOPE &&
1561a9b05723SYOSHIFUJI Hideaki 				    score->scopedist > 0) {
1562a9b05723SYOSHIFUJI Hideaki 					/*
1563a9b05723SYOSHIFUJI Hideaki 					 * special case:
1564a9b05723SYOSHIFUJI Hideaki 					 * each remaining entry
1565a9b05723SYOSHIFUJI Hideaki 					 * has too small (not enough)
1566a9b05723SYOSHIFUJI Hideaki 					 * scope, because ifa entries
1567a9b05723SYOSHIFUJI Hideaki 					 * are sorted by their scope
1568a9b05723SYOSHIFUJI Hideaki 					 * values.
1569072047e4SYOSHIFUJI Hideaki 					 */
15709131f3deSYOSHIFUJI Hideaki/吉藤英明 					goto out;
1571072047e4SYOSHIFUJI Hideaki 				}
1572a9b05723SYOSHIFUJI Hideaki 				break;
1573a9b05723SYOSHIFUJI Hideaki 			} else if (minihiscore < miniscore) {
1574a9b05723SYOSHIFUJI Hideaki 				if (hiscore->ifa)
1575a9b05723SYOSHIFUJI Hideaki 					in6_ifa_put(hiscore->ifa);
157699cd07a5SJuha-Matti Tapio 
1577a9b05723SYOSHIFUJI Hideaki 				in6_ifa_hold(score->ifa);
1578072047e4SYOSHIFUJI Hideaki 
1579a0bffffcSIlpo Järvinen 				swap(hiscore, score);
1580c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				hiscore_idx = 1 - hiscore_idx;
1581a9b05723SYOSHIFUJI Hideaki 
1582a9b05723SYOSHIFUJI Hideaki 				/* restore our iterator */
1583a9b05723SYOSHIFUJI Hideaki 				score->ifa = hiscore->ifa;
1584a9b05723SYOSHIFUJI Hideaki 
1585a9b05723SYOSHIFUJI Hideaki 				break;
15861da177e4SLinus Torvalds 			}
1587a9b05723SYOSHIFUJI Hideaki 		}
1588a9b05723SYOSHIFUJI Hideaki 	}
15899131f3deSYOSHIFUJI Hideaki/吉藤英明 out:
15901da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
1591c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	return hiscore_idx;
15921da177e4SLinus Torvalds }
15939131f3deSYOSHIFUJI Hideaki/吉藤英明 
1594afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net,
1595afbac601SDavid Ahern 				 const struct net_device *dst_dev,
1596afbac601SDavid Ahern 				 const struct net_device *master,
1597afbac601SDavid Ahern 				 struct ipv6_saddr_dst *dst,
1598afbac601SDavid Ahern 				 struct ipv6_saddr_score *scores,
1599afbac601SDavid Ahern 				 int hiscore_idx)
1600afbac601SDavid Ahern {
1601afbac601SDavid Ahern 	struct inet6_dev *idev;
1602afbac601SDavid Ahern 
1603afbac601SDavid Ahern 	idev = __in6_dev_get(dst_dev);
1604afbac601SDavid Ahern 	if (idev)
1605afbac601SDavid Ahern 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1606afbac601SDavid Ahern 						   scores, hiscore_idx);
1607afbac601SDavid Ahern 
1608afbac601SDavid Ahern 	idev = __in6_dev_get(master);
1609afbac601SDavid Ahern 	if (idev)
1610afbac601SDavid Ahern 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1611afbac601SDavid Ahern 						   scores, hiscore_idx);
1612afbac601SDavid Ahern 
1613afbac601SDavid Ahern 	return hiscore_idx;
1614afbac601SDavid Ahern }
1615afbac601SDavid Ahern 
16169131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
16179131f3deSYOSHIFUJI Hideaki/吉藤英明 		       const struct in6_addr *daddr, unsigned int prefs,
16189131f3deSYOSHIFUJI Hideaki/吉藤英明 		       struct in6_addr *saddr)
16199131f3deSYOSHIFUJI Hideaki/吉藤英明 {
1620c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score scores[2], *hiscore;
16219131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_dst dst;
16229131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct inet6_dev *idev;
16239131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct net_device *dev;
16249131f3deSYOSHIFUJI Hideaki/吉藤英明 	int dst_type;
16259131f3deSYOSHIFUJI Hideaki/吉藤英明 	bool use_oif_addr = false;
1626c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	int hiscore_idx = 0;
16279131f3deSYOSHIFUJI Hideaki/吉藤英明 
16289131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst_type = __ipv6_addr_type(daddr);
16299131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.addr = daddr;
16309131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
16319131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.scope = __ipv6_addr_src_scope(dst_type);
16329131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
16339131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.prefs = prefs;
16349131f3deSYOSHIFUJI Hideaki/吉藤英明 
1635c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].rule = -1;
1636c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].ifa = NULL;
16379131f3deSYOSHIFUJI Hideaki/吉藤英明 
16389131f3deSYOSHIFUJI Hideaki/吉藤英明 	rcu_read_lock();
16399131f3deSYOSHIFUJI Hideaki/吉藤英明 
16409131f3deSYOSHIFUJI Hideaki/吉藤英明 	/* Candidate Source Address (section 4)
16419131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *  - multicast and link-local destination address,
16429131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    the set of candidate source address MUST only
16439131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
16449131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same link as the outgoing
16459131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.
16469131f3deSYOSHIFUJI Hideaki/吉藤英明 	 * (- For site-local destination addresses, the
16479131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    set of candidate source addresses MUST only
16489131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
16499131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same site as the outgoing
16509131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.)
16513985e8a3SErik Kline 	 *  - "It is RECOMMENDED that the candidate source addresses
16523985e8a3SErik Kline 	 *    be the set of unicast addresses assigned to the
16533985e8a3SErik Kline 	 *    interface that will be used to send to the destination
16543985e8a3SErik Kline 	 *    (the 'outgoing' interface)." (RFC 6724)
16559131f3deSYOSHIFUJI Hideaki/吉藤英明 	 */
16569131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (dst_dev) {
16579131f3deSYOSHIFUJI Hideaki/吉藤英明 		idev = __in6_dev_get(dst_dev);
16583985e8a3SErik Kline 		if ((dst_type & IPV6_ADDR_MULTICAST) ||
16593985e8a3SErik Kline 		    dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
16603985e8a3SErik Kline 		    (idev && idev->cnf.use_oif_addrs_only)) {
16619131f3deSYOSHIFUJI Hideaki/吉藤英明 			use_oif_addr = true;
16629131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
16639131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
16649131f3deSYOSHIFUJI Hideaki/吉藤英明 
16659131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (use_oif_addr) {
1666c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 		if (idev)
1667c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
16689131f3deSYOSHIFUJI Hideaki/吉藤英明 	} else {
1669afbac601SDavid Ahern 		const struct net_device *master;
1670afbac601SDavid Ahern 		int master_idx = 0;
1671afbac601SDavid Ahern 
1672afbac601SDavid Ahern 		/* if dst_dev exists and is enslaved to an L3 device, then
1673afbac601SDavid Ahern 		 * prefer addresses from dst_dev and then the master over
1674afbac601SDavid Ahern 		 * any other enslaved devices in the L3 domain.
1675afbac601SDavid Ahern 		 */
1676afbac601SDavid Ahern 		master = l3mdev_master_dev_rcu(dst_dev);
1677afbac601SDavid Ahern 		if (master) {
1678afbac601SDavid Ahern 			master_idx = master->ifindex;
1679afbac601SDavid Ahern 
1680afbac601SDavid Ahern 			hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1681afbac601SDavid Ahern 							    master, &dst,
1682afbac601SDavid Ahern 							    scores, hiscore_idx);
1683afbac601SDavid Ahern 
1684afbac601SDavid Ahern 			if (scores[hiscore_idx].ifa)
1685afbac601SDavid Ahern 				goto out;
1686afbac601SDavid Ahern 		}
1687afbac601SDavid Ahern 
16889131f3deSYOSHIFUJI Hideaki/吉藤英明 		for_each_netdev_rcu(net, dev) {
1689afbac601SDavid Ahern 			/* only consider addresses on devices in the
1690afbac601SDavid Ahern 			 * same L3 domain
1691afbac601SDavid Ahern 			 */
1692afbac601SDavid Ahern 			if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1693afbac601SDavid Ahern 				continue;
16949131f3deSYOSHIFUJI Hideaki/吉藤英明 			idev = __in6_dev_get(dev);
16959131f3deSYOSHIFUJI Hideaki/吉藤英明 			if (!idev)
16969131f3deSYOSHIFUJI Hideaki/吉藤英明 				continue;
1697c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
16989131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
16999131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
1700afbac601SDavid Ahern 
1701afbac601SDavid Ahern out:
17028814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
17031da177e4SLinus Torvalds 
1704c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	hiscore = &scores[hiscore_idx];
1705a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1706072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
17071da177e4SLinus Torvalds 
17084e3fd7a0SAlexey Dobriyan 	*saddr = hiscore->ifa->addr;
1709a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1710072047e4SYOSHIFUJI Hideaki 	return 0;
17111da177e4SLinus Torvalds }
17125e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
17131da177e4SLinus Torvalds 
17148965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1715479840ffSJiri Pirko 		      u32 banned_flags)
1716b7b1bfceSHannes Frederic Sowa {
1717b7b1bfceSHannes Frederic Sowa 	struct inet6_ifaddr *ifp;
1718b7b1bfceSHannes Frederic Sowa 	int err = -EADDRNOTAVAIL;
1719b7b1bfceSHannes Frederic Sowa 
1720602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1721602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1722602582caSHannes Frederic Sowa 			break;
1723b7b1bfceSHannes Frederic Sowa 		if (ifp->scope == IFA_LINK &&
1724b7b1bfceSHannes Frederic Sowa 		    !(ifp->flags & banned_flags)) {
1725b7b1bfceSHannes Frederic Sowa 			*addr = ifp->addr;
1726b7b1bfceSHannes Frederic Sowa 			err = 0;
1727b7b1bfceSHannes Frederic Sowa 			break;
1728b7b1bfceSHannes Frederic Sowa 		}
1729b7b1bfceSHannes Frederic Sowa 	}
1730b7b1bfceSHannes Frederic Sowa 	return err;
1731b7b1bfceSHannes Frederic Sowa }
1732b7b1bfceSHannes Frederic Sowa 
173395c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1734479840ffSJiri Pirko 		    u32 banned_flags)
17351da177e4SLinus Torvalds {
17361da177e4SLinus Torvalds 	struct inet6_dev *idev;
17371da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
17381da177e4SLinus Torvalds 
17398814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1740e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1741e21e8467SStephen Hemminger 	if (idev) {
17421da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1743b7b1bfceSHannes Frederic Sowa 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
17441da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
17451da177e4SLinus Torvalds 	}
17468814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
17471da177e4SLinus Torvalds 	return err;
17481da177e4SLinus Torvalds }
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
17511da177e4SLinus Torvalds {
17521da177e4SLinus Torvalds 	int cnt = 0;
17531da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1756502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list)
17571da177e4SLinus Torvalds 		cnt++;
17581da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
17591da177e4SLinus Torvalds 	return cnt;
17601da177e4SLinus Torvalds }
17611da177e4SLinus Torvalds 
1762b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
17632a7851bfSFlorian Westphal 		  const struct net_device *dev, int strict)
17641da177e4SLinus Torvalds {
1765c58da4c6SErik Kline 	return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1766c58da4c6SErik Kline }
1767c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr);
1768c58da4c6SErik Kline 
1769c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1770c58da4c6SErik Kline 			    const struct net_device *dev, int strict,
1771c58da4c6SErik Kline 			    u32 banned_flags)
1772c58da4c6SErik Kline {
1773eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1774ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
1775c58da4c6SErik Kline 	u32 ifp_flags;
17761da177e4SLinus Torvalds 
17775c578aedSstephen hemminger 	rcu_read_lock_bh();
1778b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1779878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1780bfeade08SDaniel Lezcano 			continue;
1781c58da4c6SErik Kline 		/* Decouple optimistic from tentative for evaluation here.
1782c58da4c6SErik Kline 		 * Ban optimistic addresses explicitly, when required.
1783c58da4c6SErik Kline 		 */
1784c58da4c6SErik Kline 		ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1785c58da4c6SErik Kline 			    ? (ifp->flags&~IFA_F_TENTATIVE)
1786c58da4c6SErik Kline 			    : ifp->flags;
17871da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1788c58da4c6SErik Kline 		    !(ifp_flags&banned_flags) &&
178963159f29SIan Morris 		    (!dev || ifp->idev->dev == dev ||
1790eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
17915c578aedSstephen hemminger 			rcu_read_unlock_bh();
1792eedf042aSStephen Hemminger 			return 1;
1793eedf042aSStephen Hemminger 		}
1794eedf042aSStephen Hemminger 	}
17955c578aedSstephen hemminger 
1796eedf042aSStephen Hemminger 	rcu_read_unlock_bh();
1797eedf042aSStephen Hemminger 	return 0;
17981da177e4SLinus Torvalds }
1799c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
18007159039aSYOSHIFUJI Hideaki 
18013e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
180206bfe655SDaniel Lezcano 			       struct net_device *dev)
18031da177e4SLinus Torvalds {
1804ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
18051da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
18061da177e4SLinus Torvalds 
1807b67bfe0dSSasha Levin 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1808878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
180906bfe655SDaniel Lezcano 			continue;
18101da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
181163159f29SIan Morris 			if (!dev || ifp->idev->dev == dev)
18123e81c6daSDavid S. Miller 				return true;
18131da177e4SLinus Torvalds 		}
18141da177e4SLinus Torvalds 	}
18153e81c6daSDavid S. Miller 	return false;
18161da177e4SLinus Torvalds }
18171da177e4SLinus Torvalds 
18187df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev.
18197df37ff3SCatalin\(ux\) M. BOIE  * If one is found it returns true.
18207df37ff3SCatalin\(ux\) M. BOIE  */
18217df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
18227df37ff3SCatalin\(ux\) M. BOIE 	const unsigned int prefix_len, struct net_device *dev)
18237df37ff3SCatalin\(ux\) M. BOIE {
18247df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_dev *idev;
18257df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_ifaddr *ifa;
18267df37ff3SCatalin\(ux\) M. BOIE 	bool ret = false;
18277df37ff3SCatalin\(ux\) M. BOIE 
18287df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_lock();
18297df37ff3SCatalin\(ux\) M. BOIE 	idev = __in6_dev_get(dev);
18307df37ff3SCatalin\(ux\) M. BOIE 	if (idev) {
18317df37ff3SCatalin\(ux\) M. BOIE 		read_lock_bh(&idev->lock);
18327df37ff3SCatalin\(ux\) M. BOIE 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
18337df37ff3SCatalin\(ux\) M. BOIE 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
18347df37ff3SCatalin\(ux\) M. BOIE 			if (ret)
18357df37ff3SCatalin\(ux\) M. BOIE 				break;
18367df37ff3SCatalin\(ux\) M. BOIE 		}
18377df37ff3SCatalin\(ux\) M. BOIE 		read_unlock_bh(&idev->lock);
18387df37ff3SCatalin\(ux\) M. BOIE 	}
18397df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_unlock();
18407df37ff3SCatalin\(ux\) M. BOIE 
18417df37ff3SCatalin\(ux\) M. BOIE 	return ret;
18427df37ff3SCatalin\(ux\) M. BOIE }
18437df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix);
18447df37ff3SCatalin\(ux\) M. BOIE 
1845b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
184652eeeb84SYOSHIFUJI Hideaki {
184752eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
184852eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
184952eeeb84SYOSHIFUJI Hideaki 	int	onlink;
185052eeeb84SYOSHIFUJI Hideaki 
185152eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
185252eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
185352eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
185452eeeb84SYOSHIFUJI Hideaki 	if (idev) {
185552eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
1856502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
185752eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
185852eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
185952eeeb84SYOSHIFUJI Hideaki 			if (onlink)
186052eeeb84SYOSHIFUJI Hideaki 				break;
186152eeeb84SYOSHIFUJI Hideaki 		}
186252eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
186352eeeb84SYOSHIFUJI Hideaki 	}
186452eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
186552eeeb84SYOSHIFUJI Hideaki 	return onlink;
186652eeeb84SYOSHIFUJI Hideaki }
186752eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
186852eeeb84SYOSHIFUJI Hideaki 
18699acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
18701cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
18711da177e4SLinus Torvalds {
1872b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
1873ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
18741da177e4SLinus Torvalds 
18755c578aedSstephen hemminger 	rcu_read_lock_bh();
1876b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1877878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
18781cab3da6SDaniel Lezcano 			continue;
18791da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
188063159f29SIan Morris 			if (!dev || ifp->idev->dev == dev ||
18811da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1882b79d1d54SDavid S. Miller 				result = ifp;
18831da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
18841da177e4SLinus Torvalds 				break;
18851da177e4SLinus Torvalds 			}
18861da177e4SLinus Torvalds 		}
18871da177e4SLinus Torvalds 	}
18885c578aedSstephen hemminger 	rcu_read_unlock_bh();
18891da177e4SLinus Torvalds 
1890b79d1d54SDavid S. Miller 	return result;
18911da177e4SLinus Torvalds }
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
18941da177e4SLinus Torvalds 
1895cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
18961da177e4SLinus Torvalds {
18973d171f39SLubomir Rintel 	if (dad_failed)
18983d171f39SLubomir Rintel 		ifp->flags |= IFA_F_DADFAILED;
18993d171f39SLubomir Rintel 
19001da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
19011da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
1902c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifp);
19031da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
19041da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
1905e2577a06SHerbert Xu 		if (dad_failed)
1906e2577a06SHerbert Xu 			ipv6_ifa_notify(0, ifp);
19071da177e4SLinus Torvalds 		in6_ifa_put(ifp);
19081da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
19091da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
19101da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
19111da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
19121da177e4SLinus Torvalds 		if (ifpub) {
19131da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
19141da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
19151da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
19161da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
19171da177e4SLinus Torvalds 		} else {
19181da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
19191da177e4SLinus Torvalds 		}
19201da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
1921c15b1ccaSHannes Frederic Sowa 	} else {
19221da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
19231da177e4SLinus Torvalds 	}
1924c15b1ccaSHannes Frederic Sowa }
19251da177e4SLinus Torvalds 
1926f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1927f2344a13SHerbert Xu {
1928f2344a13SHerbert Xu 	int err = -ENOENT;
1929f2344a13SHerbert Xu 
19308e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
1931f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
1932f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
1933f2344a13SHerbert Xu 		err = 0;
1934f2344a13SHerbert Xu 	}
19358e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
1936f2344a13SHerbert Xu 
1937f2344a13SHerbert Xu 	return err;
1938f2344a13SHerbert Xu }
1939f2344a13SHerbert Xu 
19403c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
19413c21edbdSYOSHIFUJI Hideaki {
19421b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
19431855b7c3SHannes Frederic Sowa 	struct net *net = dev_net(ifp->idev->dev);
19449bdd8d40SBrian Haley 
1945853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
1946853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
1947f2344a13SHerbert Xu 		return;
1948853dc2e0SUrsula Braun 	}
1949f2344a13SHerbert Xu 
1950e87cc472SJoe Perches 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
19510522fea6SJens Rosenboom 			     ifp->idev->dev->name, &ifp->addr);
19529bdd8d40SBrian Haley 
19535f40ef77SHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
19541b34be74SYOSHIFUJI Hideaki 
19555f40ef77SHannes Frederic Sowa 	if (ifp->flags & IFA_F_STABLE_PRIVACY) {
19565f40ef77SHannes Frederic Sowa 		int scope = ifp->scope;
19575f40ef77SHannes Frederic Sowa 		u32 flags = ifp->flags;
19585f40ef77SHannes Frederic Sowa 		struct in6_addr new_addr;
19595f40ef77SHannes Frederic Sowa 		struct inet6_ifaddr *ifp2;
19605f40ef77SHannes Frederic Sowa 		u32 valid_lft, preferred_lft;
19615f40ef77SHannes Frederic Sowa 		int pfxlen = ifp->prefix_len;
19625f40ef77SHannes Frederic Sowa 		int retries = ifp->stable_privacy_retry + 1;
19635f40ef77SHannes Frederic Sowa 
19641855b7c3SHannes Frederic Sowa 		if (retries > net->ipv6.sysctl.idgen_retries) {
19655f40ef77SHannes Frederic Sowa 			net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
19665f40ef77SHannes Frederic Sowa 					     ifp->idev->dev->name);
19675f40ef77SHannes Frederic Sowa 			goto errdad;
19685f40ef77SHannes Frederic Sowa 		}
19695f40ef77SHannes Frederic Sowa 
19705f40ef77SHannes Frederic Sowa 		new_addr = ifp->addr;
19715f40ef77SHannes Frederic Sowa 		if (ipv6_generate_stable_address(&new_addr, retries,
19725f40ef77SHannes Frederic Sowa 						 idev))
19735f40ef77SHannes Frederic Sowa 			goto errdad;
19745f40ef77SHannes Frederic Sowa 
19755f40ef77SHannes Frederic Sowa 		valid_lft = ifp->valid_lft;
19765f40ef77SHannes Frederic Sowa 		preferred_lft = ifp->prefered_lft;
19775f40ef77SHannes Frederic Sowa 
19785f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp->lock);
19795f40ef77SHannes Frederic Sowa 
19805f40ef77SHannes Frederic Sowa 		if (idev->cnf.max_addresses &&
19815f40ef77SHannes Frederic Sowa 		    ipv6_count_addresses(idev) >=
19825f40ef77SHannes Frederic Sowa 		    idev->cnf.max_addresses)
19835f40ef77SHannes Frederic Sowa 			goto lock_errdad;
19845f40ef77SHannes Frederic Sowa 
19855f40ef77SHannes Frederic Sowa 		net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
19865f40ef77SHannes Frederic Sowa 				     ifp->idev->dev->name);
19875f40ef77SHannes Frederic Sowa 
19885f40ef77SHannes Frederic Sowa 		ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
19895f40ef77SHannes Frederic Sowa 				     scope, flags, valid_lft,
19905f40ef77SHannes Frederic Sowa 				     preferred_lft);
19915f40ef77SHannes Frederic Sowa 		if (IS_ERR(ifp2))
19925f40ef77SHannes Frederic Sowa 			goto lock_errdad;
19935f40ef77SHannes Frederic Sowa 
19945f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp2->lock);
19955f40ef77SHannes Frederic Sowa 		ifp2->stable_privacy_retry = retries;
19965f40ef77SHannes Frederic Sowa 		ifp2->state = INET6_IFADDR_STATE_PREDAD;
19975f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp2->lock);
19985f40ef77SHannes Frederic Sowa 
19991855b7c3SHannes Frederic Sowa 		addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
20005f40ef77SHannes Frederic Sowa 		in6_ifa_put(ifp2);
20015f40ef77SHannes Frederic Sowa lock_errdad:
20025f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp->lock);
20031b34be74SYOSHIFUJI Hideaki 	}
20041b34be74SYOSHIFUJI Hideaki 
20055f40ef77SHannes Frederic Sowa errdad:
2006c15b1ccaSHannes Frederic Sowa 	/* transition from _POSTDAD to _ERRDAD */
2007c15b1ccaSHannes Frederic Sowa 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
20088e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
2009c15b1ccaSHannes Frederic Sowa 
2010c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, 0);
2011751eb6b6SWei Yongjun 	in6_ifa_put(ifp);
20123c21edbdSYOSHIFUJI Hideaki }
20131da177e4SLinus Torvalds 
2014a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group.
2015a9ed4a29SSabrina Dubroca  * caller must hold RTNL */
2016b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
20171da177e4SLinus Torvalds {
20181da177e4SLinus Torvalds 	struct in6_addr maddr;
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
20211da177e4SLinus Torvalds 		return;
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
20241da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
20251da177e4SLinus Torvalds }
20261da177e4SLinus Torvalds 
2027a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
2028b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
20291da177e4SLinus Torvalds {
20301da177e4SLinus Torvalds 	struct in6_addr maddr;
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
20331da177e4SLinus Torvalds 		return;
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
20361da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
20371da177e4SLinus Torvalds }
20381da177e4SLinus Torvalds 
2039a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
204020380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
20411da177e4SLinus Torvalds {
20421da177e4SLinus Torvalds 	struct in6_addr addr;
2043c15b1ccaSHannes Frederic Sowa 
204488ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
20452bda8a0cSBjørn Mork 		return;
20461da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
20471da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
20481da177e4SLinus Torvalds 		return;
2049013b4d90SWANG Cong 	__ipv6_dev_ac_inc(ifp->idev, &addr);
20501da177e4SLinus Torvalds }
20511da177e4SLinus Torvalds 
2052a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
205320380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
20541da177e4SLinus Torvalds {
20551da177e4SLinus Torvalds 	struct in6_addr addr;
2056c15b1ccaSHannes Frederic Sowa 
205788ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
205832019e65SYOSHIFUJI Hideaki 		return;
20591da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
20601da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
20611da177e4SLinus Torvalds 		return;
20621da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
20631da177e4SLinus Torvalds }
20641da177e4SLinus Torvalds 
206506a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
206606a4c1c5Salex.bluesman.smirnov@gmail.com {
20675241c2d7SAlexander Aring 	if (dev->addr_len != EUI64_ADDR_LEN)
206806a4c1c5Salex.bluesman.smirnov@gmail.com 		return -1;
20695241c2d7SAlexander Aring 	memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
20705e98a36eSYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
207106a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
207206a4c1c5Salex.bluesman.smirnov@gmail.com }
207306a4c1c5Salex.bluesman.smirnov@gmail.com 
2074cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2075cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
2076cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	union fwnet_hwaddr *ha;
2077cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2078cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
2079cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
2080cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2081cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	ha = (union fwnet_hwaddr *)dev->dev_addr;
2082cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2083cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2084cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
2085cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
2086cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
2087cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
2088073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2089073a8e0eSYOSHIFUJI Hideaki {
20901da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
20911da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
20921da177e4SLinus Torvalds 		return -1;
20931da177e4SLinus Torvalds 	memset(eui, 0, 7);
20941da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
20951da177e4SLinus Torvalds 	return 0;
2096073a8e0eSYOSHIFUJI Hideaki }
2097073a8e0eSYOSHIFUJI Hideaki 
2098073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2099073a8e0eSYOSHIFUJI Hideaki {
21001da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
21011da177e4SLinus Torvalds 		return -1;
21021da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
21031da177e4SLinus Torvalds 	eui[0] |= 2;
21041da177e4SLinus Torvalds 	return 0;
21051da177e4SLinus Torvalds }
2106073a8e0eSYOSHIFUJI Hideaki 
2107c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2108dfd982baSYOSHIFUJI Hideaki {
21099af28511SSascha Hlusiak 	if (addr == 0)
21109af28511SSascha Hlusiak 		return -1;
2111dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2112dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2113dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2114dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2115dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2116dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2117dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
2118dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
2119dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
2120dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
2121dfd982baSYOSHIFUJI Hideaki 	return 0;
2122dfd982baSYOSHIFUJI Hideaki }
2123dfd982baSYOSHIFUJI Hideaki 
2124dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2125dfd982baSYOSHIFUJI Hideaki {
2126dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
2127dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2128dfd982baSYOSHIFUJI Hideaki 	return -1;
2129dfd982baSYOSHIFUJI Hideaki }
2130dfd982baSYOSHIFUJI Hideaki 
2131aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2132aee80b54Sstephen hemminger {
2133aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2134aee80b54Sstephen hemminger }
2135aee80b54Sstephen hemminger 
2136e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2137e837735eSNicolas Dichtel {
2138e837735eSNicolas Dichtel 	memcpy(eui, dev->perm_addr, 3);
2139e837735eSNicolas Dichtel 	memcpy(eui + 5, dev->perm_addr + 3, 3);
2140e837735eSNicolas Dichtel 	eui[3] = 0xFF;
2141e837735eSNicolas Dichtel 	eui[4] = 0xFE;
2142e837735eSNicolas Dichtel 	eui[0] ^= 2;
2143e837735eSNicolas Dichtel 	return 0;
2144e837735eSNicolas Dichtel }
2145e837735eSNicolas Dichtel 
2146073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2147073a8e0eSYOSHIFUJI Hideaki {
2148073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
2149073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
2150073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
2151073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
2152073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
2153073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
2154073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
2155073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
2156c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
2157dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
2158aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
215945ce0fd1SFelix Jia 	case ARPHRD_TUNNEL:
2160aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
2161e74bccb8SJukka Rissanen 	case ARPHRD_6LOWPAN:
216206a4c1c5Salex.bluesman.smirnov@gmail.com 		return addrconf_ifid_eui64(eui, dev);
2163cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
2164cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
2165e837735eSNicolas Dichtel 	case ARPHRD_TUNNEL6:
216645ce0fd1SFelix Jia 	case ARPHRD_IP6GRE:
2167e837735eSNicolas Dichtel 		return addrconf_ifid_ip6tnl(eui, dev);
2168073a8e0eSYOSHIFUJI Hideaki 	}
21691da177e4SLinus Torvalds 	return -1;
21701da177e4SLinus Torvalds }
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
21731da177e4SLinus Torvalds {
21741da177e4SLinus Torvalds 	int err = -1;
21751da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
21761da177e4SLinus Torvalds 
21771da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2178602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2179602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
2180602582caSHannes Frederic Sowa 			break;
21811da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
21821da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
21831da177e4SLinus Torvalds 			err = 0;
21841da177e4SLinus Torvalds 			break;
21851da177e4SLinus Torvalds 		}
21861da177e4SLinus Torvalds 	}
21871da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
21881da177e4SLinus Torvalds 	return err;
21891da177e4SLinus Torvalds }
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
21929d6280daSJiri Bohac static void ipv6_regen_rndid(struct inet6_dev *idev)
21931da177e4SLinus Torvalds {
21941da177e4SLinus Torvalds regen:
2195955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
21961da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds 	/*
21991da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
22001da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
22011da177e4SLinus Torvalds 	 *
22021da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
22031da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
2204c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
22051da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
22061da177e4SLinus Torvalds 	 *  - value 0
22071da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
22081da177e4SLinus Torvalds 	 */
22091da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
22101da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
22111da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
22121da177e4SLinus Torvalds 		goto regen;
22131da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
22141da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
22151da177e4SLinus Torvalds 			goto regen;
22161da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
22171da177e4SLinus Torvalds 			goto regen;
22181da177e4SLinus Torvalds 	}
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
22219d6280daSJiri Bohac static void  ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
22228e5e8f30SEldad Zack {
22231da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
22249d6280daSJiri Bohac 		ipv6_regen_rndid(idev);
22251da177e4SLinus Torvalds }
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds /*
22281da177e4SLinus Torvalds  *	Add prefix route.
22291da177e4SLinus Torvalds  */
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds static void
22321da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2233e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
22341da177e4SLinus Torvalds {
223586872cb5SThomas Graf 	struct fib6_config cfg = {
2236ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
223786872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
223886872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
223986872cb5SThomas Graf 		.fc_expires = expires,
224086872cb5SThomas Graf 		.fc_dst_len = plen,
224186872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
2242c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
2243f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
224486872cb5SThomas Graf 	};
22451da177e4SLinus Torvalds 
22464e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
22471da177e4SLinus Torvalds 
22481da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
22491da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
22501da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
22511da177e4SLinus Torvalds 	 */
225207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
22531da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
225486872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
22550be669bbSJoerg Roedel #endif
22561da177e4SLinus Torvalds 
225786872cb5SThomas Graf 	ip6_route_add(&cfg);
22581da177e4SLinus Torvalds }
22591da177e4SLinus Torvalds 
226014ef37b6SAndreas Hofmeister 
226114ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
226214ef37b6SAndreas Hofmeister 						  int plen,
226314ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
226414ef37b6SAndreas Hofmeister 						  u32 flags, u32 noflags)
226514ef37b6SAndreas Hofmeister {
226614ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
226714ef37b6SAndreas Hofmeister 	struct rt6_info *rt = NULL;
226814ef37b6SAndreas Hofmeister 	struct fib6_table *table;
2269ca254490SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
227014ef37b6SAndreas Hofmeister 
2271ca254490SDavid Ahern 	table = fib6_get_table(dev_net(dev), tb_id);
227263159f29SIan Morris 	if (!table)
227314ef37b6SAndreas Hofmeister 		return NULL;
227414ef37b6SAndreas Hofmeister 
22755744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
227614ef37b6SAndreas Hofmeister 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
227714ef37b6SAndreas Hofmeister 	if (!fn)
227814ef37b6SAndreas Hofmeister 		goto out;
22791f56a01fSMartin KaFai Lau 
22801f56a01fSMartin KaFai Lau 	noflags |= RTF_CACHE;
228114ef37b6SAndreas Hofmeister 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2282d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != dev->ifindex)
228314ef37b6SAndreas Hofmeister 			continue;
228414ef37b6SAndreas Hofmeister 		if ((rt->rt6i_flags & flags) != flags)
228514ef37b6SAndreas Hofmeister 			continue;
228685da53bfSRomain Kuntz 		if ((rt->rt6i_flags & noflags) != 0)
228714ef37b6SAndreas Hofmeister 			continue;
228814ef37b6SAndreas Hofmeister 		dst_hold(&rt->dst);
228914ef37b6SAndreas Hofmeister 		break;
229014ef37b6SAndreas Hofmeister 	}
229114ef37b6SAndreas Hofmeister out:
22925744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
229314ef37b6SAndreas Hofmeister 	return rt;
229414ef37b6SAndreas Hofmeister }
229514ef37b6SAndreas Hofmeister 
229614ef37b6SAndreas Hofmeister 
22971da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
23001da177e4SLinus Torvalds {
230186872cb5SThomas Graf 	struct fib6_config cfg = {
2302ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
230386872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
230486872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
230586872cb5SThomas Graf 		.fc_dst_len = 8,
230686872cb5SThomas Graf 		.fc_flags = RTF_UP,
2307c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
230886872cb5SThomas Graf 	};
23091da177e4SLinus Torvalds 
231086872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
231186872cb5SThomas Graf 
231286872cb5SThomas Graf 	ip6_route_add(&cfg);
23131da177e4SLinus Torvalds }
23141da177e4SLinus Torvalds 
23151da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
23161da177e4SLinus Torvalds {
23171da177e4SLinus Torvalds 	struct inet6_dev *idev;
23181da177e4SLinus Torvalds 
23191da177e4SLinus Torvalds 	ASSERT_RTNL();
23201da177e4SLinus Torvalds 
2321e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2322e21e8467SStephen Hemminger 	if (!idev)
232364e724f6SBrian Haley 		return ERR_PTR(-ENOBUFS);
232464e724f6SBrian Haley 
232564e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
232664e724f6SBrian Haley 		return ERR_PTR(-EACCES);
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds 	/* Add default multicast route */
2329ba46ee4cSDavid Ahern 	if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
23301da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds 	return idev;
23331da177e4SLinus Torvalds }
23341da177e4SLinus Torvalds 
233553bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev,
233653bd6749SJiri Pirko 			     struct inet6_ifaddr *ifp,
233753bd6749SJiri Pirko 			     __u32 valid_lft, __u32 prefered_lft,
233853bd6749SJiri Pirko 			     bool create, unsigned long now)
233953bd6749SJiri Pirko {
234053bd6749SJiri Pirko 	u32 flags;
234153bd6749SJiri Pirko 	struct inet6_ifaddr *ift;
234253bd6749SJiri Pirko 
234353bd6749SJiri Pirko 	read_lock_bh(&idev->lock);
234453bd6749SJiri Pirko 	/* update all temporary addresses in the list */
234553bd6749SJiri Pirko 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
234653bd6749SJiri Pirko 		int age, max_valid, max_prefered;
234753bd6749SJiri Pirko 
234853bd6749SJiri Pirko 		if (ifp != ift->ifpub)
234953bd6749SJiri Pirko 			continue;
235053bd6749SJiri Pirko 
235153bd6749SJiri Pirko 		/* RFC 4941 section 3.3:
235253bd6749SJiri Pirko 		 * If a received option will extend the lifetime of a public
235353bd6749SJiri Pirko 		 * address, the lifetimes of temporary addresses should
235453bd6749SJiri Pirko 		 * be extended, subject to the overall constraint that no
235553bd6749SJiri Pirko 		 * temporary addresses should ever remain "valid" or "preferred"
235653bd6749SJiri Pirko 		 * for a time longer than (TEMP_VALID_LIFETIME) or
235753bd6749SJiri Pirko 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
235853bd6749SJiri Pirko 		 */
235953bd6749SJiri Pirko 		age = (now - ift->cstamp) / HZ;
236053bd6749SJiri Pirko 		max_valid = idev->cnf.temp_valid_lft - age;
236153bd6749SJiri Pirko 		if (max_valid < 0)
236253bd6749SJiri Pirko 			max_valid = 0;
236353bd6749SJiri Pirko 
236453bd6749SJiri Pirko 		max_prefered = idev->cnf.temp_prefered_lft -
236576506a98SJiri Bohac 			       idev->desync_factor - age;
236653bd6749SJiri Pirko 		if (max_prefered < 0)
236753bd6749SJiri Pirko 			max_prefered = 0;
236853bd6749SJiri Pirko 
236953bd6749SJiri Pirko 		if (valid_lft > max_valid)
237053bd6749SJiri Pirko 			valid_lft = max_valid;
237153bd6749SJiri Pirko 
237253bd6749SJiri Pirko 		if (prefered_lft > max_prefered)
237353bd6749SJiri Pirko 			prefered_lft = max_prefered;
237453bd6749SJiri Pirko 
237553bd6749SJiri Pirko 		spin_lock(&ift->lock);
237653bd6749SJiri Pirko 		flags = ift->flags;
237753bd6749SJiri Pirko 		ift->valid_lft = valid_lft;
237853bd6749SJiri Pirko 		ift->prefered_lft = prefered_lft;
237953bd6749SJiri Pirko 		ift->tstamp = now;
238053bd6749SJiri Pirko 		if (prefered_lft > 0)
238153bd6749SJiri Pirko 			ift->flags &= ~IFA_F_DEPRECATED;
238253bd6749SJiri Pirko 
238353bd6749SJiri Pirko 		spin_unlock(&ift->lock);
238453bd6749SJiri Pirko 		if (!(flags&IFA_F_TENTATIVE))
238553bd6749SJiri Pirko 			ipv6_ifa_notify(0, ift);
238653bd6749SJiri Pirko 	}
238753bd6749SJiri Pirko 
238853bd6749SJiri Pirko 	if ((create || list_empty(&idev->tempaddr_list)) &&
238953bd6749SJiri Pirko 	    idev->cnf.use_tempaddr > 0) {
239053bd6749SJiri Pirko 		/* When a new public address is created as described
239153bd6749SJiri Pirko 		 * in [ADDRCONF], also create a new temporary address.
239253bd6749SJiri Pirko 		 * Also create a temporary address if it's enabled but
239353bd6749SJiri Pirko 		 * no temporary address currently exists.
239453bd6749SJiri Pirko 		 */
239553bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
239653bd6749SJiri Pirko 		ipv6_create_tempaddr(ifp, NULL);
239753bd6749SJiri Pirko 	} else {
239853bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
239953bd6749SJiri Pirko 	}
240053bd6749SJiri Pirko }
240153bd6749SJiri Pirko 
2402cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2403cc9da6ccSBjørn Mork {
2404d35a00b8SFelix Jia 	return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2405d35a00b8SFelix Jia 	       idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2406cc9da6ccSBjørn Mork }
2407cc9da6ccSBjørn Mork 
2408cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
24094f672235SAlexander Aring 				 const struct prefix_info *pinfo,
24104f672235SAlexander Aring 				 struct inet6_dev *in6_dev,
2411cc84b3c6SAlexander Aring 				 const struct in6_addr *addr, int addr_type,
2412cc84b3c6SAlexander Aring 				 u32 addr_flags, bool sllao, bool tokenized,
24134f672235SAlexander Aring 				 __u32 valid_lft, u32 prefered_lft)
24144f672235SAlexander Aring {
24154f672235SAlexander Aring 	struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
24164f672235SAlexander Aring 	int create = 0, update_lft = 0;
24174f672235SAlexander Aring 
24184f672235SAlexander Aring 	if (!ifp && valid_lft) {
24194f672235SAlexander Aring 		int max_addresses = in6_dev->cnf.max_addresses;
24204f672235SAlexander Aring 
24214f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
24224f672235SAlexander Aring 		if (in6_dev->cnf.optimistic_dad &&
24234f672235SAlexander Aring 		    !net->ipv6.devconf_all->forwarding && sllao)
24244f672235SAlexander Aring 			addr_flags |= IFA_F_OPTIMISTIC;
24254f672235SAlexander Aring #endif
24264f672235SAlexander Aring 
24274f672235SAlexander Aring 		/* Do not allow to create too much of autoconfigured
24284f672235SAlexander Aring 		 * addresses; this would be too easy way to crash kernel.
24294f672235SAlexander Aring 		 */
24304f672235SAlexander Aring 		if (!max_addresses ||
24314f672235SAlexander Aring 		    ipv6_count_addresses(in6_dev) < max_addresses)
24324f672235SAlexander Aring 			ifp = ipv6_add_addr(in6_dev, addr, NULL,
24334f672235SAlexander Aring 					    pinfo->prefix_len,
24344f672235SAlexander Aring 					    addr_type&IPV6_ADDR_SCOPE_MASK,
24354f672235SAlexander Aring 					    addr_flags, valid_lft,
24364f672235SAlexander Aring 					    prefered_lft);
24374f672235SAlexander Aring 
24384f672235SAlexander Aring 		if (IS_ERR_OR_NULL(ifp))
24394f672235SAlexander Aring 			return -1;
24404f672235SAlexander Aring 
24414f672235SAlexander Aring 		update_lft = 0;
24424f672235SAlexander Aring 		create = 1;
24434f672235SAlexander Aring 		spin_lock_bh(&ifp->lock);
24444f672235SAlexander Aring 		ifp->flags |= IFA_F_MANAGETEMPADDR;
24454f672235SAlexander Aring 		ifp->cstamp = jiffies;
24464f672235SAlexander Aring 		ifp->tokenized = tokenized;
24474f672235SAlexander Aring 		spin_unlock_bh(&ifp->lock);
24484f672235SAlexander Aring 		addrconf_dad_start(ifp);
24494f672235SAlexander Aring 	}
24504f672235SAlexander Aring 
24514f672235SAlexander Aring 	if (ifp) {
24524f672235SAlexander Aring 		u32 flags;
24534f672235SAlexander Aring 		unsigned long now;
24544f672235SAlexander Aring 		u32 stored_lft;
24554f672235SAlexander Aring 
24564f672235SAlexander Aring 		/* update lifetime (RFC2462 5.5.3 e) */
24574f672235SAlexander Aring 		spin_lock_bh(&ifp->lock);
24584f672235SAlexander Aring 		now = jiffies;
24594f672235SAlexander Aring 		if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
24604f672235SAlexander Aring 			stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
24614f672235SAlexander Aring 		else
24624f672235SAlexander Aring 			stored_lft = 0;
24634f672235SAlexander Aring 		if (!update_lft && !create && stored_lft) {
24644f672235SAlexander Aring 			const u32 minimum_lft = min_t(u32,
24654f672235SAlexander Aring 				stored_lft, MIN_VALID_LIFETIME);
24664f672235SAlexander Aring 			valid_lft = max(valid_lft, minimum_lft);
24674f672235SAlexander Aring 
24684f672235SAlexander Aring 			/* RFC4862 Section 5.5.3e:
24694f672235SAlexander Aring 			 * "Note that the preferred lifetime of the
24704f672235SAlexander Aring 			 *  corresponding address is always reset to
24714f672235SAlexander Aring 			 *  the Preferred Lifetime in the received
24724f672235SAlexander Aring 			 *  Prefix Information option, regardless of
24734f672235SAlexander Aring 			 *  whether the valid lifetime is also reset or
24744f672235SAlexander Aring 			 *  ignored."
24754f672235SAlexander Aring 			 *
24764f672235SAlexander Aring 			 * So we should always update prefered_lft here.
24774f672235SAlexander Aring 			 */
24784f672235SAlexander Aring 			update_lft = 1;
24794f672235SAlexander Aring 		}
24804f672235SAlexander Aring 
24814f672235SAlexander Aring 		if (update_lft) {
24824f672235SAlexander Aring 			ifp->valid_lft = valid_lft;
24834f672235SAlexander Aring 			ifp->prefered_lft = prefered_lft;
24844f672235SAlexander Aring 			ifp->tstamp = now;
24854f672235SAlexander Aring 			flags = ifp->flags;
24864f672235SAlexander Aring 			ifp->flags &= ~IFA_F_DEPRECATED;
24874f672235SAlexander Aring 			spin_unlock_bh(&ifp->lock);
24884f672235SAlexander Aring 
24894f672235SAlexander Aring 			if (!(flags&IFA_F_TENTATIVE))
24904f672235SAlexander Aring 				ipv6_ifa_notify(0, ifp);
24914f672235SAlexander Aring 		} else
24924f672235SAlexander Aring 			spin_unlock_bh(&ifp->lock);
24934f672235SAlexander Aring 
24944f672235SAlexander Aring 		manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
24954f672235SAlexander Aring 				 create, now);
24964f672235SAlexander Aring 
24974f672235SAlexander Aring 		in6_ifa_put(ifp);
24984f672235SAlexander Aring 		addrconf_verify();
24994f672235SAlexander Aring 	}
25004f672235SAlexander Aring 
25014f672235SAlexander Aring 	return 0;
25024f672235SAlexander Aring }
2503cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
25044f672235SAlexander Aring 
2505e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
25061da177e4SLinus Torvalds {
25071da177e4SLinus Torvalds 	struct prefix_info *pinfo;
25081da177e4SLinus Torvalds 	__u32 valid_lft;
25091da177e4SLinus Torvalds 	__u32 prefered_lft;
25104f672235SAlexander Aring 	int addr_type, err;
251164236f3fSHannes Frederic Sowa 	u32 addr_flags = 0;
25121da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
251356d417b1SBrian Haley 	struct net *net = dev_net(dev);
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
2518ba3542e1Sdingtianhong 		ADBG("addrconf: prefix option too short\n");
25191da177e4SLinus Torvalds 		return;
25201da177e4SLinus Torvalds 	}
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds 	/*
25231da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
25241da177e4SLinus Torvalds 	 */
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
25291da177e4SLinus Torvalds 		return;
25301da177e4SLinus Torvalds 
25311da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
25321da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2535e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
25361da177e4SLinus Torvalds 		return;
25371da177e4SLinus Torvalds 	}
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
25401da177e4SLinus Torvalds 
254163159f29SIan Morris 	if (!in6_dev) {
2542e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2543e87cc472SJoe Perches 				    dev->name);
25441da177e4SLinus Torvalds 		return;
25451da177e4SLinus Torvalds 	}
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 	/*
25481da177e4SLinus Torvalds 	 *	Two things going on here:
25491da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
25501da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
25511da177e4SLinus Torvalds 	 */
25521da177e4SLinus Torvalds 
25534bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
25544bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
25554bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
25564bed72e4SYOSHIFUJI Hideaki 
25571da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
25586f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
25596f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
25606f704992SYOSHIFUJI Hideaki 		 * not good.
25611da177e4SLinus Torvalds 		 */
25624bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
25634bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
25644bed72e4SYOSHIFUJI Hideaki 		else
25654bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
25663dd4bc68SYOSHIFUJI Hideaki 
25674bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
25684bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
25691da177e4SLinus Torvalds 
257014ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
257114ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
257214ef37b6SAndreas Hofmeister 					       dev,
257314ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
257414ef37b6SAndreas Hofmeister 					       RTF_GATEWAY | RTF_DEFAULT);
25751da177e4SLinus Torvalds 
257614ef37b6SAndreas Hofmeister 		if (rt) {
25776f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
25781da177e4SLinus Torvalds 			if (valid_lft == 0) {
2579e0a1ad73SThomas Graf 				ip6_del_rt(rt);
25801da177e4SLinus Torvalds 				rt = NULL;
25814bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
25826f704992SYOSHIFUJI Hideaki 				/* not infinity */
25831716a961SGao feng 				rt6_set_expires(rt, jiffies + rt_expires);
25846f704992SYOSHIFUJI Hideaki 			} else {
25851716a961SGao feng 				rt6_clean_expires(rt);
25861da177e4SLinus Torvalds 			}
25871da177e4SLinus Torvalds 		} else if (valid_lft) {
25886f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
25894bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
25904bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
25916f704992SYOSHIFUJI Hideaki 				/* not infinity */
25926f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
25936f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
25946f704992SYOSHIFUJI Hideaki 			}
25951da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
25966f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
25971da177e4SLinus Torvalds 		}
259894e187c0SAmerigo Wang 		ip6_rt_put(rt);
25991da177e4SLinus Torvalds 	}
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
26041da177e4SLinus Torvalds 		struct in6_addr addr;
2605f997c55cSAlexander Aring 		bool tokenized = false, dev_addr_generated = false;
26061da177e4SLinus Torvalds 
26071da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
26081da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2609f53adae4SDaniel Borkmann 
2610f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2611f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2612f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2613f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2614f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
2615617fe29dSDaniel Borkmann 				tokenized = true;
2616cc9da6ccSBjørn Mork 			} else if (is_addr_mode_generate_stable(in6_dev) &&
2617622c81d5SHannes Frederic Sowa 				   !ipv6_generate_stable_address(&addr, 0,
2618622c81d5SHannes Frederic Sowa 								 in6_dev)) {
261964236f3fSHannes Frederic Sowa 				addr_flags |= IFA_F_STABLE_PRIVACY;
2620622c81d5SHannes Frederic Sowa 				goto ok;
2621f53adae4SDaniel Borkmann 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
26221da177e4SLinus Torvalds 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
26234f672235SAlexander Aring 				goto put;
2624f997c55cSAlexander Aring 			} else {
2625f997c55cSAlexander Aring 				dev_addr_generated = true;
26261da177e4SLinus Torvalds 			}
26271da177e4SLinus Torvalds 			goto ok;
26281da177e4SLinus Torvalds 		}
2629e87cc472SJoe Perches 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
26301da177e4SLinus Torvalds 				    pinfo->prefix_len);
26314f672235SAlexander Aring 		goto put;
26321da177e4SLinus Torvalds 
26331da177e4SLinus Torvalds ok:
26344f672235SAlexander Aring 		err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
26354f672235SAlexander Aring 						   &addr, addr_type,
26364f672235SAlexander Aring 						   addr_flags, sllao,
26374f672235SAlexander Aring 						   tokenized, valid_lft,
26388a226b2cSJiri Benc 						   prefered_lft);
26394f672235SAlexander Aring 		if (err)
26404f672235SAlexander Aring 			goto put;
2641f997c55cSAlexander Aring 
2642f997c55cSAlexander Aring 		/* Ignore error case here because previous prefix add addr was
2643f997c55cSAlexander Aring 		 * successful which will be notified.
2644f997c55cSAlexander Aring 		 */
2645f997c55cSAlexander Aring 		ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2646f997c55cSAlexander Aring 					      addr_type, addr_flags, sllao,
2647f997c55cSAlexander Aring 					      tokenized, valid_lft,
2648f997c55cSAlexander Aring 					      prefered_lft,
2649f997c55cSAlexander Aring 					      dev_addr_generated);
26501da177e4SLinus Torvalds 	}
26511da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
26524f672235SAlexander Aring put:
26531da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
26541da177e4SLinus Torvalds }
26551da177e4SLinus Torvalds 
26561da177e4SLinus Torvalds /*
26571da177e4SLinus Torvalds  *	Set destination address.
26581da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
26591da177e4SLinus Torvalds  *	device.
26601da177e4SLinus Torvalds  */
2661af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
26621da177e4SLinus Torvalds {
26631da177e4SLinus Torvalds 	struct in6_ifreq ireq;
26641da177e4SLinus Torvalds 	struct net_device *dev;
26651da177e4SLinus Torvalds 	int err = -EINVAL;
26661da177e4SLinus Torvalds 
26671da177e4SLinus Torvalds 	rtnl_lock();
26681da177e4SLinus Torvalds 
26691da177e4SLinus Torvalds 	err = -EFAULT;
26701da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
26711da177e4SLinus Torvalds 		goto err_exit;
26721da177e4SLinus Torvalds 
2673af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
26741da177e4SLinus Torvalds 
26751da177e4SLinus Torvalds 	err = -ENODEV;
267663159f29SIan Morris 	if (!dev)
26771da177e4SLinus Torvalds 		goto err_exit;
26781da177e4SLinus Torvalds 
267907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
26801da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
26815bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
26821da177e4SLinus Torvalds 		struct ifreq ifr;
26831da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
26841da177e4SLinus Torvalds 
26851da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
26861da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
26871da177e4SLinus Torvalds 			goto err_exit;
26881da177e4SLinus Torvalds 
26891da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
26901da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
26911da177e4SLinus Torvalds 		p.iph.saddr = 0;
26921da177e4SLinus Torvalds 		p.iph.version = 4;
26931da177e4SLinus Torvalds 		p.iph.ihl = 5;
26941da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
26951da177e4SLinus Torvalds 		p.iph.ttl = 64;
2696d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
26971da177e4SLinus Torvalds 
26985bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
26995bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
27005bc3eb7eSStephen Hemminger 
27015bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
27025bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
27031da177e4SLinus Torvalds 			set_fs(oldfs);
27045bc3eb7eSStephen Hemminger 		} else
27055bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds 		if (err == 0) {
27081da177e4SLinus Torvalds 			err = -ENOBUFS;
2709af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2710af284937SDaniel Lezcano 			if (!dev)
27111da177e4SLinus Torvalds 				goto err_exit;
27121da177e4SLinus Torvalds 			err = dev_open(dev);
27131da177e4SLinus Torvalds 		}
27141da177e4SLinus Torvalds 	}
27150be669bbSJoerg Roedel #endif
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds err_exit:
27181da177e4SLinus Torvalds 	rtnl_unlock();
27191da177e4SLinus Torvalds 	return err;
27201da177e4SLinus Torvalds }
27211da177e4SLinus Torvalds 
272293a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join,
272393a714d6SMadhu Challa 			  const struct in6_addr *addr, int ifindex)
272493a714d6SMadhu Challa {
272593a714d6SMadhu Challa 	int ret;
272693a714d6SMadhu Challa 
272793a714d6SMadhu Challa 	ASSERT_RTNL();
272893a714d6SMadhu Challa 
272993a714d6SMadhu Challa 	lock_sock(sk);
273093a714d6SMadhu Challa 	if (join)
273154ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_join(sk, ifindex, addr);
273293a714d6SMadhu Challa 	else
273354ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_drop(sk, ifindex, addr);
273493a714d6SMadhu Challa 	release_sock(sk);
273593a714d6SMadhu Challa 
273693a714d6SMadhu Challa 	return ret;
273793a714d6SMadhu Challa }
273893a714d6SMadhu Challa 
27391da177e4SLinus Torvalds /*
27401da177e4SLinus Torvalds  *	Manual configuration of address on an interface
27411da177e4SLinus Torvalds  */
2742479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex,
2743479840ffSJiri Pirko 			  const struct in6_addr *pfx,
2744caeaba79SNicolas Dichtel 			  const struct in6_addr *peer_pfx,
2745479840ffSJiri Pirko 			  unsigned int plen, __u32 ifa_flags,
2746479840ffSJiri Pirko 			  __u32 prefered_lft, __u32 valid_lft)
27471da177e4SLinus Torvalds {
27481da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
27491da177e4SLinus Torvalds 	struct inet6_dev *idev;
27501da177e4SLinus Torvalds 	struct net_device *dev;
275193a714d6SMadhu Challa 	unsigned long timeout;
275293a714d6SMadhu Challa 	clock_t expires;
27531da177e4SLinus Torvalds 	int scope;
27546f704992SYOSHIFUJI Hideaki 	u32 flags;
27551da177e4SLinus Torvalds 
27561da177e4SLinus Torvalds 	ASSERT_RTNL();
27571da177e4SLinus Torvalds 
275824ef0da7SThomas Graf 	if (plen > 128)
275924ef0da7SThomas Graf 		return -EINVAL;
276024ef0da7SThomas Graf 
27610778769dSNoriaki TAKAMIYA 	/* check the lifetime */
27620778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
27630778769dSNoriaki TAKAMIYA 		return -EINVAL;
27640778769dSNoriaki TAKAMIYA 
276553bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
276653bd6749SJiri Pirko 		return -EINVAL;
276753bd6749SJiri Pirko 
2768af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2769af284937SDaniel Lezcano 	if (!dev)
27701da177e4SLinus Torvalds 		return -ENODEV;
27711da177e4SLinus Torvalds 
277264e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
277364e724f6SBrian Haley 	if (IS_ERR(idev))
277464e724f6SBrian Haley 		return PTR_ERR(idev);
27751da177e4SLinus Torvalds 
277693a714d6SMadhu Challa 	if (ifa_flags & IFA_F_MCAUTOJOIN) {
277793a714d6SMadhu Challa 		int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
277893a714d6SMadhu Challa 					 true, pfx, ifindex);
277993a714d6SMadhu Challa 
278093a714d6SMadhu Challa 		if (ret < 0)
278193a714d6SMadhu Challa 			return ret;
278293a714d6SMadhu Challa 	}
278393a714d6SMadhu Challa 
27841da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
27851da177e4SLinus Torvalds 
27864bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
27874bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
27884bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
27894bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
27906f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
27914bed72e4SYOSHIFUJI Hideaki 	} else {
27924bed72e4SYOSHIFUJI Hideaki 		expires = 0;
27934bed72e4SYOSHIFUJI Hideaki 		flags = 0;
27944bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
27956f704992SYOSHIFUJI Hideaki 	}
27960778769dSNoriaki TAKAMIYA 
27974bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
27984bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
27994bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
28000778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
28014bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
28024bed72e4SYOSHIFUJI Hideaki 	}
28030778769dSNoriaki TAKAMIYA 
28048a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
28058a226b2cSJiri Benc 			    valid_lft, prefered_lft);
28060778769dSNoriaki TAKAMIYA 
28071da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
2808761aac73SThomas Haller 		if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
280946d48046SYOSHIFUJI Hideaki 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
28106f704992SYOSHIFUJI Hideaki 					      expires, flags);
2811761aac73SThomas Haller 		}
2812761aac73SThomas Haller 
281395c385b4SNeil Horman 		/*
281495c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
281595c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
281695c385b4SNeil Horman 		 * manually configured addresses
281795c385b4SNeil Horman 		 */
2818cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
281953bd6749SJiri Pirko 		if (ifa_flags & IFA_F_MANAGETEMPADDR)
282053bd6749SJiri Pirko 			manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
282153bd6749SJiri Pirko 					 true, jiffies);
28221da177e4SLinus Torvalds 		in6_ifa_put(ifp);
2823c15b1ccaSHannes Frederic Sowa 		addrconf_verify_rtnl();
28241da177e4SLinus Torvalds 		return 0;
282593a714d6SMadhu Challa 	} else if (ifa_flags & IFA_F_MCAUTOJOIN) {
282693a714d6SMadhu Challa 		ipv6_mc_config(net->ipv6.mc_autojoin_sk,
282793a714d6SMadhu Challa 			       false, pfx, ifindex);
28281da177e4SLinus Torvalds 	}
28291da177e4SLinus Torvalds 
28301da177e4SLinus Torvalds 	return PTR_ERR(ifp);
28311da177e4SLinus Torvalds }
28321da177e4SLinus Torvalds 
28336046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
28346046d5b4SHeiner Kallweit 			  const struct in6_addr *pfx, unsigned int plen)
28351da177e4SLinus Torvalds {
28361da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
28371da177e4SLinus Torvalds 	struct inet6_dev *idev;
28381da177e4SLinus Torvalds 	struct net_device *dev;
28391da177e4SLinus Torvalds 
284024ef0da7SThomas Graf 	if (plen > 128)
284124ef0da7SThomas Graf 		return -EINVAL;
284224ef0da7SThomas Graf 
2843af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2844af284937SDaniel Lezcano 	if (!dev)
28451da177e4SLinus Torvalds 		return -ENODEV;
28461da177e4SLinus Torvalds 
2847e5d08d71SIan Morris 	idev = __in6_dev_get(dev);
284863159f29SIan Morris 	if (!idev)
28491da177e4SLinus Torvalds 		return -ENXIO;
28501da177e4SLinus Torvalds 
28511da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2852502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
28531da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
28541da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
28551da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
28561da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
28571da177e4SLinus Torvalds 
28586046d5b4SHeiner Kallweit 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
28596046d5b4SHeiner Kallweit 			    (ifa_flags & IFA_F_MANAGETEMPADDR))
28606046d5b4SHeiner Kallweit 				manage_tempaddrs(idev, ifp, 0, 0, false,
28616046d5b4SHeiner Kallweit 						 jiffies);
28621da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
28636046d5b4SHeiner Kallweit 			addrconf_verify_rtnl();
286493a714d6SMadhu Challa 			if (ipv6_addr_is_multicast(pfx)) {
286593a714d6SMadhu Challa 				ipv6_mc_config(net->ipv6.mc_autojoin_sk,
286693a714d6SMadhu Challa 					       false, pfx, dev->ifindex);
286793a714d6SMadhu Challa 			}
28681da177e4SLinus Torvalds 			return 0;
28691da177e4SLinus Torvalds 		}
28701da177e4SLinus Torvalds 	}
28711da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
28721da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
28731da177e4SLinus Torvalds }
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds 
2876af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
28771da177e4SLinus Torvalds {
28781da177e4SLinus Torvalds 	struct in6_ifreq ireq;
28791da177e4SLinus Torvalds 	int err;
28801da177e4SLinus Torvalds 
2881af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
28821da177e4SLinus Torvalds 		return -EPERM;
28831da177e4SLinus Torvalds 
28841da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
28851da177e4SLinus Torvalds 		return -EFAULT;
28861da177e4SLinus Torvalds 
28871da177e4SLinus Torvalds 	rtnl_lock();
2888caeaba79SNicolas Dichtel 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2889af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2890af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
28911da177e4SLinus Torvalds 	rtnl_unlock();
28921da177e4SLinus Torvalds 	return err;
28931da177e4SLinus Torvalds }
28941da177e4SLinus Torvalds 
2895af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
28961da177e4SLinus Torvalds {
28971da177e4SLinus Torvalds 	struct in6_ifreq ireq;
28981da177e4SLinus Torvalds 	int err;
28991da177e4SLinus Torvalds 
2900af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
29011da177e4SLinus Torvalds 		return -EPERM;
29021da177e4SLinus Torvalds 
29031da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
29041da177e4SLinus Torvalds 		return -EFAULT;
29051da177e4SLinus Torvalds 
29061da177e4SLinus Torvalds 	rtnl_lock();
29076046d5b4SHeiner Kallweit 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2908af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
29091da177e4SLinus Torvalds 	rtnl_unlock();
29101da177e4SLinus Torvalds 	return err;
29111da177e4SLinus Torvalds }
29121da177e4SLinus Torvalds 
2913b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2914b5f348e5SIlpo Järvinen 		     int plen, int scope)
2915b5f348e5SIlpo Järvinen {
2916b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
2917b5f348e5SIlpo Järvinen 
29188a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, addr, NULL, plen,
291907edd741SHannes Frederic Sowa 			    scope, IFA_F_PERMANENT,
292007edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2921b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
2922b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
2923b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
2924b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
2925764d3be6SPaolo Abeni 		rt_genid_bump_ipv6(dev_net(idev->dev));
2926b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2927b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
2928b5f348e5SIlpo Järvinen 	}
2929b5f348e5SIlpo Järvinen }
2930b5f348e5SIlpo Järvinen 
293107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
29321da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
29331da177e4SLinus Torvalds {
29341da177e4SLinus Torvalds 	struct in6_addr addr;
29351da177e4SLinus Torvalds 	struct net_device *dev;
2936c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
2937929c9cf3SNicolas Dichtel 	int scope, plen;
2938f0e2acfaSNicolas Dichtel 	u32 pflags = 0;
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds 	ASSERT_RTNL();
29411da177e4SLinus Torvalds 
29421da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
29431da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
29441da177e4SLinus Torvalds 
29451da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
29461da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
29471da177e4SLinus Torvalds 		scope = IFA_LINK;
2948929c9cf3SNicolas Dichtel 		plen = 64;
29491da177e4SLinus Torvalds 	} else {
29501da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
2951929c9cf3SNicolas Dichtel 		plen = 96;
2952f0e2acfaSNicolas Dichtel 		pflags |= RTF_NONEXTHOP;
29531da177e4SLinus Torvalds 	}
29541da177e4SLinus Torvalds 
29551da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
2956929c9cf3SNicolas Dichtel 		add_addr(idev, &addr, plen, scope);
2957f0e2acfaSNicolas Dichtel 		addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
29581da177e4SLinus Torvalds 		return;
29591da177e4SLinus Torvalds 	}
29601da177e4SLinus Torvalds 
29616fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
2962e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
29631da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
29641da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
29651da177e4SLinus Torvalds 
29661da177e4SLinus Torvalds 			int flag = scope;
29671da177e4SLinus Torvalds 
29681da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
29691da177e4SLinus Torvalds 
29701da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
29711da177e4SLinus Torvalds 
29721da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
29731da177e4SLinus Torvalds 					continue;
29741da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
29751da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
29761da177e4SLinus Torvalds 						continue;
29771da177e4SLinus Torvalds 					flag |= IFA_HOST;
29781da177e4SLinus Torvalds 				}
29791da177e4SLinus Torvalds 
2980b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
2981f0e2acfaSNicolas Dichtel 				addrconf_prefix_route(&addr, plen, idev->dev, 0,
2982f0e2acfaSNicolas Dichtel 						      pflags);
29831da177e4SLinus Torvalds 			}
29841da177e4SLinus Torvalds 		}
29851da177e4SLinus Torvalds 	}
29861da177e4SLinus Torvalds }
29870be669bbSJoerg Roedel #endif
29881da177e4SLinus Torvalds 
29891da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
29901da177e4SLinus Torvalds {
29911da177e4SLinus Torvalds 	struct inet6_dev  *idev;
299225fb6ca4SBalakumaran Kannan 	struct net_device *sp_dev;
299325fb6ca4SBalakumaran Kannan 	struct inet6_ifaddr *sp_ifa;
299425fb6ca4SBalakumaran Kannan 	struct rt6_info *sp_rt;
29951da177e4SLinus Torvalds 
29961da177e4SLinus Torvalds 	/* ::1 */
29971da177e4SLinus Torvalds 
29981da177e4SLinus Torvalds 	ASSERT_RTNL();
29991da177e4SLinus Torvalds 
3000e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
300163159f29SIan Morris 	if (!idev) {
300291df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
30031da177e4SLinus Torvalds 		return;
30041da177e4SLinus Torvalds 	}
30051da177e4SLinus Torvalds 
3006b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
300725fb6ca4SBalakumaran Kannan 
300825fb6ca4SBalakumaran Kannan 	/* Add routes to other interface's IPv6 addresses */
300925fb6ca4SBalakumaran Kannan 	for_each_netdev(dev_net(dev), sp_dev) {
301025fb6ca4SBalakumaran Kannan 		if (!strcmp(sp_dev->name, dev->name))
301125fb6ca4SBalakumaran Kannan 			continue;
301225fb6ca4SBalakumaran Kannan 
301325fb6ca4SBalakumaran Kannan 		idev = __in6_dev_get(sp_dev);
301425fb6ca4SBalakumaran Kannan 		if (!idev)
301525fb6ca4SBalakumaran Kannan 			continue;
301625fb6ca4SBalakumaran Kannan 
301725fb6ca4SBalakumaran Kannan 		read_lock_bh(&idev->lock);
301825fb6ca4SBalakumaran Kannan 		list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
301925fb6ca4SBalakumaran Kannan 
302025fb6ca4SBalakumaran Kannan 			if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
302125fb6ca4SBalakumaran Kannan 				continue;
302225fb6ca4SBalakumaran Kannan 
302333d99113SGao feng 			if (sp_ifa->rt) {
302433d99113SGao feng 				/* This dst has been added to garbage list when
302533d99113SGao feng 				 * lo device down, release this obsolete dst and
302633d99113SGao feng 				 * reallocate a new router for ifa.
302733d99113SGao feng 				 */
3028a220445fSNicolas Dichtel 				if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
302933d99113SGao feng 					ip6_rt_put(sp_ifa->rt);
303033d99113SGao feng 					sp_ifa->rt = NULL;
303133d99113SGao feng 				} else {
3032a881ae1fSGao feng 					continue;
303333d99113SGao feng 				}
303433d99113SGao feng 			}
3035a881ae1fSGao feng 
303657ec0afeSFrançois-Xavier Le Bail 			sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
303725fb6ca4SBalakumaran Kannan 
303825fb6ca4SBalakumaran Kannan 			/* Failure cases are ignored */
3039534c8779SGao feng 			if (!IS_ERR(sp_rt)) {
3040534c8779SGao feng 				sp_ifa->rt = sp_rt;
304125fb6ca4SBalakumaran Kannan 				ip6_ins_rt(sp_rt);
304225fb6ca4SBalakumaran Kannan 			}
3043534c8779SGao feng 		}
304425fb6ca4SBalakumaran Kannan 		read_unlock_bh(&idev->lock);
304525fb6ca4SBalakumaran Kannan 	}
30461da177e4SLinus Torvalds }
30471da177e4SLinus Torvalds 
30482ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev,
304964236f3fSHannes Frederic Sowa 			    const struct in6_addr *addr, u32 flags)
30501da177e4SLinus Torvalds {
30511da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
305264236f3fSHannes Frederic Sowa 	u32 addr_flags = flags | IFA_F_PERMANENT;
30531da177e4SLinus Torvalds 
305495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
305595c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
3056702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
305795c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
305895c385b4SNeil Horman #endif
305995c385b4SNeil Horman 
306007edd741SHannes Frederic Sowa 	ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
306107edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
30621da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
306346d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
3064cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
30651da177e4SLinus Torvalds 		in6_ifa_put(ifp);
30661da177e4SLinus Torvalds 	}
30671da177e4SLinus Torvalds }
30682ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
30691da177e4SLinus Torvalds 
3070622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address)
3071622c81d5SHannes Frederic Sowa {
3072622c81d5SHannes Frederic Sowa 	if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3073622c81d5SHannes Frederic Sowa 		return true;
3074622c81d5SHannes Frederic Sowa 
3075622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0x02005eff) &&
3076622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3077622c81d5SHannes Frederic Sowa 		return true;
3078622c81d5SHannes Frederic Sowa 
3079622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3080622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3081622c81d5SHannes Frederic Sowa 		return true;
3082622c81d5SHannes Frederic Sowa 
3083622c81d5SHannes Frederic Sowa 	return false;
3084622c81d5SHannes Frederic Sowa }
3085622c81d5SHannes Frederic Sowa 
3086622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address,
3087622c81d5SHannes Frederic Sowa 					u8 dad_count,
3088622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev)
3089622c81d5SHannes Frederic Sowa {
3090622c81d5SHannes Frederic Sowa 	static DEFINE_SPINLOCK(lock);
3091622c81d5SHannes Frederic Sowa 	static __u32 digest[SHA_DIGEST_WORDS];
3092622c81d5SHannes Frederic Sowa 	static __u32 workspace[SHA_WORKSPACE_WORDS];
3093622c81d5SHannes Frederic Sowa 
3094622c81d5SHannes Frederic Sowa 	static union {
3095622c81d5SHannes Frederic Sowa 		char __data[SHA_MESSAGE_BYTES];
3096622c81d5SHannes Frederic Sowa 		struct {
3097622c81d5SHannes Frederic Sowa 			struct in6_addr secret;
3098ff40217eSHannes Frederic Sowa 			__be32 prefix[2];
3099622c81d5SHannes Frederic Sowa 			unsigned char hwaddr[MAX_ADDR_LEN];
3100622c81d5SHannes Frederic Sowa 			u8 dad_count;
3101622c81d5SHannes Frederic Sowa 		} __packed;
3102622c81d5SHannes Frederic Sowa 	} data;
3103622c81d5SHannes Frederic Sowa 
3104622c81d5SHannes Frederic Sowa 	struct in6_addr secret;
3105622c81d5SHannes Frederic Sowa 	struct in6_addr temp;
3106622c81d5SHannes Frederic Sowa 	struct net *net = dev_net(idev->dev);
3107622c81d5SHannes Frederic Sowa 
3108622c81d5SHannes Frederic Sowa 	BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3109622c81d5SHannes Frederic Sowa 
3110622c81d5SHannes Frederic Sowa 	if (idev->cnf.stable_secret.initialized)
3111622c81d5SHannes Frederic Sowa 		secret = idev->cnf.stable_secret.secret;
3112622c81d5SHannes Frederic Sowa 	else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3113622c81d5SHannes Frederic Sowa 		secret = net->ipv6.devconf_dflt->stable_secret.secret;
3114622c81d5SHannes Frederic Sowa 	else
3115622c81d5SHannes Frederic Sowa 		return -1;
3116622c81d5SHannes Frederic Sowa 
3117622c81d5SHannes Frederic Sowa retry:
3118622c81d5SHannes Frederic Sowa 	spin_lock_bh(&lock);
3119622c81d5SHannes Frederic Sowa 
3120622c81d5SHannes Frederic Sowa 	sha_init(digest);
3121622c81d5SHannes Frederic Sowa 	memset(&data, 0, sizeof(data));
3122622c81d5SHannes Frederic Sowa 	memset(workspace, 0, sizeof(workspace));
3123622c81d5SHannes Frederic Sowa 	memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3124ff40217eSHannes Frederic Sowa 	data.prefix[0] = address->s6_addr32[0];
3125ff40217eSHannes Frederic Sowa 	data.prefix[1] = address->s6_addr32[1];
3126622c81d5SHannes Frederic Sowa 	data.secret = secret;
3127622c81d5SHannes Frederic Sowa 	data.dad_count = dad_count;
3128622c81d5SHannes Frederic Sowa 
3129622c81d5SHannes Frederic Sowa 	sha_transform(digest, data.__data, workspace);
3130622c81d5SHannes Frederic Sowa 
3131622c81d5SHannes Frederic Sowa 	temp = *address;
3132ff40217eSHannes Frederic Sowa 	temp.s6_addr32[2] = (__force __be32)digest[0];
3133ff40217eSHannes Frederic Sowa 	temp.s6_addr32[3] = (__force __be32)digest[1];
3134622c81d5SHannes Frederic Sowa 
3135622c81d5SHannes Frederic Sowa 	spin_unlock_bh(&lock);
3136622c81d5SHannes Frederic Sowa 
3137622c81d5SHannes Frederic Sowa 	if (ipv6_reserved_interfaceid(temp)) {
3138622c81d5SHannes Frederic Sowa 		dad_count++;
31391855b7c3SHannes Frederic Sowa 		if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3140622c81d5SHannes Frederic Sowa 			return -1;
3141622c81d5SHannes Frederic Sowa 		goto retry;
3142622c81d5SHannes Frederic Sowa 	}
3143622c81d5SHannes Frederic Sowa 
3144622c81d5SHannes Frederic Sowa 	*address = temp;
3145622c81d5SHannes Frederic Sowa 	return 0;
3146622c81d5SHannes Frederic Sowa }
3147622c81d5SHannes Frederic Sowa 
3148cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3149cc9da6ccSBjørn Mork {
3150cc9da6ccSBjørn Mork 	struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3151cc9da6ccSBjørn Mork 
3152cc9da6ccSBjørn Mork 	if (s->initialized)
3153cc9da6ccSBjørn Mork 		return;
3154cc9da6ccSBjørn Mork 	s = &idev->cnf.stable_secret;
3155cc9da6ccSBjørn Mork 	get_random_bytes(&s->secret, sizeof(s->secret));
3156cc9da6ccSBjørn Mork 	s->initialized = true;
3157cc9da6ccSBjørn Mork }
3158cc9da6ccSBjørn Mork 
3159bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3160bc91b0f0SJiri Pirko {
3161bc91b0f0SJiri Pirko 	struct in6_addr addr;
3162bc91b0f0SJiri Pirko 
3163ca254490SDavid Ahern 	/* no link local addresses on L3 master devices */
3164ca254490SDavid Ahern 	if (netif_is_l3_master(idev->dev))
3165ca254490SDavid Ahern 		return;
3166ca254490SDavid Ahern 
3167bc91b0f0SJiri Pirko 	ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3168622c81d5SHannes Frederic Sowa 
3169d35a00b8SFelix Jia 	switch (idev->cnf.addr_gen_mode) {
3170cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_RANDOM:
3171cc9da6ccSBjørn Mork 		ipv6_gen_mode_random_init(idev);
3172cc9da6ccSBjørn Mork 		/* fallthrough */
3173cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3174622c81d5SHannes Frederic Sowa 		if (!ipv6_generate_stable_address(&addr, 0, idev))
317564236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr,
317664236f3fSHannes Frederic Sowa 					       IFA_F_STABLE_PRIVACY);
3177622c81d5SHannes Frederic Sowa 		else if (prefix_route)
3178622c81d5SHannes Frederic Sowa 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3179cc9da6ccSBjørn Mork 		break;
3180cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_EUI64:
3181bc91b0f0SJiri Pirko 		/* addrconf_add_linklocal also adds a prefix_route and we
3182bc91b0f0SJiri Pirko 		 * only need to care about prefix routes if ipv6_generate_eui64
3183bc91b0f0SJiri Pirko 		 * couldn't generate one.
3184bc91b0f0SJiri Pirko 		 */
3185bc91b0f0SJiri Pirko 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
318664236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr, 0);
3187bc91b0f0SJiri Pirko 		else if (prefix_route)
3188bc91b0f0SJiri Pirko 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3189cc9da6ccSBjørn Mork 		break;
3190cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_NONE:
3191cc9da6ccSBjørn Mork 	default:
3192cc9da6ccSBjørn Mork 		/* will not add any link local address */
3193cc9da6ccSBjørn Mork 		break;
3194bc91b0f0SJiri Pirko 	}
3195bc91b0f0SJiri Pirko }
3196bc91b0f0SJiri Pirko 
31971da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
31981da177e4SLinus Torvalds {
31991da177e4SLinus Torvalds 	struct inet6_dev *idev;
32001da177e4SLinus Torvalds 
32011da177e4SLinus Torvalds 	ASSERT_RTNL();
32021da177e4SLinus Torvalds 
320374235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
320474235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
320574235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
320606a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
3207e837735eSNicolas Dichtel 	    (dev->type != ARPHRD_IEEE1394) &&
3208e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_TUNNEL6) &&
3209cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_6LOWPAN) &&
321045ce0fd1SFelix Jia 	    (dev->type != ARPHRD_IP6GRE) &&
321145ce0fd1SFelix Jia 	    (dev->type != ARPHRD_IPGRE) &&
321245ce0fd1SFelix Jia 	    (dev->type != ARPHRD_TUNNEL) &&
3213cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_NONE)) {
321474235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
321574235a25SHerbert Xu 		return;
321674235a25SHerbert Xu 	}
321774235a25SHerbert Xu 
32181da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
321964e724f6SBrian Haley 	if (IS_ERR(idev))
32201da177e4SLinus Torvalds 		return;
32211da177e4SLinus Torvalds 
3222cc9da6ccSBjørn Mork 	/* this device type has no EUI support */
3223cc9da6ccSBjørn Mork 	if (dev->type == ARPHRD_NONE &&
3224d35a00b8SFelix Jia 	    idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3225d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3226cc9da6ccSBjørn Mork 
3227bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, false);
32281da177e4SLinus Torvalds }
32291da177e4SLinus Torvalds 
323007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
32311da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
32321da177e4SLinus Torvalds {
32331da177e4SLinus Torvalds 	struct inet6_dev *idev;
32341da177e4SLinus Torvalds 
32351da177e4SLinus Torvalds 	ASSERT_RTNL();
32361da177e4SLinus Torvalds 
32371da177e4SLinus Torvalds 	/*
32381da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
32391da177e4SLinus Torvalds 	 * addresses... we should configure all of
32401da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
32411da177e4SLinus Torvalds 	 */
32421da177e4SLinus Torvalds 
3243e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
324463159f29SIan Morris 	if (!idev) {
324591df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
32461da177e4SLinus Torvalds 		return;
32471da177e4SLinus Torvalds 	}
32481da177e4SLinus Torvalds 
3249c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
3250bc91b0f0SJiri Pirko 		addrconf_addr_gen(idev, false);
3251c7dc89c0SFred L. Templin 		return;
3252c7dc89c0SFred L. Templin 	}
3253c7dc89c0SFred L. Templin 
32541da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
32551da177e4SLinus Torvalds 
325662b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
32571da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
32581da177e4SLinus Torvalds }
32590be669bbSJoerg Roedel #endif
32601da177e4SLinus Torvalds 
326107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3262aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
3263aee80b54Sstephen hemminger {
3264aee80b54Sstephen hemminger 	struct inet6_dev *idev;
3265aee80b54Sstephen hemminger 
3266aee80b54Sstephen hemminger 	ASSERT_RTNL();
3267aee80b54Sstephen hemminger 
3268e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
326963159f29SIan Morris 	if (!idev) {
327091df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
3271aee80b54Sstephen hemminger 		return;
3272aee80b54Sstephen hemminger 	}
3273aee80b54Sstephen hemminger 
3274bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, true);
3275d9e4ce65SHannes Frederic Sowa 	if (dev->flags & IFF_POINTOPOINT)
3276d9e4ce65SHannes Frederic Sowa 		addrconf_add_mroute(dev);
3277aee80b54Sstephen hemminger }
3278aee80b54Sstephen hemminger #endif
3279aee80b54Sstephen hemminger 
3280f1705ec1SDavid Ahern static int fixup_permanent_addr(struct inet6_dev *idev,
3281f1705ec1SDavid Ahern 				struct inet6_ifaddr *ifp)
3282f1705ec1SDavid Ahern {
3283f1705ec1SDavid Ahern 	if (!ifp->rt) {
3284f1705ec1SDavid Ahern 		struct rt6_info *rt;
3285f1705ec1SDavid Ahern 
3286f1705ec1SDavid Ahern 		rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3287f1705ec1SDavid Ahern 		if (unlikely(IS_ERR(rt)))
3288f1705ec1SDavid Ahern 			return PTR_ERR(rt);
3289f1705ec1SDavid Ahern 
3290f1705ec1SDavid Ahern 		ifp->rt = rt;
3291f1705ec1SDavid Ahern 	}
3292f1705ec1SDavid Ahern 
3293f1705ec1SDavid Ahern 	if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3294f1705ec1SDavid Ahern 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3295f1705ec1SDavid Ahern 				      idev->dev, 0, 0);
3296f1705ec1SDavid Ahern 	}
3297f1705ec1SDavid Ahern 
3298f1705ec1SDavid Ahern 	addrconf_dad_start(ifp);
3299f1705ec1SDavid Ahern 
3300f1705ec1SDavid Ahern 	return 0;
3301f1705ec1SDavid Ahern }
3302f1705ec1SDavid Ahern 
3303f1705ec1SDavid Ahern static void addrconf_permanent_addr(struct net_device *dev)
3304f1705ec1SDavid Ahern {
3305f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifp, *tmp;
3306f1705ec1SDavid Ahern 	struct inet6_dev *idev;
3307f1705ec1SDavid Ahern 
3308f1705ec1SDavid Ahern 	idev = __in6_dev_get(dev);
3309f1705ec1SDavid Ahern 	if (!idev)
3310f1705ec1SDavid Ahern 		return;
3311f1705ec1SDavid Ahern 
3312f1705ec1SDavid Ahern 	write_lock_bh(&idev->lock);
3313f1705ec1SDavid Ahern 
3314f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3315f1705ec1SDavid Ahern 		if ((ifp->flags & IFA_F_PERMANENT) &&
3316f1705ec1SDavid Ahern 		    fixup_permanent_addr(idev, ifp) < 0) {
3317f1705ec1SDavid Ahern 			write_unlock_bh(&idev->lock);
3318f1705ec1SDavid Ahern 			ipv6_del_addr(ifp);
3319f1705ec1SDavid Ahern 			write_lock_bh(&idev->lock);
3320f1705ec1SDavid Ahern 
3321f1705ec1SDavid Ahern 			net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3322f1705ec1SDavid Ahern 					     idev->dev->name, &ifp->addr);
3323f1705ec1SDavid Ahern 		}
3324f1705ec1SDavid Ahern 	}
3325f1705ec1SDavid Ahern 
3326f1705ec1SDavid Ahern 	write_unlock_bh(&idev->lock);
3327f1705ec1SDavid Ahern }
3328f1705ec1SDavid Ahern 
33291da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
3330351638e7SJiri Pirko 			   void *ptr)
33311da177e4SLinus Torvalds {
3332351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
33334f7f34eaSDavid Ahern 	struct netdev_notifier_changeupper_info *info;
3334748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
3335c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
3336b217d616SHerbert Xu 	int err;
33371da177e4SLinus Torvalds 
33381da177e4SLinus Torvalds 	switch (event) {
333945ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
334074235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
334145ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
3342a317a2f1SWANG Cong 			if (IS_ERR(idev))
3343a317a2f1SWANG Cong 				return notifier_from_errno(PTR_ERR(idev));
334445ba9dd2SYOSHIFUJI Hideaki 		}
334545ba9dd2SYOSHIFUJI Hideaki 		break;
3346bcdd553fSStephen Hemminger 
3347b7b0b1d2SAlexander Duyck 	case NETDEV_CHANGEMTU:
3348b7b0b1d2SAlexander Duyck 		/* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3349b7b0b1d2SAlexander Duyck 		if (dev->mtu < IPV6_MIN_MTU) {
3350b7b0b1d2SAlexander Duyck 			addrconf_ifdown(dev, 1);
3351b7b0b1d2SAlexander Duyck 			break;
3352b7b0b1d2SAlexander Duyck 		}
3353b7b0b1d2SAlexander Duyck 
3354b7b0b1d2SAlexander Duyck 		if (idev) {
3355b7b0b1d2SAlexander Duyck 			rt6_mtu_change(dev, dev->mtu);
3356b7b0b1d2SAlexander Duyck 			idev->cnf.mtu6 = dev->mtu;
3357b7b0b1d2SAlexander Duyck 			break;
3358b7b0b1d2SAlexander Duyck 		}
3359b7b0b1d2SAlexander Duyck 
3360b7b0b1d2SAlexander Duyck 		/* allocate new idev */
3361b7b0b1d2SAlexander Duyck 		idev = ipv6_add_dev(dev);
3362b7b0b1d2SAlexander Duyck 		if (IS_ERR(idev))
3363b7b0b1d2SAlexander Duyck 			break;
3364b7b0b1d2SAlexander Duyck 
3365b7b0b1d2SAlexander Duyck 		/* device is still not ready */
3366b7b0b1d2SAlexander Duyck 		if (!(idev->if_flags & IF_READY))
3367b7b0b1d2SAlexander Duyck 			break;
3368b7b0b1d2SAlexander Duyck 
3369b7b0b1d2SAlexander Duyck 		run_pending = 1;
3370b7b0b1d2SAlexander Duyck 
3371b7b0b1d2SAlexander Duyck 		/* fall through */
3372b7b0b1d2SAlexander Duyck 
33731da177e4SLinus Torvalds 	case NETDEV_UP:
33743c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
3375c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
3376c2edacf8SJay Vosburgh 			break;
3377c2edacf8SJay Vosburgh 
33783ce62a84SWANG Cong 		if (idev && idev->cnf.disable_ipv6)
33793ce62a84SWANG Cong 			break;
33803ce62a84SWANG Cong 
33813c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
338238bd10c4SDavid Ahern 			/* restore routes for permanent addresses */
338338bd10c4SDavid Ahern 			addrconf_permanent_addr(dev);
338438bd10c4SDavid Ahern 
3385f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
33863c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
3387f3213831SJoe Perches 				pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
33883c21edbdSYOSHIFUJI Hideaki 					dev->name);
33893c21edbdSYOSHIFUJI Hideaki 				break;
33903c21edbdSYOSHIFUJI Hideaki 			}
339199081049SKristian Slavov 
3392d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
3393d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
3394d31c7b8fSEvgeniy Polyakov 
3395a317a2f1SWANG Cong 			if (!IS_ERR_OR_NULL(idev)) {
339699081049SKristian Slavov 				idev->if_flags |= IF_READY;
3397e3ec6cfcSBenjamin Thery 				run_pending = 1;
3398e3ec6cfcSBenjamin Thery 			}
3399b7b0b1d2SAlexander Duyck 		} else if (event == NETDEV_CHANGE) {
3400f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
34013c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
34023c21edbdSYOSHIFUJI Hideaki 				break;
34033c21edbdSYOSHIFUJI Hideaki 			}
34043c21edbdSYOSHIFUJI Hideaki 
34053c21edbdSYOSHIFUJI Hideaki 			if (idev) {
3406a088d1d7SLinus Lüssing 				if (idev->if_flags & IF_READY) {
3407a088d1d7SLinus Lüssing 					/* device is already configured -
3408a088d1d7SLinus Lüssing 					 * but resend MLD reports, we might
3409a088d1d7SLinus Lüssing 					 * have roamed and need to update
3410a088d1d7SLinus Lüssing 					 * multicast snooping switches
3411a088d1d7SLinus Lüssing 					 */
3412a088d1d7SLinus Lüssing 					ipv6_mc_up(idev);
34133c21edbdSYOSHIFUJI Hideaki 					break;
3414a088d1d7SLinus Lüssing 				}
34153c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
34163c21edbdSYOSHIFUJI Hideaki 			}
34173c21edbdSYOSHIFUJI Hideaki 
3418f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
34193c21edbdSYOSHIFUJI Hideaki 				dev->name);
34203c21edbdSYOSHIFUJI Hideaki 
3421c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
34223c21edbdSYOSHIFUJI Hideaki 		}
34233c21edbdSYOSHIFUJI Hideaki 
34241da177e4SLinus Torvalds 		switch (dev->type) {
342507a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
34261da177e4SLinus Torvalds 		case ARPHRD_SIT:
34271da177e4SLinus Torvalds 			addrconf_sit_config(dev);
34281da177e4SLinus Torvalds 			break;
34290be669bbSJoerg Roedel #endif
343007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3431aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
3432aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
3433aee80b54Sstephen hemminger 			break;
3434aee80b54Sstephen hemminger #endif
34351da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
34361da177e4SLinus Torvalds 			init_loopback(dev);
34371da177e4SLinus Torvalds 			break;
34381da177e4SLinus Torvalds 
34391da177e4SLinus Torvalds 		default:
34401da177e4SLinus Torvalds 			addrconf_dev_config(dev);
34411da177e4SLinus Torvalds 			break;
34423ff50b79SStephen Hemminger 		}
3443bcdd553fSStephen Hemminger 
3444a317a2f1SWANG Cong 		if (!IS_ERR_OR_NULL(idev)) {
3445c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
3446c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
3447c5e33bddSYOSHIFUJI Hideaki 
3448bcdd553fSStephen Hemminger 			/*
3449bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
3450bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
3451bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
34521da177e4SLinus Torvalds 			 */
3453bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
3454bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
34551da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
34561da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
34571da177e4SLinus Torvalds 			}
34581da177e4SLinus Torvalds 			idev->tstamp = jiffies;
34591da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
3460bcdd553fSStephen Hemminger 
3461bcdd553fSStephen Hemminger 			/*
3462bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
3463bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
34641da177e4SLinus Torvalds 			 */
34651da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
3466bcdd553fSStephen Hemminger 				addrconf_ifdown(dev, 1);
34671da177e4SLinus Torvalds 		}
34681da177e4SLinus Torvalds 		break;
34691da177e4SLinus Torvalds 
34701da177e4SLinus Torvalds 	case NETDEV_DOWN:
34711da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
34721da177e4SLinus Torvalds 		/*
34731da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
34741da177e4SLinus Torvalds 		 */
34751da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
34761da177e4SLinus Torvalds 		break;
34773c21edbdSYOSHIFUJI Hideaki 
34781da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
34791da177e4SLinus Torvalds 		if (idev) {
34805632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
3481408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
3482a317a2f1SWANG Cong 			err = addrconf_sysctl_register(idev);
3483b217d616SHerbert Xu 			if (err)
3484b217d616SHerbert Xu 				return notifier_from_errno(err);
3485a317a2f1SWANG Cong 			err = snmp6_register_dev(idev);
3486a317a2f1SWANG Cong 			if (err) {
3487a317a2f1SWANG Cong 				addrconf_sysctl_unregister(idev);
3488a317a2f1SWANG Cong 				return notifier_from_errno(err);
3489a317a2f1SWANG Cong 			}
34905632c515SStephen Hemminger 		}
34911da177e4SLinus Torvalds 		break;
3492bcdd553fSStephen Hemminger 
349393d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
349493d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
34953ef0952cSAndrew Lunn 		if (idev)
349693d9b7d7SJiri Pirko 			addrconf_type_change(dev, event);
349775c78500SMoni Shoua 		break;
34984f7f34eaSDavid Ahern 
34994f7f34eaSDavid Ahern 	case NETDEV_CHANGEUPPER:
35004f7f34eaSDavid Ahern 		info = ptr;
35014f7f34eaSDavid Ahern 
35024f7f34eaSDavid Ahern 		/* flush all routes if dev is linked to or unlinked from
35034f7f34eaSDavid Ahern 		 * an L3 master device (e.g., VRF)
35044f7f34eaSDavid Ahern 		 */
35054f7f34eaSDavid Ahern 		if (info->upper_dev && netif_is_l3_master(info->upper_dev))
35064f7f34eaSDavid Ahern 			addrconf_ifdown(dev, 0);
35073ff50b79SStephen Hemminger 	}
35081da177e4SLinus Torvalds 
35091da177e4SLinus Torvalds 	return NOTIFY_OK;
35101da177e4SLinus Torvalds }
35111da177e4SLinus Torvalds 
35121da177e4SLinus Torvalds /*
35131da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
35141da177e4SLinus Torvalds  */
35151da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
35161da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
35171da177e4SLinus Torvalds };
35181da177e4SLinus Torvalds 
351993d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
352075c78500SMoni Shoua {
352175c78500SMoni Shoua 	struct inet6_dev *idev;
352275c78500SMoni Shoua 	ASSERT_RTNL();
352375c78500SMoni Shoua 
352475c78500SMoni Shoua 	idev = __in6_dev_get(dev);
352575c78500SMoni Shoua 
352693d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
352775c78500SMoni Shoua 		ipv6_mc_remap(idev);
352893d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
352975c78500SMoni Shoua 		ipv6_mc_unmap(idev);
353075c78500SMoni Shoua }
353175c78500SMoni Shoua 
353270af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr)
353370af921dSDavid Ahern {
353470af921dSDavid Ahern 	return ipv6_addr_type(addr) &
353570af921dSDavid Ahern 		(IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
353670af921dSDavid Ahern }
353770af921dSDavid Ahern 
35381da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
35391da177e4SLinus Torvalds {
3540c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
3541502a2ffdSstephen hemminger 	struct inet6_dev *idev;
3542f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifa, *tmp;
3543f1705ec1SDavid Ahern 	struct list_head del_list;
3544f1705ec1SDavid Ahern 	int _keep_addr;
3545f1705ec1SDavid Ahern 	bool keep_addr;
354673a8bd74SDavid S. Miller 	int state, i;
35471da177e4SLinus Torvalds 
35481da177e4SLinus Torvalds 	ASSERT_RTNL();
35491da177e4SLinus Torvalds 
3550f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
355173a8bd74SDavid S. Miller 	neigh_ifdown(&nd_tbl, dev);
35521da177e4SLinus Torvalds 
35531da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
355463159f29SIan Morris 	if (!idev)
35551da177e4SLinus Torvalds 		return -ENODEV;
35561da177e4SLinus Torvalds 
3557bcdd553fSStephen Hemminger 	/*
3558bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3559bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
35601da177e4SLinus Torvalds 	 */
3561439e2385SDenis V. Lunev 	if (how) {
35621da177e4SLinus Torvalds 		idev->dead = 1;
35638814c4b5SYOSHIFUJI Hideaki 
35648814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3565a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
35661da177e4SLinus Torvalds 
35671da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
35681da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
35691da177e4SLinus Torvalds 
35701da177e4SLinus Torvalds 	}
35711da177e4SLinus Torvalds 
3572f1705ec1SDavid Ahern 	/* aggregate the system setting and interface setting */
3573f1705ec1SDavid Ahern 	_keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3574f1705ec1SDavid Ahern 	if (!_keep_addr)
3575f1705ec1SDavid Ahern 		_keep_addr = idev->cnf.keep_addr_on_down;
3576f1705ec1SDavid Ahern 
3577f1705ec1SDavid Ahern 	/* combine the user config with event to determine if permanent
3578f1705ec1SDavid Ahern 	 * addresses are to be removed from address hash table
3579f1705ec1SDavid Ahern 	 */
3580bc561632SMike Manning 	keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
3581f1705ec1SDavid Ahern 
358273a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
358373a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
358473a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
358573a8bd74SDavid S. Miller 
358673a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
358773a8bd74SDavid S. Miller restart:
3588b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
358973a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
3590c15b1ccaSHannes Frederic Sowa 				addrconf_del_dad_work(ifa);
3591f1705ec1SDavid Ahern 				/* combined flag + permanent flag decide if
3592f1705ec1SDavid Ahern 				 * address is retained on a down event
3593f1705ec1SDavid Ahern 				 */
3594f1705ec1SDavid Ahern 				if (!keep_addr ||
359570af921dSDavid Ahern 				    !(ifa->flags & IFA_F_PERMANENT) ||
359670af921dSDavid Ahern 				    addr_is_local(&ifa->addr)) {
3597f1705ec1SDavid Ahern 					hlist_del_init_rcu(&ifa->addr_lst);
359873a8bd74SDavid S. Miller 					goto restart;
359973a8bd74SDavid S. Miller 				}
360073a8bd74SDavid S. Miller 			}
3601f1705ec1SDavid Ahern 		}
360273a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
360373a8bd74SDavid S. Miller 	}
360473a8bd74SDavid S. Miller 
36051da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
36061da177e4SLinus Torvalds 
3607b7b1bfceSHannes Frederic Sowa 	addrconf_del_rs_timer(idev);
3608b7b1bfceSHannes Frederic Sowa 
3609bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3610439e2385SDenis V. Lunev 	if (!how)
36113c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
36121da177e4SLinus Torvalds 
3613bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3614372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3615372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3616372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3617372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
36181da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
36191da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
36201da177e4SLinus Torvalds 
36211da177e4SLinus Torvalds 		if (ifa->ifpub) {
36221da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
36231da177e4SLinus Torvalds 			ifa->ifpub = NULL;
36241da177e4SLinus Torvalds 		}
36251da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
36261da177e4SLinus Torvalds 		in6_ifa_put(ifa);
36271da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
36281da177e4SLinus Torvalds 	}
36298f37ada5Sstephen hemminger 
3630f1705ec1SDavid Ahern 	/* re-combine the user config with event to determine if permanent
3631f1705ec1SDavid Ahern 	 * addresses are to be removed from the interface list
3632f1705ec1SDavid Ahern 	 */
3633bc561632SMike Manning 	keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
3634f1705ec1SDavid Ahern 
3635f1705ec1SDavid Ahern 	INIT_LIST_HEAD(&del_list);
3636f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
363738bd10c4SDavid Ahern 		struct rt6_info *rt = NULL;
363838bd10c4SDavid Ahern 
3639c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifa);
364084e8b803Sstephen hemminger 
36414c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
36428e8e676dSHannes Frederic Sowa 		spin_lock_bh(&ifa->lock);
3643f1705ec1SDavid Ahern 
364470af921dSDavid Ahern 		if (keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
364570af921dSDavid Ahern 		    !addr_is_local(&ifa->addr)) {
3646f1705ec1SDavid Ahern 			/* set state to skip the notifier below */
3647f1705ec1SDavid Ahern 			state = INET6_IFADDR_STATE_DEAD;
3648f1705ec1SDavid Ahern 			ifa->state = 0;
3649f1705ec1SDavid Ahern 			if (!(ifa->flags & IFA_F_NODAD))
3650f1705ec1SDavid Ahern 				ifa->flags |= IFA_F_TENTATIVE;
365138bd10c4SDavid Ahern 
365238bd10c4SDavid Ahern 			rt = ifa->rt;
365338bd10c4SDavid Ahern 			ifa->rt = NULL;
3654f1705ec1SDavid Ahern 		} else {
36554c5ff6a6SHerbert Xu 			state = ifa->state;
36564c5ff6a6SHerbert Xu 			ifa->state = INET6_IFADDR_STATE_DEAD;
3657f1705ec1SDavid Ahern 
3658c882219aSWei Yongjun 			list_move(&ifa->if_list, &del_list);
3659f1705ec1SDavid Ahern 		}
3660f1705ec1SDavid Ahern 
36618e8e676dSHannes Frederic Sowa 		spin_unlock_bh(&ifa->lock);
36624c5ff6a6SHerbert Xu 
366338bd10c4SDavid Ahern 		if (rt)
366438bd10c4SDavid Ahern 			ip6_del_rt(rt);
366538bd10c4SDavid Ahern 
36669d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
36671da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
3668f88c91ddSCong Wang 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3669ea06f717SMike Manning 		} else {
3670ea06f717SMike Manning 			if (idev->cnf.forwarding)
3671ea06f717SMike Manning 				addrconf_leave_anycast(ifa);
3672ea06f717SMike Manning 			addrconf_leave_solict(ifa->idev, &ifa->addr);
36732de79570SLorenzo Colitti 		}
367473a8bd74SDavid S. Miller 
36751da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
36761da177e4SLinus Torvalds 	}
36778f37ada5Sstephen hemminger 
36781da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
36791da177e4SLinus Torvalds 
3680f1705ec1SDavid Ahern 	/* now clean up addresses to be removed */
3681f1705ec1SDavid Ahern 	while (!list_empty(&del_list)) {
3682f1705ec1SDavid Ahern 		ifa = list_first_entry(&del_list,
3683f1705ec1SDavid Ahern 				       struct inet6_ifaddr, if_list);
3684f1705ec1SDavid Ahern 		list_del(&ifa->if_list);
3685f1705ec1SDavid Ahern 
3686f1705ec1SDavid Ahern 		in6_ifa_put(ifa);
3687f1705ec1SDavid Ahern 	}
3688f1705ec1SDavid Ahern 
3689381f4dcaSSabrina Dubroca 	/* Step 5: Discard anycast and multicast list */
3690381f4dcaSSabrina Dubroca 	if (how) {
3691381f4dcaSSabrina Dubroca 		ipv6_ac_destroy_dev(idev);
36921da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
3693381f4dcaSSabrina Dubroca 	} else {
36941da177e4SLinus Torvalds 		ipv6_mc_down(idev);
3695381f4dcaSSabrina Dubroca 	}
36961da177e4SLinus Torvalds 
36971da177e4SLinus Torvalds 	idev->tstamp = jiffies;
36981da177e4SLinus Torvalds 
3699bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3700439e2385SDenis V. Lunev 	if (how) {
3701408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
37021da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
37031da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
37041da177e4SLinus Torvalds 		in6_dev_put(idev);
37051da177e4SLinus Torvalds 	}
37061da177e4SLinus Torvalds 	return 0;
37071da177e4SLinus Torvalds }
37081da177e4SLinus Torvalds 
37091da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
37101da177e4SLinus Torvalds {
3711b7b1bfceSHannes Frederic Sowa 	struct inet6_dev *idev = (struct inet6_dev *)data;
3712caf92bc4SCong Wang 	struct net_device *dev = idev->dev;
3713b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
37141da177e4SLinus Torvalds 
3715b7b1bfceSHannes Frederic Sowa 	write_lock(&idev->lock);
37165b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
37171da177e4SLinus Torvalds 		goto out;
37181da177e4SLinus Torvalds 
37199ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
37201da177e4SLinus Torvalds 		goto out;
37215b2a1953Sstephen hemminger 
37225b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
37235b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
37245b2a1953Sstephen hemminger 		goto out;
37251da177e4SLinus Torvalds 
3726bd11f074SMaciej Żenczykowski 	if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3727caf92bc4SCong Wang 		write_unlock(&idev->lock);
3728caf92bc4SCong Wang 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3729caf92bc4SCong Wang 			ndisc_send_rs(dev, &lladdr,
3730b7b1bfceSHannes Frederic Sowa 				      &in6addr_linklocal_allrouters);
3731b7b1bfceSHannes Frederic Sowa 		else
3732caf92bc4SCong Wang 			goto put;
3733b7b1bfceSHannes Frederic Sowa 
3734caf92bc4SCong Wang 		write_lock(&idev->lock);
3735bd11f074SMaciej Żenczykowski 		idev->rs_interval = rfc3315_s14_backoff_update(
3736bd11f074SMaciej Żenczykowski 			idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
37371da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
3738b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3739b7b1bfceSHannes Frederic Sowa 					     idev->cnf.rtr_solicits) ?
37405b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_delay :
3741bd11f074SMaciej Żenczykowski 				      idev->rs_interval);
37421da177e4SLinus Torvalds 	} else {
37431da177e4SLinus Torvalds 		/*
37441da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
37451da177e4SLinus Torvalds 		 * assumption any longer.
37461da177e4SLinus Torvalds 		 */
374791df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
37481da177e4SLinus Torvalds 	}
37491da177e4SLinus Torvalds 
37501da177e4SLinus Torvalds out:
3751b7b1bfceSHannes Frederic Sowa 	write_unlock(&idev->lock);
3752caf92bc4SCong Wang put:
3753b7b1bfceSHannes Frederic Sowa 	in6_dev_put(idev);
37541da177e4SLinus Torvalds }
37551da177e4SLinus Torvalds 
37561da177e4SLinus Torvalds /*
37571da177e4SLinus Torvalds  *	Duplicate Address Detection
37581da177e4SLinus Torvalds  */
37593c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
37603c21edbdSYOSHIFUJI Hideaki {
37613c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
37623c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
3763adc176c5SErik Nordmark 	u64 nonce;
37643c21edbdSYOSHIFUJI Hideaki 
376595c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
376695c385b4SNeil Horman 		rand_num = 0;
376795c385b4SNeil Horman 	else
376863862b5bSAruna-Hewapathirane 		rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
376995c385b4SNeil Horman 
3770adc176c5SErik Nordmark 	nonce = 0;
3771adc176c5SErik Nordmark 	if (idev->cnf.enhanced_dad ||
3772adc176c5SErik Nordmark 	    dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3773adc176c5SErik Nordmark 		do
3774adc176c5SErik Nordmark 			get_random_bytes(&nonce, 6);
3775adc176c5SErik Nordmark 		while (nonce == 0);
3776adc176c5SErik Nordmark 	}
3777adc176c5SErik Nordmark 	ifp->dad_nonce = nonce;
3778b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes = idev->cnf.dad_transmits;
3779c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, rand_num);
37803c21edbdSYOSHIFUJI Hideaki }
37813c21edbdSYOSHIFUJI Hideaki 
3782c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
37831da177e4SLinus Torvalds {
37841da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
37851da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3786764d3be6SPaolo Abeni 	bool bump_id, notify = false;
37871da177e4SLinus Torvalds 
37881da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
37891da177e4SLinus Torvalds 
379063862b5bSAruna-Hewapathirane 	prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
37911da177e4SLinus Torvalds 
37921da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3793622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3794e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
37951da177e4SLinus Torvalds 		goto out;
37961da177e4SLinus Torvalds 
37971da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
37981b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
379955ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
380055ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3801764d3be6SPaolo Abeni 		bump_id = ifp->flags & IFA_F_TENTATIVE;
3802cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
380321809fafSstephen hemminger 		spin_unlock(&ifp->lock);
38041da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
38051da177e4SLinus Torvalds 
3806764d3be6SPaolo Abeni 		addrconf_dad_completed(ifp, bump_id);
38071da177e4SLinus Torvalds 		return;
38081da177e4SLinus Torvalds 	}
38091da177e4SLinus Torvalds 
38106732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
381121809fafSstephen hemminger 		spin_unlock(&ifp->lock);
38126732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
38133c21edbdSYOSHIFUJI Hideaki 		/*
3814590a9887SMasatake YAMATO 		 * If the device is not ready:
38153c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
38163c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
38173c21edbdSYOSHIFUJI Hideaki 		 */
38183c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
3819cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
38206732badeSYOSHIFUJI Hideaki 		return;
38213c21edbdSYOSHIFUJI Hideaki 	}
382295c385b4SNeil Horman 
382395c385b4SNeil Horman 	/*
382495c385b4SNeil Horman 	 * Optimistic nodes can start receiving
382595c385b4SNeil Horman 	 * Frames right away
382695c385b4SNeil Horman 	 */
38277fd2561eSErik Kline 	if (ifp->flags & IFA_F_OPTIMISTIC) {
382895c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
38297fd2561eSErik Kline 		if (ipv6_use_optimistic_addr(idev)) {
38307fd2561eSErik Kline 			/* Because optimistic nodes can use this address,
38317fd2561eSErik Kline 			 * notify listeners. If DAD fails, RTM_DELADDR is sent.
38327fd2561eSErik Kline 			 */
383316186a82Ssubashab@codeaurora.org 			notify = true;
38347fd2561eSErik Kline 		}
38357fd2561eSErik Kline 	}
383695c385b4SNeil Horman 
38376732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
38381da177e4SLinus Torvalds out:
3839622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
38401da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
384116186a82Ssubashab@codeaurora.org 	if (notify)
384216186a82Ssubashab@codeaurora.org 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
38431da177e4SLinus Torvalds }
38441da177e4SLinus Torvalds 
3845c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp)
38461da177e4SLinus Torvalds {
3847c15b1ccaSHannes Frederic Sowa 	bool begin_dad = false;
3848c15b1ccaSHannes Frederic Sowa 
38498e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3850c15b1ccaSHannes Frederic Sowa 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3851c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_PREDAD;
3852c15b1ccaSHannes Frederic Sowa 		begin_dad = true;
3853c15b1ccaSHannes Frederic Sowa 	}
38548e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3855c15b1ccaSHannes Frederic Sowa 
3856c15b1ccaSHannes Frederic Sowa 	if (begin_dad)
3857c15b1ccaSHannes Frederic Sowa 		addrconf_mod_dad_work(ifp, 0);
3858c15b1ccaSHannes Frederic Sowa }
3859c15b1ccaSHannes Frederic Sowa 
3860c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w)
3861c15b1ccaSHannes Frederic Sowa {
3862c15b1ccaSHannes Frederic Sowa 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3863c15b1ccaSHannes Frederic Sowa 						struct inet6_ifaddr,
3864c15b1ccaSHannes Frederic Sowa 						dad_work);
38651da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
3866764d3be6SPaolo Abeni 	bool bump_id, disable_ipv6 = false;
38671da177e4SLinus Torvalds 	struct in6_addr mcaddr;
38681da177e4SLinus Torvalds 
3869c15b1ccaSHannes Frederic Sowa 	enum {
3870c15b1ccaSHannes Frederic Sowa 		DAD_PROCESS,
3871c15b1ccaSHannes Frederic Sowa 		DAD_BEGIN,
3872c15b1ccaSHannes Frederic Sowa 		DAD_ABORT,
3873c15b1ccaSHannes Frederic Sowa 	} action = DAD_PROCESS;
3874c15b1ccaSHannes Frederic Sowa 
3875c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
3876c15b1ccaSHannes Frederic Sowa 
38778e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3878c15b1ccaSHannes Frederic Sowa 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3879c15b1ccaSHannes Frederic Sowa 		action = DAD_BEGIN;
3880c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_DAD;
3881c15b1ccaSHannes Frederic Sowa 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3882c15b1ccaSHannes Frederic Sowa 		action = DAD_ABORT;
3883c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
388485b51b12SMike Manning 
388585b51b12SMike Manning 		if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
388685b51b12SMike Manning 		    !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
388785b51b12SMike Manning 			struct in6_addr addr;
388885b51b12SMike Manning 
388985b51b12SMike Manning 			addr.s6_addr32[0] = htonl(0xfe800000);
389085b51b12SMike Manning 			addr.s6_addr32[1] = 0;
389185b51b12SMike Manning 
389285b51b12SMike Manning 			if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
389385b51b12SMike Manning 			    ipv6_addr_equal(&ifp->addr, &addr)) {
389485b51b12SMike Manning 				/* DAD failed for link-local based on MAC */
389585b51b12SMike Manning 				idev->cnf.disable_ipv6 = 1;
389685b51b12SMike Manning 
389785b51b12SMike Manning 				pr_info("%s: IPv6 being disabled!\n",
389885b51b12SMike Manning 					ifp->idev->dev->name);
389985b51b12SMike Manning 				disable_ipv6 = true;
390085b51b12SMike Manning 			}
390185b51b12SMike Manning 		}
3902c15b1ccaSHannes Frederic Sowa 	}
39038e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3904c15b1ccaSHannes Frederic Sowa 
3905c15b1ccaSHannes Frederic Sowa 	if (action == DAD_BEGIN) {
3906c15b1ccaSHannes Frederic Sowa 		addrconf_dad_begin(ifp);
3907c15b1ccaSHannes Frederic Sowa 		goto out;
3908c15b1ccaSHannes Frederic Sowa 	} else if (action == DAD_ABORT) {
3909751eb6b6SWei Yongjun 		in6_ifa_hold(ifp);
3910c15b1ccaSHannes Frederic Sowa 		addrconf_dad_stop(ifp, 1);
391185b51b12SMike Manning 		if (disable_ipv6)
391285b51b12SMike Manning 			addrconf_ifdown(idev->dev, 0);
3913c15b1ccaSHannes Frederic Sowa 		goto out;
3914c15b1ccaSHannes Frederic Sowa 	}
3915c15b1ccaSHannes Frederic Sowa 
3916b7b1bfceSHannes Frederic Sowa 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
3917f2344a13SHerbert Xu 		goto out;
3918f2344a13SHerbert Xu 
3919c15b1ccaSHannes Frederic Sowa 	write_lock_bh(&idev->lock);
3920122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
3921c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
39221da177e4SLinus Torvalds 		goto out;
39231da177e4SLinus Torvalds 	}
392421809fafSstephen hemminger 
392521809fafSstephen hemminger 	spin_lock(&ifp->lock);
3926622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3927622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
3928c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
3929622ccdf1SHerbert Xu 		goto out;
3930622ccdf1SHerbert Xu 	}
3931622ccdf1SHerbert Xu 
3932b7b1bfceSHannes Frederic Sowa 	if (ifp->dad_probes == 0) {
39331da177e4SLinus Torvalds 		/*
39341da177e4SLinus Torvalds 		 * DAD was successful
39351da177e4SLinus Torvalds 		 */
39361da177e4SLinus Torvalds 
3937764d3be6SPaolo Abeni 		bump_id = ifp->flags & IFA_F_TENTATIVE;
3938cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
393921809fafSstephen hemminger 		spin_unlock(&ifp->lock);
3940c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
39411da177e4SLinus Torvalds 
3942764d3be6SPaolo Abeni 		addrconf_dad_completed(ifp, bump_id);
39431da177e4SLinus Torvalds 
39441da177e4SLinus Torvalds 		goto out;
39451da177e4SLinus Torvalds 	}
39461da177e4SLinus Torvalds 
3947b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes--;
3948c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp,
39491f9248e5SJiri Pirko 			      NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
395021809fafSstephen hemminger 	spin_unlock(&ifp->lock);
3951c15b1ccaSHannes Frederic Sowa 	write_unlock_bh(&idev->lock);
39521da177e4SLinus Torvalds 
39531da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
39541da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3955adc176c5SErik Nordmark 	ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
3956adc176c5SErik Nordmark 		      ifp->dad_nonce);
39571da177e4SLinus Torvalds out:
39581da177e4SLinus Torvalds 	in6_ifa_put(ifp);
3959c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
39601da177e4SLinus Torvalds }
39611da177e4SLinus Torvalds 
396211ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */
396311ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
396411ffff75SHannes Frederic Sowa {
396511ffff75SHannes Frederic Sowa 	struct inet6_ifaddr *ifpiter;
396611ffff75SHannes Frederic Sowa 	struct inet6_dev *idev = ifp->idev;
396711ffff75SHannes Frederic Sowa 
3968602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3969602582caSHannes Frederic Sowa 		if (ifpiter->scope > IFA_LINK)
3970602582caSHannes Frederic Sowa 			break;
397111ffff75SHannes Frederic Sowa 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
397211ffff75SHannes Frederic Sowa 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
397311ffff75SHannes Frederic Sowa 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
397411ffff75SHannes Frederic Sowa 		    IFA_F_PERMANENT)
397511ffff75SHannes Frederic Sowa 			return false;
397611ffff75SHannes Frederic Sowa 	}
397711ffff75SHannes Frederic Sowa 	return true;
397811ffff75SHannes Frederic Sowa }
397911ffff75SHannes Frederic Sowa 
3980764d3be6SPaolo Abeni static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
39811da177e4SLinus Torvalds {
39821da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
3983b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
3984b173ee48SHannes Frederic Sowa 	bool send_rs, send_mld;
3985b7b1bfceSHannes Frederic Sowa 
3986c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
39871da177e4SLinus Torvalds 
39881da177e4SLinus Torvalds 	/*
39891da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
39901da177e4SLinus Torvalds 	 */
39911da177e4SLinus Torvalds 
39921da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
39931da177e4SLinus Torvalds 
3994026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
3995026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
39961da177e4SLinus Torvalds 	 */
39971da177e4SLinus Torvalds 
39981ec047ebSHannes Frederic Sowa 	read_lock_bh(&ifp->idev->lock);
399911ffff75SHannes Frederic Sowa 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4000b173ee48SHannes Frederic Sowa 	send_rs = send_mld &&
4001b173ee48SHannes Frederic Sowa 		  ipv6_accept_ra(ifp->idev) &&
4002bd11f074SMaciej Żenczykowski 		  ifp->idev->cnf.rtr_solicits != 0 &&
4003b173ee48SHannes Frederic Sowa 		  (dev->flags&IFF_LOOPBACK) == 0;
40041ec047ebSHannes Frederic Sowa 	read_unlock_bh(&ifp->idev->lock);
40051ec047ebSHannes Frederic Sowa 
4006b173ee48SHannes Frederic Sowa 	/* While dad is in progress mld report's source address is in6_addrany.
4007b173ee48SHannes Frederic Sowa 	 * Resend with proper ll now.
4008b173ee48SHannes Frederic Sowa 	 */
4009b173ee48SHannes Frederic Sowa 	if (send_mld)
4010b173ee48SHannes Frederic Sowa 		ipv6_mc_dad_complete(ifp->idev);
4011b173ee48SHannes Frederic Sowa 
40121ec047ebSHannes Frederic Sowa 	if (send_rs) {
40131da177e4SLinus Torvalds 		/*
40141da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
40151da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
40161da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
40171da177e4SLinus Torvalds 		 */
40181ec047ebSHannes Frederic Sowa 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4019b7b1bfceSHannes Frederic Sowa 			return;
40201ec047ebSHannes Frederic Sowa 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
40211da177e4SLinus Torvalds 
4022b7b1bfceSHannes Frederic Sowa 		write_lock_bh(&ifp->idev->lock);
4023b7b1bfceSHannes Frederic Sowa 		spin_lock(&ifp->lock);
4024bd11f074SMaciej Żenczykowski 		ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4025bd11f074SMaciej Żenczykowski 			ifp->idev->cnf.rtr_solicit_interval);
4026b7b1bfceSHannes Frederic Sowa 		ifp->idev->rs_probes = 1;
40271da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
4028bd11f074SMaciej Żenczykowski 		addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4029b7b1bfceSHannes Frederic Sowa 		spin_unlock(&ifp->lock);
4030b7b1bfceSHannes Frederic Sowa 		write_unlock_bh(&ifp->idev->lock);
40311da177e4SLinus Torvalds 	}
4032764d3be6SPaolo Abeni 
4033764d3be6SPaolo Abeni 	if (bump_id)
4034764d3be6SPaolo Abeni 		rt_genid_bump_ipv6(dev_net(dev));
4035a11a7f71SMarcus Huewe 
4036a11a7f71SMarcus Huewe 	/* Make sure that a new temporary address will be created
4037a11a7f71SMarcus Huewe 	 * before this temporary address becomes deprecated.
4038a11a7f71SMarcus Huewe 	 */
4039a11a7f71SMarcus Huewe 	if (ifp->flags & IFA_F_TEMPORARY)
4040a11a7f71SMarcus Huewe 		addrconf_verify_rtnl();
40411da177e4SLinus Torvalds }
40421da177e4SLinus Torvalds 
4043e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev)
4044e21e8467SStephen Hemminger {
4045c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
4046c5e33bddSYOSHIFUJI Hideaki 
4047c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
4048502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
404921809fafSstephen hemminger 		spin_lock(&ifp->lock);
4050f2344a13SHerbert Xu 		if (ifp->flags & IFA_F_TENTATIVE &&
4051f2344a13SHerbert Xu 		    ifp->state == INET6_IFADDR_STATE_DAD)
4052c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
4053f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
4054c5e33bddSYOSHIFUJI Hideaki 	}
4055c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
4056c5e33bddSYOSHIFUJI Hideaki }
4057c5e33bddSYOSHIFUJI Hideaki 
40581da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
40591da177e4SLinus Torvalds struct if6_iter_state {
40603c40090aSDaniel Lezcano 	struct seq_net_private p;
40611da177e4SLinus Torvalds 	int bucket;
40621d578303SMihai Maruseac 	int offset;
40631da177e4SLinus Torvalds };
40641da177e4SLinus Torvalds 
40651d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
40661da177e4SLinus Torvalds {
40671da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
40681da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
40691218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
40701d578303SMihai Maruseac 	int p = 0;
40711da177e4SLinus Torvalds 
40721d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
40731d578303SMihai Maruseac 	if (pos == 0) {
40741d578303SMihai Maruseac 		state->bucket = 0;
40751d578303SMihai Maruseac 		state->offset = 0;
40761d578303SMihai Maruseac 	}
40771d578303SMihai Maruseac 
40781d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4079b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
40801d578303SMihai Maruseac 					 addr_lst) {
40819f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
40829f0d3c27SEric Dumazet 				continue;
40831d578303SMihai Maruseac 			/* sync with offset */
40841d578303SMihai Maruseac 			if (p < state->offset) {
40851d578303SMihai Maruseac 				p++;
40861d578303SMihai Maruseac 				continue;
40871d578303SMihai Maruseac 			}
40881d578303SMihai Maruseac 			state->offset++;
40891da177e4SLinus Torvalds 			return ifa;
40901da177e4SLinus Torvalds 		}
40911d578303SMihai Maruseac 
40921d578303SMihai Maruseac 		/* prepare for next bucket */
40931d578303SMihai Maruseac 		state->offset = 0;
40941d578303SMihai Maruseac 		p = 0;
40951d578303SMihai Maruseac 	}
4096c2e21293Sstephen hemminger 	return NULL;
4097c2e21293Sstephen hemminger }
40981da177e4SLinus Torvalds 
4099c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4100c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
41011da177e4SLinus Torvalds {
41021da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
41031218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
41041da177e4SLinus Torvalds 
4105b67bfe0dSSasha Levin 	hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
41069f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
41079f0d3c27SEric Dumazet 			continue;
41081d578303SMihai Maruseac 		state->offset++;
41091da177e4SLinus Torvalds 		return ifa;
41101d578303SMihai Maruseac 	}
41111da177e4SLinus Torvalds 
4112c2e21293Sstephen hemminger 	while (++state->bucket < IN6_ADDR_HSIZE) {
41131d578303SMihai Maruseac 		state->offset = 0;
4114b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa,
4115c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
41169f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
41179f0d3c27SEric Dumazet 				continue;
41181d578303SMihai Maruseac 			state->offset++;
4119c2e21293Sstephen hemminger 			return ifa;
4120c2e21293Sstephen hemminger 		}
4121c2e21293Sstephen hemminger 	}
4122c2e21293Sstephen hemminger 
4123c2e21293Sstephen hemminger 	return NULL;
4124c2e21293Sstephen hemminger }
4125c2e21293Sstephen hemminger 
41261da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
41274f70eccaSEric Dumazet 	__acquires(rcu_bh)
41281da177e4SLinus Torvalds {
41295c578aedSstephen hemminger 	rcu_read_lock_bh();
41301d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
41311da177e4SLinus Torvalds }
41321da177e4SLinus Torvalds 
41331da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
41341da177e4SLinus Torvalds {
41351da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
41361da177e4SLinus Torvalds 
41371da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
41381da177e4SLinus Torvalds 	++*pos;
41391da177e4SLinus Torvalds 	return ifa;
41401da177e4SLinus Torvalds }
41411da177e4SLinus Torvalds 
41421da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
41434f70eccaSEric Dumazet 	__releases(rcu_bh)
41441da177e4SLinus Torvalds {
41455c578aedSstephen hemminger 	rcu_read_unlock_bh();
41461da177e4SLinus Torvalds }
41471da177e4SLinus Torvalds 
41481da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
41491da177e4SLinus Torvalds {
41501da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4151971a351cSJiri Pirko 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4152b071195dSHarvey Harrison 		   &ifp->addr,
41531da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
41541da177e4SLinus Torvalds 		   ifp->prefix_len,
41551da177e4SLinus Torvalds 		   ifp->scope,
4156971a351cSJiri Pirko 		   (u8) ifp->flags,
41571da177e4SLinus Torvalds 		   ifp->idev->dev->name);
41581da177e4SLinus Torvalds 	return 0;
41591da177e4SLinus Torvalds }
41601da177e4SLinus Torvalds 
416156b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
41621da177e4SLinus Torvalds 	.start	= if6_seq_start,
41631da177e4SLinus Torvalds 	.next	= if6_seq_next,
41641da177e4SLinus Torvalds 	.show	= if6_seq_show,
41651da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
41661da177e4SLinus Torvalds };
41671da177e4SLinus Torvalds 
41681da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
41691da177e4SLinus Torvalds {
41703c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
4171cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
41721da177e4SLinus Torvalds }
41731da177e4SLinus Torvalds 
41749a32144eSArjan van de Ven static const struct file_operations if6_fops = {
41751da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
41761da177e4SLinus Torvalds 	.open		= if6_seq_open,
41771da177e4SLinus Torvalds 	.read		= seq_read,
41781da177e4SLinus Torvalds 	.llseek		= seq_lseek,
41793c40090aSDaniel Lezcano 	.release	= seq_release_net,
41801da177e4SLinus Torvalds };
41811da177e4SLinus Torvalds 
41822c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
41831da177e4SLinus Torvalds {
4184d4beaa66SGao feng 	if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
41851da177e4SLinus Torvalds 		return -ENOMEM;
41861da177e4SLinus Torvalds 	return 0;
41871da177e4SLinus Torvalds }
41881da177e4SLinus Torvalds 
41892c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
41903c40090aSDaniel Lezcano {
4191ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
41923c40090aSDaniel Lezcano }
41933c40090aSDaniel Lezcano 
41943c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
41953c40090aSDaniel Lezcano 	.init = if6_proc_net_init,
41963c40090aSDaniel Lezcano 	.exit = if6_proc_net_exit,
41973c40090aSDaniel Lezcano };
41983c40090aSDaniel Lezcano 
41993c40090aSDaniel Lezcano int __init if6_proc_init(void)
42003c40090aSDaniel Lezcano {
42013c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
42023c40090aSDaniel Lezcano }
42033c40090aSDaniel Lezcano 
42041da177e4SLinus Torvalds void if6_proc_exit(void)
42051da177e4SLinus Torvalds {
42063c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
42071da177e4SLinus Torvalds }
42081da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
42091da177e4SLinus Torvalds 
421007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
42113b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
4212b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
42133b9f9a1cSNoriaki TAKAMIYA {
42143b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
4215c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
4216ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
4217c2e21293Sstephen hemminger 
42185c578aedSstephen hemminger 	rcu_read_lock_bh();
4219b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
4220878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
4221389f6612SDaniel Lezcano 			continue;
4222caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
42233b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
42243b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
42253b9f9a1cSNoriaki TAKAMIYA 			break;
42263b9f9a1cSNoriaki TAKAMIYA 		}
42273b9f9a1cSNoriaki TAKAMIYA 	}
42285c578aedSstephen hemminger 	rcu_read_unlock_bh();
42293b9f9a1cSNoriaki TAKAMIYA 	return ret;
42303b9f9a1cSNoriaki TAKAMIYA }
42313b9f9a1cSNoriaki TAKAMIYA #endif
42323b9f9a1cSNoriaki TAKAMIYA 
42331da177e4SLinus Torvalds /*
42341da177e4SLinus Torvalds  *	Periodic address status verification
42351da177e4SLinus Torvalds  */
42361da177e4SLinus Torvalds 
4237c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void)
42381da177e4SLinus Torvalds {
4239b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
42401da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
42411da177e4SLinus Torvalds 	int i;
42421da177e4SLinus Torvalds 
4243c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4244c15b1ccaSHannes Frederic Sowa 
42455c578aedSstephen hemminger 	rcu_read_lock_bh();
42461da177e4SLinus Torvalds 	now = jiffies;
4247b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
42481da177e4SLinus Torvalds 
4249c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
42501da177e4SLinus Torvalds 
42511da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
42521da177e4SLinus Torvalds restart:
4253c15b1ccaSHannes Frederic Sowa 		hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
42541da177e4SLinus Torvalds 			unsigned long age;
42551da177e4SLinus Torvalds 
4256fad8da3eSYasushi Asano 			/* When setting preferred_lft to a value not zero or
4257fad8da3eSYasushi Asano 			 * infinity, while valid_lft is infinity
4258fad8da3eSYasushi Asano 			 * IFA_F_PERMANENT has a non-infinity life time.
4259fad8da3eSYasushi Asano 			 */
4260fad8da3eSYasushi Asano 			if ((ifp->flags & IFA_F_PERMANENT) &&
4261fad8da3eSYasushi Asano 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
42621da177e4SLinus Torvalds 				continue;
42631da177e4SLinus Torvalds 
42641da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
4265b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
4266b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
42671da177e4SLinus Torvalds 
42688f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
42698f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
42701da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
42711da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
42721da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
42731da177e4SLinus Torvalds 				goto restart;
42748f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
42758f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
42768f27ebb9SYOSHIFUJI Hideaki 				continue;
42771da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
4278a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
42791da177e4SLinus Torvalds 				int deprecate = 0;
42801da177e4SLinus Torvalds 
42811da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
42821da177e4SLinus Torvalds 					deprecate = 1;
42831da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
42841da177e4SLinus Torvalds 				}
42851da177e4SLinus Torvalds 
4286fad8da3eSYasushi Asano 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4287fad8da3eSYasushi Asano 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
42881da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
42891da177e4SLinus Torvalds 
42901da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
42911da177e4SLinus Torvalds 
42921da177e4SLinus Torvalds 				if (deprecate) {
42931da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
42941da177e4SLinus Torvalds 
42951da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
42961da177e4SLinus Torvalds 					in6_ifa_put(ifp);
42971da177e4SLinus Torvalds 					goto restart;
42981da177e4SLinus Torvalds 				}
42991da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
43001da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
430188949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
430288949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
43031f9248e5SJiri Pirko 					NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
430488949cf4Sstephen hemminger 
43051da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
43061da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
43071da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
43081da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
43091da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
43101da177e4SLinus Torvalds 						ifp->regen_count++;
43111da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
43121da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
43131da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
43145c578aedSstephen hemminger 
4315291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
4316291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
4317291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
43181da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
43191da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
43201da177e4SLinus Torvalds 						in6_ifa_put(ifp);
43211da177e4SLinus Torvalds 						goto restart;
43221da177e4SLinus Torvalds 					}
43231da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
43241da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
43251da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
43261da177e4SLinus Torvalds 			} else {
43271da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
43281da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
43291da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
43301da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
43311da177e4SLinus Torvalds 			}
43321da177e4SLinus Torvalds 		}
43331da177e4SLinus Torvalds 	}
43341da177e4SLinus Torvalds 
4335b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
4336b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
4337b2db7564SYOSHIFUJI Hideaki 
4338b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
4339b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4340b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
4341b2db7564SYOSHIFUJI Hideaki 
4342b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4343b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4344b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4345b2db7564SYOSHIFUJI Hideaki 
4346ba3542e1Sdingtianhong 	ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4347ba3542e1Sdingtianhong 	      now, next, next_sec, next_sched);
4348c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
43495c578aedSstephen hemminger 	rcu_read_unlock_bh();
43501da177e4SLinus Torvalds }
43511da177e4SLinus Torvalds 
4352c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w)
4353c15b1ccaSHannes Frederic Sowa {
4354c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
4355c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4356c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
4357c15b1ccaSHannes Frederic Sowa }
4358c15b1ccaSHannes Frederic Sowa 
4359c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void)
4360c15b1ccaSHannes Frederic Sowa {
4361c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4362c15b1ccaSHannes Frederic Sowa }
4363c15b1ccaSHannes Frederic Sowa 
4364caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4365caeaba79SNicolas Dichtel 				     struct in6_addr **peer_pfx)
4366461d8837SThomas Graf {
4367461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
4368461d8837SThomas Graf 
4369caeaba79SNicolas Dichtel 	*peer_pfx = NULL;
4370caeaba79SNicolas Dichtel 
4371461d8837SThomas Graf 	if (addr)
4372461d8837SThomas Graf 		pfx = nla_data(addr);
4373461d8837SThomas Graf 
4374461d8837SThomas Graf 	if (local) {
4375461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4376caeaba79SNicolas Dichtel 			*peer_pfx = pfx;
4377461d8837SThomas Graf 		pfx = nla_data(local);
4378461d8837SThomas Graf 	}
4379461d8837SThomas Graf 
4380461d8837SThomas Graf 	return pfx;
4381461d8837SThomas Graf }
4382461d8837SThomas Graf 
4383ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4384461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
4385461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
4386461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
4387479840ffSJiri Pirko 	[IFA_FLAGS]		= { .len = sizeof(u32) },
4388461d8837SThomas Graf };
4389461d8837SThomas Graf 
43901da177e4SLinus Torvalds static int
4391661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
43921da177e4SLinus Torvalds {
43933b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4394b933f716SThomas Graf 	struct ifaddrmsg *ifm;
4395b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4396caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
43976046d5b4SHeiner Kallweit 	u32 ifa_flags;
4398b933f716SThomas Graf 	int err;
43991da177e4SLinus Torvalds 
4400b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4401b933f716SThomas Graf 	if (err < 0)
4402b933f716SThomas Graf 		return err;
4403b933f716SThomas Graf 
4404b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
4405caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
440663159f29SIan Morris 	if (!pfx)
44071da177e4SLinus Torvalds 		return -EINVAL;
44081da177e4SLinus Torvalds 
44096046d5b4SHeiner Kallweit 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
44106046d5b4SHeiner Kallweit 
44116046d5b4SHeiner Kallweit 	/* We ignore other flags so far. */
44126046d5b4SHeiner Kallweit 	ifa_flags &= IFA_F_MANAGETEMPADDR;
44136046d5b4SHeiner Kallweit 
44146046d5b4SHeiner Kallweit 	return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
44156046d5b4SHeiner Kallweit 			      ifm->ifa_prefixlen);
44161da177e4SLinus Torvalds }
44171da177e4SLinus Torvalds 
4418479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
441955ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
4420081bba5bSNoriaki TAKAMIYA {
44216f704992SYOSHIFUJI Hideaki 	u32 flags;
44226f704992SYOSHIFUJI Hideaki 	clock_t expires;
44234bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
442453bd6749SJiri Pirko 	bool was_managetempaddr;
44255b84efecSThomas Haller 	bool had_prefixroute;
442646d48046SYOSHIFUJI Hideaki 
4427c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4428c15b1ccaSHannes Frederic Sowa 
4429081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
4430081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
4431081bba5bSNoriaki TAKAMIYA 
443253bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR &&
443353bd6749SJiri Pirko 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
443453bd6749SJiri Pirko 		return -EINVAL;
443553bd6749SJiri Pirko 
44364bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
44374bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
44384bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
44394bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
44406f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
44414bed72e4SYOSHIFUJI Hideaki 	} else {
44424bed72e4SYOSHIFUJI Hideaki 		expires = 0;
44434bed72e4SYOSHIFUJI Hideaki 		flags = 0;
44444bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
44456f704992SYOSHIFUJI Hideaki 	}
4446081bba5bSNoriaki TAKAMIYA 
44474bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
44484bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
44494bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
445055ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
44514bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
44524bed72e4SYOSHIFUJI Hideaki 	}
4453081bba5bSNoriaki TAKAMIYA 
4454081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
445553bd6749SJiri Pirko 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
44565b84efecSThomas Haller 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
44575b84efecSThomas Haller 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
445853bd6749SJiri Pirko 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4459761aac73SThomas Haller 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4460761aac73SThomas Haller 			IFA_F_NOPREFIXROUTE);
446153bd6749SJiri Pirko 	ifp->flags |= ifa_flags;
4462081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
4463081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
4464081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
4465081bba5bSNoriaki TAKAMIYA 
4466081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
4467081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
4468081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
4469081bba5bSNoriaki TAKAMIYA 
4470761aac73SThomas Haller 	if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
447146d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
44726f704992SYOSHIFUJI Hideaki 				      expires, flags);
44735b84efecSThomas Haller 	} else if (had_prefixroute) {
44745b84efecSThomas Haller 		enum cleanup_prefix_rt_t action;
44755b84efecSThomas Haller 		unsigned long rt_expires;
44765b84efecSThomas Haller 
44775b84efecSThomas Haller 		write_lock_bh(&ifp->idev->lock);
44785b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &rt_expires);
44795b84efecSThomas Haller 		write_unlock_bh(&ifp->idev->lock);
44805b84efecSThomas Haller 
44815b84efecSThomas Haller 		if (action != CLEANUP_PREFIX_RT_NOP) {
44825b84efecSThomas Haller 			cleanup_prefix_route(ifp, rt_expires,
44835b84efecSThomas Haller 				action == CLEANUP_PREFIX_RT_DEL);
44845b84efecSThomas Haller 		}
4485761aac73SThomas Haller 	}
448653bd6749SJiri Pirko 
448753bd6749SJiri Pirko 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
448853bd6749SJiri Pirko 		if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
448953bd6749SJiri Pirko 			valid_lft = prefered_lft = 0;
449053bd6749SJiri Pirko 		manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
449153bd6749SJiri Pirko 				 !was_managetempaddr, jiffies);
449253bd6749SJiri Pirko 	}
449353bd6749SJiri Pirko 
4494c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4495081bba5bSNoriaki TAKAMIYA 
4496081bba5bSNoriaki TAKAMIYA 	return 0;
4497081bba5bSNoriaki TAKAMIYA }
4498081bba5bSNoriaki TAKAMIYA 
4499081bba5bSNoriaki TAKAMIYA static int
4500661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
45011da177e4SLinus Torvalds {
45023b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4503461d8837SThomas Graf 	struct ifaddrmsg *ifm;
4504461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4505caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
45067198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
45077198f8ceSThomas Graf 	struct net_device *dev;
450855ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4509479840ffSJiri Pirko 	u32 ifa_flags;
4510461d8837SThomas Graf 	int err;
45111da177e4SLinus Torvalds 
4512461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4513461d8837SThomas Graf 	if (err < 0)
4514461d8837SThomas Graf 		return err;
4515461d8837SThomas Graf 
4516461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
4517caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
451863159f29SIan Morris 	if (!pfx)
45191da177e4SLinus Torvalds 		return -EINVAL;
45201da177e4SLinus Torvalds 
4521461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
45220778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
4523461d8837SThomas Graf 
4524461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
45250778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
4526461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
4527461d8837SThomas Graf 	} else {
4528461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
4529461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
45300778769dSNoriaki TAKAMIYA 	}
45310778769dSNoriaki TAKAMIYA 
4532af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
453363159f29SIan Morris 	if (!dev)
45347198f8ceSThomas Graf 		return -ENODEV;
4535081bba5bSNoriaki TAKAMIYA 
4536479840ffSJiri Pirko 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4537479840ffSJiri Pirko 
453855ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
4539761aac73SThomas Haller 	ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
454093a714d6SMadhu Challa 		     IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
454155ebaef1SNoriaki TAKAMIYA 
45421cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
454363159f29SIan Morris 	if (!ifa) {
45447198f8ceSThomas Graf 		/*
45457198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
4546db9c7c39Sstephen hemminger 		 * userspace already relies on not having to provide this.
45477198f8ceSThomas Graf 		 */
4548caeaba79SNicolas Dichtel 		return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4549af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
4550af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
45511da177e4SLinus Torvalds 	}
45521da177e4SLinus Torvalds 
45537198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
45547198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
45557198f8ceSThomas Graf 		err = -EEXIST;
45567198f8ceSThomas Graf 	else
455755ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
45587198f8ceSThomas Graf 
45597198f8ceSThomas Graf 	in6_ifa_put(ifa);
45607198f8ceSThomas Graf 
45617198f8ceSThomas Graf 	return err;
45627198f8ceSThomas Graf }
45637198f8ceSThomas Graf 
4564479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4565101bb229SThomas Graf 			  u8 scope, int ifindex)
4566101bb229SThomas Graf {
4567101bb229SThomas Graf 	struct ifaddrmsg *ifm;
4568101bb229SThomas Graf 
4569101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
4570101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
4571101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
4572101bb229SThomas Graf 	ifm->ifa_flags = flags;
4573101bb229SThomas Graf 	ifm->ifa_scope = scope;
4574101bb229SThomas Graf 	ifm->ifa_index = ifindex;
4575101bb229SThomas Graf }
4576101bb229SThomas Graf 
457785486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
457885486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
457985486af0SThomas Graf {
458085486af0SThomas Graf 	struct ifa_cacheinfo ci;
458185486af0SThomas Graf 
458218a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
458318a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
458485486af0SThomas Graf 	ci.ifa_prefered = preferred;
458585486af0SThomas Graf 	ci.ifa_valid = valid;
458685486af0SThomas Graf 
458785486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
458885486af0SThomas Graf }
458985486af0SThomas Graf 
4590101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
4591101bb229SThomas Graf {
4592101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
4593101bb229SThomas Graf 		return RT_SCOPE_HOST;
4594101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
4595101bb229SThomas Graf 		return RT_SCOPE_LINK;
4596101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
4597101bb229SThomas Graf 		return RT_SCOPE_SITE;
4598101bb229SThomas Graf 	else
4599101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
4600101bb229SThomas Graf }
4601101bb229SThomas Graf 
46020ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
46030ab6803bSThomas Graf {
4604339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4605caeaba79SNicolas Dichtel 	       + nla_total_size(16) /* IFA_LOCAL */
4606339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
4607479840ffSJiri Pirko 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
4608479840ffSJiri Pirko 	       + nla_total_size(4)  /* IFA_FLAGS */;
46090ab6803bSThomas Graf }
4610c5396a31SYOSHIFUJI Hideaki 
46111da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
461215e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
46131da177e4SLinus Torvalds {
46141da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
461585486af0SThomas Graf 	u32 preferred, valid;
46161da177e4SLinus Torvalds 
461715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
461863159f29SIan Morris 	if (!nlh)
461926932566SPatrick McHardy 		return -EMSGSIZE;
46200ab6803bSThomas Graf 
4621101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4622101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
4623101bb229SThomas Graf 
4624fad8da3eSYasushi Asano 	if (!((ifa->flags&IFA_F_PERMANENT) &&
4625fad8da3eSYasushi Asano 	      (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
462685486af0SThomas Graf 		preferred = ifa->prefered_lft;
462785486af0SThomas Graf 		valid = ifa->valid_lft;
462885486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
46291da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
4630a1faa698SJens Rosenboom 			if (preferred > tval)
463185486af0SThomas Graf 				preferred -= tval;
4632a1faa698SJens Rosenboom 			else
4633a1faa698SJens Rosenboom 				preferred = 0;
4634f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
4635f56619fcSBen Hutchings 				if (valid > tval)
463685486af0SThomas Graf 					valid -= tval;
4637f56619fcSBen Hutchings 				else
4638f56619fcSBen Hutchings 					valid = 0;
4639f56619fcSBen Hutchings 			}
46401da177e4SLinus Torvalds 		}
46411da177e4SLinus Torvalds 	} else {
464285486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
464385486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
46441da177e4SLinus Torvalds 	}
464585486af0SThomas Graf 
46467996c799SCong Wang 	if (!ipv6_addr_any(&ifa->peer_addr)) {
4647930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4648930345eaSJiri Benc 		    nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4649caeaba79SNicolas Dichtel 			goto error;
4650caeaba79SNicolas Dichtel 	} else
4651930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4652caeaba79SNicolas Dichtel 			goto error;
4653caeaba79SNicolas Dichtel 
4654caeaba79SNicolas Dichtel 	if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4655caeaba79SNicolas Dichtel 		goto error;
465685486af0SThomas Graf 
4657479840ffSJiri Pirko 	if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4658479840ffSJiri Pirko 		goto error;
4659479840ffSJiri Pirko 
4660053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4661053c095aSJohannes Berg 	return 0;
4662caeaba79SNicolas Dichtel 
4663caeaba79SNicolas Dichtel error:
4664caeaba79SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
4665caeaba79SNicolas Dichtel 	return -EMSGSIZE;
46661da177e4SLinus Torvalds }
46671da177e4SLinus Torvalds 
46681da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
466915e47304SEric W. Biederman 				u32 portid, u32 seq, int event, u16 flags)
46701da177e4SLinus Torvalds {
46711da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4672101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4673101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
46741da177e4SLinus Torvalds 
46751da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4676101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4677101bb229SThomas Graf 
467815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
467963159f29SIan Morris 	if (!nlh)
468026932566SPatrick McHardy 		return -EMSGSIZE;
46810ab6803bSThomas Graf 
4682101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4683930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
46840ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
468526932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
468626932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
468726932566SPatrick McHardy 		return -EMSGSIZE;
468826932566SPatrick McHardy 	}
468985486af0SThomas Graf 
4690053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4691053c095aSJohannes Berg 	return 0;
46921da177e4SLinus Torvalds }
46931da177e4SLinus Torvalds 
46941da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
469515e47304SEric W. Biederman 				u32 portid, u32 seq, int event, unsigned int flags)
46961da177e4SLinus Torvalds {
46971da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4698101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4699101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
47001da177e4SLinus Torvalds 
47011da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4702101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4703101bb229SThomas Graf 
470415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
470563159f29SIan Morris 	if (!nlh)
470626932566SPatrick McHardy 		return -EMSGSIZE;
47070ab6803bSThomas Graf 
4708101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4709930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
47100ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
471126932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
471226932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
471326932566SPatrick McHardy 		return -EMSGSIZE;
471426932566SPatrick McHardy 	}
471585486af0SThomas Graf 
4716053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4717053c095aSJohannes Berg 	return 0;
47181da177e4SLinus Torvalds }
47191da177e4SLinus Torvalds 
4720e21e8467SStephen Hemminger enum addr_type_t {
47211da177e4SLinus Torvalds 	UNICAST_ADDR,
47221da177e4SLinus Torvalds 	MULTICAST_ADDR,
47231da177e4SLinus Torvalds 	ANYCAST_ADDR,
47241da177e4SLinus Torvalds };
47251da177e4SLinus Torvalds 
4726234b27c3SEric Dumazet /* called with rcu_read_lock() */
4727234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4728234b27c3SEric Dumazet 			  struct netlink_callback *cb, enum addr_type_t type,
4729234b27c3SEric Dumazet 			  int s_ip_idx, int *p_ip_idx)
47301da177e4SLinus Torvalds {
47311da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
47321da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
4733234b27c3SEric Dumazet 	int err = 1;
4734234b27c3SEric Dumazet 	int ip_idx = *p_ip_idx;
47351da177e4SLinus Torvalds 
47361da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
47371da177e4SLinus Torvalds 	switch (type) {
4738502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
4739502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
4740502a2ffdSstephen hemminger 
4741ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
4742502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
4743502a2ffdSstephen hemminger 			if (++ip_idx < s_ip_idx)
47441da177e4SLinus Torvalds 				continue;
47455d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifaddr(skb, ifa,
474615e47304SEric W. Biederman 						NETLINK_CB(cb->skb).portid,
47475d5619b4SYOSHIFUJI Hideaki 						cb->nlh->nlmsg_seq,
47485d5619b4SYOSHIFUJI Hideaki 						RTM_NEWADDR,
47495d5619b4SYOSHIFUJI Hideaki 						NLM_F_MULTI);
4750053c095aSJohannes Berg 			if (err < 0)
4751bcd32326SEric Dumazet 				break;
475263998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
47531da177e4SLinus Torvalds 		}
47541da177e4SLinus Torvalds 		break;
4755502a2ffdSstephen hemminger 	}
47561da177e4SLinus Torvalds 	case MULTICAST_ADDR:
47571da177e4SLinus Torvalds 		/* multicast address */
47581da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
47591da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
47601da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
47611da177e4SLinus Torvalds 				continue;
47625d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifmcaddr(skb, ifmca,
476315e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
47645d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
47655d5619b4SYOSHIFUJI Hideaki 						  RTM_GETMULTICAST,
47665d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4767053c095aSJohannes Berg 			if (err < 0)
4768bcd32326SEric Dumazet 				break;
47691da177e4SLinus Torvalds 		}
47701da177e4SLinus Torvalds 		break;
47711da177e4SLinus Torvalds 	case ANYCAST_ADDR:
47721da177e4SLinus Torvalds 		/* anycast address */
47731da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
47741da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
47751da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
47761da177e4SLinus Torvalds 				continue;
47775d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifacaddr(skb, ifaca,
477815e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
47795d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
47805d5619b4SYOSHIFUJI Hideaki 						  RTM_GETANYCAST,
47815d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4782053c095aSJohannes Berg 			if (err < 0)
4783bcd32326SEric Dumazet 				break;
47841da177e4SLinus Torvalds 		}
47851da177e4SLinus Torvalds 		break;
47861da177e4SLinus Torvalds 	default:
47871da177e4SLinus Torvalds 		break;
47881da177e4SLinus Torvalds 	}
47891da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
4790234b27c3SEric Dumazet 	*p_ip_idx = ip_idx;
4791234b27c3SEric Dumazet 	return err;
4792234b27c3SEric Dumazet }
47935d5619b4SYOSHIFUJI Hideaki 
4794234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4795234b27c3SEric Dumazet 			   enum addr_type_t type)
4796234b27c3SEric Dumazet {
4797234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
4798234b27c3SEric Dumazet 	int h, s_h;
4799234b27c3SEric Dumazet 	int idx, ip_idx;
4800234b27c3SEric Dumazet 	int s_idx, s_ip_idx;
4801234b27c3SEric Dumazet 	struct net_device *dev;
4802234b27c3SEric Dumazet 	struct inet6_dev *idev;
4803234b27c3SEric Dumazet 	struct hlist_head *head;
4804234b27c3SEric Dumazet 
4805234b27c3SEric Dumazet 	s_h = cb->args[0];
4806234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
4807234b27c3SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
4808234b27c3SEric Dumazet 
4809234b27c3SEric Dumazet 	rcu_read_lock();
481063998ac2SNicolas Dichtel 	cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4811234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4812234b27c3SEric Dumazet 		idx = 0;
4813234b27c3SEric Dumazet 		head = &net->dev_index_head[h];
4814b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
4815234b27c3SEric Dumazet 			if (idx < s_idx)
4816234b27c3SEric Dumazet 				goto cont;
48174b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
4818234b27c3SEric Dumazet 				s_ip_idx = 0;
4819234b27c3SEric Dumazet 			ip_idx = 0;
4820e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
4821e21e8467SStephen Hemminger 			if (!idev)
4822234b27c3SEric Dumazet 				goto cont;
4823234b27c3SEric Dumazet 
4824234b27c3SEric Dumazet 			if (in6_dump_addrs(idev, skb, cb, type,
48257b46a644SDavid S. Miller 					   s_ip_idx, &ip_idx) < 0)
4826234b27c3SEric Dumazet 				goto done;
48277562f876SPavel Emelianov cont:
48287562f876SPavel Emelianov 			idx++;
48291da177e4SLinus Torvalds 		}
4830234b27c3SEric Dumazet 	}
4831234b27c3SEric Dumazet done:
4832234b27c3SEric Dumazet 	rcu_read_unlock();
4833234b27c3SEric Dumazet 	cb->args[0] = h;
4834234b27c3SEric Dumazet 	cb->args[1] = idx;
4835234b27c3SEric Dumazet 	cb->args[2] = ip_idx;
4836234b27c3SEric Dumazet 
48371da177e4SLinus Torvalds 	return skb->len;
48381da177e4SLinus Torvalds }
48391da177e4SLinus Torvalds 
48401da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
48411da177e4SLinus Torvalds {
48421da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
4843b854272bSDenis V. Lunev 
48441da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
48451da177e4SLinus Torvalds }
48461da177e4SLinus Torvalds 
48471da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
48481da177e4SLinus Torvalds {
48491da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
4850b854272bSDenis V. Lunev 
48511da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
48521da177e4SLinus Torvalds }
48531da177e4SLinus Torvalds 
48541da177e4SLinus Torvalds 
48551da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
48561da177e4SLinus Torvalds {
48571da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
4858b854272bSDenis V. Lunev 
48591da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
48601da177e4SLinus Torvalds }
48611da177e4SLinus Torvalds 
4862661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
48636c223828SNoriaki TAKAMIYA {
48643b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
48651b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
48661b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4867caeaba79SNicolas Dichtel 	struct in6_addr *addr = NULL, *peer;
48686c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
48696c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
48706c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
48716c223828SNoriaki TAKAMIYA 	int err;
48726c223828SNoriaki TAKAMIYA 
48731b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
48741b29fc2cSThomas Graf 	if (err < 0)
48751b29fc2cSThomas Graf 		goto errout;
48766c223828SNoriaki TAKAMIYA 
4877caeaba79SNicolas Dichtel 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
487863159f29SIan Morris 	if (!addr) {
48791b29fc2cSThomas Graf 		err = -EINVAL;
48801b29fc2cSThomas Graf 		goto errout;
48811b29fc2cSThomas Graf 	}
48821b29fc2cSThomas Graf 
48831b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
48846c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
48856fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
48866c223828SNoriaki TAKAMIYA 
4887e21e8467SStephen Hemminger 	ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4888e21e8467SStephen Hemminger 	if (!ifa) {
48891b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
48901b29fc2cSThomas Graf 		goto errout;
48916c223828SNoriaki TAKAMIYA 	}
48926c223828SNoriaki TAKAMIYA 
4893e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4894e21e8467SStephen Hemminger 	if (!skb) {
48951b29fc2cSThomas Graf 		err = -ENOBUFS;
48961b29fc2cSThomas Graf 		goto errout_ifa;
48971b29fc2cSThomas Graf 	}
48981b29fc2cSThomas Graf 
489915e47304SEric W. Biederman 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
49006c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
490126932566SPatrick McHardy 	if (err < 0) {
490226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
490326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
490426932566SPatrick McHardy 		kfree_skb(skb);
490526932566SPatrick McHardy 		goto errout_ifa;
490626932566SPatrick McHardy 	}
490715e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
49081b29fc2cSThomas Graf errout_ifa:
49096c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
49101b29fc2cSThomas Graf errout:
49116c223828SNoriaki TAKAMIYA 	return err;
49126c223828SNoriaki TAKAMIYA }
49136c223828SNoriaki TAKAMIYA 
49141da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
49151da177e4SLinus Torvalds {
49161da177e4SLinus Torvalds 	struct sk_buff *skb;
4917c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
49185d620266SThomas Graf 	int err = -ENOBUFS;
49191da177e4SLinus Torvalds 
4920f784ad3dSMahesh Bandewar 	/* Don't send DELADDR notification for TENTATIVE address,
4921f784ad3dSMahesh Bandewar 	 * since NEWADDR notification is sent only after removing
4922f784ad3dSMahesh Bandewar 	 * TENTATIVE flag.
4923f784ad3dSMahesh Bandewar 	 */
4924f784ad3dSMahesh Bandewar 	if (ifa->flags & IFA_F_TENTATIVE && event == RTM_DELADDR)
4925f784ad3dSMahesh Bandewar 		return;
4926f784ad3dSMahesh Bandewar 
49270ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
492863159f29SIan Morris 	if (!skb)
49295d620266SThomas Graf 		goto errout;
49305d620266SThomas Graf 
49315d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
493226932566SPatrick McHardy 	if (err < 0) {
493326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
493426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
493526932566SPatrick McHardy 		kfree_skb(skb);
493626932566SPatrick McHardy 		goto errout;
493726932566SPatrick McHardy 	}
49381ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
49391ce85fe4SPablo Neira Ayuso 	return;
49405d620266SThomas Graf errout:
49415d620266SThomas Graf 	if (err < 0)
49426fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
49431da177e4SLinus Torvalds }
49441da177e4SLinus Torvalds 
4945b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
49461da177e4SLinus Torvalds 				__s32 *array, int bytes)
49471da177e4SLinus Torvalds {
494804561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
494904561c1fSThomas Graf 
49501da177e4SLinus Torvalds 	memset(array, 0, bytes);
49511da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
49521da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
49531da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
49541da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
49551da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
49561da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
49571da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
49581da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
495993908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
496093908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
4961bd11f074SMaciej Żenczykowski 	array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4962bd11f074SMaciej Żenczykowski 		jiffies_to_msecs(cnf->rtr_solicit_max_interval);
496393908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
496493908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
49651da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4966fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4967fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4968fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4969fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
49701da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
49711da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
49721da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
49731da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
49741da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
49751da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
497665f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
49778013d1d7SHangbin Liu 	array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
4978c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4979930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4980930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
498193908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
498293908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
4983fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
4984*bbea124bSJoel Scherpelz 	array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
498509c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
498609c884d4SYOSHIFUJI Hideaki #endif
4987930d6ff2SYOSHIFUJI Hideaki #endif
4988fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
49890bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
499095c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
499195c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
49927fd2561eSErik Kline 	array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
499395c385b4SNeil Horman #endif
49947bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
49957bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
49967bc570c8SYOSHIFUJI Hideaki #endif
4997778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
49981b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4999c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
50005cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5001b800c3b9SHannes Frederic Sowa 	array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5002d9333196SBen Greear 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5003c2943f14SHarout Hedeshian 	array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
500435103d11SAndy Gospodarek 	array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
50053d1bec99SHannes Frederic Sowa 	/* we omit DEVCONF_STABLE_SECRET for now */
50063985e8a3SErik Kline 	array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5007abbc3043SJohannes Berg 	array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
50087a02bf89SJohannes Berg 	array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5009f1705ec1SDavid Ahern 	array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
50101ababebaSDavid Lebrun 	array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5011bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
5012bf355b8dSDavid Lebrun 	array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5013bf355b8dSDavid Lebrun #endif
5014adc176c5SErik Nordmark 	array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5015d35a00b8SFelix Jia 	array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5016df789fe7SDavid Forster 	array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
50171da177e4SLinus Torvalds }
50181da177e4SLinus Torvalds 
5019b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
5020b382b191SThomas Graf {
5021b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
5022b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
5023b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5024b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5025f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5026f53adae4SDaniel Borkmann 	     + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
5027b382b191SThomas Graf }
5028b382b191SThomas Graf 
5029339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
5030339bf98fSThomas Graf {
5031339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5032339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5033339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5034339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
5035339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
50360344338bSAndy Gospodarek 	       + nla_total_size(1) /* IFLA_OPERSTATE */
5037b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5038339bf98fSThomas Graf }
5039c5396a31SYOSHIFUJI Hideaki 
5040be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5041aca05671SJia He 					int bytes)
50427f7d9a6bSHerbert Xu {
50437f7d9a6bSHerbert Xu 	int i;
5044aca05671SJia He 	int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
50457f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
50467f7d9a6bSHerbert Xu 
50477f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
5048aca05671SJia He 	put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5049aca05671SJia He 	for (i = 1; i < ICMP6_MIB_MAX; i++)
5050be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
50517f7d9a6bSHerbert Xu 
5052aca05671SJia He 	memset(&stats[ICMP6_MIB_MAX], 0, pad);
50537f7d9a6bSHerbert Xu }
50547f7d9a6bSHerbert Xu 
5055698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5056a3a77372SRaghavendra K T 					int bytes, size_t syncpoff)
50574ce3c183SEric Dumazet {
5058a3a77372SRaghavendra K T 	int i, c;
5059a3a77372SRaghavendra K T 	u64 buff[IPSTATS_MIB_MAX];
5060a3a77372SRaghavendra K T 	int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5061a3a77372SRaghavendra K T 
50624ce3c183SEric Dumazet 	BUG_ON(pad < 0);
50634ce3c183SEric Dumazet 
5064a3a77372SRaghavendra K T 	memset(buff, 0, sizeof(buff));
5065a3a77372SRaghavendra K T 	buff[0] = IPSTATS_MIB_MAX;
50664ce3c183SEric Dumazet 
5067a3a77372SRaghavendra K T 	for_each_possible_cpu(c) {
5068a3a77372SRaghavendra K T 		for (i = 1; i < IPSTATS_MIB_MAX; i++)
5069a3a77372SRaghavendra K T 			buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5070a3a77372SRaghavendra K T 	}
5071a3a77372SRaghavendra K T 
5072a3a77372SRaghavendra K T 	memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5073a3a77372SRaghavendra K T 	memset(&stats[IPSTATS_MIB_MAX], 0, pad);
50744ce3c183SEric Dumazet }
50754ce3c183SEric Dumazet 
50767f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
50777f7d9a6bSHerbert Xu 			     int bytes)
50787f7d9a6bSHerbert Xu {
50797f7d9a6bSHerbert Xu 	switch (attrtype) {
50807f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
5081a3a77372SRaghavendra K T 		__snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5082a3a77372SRaghavendra K T 				     offsetof(struct ipstats_mib, syncp));
50837f7d9a6bSHerbert Xu 		break;
50847f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
5085aca05671SJia He 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
50867f7d9a6bSHerbert Xu 		break;
50877f7d9a6bSHerbert Xu 	}
50887f7d9a6bSHerbert Xu }
50897f7d9a6bSHerbert Xu 
5090d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5091d5566fd7SSowmini Varadhan 				  u32 ext_filter_mask)
50921da177e4SLinus Torvalds {
5093bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
50941da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
50951da177e4SLinus Torvalds 
5096c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5097c78679e8SDavid S. Miller 		goto nla_put_failure;
50981da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
509924912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
510024912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
51011f9248e5SJiri Pirko 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5102c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5103c78679e8SDavid S. Miller 		goto nla_put_failure;
5104bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
510563159f29SIan Morris 	if (!nla)
510604561c1fSThomas Graf 		goto nla_put_failure;
5107bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
51081da177e4SLinus Torvalds 
5109bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
5110bf99f1bdSYOSHIFUJI Hideaki 
5111d5566fd7SSowmini Varadhan 	if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5112d5566fd7SSowmini Varadhan 		return 0;
5113d5566fd7SSowmini Varadhan 
5114bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
511563159f29SIan Morris 	if (!nla)
5116bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
5117bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5118bf99f1bdSYOSHIFUJI Hideaki 
5119bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
512063159f29SIan Morris 	if (!nla)
5121bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
5122bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
51231da177e4SLinus Torvalds 
5124f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
512563159f29SIan Morris 	if (!nla)
5126f53adae4SDaniel Borkmann 		goto nla_put_failure;
5127bc91b0f0SJiri Pirko 
5128d35a00b8SFelix Jia 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5129bc91b0f0SJiri Pirko 		goto nla_put_failure;
5130bc91b0f0SJiri Pirko 
5131f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
5132f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5133f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
5134f53adae4SDaniel Borkmann 
5135b382b191SThomas Graf 	return 0;
5136b382b191SThomas Graf 
5137b382b191SThomas Graf nla_put_failure:
5138b382b191SThomas Graf 	return -EMSGSIZE;
5139b382b191SThomas Graf }
5140b382b191SThomas Graf 
5141b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev,
5142b1974ed0SArad, Ronen 				     u32 ext_filter_mask)
5143b382b191SThomas Graf {
5144b382b191SThomas Graf 	if (!__in6_dev_get(dev))
5145b382b191SThomas Graf 		return 0;
5146b382b191SThomas Graf 
5147b382b191SThomas Graf 	return inet6_ifla6_size();
5148b382b191SThomas Graf }
5149b382b191SThomas Graf 
5150d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5151d5566fd7SSowmini Varadhan 			      u32 ext_filter_mask)
5152b382b191SThomas Graf {
5153b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
5154b382b191SThomas Graf 
5155b382b191SThomas Graf 	if (!idev)
5156b382b191SThomas Graf 		return -ENODATA;
5157b382b191SThomas Graf 
5158d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5159b382b191SThomas Graf 		return -EMSGSIZE;
5160b382b191SThomas Graf 
5161b382b191SThomas Graf 	return 0;
5162b382b191SThomas Graf }
5163b382b191SThomas Graf 
5164f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5165f53adae4SDaniel Borkmann {
5166f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
5167f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
516847e27d5eSDaniel Borkmann 	bool clear_token, update_rs = false;
5169dc848292SHannes Frederic Sowa 	struct in6_addr ll_addr;
5170f53adae4SDaniel Borkmann 
5171c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
5172c15b1ccaSHannes Frederic Sowa 
517363159f29SIan Morris 	if (!token)
5174f53adae4SDaniel Borkmann 		return -EINVAL;
5175f53adae4SDaniel Borkmann 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5176f53adae4SDaniel Borkmann 		return -EINVAL;
5177f53adae4SDaniel Borkmann 	if (!ipv6_accept_ra(idev))
5178f53adae4SDaniel Borkmann 		return -EINVAL;
5179bd11f074SMaciej Żenczykowski 	if (idev->cnf.rtr_solicits == 0)
5180f53adae4SDaniel Borkmann 		return -EINVAL;
5181f53adae4SDaniel Borkmann 
5182f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5183f53adae4SDaniel Borkmann 
5184f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5185f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5186f53adae4SDaniel Borkmann 
5187f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5188f53adae4SDaniel Borkmann 
518947e27d5eSDaniel Borkmann 	clear_token = ipv6_addr_any(token);
519047e27d5eSDaniel Borkmann 	if (clear_token)
519147e27d5eSDaniel Borkmann 		goto update_lft;
519247e27d5eSDaniel Borkmann 
5193dc848292SHannes Frederic Sowa 	if (!idev->dead && (idev->if_flags & IF_READY) &&
5194dc848292SHannes Frederic Sowa 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5195dc848292SHannes Frederic Sowa 			     IFA_F_OPTIMISTIC)) {
5196fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
5197fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
5198fc403832SDaniel Borkmann 		 */
5199f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5200fc403832SDaniel Borkmann 		update_rs = true;
5201fc403832SDaniel Borkmann 	}
5202f53adae4SDaniel Borkmann 
520347e27d5eSDaniel Borkmann update_lft:
5204f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5205fc403832SDaniel Borkmann 
520677ecaaceSHannes Frederic Sowa 	if (update_rs) {
5207f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
5208bd11f074SMaciej Żenczykowski 		idev->rs_interval = rfc3315_s14_backoff_init(
5209bd11f074SMaciej Żenczykowski 			idev->cnf.rtr_solicit_interval);
521077ecaaceSHannes Frederic Sowa 		idev->rs_probes = 1;
5211bd11f074SMaciej Żenczykowski 		addrconf_mod_rs_timer(idev, idev->rs_interval);
521277ecaaceSHannes Frederic Sowa 	}
5213f53adae4SDaniel Borkmann 
5214f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
5215f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
5216f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
5217617fe29dSDaniel Borkmann 		if (ifp->tokenized) {
5218f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
5219f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
5220f53adae4SDaniel Borkmann 		}
5221f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
5222f53adae4SDaniel Borkmann 	}
5223f53adae4SDaniel Borkmann 
5224f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5225b2ed64a9SLubomir Rintel 	inet6_ifinfo_notify(RTM_NEWLINK, idev);
5226c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
5227f53adae4SDaniel Borkmann 	return 0;
5228f53adae4SDaniel Borkmann }
5229f53adae4SDaniel Borkmann 
523011b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
523111b1f828SDaniel Borkmann 	[IFLA_INET6_ADDR_GEN_MODE]	= { .type = NLA_U8 },
523211b1f828SDaniel Borkmann 	[IFLA_INET6_TOKEN]		= { .len = sizeof(struct in6_addr) },
523311b1f828SDaniel Borkmann };
523411b1f828SDaniel Borkmann 
523511b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev,
523611b1f828SDaniel Borkmann 				  const struct nlattr *nla)
523711b1f828SDaniel Borkmann {
523811b1f828SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
523911b1f828SDaniel Borkmann 
524011b1f828SDaniel Borkmann 	if (dev && !__in6_dev_get(dev))
524111b1f828SDaniel Borkmann 		return -EAFNOSUPPORT;
524211b1f828SDaniel Borkmann 
524311b1f828SDaniel Borkmann 	return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
524411b1f828SDaniel Borkmann }
524511b1f828SDaniel Borkmann 
5246d35a00b8SFelix Jia static int check_addr_gen_mode(int mode)
5247d35a00b8SFelix Jia {
5248d35a00b8SFelix Jia 	if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5249d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_NONE &&
5250d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5251d35a00b8SFelix Jia 	    mode != IN6_ADDR_GEN_MODE_RANDOM)
5252d35a00b8SFelix Jia 		return -EINVAL;
5253d35a00b8SFelix Jia 	return 1;
5254d35a00b8SFelix Jia }
5255d35a00b8SFelix Jia 
5256d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5257d35a00b8SFelix Jia 				int mode)
5258d35a00b8SFelix Jia {
5259d35a00b8SFelix Jia 	if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5260d35a00b8SFelix Jia 	    !idev->cnf.stable_secret.initialized &&
5261d35a00b8SFelix Jia 	    !net->ipv6.devconf_dflt->stable_secret.initialized)
5262d35a00b8SFelix Jia 		return -EINVAL;
5263d35a00b8SFelix Jia 	return 1;
5264d35a00b8SFelix Jia }
5265d35a00b8SFelix Jia 
5266f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5267f53adae4SDaniel Borkmann {
5268f53adae4SDaniel Borkmann 	int err = -EINVAL;
5269f53adae4SDaniel Borkmann 	struct inet6_dev *idev = __in6_dev_get(dev);
5270f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
5271f53adae4SDaniel Borkmann 
5272f53adae4SDaniel Borkmann 	if (!idev)
5273f53adae4SDaniel Borkmann 		return -EAFNOSUPPORT;
5274f53adae4SDaniel Borkmann 
5275f53adae4SDaniel Borkmann 	if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5276f53adae4SDaniel Borkmann 		BUG();
5277f53adae4SDaniel Borkmann 
5278bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_TOKEN]) {
5279f53adae4SDaniel Borkmann 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5280bc91b0f0SJiri Pirko 		if (err)
5281bc91b0f0SJiri Pirko 			return err;
5282bc91b0f0SJiri Pirko 	}
5283bc91b0f0SJiri Pirko 
5284bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5285bc91b0f0SJiri Pirko 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5286bc91b0f0SJiri Pirko 
5287d35a00b8SFelix Jia 		if (check_addr_gen_mode(mode) < 0 ||
5288d35a00b8SFelix Jia 		    check_stable_privacy(idev, dev_net(dev), mode) < 0)
5289bc91b0f0SJiri Pirko 			return -EINVAL;
5290622c81d5SHannes Frederic Sowa 
5291d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = mode;
5292bc91b0f0SJiri Pirko 		err = 0;
5293bc91b0f0SJiri Pirko 	}
5294f53adae4SDaniel Borkmann 
5295f53adae4SDaniel Borkmann 	return err;
5296f53adae4SDaniel Borkmann }
5297f53adae4SDaniel Borkmann 
5298b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
529915e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
5300b382b191SThomas Graf {
5301b382b191SThomas Graf 	struct net_device *dev = idev->dev;
5302b382b191SThomas Graf 	struct ifinfomsg *hdr;
5303b382b191SThomas Graf 	struct nlmsghdr *nlh;
5304b382b191SThomas Graf 	void *protoinfo;
5305b382b191SThomas Graf 
530615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
530763159f29SIan Morris 	if (!nlh)
5308b382b191SThomas Graf 		return -EMSGSIZE;
5309b382b191SThomas Graf 
5310b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
5311b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
5312b382b191SThomas Graf 	hdr->__ifi_pad = 0;
5313b382b191SThomas Graf 	hdr->ifi_type = dev->type;
5314b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
5315b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
5316b382b191SThomas Graf 	hdr->ifi_change = 0;
5317b382b191SThomas Graf 
5318c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5319c78679e8SDavid S. Miller 	    (dev->addr_len &&
5320c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5321c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5322a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
53230344338bSAndy Gospodarek 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
53240344338bSAndy Gospodarek 	    nla_put_u8(skb, IFLA_OPERSTATE,
53250344338bSAndy Gospodarek 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5326c78679e8SDavid S. Miller 		goto nla_put_failure;
5327b382b191SThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
532863159f29SIan Morris 	if (!protoinfo)
5329b382b191SThomas Graf 		goto nla_put_failure;
5330b382b191SThomas Graf 
5331d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5332b382b191SThomas Graf 		goto nla_put_failure;
5333b382b191SThomas Graf 
533404561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
5335053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5336053c095aSJohannes Berg 	return 0;
53371da177e4SLinus Torvalds 
533804561c1fSThomas Graf nla_put_failure:
533926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
534026932566SPatrick McHardy 	return -EMSGSIZE;
53411da177e4SLinus Torvalds }
53421da177e4SLinus Torvalds 
53431da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
53441da177e4SLinus Torvalds {
53453b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
534684d2697dSEric Dumazet 	int h, s_h;
5347434a8a58SDavid S. Miller 	int idx = 0, s_idx;
53481da177e4SLinus Torvalds 	struct net_device *dev;
53491da177e4SLinus Torvalds 	struct inet6_dev *idev;
535084d2697dSEric Dumazet 	struct hlist_head *head;
53511da177e4SLinus Torvalds 
535284d2697dSEric Dumazet 	s_h = cb->args[0];
535384d2697dSEric Dumazet 	s_idx = cb->args[1];
535484d2697dSEric Dumazet 
535584d2697dSEric Dumazet 	rcu_read_lock();
535684d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
53577562f876SPavel Emelianov 		idx = 0;
535884d2697dSEric Dumazet 		head = &net->dev_index_head[h];
5359b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
53601da177e4SLinus Torvalds 			if (idx < s_idx)
53617562f876SPavel Emelianov 				goto cont;
536284d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
536384d2697dSEric Dumazet 			if (!idev)
53647562f876SPavel Emelianov 				goto cont;
536584d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
536615e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
536784d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
5368053c095aSJohannes Berg 					      RTM_NEWLINK, NLM_F_MULTI) < 0)
536984d2697dSEric Dumazet 				goto out;
53707562f876SPavel Emelianov cont:
53717562f876SPavel Emelianov 			idx++;
53721da177e4SLinus Torvalds 		}
537384d2697dSEric Dumazet 	}
537484d2697dSEric Dumazet out:
537584d2697dSEric Dumazet 	rcu_read_unlock();
537684d2697dSEric Dumazet 	cb->args[1] = idx;
537784d2697dSEric Dumazet 	cb->args[0] = h;
53781da177e4SLinus Torvalds 
53791da177e4SLinus Torvalds 	return skb->len;
53801da177e4SLinus Torvalds }
53811da177e4SLinus Torvalds 
53821da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
53831da177e4SLinus Torvalds {
53841da177e4SLinus Torvalds 	struct sk_buff *skb;
5385c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
53868d7a76c9SThomas Graf 	int err = -ENOBUFS;
53871da177e4SLinus Torvalds 
5388339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
538963159f29SIan Morris 	if (!skb)
53908d7a76c9SThomas Graf 		goto errout;
53918d7a76c9SThomas Graf 
53928d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
539326932566SPatrick McHardy 	if (err < 0) {
539426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
539526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
539626932566SPatrick McHardy 		kfree_skb(skb);
539726932566SPatrick McHardy 		goto errout;
539826932566SPatrick McHardy 	}
53995f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
54001ce85fe4SPablo Neira Ayuso 	return;
54018d7a76c9SThomas Graf errout:
54028d7a76c9SThomas Graf 	if (err < 0)
54035f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
54041da177e4SLinus Torvalds }
54051da177e4SLinus Torvalds 
5406339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
5407339bf98fSThomas Graf {
5408339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
5409339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
5410339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
5411339bf98fSThomas Graf }
5412c5396a31SYOSHIFUJI Hideaki 
54131da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
541415e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
5415b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
54161da177e4SLinus Torvalds {
54171da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
54181da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
54191da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
54201da177e4SLinus Torvalds 
542115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
542263159f29SIan Morris 	if (!nlh)
542326932566SPatrick McHardy 		return -EMSGSIZE;
54246051e2f4SThomas Graf 
54256051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
54261da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
54278a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
54288a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
54291da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
54301da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
54311da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
54328a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
54331da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
54341da177e4SLinus Torvalds 	if (pinfo->onlink)
54351da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
54361da177e4SLinus Torvalds 	if (pinfo->autoconf)
54371da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
54381da177e4SLinus Torvalds 
5439c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5440c78679e8SDavid S. Miller 		goto nla_put_failure;
54411da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
54421da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
5443c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5444c78679e8SDavid S. Miller 		goto nla_put_failure;
5445053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5446053c095aSJohannes Berg 	return 0;
54471da177e4SLinus Torvalds 
54486051e2f4SThomas Graf nla_put_failure:
544926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
545026932566SPatrick McHardy 	return -EMSGSIZE;
54511da177e4SLinus Torvalds }
54521da177e4SLinus Torvalds 
54531da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
54541da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
54551da177e4SLinus Torvalds {
54561da177e4SLinus Torvalds 	struct sk_buff *skb;
5457c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
54588c384bfaSThomas Graf 	int err = -ENOBUFS;
54591da177e4SLinus Torvalds 
5460339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
546163159f29SIan Morris 	if (!skb)
54628c384bfaSThomas Graf 		goto errout;
54638c384bfaSThomas Graf 
54648c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
546526932566SPatrick McHardy 	if (err < 0) {
546626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
546726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
546826932566SPatrick McHardy 		kfree_skb(skb);
546926932566SPatrick McHardy 		goto errout;
547026932566SPatrick McHardy 	}
54711ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
54721ce85fe4SPablo Neira Ayuso 	return;
54738c384bfaSThomas Graf errout:
54748c384bfaSThomas Graf 	if (err < 0)
54756fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
54761da177e4SLinus Torvalds }
54771da177e4SLinus Torvalds 
54781da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
54791da177e4SLinus Torvalds {
548063998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
548163998ac2SNicolas Dichtel 
5482c15b1ccaSHannes Frederic Sowa 	if (event)
5483c15b1ccaSHannes Frederic Sowa 		ASSERT_RTNL();
5484c15b1ccaSHannes Frederic Sowa 
54851da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
54861da177e4SLinus Torvalds 
54871da177e4SLinus Torvalds 	switch (event) {
54881da177e4SLinus Torvalds 	case RTM_NEWADDR:
548995c385b4SNeil Horman 		/*
549095c385b4SNeil Horman 		 * If the address was optimistic
549195c385b4SNeil Horman 		 * we inserted the route at the start of
549295c385b4SNeil Horman 		 * our DAD process, so we don't need
549395c385b4SNeil Horman 		 * to do it again
549495c385b4SNeil Horman 		 */
549595c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
549640e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
54971da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
54981da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
54997996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr))
5500caeaba79SNicolas Dichtel 			addrconf_prefix_route(&ifp->peer_addr, 128,
5501caeaba79SNicolas Dichtel 					      ifp->idev->dev, 0, 0);
55021da177e4SLinus Torvalds 		break;
55031da177e4SLinus Torvalds 	case RTM_DELADDR:
55041da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
55051da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
55061da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
55077996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr)) {
5508caeaba79SNicolas Dichtel 			struct rt6_info *rt;
5509caeaba79SNicolas Dichtel 
5510e7478dfcSNicolas Dichtel 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5511e7478dfcSNicolas Dichtel 						       ifp->idev->dev, 0, 0);
55128e3d5be7SMartin KaFai Lau 			if (rt)
55138e3d5be7SMartin KaFai Lau 				ip6_del_rt(rt);
5514caeaba79SNicolas Dichtel 		}
551538bd10c4SDavid Ahern 		if (ifp->rt) {
5516d8d1f30bSChangli Gao 			dst_hold(&ifp->rt->dst);
55178e3d5be7SMartin KaFai Lau 			ip6_del_rt(ifp->rt);
551838bd10c4SDavid Ahern 		}
5519705f1c86SHannes Frederic Sowa 		rt_genid_bump_ipv6(net);
55201da177e4SLinus Torvalds 		break;
55211da177e4SLinus Torvalds 	}
552263998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
55231da177e4SLinus Torvalds }
55241da177e4SLinus Torvalds 
55251da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
55261da177e4SLinus Torvalds {
55278814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
55281da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
55291da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
55308814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
55311da177e4SLinus Torvalds }
55321da177e4SLinus Torvalds 
55331da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
55341da177e4SLinus Torvalds 
55351da177e4SLinus Torvalds static
5536fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
55371da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
55381da177e4SLinus Torvalds {
55391da177e4SLinus Torvalds 	int *valp = ctl->data;
55401da177e4SLinus Torvalds 	int val = *valp;
554188af182eSEric W. Biederman 	loff_t pos = *ppos;
5542fe2c6338SJoe Perches 	struct ctl_table lctl;
55431da177e4SLinus Torvalds 	int ret;
55441da177e4SLinus Torvalds 
5545013d97e9SFrancesco Ruggeri 	/*
5546013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
5547013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5548013d97e9SFrancesco Ruggeri 	 */
5549013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5550013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5551013d97e9SFrancesco Ruggeri 
5552013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
55531da177e4SLinus Torvalds 
5554c8fecf22SPavel Emelyanov 	if (write)
5555b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
555688af182eSEric W. Biederman 	if (ret)
555788af182eSEric W. Biederman 		*ppos = pos;
55581da177e4SLinus Torvalds 	return ret;
55591da177e4SLinus Torvalds }
55601da177e4SLinus Torvalds 
556177751427SMarcelo Leitner static
556277751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
556377751427SMarcelo Leitner 			void __user *buffer, size_t *lenp, loff_t *ppos)
556477751427SMarcelo Leitner {
556577751427SMarcelo Leitner 	struct inet6_dev *idev = ctl->extra1;
556677751427SMarcelo Leitner 	int min_mtu = IPV6_MIN_MTU;
556777751427SMarcelo Leitner 	struct ctl_table lctl;
556877751427SMarcelo Leitner 
556977751427SMarcelo Leitner 	lctl = *ctl;
557077751427SMarcelo Leitner 	lctl.extra1 = &min_mtu;
557177751427SMarcelo Leitner 	lctl.extra2 = idev ? &idev->dev->mtu : NULL;
557277751427SMarcelo Leitner 
557377751427SMarcelo Leitner 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
557477751427SMarcelo Leitner }
557577751427SMarcelo Leitner 
557656d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
557756d417b1SBrian Haley {
557875538c2bSCong Wang 	struct netdev_notifier_info info;
557975538c2bSCong Wang 
558056d417b1SBrian Haley 	if (!idev || !idev->dev)
558156d417b1SBrian Haley 		return;
558256d417b1SBrian Haley 
558375538c2bSCong Wang 	netdev_notifier_info_init(&info, idev->dev);
558456d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
558575538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_DOWN, &info);
558656d417b1SBrian Haley 	else
558775538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_UP, &info);
558856d417b1SBrian Haley }
558956d417b1SBrian Haley 
559056d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
559156d417b1SBrian Haley {
559256d417b1SBrian Haley 	struct net_device *dev;
559356d417b1SBrian Haley 	struct inet6_dev *idev;
559456d417b1SBrian Haley 
559503e4deffSKefeng Wang 	for_each_netdev(net, dev) {
559656d417b1SBrian Haley 		idev = __in6_dev_get(dev);
559756d417b1SBrian Haley 		if (idev) {
559856d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
559956d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
560056d417b1SBrian Haley 			if (changed)
560156d417b1SBrian Haley 				dev_disable_change(idev);
560256d417b1SBrian Haley 		}
560356d417b1SBrian Haley 	}
560456d417b1SBrian Haley }
560556d417b1SBrian Haley 
5606013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
560756d417b1SBrian Haley {
560856d417b1SBrian Haley 	struct net *net;
5609013d97e9SFrancesco Ruggeri 	int old;
5610013d97e9SFrancesco Ruggeri 
5611013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
5612013d97e9SFrancesco Ruggeri 		return restart_syscall();
561356d417b1SBrian Haley 
561456d417b1SBrian Haley 	net = (struct net *)table->extra2;
5615013d97e9SFrancesco Ruggeri 	old = *p;
5616013d97e9SFrancesco Ruggeri 	*p = newf;
561756d417b1SBrian Haley 
5618013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5619013d97e9SFrancesco Ruggeri 		rtnl_unlock();
562056d417b1SBrian Haley 		return 0;
562188af182eSEric W. Biederman 	}
562256d417b1SBrian Haley 
562356d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
562456d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
562556d417b1SBrian Haley 		addrconf_disable_change(net, newf);
5626013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
562756d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
562856d417b1SBrian Haley 
562956d417b1SBrian Haley 	rtnl_unlock();
563056d417b1SBrian Haley 	return 0;
563156d417b1SBrian Haley }
563256d417b1SBrian Haley 
563356d417b1SBrian Haley static
5634fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
563556d417b1SBrian Haley 			    void __user *buffer, size_t *lenp, loff_t *ppos)
563656d417b1SBrian Haley {
563756d417b1SBrian Haley 	int *valp = ctl->data;
563856d417b1SBrian Haley 	int val = *valp;
563988af182eSEric W. Biederman 	loff_t pos = *ppos;
5640fe2c6338SJoe Perches 	struct ctl_table lctl;
564156d417b1SBrian Haley 	int ret;
564256d417b1SBrian Haley 
5643013d97e9SFrancesco Ruggeri 	/*
5644013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
5645013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5646013d97e9SFrancesco Ruggeri 	 */
5647013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5648013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5649013d97e9SFrancesco Ruggeri 
5650013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
565156d417b1SBrian Haley 
565256d417b1SBrian Haley 	if (write)
565356d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
565488af182eSEric W. Biederman 	if (ret)
565588af182eSEric W. Biederman 		*ppos = pos;
565656d417b1SBrian Haley 	return ret;
565756d417b1SBrian Haley }
565856d417b1SBrian Haley 
5659c92d5491Sstephen hemminger static
5660c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5661c92d5491Sstephen hemminger 			      void __user *buffer, size_t *lenp, loff_t *ppos)
5662c92d5491Sstephen hemminger {
5663c92d5491Sstephen hemminger 	int *valp = ctl->data;
5664c92d5491Sstephen hemminger 	int ret;
5665c92d5491Sstephen hemminger 	int old, new;
5666c92d5491Sstephen hemminger 
5667c92d5491Sstephen hemminger 	old = *valp;
5668c92d5491Sstephen hemminger 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5669c92d5491Sstephen hemminger 	new = *valp;
5670c92d5491Sstephen hemminger 
5671c92d5491Sstephen hemminger 	if (write && old != new) {
5672c92d5491Sstephen hemminger 		struct net *net = ctl->extra2;
5673c92d5491Sstephen hemminger 
5674c92d5491Sstephen hemminger 		if (!rtnl_trylock())
5675c92d5491Sstephen hemminger 			return restart_syscall();
5676c92d5491Sstephen hemminger 
5677c92d5491Sstephen hemminger 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5678c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5679c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_DEFAULT,
5680c92d5491Sstephen hemminger 						     net->ipv6.devconf_dflt);
5681c92d5491Sstephen hemminger 		else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5682c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5683c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_ALL,
5684c92d5491Sstephen hemminger 						     net->ipv6.devconf_all);
5685c92d5491Sstephen hemminger 		else {
5686c92d5491Sstephen hemminger 			struct inet6_dev *idev = ctl->extra1;
5687c92d5491Sstephen hemminger 
5688c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5689c92d5491Sstephen hemminger 						     idev->dev->ifindex,
5690c92d5491Sstephen hemminger 						     &idev->cnf);
5691c92d5491Sstephen hemminger 		}
5692c92d5491Sstephen hemminger 		rtnl_unlock();
5693c92d5491Sstephen hemminger 	}
5694c92d5491Sstephen hemminger 
5695c92d5491Sstephen hemminger 	return ret;
5696c92d5491Sstephen hemminger }
5697c92d5491Sstephen hemminger 
5698d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5699d35a00b8SFelix Jia 					 void __user *buffer, size_t *lenp,
5700d35a00b8SFelix Jia 					 loff_t *ppos)
5701d35a00b8SFelix Jia {
5702d35a00b8SFelix Jia 	int ret = 0;
5703d35a00b8SFelix Jia 	int new_val;
5704d35a00b8SFelix Jia 	struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5705d35a00b8SFelix Jia 	struct net *net = (struct net *)ctl->extra2;
5706d35a00b8SFelix Jia 
57078c171d6cSFelix Jia 	if (!rtnl_trylock())
57088c171d6cSFelix Jia 		return restart_syscall();
57098c171d6cSFelix Jia 
5710d35a00b8SFelix Jia 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5711d35a00b8SFelix Jia 
5712d35a00b8SFelix Jia 	if (write) {
5713d35a00b8SFelix Jia 		new_val = *((int *)ctl->data);
5714d35a00b8SFelix Jia 
57158c171d6cSFelix Jia 		if (check_addr_gen_mode(new_val) < 0) {
57168c171d6cSFelix Jia 			ret = -EINVAL;
57178c171d6cSFelix Jia 			goto out;
57188c171d6cSFelix Jia 		}
5719d35a00b8SFelix Jia 
5720d35a00b8SFelix Jia 		/* request for default */
5721d35a00b8SFelix Jia 		if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5722d35a00b8SFelix Jia 			ipv6_devconf_dflt.addr_gen_mode = new_val;
5723d35a00b8SFelix Jia 
5724d35a00b8SFelix Jia 		/* request for individual net device */
5725d35a00b8SFelix Jia 		} else {
5726d35a00b8SFelix Jia 			if (!idev)
57278c171d6cSFelix Jia 				goto out;
5728d35a00b8SFelix Jia 
57298c171d6cSFelix Jia 			if (check_stable_privacy(idev, net, new_val) < 0) {
57308c171d6cSFelix Jia 				ret = -EINVAL;
57318c171d6cSFelix Jia 				goto out;
57328c171d6cSFelix Jia 			}
5733d35a00b8SFelix Jia 
5734d35a00b8SFelix Jia 			if (idev->cnf.addr_gen_mode != new_val) {
5735d35a00b8SFelix Jia 				idev->cnf.addr_gen_mode = new_val;
5736d35a00b8SFelix Jia 				addrconf_dev_config(idev->dev);
57378c171d6cSFelix Jia 			}
57388c171d6cSFelix Jia 		}
57398c171d6cSFelix Jia 	}
57408c171d6cSFelix Jia 
57418c171d6cSFelix Jia out:
5742d35a00b8SFelix Jia 	rtnl_unlock();
5743d35a00b8SFelix Jia 
5744d35a00b8SFelix Jia 	return ret;
5745d35a00b8SFelix Jia }
5746d35a00b8SFelix Jia 
57473d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
57483d1bec99SHannes Frederic Sowa 					 void __user *buffer, size_t *lenp,
57493d1bec99SHannes Frederic Sowa 					 loff_t *ppos)
57503d1bec99SHannes Frederic Sowa {
57513d1bec99SHannes Frederic Sowa 	int err;
57523d1bec99SHannes Frederic Sowa 	struct in6_addr addr;
57533d1bec99SHannes Frederic Sowa 	char str[IPV6_MAX_STRLEN];
57543d1bec99SHannes Frederic Sowa 	struct ctl_table lctl = *ctl;
5755622c81d5SHannes Frederic Sowa 	struct net *net = ctl->extra2;
57563d1bec99SHannes Frederic Sowa 	struct ipv6_stable_secret *secret = ctl->data;
57573d1bec99SHannes Frederic Sowa 
5758622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5759622c81d5SHannes Frederic Sowa 		return -EIO;
5760622c81d5SHannes Frederic Sowa 
57613d1bec99SHannes Frederic Sowa 	lctl.maxlen = IPV6_MAX_STRLEN;
57623d1bec99SHannes Frederic Sowa 	lctl.data = str;
57633d1bec99SHannes Frederic Sowa 
57643d1bec99SHannes Frederic Sowa 	if (!rtnl_trylock())
57653d1bec99SHannes Frederic Sowa 		return restart_syscall();
57663d1bec99SHannes Frederic Sowa 
57673d1bec99SHannes Frederic Sowa 	if (!write && !secret->initialized) {
57683d1bec99SHannes Frederic Sowa 		err = -EIO;
57693d1bec99SHannes Frederic Sowa 		goto out;
57703d1bec99SHannes Frederic Sowa 	}
57713d1bec99SHannes Frederic Sowa 
57725449a5caSWANG Cong 	err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
57733d1bec99SHannes Frederic Sowa 	if (err >= sizeof(str)) {
57743d1bec99SHannes Frederic Sowa 		err = -EIO;
57753d1bec99SHannes Frederic Sowa 		goto out;
57763d1bec99SHannes Frederic Sowa 	}
57773d1bec99SHannes Frederic Sowa 
57783d1bec99SHannes Frederic Sowa 	err = proc_dostring(&lctl, write, buffer, lenp, ppos);
57793d1bec99SHannes Frederic Sowa 	if (err || !write)
57803d1bec99SHannes Frederic Sowa 		goto out;
57813d1bec99SHannes Frederic Sowa 
57823d1bec99SHannes Frederic Sowa 	if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
57833d1bec99SHannes Frederic Sowa 		err = -EIO;
57843d1bec99SHannes Frederic Sowa 		goto out;
57853d1bec99SHannes Frederic Sowa 	}
57863d1bec99SHannes Frederic Sowa 
57873d1bec99SHannes Frederic Sowa 	secret->initialized = true;
57883d1bec99SHannes Frederic Sowa 	secret->secret = addr;
57893d1bec99SHannes Frederic Sowa 
5790622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5791622c81d5SHannes Frederic Sowa 		struct net_device *dev;
5792622c81d5SHannes Frederic Sowa 
5793622c81d5SHannes Frederic Sowa 		for_each_netdev(net, dev) {
5794622c81d5SHannes Frederic Sowa 			struct inet6_dev *idev = __in6_dev_get(dev);
5795622c81d5SHannes Frederic Sowa 
5796622c81d5SHannes Frederic Sowa 			if (idev) {
5797d35a00b8SFelix Jia 				idev->cnf.addr_gen_mode =
5798622c81d5SHannes Frederic Sowa 					IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5799622c81d5SHannes Frederic Sowa 			}
5800622c81d5SHannes Frederic Sowa 		}
5801622c81d5SHannes Frederic Sowa 	} else {
5802622c81d5SHannes Frederic Sowa 		struct inet6_dev *idev = ctl->extra1;
5803622c81d5SHannes Frederic Sowa 
5804d35a00b8SFelix Jia 		idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5805622c81d5SHannes Frederic Sowa 	}
5806622c81d5SHannes Frederic Sowa 
58073d1bec99SHannes Frederic Sowa out:
58083d1bec99SHannes Frederic Sowa 	rtnl_unlock();
58093d1bec99SHannes Frederic Sowa 
58103d1bec99SHannes Frederic Sowa 	return err;
58113d1bec99SHannes Frederic Sowa }
5812c92d5491Sstephen hemminger 
581335103d11SAndy Gospodarek static
581435103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
581535103d11SAndy Gospodarek 						int write,
581635103d11SAndy Gospodarek 						void __user *buffer,
581735103d11SAndy Gospodarek 						size_t *lenp,
581835103d11SAndy Gospodarek 						loff_t *ppos)
581935103d11SAndy Gospodarek {
582035103d11SAndy Gospodarek 	int *valp = ctl->data;
582135103d11SAndy Gospodarek 	int val = *valp;
582235103d11SAndy Gospodarek 	loff_t pos = *ppos;
582335103d11SAndy Gospodarek 	struct ctl_table lctl;
582435103d11SAndy Gospodarek 	int ret;
582535103d11SAndy Gospodarek 
582635103d11SAndy Gospodarek 	/* ctl->data points to idev->cnf.ignore_routes_when_linkdown
582735103d11SAndy Gospodarek 	 * we should not modify it until we get the rtnl lock.
582835103d11SAndy Gospodarek 	 */
582935103d11SAndy Gospodarek 	lctl = *ctl;
583035103d11SAndy Gospodarek 	lctl.data = &val;
583135103d11SAndy Gospodarek 
583235103d11SAndy Gospodarek 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
583335103d11SAndy Gospodarek 
583435103d11SAndy Gospodarek 	if (write)
583535103d11SAndy Gospodarek 		ret = addrconf_fixup_linkdown(ctl, valp, val);
583635103d11SAndy Gospodarek 	if (ret)
583735103d11SAndy Gospodarek 		*ppos = pos;
583835103d11SAndy Gospodarek 	return ret;
583935103d11SAndy Gospodarek }
584035103d11SAndy Gospodarek 
5841df789fe7SDavid Forster static
5842df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action)
5843df789fe7SDavid Forster {
5844df789fe7SDavid Forster 	if (rt) {
5845df789fe7SDavid Forster 		if (action)
5846df789fe7SDavid Forster 			rt->dst.flags |= DST_NOPOLICY;
5847df789fe7SDavid Forster 		else
5848df789fe7SDavid Forster 			rt->dst.flags &= ~DST_NOPOLICY;
5849df789fe7SDavid Forster 	}
5850df789fe7SDavid Forster }
5851df789fe7SDavid Forster 
5852df789fe7SDavid Forster static
5853df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
5854df789fe7SDavid Forster {
5855df789fe7SDavid Forster 	struct inet6_ifaddr *ifa;
5856df789fe7SDavid Forster 
5857df789fe7SDavid Forster 	read_lock_bh(&idev->lock);
5858df789fe7SDavid Forster 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
5859df789fe7SDavid Forster 		spin_lock(&ifa->lock);
5860df789fe7SDavid Forster 		if (ifa->rt) {
5861df789fe7SDavid Forster 			struct rt6_info *rt = ifa->rt;
5862df789fe7SDavid Forster 			struct fib6_table *table = rt->rt6i_table;
5863df789fe7SDavid Forster 			int cpu;
5864df789fe7SDavid Forster 
5865df789fe7SDavid Forster 			read_lock(&table->tb6_lock);
5866df789fe7SDavid Forster 			addrconf_set_nopolicy(ifa->rt, val);
5867df789fe7SDavid Forster 			if (rt->rt6i_pcpu) {
5868df789fe7SDavid Forster 				for_each_possible_cpu(cpu) {
5869df789fe7SDavid Forster 					struct rt6_info **rtp;
5870df789fe7SDavid Forster 
5871df789fe7SDavid Forster 					rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
5872df789fe7SDavid Forster 					addrconf_set_nopolicy(*rtp, val);
5873df789fe7SDavid Forster 				}
5874df789fe7SDavid Forster 			}
5875df789fe7SDavid Forster 			read_unlock(&table->tb6_lock);
5876df789fe7SDavid Forster 		}
5877df789fe7SDavid Forster 		spin_unlock(&ifa->lock);
5878df789fe7SDavid Forster 	}
5879df789fe7SDavid Forster 	read_unlock_bh(&idev->lock);
5880df789fe7SDavid Forster }
5881df789fe7SDavid Forster 
5882df789fe7SDavid Forster static
5883df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
5884df789fe7SDavid Forster {
5885df789fe7SDavid Forster 	struct inet6_dev *idev;
5886df789fe7SDavid Forster 	struct net *net;
5887df789fe7SDavid Forster 
5888df789fe7SDavid Forster 	if (!rtnl_trylock())
5889df789fe7SDavid Forster 		return restart_syscall();
5890df789fe7SDavid Forster 
5891df789fe7SDavid Forster 	*valp = val;
5892df789fe7SDavid Forster 
5893df789fe7SDavid Forster 	net = (struct net *)ctl->extra2;
5894df789fe7SDavid Forster 	if (valp == &net->ipv6.devconf_dflt->disable_policy) {
5895df789fe7SDavid Forster 		rtnl_unlock();
5896df789fe7SDavid Forster 		return 0;
5897df789fe7SDavid Forster 	}
5898df789fe7SDavid Forster 
5899df789fe7SDavid Forster 	if (valp == &net->ipv6.devconf_all->disable_policy)  {
5900df789fe7SDavid Forster 		struct net_device *dev;
5901df789fe7SDavid Forster 
5902df789fe7SDavid Forster 		for_each_netdev(net, dev) {
5903df789fe7SDavid Forster 			idev = __in6_dev_get(dev);
5904df789fe7SDavid Forster 			if (idev)
5905df789fe7SDavid Forster 				addrconf_disable_policy_idev(idev, val);
5906df789fe7SDavid Forster 		}
5907df789fe7SDavid Forster 	} else {
5908df789fe7SDavid Forster 		idev = (struct inet6_dev *)ctl->extra1;
5909df789fe7SDavid Forster 		addrconf_disable_policy_idev(idev, val);
5910df789fe7SDavid Forster 	}
5911df789fe7SDavid Forster 
5912df789fe7SDavid Forster 	rtnl_unlock();
5913df789fe7SDavid Forster 	return 0;
5914df789fe7SDavid Forster }
5915df789fe7SDavid Forster 
5916df789fe7SDavid Forster static
5917df789fe7SDavid Forster int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
5918df789fe7SDavid Forster 				   void __user *buffer, size_t *lenp,
5919df789fe7SDavid Forster 				   loff_t *ppos)
5920df789fe7SDavid Forster {
5921df789fe7SDavid Forster 	int *valp = ctl->data;
5922df789fe7SDavid Forster 	int val = *valp;
5923df789fe7SDavid Forster 	loff_t pos = *ppos;
5924df789fe7SDavid Forster 	struct ctl_table lctl;
5925df789fe7SDavid Forster 	int ret;
5926df789fe7SDavid Forster 
5927df789fe7SDavid Forster 	lctl = *ctl;
5928df789fe7SDavid Forster 	lctl.data = &val;
5929df789fe7SDavid Forster 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5930df789fe7SDavid Forster 
5931df789fe7SDavid Forster 	if (write && (*valp != val))
5932df789fe7SDavid Forster 		ret = addrconf_disable_policy(ctl, valp, val);
5933df789fe7SDavid Forster 
5934df789fe7SDavid Forster 	if (ret)
5935df789fe7SDavid Forster 		*ppos = pos;
5936df789fe7SDavid Forster 
5937df789fe7SDavid Forster 	return ret;
5938df789fe7SDavid Forster }
5939df789fe7SDavid Forster 
5940cb4a4c69SMaciej Żenczykowski static int minus_one = -1;
5941cb9e684eSMaciej Żenczykowski static const int one = 1;
5942cb9e684eSMaciej Żenczykowski static const int two_five_five = 255;
5943cb9e684eSMaciej Żenczykowski 
5944607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = {
59451da177e4SLinus Torvalds 	{
59461da177e4SLinus Torvalds 		.procname	= "forwarding",
59471da177e4SLinus Torvalds 		.data		= &ipv6_devconf.forwarding,
59481da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59491da177e4SLinus Torvalds 		.mode		= 0644,
59506d9f239aSAlexey Dobriyan 		.proc_handler	= addrconf_sysctl_forward,
59511da177e4SLinus Torvalds 	},
59521da177e4SLinus Torvalds 	{
59531da177e4SLinus Torvalds 		.procname	= "hop_limit",
59541da177e4SLinus Torvalds 		.data		= &ipv6_devconf.hop_limit,
59551da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59561da177e4SLinus Torvalds 		.mode		= 0644,
5957cb9e684eSMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
5958cb9e684eSMaciej Żenczykowski 		.extra1		= (void *)&one,
5959cb9e684eSMaciej Żenczykowski 		.extra2		= (void *)&two_five_five,
59601da177e4SLinus Torvalds 	},
59611da177e4SLinus Torvalds 	{
59621da177e4SLinus Torvalds 		.procname	= "mtu",
59631da177e4SLinus Torvalds 		.data		= &ipv6_devconf.mtu6,
59641da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59651da177e4SLinus Torvalds 		.mode		= 0644,
596677751427SMarcelo Leitner 		.proc_handler	= addrconf_sysctl_mtu,
59671da177e4SLinus Torvalds 	},
59681da177e4SLinus Torvalds 	{
59691da177e4SLinus Torvalds 		.procname	= "accept_ra",
59701da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_ra,
59711da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59721da177e4SLinus Torvalds 		.mode		= 0644,
59736d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
59741da177e4SLinus Torvalds 	},
59751da177e4SLinus Torvalds 	{
59761da177e4SLinus Torvalds 		.procname	= "accept_redirects",
59771da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_redirects,
59781da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59791da177e4SLinus Torvalds 		.mode		= 0644,
59806d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
59811da177e4SLinus Torvalds 	},
59821da177e4SLinus Torvalds 	{
59831da177e4SLinus Torvalds 		.procname	= "autoconf",
59841da177e4SLinus Torvalds 		.data		= &ipv6_devconf.autoconf,
59851da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59861da177e4SLinus Torvalds 		.mode		= 0644,
59876d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
59881da177e4SLinus Torvalds 	},
59891da177e4SLinus Torvalds 	{
59901da177e4SLinus Torvalds 		.procname	= "dad_transmits",
59911da177e4SLinus Torvalds 		.data		= &ipv6_devconf.dad_transmits,
59921da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
59931da177e4SLinus Torvalds 		.mode		= 0644,
59946d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
59951da177e4SLinus Torvalds 	},
59961da177e4SLinus Torvalds 	{
59971da177e4SLinus Torvalds 		.procname	= "router_solicitations",
59981da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicits,
59991da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60001da177e4SLinus Torvalds 		.mode		= 0644,
6001cb4a4c69SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_minmax,
6002cb4a4c69SMaciej Żenczykowski 		.extra1		= &minus_one,
60031da177e4SLinus Torvalds 	},
60041da177e4SLinus Torvalds 	{
60051da177e4SLinus Torvalds 		.procname	= "router_solicitation_interval",
60061da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_interval,
60071da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60081da177e4SLinus Torvalds 		.mode		= 0644,
60096d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
60101da177e4SLinus Torvalds 	},
60111da177e4SLinus Torvalds 	{
6012bd11f074SMaciej Żenczykowski 		.procname	= "router_solicitation_max_interval",
6013bd11f074SMaciej Żenczykowski 		.data		= &ipv6_devconf.rtr_solicit_max_interval,
6014bd11f074SMaciej Żenczykowski 		.maxlen		= sizeof(int),
6015bd11f074SMaciej Żenczykowski 		.mode		= 0644,
6016bd11f074SMaciej Żenczykowski 		.proc_handler	= proc_dointvec_jiffies,
6017bd11f074SMaciej Żenczykowski 	},
6018bd11f074SMaciej Żenczykowski 	{
60191da177e4SLinus Torvalds 		.procname	= "router_solicitation_delay",
60201da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_delay,
60211da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60221da177e4SLinus Torvalds 		.mode		= 0644,
60236d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
60241da177e4SLinus Torvalds 	},
60251da177e4SLinus Torvalds 	{
60261da177e4SLinus Torvalds 		.procname	= "force_mld_version",
60271da177e4SLinus Torvalds 		.data		= &ipv6_devconf.force_mld_version,
60281da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60291da177e4SLinus Torvalds 		.mode		= 0644,
60306d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60311da177e4SLinus Torvalds 	},
6032fc4eba58SHannes Frederic Sowa 	{
6033fc4eba58SHannes Frederic Sowa 		.procname	= "mldv1_unsolicited_report_interval",
6034fc4eba58SHannes Frederic Sowa 		.data		=
6035fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv1_unsolicited_report_interval,
6036fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6037fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
6038fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
6039fc4eba58SHannes Frederic Sowa 	},
6040fc4eba58SHannes Frederic Sowa 	{
6041fc4eba58SHannes Frederic Sowa 		.procname	= "mldv2_unsolicited_report_interval",
6042fc4eba58SHannes Frederic Sowa 		.data		=
6043fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv2_unsolicited_report_interval,
6044fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6045fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
6046fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
6047fc4eba58SHannes Frederic Sowa 	},
60481da177e4SLinus Torvalds 	{
60491da177e4SLinus Torvalds 		.procname	= "use_tempaddr",
60501da177e4SLinus Torvalds 		.data		= &ipv6_devconf.use_tempaddr,
60511da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60521da177e4SLinus Torvalds 		.mode		= 0644,
60536d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60541da177e4SLinus Torvalds 	},
60551da177e4SLinus Torvalds 	{
60561da177e4SLinus Torvalds 		.procname	= "temp_valid_lft",
60571da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_valid_lft,
60581da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60591da177e4SLinus Torvalds 		.mode		= 0644,
60606d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60611da177e4SLinus Torvalds 	},
60621da177e4SLinus Torvalds 	{
60631da177e4SLinus Torvalds 		.procname	= "temp_prefered_lft",
60641da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_prefered_lft,
60651da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60661da177e4SLinus Torvalds 		.mode		= 0644,
60676d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60681da177e4SLinus Torvalds 	},
60691da177e4SLinus Torvalds 	{
60701da177e4SLinus Torvalds 		.procname	= "regen_max_retry",
60711da177e4SLinus Torvalds 		.data		= &ipv6_devconf.regen_max_retry,
60721da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60731da177e4SLinus Torvalds 		.mode		= 0644,
60746d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60751da177e4SLinus Torvalds 	},
60761da177e4SLinus Torvalds 	{
60771da177e4SLinus Torvalds 		.procname	= "max_desync_factor",
60781da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_desync_factor,
60791da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60801da177e4SLinus Torvalds 		.mode		= 0644,
60816d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60821da177e4SLinus Torvalds 	},
60831da177e4SLinus Torvalds 	{
60841da177e4SLinus Torvalds 		.procname	= "max_addresses",
60851da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_addresses,
60861da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
60871da177e4SLinus Torvalds 		.mode		= 0644,
60886d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
60891da177e4SLinus Torvalds 	},
60901da177e4SLinus Torvalds 	{
609165f5c7c1SYOSHIFUJI Hideaki 		.procname	= "accept_ra_defrtr",
609265f5c7c1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_defrtr,
609365f5c7c1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
609465f5c7c1SYOSHIFUJI Hideaki 		.mode		= 0644,
60956d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
609665f5c7c1SYOSHIFUJI Hideaki 	},
609765f5c7c1SYOSHIFUJI Hideaki 	{
60988013d1d7SHangbin Liu 		.procname	= "accept_ra_min_hop_limit",
60998013d1d7SHangbin Liu 		.data		= &ipv6_devconf.accept_ra_min_hop_limit,
61008013d1d7SHangbin Liu 		.maxlen		= sizeof(int),
61018013d1d7SHangbin Liu 		.mode		= 0644,
61028013d1d7SHangbin Liu 		.proc_handler	= proc_dointvec,
61038013d1d7SHangbin Liu 	},
61048013d1d7SHangbin Liu 	{
6105c4fd30ebSYOSHIFUJI Hideaki 		.procname	= "accept_ra_pinfo",
6106c4fd30ebSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_pinfo,
6107c4fd30ebSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6108c4fd30ebSYOSHIFUJI Hideaki 		.mode		= 0644,
61096d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
6110c4fd30ebSYOSHIFUJI Hideaki 	},
6111930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
6112930d6ff2SYOSHIFUJI Hideaki 	{
6113930d6ff2SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rtr_pref",
6114930d6ff2SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rtr_pref,
6115930d6ff2SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6116930d6ff2SYOSHIFUJI Hideaki 		.mode		= 0644,
61176d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
6118930d6ff2SYOSHIFUJI Hideaki 	},
611952e16356SYOSHIFUJI Hideaki 	{
612052e16356SYOSHIFUJI Hideaki 		.procname	= "router_probe_interval",
612152e16356SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.rtr_probe_interval,
612252e16356SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
612352e16356SYOSHIFUJI Hideaki 		.mode		= 0644,
61246d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
612552e16356SYOSHIFUJI Hideaki 	},
6126fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
612709c884d4SYOSHIFUJI Hideaki 	{
6128*bbea124bSJoel Scherpelz 		.procname	= "accept_ra_rt_info_min_plen",
6129*bbea124bSJoel Scherpelz 		.data		= &ipv6_devconf.accept_ra_rt_info_min_plen,
6130*bbea124bSJoel Scherpelz 		.maxlen		= sizeof(int),
6131*bbea124bSJoel Scherpelz 		.mode		= 0644,
6132*bbea124bSJoel Scherpelz 		.proc_handler	= proc_dointvec,
6133*bbea124bSJoel Scherpelz 	},
6134*bbea124bSJoel Scherpelz 	{
613509c884d4SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rt_info_max_plen",
613609c884d4SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
613709c884d4SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
613809c884d4SYOSHIFUJI Hideaki 		.mode		= 0644,
61396d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
614009c884d4SYOSHIFUJI Hideaki 	},
614109c884d4SYOSHIFUJI Hideaki #endif
6142930d6ff2SYOSHIFUJI Hideaki #endif
6143c4fd30ebSYOSHIFUJI Hideaki 	{
6144fbea49e1SYOSHIFUJI Hideaki 		.procname	= "proxy_ndp",
6145fbea49e1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.proxy_ndp,
6146fbea49e1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6147fbea49e1SYOSHIFUJI Hideaki 		.mode		= 0644,
6148c92d5491Sstephen hemminger 		.proc_handler	= addrconf_sysctl_proxy_ndp,
6149fbea49e1SYOSHIFUJI Hideaki 	},
6150fbea49e1SYOSHIFUJI Hideaki 	{
61510bcbc926SYOSHIFUJI Hideaki 		.procname	= "accept_source_route",
61520bcbc926SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_source_route,
61530bcbc926SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
61540bcbc926SYOSHIFUJI Hideaki 		.mode		= 0644,
61556d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61560bcbc926SYOSHIFUJI Hideaki 	},
615795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
615895c385b4SNeil Horman 	{
615995c385b4SNeil Horman 		.procname	= "optimistic_dad",
616095c385b4SNeil Horman 		.data		= &ipv6_devconf.optimistic_dad,
616195c385b4SNeil Horman 		.maxlen		= sizeof(int),
616295c385b4SNeil Horman 		.mode		= 0644,
61636d9f239aSAlexey Dobriyan 		.proc_handler   = proc_dointvec,
616495c385b4SNeil Horman 	},
61657fd2561eSErik Kline 	{
61667fd2561eSErik Kline 		.procname	= "use_optimistic",
61677fd2561eSErik Kline 		.data		= &ipv6_devconf.use_optimistic,
61687fd2561eSErik Kline 		.maxlen		= sizeof(int),
61697fd2561eSErik Kline 		.mode		= 0644,
61707fd2561eSErik Kline 		.proc_handler	= proc_dointvec,
61717fd2561eSErik Kline 	},
617295c385b4SNeil Horman #endif
61737bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
61747bc570c8SYOSHIFUJI Hideaki 	{
61757bc570c8SYOSHIFUJI Hideaki 		.procname	= "mc_forwarding",
61767bc570c8SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.mc_forwarding,
61777bc570c8SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6178a4e6db07SDavid S. Miller 		.mode		= 0444,
61796d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61807bc570c8SYOSHIFUJI Hideaki 	},
61817bc570c8SYOSHIFUJI Hideaki #endif
61820bcbc926SYOSHIFUJI Hideaki 	{
6183778d80beSYOSHIFUJI Hideaki 		.procname	= "disable_ipv6",
6184778d80beSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.disable_ipv6,
6185778d80beSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
6186778d80beSYOSHIFUJI Hideaki 		.mode		= 0644,
618756d417b1SBrian Haley 		.proc_handler	= addrconf_sysctl_disable,
6188778d80beSYOSHIFUJI Hideaki 	},
6189778d80beSYOSHIFUJI Hideaki 	{
61901b34be74SYOSHIFUJI Hideaki 		.procname	= "accept_dad",
61911b34be74SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_dad,
61921b34be74SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
61931b34be74SYOSHIFUJI Hideaki 		.mode		= 0644,
61946d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
61951b34be74SYOSHIFUJI Hideaki 	},
61961b34be74SYOSHIFUJI Hideaki 	{
6197f7734fdfSOctavian Purdila 		.procname	= "force_tllao",
6198f7734fdfSOctavian Purdila 		.data		= &ipv6_devconf.force_tllao,
6199f7734fdfSOctavian Purdila 		.maxlen		= sizeof(int),
6200f7734fdfSOctavian Purdila 		.mode		= 0644,
6201f7734fdfSOctavian Purdila 		.proc_handler	= proc_dointvec
6202f7734fdfSOctavian Purdila 	},
6203f7734fdfSOctavian Purdila 	{
62045cb04436SHannes Frederic Sowa 		.procname	= "ndisc_notify",
62055cb04436SHannes Frederic Sowa 		.data		= &ipv6_devconf.ndisc_notify,
62065cb04436SHannes Frederic Sowa 		.maxlen		= sizeof(int),
62075cb04436SHannes Frederic Sowa 		.mode		= 0644,
62085cb04436SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
62095cb04436SHannes Frederic Sowa 	},
62105cb04436SHannes Frederic Sowa 	{
6211b800c3b9SHannes Frederic Sowa 		.procname	= "suppress_frag_ndisc",
6212b800c3b9SHannes Frederic Sowa 		.data		= &ipv6_devconf.suppress_frag_ndisc,
6213b800c3b9SHannes Frederic Sowa 		.maxlen		= sizeof(int),
6214b800c3b9SHannes Frederic Sowa 		.mode		= 0644,
6215b800c3b9SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
6216b800c3b9SHannes Frederic Sowa 	},
6217b800c3b9SHannes Frederic Sowa 	{
6218d9333196SBen Greear 		.procname	= "accept_ra_from_local",
6219d9333196SBen Greear 		.data		= &ipv6_devconf.accept_ra_from_local,
6220d9333196SBen Greear 		.maxlen		= sizeof(int),
6221d9333196SBen Greear 		.mode		= 0644,
6222d9333196SBen Greear 		.proc_handler	= proc_dointvec,
6223d9333196SBen Greear 	},
6224d9333196SBen Greear 	{
6225c2943f14SHarout Hedeshian 		.procname	= "accept_ra_mtu",
6226c2943f14SHarout Hedeshian 		.data		= &ipv6_devconf.accept_ra_mtu,
6227c2943f14SHarout Hedeshian 		.maxlen		= sizeof(int),
6228c2943f14SHarout Hedeshian 		.mode		= 0644,
6229c2943f14SHarout Hedeshian 		.proc_handler	= proc_dointvec,
6230c2943f14SHarout Hedeshian 	},
6231c2943f14SHarout Hedeshian 	{
62323d1bec99SHannes Frederic Sowa 		.procname	= "stable_secret",
62333d1bec99SHannes Frederic Sowa 		.data		= &ipv6_devconf.stable_secret,
62343d1bec99SHannes Frederic Sowa 		.maxlen		= IPV6_MAX_STRLEN,
62353d1bec99SHannes Frederic Sowa 		.mode		= 0600,
62363d1bec99SHannes Frederic Sowa 		.proc_handler	= addrconf_sysctl_stable_secret,
62373d1bec99SHannes Frederic Sowa 	},
62383d1bec99SHannes Frederic Sowa 	{
62393985e8a3SErik Kline 		.procname	= "use_oif_addrs_only",
62403985e8a3SErik Kline 		.data		= &ipv6_devconf.use_oif_addrs_only,
62413985e8a3SErik Kline 		.maxlen		= sizeof(int),
62423985e8a3SErik Kline 		.mode		= 0644,
62433985e8a3SErik Kline 		.proc_handler	= proc_dointvec,
624435103d11SAndy Gospodarek 	},
624535103d11SAndy Gospodarek 	{
624635103d11SAndy Gospodarek 		.procname	= "ignore_routes_with_linkdown",
624735103d11SAndy Gospodarek 		.data		= &ipv6_devconf.ignore_routes_with_linkdown,
624835103d11SAndy Gospodarek 		.maxlen		= sizeof(int),
624935103d11SAndy Gospodarek 		.mode		= 0644,
625035103d11SAndy Gospodarek 		.proc_handler	= addrconf_sysctl_ignore_routes_with_linkdown,
62513985e8a3SErik Kline 	},
62523985e8a3SErik Kline 	{
6253abbc3043SJohannes Berg 		.procname	= "drop_unicast_in_l2_multicast",
6254abbc3043SJohannes Berg 		.data		= &ipv6_devconf.drop_unicast_in_l2_multicast,
6255abbc3043SJohannes Berg 		.maxlen		= sizeof(int),
6256abbc3043SJohannes Berg 		.mode		= 0644,
6257abbc3043SJohannes Berg 		.proc_handler	= proc_dointvec,
6258abbc3043SJohannes Berg 	},
6259abbc3043SJohannes Berg 	{
62607a02bf89SJohannes Berg 		.procname	= "drop_unsolicited_na",
62617a02bf89SJohannes Berg 		.data		= &ipv6_devconf.drop_unsolicited_na,
62627a02bf89SJohannes Berg 		.maxlen		= sizeof(int),
62637a02bf89SJohannes Berg 		.mode		= 0644,
62647a02bf89SJohannes Berg 		.proc_handler	= proc_dointvec,
62657a02bf89SJohannes Berg 	},
62667a02bf89SJohannes Berg 	{
6267f1705ec1SDavid Ahern 		.procname	= "keep_addr_on_down",
6268f1705ec1SDavid Ahern 		.data		= &ipv6_devconf.keep_addr_on_down,
6269f1705ec1SDavid Ahern 		.maxlen		= sizeof(int),
6270f1705ec1SDavid Ahern 		.mode		= 0644,
6271f1705ec1SDavid Ahern 		.proc_handler	= proc_dointvec,
6272f1705ec1SDavid Ahern 
6273f1705ec1SDavid Ahern 	},
6274f1705ec1SDavid Ahern 	{
62751ababebaSDavid Lebrun 		.procname	= "seg6_enabled",
62761ababebaSDavid Lebrun 		.data		= &ipv6_devconf.seg6_enabled,
62771ababebaSDavid Lebrun 		.maxlen		= sizeof(int),
62781ababebaSDavid Lebrun 		.mode		= 0644,
62791ababebaSDavid Lebrun 		.proc_handler	= proc_dointvec,
62801ababebaSDavid Lebrun 	},
6281bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
6282bf355b8dSDavid Lebrun 	{
6283bf355b8dSDavid Lebrun 		.procname	= "seg6_require_hmac",
6284bf355b8dSDavid Lebrun 		.data		= &ipv6_devconf.seg6_require_hmac,
6285bf355b8dSDavid Lebrun 		.maxlen		= sizeof(int),
6286bf355b8dSDavid Lebrun 		.mode		= 0644,
6287bf355b8dSDavid Lebrun 		.proc_handler	= proc_dointvec,
6288bf355b8dSDavid Lebrun 	},
6289bf355b8dSDavid Lebrun #endif
62901ababebaSDavid Lebrun 	{
6291adc176c5SErik Nordmark 		.procname       = "enhanced_dad",
6292adc176c5SErik Nordmark 		.data           = &ipv6_devconf.enhanced_dad,
6293adc176c5SErik Nordmark 		.maxlen         = sizeof(int),
6294adc176c5SErik Nordmark 		.mode           = 0644,
6295adc176c5SErik Nordmark 		.proc_handler   = proc_dointvec,
6296adc176c5SErik Nordmark 	},
6297adc176c5SErik Nordmark 	{
6298d35a00b8SFelix Jia 		.procname		= "addr_gen_mode",
6299d35a00b8SFelix Jia 		.data			= &ipv6_devconf.addr_gen_mode,
6300d35a00b8SFelix Jia 		.maxlen			= sizeof(int),
6301d35a00b8SFelix Jia 		.mode			= 0644,
6302d35a00b8SFelix Jia 		.proc_handler	= addrconf_sysctl_addr_gen_mode,
6303d35a00b8SFelix Jia 	},
6304d35a00b8SFelix Jia 	{
6305df789fe7SDavid Forster 		.procname       = "disable_policy",
6306df789fe7SDavid Forster 		.data           = &ipv6_devconf.disable_policy,
6307df789fe7SDavid Forster 		.maxlen         = sizeof(int),
6308df789fe7SDavid Forster 		.mode           = 0644,
6309df789fe7SDavid Forster 		.proc_handler   = addrconf_sysctl_disable_policy,
6310df789fe7SDavid Forster 	},
6311df789fe7SDavid Forster 	{
6312f8572d8fSEric W. Biederman 		/* sentinel */
63131da177e4SLinus Torvalds 	}
63141da177e4SLinus Torvalds };
63151da177e4SLinus Torvalds 
6316bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6317f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
63181da177e4SLinus Torvalds {
631929c994e3SNicolas Dichtel 	int i, ifindex;
6320607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
63216105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
63221dab6222SPavel Emelyanov 
6323607ea7cdSKonstantin Khlebnikov 	table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6324607ea7cdSKonstantin Khlebnikov 	if (!table)
6325f68635e6SPavel Emelyanov 		goto out;
6326f68635e6SPavel Emelyanov 
6327607ea7cdSKonstantin Khlebnikov 	for (i = 0; table[i].data; i++) {
6328607ea7cdSKonstantin Khlebnikov 		table[i].data += (char *)p - (char *)&ipv6_devconf;
6329cb9e684eSMaciej Żenczykowski 		/* If one of these is already set, then it is not safe to
6330cb9e684eSMaciej Żenczykowski 		 * overwrite either of them: this makes proc_dointvec_minmax
6331cb9e684eSMaciej Żenczykowski 		 * usable.
6332cb9e684eSMaciej Żenczykowski 		 */
6333cb9e684eSMaciej Żenczykowski 		if (!table[i].extra1 && !table[i].extra2) {
6334607ea7cdSKonstantin Khlebnikov 			table[i].extra1 = idev; /* embedded; no ref */
6335607ea7cdSKonstantin Khlebnikov 			table[i].extra2 = net;
63361da177e4SLinus Torvalds 		}
6337cb9e684eSMaciej Żenczykowski 	}
63381da177e4SLinus Torvalds 
63396105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
63401da177e4SLinus Torvalds 
6341607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = register_net_sysctl(net, path, table);
6342607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
63436105e293SEric W. Biederman 		goto free;
6344f68635e6SPavel Emelyanov 
634529c994e3SNicolas Dichtel 	if (!strcmp(dev_name, "all"))
634629c994e3SNicolas Dichtel 		ifindex = NETCONFA_IFINDEX_ALL;
634729c994e3SNicolas Dichtel 	else if (!strcmp(dev_name, "default"))
634829c994e3SNicolas Dichtel 		ifindex = NETCONFA_IFINDEX_DEFAULT;
634929c994e3SNicolas Dichtel 	else
635029c994e3SNicolas Dichtel 		ifindex = idev->dev->ifindex;
635129c994e3SNicolas Dichtel 	inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
635295897312SPavel Emelyanov 	return 0;
63531da177e4SLinus Torvalds 
63541da177e4SLinus Torvalds free:
6355607ea7cdSKonstantin Khlebnikov 	kfree(table);
6356f68635e6SPavel Emelyanov out:
635795897312SPavel Emelyanov 	return -ENOBUFS;
63581da177e4SLinus Torvalds }
63591da177e4SLinus Torvalds 
6360408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6361f52295a9SPavel Emelyanov {
6362607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
6363f52295a9SPavel Emelyanov 
6364607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
6365408c4768SPavel Emelyanov 		return;
6366408c4768SPavel Emelyanov 
6367607ea7cdSKonstantin Khlebnikov 	table = p->sysctl_header->ctl_table_arg;
6368607ea7cdSKonstantin Khlebnikov 	unregister_net_sysctl_table(p->sysctl_header);
6369607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = NULL;
6370607ea7cdSKonstantin Khlebnikov 	kfree(table);
63711da177e4SLinus Torvalds }
6372408c4768SPavel Emelyanov 
6373a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev)
6374408c4768SPavel Emelyanov {
6375a317a2f1SWANG Cong 	int err;
6376a317a2f1SWANG Cong 
6377a317a2f1SWANG Cong 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
6378a317a2f1SWANG Cong 		return -EINVAL;
6379a317a2f1SWANG Cong 
6380a317a2f1SWANG Cong 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6381f8572d8fSEric W. Biederman 				    &ndisc_ifinfo_sysctl_change);
6382a317a2f1SWANG Cong 	if (err)
6383a317a2f1SWANG Cong 		return err;
6384a317a2f1SWANG Cong 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6385f8572d8fSEric W. Biederman 					 idev, &idev->cnf);
6386a317a2f1SWANG Cong 	if (err)
6387a317a2f1SWANG Cong 		neigh_sysctl_unregister(idev->nd_parms);
6388a317a2f1SWANG Cong 
6389a317a2f1SWANG Cong 	return err;
6390408c4768SPavel Emelyanov }
6391408c4768SPavel Emelyanov 
6392408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6393408c4768SPavel Emelyanov {
6394408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
6395408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
63961da177e4SLinus Torvalds }
63971da177e4SLinus Torvalds 
63981da177e4SLinus Torvalds 
63991da177e4SLinus Torvalds #endif
64001da177e4SLinus Torvalds 
64012c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
6402e0da5a48SPavel Emelyanov {
6403a79ca223SHong Zhiguo 	int err = -ENOMEM;
6404e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
6405e0da5a48SPavel Emelyanov 
6406a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
640763159f29SIan Morris 	if (!all)
6408e0da5a48SPavel Emelyanov 		goto err_alloc_all;
6409e0da5a48SPavel Emelyanov 
6410a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
641163159f29SIan Morris 	if (!dflt)
6412e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
6413a79ca223SHong Zhiguo 
641456d417b1SBrian Haley 	/* these will be inherited by all namespaces */
641556d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
641656d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6417e0da5a48SPavel Emelyanov 
64183d1bec99SHannes Frederic Sowa 	dflt->stable_secret.initialized = false;
64193d1bec99SHannes Frederic Sowa 	all->stable_secret.initialized = false;
64203d1bec99SHannes Frederic Sowa 
6421e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
6422e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
6423e0da5a48SPavel Emelyanov 
6424e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
6425f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
6426e0da5a48SPavel Emelyanov 	if (err < 0)
6427e0da5a48SPavel Emelyanov 		goto err_reg_all;
6428e0da5a48SPavel Emelyanov 
6429f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6430e0da5a48SPavel Emelyanov 	if (err < 0)
6431e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
6432e0da5a48SPavel Emelyanov #endif
6433e0da5a48SPavel Emelyanov 	return 0;
6434e0da5a48SPavel Emelyanov 
6435e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
6436e0da5a48SPavel Emelyanov err_reg_dflt:
6437e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
6438e0da5a48SPavel Emelyanov err_reg_all:
6439e0da5a48SPavel Emelyanov 	kfree(dflt);
6440e0da5a48SPavel Emelyanov #endif
6441e0da5a48SPavel Emelyanov err_alloc_dflt:
6442e0da5a48SPavel Emelyanov 	kfree(all);
6443e0da5a48SPavel Emelyanov err_alloc_all:
6444e0da5a48SPavel Emelyanov 	return err;
6445e0da5a48SPavel Emelyanov }
6446e0da5a48SPavel Emelyanov 
64472c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
6448e0da5a48SPavel Emelyanov {
6449e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
6450e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6451e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
6452e0da5a48SPavel Emelyanov #endif
6453e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_dflt);
6454e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_all);
6455e0da5a48SPavel Emelyanov }
6456e0da5a48SPavel Emelyanov 
6457e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
6458e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
6459e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
6460e0da5a48SPavel Emelyanov };
6461e0da5a48SPavel Emelyanov 
6462207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = {
6463b382b191SThomas Graf 	.family		  = AF_INET6,
6464b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
6465b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
646611b1f828SDaniel Borkmann 	.validate_link_af = inet6_validate_link_af,
6467f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
6468b382b191SThomas Graf };
6469b382b191SThomas Graf 
64701da177e4SLinus Torvalds /*
64711da177e4SLinus Torvalds  *	Init / cleanup code
64721da177e4SLinus Torvalds  */
64731da177e4SLinus Torvalds 
64741da177e4SLinus Torvalds int __init addrconf_init(void)
64751da177e4SLinus Torvalds {
6476a317a2f1SWANG Cong 	struct inet6_dev *idev;
6477c2e21293Sstephen hemminger 	int i, err;
64782a8cc6c8SYOSHIFUJI Hideaki 
6479e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
6480e21e8467SStephen Hemminger 	if (err < 0) {
6481f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
6482f3213831SJoe Perches 			__func__, err);
64832cc6d2bfSNeil Horman 		goto out;
64842a8cc6c8SYOSHIFUJI Hideaki 	}
64851da177e4SLinus Torvalds 
64862cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
64872cc6d2bfSNeil Horman 	if (err < 0)
64882cc6d2bfSNeil Horman 		goto out_addrlabel;
6489e0da5a48SPavel Emelyanov 
6490c15b1ccaSHannes Frederic Sowa 	addrconf_wq = create_workqueue("ipv6_addrconf");
6491c15b1ccaSHannes Frederic Sowa 	if (!addrconf_wq) {
6492c15b1ccaSHannes Frederic Sowa 		err = -ENOMEM;
6493c15b1ccaSHannes Frederic Sowa 		goto out_nowq;
6494c15b1ccaSHannes Frederic Sowa 	}
6495c15b1ccaSHannes Frederic Sowa 
64961da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
64971da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
64981da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
64991da177e4SLinus Torvalds 	 * to other devices which are up.
65001da177e4SLinus Torvalds 	 *
65011da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
65021da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
65031da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
65041da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
65051da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
65061da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
65071da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
65081da177e4SLinus Torvalds 	 *
65091da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
65101da177e4SLinus Torvalds 	 * loopback_dev first by hand.
65111da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
65121da177e4SLinus Torvalds 	 * device and it being up should be removed.
65131da177e4SLinus Torvalds 	 */
65141da177e4SLinus Torvalds 	rtnl_lock();
6515a317a2f1SWANG Cong 	idev = ipv6_add_dev(init_net.loopback_dev);
65161da177e4SLinus Torvalds 	rtnl_unlock();
6517a317a2f1SWANG Cong 	if (IS_ERR(idev)) {
6518a317a2f1SWANG Cong 		err = PTR_ERR(idev);
6519e0da5a48SPavel Emelyanov 		goto errlo;
6520a317a2f1SWANG Cong 	}
65211da177e4SLinus Torvalds 
6522c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
6523c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6524c2e21293Sstephen hemminger 
65251da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
65261da177e4SLinus Torvalds 
6527c15b1ccaSHannes Frederic Sowa 	addrconf_verify();
6528c127ea2cSThomas Graf 
65293678a9d8Sstephen hemminger 	rtnl_af_register(&inet6_ops);
6530b382b191SThomas Graf 
6531c7ac8679SGreg Rose 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6532c7ac8679SGreg Rose 			      NULL);
6533c127ea2cSThomas Graf 	if (err < 0)
6534c127ea2cSThomas Graf 		goto errout;
6535c127ea2cSThomas Graf 
6536c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
6537c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6538c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6539c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6540c7ac8679SGreg Rose 			inet6_dump_ifaddr, NULL);
6541c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6542c7ac8679SGreg Rose 			inet6_dump_ifmcaddr, NULL);
6543c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6544c7ac8679SGreg Rose 			inet6_dump_ifacaddr, NULL);
654576f8f6cbSNicolas Dichtel 	__rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
65467a674200SNicolas Dichtel 			inet6_netconf_dump_devconf, NULL);
6547c127ea2cSThomas Graf 
65482a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
65492a8cc6c8SYOSHIFUJI Hideaki 
65501da177e4SLinus Torvalds 	return 0;
6551c127ea2cSThomas Graf errout:
6552b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
6553c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
6554e0da5a48SPavel Emelyanov errlo:
6555c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
6556c15b1ccaSHannes Frederic Sowa out_nowq:
6557e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
65582cc6d2bfSNeil Horman out_addrlabel:
65592cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
65602cc6d2bfSNeil Horman out:
6561c127ea2cSThomas Graf 	return err;
65621da177e4SLinus Torvalds }
65631da177e4SLinus Torvalds 
656409f7709fSDaniel Lezcano void addrconf_cleanup(void)
65651da177e4SLinus Torvalds {
6566176c39afSDaniel Lezcano 	struct net_device *dev;
65671da177e4SLinus Torvalds 	int i;
65681da177e4SLinus Torvalds 
65691da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
6570e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
65712cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
65721da177e4SLinus Torvalds 
65731da177e4SLinus Torvalds 	rtnl_lock();
65741da177e4SLinus Torvalds 
6575b382b191SThomas Graf 	__rtnl_af_unregister(&inet6_ops);
6576b382b191SThomas Graf 
6577176c39afSDaniel Lezcano 	/* clean dev list */
6578176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
6579176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
6580176c39afSDaniel Lezcano 			continue;
6581176c39afSDaniel Lezcano 		addrconf_ifdown(dev, 1);
6582176c39afSDaniel Lezcano 	}
6583176c39afSDaniel Lezcano 	addrconf_ifdown(init_net.loopback_dev, 2);
6584176c39afSDaniel Lezcano 
65851da177e4SLinus Torvalds 	/*
65861da177e4SLinus Torvalds 	 *	Check hash table.
65871da177e4SLinus Torvalds 	 */
65885c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
6589c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
6590c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
65915c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
6592c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
65931da177e4SLinus Torvalds 	rtnl_unlock();
6594c15b1ccaSHannes Frederic Sowa 
6595c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
65961da177e4SLinus Torvalds }
6597