xref: /openbmc/linux/net/ipv6/addrconf.c (revision ba46ee4c0ed122fa14aa2f5d6994c166a01ae2c0)
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>
461da177e4SLinus Torvalds #include <linux/socket.h>
471da177e4SLinus Torvalds #include <linux/sockios.h>
481da177e4SLinus Torvalds #include <linux/net.h>
493d1bec99SHannes Frederic Sowa #include <linux/inet.h>
501da177e4SLinus Torvalds #include <linux/in6.h>
511da177e4SLinus Torvalds #include <linux/netdevice.h>
521823730fSThomas Graf #include <linux/if_addr.h>
531da177e4SLinus Torvalds #include <linux/if_arp.h>
541da177e4SLinus Torvalds #include <linux/if_arcnet.h>
551da177e4SLinus Torvalds #include <linux/if_infiniband.h>
561da177e4SLinus Torvalds #include <linux/route.h>
571da177e4SLinus Torvalds #include <linux/inetdevice.h>
581da177e4SLinus Torvalds #include <linux/init.h>
595a0e3ad6STejun Heo #include <linux/slab.h>
601da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
611da177e4SLinus Torvalds #include <linux/sysctl.h>
621da177e4SLinus Torvalds #endif
634fc268d2SRandy Dunlap #include <linux/capability.h>
641da177e4SLinus Torvalds #include <linux/delay.h>
651da177e4SLinus Torvalds #include <linux/notifier.h>
66543537bdSPaulo Marques #include <linux/string.h>
67ddbe5032SEric Dumazet #include <linux/hash.h>
681da177e4SLinus Torvalds 
69457c4cbcSEric W. Biederman #include <net/net_namespace.h>
701da177e4SLinus Torvalds #include <net/sock.h>
711da177e4SLinus Torvalds #include <net/snmp.h>
721da177e4SLinus Torvalds 
735241c2d7SAlexander Aring #include <net/6lowpan.h>
74cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h>
751da177e4SLinus Torvalds #include <net/ipv6.h>
761da177e4SLinus Torvalds #include <net/protocol.h>
771da177e4SLinus Torvalds #include <net/ndisc.h>
781da177e4SLinus Torvalds #include <net/ip6_route.h>
791da177e4SLinus Torvalds #include <net/addrconf.h>
801da177e4SLinus Torvalds #include <net/tcp.h>
811da177e4SLinus Torvalds #include <net/ip.h>
825d620266SThomas Graf #include <net/netlink.h>
83f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
84ca254490SDavid Ahern #include <net/l3mdev.h>
851da177e4SLinus Torvalds #include <linux/if_tunnel.h>
861da177e4SLinus Torvalds #include <linux/rtnetlink.h>
87f3a1bfb1SNicolas Dichtel #include <linux/netconf.h>
881da177e4SLinus Torvalds #include <linux/random.h>
89e21e8467SStephen Hemminger #include <linux/uaccess.h>
907f7d9a6bSHerbert Xu #include <asm/unaligned.h>
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds #include <linux/proc_fs.h>
931da177e4SLinus Torvalds #include <linux/seq_file.h>
94bc3b2d7fSPaul Gortmaker #include <linux/export.h>
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /* Set to 3 to get tracing... */
971da177e4SLinus Torvalds #define ACONF_DEBUG 2
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
100ba3542e1Sdingtianhong #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
1011da177e4SLinus Torvalds #else
102ba3542e1Sdingtianhong #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
1031da177e4SLinus Torvalds #endif
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
10618a31e1eSThomas Graf 
1073d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \
1083d1bec99SHannes Frederic Sowa 	sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
1093d1bec99SHannes Frederic Sowa 
11018a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp)
11118a31e1eSThomas Graf {
11218a31e1eSThomas Graf 	return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
11318a31e1eSThomas Graf }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
116a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev);
117408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
118408c4768SPavel Emelyanov #else
119a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev)
120408c4768SPavel Emelyanov {
121a317a2f1SWANG Cong 	return 0;
122408c4768SPavel Emelyanov }
123408c4768SPavel Emelyanov 
124408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
125408c4768SPavel Emelyanov {
126408c4768SPavel Emelyanov }
1271da177e4SLinus Torvalds #endif
1281da177e4SLinus Torvalds 
129eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev);
130eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1311da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data);
1321da177e4SLinus Torvalds 
1331b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
1341da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev);
135622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr,
136622c81d5SHannes Frederic Sowa 					u8 dad_count,
137622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  *	Configured unicast address hash table
1411da177e4SLinus Torvalds  */
142c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
1435c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock);
1441da177e4SLinus Torvalds 
145c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void);
146c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void);
147c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *);
1481da177e4SLinus Torvalds 
149c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq;
150c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1531da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1541da177e4SLinus Torvalds 
15593d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev,
15675c78500SMoni Shoua 				 unsigned long event);
1571da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1581da177e4SLinus Torvalds 
15921caa662SRomain Kuntz static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
16021caa662SRomain Kuntz 						  int plen,
16121caa662SRomain Kuntz 						  const struct net_device *dev,
16221caa662SRomain Kuntz 						  u32 flags, u32 noflags);
16321caa662SRomain Kuntz 
164cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp);
165c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w);
1661da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
167c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1681da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1691da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1701da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1731da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1743e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
17506bfe655SDaniel Lezcano 			       struct net_device *dev);
1761da177e4SLinus Torvalds 
177888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1781da177e4SLinus Torvalds 	.forwarding		= 0,
1791da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1801da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1811da177e4SLinus Torvalds 	.accept_ra		= 1,
1821da177e4SLinus Torvalds 	.accept_redirects	= 1,
1831da177e4SLinus Torvalds 	.autoconf		= 1,
1841da177e4SLinus Torvalds 	.force_mld_version	= 0,
185fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
186fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
1871da177e4SLinus Torvalds 	.dad_transmits		= 1,
1881da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1891da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1901da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1911da177e4SLinus Torvalds 	.use_tempaddr		= 0,
1921da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1931da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1941da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1951da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1961da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
19765f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
198d9333196SBen Greear 	.accept_ra_from_local	= 0,
1998013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
200c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
201930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
202930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
20352e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
20409c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
20509c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
20609c884d4SYOSHIFUJI Hideaki #endif
207930d6ff2SYOSHIFUJI Hideaki #endif
208fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2090bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
210778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2111b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
212b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
213c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2143d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2153d1bec99SHannes Frederic Sowa 		.initialized = false,
2163985e8a3SErik Kline 	},
2173985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
21835103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
219f1705ec1SDavid Ahern 	.keep_addr_on_down	= 0,
2201da177e4SLinus Torvalds };
2211da177e4SLinus Torvalds 
222ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2231da177e4SLinus Torvalds 	.forwarding		= 0,
2241da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2251da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2261da177e4SLinus Torvalds 	.accept_ra		= 1,
2271da177e4SLinus Torvalds 	.accept_redirects	= 1,
2281da177e4SLinus Torvalds 	.autoconf		= 1,
229fc4eba58SHannes Frederic Sowa 	.force_mld_version	= 0,
230fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
231fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2321da177e4SLinus Torvalds 	.dad_transmits		= 1,
2331da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2341da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
2351da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2361da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2371da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2381da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2391da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2401da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2411da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
24265f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
243d9333196SBen Greear 	.accept_ra_from_local	= 0,
2448013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
245c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
246930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
247930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
24852e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
24909c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
25009c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
25109c884d4SYOSHIFUJI Hideaki #endif
252930d6ff2SYOSHIFUJI Hideaki #endif
253fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2540bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
255778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2561b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
257b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
258c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2593d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2603d1bec99SHannes Frederic Sowa 		.initialized = false,
2613d1bec99SHannes Frederic Sowa 	},
2623985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
26335103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
264f1705ec1SDavid Ahern 	.keep_addr_on_down	= 0,
2651da177e4SLinus Torvalds };
2661da177e4SLinus Torvalds 
267f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
26805297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev)
269f24e3d65SMitsuru Chinen {
27005297949SDavid S. Miller 	return !qdisc_tx_is_noop(dev);
271f24e3d65SMitsuru Chinen }
272f24e3d65SMitsuru Chinen 
273b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev)
2741da177e4SLinus Torvalds {
275b7b1bfceSHannes Frederic Sowa 	if (del_timer(&idev->rs_timer))
276b7b1bfceSHannes Frederic Sowa 		__in6_dev_put(idev);
277b7b1bfceSHannes Frederic Sowa }
278b7b1bfceSHannes Frederic Sowa 
279c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
280b7b1bfceSHannes Frederic Sowa {
281c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
2821da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
285b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev,
2861da177e4SLinus Torvalds 				  unsigned long when)
2871da177e4SLinus Torvalds {
288b7b1bfceSHannes Frederic Sowa 	if (!timer_pending(&idev->rs_timer))
289b7b1bfceSHannes Frederic Sowa 		in6_dev_hold(idev);
290b7b1bfceSHannes Frederic Sowa 	mod_timer(&idev->rs_timer, jiffies + when);
2911da177e4SLinus Torvalds }
292b7b1bfceSHannes Frederic Sowa 
293c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
294c15b1ccaSHannes Frederic Sowa 				   unsigned long delay)
295b7b1bfceSHannes Frederic Sowa {
296c15b1ccaSHannes Frederic Sowa 	if (!delayed_work_pending(&ifp->dad_work))
297b7b1bfceSHannes Frederic Sowa 		in6_ifa_hold(ifp);
298c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3017f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
3027f7d9a6bSHerbert Xu {
303827da44cSJohn Stultz 	int i;
304827da44cSJohn Stultz 
305698365faSWANG Cong 	idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
306698365faSWANG Cong 	if (!idev->stats.ipv6)
3077f7d9a6bSHerbert Xu 		goto err_ip;
308827da44cSJohn Stultz 
309827da44cSJohn Stultz 	for_each_possible_cpu(i) {
310827da44cSJohn Stultz 		struct ipstats_mib *addrconf_stats;
311698365faSWANG Cong 		addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
312827da44cSJohn Stultz 		u64_stats_init(&addrconf_stats->syncp);
313827da44cSJohn Stultz 	}
314827da44cSJohn Stultz 
315827da44cSJohn Stultz 
316be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
317be281e55SEric Dumazet 					GFP_KERNEL);
318be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
3197f7d9a6bSHerbert Xu 		goto err_icmp;
320be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
321be281e55SEric Dumazet 					   GFP_KERNEL);
322be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
32314878f75SDavid L Stevens 		goto err_icmpmsg;
3247f7d9a6bSHerbert Xu 
3257f7d9a6bSHerbert Xu 	return 0;
3267f7d9a6bSHerbert Xu 
32714878f75SDavid L Stevens err_icmpmsg:
328be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3297f7d9a6bSHerbert Xu err_icmp:
330698365faSWANG Cong 	free_percpu(idev->stats.ipv6);
3317f7d9a6bSHerbert Xu err_ip:
332aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3337f7d9a6bSHerbert Xu }
3347f7d9a6bSHerbert Xu 
3351da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3361da177e4SLinus Torvalds {
3371da177e4SLinus Torvalds 	struct inet6_dev *ndev;
338a317a2f1SWANG Cong 	int err = -ENOMEM;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	ASSERT_RTNL();
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
343a317a2f1SWANG Cong 		return ERR_PTR(-EINVAL);
3441da177e4SLinus Torvalds 
345322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
34663159f29SIan Morris 	if (!ndev)
347a317a2f1SWANG Cong 		return ERR_PTR(err);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3501da177e4SLinus Torvalds 	ndev->dev = dev;
351502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
352b7b1bfceSHannes Frederic Sowa 	setup_timer(&ndev->rs_timer, addrconf_rs_timer,
353b7b1bfceSHannes Frederic Sowa 		    (unsigned long)ndev);
354c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3559b29c696SHannes Frederic Sowa 
3569b29c696SHannes Frederic Sowa 	if (ndev->cnf.stable_secret.initialized)
3579b29c696SHannes Frederic Sowa 		ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
3589b29c696SHannes Frederic Sowa 	else
3599b29c696SHannes Frederic Sowa 		ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64;
3609b29c696SHannes Frederic Sowa 
3611da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3621da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
36363159f29SIan Morris 	if (!ndev->nd_parms) {
3641da177e4SLinus Torvalds 		kfree(ndev);
365a317a2f1SWANG Cong 		return ERR_PTR(err);
3661da177e4SLinus Torvalds 	}
3670187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
3680187bdfbSBen Hutchings 		dev_disable_lro(dev);
3691da177e4SLinus Torvalds 	/* We refer to the device */
3701da177e4SLinus Torvalds 	dev_hold(dev);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
373ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
374f3213831SJoe Perches 			"%s: cannot allocate memory for statistics; dev=%s.\n",
375ba3542e1Sdingtianhong 			__func__, dev->name);
3761da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3778603e33dSRoy Li 		dev_put(dev);
3788603e33dSRoy Li 		kfree(ndev);
379a317a2f1SWANG Cong 		return ERR_PTR(err);
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
383ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
384f3213831SJoe Perches 			"%s: cannot create /proc/net/dev_snmp6/%s\n",
385ba3542e1Sdingtianhong 			__func__, dev->name);
386a317a2f1SWANG Cong 		goto err_release;
3871da177e4SLinus Torvalds 	}
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3901da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3911da177e4SLinus Torvalds 	 */
3921da177e4SLinus Torvalds 	in6_dev_hold(ndev);
3931da177e4SLinus Torvalds 
3941b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
3951b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
3961b34be74SYOSHIFUJI Hideaki 
39707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
398c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
399f3213831SJoe Perches 		pr_info("%s: Disabled Multicast RS\n", dev->name);
400c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
401c7dc89c0SFred L. Templin 	}
402b077d7abSYOSHIFUJI Hideaki #endif
403b077d7abSYOSHIFUJI Hideaki 
404372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
4051da177e4SLinus Torvalds 	setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
4061da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
4071da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4089625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4091da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4101da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4110be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
4121da177e4SLinus Torvalds 	} else {
4131da177e4SLinus Torvalds 		in6_dev_hold(ndev);
4141da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
4151da177e4SLinus Torvalds 	}
4165d9efa7eSDavid S. Miller 
417914faa14SDaniel Borkmann 	ndev->token = in6addr_any;
4181da177e4SLinus Torvalds 
419f24e3d65SMitsuru Chinen 	if (netif_running(dev) && addrconf_qdisc_ok(dev))
42053aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
42153aadcc9SHerbert Xu 
4221da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4231da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
424a317a2f1SWANG Cong 	err = addrconf_sysctl_register(ndev);
425a317a2f1SWANG Cong 	if (err) {
426a317a2f1SWANG Cong 		ipv6_mc_destroy_dev(ndev);
427a317a2f1SWANG Cong 		del_timer(&ndev->regen_timer);
4282a189f9eSSabrina Dubroca 		snmp6_unregister_dev(ndev);
429a317a2f1SWANG Cong 		goto err_release;
430a317a2f1SWANG Cong 	}
43130c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
432cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip6_ptr, ndev);
433d88ae4ccSYOSHIFUJI Hideaki 
4342c5e8933SHannes Frederic Sowa 	/* Join interface-local all-node multicast group */
4352c5e8933SHannes Frederic Sowa 	ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4362c5e8933SHannes Frederic Sowa 
437d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
438f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
439d88ae4ccSYOSHIFUJI Hideaki 
440d6ddef9eSLi Wei 	/* Join all-router multicast group if forwarding is set */
4418b2aaedeSLi Wei 	if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
442d6ddef9eSLi Wei 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
443d6ddef9eSLi Wei 
4441da177e4SLinus Torvalds 	return ndev;
445a317a2f1SWANG Cong 
446a317a2f1SWANG Cong err_release:
447a317a2f1SWANG Cong 	neigh_parms_release(&nd_tbl, ndev->nd_parms);
448a317a2f1SWANG Cong 	ndev->dead = 1;
449a317a2f1SWANG Cong 	in6_dev_finish_destroy(ndev);
450a317a2f1SWANG Cong 	return ERR_PTR(err);
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
453cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	struct inet6_dev *idev;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	ASSERT_RTNL();
4581da177e4SLinus Torvalds 
459e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
460e21e8467SStephen Hemminger 	if (!idev) {
461e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
462a317a2f1SWANG Cong 		if (IS_ERR(idev))
4631da177e4SLinus Torvalds 			return NULL;
4641da177e4SLinus Torvalds 	}
465c5e33bddSYOSHIFUJI Hideaki 
4661da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4671da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4681da177e4SLinus Torvalds 	return idev;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
471f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
472f3a1bfb1SNicolas Dichtel {
473f3a1bfb1SNicolas Dichtel 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
474f3a1bfb1SNicolas Dichtel 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
475136ba622SZhang Shengju 	bool all = false;
476f3a1bfb1SNicolas Dichtel 
477136ba622SZhang Shengju 	if (type == NETCONFA_ALL)
478136ba622SZhang Shengju 		all = true;
479136ba622SZhang Shengju 
480136ba622SZhang Shengju 	if (all || type == NETCONFA_FORWARDING)
481f3a1bfb1SNicolas Dichtel 		size += nla_total_size(4);
482b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
483136ba622SZhang Shengju 	if (all || type == NETCONFA_MC_FORWARDING)
484d67b8c61SNicolas Dichtel 		size += nla_total_size(4);
485b1afce95SDavid S. Miller #endif
486136ba622SZhang Shengju 	if (all || type == NETCONFA_PROXY_NEIGH)
487c92d5491Sstephen hemminger 		size += nla_total_size(4);
488f3a1bfb1SNicolas Dichtel 
489136ba622SZhang Shengju 	if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
49035103d11SAndy Gospodarek 		size += nla_total_size(4);
49135103d11SAndy Gospodarek 
492f3a1bfb1SNicolas Dichtel 	return size;
493f3a1bfb1SNicolas Dichtel }
494f3a1bfb1SNicolas Dichtel 
495f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
496f3a1bfb1SNicolas Dichtel 				      struct ipv6_devconf *devconf, u32 portid,
497f3a1bfb1SNicolas Dichtel 				      u32 seq, int event, unsigned int flags,
498f3a1bfb1SNicolas Dichtel 				      int type)
499f3a1bfb1SNicolas Dichtel {
500f3a1bfb1SNicolas Dichtel 	struct nlmsghdr  *nlh;
501f3a1bfb1SNicolas Dichtel 	struct netconfmsg *ncm;
502136ba622SZhang Shengju 	bool all = false;
503f3a1bfb1SNicolas Dichtel 
504f3a1bfb1SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
505f3a1bfb1SNicolas Dichtel 			flags);
50663159f29SIan Morris 	if (!nlh)
507f3a1bfb1SNicolas Dichtel 		return -EMSGSIZE;
508f3a1bfb1SNicolas Dichtel 
509136ba622SZhang Shengju 	if (type == NETCONFA_ALL)
510136ba622SZhang Shengju 		all = true;
511136ba622SZhang Shengju 
512f3a1bfb1SNicolas Dichtel 	ncm = nlmsg_data(nlh);
513f3a1bfb1SNicolas Dichtel 	ncm->ncm_family = AF_INET6;
514f3a1bfb1SNicolas Dichtel 
515f3a1bfb1SNicolas Dichtel 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
516f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
517f3a1bfb1SNicolas Dichtel 
518136ba622SZhang Shengju 	if ((all || type == NETCONFA_FORWARDING) &&
519f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
520f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
521b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
522136ba622SZhang Shengju 	if ((all || type == NETCONFA_MC_FORWARDING) &&
523d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
524d67b8c61SNicolas Dichtel 			devconf->mc_forwarding) < 0)
525d67b8c61SNicolas Dichtel 		goto nla_put_failure;
526b1afce95SDavid S. Miller #endif
527136ba622SZhang Shengju 	if ((all || type == NETCONFA_PROXY_NEIGH) &&
528c92d5491Sstephen hemminger 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
529c92d5491Sstephen hemminger 		goto nla_put_failure;
530c92d5491Sstephen hemminger 
531136ba622SZhang Shengju 	if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
53235103d11SAndy Gospodarek 	    nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
53335103d11SAndy Gospodarek 			devconf->ignore_routes_with_linkdown) < 0)
53435103d11SAndy Gospodarek 		goto nla_put_failure;
53535103d11SAndy Gospodarek 
536053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
537053c095aSJohannes Berg 	return 0;
538f3a1bfb1SNicolas Dichtel 
539f3a1bfb1SNicolas Dichtel nla_put_failure:
540f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
541f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
542f3a1bfb1SNicolas Dichtel }
543f3a1bfb1SNicolas Dichtel 
544d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
545f3a1bfb1SNicolas Dichtel 				  struct ipv6_devconf *devconf)
546f3a1bfb1SNicolas Dichtel {
547f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
548f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
549f3a1bfb1SNicolas Dichtel 
550f3a1bfb1SNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
55163159f29SIan Morris 	if (!skb)
552f3a1bfb1SNicolas Dichtel 		goto errout;
553f3a1bfb1SNicolas Dichtel 
554f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
555f3a1bfb1SNicolas Dichtel 					 RTM_NEWNETCONF, 0, type);
556f3a1bfb1SNicolas Dichtel 	if (err < 0) {
557f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
558f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
559f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
560f3a1bfb1SNicolas Dichtel 		goto errout;
561f3a1bfb1SNicolas Dichtel 	}
562f3a1bfb1SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
563f3a1bfb1SNicolas Dichtel 	return;
564f3a1bfb1SNicolas Dichtel errout:
565f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
566f3a1bfb1SNicolas Dichtel }
567f3a1bfb1SNicolas Dichtel 
56876f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
56976f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
57076f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
571c92d5491Sstephen hemminger 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
57235103d11SAndy Gospodarek 	[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]	= { .len = sizeof(int) },
57376f8f6cbSNicolas Dichtel };
57476f8f6cbSNicolas Dichtel 
57576f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
576661d2967SThomas Graf 				     struct nlmsghdr *nlh)
57776f8f6cbSNicolas Dichtel {
57876f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
57976f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
58076f8f6cbSNicolas Dichtel 	struct netconfmsg *ncm;
58176f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
58276f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
58376f8f6cbSNicolas Dichtel 	struct inet6_dev *in6_dev;
58476f8f6cbSNicolas Dichtel 	struct net_device *dev;
58576f8f6cbSNicolas Dichtel 	int ifindex;
58676f8f6cbSNicolas Dichtel 	int err;
58776f8f6cbSNicolas Dichtel 
58876f8f6cbSNicolas Dichtel 	err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
58976f8f6cbSNicolas Dichtel 			  devconf_ipv6_policy);
59076f8f6cbSNicolas Dichtel 	if (err < 0)
59176f8f6cbSNicolas Dichtel 		goto errout;
59276f8f6cbSNicolas Dichtel 
593a97eb33fSAnton Protopopov 	err = -EINVAL;
59476f8f6cbSNicolas Dichtel 	if (!tb[NETCONFA_IFINDEX])
59576f8f6cbSNicolas Dichtel 		goto errout;
59676f8f6cbSNicolas Dichtel 
59776f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
59876f8f6cbSNicolas Dichtel 	switch (ifindex) {
59976f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
60076f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
60176f8f6cbSNicolas Dichtel 		break;
60276f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
60376f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
60476f8f6cbSNicolas Dichtel 		break;
60576f8f6cbSNicolas Dichtel 	default:
60676f8f6cbSNicolas Dichtel 		dev = __dev_get_by_index(net, ifindex);
60763159f29SIan Morris 		if (!dev)
60876f8f6cbSNicolas Dichtel 			goto errout;
60976f8f6cbSNicolas Dichtel 		in6_dev = __in6_dev_get(dev);
61063159f29SIan Morris 		if (!in6_dev)
61176f8f6cbSNicolas Dichtel 			goto errout;
61276f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
61376f8f6cbSNicolas Dichtel 		break;
61476f8f6cbSNicolas Dichtel 	}
61576f8f6cbSNicolas Dichtel 
61676f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
617136ba622SZhang Shengju 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
61863159f29SIan Morris 	if (!skb)
61976f8f6cbSNicolas Dichtel 		goto errout;
62076f8f6cbSNicolas Dichtel 
62176f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
62276f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
62376f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
624136ba622SZhang Shengju 					 NETCONFA_ALL);
62576f8f6cbSNicolas Dichtel 	if (err < 0) {
62676f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
62776f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
62876f8f6cbSNicolas Dichtel 		kfree_skb(skb);
62976f8f6cbSNicolas Dichtel 		goto errout;
63076f8f6cbSNicolas Dichtel 	}
63176f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
63276f8f6cbSNicolas Dichtel errout:
63376f8f6cbSNicolas Dichtel 	return err;
63476f8f6cbSNicolas Dichtel }
63576f8f6cbSNicolas Dichtel 
6367a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
6377a674200SNicolas Dichtel 				      struct netlink_callback *cb)
6387a674200SNicolas Dichtel {
6397a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
6407a674200SNicolas Dichtel 	int h, s_h;
6417a674200SNicolas Dichtel 	int idx, s_idx;
6427a674200SNicolas Dichtel 	struct net_device *dev;
6437a674200SNicolas Dichtel 	struct inet6_dev *idev;
6447a674200SNicolas Dichtel 	struct hlist_head *head;
6457a674200SNicolas Dichtel 
6467a674200SNicolas Dichtel 	s_h = cb->args[0];
6477a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
6487a674200SNicolas Dichtel 
6497a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6507a674200SNicolas Dichtel 		idx = 0;
6517a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
6527a674200SNicolas Dichtel 		rcu_read_lock();
65363998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
65463998ac2SNicolas Dichtel 			  net->dev_base_seq;
6557a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
6567a674200SNicolas Dichtel 			if (idx < s_idx)
6577a674200SNicolas Dichtel 				goto cont;
6587a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
6597a674200SNicolas Dichtel 			if (!idev)
6607a674200SNicolas Dichtel 				goto cont;
6617a674200SNicolas Dichtel 
6627a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
6637a674200SNicolas Dichtel 						       &idev->cnf,
6647a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
6657a674200SNicolas Dichtel 						       cb->nlh->nlmsg_seq,
6667a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
6677a674200SNicolas Dichtel 						       NLM_F_MULTI,
668136ba622SZhang Shengju 						       NETCONFA_ALL) < 0) {
6697a674200SNicolas Dichtel 				rcu_read_unlock();
6707a674200SNicolas Dichtel 				goto done;
6717a674200SNicolas Dichtel 			}
67263998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
6737a674200SNicolas Dichtel cont:
6747a674200SNicolas Dichtel 			idx++;
6757a674200SNicolas Dichtel 		}
6767a674200SNicolas Dichtel 		rcu_read_unlock();
6777a674200SNicolas Dichtel 	}
6787a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
6797a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
6807a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
6817a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6827a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6837a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
684136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
6857a674200SNicolas Dichtel 			goto done;
6867a674200SNicolas Dichtel 		else
6877a674200SNicolas Dichtel 			h++;
6887a674200SNicolas Dichtel 	}
6897a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
6907a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
6917a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
6927a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6937a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6947a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
695136ba622SZhang Shengju 					       NETCONFA_ALL) < 0)
6967a674200SNicolas Dichtel 			goto done;
6977a674200SNicolas Dichtel 		else
6987a674200SNicolas Dichtel 			h++;
6997a674200SNicolas Dichtel 	}
7007a674200SNicolas Dichtel done:
7017a674200SNicolas Dichtel 	cb->args[0] = h;
7027a674200SNicolas Dichtel 	cb->args[1] = idx;
7037a674200SNicolas Dichtel 
7047a674200SNicolas Dichtel 	return skb->len;
7057a674200SNicolas Dichtel }
7067a674200SNicolas Dichtel 
7071da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
7081da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
7091da177e4SLinus Torvalds {
7101da177e4SLinus Torvalds 	struct net_device *dev;
7111da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	if (!idev)
7141da177e4SLinus Torvalds 		return;
7151da177e4SLinus Torvalds 	dev = idev->dev;
7160187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
7170187bdfbSBen Hutchings 		dev_disable_lro(dev);
7181a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
7192c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
720f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
7212c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
7222c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
7232c5e8933SHannes Frederic Sowa 		} else {
724f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
7252c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
7262c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
7272c5e8933SHannes Frederic Sowa 		}
7281da177e4SLinus Torvalds 	}
729502a2ffdSstephen hemminger 
730502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
7312c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
7322c12a74cSMichal Wrobel 			continue;
7331da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
7341da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
7351da177e4SLinus Torvalds 		else
7361da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
7371da177e4SLinus Torvalds 	}
738f3a1bfb1SNicolas Dichtel 	inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
739f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 
743e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
7441da177e4SLinus Torvalds {
7451da177e4SLinus Torvalds 	struct net_device *dev;
7461da177e4SLinus Torvalds 	struct inet6_dev *idev;
7471da177e4SLinus Torvalds 
7484acd4945SBen Hutchings 	for_each_netdev(net, dev) {
7491da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
7501da177e4SLinus Torvalds 		if (idev) {
751e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
752e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
7531da177e4SLinus Torvalds 			if (changed)
7541da177e4SLinus Torvalds 				dev_forward_change(idev);
7551da177e4SLinus Torvalds 		}
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds }
758c8fecf22SPavel Emelyanov 
759013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
760c8fecf22SPavel Emelyanov {
761bff16c2fSPavel Emelyanov 	struct net *net;
762013d97e9SFrancesco Ruggeri 	int old;
763013d97e9SFrancesco Ruggeri 
764013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
765013d97e9SFrancesco Ruggeri 		return restart_syscall();
766bff16c2fSPavel Emelyanov 
767bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
768013d97e9SFrancesco Ruggeri 	old = *p;
769013d97e9SFrancesco Ruggeri 	*p = newf;
770c8fecf22SPavel Emelyanov 
771013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
772f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
773f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
774f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
775f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
776013d97e9SFrancesco Ruggeri 		rtnl_unlock();
777013d97e9SFrancesco Ruggeri 		return 0;
77888af182eSEric W. Biederman 	}
779b325fddbSStephen Hemminger 
780e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
781e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
782e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
783f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
784f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
785f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
786f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
787013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
788c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
7890187bdfbSBen Hutchings 	rtnl_unlock();
790c8fecf22SPavel Emelyanov 
791013d97e9SFrancesco Ruggeri 	if (newf)
7927b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
793b325fddbSStephen Hemminger 	return 1;
794c8fecf22SPavel Emelyanov }
79535103d11SAndy Gospodarek 
79635103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf)
79735103d11SAndy Gospodarek {
79835103d11SAndy Gospodarek 	struct net_device *dev;
79935103d11SAndy Gospodarek 	struct inet6_dev *idev;
80035103d11SAndy Gospodarek 
80135103d11SAndy Gospodarek 	for_each_netdev(net, dev) {
80235103d11SAndy Gospodarek 		idev = __in6_dev_get(dev);
80335103d11SAndy Gospodarek 		if (idev) {
80435103d11SAndy Gospodarek 			int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
80535103d11SAndy Gospodarek 
80635103d11SAndy Gospodarek 			idev->cnf.ignore_routes_with_linkdown = newf;
80735103d11SAndy Gospodarek 			if (changed)
80835103d11SAndy Gospodarek 				inet6_netconf_notify_devconf(dev_net(dev),
80935103d11SAndy Gospodarek 							     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
81035103d11SAndy Gospodarek 							     dev->ifindex,
81135103d11SAndy Gospodarek 							     &idev->cnf);
81235103d11SAndy Gospodarek 		}
81335103d11SAndy Gospodarek 	}
81435103d11SAndy Gospodarek }
81535103d11SAndy Gospodarek 
81635103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
81735103d11SAndy Gospodarek {
81835103d11SAndy Gospodarek 	struct net *net;
81935103d11SAndy Gospodarek 	int old;
82035103d11SAndy Gospodarek 
82135103d11SAndy Gospodarek 	if (!rtnl_trylock())
82235103d11SAndy Gospodarek 		return restart_syscall();
82335103d11SAndy Gospodarek 
82435103d11SAndy Gospodarek 	net = (struct net *)table->extra2;
82535103d11SAndy Gospodarek 	old = *p;
82635103d11SAndy Gospodarek 	*p = newf;
82735103d11SAndy Gospodarek 
82835103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
82935103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
83035103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
83135103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
83235103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_DEFAULT,
83335103d11SAndy Gospodarek 						     net->ipv6.devconf_dflt);
83435103d11SAndy Gospodarek 		rtnl_unlock();
83535103d11SAndy Gospodarek 		return 0;
83635103d11SAndy Gospodarek 	}
83735103d11SAndy Gospodarek 
83835103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
83935103d11SAndy Gospodarek 		net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
84035103d11SAndy Gospodarek 		addrconf_linkdown_change(net, newf);
84135103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
84235103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
84335103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
84435103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_ALL,
84535103d11SAndy Gospodarek 						     net->ipv6.devconf_all);
84635103d11SAndy Gospodarek 	}
84735103d11SAndy Gospodarek 	rtnl_unlock();
84835103d11SAndy Gospodarek 
84935103d11SAndy Gospodarek 	return 1;
85035103d11SAndy Gospodarek }
85135103d11SAndy Gospodarek 
8521da177e4SLinus Torvalds #endif
8531da177e4SLinus Torvalds 
8545c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
8551da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
8561da177e4SLinus Torvalds {
857c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
858547b792cSIlpo Järvinen 
8591da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
86091df42beSJoe Perches 	pr_debug("%s\n", __func__);
8611da177e4SLinus Torvalds #endif
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
8641da177e4SLinus Torvalds 
865c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
866c15b1ccaSHannes Frederic Sowa 		pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
867c15b1ccaSHannes Frederic Sowa 			  ifp);
8681da177e4SLinus Torvalds 
869e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
870f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
8711da177e4SLinus Torvalds 		return;
8721da177e4SLinus Torvalds 	}
87394e187c0SAmerigo Wang 	ip6_rt_put(ifp->rt);
8741da177e4SLinus Torvalds 
875e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
878e55ffac6SBrian Haley static void
879e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
880e55ffac6SBrian Haley {
881502a2ffdSstephen hemminger 	struct list_head *p;
8828a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
883e55ffac6SBrian Haley 
884e55ffac6SBrian Haley 	/*
885e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
886e55ffac6SBrian Haley 	 * global before linklocal.
887e55ffac6SBrian Haley 	 */
888502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
889502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
890502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
8918a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
892e55ffac6SBrian Haley 			break;
893e55ffac6SBrian Haley 	}
894e55ffac6SBrian Haley 
895b54c9b98SDavid S. Miller 	list_add_tail(&ifp->if_list, p);
896e55ffac6SBrian Haley }
897e55ffac6SBrian Haley 
898ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr)
8993eb84f49SYOSHIFUJI Hideaki {
900ddbe5032SEric Dumazet 	return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
9013eb84f49SYOSHIFUJI Hideaki }
9023eb84f49SYOSHIFUJI Hideaki 
9031da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds static struct inet6_ifaddr *
9063f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
9073f8f5298SJiri Pirko 	      const struct in6_addr *peer_addr, int pfxlen,
9088a226b2cSJiri Benc 	      int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
9091da177e4SLinus Torvalds {
9101da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
9111da177e4SLinus Torvalds 	struct rt6_info *rt;
9123a88a81dSstephen hemminger 	unsigned int hash;
9131da177e4SLinus Torvalds 	int err = 0;
914d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
915d68b8270SYOSHIFUJI Hideaki 
916d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
917d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
918d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
919d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
920d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
9211da177e4SLinus Torvalds 
9228814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
9231da177e4SLinus Torvalds 	if (idev->dead) {
9241da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
9251da177e4SLinus Torvalds 		goto out2;
9261da177e4SLinus Torvalds 	}
9271da177e4SLinus Torvalds 
92856d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
9299bdd8d40SBrian Haley 		err = -EACCES;
9309bdd8d40SBrian Haley 		goto out2;
9319bdd8d40SBrian Haley 	}
9329bdd8d40SBrian Haley 
9335c578aedSstephen hemminger 	spin_lock(&addrconf_hash_lock);
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
936c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
937ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: already assigned\n");
9381da177e4SLinus Torvalds 		err = -EEXIST;
9391da177e4SLinus Torvalds 		goto out;
9401da177e4SLinus Torvalds 	}
9411da177e4SLinus Torvalds 
942322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
9431da177e4SLinus Torvalds 
94463159f29SIan Morris 	if (!ifa) {
945ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: malloc failed\n");
9461da177e4SLinus Torvalds 		err = -ENOBUFS;
9471da177e4SLinus Torvalds 		goto out;
9481da177e4SLinus Torvalds 	}
9491da177e4SLinus Torvalds 
9508f031519SDavid S. Miller 	rt = addrconf_dst_alloc(idev, addr, false);
9511da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
9521da177e4SLinus Torvalds 		err = PTR_ERR(rt);
9531da177e4SLinus Torvalds 		goto out;
9541da177e4SLinus Torvalds 	}
9551da177e4SLinus Torvalds 
956bba24896SJiri Pirko 	neigh_parms_data_state_setall(idev->nd_parms);
957bba24896SJiri Pirko 
9584e3fd7a0SAlexey Dobriyan 	ifa->addr = *addr;
9593f8f5298SJiri Pirko 	if (peer_addr)
9603f8f5298SJiri Pirko 		ifa->peer_addr = *peer_addr;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
963c15b1ccaSHannes Frederic Sowa 	INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
964c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
9651da177e4SLinus Torvalds 	ifa->scope = scope;
9661da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
9671da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
9688a226b2cSJiri Benc 	ifa->valid_lft = valid_lft;
9698a226b2cSJiri Benc 	ifa->prefered_lft = prefered_lft;
9701da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
971617fe29dSDaniel Borkmann 	ifa->tokenized = false;
9721da177e4SLinus Torvalds 
97357f5f544SKeir Fraser 	ifa->rt = rt;
97457f5f544SKeir Fraser 
9751da177e4SLinus Torvalds 	ifa->idev = idev;
9761da177e4SLinus Torvalds 	in6_dev_hold(idev);
9771da177e4SLinus Torvalds 	/* For caller */
9781da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	/* Add to big hash table */
981ddbe5032SEric Dumazet 	hash = inet6_addr_hash(addr);
9821da177e4SLinus Torvalds 
9835c578aedSstephen hemminger 	hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
9845c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	write_lock(&idev->lock);
9871da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
988e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
991372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
9921da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
9931da177e4SLinus Torvalds 	}
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
9961da177e4SLinus Torvalds 	write_unlock(&idev->lock);
9971da177e4SLinus Torvalds out2:
9988814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
9991da177e4SLinus Torvalds 
1000fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
1001f88c91ddSCong Wang 		inet6addr_notifier_call_chain(NETDEV_UP, ifa);
10021da177e4SLinus Torvalds 	else {
10031da177e4SLinus Torvalds 		kfree(ifa);
10041da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
10051da177e4SLinus Torvalds 	}
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	return ifa;
10081da177e4SLinus Torvalds out:
10095c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
10101da177e4SLinus Torvalds 	goto out2;
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds 
10135b84efecSThomas Haller enum cleanup_prefix_rt_t {
10145b84efecSThomas Haller 	CLEANUP_PREFIX_RT_NOP,    /* no cleanup action for prefix route */
10155b84efecSThomas Haller 	CLEANUP_PREFIX_RT_DEL,    /* delete the prefix route */
10165b84efecSThomas Haller 	CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
10175b84efecSThomas Haller };
10185b84efecSThomas Haller 
10195b84efecSThomas Haller /*
10205b84efecSThomas Haller  * Check, whether the prefix for ifp would still need a prefix route
10215b84efecSThomas Haller  * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
10225b84efecSThomas Haller  * constants.
10235b84efecSThomas Haller  *
10245b84efecSThomas Haller  * 1) we don't purge prefix if address was not permanent.
10255b84efecSThomas Haller  *    prefix is managed by its own lifetime.
10265b84efecSThomas Haller  * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
10275b84efecSThomas Haller  * 3) if there are no addresses, delete prefix.
10285b84efecSThomas Haller  * 4) if there are still other permanent address(es),
10295b84efecSThomas Haller  *    corresponding prefix is still permanent.
10305b84efecSThomas Haller  * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
10315b84efecSThomas Haller  *    don't purge the prefix, assume user space is managing it.
10325b84efecSThomas Haller  * 6) otherwise, update prefix lifetime to the
10335b84efecSThomas Haller  *    longest valid lifetime among the corresponding
10345b84efecSThomas Haller  *    addresses on the device.
10355b84efecSThomas Haller  *    Note: subsequent RA will update lifetime.
10365b84efecSThomas Haller  **/
10375b84efecSThomas Haller static enum cleanup_prefix_rt_t
10385b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
10395b84efecSThomas Haller {
10405b84efecSThomas Haller 	struct inet6_ifaddr *ifa;
10415b84efecSThomas Haller 	struct inet6_dev *idev = ifp->idev;
10425b84efecSThomas Haller 	unsigned long lifetime;
10435b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
10445b84efecSThomas Haller 
10455b84efecSThomas Haller 	*expires = jiffies;
10465b84efecSThomas Haller 
10475b84efecSThomas Haller 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
10485b84efecSThomas Haller 		if (ifa == ifp)
10495b84efecSThomas Haller 			continue;
10505b84efecSThomas Haller 		if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
10515b84efecSThomas Haller 				       ifp->prefix_len))
10525b84efecSThomas Haller 			continue;
10535b84efecSThomas Haller 		if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
10545b84efecSThomas Haller 			return CLEANUP_PREFIX_RT_NOP;
10555b84efecSThomas Haller 
10565b84efecSThomas Haller 		action = CLEANUP_PREFIX_RT_EXPIRE;
10575b84efecSThomas Haller 
10585b84efecSThomas Haller 		spin_lock(&ifa->lock);
10595b84efecSThomas Haller 
10605b84efecSThomas Haller 		lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
10615b84efecSThomas Haller 		/*
10625b84efecSThomas Haller 		 * Note: Because this address is
10635b84efecSThomas Haller 		 * not permanent, lifetime <
10645b84efecSThomas Haller 		 * LONG_MAX / HZ here.
10655b84efecSThomas Haller 		 */
10665b84efecSThomas Haller 		if (time_before(*expires, ifa->tstamp + lifetime * HZ))
10675b84efecSThomas Haller 			*expires = ifa->tstamp + lifetime * HZ;
10685b84efecSThomas Haller 		spin_unlock(&ifa->lock);
10695b84efecSThomas Haller 	}
10705b84efecSThomas Haller 
10715b84efecSThomas Haller 	return action;
10725b84efecSThomas Haller }
10735b84efecSThomas Haller 
10745b84efecSThomas Haller static void
10755b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
10765b84efecSThomas Haller {
10775b84efecSThomas Haller 	struct rt6_info *rt;
10785b84efecSThomas Haller 
10795b84efecSThomas Haller 	rt = addrconf_get_prefix_route(&ifp->addr,
10805b84efecSThomas Haller 				       ifp->prefix_len,
10815b84efecSThomas Haller 				       ifp->idev->dev,
10825b84efecSThomas Haller 				       0, RTF_GATEWAY | RTF_DEFAULT);
10835b84efecSThomas Haller 	if (rt) {
10845b84efecSThomas Haller 		if (del_rt)
10855b84efecSThomas Haller 			ip6_del_rt(rt);
10865b84efecSThomas Haller 		else {
10875b84efecSThomas Haller 			if (!(rt->rt6i_flags & RTF_EXPIRES))
10885b84efecSThomas Haller 				rt6_set_expires(rt, expires);
10895b84efecSThomas Haller 			ip6_rt_put(rt);
10905b84efecSThomas Haller 		}
10915b84efecSThomas Haller 	}
10925b84efecSThomas Haller }
10935b84efecSThomas Haller 
10945b84efecSThomas Haller 
10951da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
10981da177e4SLinus Torvalds {
10994c5ff6a6SHerbert Xu 	int state;
11005b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
11015b84efecSThomas Haller 	unsigned long expires;
11021da177e4SLinus Torvalds 
1103c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
1104c15b1ccaSHannes Frederic Sowa 
11058e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
11064c5ff6a6SHerbert Xu 	state = ifp->state;
1107e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
11088e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
11094c5ff6a6SHerbert Xu 
11104c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
11114c5ff6a6SHerbert Xu 		goto out;
11121da177e4SLinus Torvalds 
11135c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
11145c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
11155c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
11161da177e4SLinus Torvalds 
11175b84efecSThomas Haller 	write_lock_bh(&ifp->idev->lock);
11185d9efa7eSDavid S. Miller 
11191da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
1120372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
11211da177e4SLinus Torvalds 		if (ifp->ifpub) {
11221da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
11231da177e4SLinus Torvalds 			ifp->ifpub = NULL;
11241da177e4SLinus Torvalds 		}
11251da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
11261da177e4SLinus Torvalds 	}
11271da177e4SLinus Torvalds 
11285b84efecSThomas Haller 	if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
11295b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &expires);
11305b84efecSThomas Haller 
1131502a2ffdSstephen hemminger 	list_del_init(&ifp->if_list);
11321da177e4SLinus Torvalds 	__in6_ifa_put(ifp);
1133502a2ffdSstephen hemminger 
11345b84efecSThomas Haller 	write_unlock_bh(&ifp->idev->lock);
11351da177e4SLinus Torvalds 
1136c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
1137b2238566SAndrey Vagin 
11381da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
11391da177e4SLinus Torvalds 
1140f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
11411da177e4SLinus Torvalds 
11425b84efecSThomas Haller 	if (action != CLEANUP_PREFIX_RT_NOP) {
11435b84efecSThomas Haller 		cleanup_prefix_route(ifp, expires,
11445b84efecSThomas Haller 			action == CLEANUP_PREFIX_RT_DEL);
11451da177e4SLinus Torvalds 	}
11461da177e4SLinus Torvalds 
1147c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1148c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
11494c5ff6a6SHerbert Xu out:
11501da177e4SLinus Torvalds 	in6_ifa_put(ifp);
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
11541da177e4SLinus Torvalds {
11551da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
11561da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
115776f793e3SLorenzo Colitti 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1158eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
11591da177e4SLinus Torvalds 	int tmp_plen;
11601da177e4SLinus Torvalds 	int ret = 0;
116195c385b4SNeil Horman 	u32 addr_flags;
116276f793e3SLorenzo Colitti 	unsigned long now = jiffies;
11631da177e4SLinus Torvalds 
116453bd6749SJiri Pirko 	write_lock_bh(&idev->lock);
11651da177e4SLinus Torvalds 	if (ift) {
11661da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
11671da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
11681da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
11691da177e4SLinus Torvalds 		tmpaddr = &addr;
11701da177e4SLinus Torvalds 	} else {
11711da177e4SLinus Torvalds 		tmpaddr = NULL;
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds retry:
11741da177e4SLinus Torvalds 	in6_dev_hold(idev);
11751da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
117653bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1177f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
11781da177e4SLinus Torvalds 		in6_dev_put(idev);
11791da177e4SLinus Torvalds 		ret = -1;
11801da177e4SLinus Torvalds 		goto out;
11811da177e4SLinus Torvalds 	}
11821da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
11831da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
11841da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
11851da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
118653bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1187f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1188f3213831SJoe Perches 			__func__);
11891da177e4SLinus Torvalds 		in6_dev_put(idev);
11901da177e4SLinus Torvalds 		ret = -1;
11911da177e4SLinus Torvalds 		goto out;
11921da177e4SLinus Torvalds 	}
11931da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
11941da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1195eb7e0575SSorin Dumitru 	__ipv6_try_regen_rndid(idev, tmpaddr);
11961da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
119776f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
11981da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
11991da177e4SLinus Torvalds 			      ifp->valid_lft,
12007a876b0eSGlenn Wurster 			      idev->cnf.temp_valid_lft + age);
12011da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
12021da177e4SLinus Torvalds 				 ifp->prefered_lft,
12037a876b0eSGlenn Wurster 				 idev->cnf.temp_prefered_lft + age -
1204784e2710SBen Hutchings 				 idev->cnf.max_desync_factor);
12051da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
12061da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
12071da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
12081da177e4SLinus Torvalds 
1209eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1210eac55bf9SBenoit Boissinot 			idev->cnf.dad_transmits *
12111f9248e5SJiri Pirko 			NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
121253bd6749SJiri Pirko 	write_unlock_bh(&idev->lock);
121395c385b4SNeil Horman 
1214eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1215eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1216eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1217eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1218ecab6701SHeiner Kallweit 	 * Use age calculation as in addrconf_verify to avoid unnecessary
1219ecab6701SHeiner Kallweit 	 * temporary addresses being generated.
1220eac55bf9SBenoit Boissinot 	 */
1221ecab6701SHeiner Kallweit 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1222ecab6701SHeiner Kallweit 	if (tmp_prefered_lft <= regen_advance + age) {
1223eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1224eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1225eac55bf9SBenoit Boissinot 		ret = -1;
1226eac55bf9SBenoit Boissinot 		goto out;
1227eac55bf9SBenoit Boissinot 	}
1228eac55bf9SBenoit Boissinot 
122995c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
123095c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
123195c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
123295c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
123395c385b4SNeil Horman 
12344b08a8f1SHannes Frederic Sowa 	ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
12358a226b2cSJiri Benc 			    ipv6_addr_scope(&addr), addr_flags,
12364b08a8f1SHannes Frederic Sowa 			    tmp_valid_lft, tmp_prefered_lft);
12374b08a8f1SHannes Frederic Sowa 	if (IS_ERR(ift)) {
12381da177e4SLinus Torvalds 		in6_ifa_put(ifp);
12391da177e4SLinus Torvalds 		in6_dev_put(idev);
1240f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
12411da177e4SLinus Torvalds 		tmpaddr = &addr;
124253bd6749SJiri Pirko 		write_lock_bh(&idev->lock);
12431da177e4SLinus Torvalds 		goto retry;
12441da177e4SLinus Torvalds 	}
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
12471da177e4SLinus Torvalds 	ift->ifpub = ifp;
124876f793e3SLorenzo Colitti 	ift->cstamp = now;
12491da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
12501da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
12511da177e4SLinus Torvalds 
1252cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
12531da177e4SLinus Torvalds 	in6_ifa_put(ift);
12541da177e4SLinus Torvalds 	in6_dev_put(idev);
12551da177e4SLinus Torvalds out:
12561da177e4SLinus Torvalds 	return ret;
12571da177e4SLinus Torvalds }
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds /*
1260072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
12611da177e4SLinus Torvalds  */
1262a9b05723SYOSHIFUJI Hideaki enum {
1263a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1264a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1265a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1266a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1267a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1268a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1269a9b05723SYOSHIFUJI Hideaki #endif
1270a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1271a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1272a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1273a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1274a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
12757fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
12767fd2561eSErik Kline 	IPV6_SADDR_RULE_NOT_OPTIMISTIC,
12777fd2561eSErik Kline #endif
1278a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1279072047e4SYOSHIFUJI Hideaki };
1280072047e4SYOSHIFUJI Hideaki 
1281a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1282a9b05723SYOSHIFUJI Hideaki 	int			rule;
1283a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1284a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1285a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1286a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1287a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1288a9b05723SYOSHIFUJI Hideaki };
1289a9b05723SYOSHIFUJI Hideaki 
1290a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
12919acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1292a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1293a9b05723SYOSHIFUJI Hideaki 	int scope;
1294a9b05723SYOSHIFUJI Hideaki 	int label;
12957cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1296a9b05723SYOSHIFUJI Hideaki };
1297072047e4SYOSHIFUJI Hideaki 
1298b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
12991da177e4SLinus Torvalds {
130045bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1301072047e4SYOSHIFUJI Hideaki 		return 1;
1302072047e4SYOSHIFUJI Hideaki 	return 0;
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
13057fd2561eSErik Kline static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
13067fd2561eSErik Kline {
13077fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
13087fd2561eSErik Kline 	return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
13097fd2561eSErik Kline #else
13107fd2561eSErik Kline 	return false;
13117fd2561eSErik Kline #endif
13127fd2561eSErik Kline }
13137fd2561eSErik Kline 
13143de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
13153de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1316a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1317a9b05723SYOSHIFUJI Hideaki 			       int i)
1318a9b05723SYOSHIFUJI Hideaki {
1319a9b05723SYOSHIFUJI Hideaki 	int ret;
1320a9b05723SYOSHIFUJI Hideaki 
1321a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1322a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1323a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1324a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1325a9b05723SYOSHIFUJI Hideaki 			break;
1326a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1327a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1328a9b05723SYOSHIFUJI Hideaki 			break;
1329a9b05723SYOSHIFUJI Hideaki 		default:
1330a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1331a9b05723SYOSHIFUJI Hideaki 		}
1332a9b05723SYOSHIFUJI Hideaki 		goto out;
1333a9b05723SYOSHIFUJI Hideaki 	}
1334a9b05723SYOSHIFUJI Hideaki 
1335a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1336a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1337a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1338a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1339a9b05723SYOSHIFUJI Hideaki 		break;
1340a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1341a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1342a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1343a9b05723SYOSHIFUJI Hideaki 		break;
1344a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1345a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1346a9b05723SYOSHIFUJI Hideaki 		 *
1347a9b05723SYOSHIFUJI Hideaki 		 *      ret
1348a9b05723SYOSHIFUJI Hideaki 		 *       ^
1349a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1350a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1351a9b05723SYOSHIFUJI Hideaki 		 *       |
1352a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1353a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1354a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1355db9c7c39Sstephen hemminger 		 *  B-15 |    \        if scope is enough for destination.
1356a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1357a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1358a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1359a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1360a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1361a9b05723SYOSHIFUJI Hideaki 		 *
1362a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1363a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1364a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1365a9b05723SYOSHIFUJI Hideaki 		 */
1366a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1367a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1368a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1369a9b05723SYOSHIFUJI Hideaki 		else
1370a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1371a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1372a9b05723SYOSHIFUJI Hideaki 		break;
1373a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
13747fd2561eSErik Kline 	    {
1375a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
13767fd2561eSErik Kline 		u8 avoid = IFA_F_DEPRECATED;
13777fd2561eSErik Kline 
13787fd2561eSErik Kline 		if (!ipv6_use_optimistic_addr(score->ifa->idev))
13797fd2561eSErik Kline 			avoid |= IFA_F_OPTIMISTIC;
1380a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
13817fd2561eSErik Kline 		      !(score->ifa->flags & avoid);
1382a9b05723SYOSHIFUJI Hideaki 		break;
13837fd2561eSErik Kline 	    }
1384a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1385a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
13867cbca67cSYOSHIFUJI Hideaki 	    {
1387a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
13887cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
13897cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1390a9b05723SYOSHIFUJI Hideaki 		break;
13917cbca67cSYOSHIFUJI Hideaki 	    }
1392a9b05723SYOSHIFUJI Hideaki #endif
1393a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1394a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1395a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1396a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1397a9b05723SYOSHIFUJI Hideaki 		break;
1398a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1399a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
14003de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
14013de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1402a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1403a9b05723SYOSHIFUJI Hideaki 		break;
1404a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
14057cbca67cSYOSHIFUJI Hideaki 	    {
1406a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
140725985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1408a9b05723SYOSHIFUJI Hideaki 		 */
14097cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
14107cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
14117cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
14127cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1413a9b05723SYOSHIFUJI Hideaki 		break;
14147cbca67cSYOSHIFUJI Hideaki 	    }
1415a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1416a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1417a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1418a9b05723SYOSHIFUJI Hideaki 		 */
1419a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1420a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1421a9b05723SYOSHIFUJI Hideaki 		break;
1422a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1423a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
142491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
142591b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
142691b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
142791b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1428a9b05723SYOSHIFUJI Hideaki 		break;
14297fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
14307fd2561eSErik Kline 	case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
14317fd2561eSErik Kline 		/* Optimistic addresses still have lower precedence than other
14327fd2561eSErik Kline 		 * preferred addresses.
14337fd2561eSErik Kline 		 */
14347fd2561eSErik Kline 		ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
14357fd2561eSErik Kline 		break;
14367fd2561eSErik Kline #endif
1437a9b05723SYOSHIFUJI Hideaki 	default:
1438a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1439a9b05723SYOSHIFUJI Hideaki 	}
1440a9b05723SYOSHIFUJI Hideaki 
1441a9b05723SYOSHIFUJI Hideaki 	if (ret)
1442a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1443a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1444a9b05723SYOSHIFUJI Hideaki out:
1445a9b05723SYOSHIFUJI Hideaki 	return ret;
1446a9b05723SYOSHIFUJI Hideaki }
1447a9b05723SYOSHIFUJI Hideaki 
1448c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net,
14499131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_dst *dst,
14509131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct inet6_dev *idev,
1451c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_score *scores,
1452c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				int hiscore_idx)
14531da177e4SLinus Torvalds {
1454c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1455072047e4SYOSHIFUJI Hideaki 
14561da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1457502a2ffdSstephen hemminger 	list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1458a9b05723SYOSHIFUJI Hideaki 		int i;
1459072047e4SYOSHIFUJI Hideaki 
1460a9b05723SYOSHIFUJI Hideaki 		/*
14616b3ae80aSYOSHIFUJI Hideaki 		 * - Tentative Address (RFC2462 section 5.4)
14626b3ae80aSYOSHIFUJI Hideaki 		 *  - A tentative address is not considered
14636b3ae80aSYOSHIFUJI Hideaki 		 *    "assigned to an interface" in the traditional
146495c385b4SNeil Horman 		 *    sense, unless it is also flagged as optimistic.
14656b3ae80aSYOSHIFUJI Hideaki 		 * - Candidate Source Address (section 4)
1466072047e4SYOSHIFUJI Hideaki 		 *  - In any case, anycast addresses, multicast
1467072047e4SYOSHIFUJI Hideaki 		 *    addresses, and the unspecified address MUST
1468072047e4SYOSHIFUJI Hideaki 		 *    NOT be included in a candidate set.
1469072047e4SYOSHIFUJI Hideaki 		 */
1470a9b05723SYOSHIFUJI Hideaki 		if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1471a9b05723SYOSHIFUJI Hideaki 		    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
14726b3ae80aSYOSHIFUJI Hideaki 			continue;
1473a9b05723SYOSHIFUJI Hideaki 
1474a9b05723SYOSHIFUJI Hideaki 		score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1475a9b05723SYOSHIFUJI Hideaki 
1476a9b05723SYOSHIFUJI Hideaki 		if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1477a9b05723SYOSHIFUJI Hideaki 			     score->addr_type & IPV6_ADDR_MULTICAST)) {
1478ba7a46f1SJoe Perches 			net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
14799131f3deSYOSHIFUJI Hideaki/吉藤英明 					    idev->dev->name);
14801da177e4SLinus Torvalds 			continue;
1481072047e4SYOSHIFUJI Hideaki 		}
1482072047e4SYOSHIFUJI Hideaki 
1483a9b05723SYOSHIFUJI Hideaki 		score->rule = -1;
1484a9b05723SYOSHIFUJI Hideaki 		bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1485072047e4SYOSHIFUJI Hideaki 
1486a9b05723SYOSHIFUJI Hideaki 		for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1487a9b05723SYOSHIFUJI Hideaki 			int minihiscore, miniscore;
1488072047e4SYOSHIFUJI Hideaki 
14899131f3deSYOSHIFUJI Hideaki/吉藤英明 			minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
14909131f3deSYOSHIFUJI Hideaki/吉藤英明 			miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1491072047e4SYOSHIFUJI Hideaki 
1492a9b05723SYOSHIFUJI Hideaki 			if (minihiscore > miniscore) {
1493a9b05723SYOSHIFUJI Hideaki 				if (i == IPV6_SADDR_RULE_SCOPE &&
1494a9b05723SYOSHIFUJI Hideaki 				    score->scopedist > 0) {
1495a9b05723SYOSHIFUJI Hideaki 					/*
1496a9b05723SYOSHIFUJI Hideaki 					 * special case:
1497a9b05723SYOSHIFUJI Hideaki 					 * each remaining entry
1498a9b05723SYOSHIFUJI Hideaki 					 * has too small (not enough)
1499a9b05723SYOSHIFUJI Hideaki 					 * scope, because ifa entries
1500a9b05723SYOSHIFUJI Hideaki 					 * are sorted by their scope
1501a9b05723SYOSHIFUJI Hideaki 					 * values.
1502072047e4SYOSHIFUJI Hideaki 					 */
15039131f3deSYOSHIFUJI Hideaki/吉藤英明 					goto out;
1504072047e4SYOSHIFUJI Hideaki 				}
1505a9b05723SYOSHIFUJI Hideaki 				break;
1506a9b05723SYOSHIFUJI Hideaki 			} else if (minihiscore < miniscore) {
1507a9b05723SYOSHIFUJI Hideaki 				if (hiscore->ifa)
1508a9b05723SYOSHIFUJI Hideaki 					in6_ifa_put(hiscore->ifa);
150999cd07a5SJuha-Matti Tapio 
1510a9b05723SYOSHIFUJI Hideaki 				in6_ifa_hold(score->ifa);
1511072047e4SYOSHIFUJI Hideaki 
1512a0bffffcSIlpo Järvinen 				swap(hiscore, score);
1513c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				hiscore_idx = 1 - hiscore_idx;
1514a9b05723SYOSHIFUJI Hideaki 
1515a9b05723SYOSHIFUJI Hideaki 				/* restore our iterator */
1516a9b05723SYOSHIFUJI Hideaki 				score->ifa = hiscore->ifa;
1517a9b05723SYOSHIFUJI Hideaki 
1518a9b05723SYOSHIFUJI Hideaki 				break;
15191da177e4SLinus Torvalds 			}
1520a9b05723SYOSHIFUJI Hideaki 		}
1521a9b05723SYOSHIFUJI Hideaki 	}
15229131f3deSYOSHIFUJI Hideaki/吉藤英明 out:
15231da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
1524c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	return hiscore_idx;
15251da177e4SLinus Torvalds }
15269131f3deSYOSHIFUJI Hideaki/吉藤英明 
15279131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
15289131f3deSYOSHIFUJI Hideaki/吉藤英明 		       const struct in6_addr *daddr, unsigned int prefs,
15299131f3deSYOSHIFUJI Hideaki/吉藤英明 		       struct in6_addr *saddr)
15309131f3deSYOSHIFUJI Hideaki/吉藤英明 {
1531c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score scores[2], *hiscore;
15329131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_dst dst;
15339131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct inet6_dev *idev;
15349131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct net_device *dev;
15359131f3deSYOSHIFUJI Hideaki/吉藤英明 	int dst_type;
15369131f3deSYOSHIFUJI Hideaki/吉藤英明 	bool use_oif_addr = false;
1537c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	int hiscore_idx = 0;
15389131f3deSYOSHIFUJI Hideaki/吉藤英明 
15399131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst_type = __ipv6_addr_type(daddr);
15409131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.addr = daddr;
15419131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
15429131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.scope = __ipv6_addr_src_scope(dst_type);
15439131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
15449131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.prefs = prefs;
15459131f3deSYOSHIFUJI Hideaki/吉藤英明 
1546c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].rule = -1;
1547c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].ifa = NULL;
15489131f3deSYOSHIFUJI Hideaki/吉藤英明 
15499131f3deSYOSHIFUJI Hideaki/吉藤英明 	rcu_read_lock();
15509131f3deSYOSHIFUJI Hideaki/吉藤英明 
15519131f3deSYOSHIFUJI Hideaki/吉藤英明 	/* Candidate Source Address (section 4)
15529131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *  - multicast and link-local destination address,
15539131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    the set of candidate source address MUST only
15549131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
15559131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same link as the outgoing
15569131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.
15579131f3deSYOSHIFUJI Hideaki/吉藤英明 	 * (- For site-local destination addresses, the
15589131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    set of candidate source addresses MUST only
15599131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
15609131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same site as the outgoing
15619131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.)
15623985e8a3SErik Kline 	 *  - "It is RECOMMENDED that the candidate source addresses
15633985e8a3SErik Kline 	 *    be the set of unicast addresses assigned to the
15643985e8a3SErik Kline 	 *    interface that will be used to send to the destination
15653985e8a3SErik Kline 	 *    (the 'outgoing' interface)." (RFC 6724)
15669131f3deSYOSHIFUJI Hideaki/吉藤英明 	 */
15679131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (dst_dev) {
15689131f3deSYOSHIFUJI Hideaki/吉藤英明 		idev = __in6_dev_get(dst_dev);
15693985e8a3SErik Kline 		if ((dst_type & IPV6_ADDR_MULTICAST) ||
15703985e8a3SErik Kline 		    dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
15713985e8a3SErik Kline 		    (idev && idev->cnf.use_oif_addrs_only)) {
15729131f3deSYOSHIFUJI Hideaki/吉藤英明 			use_oif_addr = true;
15739131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
15749131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
15759131f3deSYOSHIFUJI Hideaki/吉藤英明 
15769131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (use_oif_addr) {
1577c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 		if (idev)
1578c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
15799131f3deSYOSHIFUJI Hideaki/吉藤英明 	} else {
15809131f3deSYOSHIFUJI Hideaki/吉藤英明 		for_each_netdev_rcu(net, dev) {
15819131f3deSYOSHIFUJI Hideaki/吉藤英明 			idev = __in6_dev_get(dev);
15829131f3deSYOSHIFUJI Hideaki/吉藤英明 			if (!idev)
15839131f3deSYOSHIFUJI Hideaki/吉藤英明 				continue;
1584c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
15859131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
15869131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
15878814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
15881da177e4SLinus Torvalds 
1589c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	hiscore = &scores[hiscore_idx];
1590a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1591072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
15921da177e4SLinus Torvalds 
15934e3fd7a0SAlexey Dobriyan 	*saddr = hiscore->ifa->addr;
1594a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1595072047e4SYOSHIFUJI Hideaki 	return 0;
15961da177e4SLinus Torvalds }
15975e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
15981da177e4SLinus Torvalds 
15998965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1600479840ffSJiri Pirko 		      u32 banned_flags)
1601b7b1bfceSHannes Frederic Sowa {
1602b7b1bfceSHannes Frederic Sowa 	struct inet6_ifaddr *ifp;
1603b7b1bfceSHannes Frederic Sowa 	int err = -EADDRNOTAVAIL;
1604b7b1bfceSHannes Frederic Sowa 
1605602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1606602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1607602582caSHannes Frederic Sowa 			break;
1608b7b1bfceSHannes Frederic Sowa 		if (ifp->scope == IFA_LINK &&
1609b7b1bfceSHannes Frederic Sowa 		    !(ifp->flags & banned_flags)) {
1610b7b1bfceSHannes Frederic Sowa 			*addr = ifp->addr;
1611b7b1bfceSHannes Frederic Sowa 			err = 0;
1612b7b1bfceSHannes Frederic Sowa 			break;
1613b7b1bfceSHannes Frederic Sowa 		}
1614b7b1bfceSHannes Frederic Sowa 	}
1615b7b1bfceSHannes Frederic Sowa 	return err;
1616b7b1bfceSHannes Frederic Sowa }
1617b7b1bfceSHannes Frederic Sowa 
161895c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1619479840ffSJiri Pirko 		    u32 banned_flags)
16201da177e4SLinus Torvalds {
16211da177e4SLinus Torvalds 	struct inet6_dev *idev;
16221da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
16231da177e4SLinus Torvalds 
16248814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1625e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1626e21e8467SStephen Hemminger 	if (idev) {
16271da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1628b7b1bfceSHannes Frederic Sowa 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
16291da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
16301da177e4SLinus Torvalds 	}
16318814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
16321da177e4SLinus Torvalds 	return err;
16331da177e4SLinus Torvalds }
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
16361da177e4SLinus Torvalds {
16371da177e4SLinus Torvalds 	int cnt = 0;
16381da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1641502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list)
16421da177e4SLinus Torvalds 		cnt++;
16431da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
16441da177e4SLinus Torvalds 	return cnt;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
1647b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
16482a7851bfSFlorian Westphal 		  const struct net_device *dev, int strict)
16491da177e4SLinus Torvalds {
1650c58da4c6SErik Kline 	return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1651c58da4c6SErik Kline }
1652c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr);
1653c58da4c6SErik Kline 
1654c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1655c58da4c6SErik Kline 			    const struct net_device *dev, int strict,
1656c58da4c6SErik Kline 			    u32 banned_flags)
1657c58da4c6SErik Kline {
1658eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1659ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
1660c58da4c6SErik Kline 	u32 ifp_flags;
16611da177e4SLinus Torvalds 
16625c578aedSstephen hemminger 	rcu_read_lock_bh();
1663b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1664878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1665bfeade08SDaniel Lezcano 			continue;
1666c58da4c6SErik Kline 		/* Decouple optimistic from tentative for evaluation here.
1667c58da4c6SErik Kline 		 * Ban optimistic addresses explicitly, when required.
1668c58da4c6SErik Kline 		 */
1669c58da4c6SErik Kline 		ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1670c58da4c6SErik Kline 			    ? (ifp->flags&~IFA_F_TENTATIVE)
1671c58da4c6SErik Kline 			    : ifp->flags;
16721da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1673c58da4c6SErik Kline 		    !(ifp_flags&banned_flags) &&
167463159f29SIan Morris 		    (!dev || ifp->idev->dev == dev ||
1675eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
16765c578aedSstephen hemminger 			rcu_read_unlock_bh();
1677eedf042aSStephen Hemminger 			return 1;
1678eedf042aSStephen Hemminger 		}
1679eedf042aSStephen Hemminger 	}
16805c578aedSstephen hemminger 
1681eedf042aSStephen Hemminger 	rcu_read_unlock_bh();
1682eedf042aSStephen Hemminger 	return 0;
16831da177e4SLinus Torvalds }
1684c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
16857159039aSYOSHIFUJI Hideaki 
16863e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
168706bfe655SDaniel Lezcano 			       struct net_device *dev)
16881da177e4SLinus Torvalds {
1689ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
16901da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
16911da177e4SLinus Torvalds 
1692b67bfe0dSSasha Levin 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1693878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
169406bfe655SDaniel Lezcano 			continue;
16951da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
169663159f29SIan Morris 			if (!dev || ifp->idev->dev == dev)
16973e81c6daSDavid S. Miller 				return true;
16981da177e4SLinus Torvalds 		}
16991da177e4SLinus Torvalds 	}
17003e81c6daSDavid S. Miller 	return false;
17011da177e4SLinus Torvalds }
17021da177e4SLinus Torvalds 
17037df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev.
17047df37ff3SCatalin\(ux\) M. BOIE  * If one is found it returns true.
17057df37ff3SCatalin\(ux\) M. BOIE  */
17067df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
17077df37ff3SCatalin\(ux\) M. BOIE 	const unsigned int prefix_len, struct net_device *dev)
17087df37ff3SCatalin\(ux\) M. BOIE {
17097df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_dev *idev;
17107df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_ifaddr *ifa;
17117df37ff3SCatalin\(ux\) M. BOIE 	bool ret = false;
17127df37ff3SCatalin\(ux\) M. BOIE 
17137df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_lock();
17147df37ff3SCatalin\(ux\) M. BOIE 	idev = __in6_dev_get(dev);
17157df37ff3SCatalin\(ux\) M. BOIE 	if (idev) {
17167df37ff3SCatalin\(ux\) M. BOIE 		read_lock_bh(&idev->lock);
17177df37ff3SCatalin\(ux\) M. BOIE 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
17187df37ff3SCatalin\(ux\) M. BOIE 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
17197df37ff3SCatalin\(ux\) M. BOIE 			if (ret)
17207df37ff3SCatalin\(ux\) M. BOIE 				break;
17217df37ff3SCatalin\(ux\) M. BOIE 		}
17227df37ff3SCatalin\(ux\) M. BOIE 		read_unlock_bh(&idev->lock);
17237df37ff3SCatalin\(ux\) M. BOIE 	}
17247df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_unlock();
17257df37ff3SCatalin\(ux\) M. BOIE 
17267df37ff3SCatalin\(ux\) M. BOIE 	return ret;
17277df37ff3SCatalin\(ux\) M. BOIE }
17287df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix);
17297df37ff3SCatalin\(ux\) M. BOIE 
1730b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
173152eeeb84SYOSHIFUJI Hideaki {
173252eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
173352eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
173452eeeb84SYOSHIFUJI Hideaki 	int	onlink;
173552eeeb84SYOSHIFUJI Hideaki 
173652eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
173752eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
173852eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
173952eeeb84SYOSHIFUJI Hideaki 	if (idev) {
174052eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
1741502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
174252eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
174352eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
174452eeeb84SYOSHIFUJI Hideaki 			if (onlink)
174552eeeb84SYOSHIFUJI Hideaki 				break;
174652eeeb84SYOSHIFUJI Hideaki 		}
174752eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
174852eeeb84SYOSHIFUJI Hideaki 	}
174952eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
175052eeeb84SYOSHIFUJI Hideaki 	return onlink;
175152eeeb84SYOSHIFUJI Hideaki }
175252eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
175352eeeb84SYOSHIFUJI Hideaki 
17549acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
17551cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
17561da177e4SLinus Torvalds {
1757b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
1758ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
17591da177e4SLinus Torvalds 
17605c578aedSstephen hemminger 	rcu_read_lock_bh();
1761b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1762878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
17631cab3da6SDaniel Lezcano 			continue;
17641da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
176563159f29SIan Morris 			if (!dev || ifp->idev->dev == dev ||
17661da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1767b79d1d54SDavid S. Miller 				result = ifp;
17681da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
17691da177e4SLinus Torvalds 				break;
17701da177e4SLinus Torvalds 			}
17711da177e4SLinus Torvalds 		}
17721da177e4SLinus Torvalds 	}
17735c578aedSstephen hemminger 	rcu_read_unlock_bh();
17741da177e4SLinus Torvalds 
1775b79d1d54SDavid S. Miller 	return result;
17761da177e4SLinus Torvalds }
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
17791da177e4SLinus Torvalds 
1780cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
17811da177e4SLinus Torvalds {
17823d171f39SLubomir Rintel 	if (dad_failed)
17833d171f39SLubomir Rintel 		ifp->flags |= IFA_F_DADFAILED;
17843d171f39SLubomir Rintel 
17851da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
17861da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
1787c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifp);
17881da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
17891da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
1790e2577a06SHerbert Xu 		if (dad_failed)
1791e2577a06SHerbert Xu 			ipv6_ifa_notify(0, ifp);
17921da177e4SLinus Torvalds 		in6_ifa_put(ifp);
17931da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
17941da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
17951da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
17961da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
17971da177e4SLinus Torvalds 		if (ifpub) {
17981da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
17991da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
18001da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
18011da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
18021da177e4SLinus Torvalds 		} else {
18031da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
18041da177e4SLinus Torvalds 		}
18051da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
1806c15b1ccaSHannes Frederic Sowa 	} else {
18071da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
18081da177e4SLinus Torvalds 	}
1809c15b1ccaSHannes Frederic Sowa }
18101da177e4SLinus Torvalds 
1811f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1812f2344a13SHerbert Xu {
1813f2344a13SHerbert Xu 	int err = -ENOENT;
1814f2344a13SHerbert Xu 
18158e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
1816f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
1817f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
1818f2344a13SHerbert Xu 		err = 0;
1819f2344a13SHerbert Xu 	}
18208e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
1821f2344a13SHerbert Xu 
1822f2344a13SHerbert Xu 	return err;
1823f2344a13SHerbert Xu }
1824f2344a13SHerbert Xu 
18253c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
18263c21edbdSYOSHIFUJI Hideaki {
18275f40ef77SHannes Frederic Sowa 	struct in6_addr addr;
18281b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
18291855b7c3SHannes Frederic Sowa 	struct net *net = dev_net(ifp->idev->dev);
18309bdd8d40SBrian Haley 
1831853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
1832853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
1833f2344a13SHerbert Xu 		return;
1834853dc2e0SUrsula Braun 	}
1835f2344a13SHerbert Xu 
1836e87cc472SJoe Perches 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
18370522fea6SJens Rosenboom 			     ifp->idev->dev->name, &ifp->addr);
18389bdd8d40SBrian Haley 
18395f40ef77SHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
18401b34be74SYOSHIFUJI Hideaki 
18415f40ef77SHannes Frederic Sowa 	if (ifp->flags & IFA_F_STABLE_PRIVACY) {
18425f40ef77SHannes Frederic Sowa 		int scope = ifp->scope;
18435f40ef77SHannes Frederic Sowa 		u32 flags = ifp->flags;
18445f40ef77SHannes Frederic Sowa 		struct in6_addr new_addr;
18455f40ef77SHannes Frederic Sowa 		struct inet6_ifaddr *ifp2;
18465f40ef77SHannes Frederic Sowa 		u32 valid_lft, preferred_lft;
18475f40ef77SHannes Frederic Sowa 		int pfxlen = ifp->prefix_len;
18485f40ef77SHannes Frederic Sowa 		int retries = ifp->stable_privacy_retry + 1;
18495f40ef77SHannes Frederic Sowa 
18501855b7c3SHannes Frederic Sowa 		if (retries > net->ipv6.sysctl.idgen_retries) {
18515f40ef77SHannes Frederic Sowa 			net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
18525f40ef77SHannes Frederic Sowa 					     ifp->idev->dev->name);
18535f40ef77SHannes Frederic Sowa 			goto errdad;
18545f40ef77SHannes Frederic Sowa 		}
18555f40ef77SHannes Frederic Sowa 
18565f40ef77SHannes Frederic Sowa 		new_addr = ifp->addr;
18575f40ef77SHannes Frederic Sowa 		if (ipv6_generate_stable_address(&new_addr, retries,
18585f40ef77SHannes Frederic Sowa 						 idev))
18595f40ef77SHannes Frederic Sowa 			goto errdad;
18605f40ef77SHannes Frederic Sowa 
18615f40ef77SHannes Frederic Sowa 		valid_lft = ifp->valid_lft;
18625f40ef77SHannes Frederic Sowa 		preferred_lft = ifp->prefered_lft;
18635f40ef77SHannes Frederic Sowa 
18645f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp->lock);
18655f40ef77SHannes Frederic Sowa 
18665f40ef77SHannes Frederic Sowa 		if (idev->cnf.max_addresses &&
18675f40ef77SHannes Frederic Sowa 		    ipv6_count_addresses(idev) >=
18685f40ef77SHannes Frederic Sowa 		    idev->cnf.max_addresses)
18695f40ef77SHannes Frederic Sowa 			goto lock_errdad;
18705f40ef77SHannes Frederic Sowa 
18715f40ef77SHannes Frederic Sowa 		net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
18725f40ef77SHannes Frederic Sowa 				     ifp->idev->dev->name);
18735f40ef77SHannes Frederic Sowa 
18745f40ef77SHannes Frederic Sowa 		ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
18755f40ef77SHannes Frederic Sowa 				     scope, flags, valid_lft,
18765f40ef77SHannes Frederic Sowa 				     preferred_lft);
18775f40ef77SHannes Frederic Sowa 		if (IS_ERR(ifp2))
18785f40ef77SHannes Frederic Sowa 			goto lock_errdad;
18795f40ef77SHannes Frederic Sowa 
18805f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp2->lock);
18815f40ef77SHannes Frederic Sowa 		ifp2->stable_privacy_retry = retries;
18825f40ef77SHannes Frederic Sowa 		ifp2->state = INET6_IFADDR_STATE_PREDAD;
18835f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp2->lock);
18845f40ef77SHannes Frederic Sowa 
18851855b7c3SHannes Frederic Sowa 		addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
18865f40ef77SHannes Frederic Sowa 		in6_ifa_put(ifp2);
18875f40ef77SHannes Frederic Sowa lock_errdad:
18885f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp->lock);
18895f40ef77SHannes Frederic Sowa 	} else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
18901b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[0] = htonl(0xfe800000);
18911b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[1] = 0;
18921b34be74SYOSHIFUJI Hideaki 
18931b34be74SYOSHIFUJI Hideaki 		if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
18941b34be74SYOSHIFUJI Hideaki 		    ipv6_addr_equal(&ifp->addr, &addr)) {
18951b34be74SYOSHIFUJI Hideaki 			/* DAD failed for link-local based on MAC address */
18961b34be74SYOSHIFUJI Hideaki 			idev->cnf.disable_ipv6 = 1;
18979bdd8d40SBrian Haley 
1898f3213831SJoe Perches 			pr_info("%s: IPv6 being disabled!\n",
18999bdd8d40SBrian Haley 				ifp->idev->dev->name);
19001b34be74SYOSHIFUJI Hideaki 		}
19011b34be74SYOSHIFUJI Hideaki 	}
19021b34be74SYOSHIFUJI Hideaki 
19035f40ef77SHannes Frederic Sowa errdad:
1904c15b1ccaSHannes Frederic Sowa 	/* transition from _POSTDAD to _ERRDAD */
1905c15b1ccaSHannes Frederic Sowa 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
19068e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
1907c15b1ccaSHannes Frederic Sowa 
1908c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, 0);
19093c21edbdSYOSHIFUJI Hideaki }
19101da177e4SLinus Torvalds 
1911a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group.
1912a9ed4a29SSabrina Dubroca  * caller must hold RTNL */
1913b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
19141da177e4SLinus Torvalds {
19151da177e4SLinus Torvalds 	struct in6_addr maddr;
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
19181da177e4SLinus Torvalds 		return;
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
19211da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds 
1924a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
1925b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
19261da177e4SLinus Torvalds {
19271da177e4SLinus Torvalds 	struct in6_addr maddr;
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
19301da177e4SLinus Torvalds 		return;
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
19331da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
19341da177e4SLinus Torvalds }
19351da177e4SLinus Torvalds 
1936a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
193720380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
19381da177e4SLinus Torvalds {
19391da177e4SLinus Torvalds 	struct in6_addr addr;
1940c15b1ccaSHannes Frederic Sowa 
194188ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
19422bda8a0cSBjørn Mork 		return;
19431da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
19441da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
19451da177e4SLinus Torvalds 		return;
1946013b4d90SWANG Cong 	__ipv6_dev_ac_inc(ifp->idev, &addr);
19471da177e4SLinus Torvalds }
19481da177e4SLinus Torvalds 
1949a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
195020380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
19511da177e4SLinus Torvalds {
19521da177e4SLinus Torvalds 	struct in6_addr addr;
1953c15b1ccaSHannes Frederic Sowa 
195488ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
195532019e65SYOSHIFUJI Hideaki 		return;
19561da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
19571da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
19581da177e4SLinus Torvalds 		return;
19591da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
19601da177e4SLinus Torvalds }
19611da177e4SLinus Torvalds 
196206a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
196306a4c1c5Salex.bluesman.smirnov@gmail.com {
19645241c2d7SAlexander Aring 	if (dev->addr_len != EUI64_ADDR_LEN)
196506a4c1c5Salex.bluesman.smirnov@gmail.com 		return -1;
19665241c2d7SAlexander Aring 	memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
19675e98a36eSYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
196806a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
196906a4c1c5Salex.bluesman.smirnov@gmail.com }
197006a4c1c5Salex.bluesman.smirnov@gmail.com 
1971cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1972cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
1973cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	union fwnet_hwaddr *ha;
1974cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1975cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
1976cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
1977cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1978cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	ha = (union fwnet_hwaddr *)dev->dev_addr;
1979cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1980cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1981cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
1982cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
1983cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
1984cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1985073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1986073a8e0eSYOSHIFUJI Hideaki {
19871da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
19881da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
19891da177e4SLinus Torvalds 		return -1;
19901da177e4SLinus Torvalds 	memset(eui, 0, 7);
19911da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
19921da177e4SLinus Torvalds 	return 0;
1993073a8e0eSYOSHIFUJI Hideaki }
1994073a8e0eSYOSHIFUJI Hideaki 
1995073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1996073a8e0eSYOSHIFUJI Hideaki {
19971da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
19981da177e4SLinus Torvalds 		return -1;
19991da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
20001da177e4SLinus Torvalds 	eui[0] |= 2;
20011da177e4SLinus Torvalds 	return 0;
20021da177e4SLinus Torvalds }
2003073a8e0eSYOSHIFUJI Hideaki 
2004c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2005dfd982baSYOSHIFUJI Hideaki {
20069af28511SSascha Hlusiak 	if (addr == 0)
20079af28511SSascha Hlusiak 		return -1;
2008dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2009dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2010dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2011dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2012dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2013dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2014dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
2015dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
2016dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
2017dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
2018dfd982baSYOSHIFUJI Hideaki 	return 0;
2019dfd982baSYOSHIFUJI Hideaki }
2020dfd982baSYOSHIFUJI Hideaki 
2021dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2022dfd982baSYOSHIFUJI Hideaki {
2023dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
2024dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2025dfd982baSYOSHIFUJI Hideaki 	return -1;
2026dfd982baSYOSHIFUJI Hideaki }
2027dfd982baSYOSHIFUJI Hideaki 
2028aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2029aee80b54Sstephen hemminger {
2030aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2031aee80b54Sstephen hemminger }
2032aee80b54Sstephen hemminger 
2033e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2034e837735eSNicolas Dichtel {
2035e837735eSNicolas Dichtel 	memcpy(eui, dev->perm_addr, 3);
2036e837735eSNicolas Dichtel 	memcpy(eui + 5, dev->perm_addr + 3, 3);
2037e837735eSNicolas Dichtel 	eui[3] = 0xFF;
2038e837735eSNicolas Dichtel 	eui[4] = 0xFE;
2039e837735eSNicolas Dichtel 	eui[0] ^= 2;
2040e837735eSNicolas Dichtel 	return 0;
2041e837735eSNicolas Dichtel }
2042e837735eSNicolas Dichtel 
2043073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2044073a8e0eSYOSHIFUJI Hideaki {
2045073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
2046073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
2047073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
2048073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
2049073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
2050073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
2051073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
2052073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
2053c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
2054dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
2055aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
2056aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
2057e74bccb8SJukka Rissanen 	case ARPHRD_6LOWPAN:
205806a4c1c5Salex.bluesman.smirnov@gmail.com 		return addrconf_ifid_eui64(eui, dev);
2059cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
2060cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
2061e837735eSNicolas Dichtel 	case ARPHRD_TUNNEL6:
2062e837735eSNicolas Dichtel 		return addrconf_ifid_ip6tnl(eui, dev);
2063073a8e0eSYOSHIFUJI Hideaki 	}
20641da177e4SLinus Torvalds 	return -1;
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
20681da177e4SLinus Torvalds {
20691da177e4SLinus Torvalds 	int err = -1;
20701da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2073602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2074602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
2075602582caSHannes Frederic Sowa 			break;
20761da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
20771da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
20781da177e4SLinus Torvalds 			err = 0;
20791da177e4SLinus Torvalds 			break;
20801da177e4SLinus Torvalds 		}
20811da177e4SLinus Torvalds 	}
20821da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
20831da177e4SLinus Torvalds 	return err;
20841da177e4SLinus Torvalds }
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2087eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev)
20881da177e4SLinus Torvalds {
20891da177e4SLinus Torvalds regen:
2090955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
20911da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 	/*
20941da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
20951da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
20961da177e4SLinus Torvalds 	 *
20971da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
20981da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
2099c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
21001da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
21011da177e4SLinus Torvalds 	 *  - value 0
21021da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
21031da177e4SLinus Torvalds 	 */
21041da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
21051da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
21061da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
21071da177e4SLinus Torvalds 		goto regen;
21081da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
21091da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
21101da177e4SLinus Torvalds 			goto regen;
21111da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
21121da177e4SLinus Torvalds 			goto regen;
21131da177e4SLinus Torvalds 	}
21141da177e4SLinus Torvalds }
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
21171da177e4SLinus Torvalds {
21181da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
21191da177e4SLinus Torvalds 	unsigned long expires;
21201da177e4SLinus Torvalds 
21218814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
21221da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds 	if (idev->dead)
21251da177e4SLinus Torvalds 		goto out;
21261da177e4SLinus Torvalds 
2127eb7e0575SSorin Dumitru 	__ipv6_regen_rndid(idev);
21281da177e4SLinus Torvalds 
21291da177e4SLinus Torvalds 	expires = jiffies +
21301da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
21311f9248e5SJiri Pirko 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
21321f9248e5SJiri Pirko 		NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
2133784e2710SBen Hutchings 		idev->cnf.max_desync_factor * HZ;
21341da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
2135f3213831SJoe Perches 		pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2136f3213831SJoe Perches 			__func__, idev->dev->name);
21371da177e4SLinus Torvalds 		goto out;
21381da177e4SLinus Torvalds 	}
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
21411da177e4SLinus Torvalds 		in6_dev_hold(idev);
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds out:
21441da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
21458814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
21461da177e4SLinus Torvalds 	in6_dev_put(idev);
21471da177e4SLinus Torvalds }
21481da177e4SLinus Torvalds 
2149eb7e0575SSorin Dumitru static void  __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
21508e5e8f30SEldad Zack {
21511da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2152eb7e0575SSorin Dumitru 		__ipv6_regen_rndid(idev);
21531da177e4SLinus Torvalds }
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds /*
21561da177e4SLinus Torvalds  *	Add prefix route.
21571da177e4SLinus Torvalds  */
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds static void
21601da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2161e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
21621da177e4SLinus Torvalds {
216386872cb5SThomas Graf 	struct fib6_config cfg = {
2164ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
216586872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
216686872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
216786872cb5SThomas Graf 		.fc_expires = expires,
216886872cb5SThomas Graf 		.fc_dst_len = plen,
216986872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
2170c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
2171f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
217286872cb5SThomas Graf 	};
21731da177e4SLinus Torvalds 
21744e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
21771da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
21781da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
21791da177e4SLinus Torvalds 	 */
218007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
21811da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
218286872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
21830be669bbSJoerg Roedel #endif
21841da177e4SLinus Torvalds 
218586872cb5SThomas Graf 	ip6_route_add(&cfg);
21861da177e4SLinus Torvalds }
21871da177e4SLinus Torvalds 
218814ef37b6SAndreas Hofmeister 
218914ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
219014ef37b6SAndreas Hofmeister 						  int plen,
219114ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
219214ef37b6SAndreas Hofmeister 						  u32 flags, u32 noflags)
219314ef37b6SAndreas Hofmeister {
219414ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
219514ef37b6SAndreas Hofmeister 	struct rt6_info *rt = NULL;
219614ef37b6SAndreas Hofmeister 	struct fib6_table *table;
2197ca254490SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
219814ef37b6SAndreas Hofmeister 
2199ca254490SDavid Ahern 	table = fib6_get_table(dev_net(dev), tb_id);
220063159f29SIan Morris 	if (!table)
220114ef37b6SAndreas Hofmeister 		return NULL;
220214ef37b6SAndreas Hofmeister 
22035744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
220414ef37b6SAndreas Hofmeister 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
220514ef37b6SAndreas Hofmeister 	if (!fn)
220614ef37b6SAndreas Hofmeister 		goto out;
22071f56a01fSMartin KaFai Lau 
22081f56a01fSMartin KaFai Lau 	noflags |= RTF_CACHE;
220914ef37b6SAndreas Hofmeister 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2210d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != dev->ifindex)
221114ef37b6SAndreas Hofmeister 			continue;
221214ef37b6SAndreas Hofmeister 		if ((rt->rt6i_flags & flags) != flags)
221314ef37b6SAndreas Hofmeister 			continue;
221485da53bfSRomain Kuntz 		if ((rt->rt6i_flags & noflags) != 0)
221514ef37b6SAndreas Hofmeister 			continue;
221614ef37b6SAndreas Hofmeister 		dst_hold(&rt->dst);
221714ef37b6SAndreas Hofmeister 		break;
221814ef37b6SAndreas Hofmeister 	}
221914ef37b6SAndreas Hofmeister out:
22205744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
222114ef37b6SAndreas Hofmeister 	return rt;
222214ef37b6SAndreas Hofmeister }
222314ef37b6SAndreas Hofmeister 
222414ef37b6SAndreas Hofmeister 
22251da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
22281da177e4SLinus Torvalds {
222986872cb5SThomas Graf 	struct fib6_config cfg = {
2230ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
223186872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
223286872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
223386872cb5SThomas Graf 		.fc_dst_len = 8,
223486872cb5SThomas Graf 		.fc_flags = RTF_UP,
2235c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
223686872cb5SThomas Graf 	};
22371da177e4SLinus Torvalds 
223886872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
223986872cb5SThomas Graf 
224086872cb5SThomas Graf 	ip6_route_add(&cfg);
22411da177e4SLinus Torvalds }
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
22441da177e4SLinus Torvalds {
22451da177e4SLinus Torvalds 	struct inet6_dev *idev;
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds 	ASSERT_RTNL();
22481da177e4SLinus Torvalds 
2249e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2250e21e8467SStephen Hemminger 	if (!idev)
225164e724f6SBrian Haley 		return ERR_PTR(-ENOBUFS);
225264e724f6SBrian Haley 
225364e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
225464e724f6SBrian Haley 		return ERR_PTR(-EACCES);
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds 	/* Add default multicast route */
2257*ba46ee4cSDavid Ahern 	if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
22581da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
22591da177e4SLinus Torvalds 
22601da177e4SLinus Torvalds 	return idev;
22611da177e4SLinus Torvalds }
22621da177e4SLinus Torvalds 
226353bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev,
226453bd6749SJiri Pirko 			     struct inet6_ifaddr *ifp,
226553bd6749SJiri Pirko 			     __u32 valid_lft, __u32 prefered_lft,
226653bd6749SJiri Pirko 			     bool create, unsigned long now)
226753bd6749SJiri Pirko {
226853bd6749SJiri Pirko 	u32 flags;
226953bd6749SJiri Pirko 	struct inet6_ifaddr *ift;
227053bd6749SJiri Pirko 
227153bd6749SJiri Pirko 	read_lock_bh(&idev->lock);
227253bd6749SJiri Pirko 	/* update all temporary addresses in the list */
227353bd6749SJiri Pirko 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
227453bd6749SJiri Pirko 		int age, max_valid, max_prefered;
227553bd6749SJiri Pirko 
227653bd6749SJiri Pirko 		if (ifp != ift->ifpub)
227753bd6749SJiri Pirko 			continue;
227853bd6749SJiri Pirko 
227953bd6749SJiri Pirko 		/* RFC 4941 section 3.3:
228053bd6749SJiri Pirko 		 * If a received option will extend the lifetime of a public
228153bd6749SJiri Pirko 		 * address, the lifetimes of temporary addresses should
228253bd6749SJiri Pirko 		 * be extended, subject to the overall constraint that no
228353bd6749SJiri Pirko 		 * temporary addresses should ever remain "valid" or "preferred"
228453bd6749SJiri Pirko 		 * for a time longer than (TEMP_VALID_LIFETIME) or
228553bd6749SJiri Pirko 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
228653bd6749SJiri Pirko 		 */
228753bd6749SJiri Pirko 		age = (now - ift->cstamp) / HZ;
228853bd6749SJiri Pirko 		max_valid = idev->cnf.temp_valid_lft - age;
228953bd6749SJiri Pirko 		if (max_valid < 0)
229053bd6749SJiri Pirko 			max_valid = 0;
229153bd6749SJiri Pirko 
229253bd6749SJiri Pirko 		max_prefered = idev->cnf.temp_prefered_lft -
229353bd6749SJiri Pirko 			       idev->cnf.max_desync_factor - age;
229453bd6749SJiri Pirko 		if (max_prefered < 0)
229553bd6749SJiri Pirko 			max_prefered = 0;
229653bd6749SJiri Pirko 
229753bd6749SJiri Pirko 		if (valid_lft > max_valid)
229853bd6749SJiri Pirko 			valid_lft = max_valid;
229953bd6749SJiri Pirko 
230053bd6749SJiri Pirko 		if (prefered_lft > max_prefered)
230153bd6749SJiri Pirko 			prefered_lft = max_prefered;
230253bd6749SJiri Pirko 
230353bd6749SJiri Pirko 		spin_lock(&ift->lock);
230453bd6749SJiri Pirko 		flags = ift->flags;
230553bd6749SJiri Pirko 		ift->valid_lft = valid_lft;
230653bd6749SJiri Pirko 		ift->prefered_lft = prefered_lft;
230753bd6749SJiri Pirko 		ift->tstamp = now;
230853bd6749SJiri Pirko 		if (prefered_lft > 0)
230953bd6749SJiri Pirko 			ift->flags &= ~IFA_F_DEPRECATED;
231053bd6749SJiri Pirko 
231153bd6749SJiri Pirko 		spin_unlock(&ift->lock);
231253bd6749SJiri Pirko 		if (!(flags&IFA_F_TENTATIVE))
231353bd6749SJiri Pirko 			ipv6_ifa_notify(0, ift);
231453bd6749SJiri Pirko 	}
231553bd6749SJiri Pirko 
231653bd6749SJiri Pirko 	if ((create || list_empty(&idev->tempaddr_list)) &&
231753bd6749SJiri Pirko 	    idev->cnf.use_tempaddr > 0) {
231853bd6749SJiri Pirko 		/* When a new public address is created as described
231953bd6749SJiri Pirko 		 * in [ADDRCONF], also create a new temporary address.
232053bd6749SJiri Pirko 		 * Also create a temporary address if it's enabled but
232153bd6749SJiri Pirko 		 * no temporary address currently exists.
232253bd6749SJiri Pirko 		 */
232353bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
232453bd6749SJiri Pirko 		ipv6_create_tempaddr(ifp, NULL);
232553bd6749SJiri Pirko 	} else {
232653bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
232753bd6749SJiri Pirko 	}
232853bd6749SJiri Pirko }
232953bd6749SJiri Pirko 
2330cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2331cc9da6ccSBjørn Mork {
2332cc9da6ccSBjørn Mork 	return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2333cc9da6ccSBjørn Mork 	       idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2334cc9da6ccSBjørn Mork }
2335cc9da6ccSBjørn Mork 
2336e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
23371da177e4SLinus Torvalds {
23381da177e4SLinus Torvalds 	struct prefix_info *pinfo;
23391da177e4SLinus Torvalds 	__u32 valid_lft;
23401da177e4SLinus Torvalds 	__u32 prefered_lft;
23411da177e4SLinus Torvalds 	int addr_type;
234264236f3fSHannes Frederic Sowa 	u32 addr_flags = 0;
23431da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
234456d417b1SBrian Haley 	struct net *net = dev_net(dev);
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
23471da177e4SLinus Torvalds 
23481da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
2349ba3542e1Sdingtianhong 		ADBG("addrconf: prefix option too short\n");
23501da177e4SLinus Torvalds 		return;
23511da177e4SLinus Torvalds 	}
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds 	/*
23541da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
23551da177e4SLinus Torvalds 	 */
23561da177e4SLinus Torvalds 
23571da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
23601da177e4SLinus Torvalds 		return;
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
23631da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2366e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
23671da177e4SLinus Torvalds 		return;
23681da177e4SLinus Torvalds 	}
23691da177e4SLinus Torvalds 
23701da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
23711da177e4SLinus Torvalds 
237263159f29SIan Morris 	if (!in6_dev) {
2373e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2374e87cc472SJoe Perches 				    dev->name);
23751da177e4SLinus Torvalds 		return;
23761da177e4SLinus Torvalds 	}
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds 	/*
23791da177e4SLinus Torvalds 	 *	Two things going on here:
23801da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
23811da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
23821da177e4SLinus Torvalds 	 */
23831da177e4SLinus Torvalds 
23844bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
23854bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
23864bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
23874bed72e4SYOSHIFUJI Hideaki 
23881da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
23896f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
23906f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
23916f704992SYOSHIFUJI Hideaki 		 * not good.
23921da177e4SLinus Torvalds 		 */
23934bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
23944bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
23954bed72e4SYOSHIFUJI Hideaki 		else
23964bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
23973dd4bc68SYOSHIFUJI Hideaki 
23984bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
23994bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
24001da177e4SLinus Torvalds 
240114ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
240214ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
240314ef37b6SAndreas Hofmeister 					       dev,
240414ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
240514ef37b6SAndreas Hofmeister 					       RTF_GATEWAY | RTF_DEFAULT);
24061da177e4SLinus Torvalds 
240714ef37b6SAndreas Hofmeister 		if (rt) {
24086f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
24091da177e4SLinus Torvalds 			if (valid_lft == 0) {
2410e0a1ad73SThomas Graf 				ip6_del_rt(rt);
24111da177e4SLinus Torvalds 				rt = NULL;
24124bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
24136f704992SYOSHIFUJI Hideaki 				/* not infinity */
24141716a961SGao feng 				rt6_set_expires(rt, jiffies + rt_expires);
24156f704992SYOSHIFUJI Hideaki 			} else {
24161716a961SGao feng 				rt6_clean_expires(rt);
24171da177e4SLinus Torvalds 			}
24181da177e4SLinus Torvalds 		} else if (valid_lft) {
24196f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
24204bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
24214bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
24226f704992SYOSHIFUJI Hideaki 				/* not infinity */
24236f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
24246f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
24256f704992SYOSHIFUJI Hideaki 			}
24261da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
24276f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
24281da177e4SLinus Torvalds 		}
242994e187c0SAmerigo Wang 		ip6_rt_put(rt);
24301da177e4SLinus Torvalds 	}
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
24331da177e4SLinus Torvalds 
24341da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
24351da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
24361da177e4SLinus Torvalds 		struct in6_addr addr;
24371da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
2438617fe29dSDaniel Borkmann 		bool tokenized = false;
24391da177e4SLinus Torvalds 
24401da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
24411da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2442f53adae4SDaniel Borkmann 
2443f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2444f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2445f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2446f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2447f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
2448617fe29dSDaniel Borkmann 				tokenized = true;
2449cc9da6ccSBjørn Mork 			} else if (is_addr_mode_generate_stable(in6_dev) &&
2450622c81d5SHannes Frederic Sowa 				   !ipv6_generate_stable_address(&addr, 0,
2451622c81d5SHannes Frederic Sowa 								 in6_dev)) {
245264236f3fSHannes Frederic Sowa 				addr_flags |= IFA_F_STABLE_PRIVACY;
2453622c81d5SHannes Frederic Sowa 				goto ok;
2454f53adae4SDaniel Borkmann 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
24551da177e4SLinus Torvalds 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
24561da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
24571da177e4SLinus Torvalds 				return;
24581da177e4SLinus Torvalds 			}
24591da177e4SLinus Torvalds 			goto ok;
24601da177e4SLinus Torvalds 		}
2461e87cc472SJoe Perches 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
24621da177e4SLinus Torvalds 				    pinfo->prefix_len);
24631da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
24641da177e4SLinus Torvalds 		return;
24651da177e4SLinus Torvalds 
24661da177e4SLinus Torvalds ok:
24671da177e4SLinus Torvalds 
246853b7997fSYOSHIFUJI Hideaki 		ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
24691da177e4SLinus Torvalds 
247063159f29SIan Morris 		if (!ifp && valid_lft) {
24711da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
247295c385b4SNeil Horman 
247395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
247495c385b4SNeil Horman 			if (in6_dev->cnf.optimistic_dad &&
2475e6bff995SNeil Horman 			    !net->ipv6.devconf_all->forwarding && sllao)
24769a1ec461SBjørn Mork 				addr_flags |= IFA_F_OPTIMISTIC;
247795c385b4SNeil Horman #endif
24781da177e4SLinus Torvalds 
24791da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
24801da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
24811da177e4SLinus Torvalds 			 */
24821da177e4SLinus Torvalds 			if (!max_addresses ||
24831da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
24843f8f5298SJiri Pirko 				ifp = ipv6_add_addr(in6_dev, &addr, NULL,
24853f8f5298SJiri Pirko 						    pinfo->prefix_len,
248695c385b4SNeil Horman 						    addr_type&IPV6_ADDR_SCOPE_MASK,
24878a226b2cSJiri Benc 						    addr_flags, valid_lft,
24888a226b2cSJiri Benc 						    prefered_lft);
24891da177e4SLinus Torvalds 
24903f0d2ba0SYOSHIFUJI Hideaki / 吉藤英明 			if (IS_ERR_OR_NULL(ifp)) {
24911da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
24921da177e4SLinus Torvalds 				return;
24931da177e4SLinus Torvalds 			}
24941da177e4SLinus Torvalds 
24958a226b2cSJiri Benc 			update_lft = 0;
24968a226b2cSJiri Benc 			create = 1;
2497c15b1ccaSHannes Frederic Sowa 			spin_lock_bh(&ifp->lock);
2498c15b1ccaSHannes Frederic Sowa 			ifp->flags |= IFA_F_MANAGETEMPADDR;
24991da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
2500617fe29dSDaniel Borkmann 			ifp->tokenized = tokenized;
2501c15b1ccaSHannes Frederic Sowa 			spin_unlock_bh(&ifp->lock);
2502cf22f9a2SDavid S. Miller 			addrconf_dad_start(ifp);
25031da177e4SLinus Torvalds 		}
25041da177e4SLinus Torvalds 
25051da177e4SLinus Torvalds 		if (ifp) {
2506479840ffSJiri Pirko 			u32 flags;
25071da177e4SLinus Torvalds 			unsigned long now;
25081da177e4SLinus Torvalds 			u32 stored_lft;
25091da177e4SLinus Torvalds 
25101da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
25118e8e676dSHannes Frederic Sowa 			spin_lock_bh(&ifp->lock);
25121da177e4SLinus Torvalds 			now = jiffies;
25131da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
25141da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
25151da177e4SLinus Torvalds 			else
25161da177e4SLinus Torvalds 				stored_lft = 0;
25178a226b2cSJiri Benc 			if (!update_lft && !create && stored_lft) {
25189451a304SFabian Frederick 				const u32 minimum_lft = min_t(u32,
25199451a304SFabian Frederick 					stored_lft, MIN_VALID_LIFETIME);
2520c9d55d5bSPaul Marks 				valid_lft = max(valid_lft, minimum_lft);
2521c9d55d5bSPaul Marks 
2522c9d55d5bSPaul Marks 				/* RFC4862 Section 5.5.3e:
2523c9d55d5bSPaul Marks 				 * "Note that the preferred lifetime of the
2524c9d55d5bSPaul Marks 				 *  corresponding address is always reset to
2525c9d55d5bSPaul Marks 				 *  the Preferred Lifetime in the received
2526c9d55d5bSPaul Marks 				 *  Prefix Information option, regardless of
2527c9d55d5bSPaul Marks 				 *  whether the valid lifetime is also reset or
2528a1ed0526SBrian Haley 				 *  ignored."
2529a1ed0526SBrian Haley 				 *
2530c9d55d5bSPaul Marks 				 * So we should always update prefered_lft here.
2531a1ed0526SBrian Haley 				 */
2532a1ed0526SBrian Haley 				update_lft = 1;
2533a1ed0526SBrian Haley 			}
25341da177e4SLinus Torvalds 
25351da177e4SLinus Torvalds 			if (update_lft) {
25361da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
25371da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
25381da177e4SLinus Torvalds 				ifp->tstamp = now;
25391da177e4SLinus Torvalds 				flags = ifp->flags;
25401da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
25418e8e676dSHannes Frederic Sowa 				spin_unlock_bh(&ifp->lock);
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
25441da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
25451da177e4SLinus Torvalds 			} else
25468e8e676dSHannes Frederic Sowa 				spin_unlock_bh(&ifp->lock);
25471da177e4SLinus Torvalds 
254853bd6749SJiri Pirko 			manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
254953bd6749SJiri Pirko 					 create, now);
25505d9efa7eSDavid S. Miller 
25511da177e4SLinus Torvalds 			in6_ifa_put(ifp);
2552c15b1ccaSHannes Frederic Sowa 			addrconf_verify();
25531da177e4SLinus Torvalds 		}
25541da177e4SLinus Torvalds 	}
25551da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
25561da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
25571da177e4SLinus Torvalds }
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds /*
25601da177e4SLinus Torvalds  *	Set destination address.
25611da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
25621da177e4SLinus Torvalds  *	device.
25631da177e4SLinus Torvalds  */
2564af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
25651da177e4SLinus Torvalds {
25661da177e4SLinus Torvalds 	struct in6_ifreq ireq;
25671da177e4SLinus Torvalds 	struct net_device *dev;
25681da177e4SLinus Torvalds 	int err = -EINVAL;
25691da177e4SLinus Torvalds 
25701da177e4SLinus Torvalds 	rtnl_lock();
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds 	err = -EFAULT;
25731da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
25741da177e4SLinus Torvalds 		goto err_exit;
25751da177e4SLinus Torvalds 
2576af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds 	err = -ENODEV;
257963159f29SIan Morris 	if (!dev)
25801da177e4SLinus Torvalds 		goto err_exit;
25811da177e4SLinus Torvalds 
258207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
25831da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
25845bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
25851da177e4SLinus Torvalds 		struct ifreq ifr;
25861da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
25891da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
25901da177e4SLinus Torvalds 			goto err_exit;
25911da177e4SLinus Torvalds 
25921da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
25931da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
25941da177e4SLinus Torvalds 		p.iph.saddr = 0;
25951da177e4SLinus Torvalds 		p.iph.version = 4;
25961da177e4SLinus Torvalds 		p.iph.ihl = 5;
25971da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
25981da177e4SLinus Torvalds 		p.iph.ttl = 64;
2599d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
26001da177e4SLinus Torvalds 
26015bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
26025bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
26035bc3eb7eSStephen Hemminger 
26045bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
26055bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
26061da177e4SLinus Torvalds 			set_fs(oldfs);
26075bc3eb7eSStephen Hemminger 		} else
26085bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds 		if (err == 0) {
26111da177e4SLinus Torvalds 			err = -ENOBUFS;
2612af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2613af284937SDaniel Lezcano 			if (!dev)
26141da177e4SLinus Torvalds 				goto err_exit;
26151da177e4SLinus Torvalds 			err = dev_open(dev);
26161da177e4SLinus Torvalds 		}
26171da177e4SLinus Torvalds 	}
26180be669bbSJoerg Roedel #endif
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds err_exit:
26211da177e4SLinus Torvalds 	rtnl_unlock();
26221da177e4SLinus Torvalds 	return err;
26231da177e4SLinus Torvalds }
26241da177e4SLinus Torvalds 
262593a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join,
262693a714d6SMadhu Challa 			  const struct in6_addr *addr, int ifindex)
262793a714d6SMadhu Challa {
262893a714d6SMadhu Challa 	int ret;
262993a714d6SMadhu Challa 
263093a714d6SMadhu Challa 	ASSERT_RTNL();
263193a714d6SMadhu Challa 
263293a714d6SMadhu Challa 	lock_sock(sk);
263393a714d6SMadhu Challa 	if (join)
263454ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_join(sk, ifindex, addr);
263593a714d6SMadhu Challa 	else
263654ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_drop(sk, ifindex, addr);
263793a714d6SMadhu Challa 	release_sock(sk);
263893a714d6SMadhu Challa 
263993a714d6SMadhu Challa 	return ret;
264093a714d6SMadhu Challa }
264193a714d6SMadhu Challa 
26421da177e4SLinus Torvalds /*
26431da177e4SLinus Torvalds  *	Manual configuration of address on an interface
26441da177e4SLinus Torvalds  */
2645479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex,
2646479840ffSJiri Pirko 			  const struct in6_addr *pfx,
2647caeaba79SNicolas Dichtel 			  const struct in6_addr *peer_pfx,
2648479840ffSJiri Pirko 			  unsigned int plen, __u32 ifa_flags,
2649479840ffSJiri Pirko 			  __u32 prefered_lft, __u32 valid_lft)
26501da177e4SLinus Torvalds {
26511da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
26521da177e4SLinus Torvalds 	struct inet6_dev *idev;
26531da177e4SLinus Torvalds 	struct net_device *dev;
265493a714d6SMadhu Challa 	unsigned long timeout;
265593a714d6SMadhu Challa 	clock_t expires;
26561da177e4SLinus Torvalds 	int scope;
26576f704992SYOSHIFUJI Hideaki 	u32 flags;
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds 	ASSERT_RTNL();
26601da177e4SLinus Torvalds 
266124ef0da7SThomas Graf 	if (plen > 128)
266224ef0da7SThomas Graf 		return -EINVAL;
266324ef0da7SThomas Graf 
26640778769dSNoriaki TAKAMIYA 	/* check the lifetime */
26650778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
26660778769dSNoriaki TAKAMIYA 		return -EINVAL;
26670778769dSNoriaki TAKAMIYA 
266853bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
266953bd6749SJiri Pirko 		return -EINVAL;
267053bd6749SJiri Pirko 
2671af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2672af284937SDaniel Lezcano 	if (!dev)
26731da177e4SLinus Torvalds 		return -ENODEV;
26741da177e4SLinus Torvalds 
267564e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
267664e724f6SBrian Haley 	if (IS_ERR(idev))
267764e724f6SBrian Haley 		return PTR_ERR(idev);
26781da177e4SLinus Torvalds 
267993a714d6SMadhu Challa 	if (ifa_flags & IFA_F_MCAUTOJOIN) {
268093a714d6SMadhu Challa 		int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
268193a714d6SMadhu Challa 					 true, pfx, ifindex);
268293a714d6SMadhu Challa 
268393a714d6SMadhu Challa 		if (ret < 0)
268493a714d6SMadhu Challa 			return ret;
268593a714d6SMadhu Challa 	}
268693a714d6SMadhu Challa 
26871da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
26881da177e4SLinus Torvalds 
26894bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
26904bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
26914bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
26924bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
26936f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
26944bed72e4SYOSHIFUJI Hideaki 	} else {
26954bed72e4SYOSHIFUJI Hideaki 		expires = 0;
26964bed72e4SYOSHIFUJI Hideaki 		flags = 0;
26974bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
26986f704992SYOSHIFUJI Hideaki 	}
26990778769dSNoriaki TAKAMIYA 
27004bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
27014bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
27024bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
27030778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
27044bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
27054bed72e4SYOSHIFUJI Hideaki 	}
27060778769dSNoriaki TAKAMIYA 
27078a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
27088a226b2cSJiri Benc 			    valid_lft, prefered_lft);
27090778769dSNoriaki TAKAMIYA 
27101da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
2711761aac73SThomas Haller 		if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
271246d48046SYOSHIFUJI Hideaki 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
27136f704992SYOSHIFUJI Hideaki 					      expires, flags);
2714761aac73SThomas Haller 		}
2715761aac73SThomas Haller 
271695c385b4SNeil Horman 		/*
271795c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
271895c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
271995c385b4SNeil Horman 		 * manually configured addresses
272095c385b4SNeil Horman 		 */
2721cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
272253bd6749SJiri Pirko 		if (ifa_flags & IFA_F_MANAGETEMPADDR)
272353bd6749SJiri Pirko 			manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
272453bd6749SJiri Pirko 					 true, jiffies);
27251da177e4SLinus Torvalds 		in6_ifa_put(ifp);
2726c15b1ccaSHannes Frederic Sowa 		addrconf_verify_rtnl();
27271da177e4SLinus Torvalds 		return 0;
272893a714d6SMadhu Challa 	} else if (ifa_flags & IFA_F_MCAUTOJOIN) {
272993a714d6SMadhu Challa 		ipv6_mc_config(net->ipv6.mc_autojoin_sk,
273093a714d6SMadhu Challa 			       false, pfx, ifindex);
27311da177e4SLinus Torvalds 	}
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds 	return PTR_ERR(ifp);
27341da177e4SLinus Torvalds }
27351da177e4SLinus Torvalds 
27366046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
27376046d5b4SHeiner Kallweit 			  const struct in6_addr *pfx, unsigned int plen)
27381da177e4SLinus Torvalds {
27391da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
27401da177e4SLinus Torvalds 	struct inet6_dev *idev;
27411da177e4SLinus Torvalds 	struct net_device *dev;
27421da177e4SLinus Torvalds 
274324ef0da7SThomas Graf 	if (plen > 128)
274424ef0da7SThomas Graf 		return -EINVAL;
274524ef0da7SThomas Graf 
2746af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2747af284937SDaniel Lezcano 	if (!dev)
27481da177e4SLinus Torvalds 		return -ENODEV;
27491da177e4SLinus Torvalds 
2750e5d08d71SIan Morris 	idev = __in6_dev_get(dev);
275163159f29SIan Morris 	if (!idev)
27521da177e4SLinus Torvalds 		return -ENXIO;
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2755502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
27561da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
27571da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
27581da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
27591da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
27601da177e4SLinus Torvalds 
27616046d5b4SHeiner Kallweit 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
27626046d5b4SHeiner Kallweit 			    (ifa_flags & IFA_F_MANAGETEMPADDR))
27636046d5b4SHeiner Kallweit 				manage_tempaddrs(idev, ifp, 0, 0, false,
27646046d5b4SHeiner Kallweit 						 jiffies);
27651da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
27666046d5b4SHeiner Kallweit 			addrconf_verify_rtnl();
276793a714d6SMadhu Challa 			if (ipv6_addr_is_multicast(pfx)) {
276893a714d6SMadhu Challa 				ipv6_mc_config(net->ipv6.mc_autojoin_sk,
276993a714d6SMadhu Challa 					       false, pfx, dev->ifindex);
277093a714d6SMadhu Challa 			}
27711da177e4SLinus Torvalds 			return 0;
27721da177e4SLinus Torvalds 		}
27731da177e4SLinus Torvalds 	}
27741da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
27751da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
27761da177e4SLinus Torvalds }
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds 
2779af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
27801da177e4SLinus Torvalds {
27811da177e4SLinus Torvalds 	struct in6_ifreq ireq;
27821da177e4SLinus Torvalds 	int err;
27831da177e4SLinus Torvalds 
2784af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
27851da177e4SLinus Torvalds 		return -EPERM;
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
27881da177e4SLinus Torvalds 		return -EFAULT;
27891da177e4SLinus Torvalds 
27901da177e4SLinus Torvalds 	rtnl_lock();
2791caeaba79SNicolas Dichtel 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2792af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2793af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
27941da177e4SLinus Torvalds 	rtnl_unlock();
27951da177e4SLinus Torvalds 	return err;
27961da177e4SLinus Torvalds }
27971da177e4SLinus Torvalds 
2798af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
27991da177e4SLinus Torvalds {
28001da177e4SLinus Torvalds 	struct in6_ifreq ireq;
28011da177e4SLinus Torvalds 	int err;
28021da177e4SLinus Torvalds 
2803af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
28041da177e4SLinus Torvalds 		return -EPERM;
28051da177e4SLinus Torvalds 
28061da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
28071da177e4SLinus Torvalds 		return -EFAULT;
28081da177e4SLinus Torvalds 
28091da177e4SLinus Torvalds 	rtnl_lock();
28106046d5b4SHeiner Kallweit 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2811af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
28121da177e4SLinus Torvalds 	rtnl_unlock();
28131da177e4SLinus Torvalds 	return err;
28141da177e4SLinus Torvalds }
28151da177e4SLinus Torvalds 
2816b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2817b5f348e5SIlpo Järvinen 		     int plen, int scope)
2818b5f348e5SIlpo Järvinen {
2819b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
2820b5f348e5SIlpo Järvinen 
28218a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, addr, NULL, plen,
282207edd741SHannes Frederic Sowa 			    scope, IFA_F_PERMANENT,
282307edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2824b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
2825b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
2826b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
2827b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
2828b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2829b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
2830b5f348e5SIlpo Järvinen 	}
2831b5f348e5SIlpo Järvinen }
2832b5f348e5SIlpo Järvinen 
283307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
28341da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
28351da177e4SLinus Torvalds {
28361da177e4SLinus Torvalds 	struct in6_addr addr;
28371da177e4SLinus Torvalds 	struct net_device *dev;
2838c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
2839929c9cf3SNicolas Dichtel 	int scope, plen;
2840f0e2acfaSNicolas Dichtel 	u32 pflags = 0;
28411da177e4SLinus Torvalds 
28421da177e4SLinus Torvalds 	ASSERT_RTNL();
28431da177e4SLinus Torvalds 
28441da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
28451da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
28461da177e4SLinus Torvalds 
28471da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
28481da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
28491da177e4SLinus Torvalds 		scope = IFA_LINK;
2850929c9cf3SNicolas Dichtel 		plen = 64;
28511da177e4SLinus Torvalds 	} else {
28521da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
2853929c9cf3SNicolas Dichtel 		plen = 96;
2854f0e2acfaSNicolas Dichtel 		pflags |= RTF_NONEXTHOP;
28551da177e4SLinus Torvalds 	}
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
2858929c9cf3SNicolas Dichtel 		add_addr(idev, &addr, plen, scope);
2859f0e2acfaSNicolas Dichtel 		addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
28601da177e4SLinus Torvalds 		return;
28611da177e4SLinus Torvalds 	}
28621da177e4SLinus Torvalds 
28636fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
2864e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
28651da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
28661da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
28671da177e4SLinus Torvalds 
28681da177e4SLinus Torvalds 			int flag = scope;
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
28711da177e4SLinus Torvalds 
28721da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
28731da177e4SLinus Torvalds 
28741da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
28751da177e4SLinus Torvalds 					continue;
28761da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
28771da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
28781da177e4SLinus Torvalds 						continue;
28791da177e4SLinus Torvalds 					flag |= IFA_HOST;
28801da177e4SLinus Torvalds 				}
28811da177e4SLinus Torvalds 
2882b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
2883f0e2acfaSNicolas Dichtel 				addrconf_prefix_route(&addr, plen, idev->dev, 0,
2884f0e2acfaSNicolas Dichtel 						      pflags);
28851da177e4SLinus Torvalds 			}
28861da177e4SLinus Torvalds 		}
28871da177e4SLinus Torvalds 	}
28881da177e4SLinus Torvalds }
28890be669bbSJoerg Roedel #endif
28901da177e4SLinus Torvalds 
28911da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
28921da177e4SLinus Torvalds {
28931da177e4SLinus Torvalds 	struct inet6_dev  *idev;
289425fb6ca4SBalakumaran Kannan 	struct net_device *sp_dev;
289525fb6ca4SBalakumaran Kannan 	struct inet6_ifaddr *sp_ifa;
289625fb6ca4SBalakumaran Kannan 	struct rt6_info *sp_rt;
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds 	/* ::1 */
28991da177e4SLinus Torvalds 
29001da177e4SLinus Torvalds 	ASSERT_RTNL();
29011da177e4SLinus Torvalds 
2902e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
290363159f29SIan Morris 	if (!idev) {
290491df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
29051da177e4SLinus Torvalds 		return;
29061da177e4SLinus Torvalds 	}
29071da177e4SLinus Torvalds 
2908b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
290925fb6ca4SBalakumaran Kannan 
291025fb6ca4SBalakumaran Kannan 	/* Add routes to other interface's IPv6 addresses */
291125fb6ca4SBalakumaran Kannan 	for_each_netdev(dev_net(dev), sp_dev) {
291225fb6ca4SBalakumaran Kannan 		if (!strcmp(sp_dev->name, dev->name))
291325fb6ca4SBalakumaran Kannan 			continue;
291425fb6ca4SBalakumaran Kannan 
291525fb6ca4SBalakumaran Kannan 		idev = __in6_dev_get(sp_dev);
291625fb6ca4SBalakumaran Kannan 		if (!idev)
291725fb6ca4SBalakumaran Kannan 			continue;
291825fb6ca4SBalakumaran Kannan 
291925fb6ca4SBalakumaran Kannan 		read_lock_bh(&idev->lock);
292025fb6ca4SBalakumaran Kannan 		list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
292125fb6ca4SBalakumaran Kannan 
292225fb6ca4SBalakumaran Kannan 			if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
292325fb6ca4SBalakumaran Kannan 				continue;
292425fb6ca4SBalakumaran Kannan 
292533d99113SGao feng 			if (sp_ifa->rt) {
292633d99113SGao feng 				/* This dst has been added to garbage list when
292733d99113SGao feng 				 * lo device down, release this obsolete dst and
292833d99113SGao feng 				 * reallocate a new router for ifa.
292933d99113SGao feng 				 */
293033d99113SGao feng 				if (sp_ifa->rt->dst.obsolete > 0) {
293133d99113SGao feng 					ip6_rt_put(sp_ifa->rt);
293233d99113SGao feng 					sp_ifa->rt = NULL;
293333d99113SGao feng 				} else {
2934a881ae1fSGao feng 					continue;
293533d99113SGao feng 				}
293633d99113SGao feng 			}
2937a881ae1fSGao feng 
293857ec0afeSFrançois-Xavier Le Bail 			sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
293925fb6ca4SBalakumaran Kannan 
294025fb6ca4SBalakumaran Kannan 			/* Failure cases are ignored */
2941534c8779SGao feng 			if (!IS_ERR(sp_rt)) {
2942534c8779SGao feng 				sp_ifa->rt = sp_rt;
294325fb6ca4SBalakumaran Kannan 				ip6_ins_rt(sp_rt);
294425fb6ca4SBalakumaran Kannan 			}
2945534c8779SGao feng 		}
294625fb6ca4SBalakumaran Kannan 		read_unlock_bh(&idev->lock);
294725fb6ca4SBalakumaran Kannan 	}
29481da177e4SLinus Torvalds }
29491da177e4SLinus Torvalds 
295064236f3fSHannes Frederic Sowa static void addrconf_add_linklocal(struct inet6_dev *idev,
295164236f3fSHannes Frederic Sowa 				   const struct in6_addr *addr, u32 flags)
29521da177e4SLinus Torvalds {
29531da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
295464236f3fSHannes Frederic Sowa 	u32 addr_flags = flags | IFA_F_PERMANENT;
29551da177e4SLinus Torvalds 
295695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
295795c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
2958702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
295995c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
296095c385b4SNeil Horman #endif
296195c385b4SNeil Horman 
296207edd741SHannes Frederic Sowa 	ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
296307edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
29641da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
296546d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2966cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
29671da177e4SLinus Torvalds 		in6_ifa_put(ifp);
29681da177e4SLinus Torvalds 	}
29691da177e4SLinus Torvalds }
29701da177e4SLinus Torvalds 
2971622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address)
2972622c81d5SHannes Frederic Sowa {
2973622c81d5SHannes Frederic Sowa 	if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
2974622c81d5SHannes Frederic Sowa 		return true;
2975622c81d5SHannes Frederic Sowa 
2976622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0x02005eff) &&
2977622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
2978622c81d5SHannes Frederic Sowa 		return true;
2979622c81d5SHannes Frederic Sowa 
2980622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0xfdffffff) &&
2981622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
2982622c81d5SHannes Frederic Sowa 		return true;
2983622c81d5SHannes Frederic Sowa 
2984622c81d5SHannes Frederic Sowa 	return false;
2985622c81d5SHannes Frederic Sowa }
2986622c81d5SHannes Frederic Sowa 
2987622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address,
2988622c81d5SHannes Frederic Sowa 					u8 dad_count,
2989622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev)
2990622c81d5SHannes Frederic Sowa {
2991622c81d5SHannes Frederic Sowa 	static DEFINE_SPINLOCK(lock);
2992622c81d5SHannes Frederic Sowa 	static __u32 digest[SHA_DIGEST_WORDS];
2993622c81d5SHannes Frederic Sowa 	static __u32 workspace[SHA_WORKSPACE_WORDS];
2994622c81d5SHannes Frederic Sowa 
2995622c81d5SHannes Frederic Sowa 	static union {
2996622c81d5SHannes Frederic Sowa 		char __data[SHA_MESSAGE_BYTES];
2997622c81d5SHannes Frederic Sowa 		struct {
2998622c81d5SHannes Frederic Sowa 			struct in6_addr secret;
2999ff40217eSHannes Frederic Sowa 			__be32 prefix[2];
3000622c81d5SHannes Frederic Sowa 			unsigned char hwaddr[MAX_ADDR_LEN];
3001622c81d5SHannes Frederic Sowa 			u8 dad_count;
3002622c81d5SHannes Frederic Sowa 		} __packed;
3003622c81d5SHannes Frederic Sowa 	} data;
3004622c81d5SHannes Frederic Sowa 
3005622c81d5SHannes Frederic Sowa 	struct in6_addr secret;
3006622c81d5SHannes Frederic Sowa 	struct in6_addr temp;
3007622c81d5SHannes Frederic Sowa 	struct net *net = dev_net(idev->dev);
3008622c81d5SHannes Frederic Sowa 
3009622c81d5SHannes Frederic Sowa 	BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3010622c81d5SHannes Frederic Sowa 
3011622c81d5SHannes Frederic Sowa 	if (idev->cnf.stable_secret.initialized)
3012622c81d5SHannes Frederic Sowa 		secret = idev->cnf.stable_secret.secret;
3013622c81d5SHannes Frederic Sowa 	else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3014622c81d5SHannes Frederic Sowa 		secret = net->ipv6.devconf_dflt->stable_secret.secret;
3015622c81d5SHannes Frederic Sowa 	else
3016622c81d5SHannes Frederic Sowa 		return -1;
3017622c81d5SHannes Frederic Sowa 
3018622c81d5SHannes Frederic Sowa retry:
3019622c81d5SHannes Frederic Sowa 	spin_lock_bh(&lock);
3020622c81d5SHannes Frederic Sowa 
3021622c81d5SHannes Frederic Sowa 	sha_init(digest);
3022622c81d5SHannes Frederic Sowa 	memset(&data, 0, sizeof(data));
3023622c81d5SHannes Frederic Sowa 	memset(workspace, 0, sizeof(workspace));
3024622c81d5SHannes Frederic Sowa 	memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3025ff40217eSHannes Frederic Sowa 	data.prefix[0] = address->s6_addr32[0];
3026ff40217eSHannes Frederic Sowa 	data.prefix[1] = address->s6_addr32[1];
3027622c81d5SHannes Frederic Sowa 	data.secret = secret;
3028622c81d5SHannes Frederic Sowa 	data.dad_count = dad_count;
3029622c81d5SHannes Frederic Sowa 
3030622c81d5SHannes Frederic Sowa 	sha_transform(digest, data.__data, workspace);
3031622c81d5SHannes Frederic Sowa 
3032622c81d5SHannes Frederic Sowa 	temp = *address;
3033ff40217eSHannes Frederic Sowa 	temp.s6_addr32[2] = (__force __be32)digest[0];
3034ff40217eSHannes Frederic Sowa 	temp.s6_addr32[3] = (__force __be32)digest[1];
3035622c81d5SHannes Frederic Sowa 
3036622c81d5SHannes Frederic Sowa 	spin_unlock_bh(&lock);
3037622c81d5SHannes Frederic Sowa 
3038622c81d5SHannes Frederic Sowa 	if (ipv6_reserved_interfaceid(temp)) {
3039622c81d5SHannes Frederic Sowa 		dad_count++;
30401855b7c3SHannes Frederic Sowa 		if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3041622c81d5SHannes Frederic Sowa 			return -1;
3042622c81d5SHannes Frederic Sowa 		goto retry;
3043622c81d5SHannes Frederic Sowa 	}
3044622c81d5SHannes Frederic Sowa 
3045622c81d5SHannes Frederic Sowa 	*address = temp;
3046622c81d5SHannes Frederic Sowa 	return 0;
3047622c81d5SHannes Frederic Sowa }
3048622c81d5SHannes Frederic Sowa 
3049cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3050cc9da6ccSBjørn Mork {
3051cc9da6ccSBjørn Mork 	struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3052cc9da6ccSBjørn Mork 
3053cc9da6ccSBjørn Mork 	if (s->initialized)
3054cc9da6ccSBjørn Mork 		return;
3055cc9da6ccSBjørn Mork 	s = &idev->cnf.stable_secret;
3056cc9da6ccSBjørn Mork 	get_random_bytes(&s->secret, sizeof(s->secret));
3057cc9da6ccSBjørn Mork 	s->initialized = true;
3058cc9da6ccSBjørn Mork }
3059cc9da6ccSBjørn Mork 
3060bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3061bc91b0f0SJiri Pirko {
3062bc91b0f0SJiri Pirko 	struct in6_addr addr;
3063bc91b0f0SJiri Pirko 
3064ca254490SDavid Ahern 	/* no link local addresses on L3 master devices */
3065ca254490SDavid Ahern 	if (netif_is_l3_master(idev->dev))
3066ca254490SDavid Ahern 		return;
3067ca254490SDavid Ahern 
3068bc91b0f0SJiri Pirko 	ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3069622c81d5SHannes Frederic Sowa 
3070cc9da6ccSBjørn Mork 	switch (idev->addr_gen_mode) {
3071cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_RANDOM:
3072cc9da6ccSBjørn Mork 		ipv6_gen_mode_random_init(idev);
3073cc9da6ccSBjørn Mork 		/* fallthrough */
3074cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3075622c81d5SHannes Frederic Sowa 		if (!ipv6_generate_stable_address(&addr, 0, idev))
307664236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr,
307764236f3fSHannes Frederic Sowa 					       IFA_F_STABLE_PRIVACY);
3078622c81d5SHannes Frederic Sowa 		else if (prefix_route)
3079622c81d5SHannes Frederic Sowa 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3080cc9da6ccSBjørn Mork 		break;
3081cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_EUI64:
3082bc91b0f0SJiri Pirko 		/* addrconf_add_linklocal also adds a prefix_route and we
3083bc91b0f0SJiri Pirko 		 * only need to care about prefix routes if ipv6_generate_eui64
3084bc91b0f0SJiri Pirko 		 * couldn't generate one.
3085bc91b0f0SJiri Pirko 		 */
3086bc91b0f0SJiri Pirko 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
308764236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr, 0);
3088bc91b0f0SJiri Pirko 		else if (prefix_route)
3089bc91b0f0SJiri Pirko 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3090cc9da6ccSBjørn Mork 		break;
3091cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_NONE:
3092cc9da6ccSBjørn Mork 	default:
3093cc9da6ccSBjørn Mork 		/* will not add any link local address */
3094cc9da6ccSBjørn Mork 		break;
3095bc91b0f0SJiri Pirko 	}
3096bc91b0f0SJiri Pirko }
3097bc91b0f0SJiri Pirko 
30981da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
30991da177e4SLinus Torvalds {
31001da177e4SLinus Torvalds 	struct inet6_dev *idev;
31011da177e4SLinus Torvalds 
31021da177e4SLinus Torvalds 	ASSERT_RTNL();
31031da177e4SLinus Torvalds 
310474235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
310574235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
310674235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
310706a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
3108e837735eSNicolas Dichtel 	    (dev->type != ARPHRD_IEEE1394) &&
3109e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_TUNNEL6) &&
3110cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_6LOWPAN) &&
3111cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_NONE)) {
311274235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
311374235a25SHerbert Xu 		return;
311474235a25SHerbert Xu 	}
311574235a25SHerbert Xu 
31161da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
311764e724f6SBrian Haley 	if (IS_ERR(idev))
31181da177e4SLinus Torvalds 		return;
31191da177e4SLinus Torvalds 
3120cc9da6ccSBjørn Mork 	/* this device type has no EUI support */
3121cc9da6ccSBjørn Mork 	if (dev->type == ARPHRD_NONE &&
3122cc9da6ccSBjørn Mork 	    idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3123cc9da6ccSBjørn Mork 		idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3124cc9da6ccSBjørn Mork 
3125bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, false);
31261da177e4SLinus Torvalds }
31271da177e4SLinus Torvalds 
312807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
31291da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
31301da177e4SLinus Torvalds {
31311da177e4SLinus Torvalds 	struct inet6_dev *idev;
31321da177e4SLinus Torvalds 
31331da177e4SLinus Torvalds 	ASSERT_RTNL();
31341da177e4SLinus Torvalds 
31351da177e4SLinus Torvalds 	/*
31361da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
31371da177e4SLinus Torvalds 	 * addresses... we should configure all of
31381da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
31391da177e4SLinus Torvalds 	 */
31401da177e4SLinus Torvalds 
3141e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
314263159f29SIan Morris 	if (!idev) {
314391df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
31441da177e4SLinus Torvalds 		return;
31451da177e4SLinus Torvalds 	}
31461da177e4SLinus Torvalds 
3147c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
3148bc91b0f0SJiri Pirko 		addrconf_addr_gen(idev, false);
3149c7dc89c0SFred L. Templin 		return;
3150c7dc89c0SFred L. Templin 	}
3151c7dc89c0SFred L. Templin 
31521da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
31531da177e4SLinus Torvalds 
315462b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
31551da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
31561da177e4SLinus Torvalds }
31570be669bbSJoerg Roedel #endif
31581da177e4SLinus Torvalds 
315907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3160aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
3161aee80b54Sstephen hemminger {
3162aee80b54Sstephen hemminger 	struct inet6_dev *idev;
3163aee80b54Sstephen hemminger 
3164aee80b54Sstephen hemminger 	ASSERT_RTNL();
3165aee80b54Sstephen hemminger 
3166e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
316763159f29SIan Morris 	if (!idev) {
316891df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
3169aee80b54Sstephen hemminger 		return;
3170aee80b54Sstephen hemminger 	}
3171aee80b54Sstephen hemminger 
3172bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, true);
3173d9e4ce65SHannes Frederic Sowa 	if (dev->flags & IFF_POINTOPOINT)
3174d9e4ce65SHannes Frederic Sowa 		addrconf_add_mroute(dev);
3175aee80b54Sstephen hemminger }
3176aee80b54Sstephen hemminger #endif
3177aee80b54Sstephen hemminger 
3178f1705ec1SDavid Ahern static int fixup_permanent_addr(struct inet6_dev *idev,
3179f1705ec1SDavid Ahern 				struct inet6_ifaddr *ifp)
3180f1705ec1SDavid Ahern {
3181f1705ec1SDavid Ahern 	if (!ifp->rt) {
3182f1705ec1SDavid Ahern 		struct rt6_info *rt;
3183f1705ec1SDavid Ahern 
3184f1705ec1SDavid Ahern 		rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3185f1705ec1SDavid Ahern 		if (unlikely(IS_ERR(rt)))
3186f1705ec1SDavid Ahern 			return PTR_ERR(rt);
3187f1705ec1SDavid Ahern 
3188f1705ec1SDavid Ahern 		ifp->rt = rt;
3189f1705ec1SDavid Ahern 	}
3190f1705ec1SDavid Ahern 
3191f1705ec1SDavid Ahern 	if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3192f1705ec1SDavid Ahern 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3193f1705ec1SDavid Ahern 				      idev->dev, 0, 0);
3194f1705ec1SDavid Ahern 	}
3195f1705ec1SDavid Ahern 
3196f1705ec1SDavid Ahern 	addrconf_dad_start(ifp);
3197f1705ec1SDavid Ahern 
3198f1705ec1SDavid Ahern 	return 0;
3199f1705ec1SDavid Ahern }
3200f1705ec1SDavid Ahern 
3201f1705ec1SDavid Ahern static void addrconf_permanent_addr(struct net_device *dev)
3202f1705ec1SDavid Ahern {
3203f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifp, *tmp;
3204f1705ec1SDavid Ahern 	struct inet6_dev *idev;
3205f1705ec1SDavid Ahern 
3206f1705ec1SDavid Ahern 	idev = __in6_dev_get(dev);
3207f1705ec1SDavid Ahern 	if (!idev)
3208f1705ec1SDavid Ahern 		return;
3209f1705ec1SDavid Ahern 
3210f1705ec1SDavid Ahern 	write_lock_bh(&idev->lock);
3211f1705ec1SDavid Ahern 
3212f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3213f1705ec1SDavid Ahern 		if ((ifp->flags & IFA_F_PERMANENT) &&
3214f1705ec1SDavid Ahern 		    fixup_permanent_addr(idev, ifp) < 0) {
3215f1705ec1SDavid Ahern 			write_unlock_bh(&idev->lock);
3216f1705ec1SDavid Ahern 			ipv6_del_addr(ifp);
3217f1705ec1SDavid Ahern 			write_lock_bh(&idev->lock);
3218f1705ec1SDavid Ahern 
3219f1705ec1SDavid Ahern 			net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3220f1705ec1SDavid Ahern 					     idev->dev->name, &ifp->addr);
3221f1705ec1SDavid Ahern 		}
3222f1705ec1SDavid Ahern 	}
3223f1705ec1SDavid Ahern 
3224f1705ec1SDavid Ahern 	write_unlock_bh(&idev->lock);
3225f1705ec1SDavid Ahern }
3226f1705ec1SDavid Ahern 
32271da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
3228351638e7SJiri Pirko 			   void *ptr)
32291da177e4SLinus Torvalds {
3230351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
32314f7f34eaSDavid Ahern 	struct netdev_notifier_changeupper_info *info;
3232748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
3233c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
3234b217d616SHerbert Xu 	int err;
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds 	switch (event) {
323745ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
323874235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
323945ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
3240a317a2f1SWANG Cong 			if (IS_ERR(idev))
3241a317a2f1SWANG Cong 				return notifier_from_errno(PTR_ERR(idev));
324245ba9dd2SYOSHIFUJI Hideaki 		}
324345ba9dd2SYOSHIFUJI Hideaki 		break;
3244bcdd553fSStephen Hemminger 
3245b7b0b1d2SAlexander Duyck 	case NETDEV_CHANGEMTU:
3246b7b0b1d2SAlexander Duyck 		/* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3247b7b0b1d2SAlexander Duyck 		if (dev->mtu < IPV6_MIN_MTU) {
3248b7b0b1d2SAlexander Duyck 			addrconf_ifdown(dev, 1);
3249b7b0b1d2SAlexander Duyck 			break;
3250b7b0b1d2SAlexander Duyck 		}
3251b7b0b1d2SAlexander Duyck 
3252b7b0b1d2SAlexander Duyck 		if (idev) {
3253b7b0b1d2SAlexander Duyck 			rt6_mtu_change(dev, dev->mtu);
3254b7b0b1d2SAlexander Duyck 			idev->cnf.mtu6 = dev->mtu;
3255b7b0b1d2SAlexander Duyck 			break;
3256b7b0b1d2SAlexander Duyck 		}
3257b7b0b1d2SAlexander Duyck 
3258b7b0b1d2SAlexander Duyck 		/* allocate new idev */
3259b7b0b1d2SAlexander Duyck 		idev = ipv6_add_dev(dev);
3260b7b0b1d2SAlexander Duyck 		if (IS_ERR(idev))
3261b7b0b1d2SAlexander Duyck 			break;
3262b7b0b1d2SAlexander Duyck 
3263b7b0b1d2SAlexander Duyck 		/* device is still not ready */
3264b7b0b1d2SAlexander Duyck 		if (!(idev->if_flags & IF_READY))
3265b7b0b1d2SAlexander Duyck 			break;
3266b7b0b1d2SAlexander Duyck 
3267b7b0b1d2SAlexander Duyck 		run_pending = 1;
3268b7b0b1d2SAlexander Duyck 
3269b7b0b1d2SAlexander Duyck 		/* fall through */
3270b7b0b1d2SAlexander Duyck 
32711da177e4SLinus Torvalds 	case NETDEV_UP:
32723c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
3273c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
3274c2edacf8SJay Vosburgh 			break;
3275c2edacf8SJay Vosburgh 
32763ce62a84SWANG Cong 		if (idev && idev->cnf.disable_ipv6)
32773ce62a84SWANG Cong 			break;
32783ce62a84SWANG Cong 
32793c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
328038bd10c4SDavid Ahern 			/* restore routes for permanent addresses */
328138bd10c4SDavid Ahern 			addrconf_permanent_addr(dev);
328238bd10c4SDavid Ahern 
3283f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
32843c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
3285f3213831SJoe Perches 				pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
32863c21edbdSYOSHIFUJI Hideaki 					dev->name);
32873c21edbdSYOSHIFUJI Hideaki 				break;
32883c21edbdSYOSHIFUJI Hideaki 			}
328999081049SKristian Slavov 
3290d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
3291d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
3292d31c7b8fSEvgeniy Polyakov 
3293a317a2f1SWANG Cong 			if (!IS_ERR_OR_NULL(idev)) {
329499081049SKristian Slavov 				idev->if_flags |= IF_READY;
3295e3ec6cfcSBenjamin Thery 				run_pending = 1;
3296e3ec6cfcSBenjamin Thery 			}
3297b7b0b1d2SAlexander Duyck 		} else if (event == NETDEV_CHANGE) {
3298f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
32993c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
33003c21edbdSYOSHIFUJI Hideaki 				break;
33013c21edbdSYOSHIFUJI Hideaki 			}
33023c21edbdSYOSHIFUJI Hideaki 
33033c21edbdSYOSHIFUJI Hideaki 			if (idev) {
3304bcdd553fSStephen Hemminger 				if (idev->if_flags & IF_READY)
33053c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
33063c21edbdSYOSHIFUJI Hideaki 					break;
33073c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
33083c21edbdSYOSHIFUJI Hideaki 			}
33093c21edbdSYOSHIFUJI Hideaki 
3310f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
33113c21edbdSYOSHIFUJI Hideaki 				dev->name);
33123c21edbdSYOSHIFUJI Hideaki 
3313c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
33143c21edbdSYOSHIFUJI Hideaki 		}
33153c21edbdSYOSHIFUJI Hideaki 
33161da177e4SLinus Torvalds 		switch (dev->type) {
331707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
33181da177e4SLinus Torvalds 		case ARPHRD_SIT:
33191da177e4SLinus Torvalds 			addrconf_sit_config(dev);
33201da177e4SLinus Torvalds 			break;
33210be669bbSJoerg Roedel #endif
332207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3323aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
3324aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
3325aee80b54Sstephen hemminger 			break;
3326aee80b54Sstephen hemminger #endif
33271da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
33281da177e4SLinus Torvalds 			init_loopback(dev);
33291da177e4SLinus Torvalds 			break;
33301da177e4SLinus Torvalds 
33311da177e4SLinus Torvalds 		default:
33321da177e4SLinus Torvalds 			addrconf_dev_config(dev);
33331da177e4SLinus Torvalds 			break;
33343ff50b79SStephen Hemminger 		}
3335bcdd553fSStephen Hemminger 
3336a317a2f1SWANG Cong 		if (!IS_ERR_OR_NULL(idev)) {
3337c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
3338c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
3339c5e33bddSYOSHIFUJI Hideaki 
3340bcdd553fSStephen Hemminger 			/*
3341bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
3342bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
3343bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
33441da177e4SLinus Torvalds 			 */
3345bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
3346bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
33471da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
33481da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
33491da177e4SLinus Torvalds 			}
33501da177e4SLinus Torvalds 			idev->tstamp = jiffies;
33511da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
3352bcdd553fSStephen Hemminger 
3353bcdd553fSStephen Hemminger 			/*
3354bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
3355bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
33561da177e4SLinus Torvalds 			 */
33571da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
3358bcdd553fSStephen Hemminger 				addrconf_ifdown(dev, 1);
33591da177e4SLinus Torvalds 		}
33601da177e4SLinus Torvalds 		break;
33611da177e4SLinus Torvalds 
33621da177e4SLinus Torvalds 	case NETDEV_DOWN:
33631da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
33641da177e4SLinus Torvalds 		/*
33651da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
33661da177e4SLinus Torvalds 		 */
33671da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
33681da177e4SLinus Torvalds 		break;
33693c21edbdSYOSHIFUJI Hideaki 
33701da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
33711da177e4SLinus Torvalds 		if (idev) {
33725632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
3373408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
3374a317a2f1SWANG Cong 			err = addrconf_sysctl_register(idev);
3375b217d616SHerbert Xu 			if (err)
3376b217d616SHerbert Xu 				return notifier_from_errno(err);
3377a317a2f1SWANG Cong 			err = snmp6_register_dev(idev);
3378a317a2f1SWANG Cong 			if (err) {
3379a317a2f1SWANG Cong 				addrconf_sysctl_unregister(idev);
3380a317a2f1SWANG Cong 				return notifier_from_errno(err);
3381a317a2f1SWANG Cong 			}
33825632c515SStephen Hemminger 		}
33831da177e4SLinus Torvalds 		break;
3384bcdd553fSStephen Hemminger 
338593d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
338693d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
33873ef0952cSAndrew Lunn 		if (idev)
338893d9b7d7SJiri Pirko 			addrconf_type_change(dev, event);
338975c78500SMoni Shoua 		break;
33904f7f34eaSDavid Ahern 
33914f7f34eaSDavid Ahern 	case NETDEV_CHANGEUPPER:
33924f7f34eaSDavid Ahern 		info = ptr;
33934f7f34eaSDavid Ahern 
33944f7f34eaSDavid Ahern 		/* flush all routes if dev is linked to or unlinked from
33954f7f34eaSDavid Ahern 		 * an L3 master device (e.g., VRF)
33964f7f34eaSDavid Ahern 		 */
33974f7f34eaSDavid Ahern 		if (info->upper_dev && netif_is_l3_master(info->upper_dev))
33984f7f34eaSDavid Ahern 			addrconf_ifdown(dev, 0);
33993ff50b79SStephen Hemminger 	}
34001da177e4SLinus Torvalds 
34011da177e4SLinus Torvalds 	return NOTIFY_OK;
34021da177e4SLinus Torvalds }
34031da177e4SLinus Torvalds 
34041da177e4SLinus Torvalds /*
34051da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
34061da177e4SLinus Torvalds  */
34071da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
34081da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
34091da177e4SLinus Torvalds };
34101da177e4SLinus Torvalds 
341193d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
341275c78500SMoni Shoua {
341375c78500SMoni Shoua 	struct inet6_dev *idev;
341475c78500SMoni Shoua 	ASSERT_RTNL();
341575c78500SMoni Shoua 
341675c78500SMoni Shoua 	idev = __in6_dev_get(dev);
341775c78500SMoni Shoua 
341893d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
341975c78500SMoni Shoua 		ipv6_mc_remap(idev);
342093d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
342175c78500SMoni Shoua 		ipv6_mc_unmap(idev);
342275c78500SMoni Shoua }
342375c78500SMoni Shoua 
342470af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr)
342570af921dSDavid Ahern {
342670af921dSDavid Ahern 	return ipv6_addr_type(addr) &
342770af921dSDavid Ahern 		(IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
342870af921dSDavid Ahern }
342970af921dSDavid Ahern 
34301da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
34311da177e4SLinus Torvalds {
3432c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
3433502a2ffdSstephen hemminger 	struct inet6_dev *idev;
3434f1705ec1SDavid Ahern 	struct inet6_ifaddr *ifa, *tmp;
3435f1705ec1SDavid Ahern 	struct list_head del_list;
3436f1705ec1SDavid Ahern 	int _keep_addr;
3437f1705ec1SDavid Ahern 	bool keep_addr;
343873a8bd74SDavid S. Miller 	int state, i;
34391da177e4SLinus Torvalds 
34401da177e4SLinus Torvalds 	ASSERT_RTNL();
34411da177e4SLinus Torvalds 
3442f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
344373a8bd74SDavid S. Miller 	neigh_ifdown(&nd_tbl, dev);
34441da177e4SLinus Torvalds 
34451da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
344663159f29SIan Morris 	if (!idev)
34471da177e4SLinus Torvalds 		return -ENODEV;
34481da177e4SLinus Torvalds 
3449bcdd553fSStephen Hemminger 	/*
3450bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3451bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
34521da177e4SLinus Torvalds 	 */
3453439e2385SDenis V. Lunev 	if (how) {
34541da177e4SLinus Torvalds 		idev->dead = 1;
34558814c4b5SYOSHIFUJI Hideaki 
34568814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3457a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
34581da177e4SLinus Torvalds 
34591da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
34601da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
34611da177e4SLinus Torvalds 
34621da177e4SLinus Torvalds 	}
34631da177e4SLinus Torvalds 
3464f1705ec1SDavid Ahern 	/* aggregate the system setting and interface setting */
3465f1705ec1SDavid Ahern 	_keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3466f1705ec1SDavid Ahern 	if (!_keep_addr)
3467f1705ec1SDavid Ahern 		_keep_addr = idev->cnf.keep_addr_on_down;
3468f1705ec1SDavid Ahern 
3469f1705ec1SDavid Ahern 	/* combine the user config with event to determine if permanent
3470f1705ec1SDavid Ahern 	 * addresses are to be removed from address hash table
3471f1705ec1SDavid Ahern 	 */
3472f1705ec1SDavid Ahern 	keep_addr = !(how || _keep_addr <= 0);
3473f1705ec1SDavid Ahern 
347473a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
347573a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
347673a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
347773a8bd74SDavid S. Miller 
347873a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
347973a8bd74SDavid S. Miller restart:
3480b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
348173a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
3482c15b1ccaSHannes Frederic Sowa 				addrconf_del_dad_work(ifa);
3483f1705ec1SDavid Ahern 				/* combined flag + permanent flag decide if
3484f1705ec1SDavid Ahern 				 * address is retained on a down event
3485f1705ec1SDavid Ahern 				 */
3486f1705ec1SDavid Ahern 				if (!keep_addr ||
348770af921dSDavid Ahern 				    !(ifa->flags & IFA_F_PERMANENT) ||
348870af921dSDavid Ahern 				    addr_is_local(&ifa->addr)) {
3489f1705ec1SDavid Ahern 					hlist_del_init_rcu(&ifa->addr_lst);
349073a8bd74SDavid S. Miller 					goto restart;
349173a8bd74SDavid S. Miller 				}
349273a8bd74SDavid S. Miller 			}
3493f1705ec1SDavid Ahern 		}
349473a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
349573a8bd74SDavid S. Miller 	}
349673a8bd74SDavid S. Miller 
34971da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
34981da177e4SLinus Torvalds 
3499b7b1bfceSHannes Frederic Sowa 	addrconf_del_rs_timer(idev);
3500b7b1bfceSHannes Frederic Sowa 
3501bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3502439e2385SDenis V. Lunev 	if (!how)
35033c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
35041da177e4SLinus Torvalds 
3505439e2385SDenis V. Lunev 	if (how && del_timer(&idev->regen_timer))
35061da177e4SLinus Torvalds 		in6_dev_put(idev);
35071da177e4SLinus Torvalds 
3508bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3509372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3510372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3511372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3512372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
35131da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
35141da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
35151da177e4SLinus Torvalds 
35161da177e4SLinus Torvalds 		if (ifa->ifpub) {
35171da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
35181da177e4SLinus Torvalds 			ifa->ifpub = NULL;
35191da177e4SLinus Torvalds 		}
35201da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
35211da177e4SLinus Torvalds 		in6_ifa_put(ifa);
35221da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
35231da177e4SLinus Torvalds 	}
35248f37ada5Sstephen hemminger 
3525f1705ec1SDavid Ahern 	/* re-combine the user config with event to determine if permanent
3526f1705ec1SDavid Ahern 	 * addresses are to be removed from the interface list
3527f1705ec1SDavid Ahern 	 */
3528f1705ec1SDavid Ahern 	keep_addr = (!how && _keep_addr > 0);
3529f1705ec1SDavid Ahern 
3530f1705ec1SDavid Ahern 	INIT_LIST_HEAD(&del_list);
3531f1705ec1SDavid Ahern 	list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
353238bd10c4SDavid Ahern 		struct rt6_info *rt = NULL;
353338bd10c4SDavid Ahern 
3534c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifa);
353584e8b803Sstephen hemminger 
35364c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
35378e8e676dSHannes Frederic Sowa 		spin_lock_bh(&ifa->lock);
3538f1705ec1SDavid Ahern 
353970af921dSDavid Ahern 		if (keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
354070af921dSDavid Ahern 		    !addr_is_local(&ifa->addr)) {
3541f1705ec1SDavid Ahern 			/* set state to skip the notifier below */
3542f1705ec1SDavid Ahern 			state = INET6_IFADDR_STATE_DEAD;
3543f1705ec1SDavid Ahern 			ifa->state = 0;
3544f1705ec1SDavid Ahern 			if (!(ifa->flags & IFA_F_NODAD))
3545f1705ec1SDavid Ahern 				ifa->flags |= IFA_F_TENTATIVE;
354638bd10c4SDavid Ahern 
354738bd10c4SDavid Ahern 			rt = ifa->rt;
354838bd10c4SDavid Ahern 			ifa->rt = NULL;
3549f1705ec1SDavid Ahern 		} else {
35504c5ff6a6SHerbert Xu 			state = ifa->state;
35514c5ff6a6SHerbert Xu 			ifa->state = INET6_IFADDR_STATE_DEAD;
3552f1705ec1SDavid Ahern 
3553f1705ec1SDavid Ahern 			list_del(&ifa->if_list);
3554f1705ec1SDavid Ahern 			list_add(&ifa->if_list, &del_list);
3555f1705ec1SDavid Ahern 		}
3556f1705ec1SDavid Ahern 
35578e8e676dSHannes Frederic Sowa 		spin_unlock_bh(&ifa->lock);
35584c5ff6a6SHerbert Xu 
355938bd10c4SDavid Ahern 		if (rt)
356038bd10c4SDavid Ahern 			ip6_del_rt(rt);
356138bd10c4SDavid Ahern 
35629d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
35631da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
3564f88c91ddSCong Wang 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
35652de79570SLorenzo Colitti 		}
356673a8bd74SDavid S. Miller 
35671da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
35681da177e4SLinus Torvalds 	}
35698f37ada5Sstephen hemminger 
35701da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
35711da177e4SLinus Torvalds 
3572f1705ec1SDavid Ahern 	/* now clean up addresses to be removed */
3573f1705ec1SDavid Ahern 	while (!list_empty(&del_list)) {
3574f1705ec1SDavid Ahern 		ifa = list_first_entry(&del_list,
3575f1705ec1SDavid Ahern 				       struct inet6_ifaddr, if_list);
3576f1705ec1SDavid Ahern 		list_del(&ifa->if_list);
3577f1705ec1SDavid Ahern 
3578f1705ec1SDavid Ahern 		in6_ifa_put(ifa);
3579f1705ec1SDavid Ahern 	}
3580f1705ec1SDavid Ahern 
3581381f4dcaSSabrina Dubroca 	/* Step 5: Discard anycast and multicast list */
3582381f4dcaSSabrina Dubroca 	if (how) {
3583381f4dcaSSabrina Dubroca 		ipv6_ac_destroy_dev(idev);
35841da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
3585381f4dcaSSabrina Dubroca 	} else {
35861da177e4SLinus Torvalds 		ipv6_mc_down(idev);
3587381f4dcaSSabrina Dubroca 	}
35881da177e4SLinus Torvalds 
35891da177e4SLinus Torvalds 	idev->tstamp = jiffies;
35901da177e4SLinus Torvalds 
3591bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3592439e2385SDenis V. Lunev 	if (how) {
3593408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
35941da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
35951da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
35961da177e4SLinus Torvalds 		in6_dev_put(idev);
35971da177e4SLinus Torvalds 	}
35981da177e4SLinus Torvalds 	return 0;
35991da177e4SLinus Torvalds }
36001da177e4SLinus Torvalds 
36011da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
36021da177e4SLinus Torvalds {
3603b7b1bfceSHannes Frederic Sowa 	struct inet6_dev *idev = (struct inet6_dev *)data;
3604caf92bc4SCong Wang 	struct net_device *dev = idev->dev;
3605b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
36061da177e4SLinus Torvalds 
3607b7b1bfceSHannes Frederic Sowa 	write_lock(&idev->lock);
36085b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
36091da177e4SLinus Torvalds 		goto out;
36101da177e4SLinus Torvalds 
36119ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
36121da177e4SLinus Torvalds 		goto out;
36135b2a1953Sstephen hemminger 
36145b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
36155b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
36165b2a1953Sstephen hemminger 		goto out;
36171da177e4SLinus Torvalds 
3618b7b1bfceSHannes Frederic Sowa 	if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
3619caf92bc4SCong Wang 		write_unlock(&idev->lock);
3620caf92bc4SCong Wang 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3621caf92bc4SCong Wang 			ndisc_send_rs(dev, &lladdr,
3622b7b1bfceSHannes Frederic Sowa 				      &in6addr_linklocal_allrouters);
3623b7b1bfceSHannes Frederic Sowa 		else
3624caf92bc4SCong Wang 			goto put;
3625b7b1bfceSHannes Frederic Sowa 
3626caf92bc4SCong Wang 		write_lock(&idev->lock);
36271da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
3628b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3629b7b1bfceSHannes Frederic Sowa 					     idev->cnf.rtr_solicits) ?
36305b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_delay :
36315b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_interval);
36321da177e4SLinus Torvalds 	} else {
36331da177e4SLinus Torvalds 		/*
36341da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
36351da177e4SLinus Torvalds 		 * assumption any longer.
36361da177e4SLinus Torvalds 		 */
363791df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
36381da177e4SLinus Torvalds 	}
36391da177e4SLinus Torvalds 
36401da177e4SLinus Torvalds out:
3641b7b1bfceSHannes Frederic Sowa 	write_unlock(&idev->lock);
3642caf92bc4SCong Wang put:
3643b7b1bfceSHannes Frederic Sowa 	in6_dev_put(idev);
36441da177e4SLinus Torvalds }
36451da177e4SLinus Torvalds 
36461da177e4SLinus Torvalds /*
36471da177e4SLinus Torvalds  *	Duplicate Address Detection
36481da177e4SLinus Torvalds  */
36493c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
36503c21edbdSYOSHIFUJI Hideaki {
36513c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
36523c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
36533c21edbdSYOSHIFUJI Hideaki 
365495c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
365595c385b4SNeil Horman 		rand_num = 0;
365695c385b4SNeil Horman 	else
365763862b5bSAruna-Hewapathirane 		rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
365895c385b4SNeil Horman 
3659b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes = idev->cnf.dad_transmits;
3660c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, rand_num);
36613c21edbdSYOSHIFUJI Hideaki }
36623c21edbdSYOSHIFUJI Hideaki 
3663c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
36641da177e4SLinus Torvalds {
36651da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
36661da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
366716186a82Ssubashab@codeaurora.org 	bool notify = false;
36681da177e4SLinus Torvalds 
36691da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
36701da177e4SLinus Torvalds 
367163862b5bSAruna-Hewapathirane 	prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3674622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3675e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
36761da177e4SLinus Torvalds 		goto out;
36771da177e4SLinus Torvalds 
36781da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
36791b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
368055ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
368155ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3682cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
368321809fafSstephen hemminger 		spin_unlock(&ifp->lock);
36841da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
36851da177e4SLinus Torvalds 
36861da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
36871da177e4SLinus Torvalds 		return;
36881da177e4SLinus Torvalds 	}
36891da177e4SLinus Torvalds 
36906732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
369121809fafSstephen hemminger 		spin_unlock(&ifp->lock);
36926732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
36933c21edbdSYOSHIFUJI Hideaki 		/*
3694590a9887SMasatake YAMATO 		 * If the device is not ready:
36953c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
36963c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
36973c21edbdSYOSHIFUJI Hideaki 		 */
36983c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
3699cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
37006732badeSYOSHIFUJI Hideaki 		return;
37013c21edbdSYOSHIFUJI Hideaki 	}
370295c385b4SNeil Horman 
370395c385b4SNeil Horman 	/*
370495c385b4SNeil Horman 	 * Optimistic nodes can start receiving
370595c385b4SNeil Horman 	 * Frames right away
370695c385b4SNeil Horman 	 */
37077fd2561eSErik Kline 	if (ifp->flags & IFA_F_OPTIMISTIC) {
370895c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
37097fd2561eSErik Kline 		if (ipv6_use_optimistic_addr(idev)) {
37107fd2561eSErik Kline 			/* Because optimistic nodes can use this address,
37117fd2561eSErik Kline 			 * notify listeners. If DAD fails, RTM_DELADDR is sent.
37127fd2561eSErik Kline 			 */
371316186a82Ssubashab@codeaurora.org 			notify = true;
37147fd2561eSErik Kline 		}
37157fd2561eSErik Kline 	}
371695c385b4SNeil Horman 
37176732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
37181da177e4SLinus Torvalds out:
3719622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
37201da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
372116186a82Ssubashab@codeaurora.org 	if (notify)
372216186a82Ssubashab@codeaurora.org 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
37231da177e4SLinus Torvalds }
37241da177e4SLinus Torvalds 
3725c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp)
37261da177e4SLinus Torvalds {
3727c15b1ccaSHannes Frederic Sowa 	bool begin_dad = false;
3728c15b1ccaSHannes Frederic Sowa 
37298e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3730c15b1ccaSHannes Frederic Sowa 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3731c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_PREDAD;
3732c15b1ccaSHannes Frederic Sowa 		begin_dad = true;
3733c15b1ccaSHannes Frederic Sowa 	}
37348e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3735c15b1ccaSHannes Frederic Sowa 
3736c15b1ccaSHannes Frederic Sowa 	if (begin_dad)
3737c15b1ccaSHannes Frederic Sowa 		addrconf_mod_dad_work(ifp, 0);
3738c15b1ccaSHannes Frederic Sowa }
3739c15b1ccaSHannes Frederic Sowa 
3740c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w)
3741c15b1ccaSHannes Frederic Sowa {
3742c15b1ccaSHannes Frederic Sowa 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3743c15b1ccaSHannes Frederic Sowa 						struct inet6_ifaddr,
3744c15b1ccaSHannes Frederic Sowa 						dad_work);
37451da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
37461da177e4SLinus Torvalds 	struct in6_addr mcaddr;
37471da177e4SLinus Torvalds 
3748c15b1ccaSHannes Frederic Sowa 	enum {
3749c15b1ccaSHannes Frederic Sowa 		DAD_PROCESS,
3750c15b1ccaSHannes Frederic Sowa 		DAD_BEGIN,
3751c15b1ccaSHannes Frederic Sowa 		DAD_ABORT,
3752c15b1ccaSHannes Frederic Sowa 	} action = DAD_PROCESS;
3753c15b1ccaSHannes Frederic Sowa 
3754c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
3755c15b1ccaSHannes Frederic Sowa 
37568e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3757c15b1ccaSHannes Frederic Sowa 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3758c15b1ccaSHannes Frederic Sowa 		action = DAD_BEGIN;
3759c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_DAD;
3760c15b1ccaSHannes Frederic Sowa 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3761c15b1ccaSHannes Frederic Sowa 		action = DAD_ABORT;
3762c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
3763c15b1ccaSHannes Frederic Sowa 	}
37648e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3765c15b1ccaSHannes Frederic Sowa 
3766c15b1ccaSHannes Frederic Sowa 	if (action == DAD_BEGIN) {
3767c15b1ccaSHannes Frederic Sowa 		addrconf_dad_begin(ifp);
3768c15b1ccaSHannes Frederic Sowa 		goto out;
3769c15b1ccaSHannes Frederic Sowa 	} else if (action == DAD_ABORT) {
3770c15b1ccaSHannes Frederic Sowa 		addrconf_dad_stop(ifp, 1);
3771c15b1ccaSHannes Frederic Sowa 		goto out;
3772c15b1ccaSHannes Frederic Sowa 	}
3773c15b1ccaSHannes Frederic Sowa 
3774b7b1bfceSHannes Frederic Sowa 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
3775f2344a13SHerbert Xu 		goto out;
3776f2344a13SHerbert Xu 
3777c15b1ccaSHannes Frederic Sowa 	write_lock_bh(&idev->lock);
3778122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
3779c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
37801da177e4SLinus Torvalds 		goto out;
37811da177e4SLinus Torvalds 	}
378221809fafSstephen hemminger 
378321809fafSstephen hemminger 	spin_lock(&ifp->lock);
3784622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3785622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
3786c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
3787622ccdf1SHerbert Xu 		goto out;
3788622ccdf1SHerbert Xu 	}
3789622ccdf1SHerbert Xu 
3790b7b1bfceSHannes Frederic Sowa 	if (ifp->dad_probes == 0) {
37911da177e4SLinus Torvalds 		/*
37921da177e4SLinus Torvalds 		 * DAD was successful
37931da177e4SLinus Torvalds 		 */
37941da177e4SLinus Torvalds 
3795cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
379621809fafSstephen hemminger 		spin_unlock(&ifp->lock);
3797c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
37981da177e4SLinus Torvalds 
37991da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
38001da177e4SLinus Torvalds 
38011da177e4SLinus Torvalds 		goto out;
38021da177e4SLinus Torvalds 	}
38031da177e4SLinus Torvalds 
3804b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes--;
3805c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp,
38061f9248e5SJiri Pirko 			      NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
380721809fafSstephen hemminger 	spin_unlock(&ifp->lock);
3808c15b1ccaSHannes Frederic Sowa 	write_unlock_bh(&idev->lock);
38091da177e4SLinus Torvalds 
38101da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
38111da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3812304d888bSNicolas Dichtel 	ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any);
38131da177e4SLinus Torvalds out:
38141da177e4SLinus Torvalds 	in6_ifa_put(ifp);
3815c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
38161da177e4SLinus Torvalds }
38171da177e4SLinus Torvalds 
381811ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */
381911ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
382011ffff75SHannes Frederic Sowa {
382111ffff75SHannes Frederic Sowa 	struct inet6_ifaddr *ifpiter;
382211ffff75SHannes Frederic Sowa 	struct inet6_dev *idev = ifp->idev;
382311ffff75SHannes Frederic Sowa 
3824602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3825602582caSHannes Frederic Sowa 		if (ifpiter->scope > IFA_LINK)
3826602582caSHannes Frederic Sowa 			break;
382711ffff75SHannes Frederic Sowa 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
382811ffff75SHannes Frederic Sowa 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
382911ffff75SHannes Frederic Sowa 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
383011ffff75SHannes Frederic Sowa 		    IFA_F_PERMANENT)
383111ffff75SHannes Frederic Sowa 			return false;
383211ffff75SHannes Frederic Sowa 	}
383311ffff75SHannes Frederic Sowa 	return true;
383411ffff75SHannes Frederic Sowa }
383511ffff75SHannes Frederic Sowa 
38361da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
38371da177e4SLinus Torvalds {
38381da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
3839b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
3840b173ee48SHannes Frederic Sowa 	bool send_rs, send_mld;
3841b7b1bfceSHannes Frederic Sowa 
3842c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
38431da177e4SLinus Torvalds 
38441da177e4SLinus Torvalds 	/*
38451da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
38461da177e4SLinus Torvalds 	 */
38471da177e4SLinus Torvalds 
38481da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
38491da177e4SLinus Torvalds 
3850026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
3851026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
38521da177e4SLinus Torvalds 	 */
38531da177e4SLinus Torvalds 
38541ec047ebSHannes Frederic Sowa 	read_lock_bh(&ifp->idev->lock);
385511ffff75SHannes Frederic Sowa 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3856b173ee48SHannes Frederic Sowa 	send_rs = send_mld &&
3857b173ee48SHannes Frederic Sowa 		  ipv6_accept_ra(ifp->idev) &&
3858b173ee48SHannes Frederic Sowa 		  ifp->idev->cnf.rtr_solicits > 0 &&
3859b173ee48SHannes Frederic Sowa 		  (dev->flags&IFF_LOOPBACK) == 0;
38601ec047ebSHannes Frederic Sowa 	read_unlock_bh(&ifp->idev->lock);
38611ec047ebSHannes Frederic Sowa 
3862b173ee48SHannes Frederic Sowa 	/* While dad is in progress mld report's source address is in6_addrany.
3863b173ee48SHannes Frederic Sowa 	 * Resend with proper ll now.
3864b173ee48SHannes Frederic Sowa 	 */
3865b173ee48SHannes Frederic Sowa 	if (send_mld)
3866b173ee48SHannes Frederic Sowa 		ipv6_mc_dad_complete(ifp->idev);
3867b173ee48SHannes Frederic Sowa 
38681ec047ebSHannes Frederic Sowa 	if (send_rs) {
38691da177e4SLinus Torvalds 		/*
38701da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
38711da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
38721da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
38731da177e4SLinus Torvalds 		 */
38741ec047ebSHannes Frederic Sowa 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3875b7b1bfceSHannes Frederic Sowa 			return;
38761ec047ebSHannes Frederic Sowa 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
38771da177e4SLinus Torvalds 
3878b7b1bfceSHannes Frederic Sowa 		write_lock_bh(&ifp->idev->lock);
3879b7b1bfceSHannes Frederic Sowa 		spin_lock(&ifp->lock);
3880b7b1bfceSHannes Frederic Sowa 		ifp->idev->rs_probes = 1;
38811da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
3882b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(ifp->idev,
3883b7b1bfceSHannes Frederic Sowa 				      ifp->idev->cnf.rtr_solicit_interval);
3884b7b1bfceSHannes Frederic Sowa 		spin_unlock(&ifp->lock);
3885b7b1bfceSHannes Frederic Sowa 		write_unlock_bh(&ifp->idev->lock);
38861da177e4SLinus Torvalds 	}
38871da177e4SLinus Torvalds }
38881da177e4SLinus Torvalds 
3889e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev)
3890e21e8467SStephen Hemminger {
3891c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
3892c5e33bddSYOSHIFUJI Hideaki 
3893c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
3894502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
389521809fafSstephen hemminger 		spin_lock(&ifp->lock);
3896f2344a13SHerbert Xu 		if (ifp->flags & IFA_F_TENTATIVE &&
3897f2344a13SHerbert Xu 		    ifp->state == INET6_IFADDR_STATE_DAD)
3898c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
3899f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
3900c5e33bddSYOSHIFUJI Hideaki 	}
3901c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
3902c5e33bddSYOSHIFUJI Hideaki }
3903c5e33bddSYOSHIFUJI Hideaki 
39041da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
39051da177e4SLinus Torvalds struct if6_iter_state {
39063c40090aSDaniel Lezcano 	struct seq_net_private p;
39071da177e4SLinus Torvalds 	int bucket;
39081d578303SMihai Maruseac 	int offset;
39091da177e4SLinus Torvalds };
39101da177e4SLinus Torvalds 
39111d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
39121da177e4SLinus Torvalds {
39131da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
39141da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
39151218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
39161d578303SMihai Maruseac 	int p = 0;
39171da177e4SLinus Torvalds 
39181d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
39191d578303SMihai Maruseac 	if (pos == 0) {
39201d578303SMihai Maruseac 		state->bucket = 0;
39211d578303SMihai Maruseac 		state->offset = 0;
39221d578303SMihai Maruseac 	}
39231d578303SMihai Maruseac 
39241d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3925b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
39261d578303SMihai Maruseac 					 addr_lst) {
39279f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
39289f0d3c27SEric Dumazet 				continue;
39291d578303SMihai Maruseac 			/* sync with offset */
39301d578303SMihai Maruseac 			if (p < state->offset) {
39311d578303SMihai Maruseac 				p++;
39321d578303SMihai Maruseac 				continue;
39331d578303SMihai Maruseac 			}
39341d578303SMihai Maruseac 			state->offset++;
39351da177e4SLinus Torvalds 			return ifa;
39361da177e4SLinus Torvalds 		}
39371d578303SMihai Maruseac 
39381d578303SMihai Maruseac 		/* prepare for next bucket */
39391d578303SMihai Maruseac 		state->offset = 0;
39401d578303SMihai Maruseac 		p = 0;
39411d578303SMihai Maruseac 	}
3942c2e21293Sstephen hemminger 	return NULL;
3943c2e21293Sstephen hemminger }
39441da177e4SLinus Torvalds 
3945c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3946c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
39471da177e4SLinus Torvalds {
39481da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
39491218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
39501da177e4SLinus Torvalds 
3951b67bfe0dSSasha Levin 	hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
39529f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
39539f0d3c27SEric Dumazet 			continue;
39541d578303SMihai Maruseac 		state->offset++;
39551da177e4SLinus Torvalds 		return ifa;
39561d578303SMihai Maruseac 	}
39571da177e4SLinus Torvalds 
3958c2e21293Sstephen hemminger 	while (++state->bucket < IN6_ADDR_HSIZE) {
39591d578303SMihai Maruseac 		state->offset = 0;
3960b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa,
3961c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
39629f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
39639f0d3c27SEric Dumazet 				continue;
39641d578303SMihai Maruseac 			state->offset++;
3965c2e21293Sstephen hemminger 			return ifa;
3966c2e21293Sstephen hemminger 		}
3967c2e21293Sstephen hemminger 	}
3968c2e21293Sstephen hemminger 
3969c2e21293Sstephen hemminger 	return NULL;
3970c2e21293Sstephen hemminger }
3971c2e21293Sstephen hemminger 
39721da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
39734f70eccaSEric Dumazet 	__acquires(rcu_bh)
39741da177e4SLinus Torvalds {
39755c578aedSstephen hemminger 	rcu_read_lock_bh();
39761d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
39771da177e4SLinus Torvalds }
39781da177e4SLinus Torvalds 
39791da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
39801da177e4SLinus Torvalds {
39811da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
39821da177e4SLinus Torvalds 
39831da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
39841da177e4SLinus Torvalds 	++*pos;
39851da177e4SLinus Torvalds 	return ifa;
39861da177e4SLinus Torvalds }
39871da177e4SLinus Torvalds 
39881da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
39894f70eccaSEric Dumazet 	__releases(rcu_bh)
39901da177e4SLinus Torvalds {
39915c578aedSstephen hemminger 	rcu_read_unlock_bh();
39921da177e4SLinus Torvalds }
39931da177e4SLinus Torvalds 
39941da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
39951da177e4SLinus Torvalds {
39961da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3997971a351cSJiri Pirko 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3998b071195dSHarvey Harrison 		   &ifp->addr,
39991da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
40001da177e4SLinus Torvalds 		   ifp->prefix_len,
40011da177e4SLinus Torvalds 		   ifp->scope,
4002971a351cSJiri Pirko 		   (u8) ifp->flags,
40031da177e4SLinus Torvalds 		   ifp->idev->dev->name);
40041da177e4SLinus Torvalds 	return 0;
40051da177e4SLinus Torvalds }
40061da177e4SLinus Torvalds 
400756b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
40081da177e4SLinus Torvalds 	.start	= if6_seq_start,
40091da177e4SLinus Torvalds 	.next	= if6_seq_next,
40101da177e4SLinus Torvalds 	.show	= if6_seq_show,
40111da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
40121da177e4SLinus Torvalds };
40131da177e4SLinus Torvalds 
40141da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
40151da177e4SLinus Torvalds {
40163c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
4017cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
40181da177e4SLinus Torvalds }
40191da177e4SLinus Torvalds 
40209a32144eSArjan van de Ven static const struct file_operations if6_fops = {
40211da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
40221da177e4SLinus Torvalds 	.open		= if6_seq_open,
40231da177e4SLinus Torvalds 	.read		= seq_read,
40241da177e4SLinus Torvalds 	.llseek		= seq_lseek,
40253c40090aSDaniel Lezcano 	.release	= seq_release_net,
40261da177e4SLinus Torvalds };
40271da177e4SLinus Torvalds 
40282c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
40291da177e4SLinus Torvalds {
4030d4beaa66SGao feng 	if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
40311da177e4SLinus Torvalds 		return -ENOMEM;
40321da177e4SLinus Torvalds 	return 0;
40331da177e4SLinus Torvalds }
40341da177e4SLinus Torvalds 
40352c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
40363c40090aSDaniel Lezcano {
4037ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
40383c40090aSDaniel Lezcano }
40393c40090aSDaniel Lezcano 
40403c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
40413c40090aSDaniel Lezcano 	.init = if6_proc_net_init,
40423c40090aSDaniel Lezcano 	.exit = if6_proc_net_exit,
40433c40090aSDaniel Lezcano };
40443c40090aSDaniel Lezcano 
40453c40090aSDaniel Lezcano int __init if6_proc_init(void)
40463c40090aSDaniel Lezcano {
40473c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
40483c40090aSDaniel Lezcano }
40493c40090aSDaniel Lezcano 
40501da177e4SLinus Torvalds void if6_proc_exit(void)
40511da177e4SLinus Torvalds {
40523c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
40531da177e4SLinus Torvalds }
40541da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
40551da177e4SLinus Torvalds 
405607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
40573b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
4058b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
40593b9f9a1cSNoriaki TAKAMIYA {
40603b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
4061c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
4062ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
4063c2e21293Sstephen hemminger 
40645c578aedSstephen hemminger 	rcu_read_lock_bh();
4065b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
4066878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
4067389f6612SDaniel Lezcano 			continue;
4068caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
40693b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
40703b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
40713b9f9a1cSNoriaki TAKAMIYA 			break;
40723b9f9a1cSNoriaki TAKAMIYA 		}
40733b9f9a1cSNoriaki TAKAMIYA 	}
40745c578aedSstephen hemminger 	rcu_read_unlock_bh();
40753b9f9a1cSNoriaki TAKAMIYA 	return ret;
40763b9f9a1cSNoriaki TAKAMIYA }
40773b9f9a1cSNoriaki TAKAMIYA #endif
40783b9f9a1cSNoriaki TAKAMIYA 
40791da177e4SLinus Torvalds /*
40801da177e4SLinus Torvalds  *	Periodic address status verification
40811da177e4SLinus Torvalds  */
40821da177e4SLinus Torvalds 
4083c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void)
40841da177e4SLinus Torvalds {
4085b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
40861da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
40871da177e4SLinus Torvalds 	int i;
40881da177e4SLinus Torvalds 
4089c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4090c15b1ccaSHannes Frederic Sowa 
40915c578aedSstephen hemminger 	rcu_read_lock_bh();
40921da177e4SLinus Torvalds 	now = jiffies;
4093b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
40941da177e4SLinus Torvalds 
4095c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
40961da177e4SLinus Torvalds 
40971da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
40981da177e4SLinus Torvalds restart:
4099c15b1ccaSHannes Frederic Sowa 		hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
41001da177e4SLinus Torvalds 			unsigned long age;
41011da177e4SLinus Torvalds 
4102fad8da3eSYasushi Asano 			/* When setting preferred_lft to a value not zero or
4103fad8da3eSYasushi Asano 			 * infinity, while valid_lft is infinity
4104fad8da3eSYasushi Asano 			 * IFA_F_PERMANENT has a non-infinity life time.
4105fad8da3eSYasushi Asano 			 */
4106fad8da3eSYasushi Asano 			if ((ifp->flags & IFA_F_PERMANENT) &&
4107fad8da3eSYasushi Asano 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
41081da177e4SLinus Torvalds 				continue;
41091da177e4SLinus Torvalds 
41101da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
4111b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
4112b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
41131da177e4SLinus Torvalds 
41148f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
41158f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
41161da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
41171da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
41181da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
41191da177e4SLinus Torvalds 				goto restart;
41208f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
41218f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
41228f27ebb9SYOSHIFUJI Hideaki 				continue;
41231da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
4124a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
41251da177e4SLinus Torvalds 				int deprecate = 0;
41261da177e4SLinus Torvalds 
41271da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
41281da177e4SLinus Torvalds 					deprecate = 1;
41291da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
41301da177e4SLinus Torvalds 				}
41311da177e4SLinus Torvalds 
4132fad8da3eSYasushi Asano 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4133fad8da3eSYasushi Asano 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
41341da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
41351da177e4SLinus Torvalds 
41361da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
41371da177e4SLinus Torvalds 
41381da177e4SLinus Torvalds 				if (deprecate) {
41391da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
41401da177e4SLinus Torvalds 
41411da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
41421da177e4SLinus Torvalds 					in6_ifa_put(ifp);
41431da177e4SLinus Torvalds 					goto restart;
41441da177e4SLinus Torvalds 				}
41451da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
41461da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
414788949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
414888949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
41491f9248e5SJiri Pirko 					NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
415088949cf4Sstephen hemminger 
41511da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
41521da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
41531da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
41541da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
41551da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
41561da177e4SLinus Torvalds 						ifp->regen_count++;
41571da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
41581da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
41591da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
41605c578aedSstephen hemminger 
4161291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
4162291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
4163291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
41641da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
41651da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
41661da177e4SLinus Torvalds 						in6_ifa_put(ifp);
41671da177e4SLinus Torvalds 						goto restart;
41681da177e4SLinus Torvalds 					}
41691da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
41701da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
41711da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
41721da177e4SLinus Torvalds 			} else {
41731da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
41741da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
41751da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
41761da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
41771da177e4SLinus Torvalds 			}
41781da177e4SLinus Torvalds 		}
41791da177e4SLinus Torvalds 	}
41801da177e4SLinus Torvalds 
4181b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
4182b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
4183b2db7564SYOSHIFUJI Hideaki 
4184b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
4185b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4186b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
4187b2db7564SYOSHIFUJI Hideaki 
4188b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4189b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4190b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4191b2db7564SYOSHIFUJI Hideaki 
4192ba3542e1Sdingtianhong 	ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4193ba3542e1Sdingtianhong 	      now, next, next_sec, next_sched);
4194c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
41955c578aedSstephen hemminger 	rcu_read_unlock_bh();
41961da177e4SLinus Torvalds }
41971da177e4SLinus Torvalds 
4198c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w)
4199c15b1ccaSHannes Frederic Sowa {
4200c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
4201c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4202c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
4203c15b1ccaSHannes Frederic Sowa }
4204c15b1ccaSHannes Frederic Sowa 
4205c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void)
4206c15b1ccaSHannes Frederic Sowa {
4207c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4208c15b1ccaSHannes Frederic Sowa }
4209c15b1ccaSHannes Frederic Sowa 
4210caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4211caeaba79SNicolas Dichtel 				     struct in6_addr **peer_pfx)
4212461d8837SThomas Graf {
4213461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
4214461d8837SThomas Graf 
4215caeaba79SNicolas Dichtel 	*peer_pfx = NULL;
4216caeaba79SNicolas Dichtel 
4217461d8837SThomas Graf 	if (addr)
4218461d8837SThomas Graf 		pfx = nla_data(addr);
4219461d8837SThomas Graf 
4220461d8837SThomas Graf 	if (local) {
4221461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4222caeaba79SNicolas Dichtel 			*peer_pfx = pfx;
4223461d8837SThomas Graf 		pfx = nla_data(local);
4224461d8837SThomas Graf 	}
4225461d8837SThomas Graf 
4226461d8837SThomas Graf 	return pfx;
4227461d8837SThomas Graf }
4228461d8837SThomas Graf 
4229ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4230461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
4231461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
4232461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
4233479840ffSJiri Pirko 	[IFA_FLAGS]		= { .len = sizeof(u32) },
4234461d8837SThomas Graf };
4235461d8837SThomas Graf 
42361da177e4SLinus Torvalds static int
4237661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
42381da177e4SLinus Torvalds {
42393b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4240b933f716SThomas Graf 	struct ifaddrmsg *ifm;
4241b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4242caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
42436046d5b4SHeiner Kallweit 	u32 ifa_flags;
4244b933f716SThomas Graf 	int err;
42451da177e4SLinus Torvalds 
4246b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4247b933f716SThomas Graf 	if (err < 0)
4248b933f716SThomas Graf 		return err;
4249b933f716SThomas Graf 
4250b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
4251caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
425263159f29SIan Morris 	if (!pfx)
42531da177e4SLinus Torvalds 		return -EINVAL;
42541da177e4SLinus Torvalds 
42556046d5b4SHeiner Kallweit 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
42566046d5b4SHeiner Kallweit 
42576046d5b4SHeiner Kallweit 	/* We ignore other flags so far. */
42586046d5b4SHeiner Kallweit 	ifa_flags &= IFA_F_MANAGETEMPADDR;
42596046d5b4SHeiner Kallweit 
42606046d5b4SHeiner Kallweit 	return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
42616046d5b4SHeiner Kallweit 			      ifm->ifa_prefixlen);
42621da177e4SLinus Torvalds }
42631da177e4SLinus Torvalds 
4264479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
426555ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
4266081bba5bSNoriaki TAKAMIYA {
42676f704992SYOSHIFUJI Hideaki 	u32 flags;
42686f704992SYOSHIFUJI Hideaki 	clock_t expires;
42694bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
427053bd6749SJiri Pirko 	bool was_managetempaddr;
42715b84efecSThomas Haller 	bool had_prefixroute;
427246d48046SYOSHIFUJI Hideaki 
4273c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4274c15b1ccaSHannes Frederic Sowa 
4275081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
4276081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
4277081bba5bSNoriaki TAKAMIYA 
427853bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR &&
427953bd6749SJiri Pirko 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
428053bd6749SJiri Pirko 		return -EINVAL;
428153bd6749SJiri Pirko 
42824bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
42834bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
42844bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
42854bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
42866f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
42874bed72e4SYOSHIFUJI Hideaki 	} else {
42884bed72e4SYOSHIFUJI Hideaki 		expires = 0;
42894bed72e4SYOSHIFUJI Hideaki 		flags = 0;
42904bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
42916f704992SYOSHIFUJI Hideaki 	}
4292081bba5bSNoriaki TAKAMIYA 
42934bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
42944bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
42954bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
429655ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
42974bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
42984bed72e4SYOSHIFUJI Hideaki 	}
4299081bba5bSNoriaki TAKAMIYA 
4300081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
430153bd6749SJiri Pirko 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
43025b84efecSThomas Haller 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
43035b84efecSThomas Haller 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
430453bd6749SJiri Pirko 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4305761aac73SThomas Haller 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4306761aac73SThomas Haller 			IFA_F_NOPREFIXROUTE);
430753bd6749SJiri Pirko 	ifp->flags |= ifa_flags;
4308081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
4309081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
4310081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
4311081bba5bSNoriaki TAKAMIYA 
4312081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
4313081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
4314081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
4315081bba5bSNoriaki TAKAMIYA 
4316761aac73SThomas Haller 	if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
431746d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
43186f704992SYOSHIFUJI Hideaki 				      expires, flags);
43195b84efecSThomas Haller 	} else if (had_prefixroute) {
43205b84efecSThomas Haller 		enum cleanup_prefix_rt_t action;
43215b84efecSThomas Haller 		unsigned long rt_expires;
43225b84efecSThomas Haller 
43235b84efecSThomas Haller 		write_lock_bh(&ifp->idev->lock);
43245b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &rt_expires);
43255b84efecSThomas Haller 		write_unlock_bh(&ifp->idev->lock);
43265b84efecSThomas Haller 
43275b84efecSThomas Haller 		if (action != CLEANUP_PREFIX_RT_NOP) {
43285b84efecSThomas Haller 			cleanup_prefix_route(ifp, rt_expires,
43295b84efecSThomas Haller 				action == CLEANUP_PREFIX_RT_DEL);
43305b84efecSThomas Haller 		}
4331761aac73SThomas Haller 	}
433253bd6749SJiri Pirko 
433353bd6749SJiri Pirko 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
433453bd6749SJiri Pirko 		if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
433553bd6749SJiri Pirko 			valid_lft = prefered_lft = 0;
433653bd6749SJiri Pirko 		manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
433753bd6749SJiri Pirko 				 !was_managetempaddr, jiffies);
433853bd6749SJiri Pirko 	}
433953bd6749SJiri Pirko 
4340c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4341081bba5bSNoriaki TAKAMIYA 
4342081bba5bSNoriaki TAKAMIYA 	return 0;
4343081bba5bSNoriaki TAKAMIYA }
4344081bba5bSNoriaki TAKAMIYA 
4345081bba5bSNoriaki TAKAMIYA static int
4346661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
43471da177e4SLinus Torvalds {
43483b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4349461d8837SThomas Graf 	struct ifaddrmsg *ifm;
4350461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4351caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
43527198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
43537198f8ceSThomas Graf 	struct net_device *dev;
435455ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4355479840ffSJiri Pirko 	u32 ifa_flags;
4356461d8837SThomas Graf 	int err;
43571da177e4SLinus Torvalds 
4358461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4359461d8837SThomas Graf 	if (err < 0)
4360461d8837SThomas Graf 		return err;
4361461d8837SThomas Graf 
4362461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
4363caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
436463159f29SIan Morris 	if (!pfx)
43651da177e4SLinus Torvalds 		return -EINVAL;
43661da177e4SLinus Torvalds 
4367461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
43680778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
4369461d8837SThomas Graf 
4370461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
43710778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
4372461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
4373461d8837SThomas Graf 	} else {
4374461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
4375461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
43760778769dSNoriaki TAKAMIYA 	}
43770778769dSNoriaki TAKAMIYA 
4378af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
437963159f29SIan Morris 	if (!dev)
43807198f8ceSThomas Graf 		return -ENODEV;
4381081bba5bSNoriaki TAKAMIYA 
4382479840ffSJiri Pirko 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4383479840ffSJiri Pirko 
438455ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
4385761aac73SThomas Haller 	ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
438693a714d6SMadhu Challa 		     IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
438755ebaef1SNoriaki TAKAMIYA 
43881cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
438963159f29SIan Morris 	if (!ifa) {
43907198f8ceSThomas Graf 		/*
43917198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
4392db9c7c39Sstephen hemminger 		 * userspace already relies on not having to provide this.
43937198f8ceSThomas Graf 		 */
4394caeaba79SNicolas Dichtel 		return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4395af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
4396af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
43971da177e4SLinus Torvalds 	}
43981da177e4SLinus Torvalds 
43997198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
44007198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
44017198f8ceSThomas Graf 		err = -EEXIST;
44027198f8ceSThomas Graf 	else
440355ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
44047198f8ceSThomas Graf 
44057198f8ceSThomas Graf 	in6_ifa_put(ifa);
44067198f8ceSThomas Graf 
44077198f8ceSThomas Graf 	return err;
44087198f8ceSThomas Graf }
44097198f8ceSThomas Graf 
4410479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4411101bb229SThomas Graf 			  u8 scope, int ifindex)
4412101bb229SThomas Graf {
4413101bb229SThomas Graf 	struct ifaddrmsg *ifm;
4414101bb229SThomas Graf 
4415101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
4416101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
4417101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
4418101bb229SThomas Graf 	ifm->ifa_flags = flags;
4419101bb229SThomas Graf 	ifm->ifa_scope = scope;
4420101bb229SThomas Graf 	ifm->ifa_index = ifindex;
4421101bb229SThomas Graf }
4422101bb229SThomas Graf 
442385486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
442485486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
442585486af0SThomas Graf {
442685486af0SThomas Graf 	struct ifa_cacheinfo ci;
442785486af0SThomas Graf 
442818a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
442918a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
443085486af0SThomas Graf 	ci.ifa_prefered = preferred;
443185486af0SThomas Graf 	ci.ifa_valid = valid;
443285486af0SThomas Graf 
443385486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
443485486af0SThomas Graf }
443585486af0SThomas Graf 
4436101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
4437101bb229SThomas Graf {
4438101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
4439101bb229SThomas Graf 		return RT_SCOPE_HOST;
4440101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
4441101bb229SThomas Graf 		return RT_SCOPE_LINK;
4442101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
4443101bb229SThomas Graf 		return RT_SCOPE_SITE;
4444101bb229SThomas Graf 	else
4445101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
4446101bb229SThomas Graf }
4447101bb229SThomas Graf 
44480ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
44490ab6803bSThomas Graf {
4450339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4451caeaba79SNicolas Dichtel 	       + nla_total_size(16) /* IFA_LOCAL */
4452339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
4453479840ffSJiri Pirko 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
4454479840ffSJiri Pirko 	       + nla_total_size(4)  /* IFA_FLAGS */;
44550ab6803bSThomas Graf }
4456c5396a31SYOSHIFUJI Hideaki 
44571da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
445815e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
44591da177e4SLinus Torvalds {
44601da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
446185486af0SThomas Graf 	u32 preferred, valid;
44621da177e4SLinus Torvalds 
446315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
446463159f29SIan Morris 	if (!nlh)
446526932566SPatrick McHardy 		return -EMSGSIZE;
44660ab6803bSThomas Graf 
4467101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4468101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
4469101bb229SThomas Graf 
4470fad8da3eSYasushi Asano 	if (!((ifa->flags&IFA_F_PERMANENT) &&
4471fad8da3eSYasushi Asano 	      (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
447285486af0SThomas Graf 		preferred = ifa->prefered_lft;
447385486af0SThomas Graf 		valid = ifa->valid_lft;
447485486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
44751da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
4476a1faa698SJens Rosenboom 			if (preferred > tval)
447785486af0SThomas Graf 				preferred -= tval;
4478a1faa698SJens Rosenboom 			else
4479a1faa698SJens Rosenboom 				preferred = 0;
4480f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
4481f56619fcSBen Hutchings 				if (valid > tval)
448285486af0SThomas Graf 					valid -= tval;
4483f56619fcSBen Hutchings 				else
4484f56619fcSBen Hutchings 					valid = 0;
4485f56619fcSBen Hutchings 			}
44861da177e4SLinus Torvalds 		}
44871da177e4SLinus Torvalds 	} else {
448885486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
448985486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
44901da177e4SLinus Torvalds 	}
449185486af0SThomas Graf 
44927996c799SCong Wang 	if (!ipv6_addr_any(&ifa->peer_addr)) {
4493930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4494930345eaSJiri Benc 		    nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4495caeaba79SNicolas Dichtel 			goto error;
4496caeaba79SNicolas Dichtel 	} else
4497930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4498caeaba79SNicolas Dichtel 			goto error;
4499caeaba79SNicolas Dichtel 
4500caeaba79SNicolas Dichtel 	if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4501caeaba79SNicolas Dichtel 		goto error;
450285486af0SThomas Graf 
4503479840ffSJiri Pirko 	if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4504479840ffSJiri Pirko 		goto error;
4505479840ffSJiri Pirko 
4506053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4507053c095aSJohannes Berg 	return 0;
4508caeaba79SNicolas Dichtel 
4509caeaba79SNicolas Dichtel error:
4510caeaba79SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
4511caeaba79SNicolas Dichtel 	return -EMSGSIZE;
45121da177e4SLinus Torvalds }
45131da177e4SLinus Torvalds 
45141da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
451515e47304SEric W. Biederman 				u32 portid, u32 seq, int event, u16 flags)
45161da177e4SLinus Torvalds {
45171da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4518101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4519101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
45201da177e4SLinus Torvalds 
45211da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4522101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4523101bb229SThomas Graf 
452415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
452563159f29SIan Morris 	if (!nlh)
452626932566SPatrick McHardy 		return -EMSGSIZE;
45270ab6803bSThomas Graf 
4528101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4529930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
45300ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
453126932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
453226932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
453326932566SPatrick McHardy 		return -EMSGSIZE;
453426932566SPatrick McHardy 	}
453585486af0SThomas Graf 
4536053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4537053c095aSJohannes Berg 	return 0;
45381da177e4SLinus Torvalds }
45391da177e4SLinus Torvalds 
45401da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
454115e47304SEric W. Biederman 				u32 portid, u32 seq, int event, unsigned int flags)
45421da177e4SLinus Torvalds {
45431da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4544101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4545101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
45461da177e4SLinus Torvalds 
45471da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4548101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4549101bb229SThomas Graf 
455015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
455163159f29SIan Morris 	if (!nlh)
455226932566SPatrick McHardy 		return -EMSGSIZE;
45530ab6803bSThomas Graf 
4554101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4555930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
45560ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
455726932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
455826932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
455926932566SPatrick McHardy 		return -EMSGSIZE;
456026932566SPatrick McHardy 	}
456185486af0SThomas Graf 
4562053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4563053c095aSJohannes Berg 	return 0;
45641da177e4SLinus Torvalds }
45651da177e4SLinus Torvalds 
4566e21e8467SStephen Hemminger enum addr_type_t {
45671da177e4SLinus Torvalds 	UNICAST_ADDR,
45681da177e4SLinus Torvalds 	MULTICAST_ADDR,
45691da177e4SLinus Torvalds 	ANYCAST_ADDR,
45701da177e4SLinus Torvalds };
45711da177e4SLinus Torvalds 
4572234b27c3SEric Dumazet /* called with rcu_read_lock() */
4573234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4574234b27c3SEric Dumazet 			  struct netlink_callback *cb, enum addr_type_t type,
4575234b27c3SEric Dumazet 			  int s_ip_idx, int *p_ip_idx)
45761da177e4SLinus Torvalds {
45771da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
45781da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
4579234b27c3SEric Dumazet 	int err = 1;
4580234b27c3SEric Dumazet 	int ip_idx = *p_ip_idx;
45811da177e4SLinus Torvalds 
45821da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
45831da177e4SLinus Torvalds 	switch (type) {
4584502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
4585502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
4586502a2ffdSstephen hemminger 
4587ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
4588502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
4589502a2ffdSstephen hemminger 			if (++ip_idx < s_ip_idx)
45901da177e4SLinus Torvalds 				continue;
45915d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifaddr(skb, ifa,
459215e47304SEric W. Biederman 						NETLINK_CB(cb->skb).portid,
45935d5619b4SYOSHIFUJI Hideaki 						cb->nlh->nlmsg_seq,
45945d5619b4SYOSHIFUJI Hideaki 						RTM_NEWADDR,
45955d5619b4SYOSHIFUJI Hideaki 						NLM_F_MULTI);
4596053c095aSJohannes Berg 			if (err < 0)
4597bcd32326SEric Dumazet 				break;
459863998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
45991da177e4SLinus Torvalds 		}
46001da177e4SLinus Torvalds 		break;
4601502a2ffdSstephen hemminger 	}
46021da177e4SLinus Torvalds 	case MULTICAST_ADDR:
46031da177e4SLinus Torvalds 		/* multicast address */
46041da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
46051da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
46061da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
46071da177e4SLinus Torvalds 				continue;
46085d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifmcaddr(skb, ifmca,
460915e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
46105d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
46115d5619b4SYOSHIFUJI Hideaki 						  RTM_GETMULTICAST,
46125d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4613053c095aSJohannes Berg 			if (err < 0)
4614bcd32326SEric Dumazet 				break;
46151da177e4SLinus Torvalds 		}
46161da177e4SLinus Torvalds 		break;
46171da177e4SLinus Torvalds 	case ANYCAST_ADDR:
46181da177e4SLinus Torvalds 		/* anycast address */
46191da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
46201da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
46211da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
46221da177e4SLinus Torvalds 				continue;
46235d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifacaddr(skb, ifaca,
462415e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
46255d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
46265d5619b4SYOSHIFUJI Hideaki 						  RTM_GETANYCAST,
46275d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4628053c095aSJohannes Berg 			if (err < 0)
4629bcd32326SEric Dumazet 				break;
46301da177e4SLinus Torvalds 		}
46311da177e4SLinus Torvalds 		break;
46321da177e4SLinus Torvalds 	default:
46331da177e4SLinus Torvalds 		break;
46341da177e4SLinus Torvalds 	}
46351da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
4636234b27c3SEric Dumazet 	*p_ip_idx = ip_idx;
4637234b27c3SEric Dumazet 	return err;
4638234b27c3SEric Dumazet }
46395d5619b4SYOSHIFUJI Hideaki 
4640234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4641234b27c3SEric Dumazet 			   enum addr_type_t type)
4642234b27c3SEric Dumazet {
4643234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
4644234b27c3SEric Dumazet 	int h, s_h;
4645234b27c3SEric Dumazet 	int idx, ip_idx;
4646234b27c3SEric Dumazet 	int s_idx, s_ip_idx;
4647234b27c3SEric Dumazet 	struct net_device *dev;
4648234b27c3SEric Dumazet 	struct inet6_dev *idev;
4649234b27c3SEric Dumazet 	struct hlist_head *head;
4650234b27c3SEric Dumazet 
4651234b27c3SEric Dumazet 	s_h = cb->args[0];
4652234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
4653234b27c3SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
4654234b27c3SEric Dumazet 
4655234b27c3SEric Dumazet 	rcu_read_lock();
465663998ac2SNicolas Dichtel 	cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4657234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4658234b27c3SEric Dumazet 		idx = 0;
4659234b27c3SEric Dumazet 		head = &net->dev_index_head[h];
4660b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
4661234b27c3SEric Dumazet 			if (idx < s_idx)
4662234b27c3SEric Dumazet 				goto cont;
46634b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
4664234b27c3SEric Dumazet 				s_ip_idx = 0;
4665234b27c3SEric Dumazet 			ip_idx = 0;
4666e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
4667e21e8467SStephen Hemminger 			if (!idev)
4668234b27c3SEric Dumazet 				goto cont;
4669234b27c3SEric Dumazet 
4670234b27c3SEric Dumazet 			if (in6_dump_addrs(idev, skb, cb, type,
46717b46a644SDavid S. Miller 					   s_ip_idx, &ip_idx) < 0)
4672234b27c3SEric Dumazet 				goto done;
46737562f876SPavel Emelianov cont:
46747562f876SPavel Emelianov 			idx++;
46751da177e4SLinus Torvalds 		}
4676234b27c3SEric Dumazet 	}
4677234b27c3SEric Dumazet done:
4678234b27c3SEric Dumazet 	rcu_read_unlock();
4679234b27c3SEric Dumazet 	cb->args[0] = h;
4680234b27c3SEric Dumazet 	cb->args[1] = idx;
4681234b27c3SEric Dumazet 	cb->args[2] = ip_idx;
4682234b27c3SEric Dumazet 
46831da177e4SLinus Torvalds 	return skb->len;
46841da177e4SLinus Torvalds }
46851da177e4SLinus Torvalds 
46861da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
46871da177e4SLinus Torvalds {
46881da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
4689b854272bSDenis V. Lunev 
46901da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
46911da177e4SLinus Torvalds }
46921da177e4SLinus Torvalds 
46931da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
46941da177e4SLinus Torvalds {
46951da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
4696b854272bSDenis V. Lunev 
46971da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
46981da177e4SLinus Torvalds }
46991da177e4SLinus Torvalds 
47001da177e4SLinus Torvalds 
47011da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
47021da177e4SLinus Torvalds {
47031da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
4704b854272bSDenis V. Lunev 
47051da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
47061da177e4SLinus Torvalds }
47071da177e4SLinus Torvalds 
4708661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
47096c223828SNoriaki TAKAMIYA {
47103b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
47111b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
47121b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4713caeaba79SNicolas Dichtel 	struct in6_addr *addr = NULL, *peer;
47146c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
47156c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
47166c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
47176c223828SNoriaki TAKAMIYA 	int err;
47186c223828SNoriaki TAKAMIYA 
47191b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
47201b29fc2cSThomas Graf 	if (err < 0)
47211b29fc2cSThomas Graf 		goto errout;
47226c223828SNoriaki TAKAMIYA 
4723caeaba79SNicolas Dichtel 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
472463159f29SIan Morris 	if (!addr) {
47251b29fc2cSThomas Graf 		err = -EINVAL;
47261b29fc2cSThomas Graf 		goto errout;
47271b29fc2cSThomas Graf 	}
47281b29fc2cSThomas Graf 
47291b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
47306c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
47316fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
47326c223828SNoriaki TAKAMIYA 
4733e21e8467SStephen Hemminger 	ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4734e21e8467SStephen Hemminger 	if (!ifa) {
47351b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
47361b29fc2cSThomas Graf 		goto errout;
47376c223828SNoriaki TAKAMIYA 	}
47386c223828SNoriaki TAKAMIYA 
4739e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4740e21e8467SStephen Hemminger 	if (!skb) {
47411b29fc2cSThomas Graf 		err = -ENOBUFS;
47421b29fc2cSThomas Graf 		goto errout_ifa;
47431b29fc2cSThomas Graf 	}
47441b29fc2cSThomas Graf 
474515e47304SEric W. Biederman 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
47466c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
474726932566SPatrick McHardy 	if (err < 0) {
474826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
474926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
475026932566SPatrick McHardy 		kfree_skb(skb);
475126932566SPatrick McHardy 		goto errout_ifa;
475226932566SPatrick McHardy 	}
475315e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
47541b29fc2cSThomas Graf errout_ifa:
47556c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
47561b29fc2cSThomas Graf errout:
47576c223828SNoriaki TAKAMIYA 	return err;
47586c223828SNoriaki TAKAMIYA }
47596c223828SNoriaki TAKAMIYA 
47601da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
47611da177e4SLinus Torvalds {
47621da177e4SLinus Torvalds 	struct sk_buff *skb;
4763c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
47645d620266SThomas Graf 	int err = -ENOBUFS;
47651da177e4SLinus Torvalds 
47660ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
476763159f29SIan Morris 	if (!skb)
47685d620266SThomas Graf 		goto errout;
47695d620266SThomas Graf 
47705d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
477126932566SPatrick McHardy 	if (err < 0) {
477226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
477326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
477426932566SPatrick McHardy 		kfree_skb(skb);
477526932566SPatrick McHardy 		goto errout;
477626932566SPatrick McHardy 	}
47771ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
47781ce85fe4SPablo Neira Ayuso 	return;
47795d620266SThomas Graf errout:
47805d620266SThomas Graf 	if (err < 0)
47816fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
47821da177e4SLinus Torvalds }
47831da177e4SLinus Torvalds 
4784b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
47851da177e4SLinus Torvalds 				__s32 *array, int bytes)
47861da177e4SLinus Torvalds {
478704561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
478804561c1fSThomas Graf 
47891da177e4SLinus Torvalds 	memset(array, 0, bytes);
47901da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
47911da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
47921da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
47931da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
47941da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
47951da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
47961da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
47971da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
479893908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
479993908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
480093908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
480193908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
48021da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4803fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4804fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4805fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4806fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
48071da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
48081da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
48091da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
48101da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
48111da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
48121da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
481365f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
48148013d1d7SHangbin Liu 	array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
4815c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4816930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4817930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
481893908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
481993908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
4820fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
482109c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
482209c884d4SYOSHIFUJI Hideaki #endif
4823930d6ff2SYOSHIFUJI Hideaki #endif
4824fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
48250bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
482695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
482795c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
48287fd2561eSErik Kline 	array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
482995c385b4SNeil Horman #endif
48307bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
48317bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
48327bc570c8SYOSHIFUJI Hideaki #endif
4833778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
48341b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4835c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
48365cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4837b800c3b9SHannes Frederic Sowa 	array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4838d9333196SBen Greear 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4839c2943f14SHarout Hedeshian 	array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
484035103d11SAndy Gospodarek 	array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
48413d1bec99SHannes Frederic Sowa 	/* we omit DEVCONF_STABLE_SECRET for now */
48423985e8a3SErik Kline 	array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
4843abbc3043SJohannes Berg 	array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
48447a02bf89SJohannes Berg 	array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
4845f1705ec1SDavid Ahern 	array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
48461da177e4SLinus Torvalds }
48471da177e4SLinus Torvalds 
4848b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
4849b382b191SThomas Graf {
4850b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
4851b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
4852b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4853b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4854f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4855f53adae4SDaniel Borkmann 	     + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4856b382b191SThomas Graf }
4857b382b191SThomas Graf 
4858339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
4859339bf98fSThomas Graf {
4860339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4861339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4862339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4863339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
4864339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
48650344338bSAndy Gospodarek 	       + nla_total_size(1) /* IFLA_OPERSTATE */
4866b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4867339bf98fSThomas Graf }
4868c5396a31SYOSHIFUJI Hideaki 
4869be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
48707d720c3eSTejun Heo 				      int items, int bytes)
48717f7d9a6bSHerbert Xu {
48727f7d9a6bSHerbert Xu 	int i;
48737f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
48747f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
48757f7d9a6bSHerbert Xu 
48767f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
48777f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
48787f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
4879be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
48807f7d9a6bSHerbert Xu 
48817f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
48827f7d9a6bSHerbert Xu }
48837f7d9a6bSHerbert Xu 
4884698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
4885a3a77372SRaghavendra K T 					int bytes, size_t syncpoff)
48864ce3c183SEric Dumazet {
4887a3a77372SRaghavendra K T 	int i, c;
4888a3a77372SRaghavendra K T 	u64 buff[IPSTATS_MIB_MAX];
4889a3a77372SRaghavendra K T 	int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
4890a3a77372SRaghavendra K T 
48914ce3c183SEric Dumazet 	BUG_ON(pad < 0);
48924ce3c183SEric Dumazet 
4893a3a77372SRaghavendra K T 	memset(buff, 0, sizeof(buff));
4894a3a77372SRaghavendra K T 	buff[0] = IPSTATS_MIB_MAX;
48954ce3c183SEric Dumazet 
4896a3a77372SRaghavendra K T 	for_each_possible_cpu(c) {
4897a3a77372SRaghavendra K T 		for (i = 1; i < IPSTATS_MIB_MAX; i++)
4898a3a77372SRaghavendra K T 			buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
4899a3a77372SRaghavendra K T 	}
4900a3a77372SRaghavendra K T 
4901a3a77372SRaghavendra K T 	memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
4902a3a77372SRaghavendra K T 	memset(&stats[IPSTATS_MIB_MAX], 0, pad);
49034ce3c183SEric Dumazet }
49044ce3c183SEric Dumazet 
49057f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
49067f7d9a6bSHerbert Xu 			     int bytes)
49077f7d9a6bSHerbert Xu {
49087f7d9a6bSHerbert Xu 	switch (attrtype) {
49097f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
4910a3a77372SRaghavendra K T 		__snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
4911a3a77372SRaghavendra K T 				     offsetof(struct ipstats_mib, syncp));
49127f7d9a6bSHerbert Xu 		break;
49137f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
4914be281e55SEric Dumazet 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
49157f7d9a6bSHerbert Xu 		break;
49167f7d9a6bSHerbert Xu 	}
49177f7d9a6bSHerbert Xu }
49187f7d9a6bSHerbert Xu 
4919d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
4920d5566fd7SSowmini Varadhan 				  u32 ext_filter_mask)
49211da177e4SLinus Torvalds {
4922bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
49231da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
49241da177e4SLinus Torvalds 
4925c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4926c78679e8SDavid S. Miller 		goto nla_put_failure;
49271da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
492824912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
492924912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
49301f9248e5SJiri Pirko 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
4931c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4932c78679e8SDavid S. Miller 		goto nla_put_failure;
4933bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
493463159f29SIan Morris 	if (!nla)
493504561c1fSThomas Graf 		goto nla_put_failure;
4936bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
49371da177e4SLinus Torvalds 
4938bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
4939bf99f1bdSYOSHIFUJI Hideaki 
4940d5566fd7SSowmini Varadhan 	if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
4941d5566fd7SSowmini Varadhan 		return 0;
4942d5566fd7SSowmini Varadhan 
4943bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
494463159f29SIan Morris 	if (!nla)
4945bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4946bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4947bf99f1bdSYOSHIFUJI Hideaki 
4948bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
494963159f29SIan Morris 	if (!nla)
4950bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4951bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
49521da177e4SLinus Torvalds 
4953f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
495463159f29SIan Morris 	if (!nla)
4955f53adae4SDaniel Borkmann 		goto nla_put_failure;
4956bc91b0f0SJiri Pirko 
4957bc91b0f0SJiri Pirko 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
4958bc91b0f0SJiri Pirko 		goto nla_put_failure;
4959bc91b0f0SJiri Pirko 
4960f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
4961f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4962f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
4963f53adae4SDaniel Borkmann 
4964b382b191SThomas Graf 	return 0;
4965b382b191SThomas Graf 
4966b382b191SThomas Graf nla_put_failure:
4967b382b191SThomas Graf 	return -EMSGSIZE;
4968b382b191SThomas Graf }
4969b382b191SThomas Graf 
4970b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev,
4971b1974ed0SArad, Ronen 				     u32 ext_filter_mask)
4972b382b191SThomas Graf {
4973b382b191SThomas Graf 	if (!__in6_dev_get(dev))
4974b382b191SThomas Graf 		return 0;
4975b382b191SThomas Graf 
4976b382b191SThomas Graf 	return inet6_ifla6_size();
4977b382b191SThomas Graf }
4978b382b191SThomas Graf 
4979d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
4980d5566fd7SSowmini Varadhan 			      u32 ext_filter_mask)
4981b382b191SThomas Graf {
4982b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
4983b382b191SThomas Graf 
4984b382b191SThomas Graf 	if (!idev)
4985b382b191SThomas Graf 		return -ENODATA;
4986b382b191SThomas Graf 
4987d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
4988b382b191SThomas Graf 		return -EMSGSIZE;
4989b382b191SThomas Graf 
4990b382b191SThomas Graf 	return 0;
4991b382b191SThomas Graf }
4992b382b191SThomas Graf 
4993f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4994f53adae4SDaniel Borkmann {
4995f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
4996f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
499747e27d5eSDaniel Borkmann 	bool clear_token, update_rs = false;
4998dc848292SHannes Frederic Sowa 	struct in6_addr ll_addr;
4999f53adae4SDaniel Borkmann 
5000c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
5001c15b1ccaSHannes Frederic Sowa 
500263159f29SIan Morris 	if (!token)
5003f53adae4SDaniel Borkmann 		return -EINVAL;
5004f53adae4SDaniel Borkmann 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5005f53adae4SDaniel Borkmann 		return -EINVAL;
5006f53adae4SDaniel Borkmann 	if (!ipv6_accept_ra(idev))
5007f53adae4SDaniel Borkmann 		return -EINVAL;
5008f53adae4SDaniel Borkmann 	if (idev->cnf.rtr_solicits <= 0)
5009f53adae4SDaniel Borkmann 		return -EINVAL;
5010f53adae4SDaniel Borkmann 
5011f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5012f53adae4SDaniel Borkmann 
5013f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5014f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5015f53adae4SDaniel Borkmann 
5016f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5017f53adae4SDaniel Borkmann 
501847e27d5eSDaniel Borkmann 	clear_token = ipv6_addr_any(token);
501947e27d5eSDaniel Borkmann 	if (clear_token)
502047e27d5eSDaniel Borkmann 		goto update_lft;
502147e27d5eSDaniel Borkmann 
5022dc848292SHannes Frederic Sowa 	if (!idev->dead && (idev->if_flags & IF_READY) &&
5023dc848292SHannes Frederic Sowa 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5024dc848292SHannes Frederic Sowa 			     IFA_F_OPTIMISTIC)) {
5025fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
5026fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
5027fc403832SDaniel Borkmann 		 */
5028f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5029fc403832SDaniel Borkmann 		update_rs = true;
5030fc403832SDaniel Borkmann 	}
5031f53adae4SDaniel Borkmann 
503247e27d5eSDaniel Borkmann update_lft:
5033f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
5034fc403832SDaniel Borkmann 
503577ecaaceSHannes Frederic Sowa 	if (update_rs) {
5036f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
503777ecaaceSHannes Frederic Sowa 		idev->rs_probes = 1;
503877ecaaceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
503977ecaaceSHannes Frederic Sowa 	}
5040f53adae4SDaniel Borkmann 
5041f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
5042f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
5043f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
5044617fe29dSDaniel Borkmann 		if (ifp->tokenized) {
5045f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
5046f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
5047f53adae4SDaniel Borkmann 		}
5048f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
5049f53adae4SDaniel Borkmann 	}
5050f53adae4SDaniel Borkmann 
5051f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
5052b2ed64a9SLubomir Rintel 	inet6_ifinfo_notify(RTM_NEWLINK, idev);
5053c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
5054f53adae4SDaniel Borkmann 	return 0;
5055f53adae4SDaniel Borkmann }
5056f53adae4SDaniel Borkmann 
505711b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
505811b1f828SDaniel Borkmann 	[IFLA_INET6_ADDR_GEN_MODE]	= { .type = NLA_U8 },
505911b1f828SDaniel Borkmann 	[IFLA_INET6_TOKEN]		= { .len = sizeof(struct in6_addr) },
506011b1f828SDaniel Borkmann };
506111b1f828SDaniel Borkmann 
506211b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev,
506311b1f828SDaniel Borkmann 				  const struct nlattr *nla)
506411b1f828SDaniel Borkmann {
506511b1f828SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
506611b1f828SDaniel Borkmann 
506711b1f828SDaniel Borkmann 	if (dev && !__in6_dev_get(dev))
506811b1f828SDaniel Borkmann 		return -EAFNOSUPPORT;
506911b1f828SDaniel Borkmann 
507011b1f828SDaniel Borkmann 	return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
507111b1f828SDaniel Borkmann }
507211b1f828SDaniel Borkmann 
5073f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5074f53adae4SDaniel Borkmann {
5075f53adae4SDaniel Borkmann 	int err = -EINVAL;
5076f53adae4SDaniel Borkmann 	struct inet6_dev *idev = __in6_dev_get(dev);
5077f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
5078f53adae4SDaniel Borkmann 
5079f53adae4SDaniel Borkmann 	if (!idev)
5080f53adae4SDaniel Borkmann 		return -EAFNOSUPPORT;
5081f53adae4SDaniel Borkmann 
5082f53adae4SDaniel Borkmann 	if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5083f53adae4SDaniel Borkmann 		BUG();
5084f53adae4SDaniel Borkmann 
5085bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_TOKEN]) {
5086f53adae4SDaniel Borkmann 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5087bc91b0f0SJiri Pirko 		if (err)
5088bc91b0f0SJiri Pirko 			return err;
5089bc91b0f0SJiri Pirko 	}
5090bc91b0f0SJiri Pirko 
5091bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5092bc91b0f0SJiri Pirko 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5093bc91b0f0SJiri Pirko 
5094bc91b0f0SJiri Pirko 		if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5095622c81d5SHannes Frederic Sowa 		    mode != IN6_ADDR_GEN_MODE_NONE &&
5096cc9da6ccSBjørn Mork 		    mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5097cc9da6ccSBjørn Mork 		    mode != IN6_ADDR_GEN_MODE_RANDOM)
5098bc91b0f0SJiri Pirko 			return -EINVAL;
5099622c81d5SHannes Frederic Sowa 
5100622c81d5SHannes Frederic Sowa 		if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5101622c81d5SHannes Frederic Sowa 		    !idev->cnf.stable_secret.initialized &&
5102622c81d5SHannes Frederic Sowa 		    !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
5103622c81d5SHannes Frederic Sowa 			return -EINVAL;
5104622c81d5SHannes Frederic Sowa 
5105bc91b0f0SJiri Pirko 		idev->addr_gen_mode = mode;
5106bc91b0f0SJiri Pirko 		err = 0;
5107bc91b0f0SJiri Pirko 	}
5108f53adae4SDaniel Borkmann 
5109f53adae4SDaniel Borkmann 	return err;
5110f53adae4SDaniel Borkmann }
5111f53adae4SDaniel Borkmann 
5112b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
511315e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
5114b382b191SThomas Graf {
5115b382b191SThomas Graf 	struct net_device *dev = idev->dev;
5116b382b191SThomas Graf 	struct ifinfomsg *hdr;
5117b382b191SThomas Graf 	struct nlmsghdr *nlh;
5118b382b191SThomas Graf 	void *protoinfo;
5119b382b191SThomas Graf 
512015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
512163159f29SIan Morris 	if (!nlh)
5122b382b191SThomas Graf 		return -EMSGSIZE;
5123b382b191SThomas Graf 
5124b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
5125b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
5126b382b191SThomas Graf 	hdr->__ifi_pad = 0;
5127b382b191SThomas Graf 	hdr->ifi_type = dev->type;
5128b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
5129b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
5130b382b191SThomas Graf 	hdr->ifi_change = 0;
5131b382b191SThomas Graf 
5132c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5133c78679e8SDavid S. Miller 	    (dev->addr_len &&
5134c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5135c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5136a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
51370344338bSAndy Gospodarek 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
51380344338bSAndy Gospodarek 	    nla_put_u8(skb, IFLA_OPERSTATE,
51390344338bSAndy Gospodarek 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5140c78679e8SDavid S. Miller 		goto nla_put_failure;
5141b382b191SThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
514263159f29SIan Morris 	if (!protoinfo)
5143b382b191SThomas Graf 		goto nla_put_failure;
5144b382b191SThomas Graf 
5145d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5146b382b191SThomas Graf 		goto nla_put_failure;
5147b382b191SThomas Graf 
514804561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
5149053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5150053c095aSJohannes Berg 	return 0;
51511da177e4SLinus Torvalds 
515204561c1fSThomas Graf nla_put_failure:
515326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
515426932566SPatrick McHardy 	return -EMSGSIZE;
51551da177e4SLinus Torvalds }
51561da177e4SLinus Torvalds 
51571da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
51581da177e4SLinus Torvalds {
51593b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
516084d2697dSEric Dumazet 	int h, s_h;
5161434a8a58SDavid S. Miller 	int idx = 0, s_idx;
51621da177e4SLinus Torvalds 	struct net_device *dev;
51631da177e4SLinus Torvalds 	struct inet6_dev *idev;
516484d2697dSEric Dumazet 	struct hlist_head *head;
51651da177e4SLinus Torvalds 
516684d2697dSEric Dumazet 	s_h = cb->args[0];
516784d2697dSEric Dumazet 	s_idx = cb->args[1];
516884d2697dSEric Dumazet 
516984d2697dSEric Dumazet 	rcu_read_lock();
517084d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
51717562f876SPavel Emelianov 		idx = 0;
517284d2697dSEric Dumazet 		head = &net->dev_index_head[h];
5173b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
51741da177e4SLinus Torvalds 			if (idx < s_idx)
51757562f876SPavel Emelianov 				goto cont;
517684d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
517784d2697dSEric Dumazet 			if (!idev)
51787562f876SPavel Emelianov 				goto cont;
517984d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
518015e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
518184d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
5182053c095aSJohannes Berg 					      RTM_NEWLINK, NLM_F_MULTI) < 0)
518384d2697dSEric Dumazet 				goto out;
51847562f876SPavel Emelianov cont:
51857562f876SPavel Emelianov 			idx++;
51861da177e4SLinus Torvalds 		}
518784d2697dSEric Dumazet 	}
518884d2697dSEric Dumazet out:
518984d2697dSEric Dumazet 	rcu_read_unlock();
519084d2697dSEric Dumazet 	cb->args[1] = idx;
519184d2697dSEric Dumazet 	cb->args[0] = h;
51921da177e4SLinus Torvalds 
51931da177e4SLinus Torvalds 	return skb->len;
51941da177e4SLinus Torvalds }
51951da177e4SLinus Torvalds 
51961da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
51971da177e4SLinus Torvalds {
51981da177e4SLinus Torvalds 	struct sk_buff *skb;
5199c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
52008d7a76c9SThomas Graf 	int err = -ENOBUFS;
52011da177e4SLinus Torvalds 
5202339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
520363159f29SIan Morris 	if (!skb)
52048d7a76c9SThomas Graf 		goto errout;
52058d7a76c9SThomas Graf 
52068d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
520726932566SPatrick McHardy 	if (err < 0) {
520826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
520926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
521026932566SPatrick McHardy 		kfree_skb(skb);
521126932566SPatrick McHardy 		goto errout;
521226932566SPatrick McHardy 	}
52135f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
52141ce85fe4SPablo Neira Ayuso 	return;
52158d7a76c9SThomas Graf errout:
52168d7a76c9SThomas Graf 	if (err < 0)
52175f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
52181da177e4SLinus Torvalds }
52191da177e4SLinus Torvalds 
5220339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
5221339bf98fSThomas Graf {
5222339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
5223339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
5224339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
5225339bf98fSThomas Graf }
5226c5396a31SYOSHIFUJI Hideaki 
52271da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
522815e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
5229b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
52301da177e4SLinus Torvalds {
52311da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
52321da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
52331da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
52341da177e4SLinus Torvalds 
523515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
523663159f29SIan Morris 	if (!nlh)
523726932566SPatrick McHardy 		return -EMSGSIZE;
52386051e2f4SThomas Graf 
52396051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
52401da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
52418a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
52428a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
52431da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
52441da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
52451da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
52468a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
52471da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
52481da177e4SLinus Torvalds 	if (pinfo->onlink)
52491da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
52501da177e4SLinus Torvalds 	if (pinfo->autoconf)
52511da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
52521da177e4SLinus Torvalds 
5253c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5254c78679e8SDavid S. Miller 		goto nla_put_failure;
52551da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
52561da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
5257c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5258c78679e8SDavid S. Miller 		goto nla_put_failure;
5259053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5260053c095aSJohannes Berg 	return 0;
52611da177e4SLinus Torvalds 
52626051e2f4SThomas Graf nla_put_failure:
526326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
526426932566SPatrick McHardy 	return -EMSGSIZE;
52651da177e4SLinus Torvalds }
52661da177e4SLinus Torvalds 
52671da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
52681da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
52691da177e4SLinus Torvalds {
52701da177e4SLinus Torvalds 	struct sk_buff *skb;
5271c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
52728c384bfaSThomas Graf 	int err = -ENOBUFS;
52731da177e4SLinus Torvalds 
5274339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
527563159f29SIan Morris 	if (!skb)
52768c384bfaSThomas Graf 		goto errout;
52778c384bfaSThomas Graf 
52788c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
527926932566SPatrick McHardy 	if (err < 0) {
528026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
528126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
528226932566SPatrick McHardy 		kfree_skb(skb);
528326932566SPatrick McHardy 		goto errout;
528426932566SPatrick McHardy 	}
52851ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
52861ce85fe4SPablo Neira Ayuso 	return;
52878c384bfaSThomas Graf errout:
52888c384bfaSThomas Graf 	if (err < 0)
52896fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
52901da177e4SLinus Torvalds }
52911da177e4SLinus Torvalds 
52921da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
52931da177e4SLinus Torvalds {
529463998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
529563998ac2SNicolas Dichtel 
5296c15b1ccaSHannes Frederic Sowa 	if (event)
5297c15b1ccaSHannes Frederic Sowa 		ASSERT_RTNL();
5298c15b1ccaSHannes Frederic Sowa 
52991da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
53001da177e4SLinus Torvalds 
53011da177e4SLinus Torvalds 	switch (event) {
53021da177e4SLinus Torvalds 	case RTM_NEWADDR:
530395c385b4SNeil Horman 		/*
530495c385b4SNeil Horman 		 * If the address was optimistic
530595c385b4SNeil Horman 		 * we inserted the route at the start of
530695c385b4SNeil Horman 		 * our DAD process, so we don't need
530795c385b4SNeil Horman 		 * to do it again
530895c385b4SNeil Horman 		 */
530995c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
531040e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
53111da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
53121da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
53137996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr))
5314caeaba79SNicolas Dichtel 			addrconf_prefix_route(&ifp->peer_addr, 128,
5315caeaba79SNicolas Dichtel 					      ifp->idev->dev, 0, 0);
53161da177e4SLinus Torvalds 		break;
53171da177e4SLinus Torvalds 	case RTM_DELADDR:
53181da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
53191da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
53201da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
53217996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr)) {
5322caeaba79SNicolas Dichtel 			struct rt6_info *rt;
5323caeaba79SNicolas Dichtel 
5324e7478dfcSNicolas Dichtel 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5325e7478dfcSNicolas Dichtel 						       ifp->idev->dev, 0, 0);
53268e3d5be7SMartin KaFai Lau 			if (rt)
53278e3d5be7SMartin KaFai Lau 				ip6_del_rt(rt);
5328caeaba79SNicolas Dichtel 		}
532938bd10c4SDavid Ahern 		if (ifp->rt) {
5330d8d1f30bSChangli Gao 			dst_hold(&ifp->rt->dst);
53318e3d5be7SMartin KaFai Lau 			ip6_del_rt(ifp->rt);
533238bd10c4SDavid Ahern 		}
5333705f1c86SHannes Frederic Sowa 		rt_genid_bump_ipv6(net);
53341da177e4SLinus Torvalds 		break;
53351da177e4SLinus Torvalds 	}
533663998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
53371da177e4SLinus Torvalds }
53381da177e4SLinus Torvalds 
53391da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
53401da177e4SLinus Torvalds {
53418814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
53421da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
53431da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
53448814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
53451da177e4SLinus Torvalds }
53461da177e4SLinus Torvalds 
53471da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
53481da177e4SLinus Torvalds 
53491da177e4SLinus Torvalds static
5350fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
53511da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
53521da177e4SLinus Torvalds {
53531da177e4SLinus Torvalds 	int *valp = ctl->data;
53541da177e4SLinus Torvalds 	int val = *valp;
535588af182eSEric W. Biederman 	loff_t pos = *ppos;
5356fe2c6338SJoe Perches 	struct ctl_table lctl;
53571da177e4SLinus Torvalds 	int ret;
53581da177e4SLinus Torvalds 
5359013d97e9SFrancesco Ruggeri 	/*
5360013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
5361013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5362013d97e9SFrancesco Ruggeri 	 */
5363013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5364013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5365013d97e9SFrancesco Ruggeri 
5366013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
53671da177e4SLinus Torvalds 
5368c8fecf22SPavel Emelyanov 	if (write)
5369b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
537088af182eSEric W. Biederman 	if (ret)
537188af182eSEric W. Biederman 		*ppos = pos;
53721da177e4SLinus Torvalds 	return ret;
53731da177e4SLinus Torvalds }
53741da177e4SLinus Torvalds 
537577751427SMarcelo Leitner static
5376d6df198dSPhil Sutter int addrconf_sysctl_hop_limit(struct ctl_table *ctl, int write,
5377d6df198dSPhil Sutter                               void __user *buffer, size_t *lenp, loff_t *ppos)
5378d6df198dSPhil Sutter {
5379d6df198dSPhil Sutter 	struct ctl_table lctl;
5380d6df198dSPhil Sutter 	int min_hl = 1, max_hl = 255;
5381d6df198dSPhil Sutter 
5382d6df198dSPhil Sutter 	lctl = *ctl;
5383d6df198dSPhil Sutter 	lctl.extra1 = &min_hl;
5384d6df198dSPhil Sutter 	lctl.extra2 = &max_hl;
5385d6df198dSPhil Sutter 
5386d6df198dSPhil Sutter 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5387d6df198dSPhil Sutter }
5388d6df198dSPhil Sutter 
5389d6df198dSPhil Sutter static
539077751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
539177751427SMarcelo Leitner 			void __user *buffer, size_t *lenp, loff_t *ppos)
539277751427SMarcelo Leitner {
539377751427SMarcelo Leitner 	struct inet6_dev *idev = ctl->extra1;
539477751427SMarcelo Leitner 	int min_mtu = IPV6_MIN_MTU;
539577751427SMarcelo Leitner 	struct ctl_table lctl;
539677751427SMarcelo Leitner 
539777751427SMarcelo Leitner 	lctl = *ctl;
539877751427SMarcelo Leitner 	lctl.extra1 = &min_mtu;
539977751427SMarcelo Leitner 	lctl.extra2 = idev ? &idev->dev->mtu : NULL;
540077751427SMarcelo Leitner 
540177751427SMarcelo Leitner 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
540277751427SMarcelo Leitner }
540377751427SMarcelo Leitner 
540456d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
540556d417b1SBrian Haley {
540675538c2bSCong Wang 	struct netdev_notifier_info info;
540775538c2bSCong Wang 
540856d417b1SBrian Haley 	if (!idev || !idev->dev)
540956d417b1SBrian Haley 		return;
541056d417b1SBrian Haley 
541175538c2bSCong Wang 	netdev_notifier_info_init(&info, idev->dev);
541256d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
541375538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_DOWN, &info);
541456d417b1SBrian Haley 	else
541575538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_UP, &info);
541656d417b1SBrian Haley }
541756d417b1SBrian Haley 
541856d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
541956d417b1SBrian Haley {
542056d417b1SBrian Haley 	struct net_device *dev;
542156d417b1SBrian Haley 	struct inet6_dev *idev;
542256d417b1SBrian Haley 
542356d417b1SBrian Haley 	rcu_read_lock();
5424c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
542556d417b1SBrian Haley 		idev = __in6_dev_get(dev);
542656d417b1SBrian Haley 		if (idev) {
542756d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
542856d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
542956d417b1SBrian Haley 			if (changed)
543056d417b1SBrian Haley 				dev_disable_change(idev);
543156d417b1SBrian Haley 		}
543256d417b1SBrian Haley 	}
5433c6d14c84SEric Dumazet 	rcu_read_unlock();
543456d417b1SBrian Haley }
543556d417b1SBrian Haley 
5436013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
543756d417b1SBrian Haley {
543856d417b1SBrian Haley 	struct net *net;
5439013d97e9SFrancesco Ruggeri 	int old;
5440013d97e9SFrancesco Ruggeri 
5441013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
5442013d97e9SFrancesco Ruggeri 		return restart_syscall();
544356d417b1SBrian Haley 
544456d417b1SBrian Haley 	net = (struct net *)table->extra2;
5445013d97e9SFrancesco Ruggeri 	old = *p;
5446013d97e9SFrancesco Ruggeri 	*p = newf;
544756d417b1SBrian Haley 
5448013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5449013d97e9SFrancesco Ruggeri 		rtnl_unlock();
545056d417b1SBrian Haley 		return 0;
545188af182eSEric W. Biederman 	}
545256d417b1SBrian Haley 
545356d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
545456d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
545556d417b1SBrian Haley 		addrconf_disable_change(net, newf);
5456013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
545756d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
545856d417b1SBrian Haley 
545956d417b1SBrian Haley 	rtnl_unlock();
546056d417b1SBrian Haley 	return 0;
546156d417b1SBrian Haley }
546256d417b1SBrian Haley 
546356d417b1SBrian Haley static
5464fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
546556d417b1SBrian Haley 			    void __user *buffer, size_t *lenp, loff_t *ppos)
546656d417b1SBrian Haley {
546756d417b1SBrian Haley 	int *valp = ctl->data;
546856d417b1SBrian Haley 	int val = *valp;
546988af182eSEric W. Biederman 	loff_t pos = *ppos;
5470fe2c6338SJoe Perches 	struct ctl_table lctl;
547156d417b1SBrian Haley 	int ret;
547256d417b1SBrian Haley 
5473013d97e9SFrancesco Ruggeri 	/*
5474013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
5475013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5476013d97e9SFrancesco Ruggeri 	 */
5477013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5478013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5479013d97e9SFrancesco Ruggeri 
5480013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
548156d417b1SBrian Haley 
548256d417b1SBrian Haley 	if (write)
548356d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
548488af182eSEric W. Biederman 	if (ret)
548588af182eSEric W. Biederman 		*ppos = pos;
548656d417b1SBrian Haley 	return ret;
548756d417b1SBrian Haley }
548856d417b1SBrian Haley 
5489c92d5491Sstephen hemminger static
5490c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5491c92d5491Sstephen hemminger 			      void __user *buffer, size_t *lenp, loff_t *ppos)
5492c92d5491Sstephen hemminger {
5493c92d5491Sstephen hemminger 	int *valp = ctl->data;
5494c92d5491Sstephen hemminger 	int ret;
5495c92d5491Sstephen hemminger 	int old, new;
5496c92d5491Sstephen hemminger 
5497c92d5491Sstephen hemminger 	old = *valp;
5498c92d5491Sstephen hemminger 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5499c92d5491Sstephen hemminger 	new = *valp;
5500c92d5491Sstephen hemminger 
5501c92d5491Sstephen hemminger 	if (write && old != new) {
5502c92d5491Sstephen hemminger 		struct net *net = ctl->extra2;
5503c92d5491Sstephen hemminger 
5504c92d5491Sstephen hemminger 		if (!rtnl_trylock())
5505c92d5491Sstephen hemminger 			return restart_syscall();
5506c92d5491Sstephen hemminger 
5507c92d5491Sstephen hemminger 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5508c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5509c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_DEFAULT,
5510c92d5491Sstephen hemminger 						     net->ipv6.devconf_dflt);
5511c92d5491Sstephen hemminger 		else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5512c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5513c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_ALL,
5514c92d5491Sstephen hemminger 						     net->ipv6.devconf_all);
5515c92d5491Sstephen hemminger 		else {
5516c92d5491Sstephen hemminger 			struct inet6_dev *idev = ctl->extra1;
5517c92d5491Sstephen hemminger 
5518c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5519c92d5491Sstephen hemminger 						     idev->dev->ifindex,
5520c92d5491Sstephen hemminger 						     &idev->cnf);
5521c92d5491Sstephen hemminger 		}
5522c92d5491Sstephen hemminger 		rtnl_unlock();
5523c92d5491Sstephen hemminger 	}
5524c92d5491Sstephen hemminger 
5525c92d5491Sstephen hemminger 	return ret;
5526c92d5491Sstephen hemminger }
5527c92d5491Sstephen hemminger 
55283d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
55293d1bec99SHannes Frederic Sowa 					 void __user *buffer, size_t *lenp,
55303d1bec99SHannes Frederic Sowa 					 loff_t *ppos)
55313d1bec99SHannes Frederic Sowa {
55323d1bec99SHannes Frederic Sowa 	int err;
55333d1bec99SHannes Frederic Sowa 	struct in6_addr addr;
55343d1bec99SHannes Frederic Sowa 	char str[IPV6_MAX_STRLEN];
55353d1bec99SHannes Frederic Sowa 	struct ctl_table lctl = *ctl;
5536622c81d5SHannes Frederic Sowa 	struct net *net = ctl->extra2;
55373d1bec99SHannes Frederic Sowa 	struct ipv6_stable_secret *secret = ctl->data;
55383d1bec99SHannes Frederic Sowa 
5539622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5540622c81d5SHannes Frederic Sowa 		return -EIO;
5541622c81d5SHannes Frederic Sowa 
55423d1bec99SHannes Frederic Sowa 	lctl.maxlen = IPV6_MAX_STRLEN;
55433d1bec99SHannes Frederic Sowa 	lctl.data = str;
55443d1bec99SHannes Frederic Sowa 
55453d1bec99SHannes Frederic Sowa 	if (!rtnl_trylock())
55463d1bec99SHannes Frederic Sowa 		return restart_syscall();
55473d1bec99SHannes Frederic Sowa 
55483d1bec99SHannes Frederic Sowa 	if (!write && !secret->initialized) {
55493d1bec99SHannes Frederic Sowa 		err = -EIO;
55503d1bec99SHannes Frederic Sowa 		goto out;
55513d1bec99SHannes Frederic Sowa 	}
55523d1bec99SHannes Frederic Sowa 
55535449a5caSWANG Cong 	err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
55543d1bec99SHannes Frederic Sowa 	if (err >= sizeof(str)) {
55553d1bec99SHannes Frederic Sowa 		err = -EIO;
55563d1bec99SHannes Frederic Sowa 		goto out;
55573d1bec99SHannes Frederic Sowa 	}
55583d1bec99SHannes Frederic Sowa 
55593d1bec99SHannes Frederic Sowa 	err = proc_dostring(&lctl, write, buffer, lenp, ppos);
55603d1bec99SHannes Frederic Sowa 	if (err || !write)
55613d1bec99SHannes Frederic Sowa 		goto out;
55623d1bec99SHannes Frederic Sowa 
55633d1bec99SHannes Frederic Sowa 	if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
55643d1bec99SHannes Frederic Sowa 		err = -EIO;
55653d1bec99SHannes Frederic Sowa 		goto out;
55663d1bec99SHannes Frederic Sowa 	}
55673d1bec99SHannes Frederic Sowa 
55683d1bec99SHannes Frederic Sowa 	secret->initialized = true;
55693d1bec99SHannes Frederic Sowa 	secret->secret = addr;
55703d1bec99SHannes Frederic Sowa 
5571622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5572622c81d5SHannes Frederic Sowa 		struct net_device *dev;
5573622c81d5SHannes Frederic Sowa 
5574622c81d5SHannes Frederic Sowa 		for_each_netdev(net, dev) {
5575622c81d5SHannes Frederic Sowa 			struct inet6_dev *idev = __in6_dev_get(dev);
5576622c81d5SHannes Frederic Sowa 
5577622c81d5SHannes Frederic Sowa 			if (idev) {
5578622c81d5SHannes Frederic Sowa 				idev->addr_gen_mode =
5579622c81d5SHannes Frederic Sowa 					IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5580622c81d5SHannes Frederic Sowa 			}
5581622c81d5SHannes Frederic Sowa 		}
5582622c81d5SHannes Frederic Sowa 	} else {
5583622c81d5SHannes Frederic Sowa 		struct inet6_dev *idev = ctl->extra1;
5584622c81d5SHannes Frederic Sowa 
5585622c81d5SHannes Frederic Sowa 		idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5586622c81d5SHannes Frederic Sowa 	}
5587622c81d5SHannes Frederic Sowa 
55883d1bec99SHannes Frederic Sowa out:
55893d1bec99SHannes Frederic Sowa 	rtnl_unlock();
55903d1bec99SHannes Frederic Sowa 
55913d1bec99SHannes Frederic Sowa 	return err;
55923d1bec99SHannes Frederic Sowa }
5593c92d5491Sstephen hemminger 
559435103d11SAndy Gospodarek static
559535103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
559635103d11SAndy Gospodarek 						int write,
559735103d11SAndy Gospodarek 						void __user *buffer,
559835103d11SAndy Gospodarek 						size_t *lenp,
559935103d11SAndy Gospodarek 						loff_t *ppos)
560035103d11SAndy Gospodarek {
560135103d11SAndy Gospodarek 	int *valp = ctl->data;
560235103d11SAndy Gospodarek 	int val = *valp;
560335103d11SAndy Gospodarek 	loff_t pos = *ppos;
560435103d11SAndy Gospodarek 	struct ctl_table lctl;
560535103d11SAndy Gospodarek 	int ret;
560635103d11SAndy Gospodarek 
560735103d11SAndy Gospodarek 	/* ctl->data points to idev->cnf.ignore_routes_when_linkdown
560835103d11SAndy Gospodarek 	 * we should not modify it until we get the rtnl lock.
560935103d11SAndy Gospodarek 	 */
561035103d11SAndy Gospodarek 	lctl = *ctl;
561135103d11SAndy Gospodarek 	lctl.data = &val;
561235103d11SAndy Gospodarek 
561335103d11SAndy Gospodarek 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
561435103d11SAndy Gospodarek 
561535103d11SAndy Gospodarek 	if (write)
561635103d11SAndy Gospodarek 		ret = addrconf_fixup_linkdown(ctl, valp, val);
561735103d11SAndy Gospodarek 	if (ret)
561835103d11SAndy Gospodarek 		*ppos = pos;
561935103d11SAndy Gospodarek 	return ret;
562035103d11SAndy Gospodarek }
562135103d11SAndy Gospodarek 
5622607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = {
56231da177e4SLinus Torvalds 	{
56241da177e4SLinus Torvalds 		.procname	= "forwarding",
56251da177e4SLinus Torvalds 		.data		= &ipv6_devconf.forwarding,
56261da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
56271da177e4SLinus Torvalds 		.mode		= 0644,
56286d9f239aSAlexey Dobriyan 		.proc_handler	= addrconf_sysctl_forward,
56291da177e4SLinus Torvalds 	},
56301da177e4SLinus Torvalds 	{
56311da177e4SLinus Torvalds 		.procname	= "hop_limit",
56321da177e4SLinus Torvalds 		.data		= &ipv6_devconf.hop_limit,
56331da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
56341da177e4SLinus Torvalds 		.mode		= 0644,
5635d6df198dSPhil Sutter 		.proc_handler	= addrconf_sysctl_hop_limit,
56361da177e4SLinus Torvalds 	},
56371da177e4SLinus Torvalds 	{
56381da177e4SLinus Torvalds 		.procname	= "mtu",
56391da177e4SLinus Torvalds 		.data		= &ipv6_devconf.mtu6,
56401da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
56411da177e4SLinus Torvalds 		.mode		= 0644,
564277751427SMarcelo Leitner 		.proc_handler	= addrconf_sysctl_mtu,
56431da177e4SLinus Torvalds 	},
56441da177e4SLinus Torvalds 	{
56451da177e4SLinus Torvalds 		.procname	= "accept_ra",
56461da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_ra,
56471da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
56481da177e4SLinus Torvalds 		.mode		= 0644,
56496d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
56501da177e4SLinus Torvalds 	},
56511da177e4SLinus Torvalds 	{
56521da177e4SLinus Torvalds 		.procname	= "accept_redirects",
56531da177e4SLinus Torvalds 		.data		= &ipv6_devconf.accept_redirects,
56541da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
56551da177e4SLinus Torvalds 		.mode		= 0644,
56566d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
56571da177e4SLinus Torvalds 	},
56581da177e4SLinus Torvalds 	{
56591da177e4SLinus Torvalds 		.procname	= "autoconf",
56601da177e4SLinus Torvalds 		.data		= &ipv6_devconf.autoconf,
56611da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
56621da177e4SLinus Torvalds 		.mode		= 0644,
56636d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
56641da177e4SLinus Torvalds 	},
56651da177e4SLinus Torvalds 	{
56661da177e4SLinus Torvalds 		.procname	= "dad_transmits",
56671da177e4SLinus Torvalds 		.data		= &ipv6_devconf.dad_transmits,
56681da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
56691da177e4SLinus Torvalds 		.mode		= 0644,
56706d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
56711da177e4SLinus Torvalds 	},
56721da177e4SLinus Torvalds 	{
56731da177e4SLinus Torvalds 		.procname	= "router_solicitations",
56741da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicits,
56751da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
56761da177e4SLinus Torvalds 		.mode		= 0644,
56776d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
56781da177e4SLinus Torvalds 	},
56791da177e4SLinus Torvalds 	{
56801da177e4SLinus Torvalds 		.procname	= "router_solicitation_interval",
56811da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_interval,
56821da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
56831da177e4SLinus Torvalds 		.mode		= 0644,
56846d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
56851da177e4SLinus Torvalds 	},
56861da177e4SLinus Torvalds 	{
56871da177e4SLinus Torvalds 		.procname	= "router_solicitation_delay",
56881da177e4SLinus Torvalds 		.data		= &ipv6_devconf.rtr_solicit_delay,
56891da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
56901da177e4SLinus Torvalds 		.mode		= 0644,
56916d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
56921da177e4SLinus Torvalds 	},
56931da177e4SLinus Torvalds 	{
56941da177e4SLinus Torvalds 		.procname	= "force_mld_version",
56951da177e4SLinus Torvalds 		.data		= &ipv6_devconf.force_mld_version,
56961da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
56971da177e4SLinus Torvalds 		.mode		= 0644,
56986d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
56991da177e4SLinus Torvalds 	},
5700fc4eba58SHannes Frederic Sowa 	{
5701fc4eba58SHannes Frederic Sowa 		.procname	= "mldv1_unsolicited_report_interval",
5702fc4eba58SHannes Frederic Sowa 		.data		=
5703fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv1_unsolicited_report_interval,
5704fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
5705fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
5706fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
5707fc4eba58SHannes Frederic Sowa 	},
5708fc4eba58SHannes Frederic Sowa 	{
5709fc4eba58SHannes Frederic Sowa 		.procname	= "mldv2_unsolicited_report_interval",
5710fc4eba58SHannes Frederic Sowa 		.data		=
5711fc4eba58SHannes Frederic Sowa 			&ipv6_devconf.mldv2_unsolicited_report_interval,
5712fc4eba58SHannes Frederic Sowa 		.maxlen		= sizeof(int),
5713fc4eba58SHannes Frederic Sowa 		.mode		= 0644,
5714fc4eba58SHannes Frederic Sowa 		.proc_handler	= proc_dointvec_ms_jiffies,
5715fc4eba58SHannes Frederic Sowa 	},
57161da177e4SLinus Torvalds 	{
57171da177e4SLinus Torvalds 		.procname	= "use_tempaddr",
57181da177e4SLinus Torvalds 		.data		= &ipv6_devconf.use_tempaddr,
57191da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
57201da177e4SLinus Torvalds 		.mode		= 0644,
57216d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
57221da177e4SLinus Torvalds 	},
57231da177e4SLinus Torvalds 	{
57241da177e4SLinus Torvalds 		.procname	= "temp_valid_lft",
57251da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_valid_lft,
57261da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
57271da177e4SLinus Torvalds 		.mode		= 0644,
57286d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
57291da177e4SLinus Torvalds 	},
57301da177e4SLinus Torvalds 	{
57311da177e4SLinus Torvalds 		.procname	= "temp_prefered_lft",
57321da177e4SLinus Torvalds 		.data		= &ipv6_devconf.temp_prefered_lft,
57331da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
57341da177e4SLinus Torvalds 		.mode		= 0644,
57356d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
57361da177e4SLinus Torvalds 	},
57371da177e4SLinus Torvalds 	{
57381da177e4SLinus Torvalds 		.procname	= "regen_max_retry",
57391da177e4SLinus Torvalds 		.data		= &ipv6_devconf.regen_max_retry,
57401da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
57411da177e4SLinus Torvalds 		.mode		= 0644,
57426d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
57431da177e4SLinus Torvalds 	},
57441da177e4SLinus Torvalds 	{
57451da177e4SLinus Torvalds 		.procname	= "max_desync_factor",
57461da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_desync_factor,
57471da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
57481da177e4SLinus Torvalds 		.mode		= 0644,
57496d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
57501da177e4SLinus Torvalds 	},
57511da177e4SLinus Torvalds 	{
57521da177e4SLinus Torvalds 		.procname	= "max_addresses",
57531da177e4SLinus Torvalds 		.data		= &ipv6_devconf.max_addresses,
57541da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
57551da177e4SLinus Torvalds 		.mode		= 0644,
57566d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
57571da177e4SLinus Torvalds 	},
57581da177e4SLinus Torvalds 	{
575965f5c7c1SYOSHIFUJI Hideaki 		.procname	= "accept_ra_defrtr",
576065f5c7c1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_defrtr,
576165f5c7c1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
576265f5c7c1SYOSHIFUJI Hideaki 		.mode		= 0644,
57636d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
576465f5c7c1SYOSHIFUJI Hideaki 	},
576565f5c7c1SYOSHIFUJI Hideaki 	{
57668013d1d7SHangbin Liu 		.procname	= "accept_ra_min_hop_limit",
57678013d1d7SHangbin Liu 		.data		= &ipv6_devconf.accept_ra_min_hop_limit,
57688013d1d7SHangbin Liu 		.maxlen		= sizeof(int),
57698013d1d7SHangbin Liu 		.mode		= 0644,
57708013d1d7SHangbin Liu 		.proc_handler	= proc_dointvec,
57718013d1d7SHangbin Liu 	},
57728013d1d7SHangbin Liu 	{
5773c4fd30ebSYOSHIFUJI Hideaki 		.procname	= "accept_ra_pinfo",
5774c4fd30ebSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_pinfo,
5775c4fd30ebSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
5776c4fd30ebSYOSHIFUJI Hideaki 		.mode		= 0644,
57776d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
5778c4fd30ebSYOSHIFUJI Hideaki 	},
5779930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
5780930d6ff2SYOSHIFUJI Hideaki 	{
5781930d6ff2SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rtr_pref",
5782930d6ff2SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rtr_pref,
5783930d6ff2SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
5784930d6ff2SYOSHIFUJI Hideaki 		.mode		= 0644,
57856d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
5786930d6ff2SYOSHIFUJI Hideaki 	},
578752e16356SYOSHIFUJI Hideaki 	{
578852e16356SYOSHIFUJI Hideaki 		.procname	= "router_probe_interval",
578952e16356SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.rtr_probe_interval,
579052e16356SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
579152e16356SYOSHIFUJI Hideaki 		.mode		= 0644,
57926d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
579352e16356SYOSHIFUJI Hideaki 	},
5794fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
579509c884d4SYOSHIFUJI Hideaki 	{
579609c884d4SYOSHIFUJI Hideaki 		.procname	= "accept_ra_rt_info_max_plen",
579709c884d4SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
579809c884d4SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
579909c884d4SYOSHIFUJI Hideaki 		.mode		= 0644,
58006d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
580109c884d4SYOSHIFUJI Hideaki 	},
580209c884d4SYOSHIFUJI Hideaki #endif
5803930d6ff2SYOSHIFUJI Hideaki #endif
5804c4fd30ebSYOSHIFUJI Hideaki 	{
5805fbea49e1SYOSHIFUJI Hideaki 		.procname	= "proxy_ndp",
5806fbea49e1SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.proxy_ndp,
5807fbea49e1SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
5808fbea49e1SYOSHIFUJI Hideaki 		.mode		= 0644,
5809c92d5491Sstephen hemminger 		.proc_handler	= addrconf_sysctl_proxy_ndp,
5810fbea49e1SYOSHIFUJI Hideaki 	},
5811fbea49e1SYOSHIFUJI Hideaki 	{
58120bcbc926SYOSHIFUJI Hideaki 		.procname	= "accept_source_route",
58130bcbc926SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_source_route,
58140bcbc926SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
58150bcbc926SYOSHIFUJI Hideaki 		.mode		= 0644,
58166d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
58170bcbc926SYOSHIFUJI Hideaki 	},
581895c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
581995c385b4SNeil Horman 	{
582095c385b4SNeil Horman 		.procname	= "optimistic_dad",
582195c385b4SNeil Horman 		.data		= &ipv6_devconf.optimistic_dad,
582295c385b4SNeil Horman 		.maxlen		= sizeof(int),
582395c385b4SNeil Horman 		.mode		= 0644,
58246d9f239aSAlexey Dobriyan 		.proc_handler   = proc_dointvec,
582595c385b4SNeil Horman 	},
58267fd2561eSErik Kline 	{
58277fd2561eSErik Kline 		.procname	= "use_optimistic",
58287fd2561eSErik Kline 		.data		= &ipv6_devconf.use_optimistic,
58297fd2561eSErik Kline 		.maxlen		= sizeof(int),
58307fd2561eSErik Kline 		.mode		= 0644,
58317fd2561eSErik Kline 		.proc_handler	= proc_dointvec,
58327fd2561eSErik Kline 	},
583395c385b4SNeil Horman #endif
58347bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
58357bc570c8SYOSHIFUJI Hideaki 	{
58367bc570c8SYOSHIFUJI Hideaki 		.procname	= "mc_forwarding",
58377bc570c8SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.mc_forwarding,
58387bc570c8SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
5839a4e6db07SDavid S. Miller 		.mode		= 0444,
58406d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
58417bc570c8SYOSHIFUJI Hideaki 	},
58427bc570c8SYOSHIFUJI Hideaki #endif
58430bcbc926SYOSHIFUJI Hideaki 	{
5844778d80beSYOSHIFUJI Hideaki 		.procname	= "disable_ipv6",
5845778d80beSYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.disable_ipv6,
5846778d80beSYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
5847778d80beSYOSHIFUJI Hideaki 		.mode		= 0644,
584856d417b1SBrian Haley 		.proc_handler	= addrconf_sysctl_disable,
5849778d80beSYOSHIFUJI Hideaki 	},
5850778d80beSYOSHIFUJI Hideaki 	{
58511b34be74SYOSHIFUJI Hideaki 		.procname	= "accept_dad",
58521b34be74SYOSHIFUJI Hideaki 		.data		= &ipv6_devconf.accept_dad,
58531b34be74SYOSHIFUJI Hideaki 		.maxlen		= sizeof(int),
58541b34be74SYOSHIFUJI Hideaki 		.mode		= 0644,
58556d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec,
58561b34be74SYOSHIFUJI Hideaki 	},
58571b34be74SYOSHIFUJI Hideaki 	{
5858f7734fdfSOctavian Purdila 		.procname	= "force_tllao",
5859f7734fdfSOctavian Purdila 		.data		= &ipv6_devconf.force_tllao,
5860f7734fdfSOctavian Purdila 		.maxlen		= sizeof(int),
5861f7734fdfSOctavian Purdila 		.mode		= 0644,
5862f7734fdfSOctavian Purdila 		.proc_handler	= proc_dointvec
5863f7734fdfSOctavian Purdila 	},
5864f7734fdfSOctavian Purdila 	{
58655cb04436SHannes Frederic Sowa 		.procname	= "ndisc_notify",
58665cb04436SHannes Frederic Sowa 		.data		= &ipv6_devconf.ndisc_notify,
58675cb04436SHannes Frederic Sowa 		.maxlen		= sizeof(int),
58685cb04436SHannes Frederic Sowa 		.mode		= 0644,
58695cb04436SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
58705cb04436SHannes Frederic Sowa 	},
58715cb04436SHannes Frederic Sowa 	{
5872b800c3b9SHannes Frederic Sowa 		.procname	= "suppress_frag_ndisc",
5873b800c3b9SHannes Frederic Sowa 		.data		= &ipv6_devconf.suppress_frag_ndisc,
5874b800c3b9SHannes Frederic Sowa 		.maxlen		= sizeof(int),
5875b800c3b9SHannes Frederic Sowa 		.mode		= 0644,
5876b800c3b9SHannes Frederic Sowa 		.proc_handler	= proc_dointvec
5877b800c3b9SHannes Frederic Sowa 	},
5878b800c3b9SHannes Frederic Sowa 	{
5879d9333196SBen Greear 		.procname	= "accept_ra_from_local",
5880d9333196SBen Greear 		.data		= &ipv6_devconf.accept_ra_from_local,
5881d9333196SBen Greear 		.maxlen		= sizeof(int),
5882d9333196SBen Greear 		.mode		= 0644,
5883d9333196SBen Greear 		.proc_handler	= proc_dointvec,
5884d9333196SBen Greear 	},
5885d9333196SBen Greear 	{
5886c2943f14SHarout Hedeshian 		.procname	= "accept_ra_mtu",
5887c2943f14SHarout Hedeshian 		.data		= &ipv6_devconf.accept_ra_mtu,
5888c2943f14SHarout Hedeshian 		.maxlen		= sizeof(int),
5889c2943f14SHarout Hedeshian 		.mode		= 0644,
5890c2943f14SHarout Hedeshian 		.proc_handler	= proc_dointvec,
5891c2943f14SHarout Hedeshian 	},
5892c2943f14SHarout Hedeshian 	{
58933d1bec99SHannes Frederic Sowa 		.procname	= "stable_secret",
58943d1bec99SHannes Frederic Sowa 		.data		= &ipv6_devconf.stable_secret,
58953d1bec99SHannes Frederic Sowa 		.maxlen		= IPV6_MAX_STRLEN,
58963d1bec99SHannes Frederic Sowa 		.mode		= 0600,
58973d1bec99SHannes Frederic Sowa 		.proc_handler	= addrconf_sysctl_stable_secret,
58983d1bec99SHannes Frederic Sowa 	},
58993d1bec99SHannes Frederic Sowa 	{
59003985e8a3SErik Kline 		.procname	= "use_oif_addrs_only",
59013985e8a3SErik Kline 		.data		= &ipv6_devconf.use_oif_addrs_only,
59023985e8a3SErik Kline 		.maxlen		= sizeof(int),
59033985e8a3SErik Kline 		.mode		= 0644,
59043985e8a3SErik Kline 		.proc_handler	= proc_dointvec,
590535103d11SAndy Gospodarek 	},
590635103d11SAndy Gospodarek 	{
590735103d11SAndy Gospodarek 		.procname	= "ignore_routes_with_linkdown",
590835103d11SAndy Gospodarek 		.data		= &ipv6_devconf.ignore_routes_with_linkdown,
590935103d11SAndy Gospodarek 		.maxlen		= sizeof(int),
591035103d11SAndy Gospodarek 		.mode		= 0644,
591135103d11SAndy Gospodarek 		.proc_handler	= addrconf_sysctl_ignore_routes_with_linkdown,
59123985e8a3SErik Kline 	},
59133985e8a3SErik Kline 	{
5914abbc3043SJohannes Berg 		.procname	= "drop_unicast_in_l2_multicast",
5915abbc3043SJohannes Berg 		.data		= &ipv6_devconf.drop_unicast_in_l2_multicast,
5916abbc3043SJohannes Berg 		.maxlen		= sizeof(int),
5917abbc3043SJohannes Berg 		.mode		= 0644,
5918abbc3043SJohannes Berg 		.proc_handler	= proc_dointvec,
5919abbc3043SJohannes Berg 	},
5920abbc3043SJohannes Berg 	{
59217a02bf89SJohannes Berg 		.procname	= "drop_unsolicited_na",
59227a02bf89SJohannes Berg 		.data		= &ipv6_devconf.drop_unsolicited_na,
59237a02bf89SJohannes Berg 		.maxlen		= sizeof(int),
59247a02bf89SJohannes Berg 		.mode		= 0644,
59257a02bf89SJohannes Berg 		.proc_handler	= proc_dointvec,
59267a02bf89SJohannes Berg 	},
59277a02bf89SJohannes Berg 	{
5928f1705ec1SDavid Ahern 		.procname	= "keep_addr_on_down",
5929f1705ec1SDavid Ahern 		.data		= &ipv6_devconf.keep_addr_on_down,
5930f1705ec1SDavid Ahern 		.maxlen		= sizeof(int),
5931f1705ec1SDavid Ahern 		.mode		= 0644,
5932f1705ec1SDavid Ahern 		.proc_handler	= proc_dointvec,
5933f1705ec1SDavid Ahern 
5934f1705ec1SDavid Ahern 	},
5935f1705ec1SDavid Ahern 	{
5936f8572d8fSEric W. Biederman 		/* sentinel */
59371da177e4SLinus Torvalds 	}
59381da177e4SLinus Torvalds };
59391da177e4SLinus Torvalds 
5940bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5941f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
59421da177e4SLinus Torvalds {
59431da177e4SLinus Torvalds 	int i;
5944607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
59456105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
59461dab6222SPavel Emelyanov 
5947607ea7cdSKonstantin Khlebnikov 	table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
5948607ea7cdSKonstantin Khlebnikov 	if (!table)
5949f68635e6SPavel Emelyanov 		goto out;
5950f68635e6SPavel Emelyanov 
5951607ea7cdSKonstantin Khlebnikov 	for (i = 0; table[i].data; i++) {
5952607ea7cdSKonstantin Khlebnikov 		table[i].data += (char *)p - (char *)&ipv6_devconf;
5953607ea7cdSKonstantin Khlebnikov 		table[i].extra1 = idev; /* embedded; no ref */
5954607ea7cdSKonstantin Khlebnikov 		table[i].extra2 = net;
59551da177e4SLinus Torvalds 	}
59561da177e4SLinus Torvalds 
59576105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
59581da177e4SLinus Torvalds 
5959607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = register_net_sysctl(net, path, table);
5960607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
59616105e293SEric W. Biederman 		goto free;
5962f68635e6SPavel Emelyanov 
596395897312SPavel Emelyanov 	return 0;
59641da177e4SLinus Torvalds 
59651da177e4SLinus Torvalds free:
5966607ea7cdSKonstantin Khlebnikov 	kfree(table);
5967f68635e6SPavel Emelyanov out:
596895897312SPavel Emelyanov 	return -ENOBUFS;
59691da177e4SLinus Torvalds }
59701da177e4SLinus Torvalds 
5971408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5972f52295a9SPavel Emelyanov {
5973607ea7cdSKonstantin Khlebnikov 	struct ctl_table *table;
5974f52295a9SPavel Emelyanov 
5975607ea7cdSKonstantin Khlebnikov 	if (!p->sysctl_header)
5976408c4768SPavel Emelyanov 		return;
5977408c4768SPavel Emelyanov 
5978607ea7cdSKonstantin Khlebnikov 	table = p->sysctl_header->ctl_table_arg;
5979607ea7cdSKonstantin Khlebnikov 	unregister_net_sysctl_table(p->sysctl_header);
5980607ea7cdSKonstantin Khlebnikov 	p->sysctl_header = NULL;
5981607ea7cdSKonstantin Khlebnikov 	kfree(table);
59821da177e4SLinus Torvalds }
5983408c4768SPavel Emelyanov 
5984a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev)
5985408c4768SPavel Emelyanov {
5986a317a2f1SWANG Cong 	int err;
5987a317a2f1SWANG Cong 
5988a317a2f1SWANG Cong 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
5989a317a2f1SWANG Cong 		return -EINVAL;
5990a317a2f1SWANG Cong 
5991a317a2f1SWANG Cong 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
5992f8572d8fSEric W. Biederman 				    &ndisc_ifinfo_sysctl_change);
5993a317a2f1SWANG Cong 	if (err)
5994a317a2f1SWANG Cong 		return err;
5995a317a2f1SWANG Cong 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5996f8572d8fSEric W. Biederman 					 idev, &idev->cnf);
5997a317a2f1SWANG Cong 	if (err)
5998a317a2f1SWANG Cong 		neigh_sysctl_unregister(idev->nd_parms);
5999a317a2f1SWANG Cong 
6000a317a2f1SWANG Cong 	return err;
6001408c4768SPavel Emelyanov }
6002408c4768SPavel Emelyanov 
6003408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6004408c4768SPavel Emelyanov {
6005408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
6006408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
60071da177e4SLinus Torvalds }
60081da177e4SLinus Torvalds 
60091da177e4SLinus Torvalds 
60101da177e4SLinus Torvalds #endif
60111da177e4SLinus Torvalds 
60122c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
6013e0da5a48SPavel Emelyanov {
6014a79ca223SHong Zhiguo 	int err = -ENOMEM;
6015e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
6016e0da5a48SPavel Emelyanov 
6017a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
601863159f29SIan Morris 	if (!all)
6019e0da5a48SPavel Emelyanov 		goto err_alloc_all;
6020e0da5a48SPavel Emelyanov 
6021a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
602263159f29SIan Morris 	if (!dflt)
6023e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
6024a79ca223SHong Zhiguo 
602556d417b1SBrian Haley 	/* these will be inherited by all namespaces */
602656d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
602756d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6028e0da5a48SPavel Emelyanov 
60293d1bec99SHannes Frederic Sowa 	dflt->stable_secret.initialized = false;
60303d1bec99SHannes Frederic Sowa 	all->stable_secret.initialized = false;
60313d1bec99SHannes Frederic Sowa 
6032e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
6033e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
6034e0da5a48SPavel Emelyanov 
6035e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
6036f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
6037e0da5a48SPavel Emelyanov 	if (err < 0)
6038e0da5a48SPavel Emelyanov 		goto err_reg_all;
6039e0da5a48SPavel Emelyanov 
6040f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6041e0da5a48SPavel Emelyanov 	if (err < 0)
6042e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
6043e0da5a48SPavel Emelyanov #endif
6044e0da5a48SPavel Emelyanov 	return 0;
6045e0da5a48SPavel Emelyanov 
6046e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
6047e0da5a48SPavel Emelyanov err_reg_dflt:
6048e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
6049e0da5a48SPavel Emelyanov err_reg_all:
6050e0da5a48SPavel Emelyanov 	kfree(dflt);
6051e0da5a48SPavel Emelyanov #endif
6052e0da5a48SPavel Emelyanov err_alloc_dflt:
6053e0da5a48SPavel Emelyanov 	kfree(all);
6054e0da5a48SPavel Emelyanov err_alloc_all:
6055e0da5a48SPavel Emelyanov 	return err;
6056e0da5a48SPavel Emelyanov }
6057e0da5a48SPavel Emelyanov 
60582c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
6059e0da5a48SPavel Emelyanov {
6060e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
6061e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6062e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
6063e0da5a48SPavel Emelyanov #endif
6064e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_dflt);
6065e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_all);
6066e0da5a48SPavel Emelyanov }
6067e0da5a48SPavel Emelyanov 
6068e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
6069e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
6070e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
6071e0da5a48SPavel Emelyanov };
6072e0da5a48SPavel Emelyanov 
6073207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = {
6074b382b191SThomas Graf 	.family		  = AF_INET6,
6075b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
6076b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
607711b1f828SDaniel Borkmann 	.validate_link_af = inet6_validate_link_af,
6078f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
6079b382b191SThomas Graf };
6080b382b191SThomas Graf 
60811da177e4SLinus Torvalds /*
60821da177e4SLinus Torvalds  *	Init / cleanup code
60831da177e4SLinus Torvalds  */
60841da177e4SLinus Torvalds 
60851da177e4SLinus Torvalds int __init addrconf_init(void)
60861da177e4SLinus Torvalds {
6087a317a2f1SWANG Cong 	struct inet6_dev *idev;
6088c2e21293Sstephen hemminger 	int i, err;
60892a8cc6c8SYOSHIFUJI Hideaki 
6090e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
6091e21e8467SStephen Hemminger 	if (err < 0) {
6092f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
6093f3213831SJoe Perches 			__func__, err);
60942cc6d2bfSNeil Horman 		goto out;
60952a8cc6c8SYOSHIFUJI Hideaki 	}
60961da177e4SLinus Torvalds 
60972cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
60982cc6d2bfSNeil Horman 	if (err < 0)
60992cc6d2bfSNeil Horman 		goto out_addrlabel;
6100e0da5a48SPavel Emelyanov 
6101c15b1ccaSHannes Frederic Sowa 	addrconf_wq = create_workqueue("ipv6_addrconf");
6102c15b1ccaSHannes Frederic Sowa 	if (!addrconf_wq) {
6103c15b1ccaSHannes Frederic Sowa 		err = -ENOMEM;
6104c15b1ccaSHannes Frederic Sowa 		goto out_nowq;
6105c15b1ccaSHannes Frederic Sowa 	}
6106c15b1ccaSHannes Frederic Sowa 
61071da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
61081da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
61091da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
61101da177e4SLinus Torvalds 	 * to other devices which are up.
61111da177e4SLinus Torvalds 	 *
61121da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
61131da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
61141da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
61151da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
61161da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
61171da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
61181da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
61191da177e4SLinus Torvalds 	 *
61201da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
61211da177e4SLinus Torvalds 	 * loopback_dev first by hand.
61221da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
61231da177e4SLinus Torvalds 	 * device and it being up should be removed.
61241da177e4SLinus Torvalds 	 */
61251da177e4SLinus Torvalds 	rtnl_lock();
6126a317a2f1SWANG Cong 	idev = ipv6_add_dev(init_net.loopback_dev);
61271da177e4SLinus Torvalds 	rtnl_unlock();
6128a317a2f1SWANG Cong 	if (IS_ERR(idev)) {
6129a317a2f1SWANG Cong 		err = PTR_ERR(idev);
6130e0da5a48SPavel Emelyanov 		goto errlo;
6131a317a2f1SWANG Cong 	}
61321da177e4SLinus Torvalds 
6133c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
6134c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6135c2e21293Sstephen hemminger 
61361da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
61371da177e4SLinus Torvalds 
6138c15b1ccaSHannes Frederic Sowa 	addrconf_verify();
6139c127ea2cSThomas Graf 
61403678a9d8Sstephen hemminger 	rtnl_af_register(&inet6_ops);
6141b382b191SThomas Graf 
6142c7ac8679SGreg Rose 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6143c7ac8679SGreg Rose 			      NULL);
6144c127ea2cSThomas Graf 	if (err < 0)
6145c127ea2cSThomas Graf 		goto errout;
6146c127ea2cSThomas Graf 
6147c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
6148c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6149c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6150c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6151c7ac8679SGreg Rose 			inet6_dump_ifaddr, NULL);
6152c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6153c7ac8679SGreg Rose 			inet6_dump_ifmcaddr, NULL);
6154c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6155c7ac8679SGreg Rose 			inet6_dump_ifacaddr, NULL);
615676f8f6cbSNicolas Dichtel 	__rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
61577a674200SNicolas Dichtel 			inet6_netconf_dump_devconf, NULL);
6158c127ea2cSThomas Graf 
61592a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
61602a8cc6c8SYOSHIFUJI Hideaki 
61611da177e4SLinus Torvalds 	return 0;
6162c127ea2cSThomas Graf errout:
6163b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
6164c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
6165e0da5a48SPavel Emelyanov errlo:
6166c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
6167c15b1ccaSHannes Frederic Sowa out_nowq:
6168e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
61692cc6d2bfSNeil Horman out_addrlabel:
61702cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
61712cc6d2bfSNeil Horman out:
6172c127ea2cSThomas Graf 	return err;
61731da177e4SLinus Torvalds }
61741da177e4SLinus Torvalds 
617509f7709fSDaniel Lezcano void addrconf_cleanup(void)
61761da177e4SLinus Torvalds {
6177176c39afSDaniel Lezcano 	struct net_device *dev;
61781da177e4SLinus Torvalds 	int i;
61791da177e4SLinus Torvalds 
61801da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
6181e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
61822cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
61831da177e4SLinus Torvalds 
61841da177e4SLinus Torvalds 	rtnl_lock();
61851da177e4SLinus Torvalds 
6186b382b191SThomas Graf 	__rtnl_af_unregister(&inet6_ops);
6187b382b191SThomas Graf 
6188176c39afSDaniel Lezcano 	/* clean dev list */
6189176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
6190176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
6191176c39afSDaniel Lezcano 			continue;
6192176c39afSDaniel Lezcano 		addrconf_ifdown(dev, 1);
6193176c39afSDaniel Lezcano 	}
6194176c39afSDaniel Lezcano 	addrconf_ifdown(init_net.loopback_dev, 2);
6195176c39afSDaniel Lezcano 
61961da177e4SLinus Torvalds 	/*
61971da177e4SLinus Torvalds 	 *	Check hash table.
61981da177e4SLinus Torvalds 	 */
61995c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
6200c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
6201c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
62025c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
6203c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
62041da177e4SLinus Torvalds 	rtnl_unlock();
6205c15b1ccaSHannes Frederic Sowa 
6206c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
62071da177e4SLinus Torvalds }
6208