xref: /openbmc/linux/net/ipv6/addrconf.c (revision a97eb33ff225f34a8124774b3373fd244f0e83ce)
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,
2191da177e4SLinus Torvalds };
2201da177e4SLinus Torvalds 
221ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2221da177e4SLinus Torvalds 	.forwarding		= 0,
2231da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2241da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2251da177e4SLinus Torvalds 	.accept_ra		= 1,
2261da177e4SLinus Torvalds 	.accept_redirects	= 1,
2271da177e4SLinus Torvalds 	.autoconf		= 1,
228fc4eba58SHannes Frederic Sowa 	.force_mld_version	= 0,
229fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
230fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2311da177e4SLinus Torvalds 	.dad_transmits		= 1,
2321da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2331da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
2341da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2351da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2361da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2371da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2381da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2391da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2401da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
24165f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
242d9333196SBen Greear 	.accept_ra_from_local	= 0,
2438013d1d7SHangbin Liu 	.accept_ra_min_hop_limit= 1,
244c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
245930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
246930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
24752e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
24809c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
24909c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
25009c884d4SYOSHIFUJI Hideaki #endif
251930d6ff2SYOSHIFUJI Hideaki #endif
252fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2530bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
254778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2551b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
256b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
257c2943f14SHarout Hedeshian 	.accept_ra_mtu		= 1,
2583d1bec99SHannes Frederic Sowa 	.stable_secret		= {
2593d1bec99SHannes Frederic Sowa 		.initialized = false,
2603d1bec99SHannes Frederic Sowa 	},
2613985e8a3SErik Kline 	.use_oif_addrs_only	= 0,
26235103d11SAndy Gospodarek 	.ignore_routes_with_linkdown = 0,
2631da177e4SLinus Torvalds };
2641da177e4SLinus Torvalds 
265f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
26605297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev)
267f24e3d65SMitsuru Chinen {
26805297949SDavid S. Miller 	return !qdisc_tx_is_noop(dev);
269f24e3d65SMitsuru Chinen }
270f24e3d65SMitsuru Chinen 
271b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev)
2721da177e4SLinus Torvalds {
273b7b1bfceSHannes Frederic Sowa 	if (del_timer(&idev->rs_timer))
274b7b1bfceSHannes Frederic Sowa 		__in6_dev_put(idev);
275b7b1bfceSHannes Frederic Sowa }
276b7b1bfceSHannes Frederic Sowa 
277c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
278b7b1bfceSHannes Frederic Sowa {
279c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
2801da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
283b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev,
2841da177e4SLinus Torvalds 				  unsigned long when)
2851da177e4SLinus Torvalds {
286b7b1bfceSHannes Frederic Sowa 	if (!timer_pending(&idev->rs_timer))
287b7b1bfceSHannes Frederic Sowa 		in6_dev_hold(idev);
288b7b1bfceSHannes Frederic Sowa 	mod_timer(&idev->rs_timer, jiffies + when);
2891da177e4SLinus Torvalds }
290b7b1bfceSHannes Frederic Sowa 
291c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
292c15b1ccaSHannes Frederic Sowa 				   unsigned long delay)
293b7b1bfceSHannes Frederic Sowa {
294c15b1ccaSHannes Frederic Sowa 	if (!delayed_work_pending(&ifp->dad_work))
295b7b1bfceSHannes Frederic Sowa 		in6_ifa_hold(ifp);
296c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2997f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
3007f7d9a6bSHerbert Xu {
301827da44cSJohn Stultz 	int i;
302827da44cSJohn Stultz 
303698365faSWANG Cong 	idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
304698365faSWANG Cong 	if (!idev->stats.ipv6)
3057f7d9a6bSHerbert Xu 		goto err_ip;
306827da44cSJohn Stultz 
307827da44cSJohn Stultz 	for_each_possible_cpu(i) {
308827da44cSJohn Stultz 		struct ipstats_mib *addrconf_stats;
309698365faSWANG Cong 		addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
310827da44cSJohn Stultz 		u64_stats_init(&addrconf_stats->syncp);
311827da44cSJohn Stultz 	}
312827da44cSJohn Stultz 
313827da44cSJohn Stultz 
314be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
315be281e55SEric Dumazet 					GFP_KERNEL);
316be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
3177f7d9a6bSHerbert Xu 		goto err_icmp;
318be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
319be281e55SEric Dumazet 					   GFP_KERNEL);
320be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
32114878f75SDavid L Stevens 		goto err_icmpmsg;
3227f7d9a6bSHerbert Xu 
3237f7d9a6bSHerbert Xu 	return 0;
3247f7d9a6bSHerbert Xu 
32514878f75SDavid L Stevens err_icmpmsg:
326be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3277f7d9a6bSHerbert Xu err_icmp:
328698365faSWANG Cong 	free_percpu(idev->stats.ipv6);
3297f7d9a6bSHerbert Xu err_ip:
330aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3317f7d9a6bSHerbert Xu }
3327f7d9a6bSHerbert Xu 
3331da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	struct inet6_dev *ndev;
336a317a2f1SWANG Cong 	int err = -ENOMEM;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	ASSERT_RTNL();
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
341a317a2f1SWANG Cong 		return ERR_PTR(-EINVAL);
3421da177e4SLinus Torvalds 
343322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
34463159f29SIan Morris 	if (!ndev)
345a317a2f1SWANG Cong 		return ERR_PTR(err);
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3481da177e4SLinus Torvalds 	ndev->dev = dev;
349502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
350b7b1bfceSHannes Frederic Sowa 	setup_timer(&ndev->rs_timer, addrconf_rs_timer,
351b7b1bfceSHannes Frederic Sowa 		    (unsigned long)ndev);
352c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3539b29c696SHannes Frederic Sowa 
3549b29c696SHannes Frederic Sowa 	if (ndev->cnf.stable_secret.initialized)
3559b29c696SHannes Frederic Sowa 		ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
3569b29c696SHannes Frederic Sowa 	else
3579b29c696SHannes Frederic Sowa 		ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64;
3589b29c696SHannes Frederic Sowa 
3591da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3601da177e4SLinus Torvalds 	ndev->cnf.sysctl = NULL;
3611da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
36263159f29SIan Morris 	if (!ndev->nd_parms) {
3631da177e4SLinus Torvalds 		kfree(ndev);
364a317a2f1SWANG Cong 		return ERR_PTR(err);
3651da177e4SLinus Torvalds 	}
3660187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
3670187bdfbSBen Hutchings 		dev_disable_lro(dev);
3681da177e4SLinus Torvalds 	/* We refer to the device */
3691da177e4SLinus Torvalds 	dev_hold(dev);
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
372ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
373f3213831SJoe Perches 			"%s: cannot allocate memory for statistics; dev=%s.\n",
374ba3542e1Sdingtianhong 			__func__, dev->name);
3751da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3768603e33dSRoy Li 		dev_put(dev);
3778603e33dSRoy Li 		kfree(ndev);
378a317a2f1SWANG Cong 		return ERR_PTR(err);
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
382ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
383f3213831SJoe Perches 			"%s: cannot create /proc/net/dev_snmp6/%s\n",
384ba3542e1Sdingtianhong 			__func__, dev->name);
385a317a2f1SWANG Cong 		goto err_release;
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3891da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3901da177e4SLinus Torvalds 	 */
3911da177e4SLinus Torvalds 	in6_dev_hold(ndev);
3921da177e4SLinus Torvalds 
3931b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
3941b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
3951b34be74SYOSHIFUJI Hideaki 
39607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
397c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
398f3213831SJoe Perches 		pr_info("%s: Disabled Multicast RS\n", dev->name);
399c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
400c7dc89c0SFred L. Templin 	}
401b077d7abSYOSHIFUJI Hideaki #endif
402b077d7abSYOSHIFUJI Hideaki 
403372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
4041da177e4SLinus Torvalds 	setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
4051da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
4061da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4079625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4081da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4091da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4100be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
4111da177e4SLinus Torvalds 	} else {
4121da177e4SLinus Torvalds 		in6_dev_hold(ndev);
4131da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
4141da177e4SLinus Torvalds 	}
4155d9efa7eSDavid S. Miller 
416914faa14SDaniel Borkmann 	ndev->token = in6addr_any;
4171da177e4SLinus Torvalds 
418f24e3d65SMitsuru Chinen 	if (netif_running(dev) && addrconf_qdisc_ok(dev))
41953aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
42053aadcc9SHerbert Xu 
4211da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4221da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
423a317a2f1SWANG Cong 	err = addrconf_sysctl_register(ndev);
424a317a2f1SWANG Cong 	if (err) {
425a317a2f1SWANG Cong 		ipv6_mc_destroy_dev(ndev);
426a317a2f1SWANG Cong 		del_timer(&ndev->regen_timer);
4272a189f9eSSabrina Dubroca 		snmp6_unregister_dev(ndev);
428a317a2f1SWANG Cong 		goto err_release;
429a317a2f1SWANG Cong 	}
43030c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
431cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip6_ptr, ndev);
432d88ae4ccSYOSHIFUJI Hideaki 
4332c5e8933SHannes Frederic Sowa 	/* Join interface-local all-node multicast group */
4342c5e8933SHannes Frederic Sowa 	ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4352c5e8933SHannes Frederic Sowa 
436d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
437f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
438d88ae4ccSYOSHIFUJI Hideaki 
439d6ddef9eSLi Wei 	/* Join all-router multicast group if forwarding is set */
4408b2aaedeSLi Wei 	if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
441d6ddef9eSLi Wei 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
442d6ddef9eSLi Wei 
4431da177e4SLinus Torvalds 	return ndev;
444a317a2f1SWANG Cong 
445a317a2f1SWANG Cong err_release:
446a317a2f1SWANG Cong 	neigh_parms_release(&nd_tbl, ndev->nd_parms);
447a317a2f1SWANG Cong 	ndev->dead = 1;
448a317a2f1SWANG Cong 	in6_dev_finish_destroy(ndev);
449a317a2f1SWANG Cong 	return ERR_PTR(err);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
452cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	struct inet6_dev *idev;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	ASSERT_RTNL();
4571da177e4SLinus Torvalds 
458e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
459e21e8467SStephen Hemminger 	if (!idev) {
460e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
461a317a2f1SWANG Cong 		if (IS_ERR(idev))
4621da177e4SLinus Torvalds 			return NULL;
4631da177e4SLinus Torvalds 	}
464c5e33bddSYOSHIFUJI Hideaki 
4651da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4661da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4671da177e4SLinus Torvalds 	return idev;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
470f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
471f3a1bfb1SNicolas Dichtel {
472f3a1bfb1SNicolas Dichtel 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
473f3a1bfb1SNicolas Dichtel 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
474f3a1bfb1SNicolas Dichtel 
47576f8f6cbSNicolas Dichtel 	/* type -1 is used for ALL */
47676f8f6cbSNicolas Dichtel 	if (type == -1 || type == NETCONFA_FORWARDING)
477f3a1bfb1SNicolas Dichtel 		size += nla_total_size(4);
478b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
479d67b8c61SNicolas Dichtel 	if (type == -1 || type == NETCONFA_MC_FORWARDING)
480d67b8c61SNicolas Dichtel 		size += nla_total_size(4);
481b1afce95SDavid S. Miller #endif
482c92d5491Sstephen hemminger 	if (type == -1 || type == NETCONFA_PROXY_NEIGH)
483c92d5491Sstephen hemminger 		size += nla_total_size(4);
484f3a1bfb1SNicolas Dichtel 
48535103d11SAndy Gospodarek 	if (type == -1 || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
48635103d11SAndy Gospodarek 		size += nla_total_size(4);
48735103d11SAndy Gospodarek 
488f3a1bfb1SNicolas Dichtel 	return size;
489f3a1bfb1SNicolas Dichtel }
490f3a1bfb1SNicolas Dichtel 
491f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
492f3a1bfb1SNicolas Dichtel 				      struct ipv6_devconf *devconf, u32 portid,
493f3a1bfb1SNicolas Dichtel 				      u32 seq, int event, unsigned int flags,
494f3a1bfb1SNicolas Dichtel 				      int type)
495f3a1bfb1SNicolas Dichtel {
496f3a1bfb1SNicolas Dichtel 	struct nlmsghdr  *nlh;
497f3a1bfb1SNicolas Dichtel 	struct netconfmsg *ncm;
498f3a1bfb1SNicolas Dichtel 
499f3a1bfb1SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
500f3a1bfb1SNicolas Dichtel 			flags);
50163159f29SIan Morris 	if (!nlh)
502f3a1bfb1SNicolas Dichtel 		return -EMSGSIZE;
503f3a1bfb1SNicolas Dichtel 
504f3a1bfb1SNicolas Dichtel 	ncm = nlmsg_data(nlh);
505f3a1bfb1SNicolas Dichtel 	ncm->ncm_family = AF_INET6;
506f3a1bfb1SNicolas Dichtel 
507f3a1bfb1SNicolas Dichtel 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
508f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
509f3a1bfb1SNicolas Dichtel 
51076f8f6cbSNicolas Dichtel 	/* type -1 is used for ALL */
51176f8f6cbSNicolas Dichtel 	if ((type == -1 || type == NETCONFA_FORWARDING) &&
512f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
513f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
514b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
515d67b8c61SNicolas Dichtel 	if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
516d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
517d67b8c61SNicolas Dichtel 			devconf->mc_forwarding) < 0)
518d67b8c61SNicolas Dichtel 		goto nla_put_failure;
519b1afce95SDavid S. Miller #endif
520c92d5491Sstephen hemminger 	if ((type == -1 || type == NETCONFA_PROXY_NEIGH) &&
521c92d5491Sstephen hemminger 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
522c92d5491Sstephen hemminger 		goto nla_put_failure;
523c92d5491Sstephen hemminger 
52435103d11SAndy Gospodarek 	if ((type == -1 || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
52535103d11SAndy Gospodarek 	    nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
52635103d11SAndy Gospodarek 			devconf->ignore_routes_with_linkdown) < 0)
52735103d11SAndy Gospodarek 		goto nla_put_failure;
52835103d11SAndy Gospodarek 
529053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
530053c095aSJohannes Berg 	return 0;
531f3a1bfb1SNicolas Dichtel 
532f3a1bfb1SNicolas Dichtel nla_put_failure:
533f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
534f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
535f3a1bfb1SNicolas Dichtel }
536f3a1bfb1SNicolas Dichtel 
537d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
538f3a1bfb1SNicolas Dichtel 				  struct ipv6_devconf *devconf)
539f3a1bfb1SNicolas Dichtel {
540f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
541f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
542f3a1bfb1SNicolas Dichtel 
543f3a1bfb1SNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
54463159f29SIan Morris 	if (!skb)
545f3a1bfb1SNicolas Dichtel 		goto errout;
546f3a1bfb1SNicolas Dichtel 
547f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
548f3a1bfb1SNicolas Dichtel 					 RTM_NEWNETCONF, 0, type);
549f3a1bfb1SNicolas Dichtel 	if (err < 0) {
550f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
551f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
552f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
553f3a1bfb1SNicolas Dichtel 		goto errout;
554f3a1bfb1SNicolas Dichtel 	}
555f3a1bfb1SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
556f3a1bfb1SNicolas Dichtel 	return;
557f3a1bfb1SNicolas Dichtel errout:
558f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
559f3a1bfb1SNicolas Dichtel }
560f3a1bfb1SNicolas Dichtel 
56176f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
56276f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
56376f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
564c92d5491Sstephen hemminger 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
56535103d11SAndy Gospodarek 	[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]	= { .len = sizeof(int) },
56676f8f6cbSNicolas Dichtel };
56776f8f6cbSNicolas Dichtel 
56876f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
569661d2967SThomas Graf 				     struct nlmsghdr *nlh)
57076f8f6cbSNicolas Dichtel {
57176f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
57276f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
57376f8f6cbSNicolas Dichtel 	struct netconfmsg *ncm;
57476f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
57576f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
57676f8f6cbSNicolas Dichtel 	struct inet6_dev *in6_dev;
57776f8f6cbSNicolas Dichtel 	struct net_device *dev;
57876f8f6cbSNicolas Dichtel 	int ifindex;
57976f8f6cbSNicolas Dichtel 	int err;
58076f8f6cbSNicolas Dichtel 
58176f8f6cbSNicolas Dichtel 	err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
58276f8f6cbSNicolas Dichtel 			  devconf_ipv6_policy);
58376f8f6cbSNicolas Dichtel 	if (err < 0)
58476f8f6cbSNicolas Dichtel 		goto errout;
58576f8f6cbSNicolas Dichtel 
586*a97eb33fSAnton Protopopov 	err = -EINVAL;
58776f8f6cbSNicolas Dichtel 	if (!tb[NETCONFA_IFINDEX])
58876f8f6cbSNicolas Dichtel 		goto errout;
58976f8f6cbSNicolas Dichtel 
59076f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
59176f8f6cbSNicolas Dichtel 	switch (ifindex) {
59276f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
59376f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
59476f8f6cbSNicolas Dichtel 		break;
59576f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
59676f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
59776f8f6cbSNicolas Dichtel 		break;
59876f8f6cbSNicolas Dichtel 	default:
59976f8f6cbSNicolas Dichtel 		dev = __dev_get_by_index(net, ifindex);
60063159f29SIan Morris 		if (!dev)
60176f8f6cbSNicolas Dichtel 			goto errout;
60276f8f6cbSNicolas Dichtel 		in6_dev = __in6_dev_get(dev);
60363159f29SIan Morris 		if (!in6_dev)
60476f8f6cbSNicolas Dichtel 			goto errout;
60576f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
60676f8f6cbSNicolas Dichtel 		break;
60776f8f6cbSNicolas Dichtel 	}
60876f8f6cbSNicolas Dichtel 
60976f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
61076f8f6cbSNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
61163159f29SIan Morris 	if (!skb)
61276f8f6cbSNicolas Dichtel 		goto errout;
61376f8f6cbSNicolas Dichtel 
61476f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
61576f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
61676f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
61776f8f6cbSNicolas Dichtel 					 -1);
61876f8f6cbSNicolas Dichtel 	if (err < 0) {
61976f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
62076f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
62176f8f6cbSNicolas Dichtel 		kfree_skb(skb);
62276f8f6cbSNicolas Dichtel 		goto errout;
62376f8f6cbSNicolas Dichtel 	}
62476f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
62576f8f6cbSNicolas Dichtel errout:
62676f8f6cbSNicolas Dichtel 	return err;
62776f8f6cbSNicolas Dichtel }
62876f8f6cbSNicolas Dichtel 
6297a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
6307a674200SNicolas Dichtel 				      struct netlink_callback *cb)
6317a674200SNicolas Dichtel {
6327a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
6337a674200SNicolas Dichtel 	int h, s_h;
6347a674200SNicolas Dichtel 	int idx, s_idx;
6357a674200SNicolas Dichtel 	struct net_device *dev;
6367a674200SNicolas Dichtel 	struct inet6_dev *idev;
6377a674200SNicolas Dichtel 	struct hlist_head *head;
6387a674200SNicolas Dichtel 
6397a674200SNicolas Dichtel 	s_h = cb->args[0];
6407a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
6417a674200SNicolas Dichtel 
6427a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6437a674200SNicolas Dichtel 		idx = 0;
6447a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
6457a674200SNicolas Dichtel 		rcu_read_lock();
64663998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
64763998ac2SNicolas Dichtel 			  net->dev_base_seq;
6487a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
6497a674200SNicolas Dichtel 			if (idx < s_idx)
6507a674200SNicolas Dichtel 				goto cont;
6517a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
6527a674200SNicolas Dichtel 			if (!idev)
6537a674200SNicolas Dichtel 				goto cont;
6547a674200SNicolas Dichtel 
6557a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
6567a674200SNicolas Dichtel 						       &idev->cnf,
6577a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
6587a674200SNicolas Dichtel 						       cb->nlh->nlmsg_seq,
6597a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
6607a674200SNicolas Dichtel 						       NLM_F_MULTI,
661053c095aSJohannes Berg 						       -1) < 0) {
6627a674200SNicolas Dichtel 				rcu_read_unlock();
6637a674200SNicolas Dichtel 				goto done;
6647a674200SNicolas Dichtel 			}
66563998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
6667a674200SNicolas Dichtel cont:
6677a674200SNicolas Dichtel 			idx++;
6687a674200SNicolas Dichtel 		}
6697a674200SNicolas Dichtel 		rcu_read_unlock();
6707a674200SNicolas Dichtel 	}
6717a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
6727a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
6737a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
6747a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6757a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6767a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
677053c095aSJohannes Berg 					       -1) < 0)
6787a674200SNicolas Dichtel 			goto done;
6797a674200SNicolas Dichtel 		else
6807a674200SNicolas Dichtel 			h++;
6817a674200SNicolas Dichtel 	}
6827a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
6837a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
6847a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
6857a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6867a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6877a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
688053c095aSJohannes Berg 					       -1) < 0)
6897a674200SNicolas Dichtel 			goto done;
6907a674200SNicolas Dichtel 		else
6917a674200SNicolas Dichtel 			h++;
6927a674200SNicolas Dichtel 	}
6937a674200SNicolas Dichtel done:
6947a674200SNicolas Dichtel 	cb->args[0] = h;
6957a674200SNicolas Dichtel 	cb->args[1] = idx;
6967a674200SNicolas Dichtel 
6977a674200SNicolas Dichtel 	return skb->len;
6987a674200SNicolas Dichtel }
6997a674200SNicolas Dichtel 
7001da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
7011da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	struct net_device *dev;
7041da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	if (!idev)
7071da177e4SLinus Torvalds 		return;
7081da177e4SLinus Torvalds 	dev = idev->dev;
7090187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
7100187bdfbSBen Hutchings 		dev_disable_lro(dev);
7111a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
7122c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
713f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
7142c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
7152c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
7162c5e8933SHannes Frederic Sowa 		} else {
717f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
7182c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
7192c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
7202c5e8933SHannes Frederic Sowa 		}
7211da177e4SLinus Torvalds 	}
722502a2ffdSstephen hemminger 
723502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
7242c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
7252c12a74cSMichal Wrobel 			continue;
7261da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
7271da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
7281da177e4SLinus Torvalds 		else
7291da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
7301da177e4SLinus Torvalds 	}
731f3a1bfb1SNicolas Dichtel 	inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
732f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 
736e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
7371da177e4SLinus Torvalds {
7381da177e4SLinus Torvalds 	struct net_device *dev;
7391da177e4SLinus Torvalds 	struct inet6_dev *idev;
7401da177e4SLinus Torvalds 
7414acd4945SBen Hutchings 	for_each_netdev(net, dev) {
7421da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
7431da177e4SLinus Torvalds 		if (idev) {
744e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
745e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
7461da177e4SLinus Torvalds 			if (changed)
7471da177e4SLinus Torvalds 				dev_forward_change(idev);
7481da177e4SLinus Torvalds 		}
7491da177e4SLinus Torvalds 	}
7501da177e4SLinus Torvalds }
751c8fecf22SPavel Emelyanov 
752013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
753c8fecf22SPavel Emelyanov {
754bff16c2fSPavel Emelyanov 	struct net *net;
755013d97e9SFrancesco Ruggeri 	int old;
756013d97e9SFrancesco Ruggeri 
757013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
758013d97e9SFrancesco Ruggeri 		return restart_syscall();
759bff16c2fSPavel Emelyanov 
760bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
761013d97e9SFrancesco Ruggeri 	old = *p;
762013d97e9SFrancesco Ruggeri 	*p = newf;
763c8fecf22SPavel Emelyanov 
764013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
765f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
766f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
767f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
768f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
769013d97e9SFrancesco Ruggeri 		rtnl_unlock();
770013d97e9SFrancesco Ruggeri 		return 0;
77188af182eSEric W. Biederman 	}
772b325fddbSStephen Hemminger 
773e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
774e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
775e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
776f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
777f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
778f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
779f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
780013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
781c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
7820187bdfbSBen Hutchings 	rtnl_unlock();
783c8fecf22SPavel Emelyanov 
784013d97e9SFrancesco Ruggeri 	if (newf)
7857b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
786b325fddbSStephen Hemminger 	return 1;
787c8fecf22SPavel Emelyanov }
78835103d11SAndy Gospodarek 
78935103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf)
79035103d11SAndy Gospodarek {
79135103d11SAndy Gospodarek 	struct net_device *dev;
79235103d11SAndy Gospodarek 	struct inet6_dev *idev;
79335103d11SAndy Gospodarek 
79435103d11SAndy Gospodarek 	for_each_netdev(net, dev) {
79535103d11SAndy Gospodarek 		idev = __in6_dev_get(dev);
79635103d11SAndy Gospodarek 		if (idev) {
79735103d11SAndy Gospodarek 			int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
79835103d11SAndy Gospodarek 
79935103d11SAndy Gospodarek 			idev->cnf.ignore_routes_with_linkdown = newf;
80035103d11SAndy Gospodarek 			if (changed)
80135103d11SAndy Gospodarek 				inet6_netconf_notify_devconf(dev_net(dev),
80235103d11SAndy Gospodarek 							     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
80335103d11SAndy Gospodarek 							     dev->ifindex,
80435103d11SAndy Gospodarek 							     &idev->cnf);
80535103d11SAndy Gospodarek 		}
80635103d11SAndy Gospodarek 	}
80735103d11SAndy Gospodarek }
80835103d11SAndy Gospodarek 
80935103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
81035103d11SAndy Gospodarek {
81135103d11SAndy Gospodarek 	struct net *net;
81235103d11SAndy Gospodarek 	int old;
81335103d11SAndy Gospodarek 
81435103d11SAndy Gospodarek 	if (!rtnl_trylock())
81535103d11SAndy Gospodarek 		return restart_syscall();
81635103d11SAndy Gospodarek 
81735103d11SAndy Gospodarek 	net = (struct net *)table->extra2;
81835103d11SAndy Gospodarek 	old = *p;
81935103d11SAndy Gospodarek 	*p = newf;
82035103d11SAndy Gospodarek 
82135103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
82235103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
82335103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
82435103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
82535103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_DEFAULT,
82635103d11SAndy Gospodarek 						     net->ipv6.devconf_dflt);
82735103d11SAndy Gospodarek 		rtnl_unlock();
82835103d11SAndy Gospodarek 		return 0;
82935103d11SAndy Gospodarek 	}
83035103d11SAndy Gospodarek 
83135103d11SAndy Gospodarek 	if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
83235103d11SAndy Gospodarek 		net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
83335103d11SAndy Gospodarek 		addrconf_linkdown_change(net, newf);
83435103d11SAndy Gospodarek 		if ((!newf) ^ (!old))
83535103d11SAndy Gospodarek 			inet6_netconf_notify_devconf(net,
83635103d11SAndy Gospodarek 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
83735103d11SAndy Gospodarek 						     NETCONFA_IFINDEX_ALL,
83835103d11SAndy Gospodarek 						     net->ipv6.devconf_all);
83935103d11SAndy Gospodarek 	}
84035103d11SAndy Gospodarek 	rtnl_unlock();
84135103d11SAndy Gospodarek 
84235103d11SAndy Gospodarek 	return 1;
84335103d11SAndy Gospodarek }
84435103d11SAndy Gospodarek 
8451da177e4SLinus Torvalds #endif
8461da177e4SLinus Torvalds 
8475c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
8481da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
8491da177e4SLinus Torvalds {
850c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
851547b792cSIlpo Järvinen 
8521da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
85391df42beSJoe Perches 	pr_debug("%s\n", __func__);
8541da177e4SLinus Torvalds #endif
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
8571da177e4SLinus Torvalds 
858c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
859c15b1ccaSHannes Frederic Sowa 		pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
860c15b1ccaSHannes Frederic Sowa 			  ifp);
8611da177e4SLinus Torvalds 
862e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
863f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
8641da177e4SLinus Torvalds 		return;
8651da177e4SLinus Torvalds 	}
86694e187c0SAmerigo Wang 	ip6_rt_put(ifp->rt);
8671da177e4SLinus Torvalds 
868e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
871e55ffac6SBrian Haley static void
872e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
873e55ffac6SBrian Haley {
874502a2ffdSstephen hemminger 	struct list_head *p;
8758a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
876e55ffac6SBrian Haley 
877e55ffac6SBrian Haley 	/*
878e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
879e55ffac6SBrian Haley 	 * global before linklocal.
880e55ffac6SBrian Haley 	 */
881502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
882502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
883502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
8848a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
885e55ffac6SBrian Haley 			break;
886e55ffac6SBrian Haley 	}
887e55ffac6SBrian Haley 
888b54c9b98SDavid S. Miller 	list_add_tail(&ifp->if_list, p);
889e55ffac6SBrian Haley }
890e55ffac6SBrian Haley 
891ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr)
8923eb84f49SYOSHIFUJI Hideaki {
893ddbe5032SEric Dumazet 	return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
8943eb84f49SYOSHIFUJI Hideaki }
8953eb84f49SYOSHIFUJI Hideaki 
8961da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds static struct inet6_ifaddr *
8993f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
9003f8f5298SJiri Pirko 	      const struct in6_addr *peer_addr, int pfxlen,
9018a226b2cSJiri Benc 	      int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
9041da177e4SLinus Torvalds 	struct rt6_info *rt;
9053a88a81dSstephen hemminger 	unsigned int hash;
9061da177e4SLinus Torvalds 	int err = 0;
907d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
908d68b8270SYOSHIFUJI Hideaki 
909d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
910d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
911d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
912d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
913d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
9141da177e4SLinus Torvalds 
9158814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
9161da177e4SLinus Torvalds 	if (idev->dead) {
9171da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
9181da177e4SLinus Torvalds 		goto out2;
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 
92156d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
9229bdd8d40SBrian Haley 		err = -EACCES;
9239bdd8d40SBrian Haley 		goto out2;
9249bdd8d40SBrian Haley 	}
9259bdd8d40SBrian Haley 
9265c578aedSstephen hemminger 	spin_lock(&addrconf_hash_lock);
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
929c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
930ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: already assigned\n");
9311da177e4SLinus Torvalds 		err = -EEXIST;
9321da177e4SLinus Torvalds 		goto out;
9331da177e4SLinus Torvalds 	}
9341da177e4SLinus Torvalds 
935322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
9361da177e4SLinus Torvalds 
93763159f29SIan Morris 	if (!ifa) {
938ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: malloc failed\n");
9391da177e4SLinus Torvalds 		err = -ENOBUFS;
9401da177e4SLinus Torvalds 		goto out;
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds 
9438f031519SDavid S. Miller 	rt = addrconf_dst_alloc(idev, addr, false);
9441da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
9451da177e4SLinus Torvalds 		err = PTR_ERR(rt);
9461da177e4SLinus Torvalds 		goto out;
9471da177e4SLinus Torvalds 	}
9481da177e4SLinus Torvalds 
949bba24896SJiri Pirko 	neigh_parms_data_state_setall(idev->nd_parms);
950bba24896SJiri Pirko 
9514e3fd7a0SAlexey Dobriyan 	ifa->addr = *addr;
9523f8f5298SJiri Pirko 	if (peer_addr)
9533f8f5298SJiri Pirko 		ifa->peer_addr = *peer_addr;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
956c15b1ccaSHannes Frederic Sowa 	INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
957c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
9581da177e4SLinus Torvalds 	ifa->scope = scope;
9591da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
9601da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
9618a226b2cSJiri Benc 	ifa->valid_lft = valid_lft;
9628a226b2cSJiri Benc 	ifa->prefered_lft = prefered_lft;
9631da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
964617fe29dSDaniel Borkmann 	ifa->tokenized = false;
9651da177e4SLinus Torvalds 
96657f5f544SKeir Fraser 	ifa->rt = rt;
96757f5f544SKeir Fraser 
9681da177e4SLinus Torvalds 	ifa->idev = idev;
9691da177e4SLinus Torvalds 	in6_dev_hold(idev);
9701da177e4SLinus Torvalds 	/* For caller */
9711da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	/* Add to big hash table */
974ddbe5032SEric Dumazet 	hash = inet6_addr_hash(addr);
9751da177e4SLinus Torvalds 
9765c578aedSstephen hemminger 	hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
9775c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	write_lock(&idev->lock);
9801da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
981e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
984372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
9851da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
9861da177e4SLinus Torvalds 	}
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
9891da177e4SLinus Torvalds 	write_unlock(&idev->lock);
9901da177e4SLinus Torvalds out2:
9918814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
9921da177e4SLinus Torvalds 
993fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
994f88c91ddSCong Wang 		inet6addr_notifier_call_chain(NETDEV_UP, ifa);
9951da177e4SLinus Torvalds 	else {
9961da177e4SLinus Torvalds 		kfree(ifa);
9971da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
9981da177e4SLinus Torvalds 	}
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 	return ifa;
10011da177e4SLinus Torvalds out:
10025c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
10031da177e4SLinus Torvalds 	goto out2;
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
10065b84efecSThomas Haller enum cleanup_prefix_rt_t {
10075b84efecSThomas Haller 	CLEANUP_PREFIX_RT_NOP,    /* no cleanup action for prefix route */
10085b84efecSThomas Haller 	CLEANUP_PREFIX_RT_DEL,    /* delete the prefix route */
10095b84efecSThomas Haller 	CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
10105b84efecSThomas Haller };
10115b84efecSThomas Haller 
10125b84efecSThomas Haller /*
10135b84efecSThomas Haller  * Check, whether the prefix for ifp would still need a prefix route
10145b84efecSThomas Haller  * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
10155b84efecSThomas Haller  * constants.
10165b84efecSThomas Haller  *
10175b84efecSThomas Haller  * 1) we don't purge prefix if address was not permanent.
10185b84efecSThomas Haller  *    prefix is managed by its own lifetime.
10195b84efecSThomas Haller  * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
10205b84efecSThomas Haller  * 3) if there are no addresses, delete prefix.
10215b84efecSThomas Haller  * 4) if there are still other permanent address(es),
10225b84efecSThomas Haller  *    corresponding prefix is still permanent.
10235b84efecSThomas Haller  * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
10245b84efecSThomas Haller  *    don't purge the prefix, assume user space is managing it.
10255b84efecSThomas Haller  * 6) otherwise, update prefix lifetime to the
10265b84efecSThomas Haller  *    longest valid lifetime among the corresponding
10275b84efecSThomas Haller  *    addresses on the device.
10285b84efecSThomas Haller  *    Note: subsequent RA will update lifetime.
10295b84efecSThomas Haller  **/
10305b84efecSThomas Haller static enum cleanup_prefix_rt_t
10315b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
10325b84efecSThomas Haller {
10335b84efecSThomas Haller 	struct inet6_ifaddr *ifa;
10345b84efecSThomas Haller 	struct inet6_dev *idev = ifp->idev;
10355b84efecSThomas Haller 	unsigned long lifetime;
10365b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
10375b84efecSThomas Haller 
10385b84efecSThomas Haller 	*expires = jiffies;
10395b84efecSThomas Haller 
10405b84efecSThomas Haller 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
10415b84efecSThomas Haller 		if (ifa == ifp)
10425b84efecSThomas Haller 			continue;
10435b84efecSThomas Haller 		if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
10445b84efecSThomas Haller 				       ifp->prefix_len))
10455b84efecSThomas Haller 			continue;
10465b84efecSThomas Haller 		if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
10475b84efecSThomas Haller 			return CLEANUP_PREFIX_RT_NOP;
10485b84efecSThomas Haller 
10495b84efecSThomas Haller 		action = CLEANUP_PREFIX_RT_EXPIRE;
10505b84efecSThomas Haller 
10515b84efecSThomas Haller 		spin_lock(&ifa->lock);
10525b84efecSThomas Haller 
10535b84efecSThomas Haller 		lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
10545b84efecSThomas Haller 		/*
10555b84efecSThomas Haller 		 * Note: Because this address is
10565b84efecSThomas Haller 		 * not permanent, lifetime <
10575b84efecSThomas Haller 		 * LONG_MAX / HZ here.
10585b84efecSThomas Haller 		 */
10595b84efecSThomas Haller 		if (time_before(*expires, ifa->tstamp + lifetime * HZ))
10605b84efecSThomas Haller 			*expires = ifa->tstamp + lifetime * HZ;
10615b84efecSThomas Haller 		spin_unlock(&ifa->lock);
10625b84efecSThomas Haller 	}
10635b84efecSThomas Haller 
10645b84efecSThomas Haller 	return action;
10655b84efecSThomas Haller }
10665b84efecSThomas Haller 
10675b84efecSThomas Haller static void
10685b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
10695b84efecSThomas Haller {
10705b84efecSThomas Haller 	struct rt6_info *rt;
10715b84efecSThomas Haller 
10725b84efecSThomas Haller 	rt = addrconf_get_prefix_route(&ifp->addr,
10735b84efecSThomas Haller 				       ifp->prefix_len,
10745b84efecSThomas Haller 				       ifp->idev->dev,
10755b84efecSThomas Haller 				       0, RTF_GATEWAY | RTF_DEFAULT);
10765b84efecSThomas Haller 	if (rt) {
10775b84efecSThomas Haller 		if (del_rt)
10785b84efecSThomas Haller 			ip6_del_rt(rt);
10795b84efecSThomas Haller 		else {
10805b84efecSThomas Haller 			if (!(rt->rt6i_flags & RTF_EXPIRES))
10815b84efecSThomas Haller 				rt6_set_expires(rt, expires);
10825b84efecSThomas Haller 			ip6_rt_put(rt);
10835b84efecSThomas Haller 		}
10845b84efecSThomas Haller 	}
10855b84efecSThomas Haller }
10865b84efecSThomas Haller 
10875b84efecSThomas Haller 
10881da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
10911da177e4SLinus Torvalds {
10924c5ff6a6SHerbert Xu 	int state;
10935b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
10945b84efecSThomas Haller 	unsigned long expires;
10951da177e4SLinus Torvalds 
1096c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
1097c15b1ccaSHannes Frederic Sowa 
10988e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
10994c5ff6a6SHerbert Xu 	state = ifp->state;
1100e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
11018e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
11024c5ff6a6SHerbert Xu 
11034c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
11044c5ff6a6SHerbert Xu 		goto out;
11051da177e4SLinus Torvalds 
11065c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
11075c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
11085c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
11091da177e4SLinus Torvalds 
11105b84efecSThomas Haller 	write_lock_bh(&ifp->idev->lock);
11115d9efa7eSDavid S. Miller 
11121da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
1113372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
11141da177e4SLinus Torvalds 		if (ifp->ifpub) {
11151da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
11161da177e4SLinus Torvalds 			ifp->ifpub = NULL;
11171da177e4SLinus Torvalds 		}
11181da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
11191da177e4SLinus Torvalds 	}
11201da177e4SLinus Torvalds 
11215b84efecSThomas Haller 	if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
11225b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &expires);
11235b84efecSThomas Haller 
1124502a2ffdSstephen hemminger 	list_del_init(&ifp->if_list);
11251da177e4SLinus Torvalds 	__in6_ifa_put(ifp);
1126502a2ffdSstephen hemminger 
11275b84efecSThomas Haller 	write_unlock_bh(&ifp->idev->lock);
11281da177e4SLinus Torvalds 
1129c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
1130b2238566SAndrey Vagin 
11311da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
11321da177e4SLinus Torvalds 
1133f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
11341da177e4SLinus Torvalds 
11355b84efecSThomas Haller 	if (action != CLEANUP_PREFIX_RT_NOP) {
11365b84efecSThomas Haller 		cleanup_prefix_route(ifp, expires,
11375b84efecSThomas Haller 			action == CLEANUP_PREFIX_RT_DEL);
11381da177e4SLinus Torvalds 	}
11391da177e4SLinus Torvalds 
1140c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1141c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
11424c5ff6a6SHerbert Xu out:
11431da177e4SLinus Torvalds 	in6_ifa_put(ifp);
11441da177e4SLinus Torvalds }
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
11471da177e4SLinus Torvalds {
11481da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
11491da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
115076f793e3SLorenzo Colitti 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1151eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
11521da177e4SLinus Torvalds 	int tmp_plen;
11531da177e4SLinus Torvalds 	int ret = 0;
115495c385b4SNeil Horman 	u32 addr_flags;
115576f793e3SLorenzo Colitti 	unsigned long now = jiffies;
11561da177e4SLinus Torvalds 
115753bd6749SJiri Pirko 	write_lock_bh(&idev->lock);
11581da177e4SLinus Torvalds 	if (ift) {
11591da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
11601da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
11611da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
11621da177e4SLinus Torvalds 		tmpaddr = &addr;
11631da177e4SLinus Torvalds 	} else {
11641da177e4SLinus Torvalds 		tmpaddr = NULL;
11651da177e4SLinus Torvalds 	}
11661da177e4SLinus Torvalds retry:
11671da177e4SLinus Torvalds 	in6_dev_hold(idev);
11681da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
116953bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1170f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
11711da177e4SLinus Torvalds 		in6_dev_put(idev);
11721da177e4SLinus Torvalds 		ret = -1;
11731da177e4SLinus Torvalds 		goto out;
11741da177e4SLinus Torvalds 	}
11751da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
11761da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
11771da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
11781da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
117953bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1180f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1181f3213831SJoe Perches 			__func__);
11821da177e4SLinus Torvalds 		in6_dev_put(idev);
11831da177e4SLinus Torvalds 		ret = -1;
11841da177e4SLinus Torvalds 		goto out;
11851da177e4SLinus Torvalds 	}
11861da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
11871da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1188eb7e0575SSorin Dumitru 	__ipv6_try_regen_rndid(idev, tmpaddr);
11891da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
119076f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
11911da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
11921da177e4SLinus Torvalds 			      ifp->valid_lft,
11937a876b0eSGlenn Wurster 			      idev->cnf.temp_valid_lft + age);
11941da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
11951da177e4SLinus Torvalds 				 ifp->prefered_lft,
11967a876b0eSGlenn Wurster 				 idev->cnf.temp_prefered_lft + age -
1197784e2710SBen Hutchings 				 idev->cnf.max_desync_factor);
11981da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
11991da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
12001da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
12011da177e4SLinus Torvalds 
1202eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1203eac55bf9SBenoit Boissinot 			idev->cnf.dad_transmits *
12041f9248e5SJiri Pirko 			NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
120553bd6749SJiri Pirko 	write_unlock_bh(&idev->lock);
120695c385b4SNeil Horman 
1207eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1208eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1209eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1210eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1211ecab6701SHeiner Kallweit 	 * Use age calculation as in addrconf_verify to avoid unnecessary
1212ecab6701SHeiner Kallweit 	 * temporary addresses being generated.
1213eac55bf9SBenoit Boissinot 	 */
1214ecab6701SHeiner Kallweit 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1215ecab6701SHeiner Kallweit 	if (tmp_prefered_lft <= regen_advance + age) {
1216eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1217eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1218eac55bf9SBenoit Boissinot 		ret = -1;
1219eac55bf9SBenoit Boissinot 		goto out;
1220eac55bf9SBenoit Boissinot 	}
1221eac55bf9SBenoit Boissinot 
122295c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
122395c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
122495c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
122595c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
122695c385b4SNeil Horman 
12274b08a8f1SHannes Frederic Sowa 	ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
12288a226b2cSJiri Benc 			    ipv6_addr_scope(&addr), addr_flags,
12294b08a8f1SHannes Frederic Sowa 			    tmp_valid_lft, tmp_prefered_lft);
12304b08a8f1SHannes Frederic Sowa 	if (IS_ERR(ift)) {
12311da177e4SLinus Torvalds 		in6_ifa_put(ifp);
12321da177e4SLinus Torvalds 		in6_dev_put(idev);
1233f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
12341da177e4SLinus Torvalds 		tmpaddr = &addr;
123553bd6749SJiri Pirko 		write_lock_bh(&idev->lock);
12361da177e4SLinus Torvalds 		goto retry;
12371da177e4SLinus Torvalds 	}
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
12401da177e4SLinus Torvalds 	ift->ifpub = ifp;
124176f793e3SLorenzo Colitti 	ift->cstamp = now;
12421da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
12431da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
12441da177e4SLinus Torvalds 
1245cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
12461da177e4SLinus Torvalds 	in6_ifa_put(ift);
12471da177e4SLinus Torvalds 	in6_dev_put(idev);
12481da177e4SLinus Torvalds out:
12491da177e4SLinus Torvalds 	return ret;
12501da177e4SLinus Torvalds }
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds /*
1253072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
12541da177e4SLinus Torvalds  */
1255a9b05723SYOSHIFUJI Hideaki enum {
1256a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1257a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1258a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1259a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1260a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1261a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1262a9b05723SYOSHIFUJI Hideaki #endif
1263a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1264a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1265a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1266a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1267a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
12687fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
12697fd2561eSErik Kline 	IPV6_SADDR_RULE_NOT_OPTIMISTIC,
12707fd2561eSErik Kline #endif
1271a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1272072047e4SYOSHIFUJI Hideaki };
1273072047e4SYOSHIFUJI Hideaki 
1274a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1275a9b05723SYOSHIFUJI Hideaki 	int			rule;
1276a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1277a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1278a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1279a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1280a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1281a9b05723SYOSHIFUJI Hideaki };
1282a9b05723SYOSHIFUJI Hideaki 
1283a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
12849acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1285a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1286a9b05723SYOSHIFUJI Hideaki 	int scope;
1287a9b05723SYOSHIFUJI Hideaki 	int label;
12887cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1289a9b05723SYOSHIFUJI Hideaki };
1290072047e4SYOSHIFUJI Hideaki 
1291b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
12921da177e4SLinus Torvalds {
129345bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1294072047e4SYOSHIFUJI Hideaki 		return 1;
1295072047e4SYOSHIFUJI Hideaki 	return 0;
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
12987fd2561eSErik Kline static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
12997fd2561eSErik Kline {
13007fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
13017fd2561eSErik Kline 	return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
13027fd2561eSErik Kline #else
13037fd2561eSErik Kline 	return false;
13047fd2561eSErik Kline #endif
13057fd2561eSErik Kline }
13067fd2561eSErik Kline 
13073de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
13083de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1309a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1310a9b05723SYOSHIFUJI Hideaki 			       int i)
1311a9b05723SYOSHIFUJI Hideaki {
1312a9b05723SYOSHIFUJI Hideaki 	int ret;
1313a9b05723SYOSHIFUJI Hideaki 
1314a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1315a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1316a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1317a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1318a9b05723SYOSHIFUJI Hideaki 			break;
1319a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1320a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1321a9b05723SYOSHIFUJI Hideaki 			break;
1322a9b05723SYOSHIFUJI Hideaki 		default:
1323a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1324a9b05723SYOSHIFUJI Hideaki 		}
1325a9b05723SYOSHIFUJI Hideaki 		goto out;
1326a9b05723SYOSHIFUJI Hideaki 	}
1327a9b05723SYOSHIFUJI Hideaki 
1328a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1329a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1330a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1331a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1332a9b05723SYOSHIFUJI Hideaki 		break;
1333a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1334a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1335a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1336a9b05723SYOSHIFUJI Hideaki 		break;
1337a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1338a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1339a9b05723SYOSHIFUJI Hideaki 		 *
1340a9b05723SYOSHIFUJI Hideaki 		 *      ret
1341a9b05723SYOSHIFUJI Hideaki 		 *       ^
1342a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1343a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1344a9b05723SYOSHIFUJI Hideaki 		 *       |
1345a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1346a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1347a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1348db9c7c39Sstephen hemminger 		 *  B-15 |    \        if scope is enough for destination.
1349a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1350a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1351a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1352a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1353a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1354a9b05723SYOSHIFUJI Hideaki 		 *
1355a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1356a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1357a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1358a9b05723SYOSHIFUJI Hideaki 		 */
1359a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1360a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1361a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1362a9b05723SYOSHIFUJI Hideaki 		else
1363a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1364a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1365a9b05723SYOSHIFUJI Hideaki 		break;
1366a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
13677fd2561eSErik Kline 	    {
1368a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
13697fd2561eSErik Kline 		u8 avoid = IFA_F_DEPRECATED;
13707fd2561eSErik Kline 
13717fd2561eSErik Kline 		if (!ipv6_use_optimistic_addr(score->ifa->idev))
13727fd2561eSErik Kline 			avoid |= IFA_F_OPTIMISTIC;
1373a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
13747fd2561eSErik Kline 		      !(score->ifa->flags & avoid);
1375a9b05723SYOSHIFUJI Hideaki 		break;
13767fd2561eSErik Kline 	    }
1377a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1378a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
13797cbca67cSYOSHIFUJI Hideaki 	    {
1380a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
13817cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
13827cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1383a9b05723SYOSHIFUJI Hideaki 		break;
13847cbca67cSYOSHIFUJI Hideaki 	    }
1385a9b05723SYOSHIFUJI Hideaki #endif
1386a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1387a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1388a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1389a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1390a9b05723SYOSHIFUJI Hideaki 		break;
1391a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1392a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
13933de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
13943de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1395a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1396a9b05723SYOSHIFUJI Hideaki 		break;
1397a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
13987cbca67cSYOSHIFUJI Hideaki 	    {
1399a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
140025985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1401a9b05723SYOSHIFUJI Hideaki 		 */
14027cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
14037cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
14047cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
14057cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1406a9b05723SYOSHIFUJI Hideaki 		break;
14077cbca67cSYOSHIFUJI Hideaki 	    }
1408a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1409a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1410a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1411a9b05723SYOSHIFUJI Hideaki 		 */
1412a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1413a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1414a9b05723SYOSHIFUJI Hideaki 		break;
1415a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1416a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
141791b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
141891b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
141991b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
142091b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1421a9b05723SYOSHIFUJI Hideaki 		break;
14227fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
14237fd2561eSErik Kline 	case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
14247fd2561eSErik Kline 		/* Optimistic addresses still have lower precedence than other
14257fd2561eSErik Kline 		 * preferred addresses.
14267fd2561eSErik Kline 		 */
14277fd2561eSErik Kline 		ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
14287fd2561eSErik Kline 		break;
14297fd2561eSErik Kline #endif
1430a9b05723SYOSHIFUJI Hideaki 	default:
1431a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1432a9b05723SYOSHIFUJI Hideaki 	}
1433a9b05723SYOSHIFUJI Hideaki 
1434a9b05723SYOSHIFUJI Hideaki 	if (ret)
1435a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1436a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1437a9b05723SYOSHIFUJI Hideaki out:
1438a9b05723SYOSHIFUJI Hideaki 	return ret;
1439a9b05723SYOSHIFUJI Hideaki }
1440a9b05723SYOSHIFUJI Hideaki 
1441c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net,
14429131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_dst *dst,
14439131f3deSYOSHIFUJI Hideaki/吉藤英明 				struct inet6_dev *idev,
1444c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				struct ipv6_saddr_score *scores,
1445c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				int hiscore_idx)
14461da177e4SLinus Torvalds {
1447c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1448072047e4SYOSHIFUJI Hideaki 
14491da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1450502a2ffdSstephen hemminger 	list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1451a9b05723SYOSHIFUJI Hideaki 		int i;
1452072047e4SYOSHIFUJI Hideaki 
1453a9b05723SYOSHIFUJI Hideaki 		/*
14546b3ae80aSYOSHIFUJI Hideaki 		 * - Tentative Address (RFC2462 section 5.4)
14556b3ae80aSYOSHIFUJI Hideaki 		 *  - A tentative address is not considered
14566b3ae80aSYOSHIFUJI Hideaki 		 *    "assigned to an interface" in the traditional
145795c385b4SNeil Horman 		 *    sense, unless it is also flagged as optimistic.
14586b3ae80aSYOSHIFUJI Hideaki 		 * - Candidate Source Address (section 4)
1459072047e4SYOSHIFUJI Hideaki 		 *  - In any case, anycast addresses, multicast
1460072047e4SYOSHIFUJI Hideaki 		 *    addresses, and the unspecified address MUST
1461072047e4SYOSHIFUJI Hideaki 		 *    NOT be included in a candidate set.
1462072047e4SYOSHIFUJI Hideaki 		 */
1463a9b05723SYOSHIFUJI Hideaki 		if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1464a9b05723SYOSHIFUJI Hideaki 		    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
14656b3ae80aSYOSHIFUJI Hideaki 			continue;
1466a9b05723SYOSHIFUJI Hideaki 
1467a9b05723SYOSHIFUJI Hideaki 		score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1468a9b05723SYOSHIFUJI Hideaki 
1469a9b05723SYOSHIFUJI Hideaki 		if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1470a9b05723SYOSHIFUJI Hideaki 			     score->addr_type & IPV6_ADDR_MULTICAST)) {
1471ba7a46f1SJoe Perches 			net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
14729131f3deSYOSHIFUJI Hideaki/吉藤英明 					    idev->dev->name);
14731da177e4SLinus Torvalds 			continue;
1474072047e4SYOSHIFUJI Hideaki 		}
1475072047e4SYOSHIFUJI Hideaki 
1476a9b05723SYOSHIFUJI Hideaki 		score->rule = -1;
1477a9b05723SYOSHIFUJI Hideaki 		bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1478072047e4SYOSHIFUJI Hideaki 
1479a9b05723SYOSHIFUJI Hideaki 		for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1480a9b05723SYOSHIFUJI Hideaki 			int minihiscore, miniscore;
1481072047e4SYOSHIFUJI Hideaki 
14829131f3deSYOSHIFUJI Hideaki/吉藤英明 			minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
14839131f3deSYOSHIFUJI Hideaki/吉藤英明 			miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1484072047e4SYOSHIFUJI Hideaki 
1485a9b05723SYOSHIFUJI Hideaki 			if (minihiscore > miniscore) {
1486a9b05723SYOSHIFUJI Hideaki 				if (i == IPV6_SADDR_RULE_SCOPE &&
1487a9b05723SYOSHIFUJI Hideaki 				    score->scopedist > 0) {
1488a9b05723SYOSHIFUJI Hideaki 					/*
1489a9b05723SYOSHIFUJI Hideaki 					 * special case:
1490a9b05723SYOSHIFUJI Hideaki 					 * each remaining entry
1491a9b05723SYOSHIFUJI Hideaki 					 * has too small (not enough)
1492a9b05723SYOSHIFUJI Hideaki 					 * scope, because ifa entries
1493a9b05723SYOSHIFUJI Hideaki 					 * are sorted by their scope
1494a9b05723SYOSHIFUJI Hideaki 					 * values.
1495072047e4SYOSHIFUJI Hideaki 					 */
14969131f3deSYOSHIFUJI Hideaki/吉藤英明 					goto out;
1497072047e4SYOSHIFUJI Hideaki 				}
1498a9b05723SYOSHIFUJI Hideaki 				break;
1499a9b05723SYOSHIFUJI Hideaki 			} else if (minihiscore < miniscore) {
1500a9b05723SYOSHIFUJI Hideaki 				if (hiscore->ifa)
1501a9b05723SYOSHIFUJI Hideaki 					in6_ifa_put(hiscore->ifa);
150299cd07a5SJuha-Matti Tapio 
1503a9b05723SYOSHIFUJI Hideaki 				in6_ifa_hold(score->ifa);
1504072047e4SYOSHIFUJI Hideaki 
1505a0bffffcSIlpo Järvinen 				swap(hiscore, score);
1506c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 				hiscore_idx = 1 - hiscore_idx;
1507a9b05723SYOSHIFUJI Hideaki 
1508a9b05723SYOSHIFUJI Hideaki 				/* restore our iterator */
1509a9b05723SYOSHIFUJI Hideaki 				score->ifa = hiscore->ifa;
1510a9b05723SYOSHIFUJI Hideaki 
1511a9b05723SYOSHIFUJI Hideaki 				break;
15121da177e4SLinus Torvalds 			}
1513a9b05723SYOSHIFUJI Hideaki 		}
1514a9b05723SYOSHIFUJI Hideaki 	}
15159131f3deSYOSHIFUJI Hideaki/吉藤英明 out:
15161da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
1517c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	return hiscore_idx;
15181da177e4SLinus Torvalds }
15199131f3deSYOSHIFUJI Hideaki/吉藤英明 
15209131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
15219131f3deSYOSHIFUJI Hideaki/吉藤英明 		       const struct in6_addr *daddr, unsigned int prefs,
15229131f3deSYOSHIFUJI Hideaki/吉藤英明 		       struct in6_addr *saddr)
15239131f3deSYOSHIFUJI Hideaki/吉藤英明 {
1524c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_score scores[2], *hiscore;
15259131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct ipv6_saddr_dst dst;
15269131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct inet6_dev *idev;
15279131f3deSYOSHIFUJI Hideaki/吉藤英明 	struct net_device *dev;
15289131f3deSYOSHIFUJI Hideaki/吉藤英明 	int dst_type;
15299131f3deSYOSHIFUJI Hideaki/吉藤英明 	bool use_oif_addr = false;
1530c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	int hiscore_idx = 0;
15319131f3deSYOSHIFUJI Hideaki/吉藤英明 
15329131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst_type = __ipv6_addr_type(daddr);
15339131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.addr = daddr;
15349131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
15359131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.scope = __ipv6_addr_src_scope(dst_type);
15369131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
15379131f3deSYOSHIFUJI Hideaki/吉藤英明 	dst.prefs = prefs;
15389131f3deSYOSHIFUJI Hideaki/吉藤英明 
1539c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].rule = -1;
1540c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	scores[hiscore_idx].ifa = NULL;
15419131f3deSYOSHIFUJI Hideaki/吉藤英明 
15429131f3deSYOSHIFUJI Hideaki/吉藤英明 	rcu_read_lock();
15439131f3deSYOSHIFUJI Hideaki/吉藤英明 
15449131f3deSYOSHIFUJI Hideaki/吉藤英明 	/* Candidate Source Address (section 4)
15459131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *  - multicast and link-local destination address,
15469131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    the set of candidate source address MUST only
15479131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
15489131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same link as the outgoing
15499131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.
15509131f3deSYOSHIFUJI Hideaki/吉藤英明 	 * (- For site-local destination addresses, the
15519131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    set of candidate source addresses MUST only
15529131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    include addresses assigned to interfaces
15539131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    belonging to the same site as the outgoing
15549131f3deSYOSHIFUJI Hideaki/吉藤英明 	 *    interface.)
15553985e8a3SErik Kline 	 *  - "It is RECOMMENDED that the candidate source addresses
15563985e8a3SErik Kline 	 *    be the set of unicast addresses assigned to the
15573985e8a3SErik Kline 	 *    interface that will be used to send to the destination
15583985e8a3SErik Kline 	 *    (the 'outgoing' interface)." (RFC 6724)
15599131f3deSYOSHIFUJI Hideaki/吉藤英明 	 */
15609131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (dst_dev) {
15619131f3deSYOSHIFUJI Hideaki/吉藤英明 		idev = __in6_dev_get(dst_dev);
15623985e8a3SErik Kline 		if ((dst_type & IPV6_ADDR_MULTICAST) ||
15633985e8a3SErik Kline 		    dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
15643985e8a3SErik Kline 		    (idev && idev->cnf.use_oif_addrs_only)) {
15659131f3deSYOSHIFUJI Hideaki/吉藤英明 			use_oif_addr = true;
15669131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
15679131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
15689131f3deSYOSHIFUJI Hideaki/吉藤英明 
15699131f3deSYOSHIFUJI Hideaki/吉藤英明 	if (use_oif_addr) {
1570c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 		if (idev)
1571c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
15729131f3deSYOSHIFUJI Hideaki/吉藤英明 	} else {
15739131f3deSYOSHIFUJI Hideaki/吉藤英明 		for_each_netdev_rcu(net, dev) {
15749131f3deSYOSHIFUJI Hideaki/吉藤英明 			idev = __in6_dev_get(dev);
15759131f3deSYOSHIFUJI Hideaki/吉藤英明 			if (!idev)
15769131f3deSYOSHIFUJI Hideaki/吉藤英明 				continue;
1577c15df306SYOSHIFUJI Hideaki 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
15789131f3deSYOSHIFUJI Hideaki/吉藤英明 		}
15799131f3deSYOSHIFUJI Hideaki/吉藤英明 	}
15808814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
15811da177e4SLinus Torvalds 
1582c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 	hiscore = &scores[hiscore_idx];
1583a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1584072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
15851da177e4SLinus Torvalds 
15864e3fd7a0SAlexey Dobriyan 	*saddr = hiscore->ifa->addr;
1587a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1588072047e4SYOSHIFUJI Hideaki 	return 0;
15891da177e4SLinus Torvalds }
15905e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
15911da177e4SLinus Torvalds 
15928965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1593479840ffSJiri Pirko 		      u32 banned_flags)
1594b7b1bfceSHannes Frederic Sowa {
1595b7b1bfceSHannes Frederic Sowa 	struct inet6_ifaddr *ifp;
1596b7b1bfceSHannes Frederic Sowa 	int err = -EADDRNOTAVAIL;
1597b7b1bfceSHannes Frederic Sowa 
1598602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1599602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1600602582caSHannes Frederic Sowa 			break;
1601b7b1bfceSHannes Frederic Sowa 		if (ifp->scope == IFA_LINK &&
1602b7b1bfceSHannes Frederic Sowa 		    !(ifp->flags & banned_flags)) {
1603b7b1bfceSHannes Frederic Sowa 			*addr = ifp->addr;
1604b7b1bfceSHannes Frederic Sowa 			err = 0;
1605b7b1bfceSHannes Frederic Sowa 			break;
1606b7b1bfceSHannes Frederic Sowa 		}
1607b7b1bfceSHannes Frederic Sowa 	}
1608b7b1bfceSHannes Frederic Sowa 	return err;
1609b7b1bfceSHannes Frederic Sowa }
1610b7b1bfceSHannes Frederic Sowa 
161195c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1612479840ffSJiri Pirko 		    u32 banned_flags)
16131da177e4SLinus Torvalds {
16141da177e4SLinus Torvalds 	struct inet6_dev *idev;
16151da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
16161da177e4SLinus Torvalds 
16178814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1618e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1619e21e8467SStephen Hemminger 	if (idev) {
16201da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1621b7b1bfceSHannes Frederic Sowa 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
16221da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
16231da177e4SLinus Torvalds 	}
16248814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
16251da177e4SLinus Torvalds 	return err;
16261da177e4SLinus Torvalds }
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
16291da177e4SLinus Torvalds {
16301da177e4SLinus Torvalds 	int cnt = 0;
16311da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1634502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list)
16351da177e4SLinus Torvalds 		cnt++;
16361da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
16371da177e4SLinus Torvalds 	return cnt;
16381da177e4SLinus Torvalds }
16391da177e4SLinus Torvalds 
1640b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
16412a7851bfSFlorian Westphal 		  const struct net_device *dev, int strict)
16421da177e4SLinus Torvalds {
1643c58da4c6SErik Kline 	return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1644c58da4c6SErik Kline }
1645c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr);
1646c58da4c6SErik Kline 
1647c58da4c6SErik Kline int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1648c58da4c6SErik Kline 			    const struct net_device *dev, int strict,
1649c58da4c6SErik Kline 			    u32 banned_flags)
1650c58da4c6SErik Kline {
1651eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1652ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
1653c58da4c6SErik Kline 	u32 ifp_flags;
16541da177e4SLinus Torvalds 
16555c578aedSstephen hemminger 	rcu_read_lock_bh();
1656b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1657878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1658bfeade08SDaniel Lezcano 			continue;
1659c58da4c6SErik Kline 		/* Decouple optimistic from tentative for evaluation here.
1660c58da4c6SErik Kline 		 * Ban optimistic addresses explicitly, when required.
1661c58da4c6SErik Kline 		 */
1662c58da4c6SErik Kline 		ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1663c58da4c6SErik Kline 			    ? (ifp->flags&~IFA_F_TENTATIVE)
1664c58da4c6SErik Kline 			    : ifp->flags;
16651da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1666c58da4c6SErik Kline 		    !(ifp_flags&banned_flags) &&
166763159f29SIan Morris 		    (!dev || ifp->idev->dev == dev ||
1668eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
16695c578aedSstephen hemminger 			rcu_read_unlock_bh();
1670eedf042aSStephen Hemminger 			return 1;
1671eedf042aSStephen Hemminger 		}
1672eedf042aSStephen Hemminger 	}
16735c578aedSstephen hemminger 
1674eedf042aSStephen Hemminger 	rcu_read_unlock_bh();
1675eedf042aSStephen Hemminger 	return 0;
16761da177e4SLinus Torvalds }
1677c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
16787159039aSYOSHIFUJI Hideaki 
16793e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
168006bfe655SDaniel Lezcano 			       struct net_device *dev)
16811da177e4SLinus Torvalds {
1682ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
16831da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
16841da177e4SLinus Torvalds 
1685b67bfe0dSSasha Levin 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1686878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
168706bfe655SDaniel Lezcano 			continue;
16881da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
168963159f29SIan Morris 			if (!dev || ifp->idev->dev == dev)
16903e81c6daSDavid S. Miller 				return true;
16911da177e4SLinus Torvalds 		}
16921da177e4SLinus Torvalds 	}
16933e81c6daSDavid S. Miller 	return false;
16941da177e4SLinus Torvalds }
16951da177e4SLinus Torvalds 
16967df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev.
16977df37ff3SCatalin\(ux\) M. BOIE  * If one is found it returns true.
16987df37ff3SCatalin\(ux\) M. BOIE  */
16997df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
17007df37ff3SCatalin\(ux\) M. BOIE 	const unsigned int prefix_len, struct net_device *dev)
17017df37ff3SCatalin\(ux\) M. BOIE {
17027df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_dev *idev;
17037df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_ifaddr *ifa;
17047df37ff3SCatalin\(ux\) M. BOIE 	bool ret = false;
17057df37ff3SCatalin\(ux\) M. BOIE 
17067df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_lock();
17077df37ff3SCatalin\(ux\) M. BOIE 	idev = __in6_dev_get(dev);
17087df37ff3SCatalin\(ux\) M. BOIE 	if (idev) {
17097df37ff3SCatalin\(ux\) M. BOIE 		read_lock_bh(&idev->lock);
17107df37ff3SCatalin\(ux\) M. BOIE 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
17117df37ff3SCatalin\(ux\) M. BOIE 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
17127df37ff3SCatalin\(ux\) M. BOIE 			if (ret)
17137df37ff3SCatalin\(ux\) M. BOIE 				break;
17147df37ff3SCatalin\(ux\) M. BOIE 		}
17157df37ff3SCatalin\(ux\) M. BOIE 		read_unlock_bh(&idev->lock);
17167df37ff3SCatalin\(ux\) M. BOIE 	}
17177df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_unlock();
17187df37ff3SCatalin\(ux\) M. BOIE 
17197df37ff3SCatalin\(ux\) M. BOIE 	return ret;
17207df37ff3SCatalin\(ux\) M. BOIE }
17217df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix);
17227df37ff3SCatalin\(ux\) M. BOIE 
1723b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
172452eeeb84SYOSHIFUJI Hideaki {
172552eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
172652eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
172752eeeb84SYOSHIFUJI Hideaki 	int	onlink;
172852eeeb84SYOSHIFUJI Hideaki 
172952eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
173052eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
173152eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
173252eeeb84SYOSHIFUJI Hideaki 	if (idev) {
173352eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
1734502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
173552eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
173652eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
173752eeeb84SYOSHIFUJI Hideaki 			if (onlink)
173852eeeb84SYOSHIFUJI Hideaki 				break;
173952eeeb84SYOSHIFUJI Hideaki 		}
174052eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
174152eeeb84SYOSHIFUJI Hideaki 	}
174252eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
174352eeeb84SYOSHIFUJI Hideaki 	return onlink;
174452eeeb84SYOSHIFUJI Hideaki }
174552eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
174652eeeb84SYOSHIFUJI Hideaki 
17479acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
17481cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
17491da177e4SLinus Torvalds {
1750b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
1751ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
17521da177e4SLinus Torvalds 
17535c578aedSstephen hemminger 	rcu_read_lock_bh();
1754b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1755878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
17561cab3da6SDaniel Lezcano 			continue;
17571da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
175863159f29SIan Morris 			if (!dev || ifp->idev->dev == dev ||
17591da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1760b79d1d54SDavid S. Miller 				result = ifp;
17611da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
17621da177e4SLinus Torvalds 				break;
17631da177e4SLinus Torvalds 			}
17641da177e4SLinus Torvalds 		}
17651da177e4SLinus Torvalds 	}
17665c578aedSstephen hemminger 	rcu_read_unlock_bh();
17671da177e4SLinus Torvalds 
1768b79d1d54SDavid S. Miller 	return result;
17691da177e4SLinus Torvalds }
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
17721da177e4SLinus Torvalds 
1773cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
17741da177e4SLinus Torvalds {
17753d171f39SLubomir Rintel 	if (dad_failed)
17763d171f39SLubomir Rintel 		ifp->flags |= IFA_F_DADFAILED;
17773d171f39SLubomir Rintel 
17781da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
17791da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
1780c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifp);
17811da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
17821da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
1783e2577a06SHerbert Xu 		if (dad_failed)
1784e2577a06SHerbert Xu 			ipv6_ifa_notify(0, ifp);
17851da177e4SLinus Torvalds 		in6_ifa_put(ifp);
17861da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
17871da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
17881da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
17891da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
17901da177e4SLinus Torvalds 		if (ifpub) {
17911da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
17921da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
17931da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
17941da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
17951da177e4SLinus Torvalds 		} else {
17961da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
17971da177e4SLinus Torvalds 		}
17981da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
1799c15b1ccaSHannes Frederic Sowa 	} else {
18001da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
18011da177e4SLinus Torvalds 	}
1802c15b1ccaSHannes Frederic Sowa }
18031da177e4SLinus Torvalds 
1804f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1805f2344a13SHerbert Xu {
1806f2344a13SHerbert Xu 	int err = -ENOENT;
1807f2344a13SHerbert Xu 
18088e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
1809f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
1810f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
1811f2344a13SHerbert Xu 		err = 0;
1812f2344a13SHerbert Xu 	}
18138e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
1814f2344a13SHerbert Xu 
1815f2344a13SHerbert Xu 	return err;
1816f2344a13SHerbert Xu }
1817f2344a13SHerbert Xu 
18183c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
18193c21edbdSYOSHIFUJI Hideaki {
18205f40ef77SHannes Frederic Sowa 	struct in6_addr addr;
18211b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
18221855b7c3SHannes Frederic Sowa 	struct net *net = dev_net(ifp->idev->dev);
18239bdd8d40SBrian Haley 
1824853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
1825853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
1826f2344a13SHerbert Xu 		return;
1827853dc2e0SUrsula Braun 	}
1828f2344a13SHerbert Xu 
1829e87cc472SJoe Perches 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
18300522fea6SJens Rosenboom 			     ifp->idev->dev->name, &ifp->addr);
18319bdd8d40SBrian Haley 
18325f40ef77SHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
18331b34be74SYOSHIFUJI Hideaki 
18345f40ef77SHannes Frederic Sowa 	if (ifp->flags & IFA_F_STABLE_PRIVACY) {
18355f40ef77SHannes Frederic Sowa 		int scope = ifp->scope;
18365f40ef77SHannes Frederic Sowa 		u32 flags = ifp->flags;
18375f40ef77SHannes Frederic Sowa 		struct in6_addr new_addr;
18385f40ef77SHannes Frederic Sowa 		struct inet6_ifaddr *ifp2;
18395f40ef77SHannes Frederic Sowa 		u32 valid_lft, preferred_lft;
18405f40ef77SHannes Frederic Sowa 		int pfxlen = ifp->prefix_len;
18415f40ef77SHannes Frederic Sowa 		int retries = ifp->stable_privacy_retry + 1;
18425f40ef77SHannes Frederic Sowa 
18431855b7c3SHannes Frederic Sowa 		if (retries > net->ipv6.sysctl.idgen_retries) {
18445f40ef77SHannes Frederic Sowa 			net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
18455f40ef77SHannes Frederic Sowa 					     ifp->idev->dev->name);
18465f40ef77SHannes Frederic Sowa 			goto errdad;
18475f40ef77SHannes Frederic Sowa 		}
18485f40ef77SHannes Frederic Sowa 
18495f40ef77SHannes Frederic Sowa 		new_addr = ifp->addr;
18505f40ef77SHannes Frederic Sowa 		if (ipv6_generate_stable_address(&new_addr, retries,
18515f40ef77SHannes Frederic Sowa 						 idev))
18525f40ef77SHannes Frederic Sowa 			goto errdad;
18535f40ef77SHannes Frederic Sowa 
18545f40ef77SHannes Frederic Sowa 		valid_lft = ifp->valid_lft;
18555f40ef77SHannes Frederic Sowa 		preferred_lft = ifp->prefered_lft;
18565f40ef77SHannes Frederic Sowa 
18575f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp->lock);
18585f40ef77SHannes Frederic Sowa 
18595f40ef77SHannes Frederic Sowa 		if (idev->cnf.max_addresses &&
18605f40ef77SHannes Frederic Sowa 		    ipv6_count_addresses(idev) >=
18615f40ef77SHannes Frederic Sowa 		    idev->cnf.max_addresses)
18625f40ef77SHannes Frederic Sowa 			goto lock_errdad;
18635f40ef77SHannes Frederic Sowa 
18645f40ef77SHannes Frederic Sowa 		net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
18655f40ef77SHannes Frederic Sowa 				     ifp->idev->dev->name);
18665f40ef77SHannes Frederic Sowa 
18675f40ef77SHannes Frederic Sowa 		ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
18685f40ef77SHannes Frederic Sowa 				     scope, flags, valid_lft,
18695f40ef77SHannes Frederic Sowa 				     preferred_lft);
18705f40ef77SHannes Frederic Sowa 		if (IS_ERR(ifp2))
18715f40ef77SHannes Frederic Sowa 			goto lock_errdad;
18725f40ef77SHannes Frederic Sowa 
18735f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp2->lock);
18745f40ef77SHannes Frederic Sowa 		ifp2->stable_privacy_retry = retries;
18755f40ef77SHannes Frederic Sowa 		ifp2->state = INET6_IFADDR_STATE_PREDAD;
18765f40ef77SHannes Frederic Sowa 		spin_unlock_bh(&ifp2->lock);
18775f40ef77SHannes Frederic Sowa 
18781855b7c3SHannes Frederic Sowa 		addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
18795f40ef77SHannes Frederic Sowa 		in6_ifa_put(ifp2);
18805f40ef77SHannes Frederic Sowa lock_errdad:
18815f40ef77SHannes Frederic Sowa 		spin_lock_bh(&ifp->lock);
18825f40ef77SHannes Frederic Sowa 	} else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
18831b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[0] = htonl(0xfe800000);
18841b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[1] = 0;
18851b34be74SYOSHIFUJI Hideaki 
18861b34be74SYOSHIFUJI Hideaki 		if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
18871b34be74SYOSHIFUJI Hideaki 		    ipv6_addr_equal(&ifp->addr, &addr)) {
18881b34be74SYOSHIFUJI Hideaki 			/* DAD failed for link-local based on MAC address */
18891b34be74SYOSHIFUJI Hideaki 			idev->cnf.disable_ipv6 = 1;
18909bdd8d40SBrian Haley 
1891f3213831SJoe Perches 			pr_info("%s: IPv6 being disabled!\n",
18929bdd8d40SBrian Haley 				ifp->idev->dev->name);
18931b34be74SYOSHIFUJI Hideaki 		}
18941b34be74SYOSHIFUJI Hideaki 	}
18951b34be74SYOSHIFUJI Hideaki 
18965f40ef77SHannes Frederic Sowa errdad:
1897c15b1ccaSHannes Frederic Sowa 	/* transition from _POSTDAD to _ERRDAD */
1898c15b1ccaSHannes Frederic Sowa 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
18998e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
1900c15b1ccaSHannes Frederic Sowa 
1901c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, 0);
19023c21edbdSYOSHIFUJI Hideaki }
19031da177e4SLinus Torvalds 
1904a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group.
1905a9ed4a29SSabrina Dubroca  * caller must hold RTNL */
1906b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
19071da177e4SLinus Torvalds {
19081da177e4SLinus Torvalds 	struct in6_addr maddr;
19091da177e4SLinus Torvalds 
19101da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
19111da177e4SLinus Torvalds 		return;
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
19141da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
19151da177e4SLinus Torvalds }
19161da177e4SLinus Torvalds 
1917a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
1918b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
19191da177e4SLinus Torvalds {
19201da177e4SLinus Torvalds 	struct in6_addr maddr;
19211da177e4SLinus Torvalds 
19221da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
19231da177e4SLinus Torvalds 		return;
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
19261da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
19271da177e4SLinus Torvalds }
19281da177e4SLinus Torvalds 
1929a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
193020380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
19311da177e4SLinus Torvalds {
19321da177e4SLinus Torvalds 	struct in6_addr addr;
1933c15b1ccaSHannes Frederic Sowa 
193488ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
19352bda8a0cSBjørn Mork 		return;
19361da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
19371da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
19381da177e4SLinus Torvalds 		return;
1939013b4d90SWANG Cong 	__ipv6_dev_ac_inc(ifp->idev, &addr);
19401da177e4SLinus Torvalds }
19411da177e4SLinus Torvalds 
1942a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
194320380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
19441da177e4SLinus Torvalds {
19451da177e4SLinus Torvalds 	struct in6_addr addr;
1946c15b1ccaSHannes Frederic Sowa 
194788ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
194832019e65SYOSHIFUJI Hideaki 		return;
19491da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
19501da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
19511da177e4SLinus Torvalds 		return;
19521da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
19531da177e4SLinus Torvalds }
19541da177e4SLinus Torvalds 
195506a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
195606a4c1c5Salex.bluesman.smirnov@gmail.com {
19575241c2d7SAlexander Aring 	if (dev->addr_len != EUI64_ADDR_LEN)
195806a4c1c5Salex.bluesman.smirnov@gmail.com 		return -1;
19595241c2d7SAlexander Aring 	memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
19605e98a36eSYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
196106a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
196206a4c1c5Salex.bluesman.smirnov@gmail.com }
196306a4c1c5Salex.bluesman.smirnov@gmail.com 
1964cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1965cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
1966cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	union fwnet_hwaddr *ha;
1967cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1968cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
1969cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
1970cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1971cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	ha = (union fwnet_hwaddr *)dev->dev_addr;
1972cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1973cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1974cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
1975cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
1976cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
1977cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1978073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1979073a8e0eSYOSHIFUJI Hideaki {
19801da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
19811da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
19821da177e4SLinus Torvalds 		return -1;
19831da177e4SLinus Torvalds 	memset(eui, 0, 7);
19841da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
19851da177e4SLinus Torvalds 	return 0;
1986073a8e0eSYOSHIFUJI Hideaki }
1987073a8e0eSYOSHIFUJI Hideaki 
1988073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1989073a8e0eSYOSHIFUJI Hideaki {
19901da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
19911da177e4SLinus Torvalds 		return -1;
19921da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
19931da177e4SLinus Torvalds 	eui[0] |= 2;
19941da177e4SLinus Torvalds 	return 0;
19951da177e4SLinus Torvalds }
1996073a8e0eSYOSHIFUJI Hideaki 
1997c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1998dfd982baSYOSHIFUJI Hideaki {
19999af28511SSascha Hlusiak 	if (addr == 0)
20009af28511SSascha Hlusiak 		return -1;
2001dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2002dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2003dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2004dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2005dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2006dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2007dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
2008dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
2009dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
2010dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
2011dfd982baSYOSHIFUJI Hideaki 	return 0;
2012dfd982baSYOSHIFUJI Hideaki }
2013dfd982baSYOSHIFUJI Hideaki 
2014dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2015dfd982baSYOSHIFUJI Hideaki {
2016dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
2017dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2018dfd982baSYOSHIFUJI Hideaki 	return -1;
2019dfd982baSYOSHIFUJI Hideaki }
2020dfd982baSYOSHIFUJI Hideaki 
2021aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2022aee80b54Sstephen hemminger {
2023aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2024aee80b54Sstephen hemminger }
2025aee80b54Sstephen hemminger 
2026e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2027e837735eSNicolas Dichtel {
2028e837735eSNicolas Dichtel 	memcpy(eui, dev->perm_addr, 3);
2029e837735eSNicolas Dichtel 	memcpy(eui + 5, dev->perm_addr + 3, 3);
2030e837735eSNicolas Dichtel 	eui[3] = 0xFF;
2031e837735eSNicolas Dichtel 	eui[4] = 0xFE;
2032e837735eSNicolas Dichtel 	eui[0] ^= 2;
2033e837735eSNicolas Dichtel 	return 0;
2034e837735eSNicolas Dichtel }
2035e837735eSNicolas Dichtel 
2036073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2037073a8e0eSYOSHIFUJI Hideaki {
2038073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
2039073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
2040073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
2041073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
2042073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
2043073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
2044073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
2045073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
2046c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
2047dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
2048aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
2049aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
2050e74bccb8SJukka Rissanen 	case ARPHRD_6LOWPAN:
205106a4c1c5Salex.bluesman.smirnov@gmail.com 		return addrconf_ifid_eui64(eui, dev);
2052cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
2053cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
2054e837735eSNicolas Dichtel 	case ARPHRD_TUNNEL6:
2055e837735eSNicolas Dichtel 		return addrconf_ifid_ip6tnl(eui, dev);
2056073a8e0eSYOSHIFUJI Hideaki 	}
20571da177e4SLinus Torvalds 	return -1;
20581da177e4SLinus Torvalds }
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
20611da177e4SLinus Torvalds {
20621da177e4SLinus Torvalds 	int err = -1;
20631da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2066602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2067602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
2068602582caSHannes Frederic Sowa 			break;
20691da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
20701da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
20711da177e4SLinus Torvalds 			err = 0;
20721da177e4SLinus Torvalds 			break;
20731da177e4SLinus Torvalds 		}
20741da177e4SLinus Torvalds 	}
20751da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
20761da177e4SLinus Torvalds 	return err;
20771da177e4SLinus Torvalds }
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2080eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev)
20811da177e4SLinus Torvalds {
20821da177e4SLinus Torvalds regen:
2083955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
20841da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds 	/*
20871da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
20881da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
20891da177e4SLinus Torvalds 	 *
20901da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
20911da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
2092c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
20931da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
20941da177e4SLinus Torvalds 	 *  - value 0
20951da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
20961da177e4SLinus Torvalds 	 */
20971da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
20981da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
20991da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
21001da177e4SLinus Torvalds 		goto regen;
21011da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
21021da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
21031da177e4SLinus Torvalds 			goto regen;
21041da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
21051da177e4SLinus Torvalds 			goto regen;
21061da177e4SLinus Torvalds 	}
21071da177e4SLinus Torvalds }
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
21101da177e4SLinus Torvalds {
21111da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
21121da177e4SLinus Torvalds 	unsigned long expires;
21131da177e4SLinus Torvalds 
21148814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
21151da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds 	if (idev->dead)
21181da177e4SLinus Torvalds 		goto out;
21191da177e4SLinus Torvalds 
2120eb7e0575SSorin Dumitru 	__ipv6_regen_rndid(idev);
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds 	expires = jiffies +
21231da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
21241f9248e5SJiri Pirko 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
21251f9248e5SJiri Pirko 		NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
2126784e2710SBen Hutchings 		idev->cnf.max_desync_factor * HZ;
21271da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
2128f3213831SJoe Perches 		pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2129f3213831SJoe Perches 			__func__, idev->dev->name);
21301da177e4SLinus Torvalds 		goto out;
21311da177e4SLinus Torvalds 	}
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
21341da177e4SLinus Torvalds 		in6_dev_hold(idev);
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds out:
21371da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
21388814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
21391da177e4SLinus Torvalds 	in6_dev_put(idev);
21401da177e4SLinus Torvalds }
21411da177e4SLinus Torvalds 
2142eb7e0575SSorin Dumitru static void  __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
21438e5e8f30SEldad Zack {
21441da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2145eb7e0575SSorin Dumitru 		__ipv6_regen_rndid(idev);
21461da177e4SLinus Torvalds }
21471da177e4SLinus Torvalds 
21481da177e4SLinus Torvalds /*
21491da177e4SLinus Torvalds  *	Add prefix route.
21501da177e4SLinus Torvalds  */
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds static void
21531da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2154e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
21551da177e4SLinus Torvalds {
215686872cb5SThomas Graf 	struct fib6_config cfg = {
2157ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
215886872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
215986872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
216086872cb5SThomas Graf 		.fc_expires = expires,
216186872cb5SThomas Graf 		.fc_dst_len = plen,
216286872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
2163c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
2164f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
216586872cb5SThomas Graf 	};
21661da177e4SLinus Torvalds 
21674e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
21701da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
21711da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
21721da177e4SLinus Torvalds 	 */
217307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
21741da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
217586872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
21760be669bbSJoerg Roedel #endif
21771da177e4SLinus Torvalds 
217886872cb5SThomas Graf 	ip6_route_add(&cfg);
21791da177e4SLinus Torvalds }
21801da177e4SLinus Torvalds 
218114ef37b6SAndreas Hofmeister 
218214ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
218314ef37b6SAndreas Hofmeister 						  int plen,
218414ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
218514ef37b6SAndreas Hofmeister 						  u32 flags, u32 noflags)
218614ef37b6SAndreas Hofmeister {
218714ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
218814ef37b6SAndreas Hofmeister 	struct rt6_info *rt = NULL;
218914ef37b6SAndreas Hofmeister 	struct fib6_table *table;
2190ca254490SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
219114ef37b6SAndreas Hofmeister 
2192ca254490SDavid Ahern 	table = fib6_get_table(dev_net(dev), tb_id);
219363159f29SIan Morris 	if (!table)
219414ef37b6SAndreas Hofmeister 		return NULL;
219514ef37b6SAndreas Hofmeister 
21965744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
219714ef37b6SAndreas Hofmeister 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
219814ef37b6SAndreas Hofmeister 	if (!fn)
219914ef37b6SAndreas Hofmeister 		goto out;
22001f56a01fSMartin KaFai Lau 
22011f56a01fSMartin KaFai Lau 	noflags |= RTF_CACHE;
220214ef37b6SAndreas Hofmeister 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2203d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != dev->ifindex)
220414ef37b6SAndreas Hofmeister 			continue;
220514ef37b6SAndreas Hofmeister 		if ((rt->rt6i_flags & flags) != flags)
220614ef37b6SAndreas Hofmeister 			continue;
220785da53bfSRomain Kuntz 		if ((rt->rt6i_flags & noflags) != 0)
220814ef37b6SAndreas Hofmeister 			continue;
220914ef37b6SAndreas Hofmeister 		dst_hold(&rt->dst);
221014ef37b6SAndreas Hofmeister 		break;
221114ef37b6SAndreas Hofmeister 	}
221214ef37b6SAndreas Hofmeister out:
22135744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
221414ef37b6SAndreas Hofmeister 	return rt;
221514ef37b6SAndreas Hofmeister }
221614ef37b6SAndreas Hofmeister 
221714ef37b6SAndreas Hofmeister 
22181da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
22211da177e4SLinus Torvalds {
222286872cb5SThomas Graf 	struct fib6_config cfg = {
2223ca254490SDavid Ahern 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
222486872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
222586872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
222686872cb5SThomas Graf 		.fc_dst_len = 8,
222786872cb5SThomas Graf 		.fc_flags = RTF_UP,
2228c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
222986872cb5SThomas Graf 	};
22301da177e4SLinus Torvalds 
223186872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
223286872cb5SThomas Graf 
223386872cb5SThomas Graf 	ip6_route_add(&cfg);
22341da177e4SLinus Torvalds }
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
22371da177e4SLinus Torvalds {
22381da177e4SLinus Torvalds 	struct inet6_dev *idev;
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds 	ASSERT_RTNL();
22411da177e4SLinus Torvalds 
2242e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2243e21e8467SStephen Hemminger 	if (!idev)
224464e724f6SBrian Haley 		return ERR_PTR(-ENOBUFS);
224564e724f6SBrian Haley 
224664e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
224764e724f6SBrian Haley 		return ERR_PTR(-EACCES);
22481da177e4SLinus Torvalds 
22491da177e4SLinus Torvalds 	/* Add default multicast route */
22504af04abaSLi Wei 	if (!(dev->flags & IFF_LOOPBACK))
22511da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
22521da177e4SLinus Torvalds 
22531da177e4SLinus Torvalds 	return idev;
22541da177e4SLinus Torvalds }
22551da177e4SLinus Torvalds 
225653bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev,
225753bd6749SJiri Pirko 			     struct inet6_ifaddr *ifp,
225853bd6749SJiri Pirko 			     __u32 valid_lft, __u32 prefered_lft,
225953bd6749SJiri Pirko 			     bool create, unsigned long now)
226053bd6749SJiri Pirko {
226153bd6749SJiri Pirko 	u32 flags;
226253bd6749SJiri Pirko 	struct inet6_ifaddr *ift;
226353bd6749SJiri Pirko 
226453bd6749SJiri Pirko 	read_lock_bh(&idev->lock);
226553bd6749SJiri Pirko 	/* update all temporary addresses in the list */
226653bd6749SJiri Pirko 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
226753bd6749SJiri Pirko 		int age, max_valid, max_prefered;
226853bd6749SJiri Pirko 
226953bd6749SJiri Pirko 		if (ifp != ift->ifpub)
227053bd6749SJiri Pirko 			continue;
227153bd6749SJiri Pirko 
227253bd6749SJiri Pirko 		/* RFC 4941 section 3.3:
227353bd6749SJiri Pirko 		 * If a received option will extend the lifetime of a public
227453bd6749SJiri Pirko 		 * address, the lifetimes of temporary addresses should
227553bd6749SJiri Pirko 		 * be extended, subject to the overall constraint that no
227653bd6749SJiri Pirko 		 * temporary addresses should ever remain "valid" or "preferred"
227753bd6749SJiri Pirko 		 * for a time longer than (TEMP_VALID_LIFETIME) or
227853bd6749SJiri Pirko 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
227953bd6749SJiri Pirko 		 */
228053bd6749SJiri Pirko 		age = (now - ift->cstamp) / HZ;
228153bd6749SJiri Pirko 		max_valid = idev->cnf.temp_valid_lft - age;
228253bd6749SJiri Pirko 		if (max_valid < 0)
228353bd6749SJiri Pirko 			max_valid = 0;
228453bd6749SJiri Pirko 
228553bd6749SJiri Pirko 		max_prefered = idev->cnf.temp_prefered_lft -
228653bd6749SJiri Pirko 			       idev->cnf.max_desync_factor - age;
228753bd6749SJiri Pirko 		if (max_prefered < 0)
228853bd6749SJiri Pirko 			max_prefered = 0;
228953bd6749SJiri Pirko 
229053bd6749SJiri Pirko 		if (valid_lft > max_valid)
229153bd6749SJiri Pirko 			valid_lft = max_valid;
229253bd6749SJiri Pirko 
229353bd6749SJiri Pirko 		if (prefered_lft > max_prefered)
229453bd6749SJiri Pirko 			prefered_lft = max_prefered;
229553bd6749SJiri Pirko 
229653bd6749SJiri Pirko 		spin_lock(&ift->lock);
229753bd6749SJiri Pirko 		flags = ift->flags;
229853bd6749SJiri Pirko 		ift->valid_lft = valid_lft;
229953bd6749SJiri Pirko 		ift->prefered_lft = prefered_lft;
230053bd6749SJiri Pirko 		ift->tstamp = now;
230153bd6749SJiri Pirko 		if (prefered_lft > 0)
230253bd6749SJiri Pirko 			ift->flags &= ~IFA_F_DEPRECATED;
230353bd6749SJiri Pirko 
230453bd6749SJiri Pirko 		spin_unlock(&ift->lock);
230553bd6749SJiri Pirko 		if (!(flags&IFA_F_TENTATIVE))
230653bd6749SJiri Pirko 			ipv6_ifa_notify(0, ift);
230753bd6749SJiri Pirko 	}
230853bd6749SJiri Pirko 
230953bd6749SJiri Pirko 	if ((create || list_empty(&idev->tempaddr_list)) &&
231053bd6749SJiri Pirko 	    idev->cnf.use_tempaddr > 0) {
231153bd6749SJiri Pirko 		/* When a new public address is created as described
231253bd6749SJiri Pirko 		 * in [ADDRCONF], also create a new temporary address.
231353bd6749SJiri Pirko 		 * Also create a temporary address if it's enabled but
231453bd6749SJiri Pirko 		 * no temporary address currently exists.
231553bd6749SJiri Pirko 		 */
231653bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
231753bd6749SJiri Pirko 		ipv6_create_tempaddr(ifp, NULL);
231853bd6749SJiri Pirko 	} else {
231953bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
232053bd6749SJiri Pirko 	}
232153bd6749SJiri Pirko }
232253bd6749SJiri Pirko 
2323cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2324cc9da6ccSBjørn Mork {
2325cc9da6ccSBjørn Mork 	return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2326cc9da6ccSBjørn Mork 	       idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2327cc9da6ccSBjørn Mork }
2328cc9da6ccSBjørn Mork 
2329e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
23301da177e4SLinus Torvalds {
23311da177e4SLinus Torvalds 	struct prefix_info *pinfo;
23321da177e4SLinus Torvalds 	__u32 valid_lft;
23331da177e4SLinus Torvalds 	__u32 prefered_lft;
23341da177e4SLinus Torvalds 	int addr_type;
233564236f3fSHannes Frederic Sowa 	u32 addr_flags = 0;
23361da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
233756d417b1SBrian Haley 	struct net *net = dev_net(dev);
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
2342ba3542e1Sdingtianhong 		ADBG("addrconf: prefix option too short\n");
23431da177e4SLinus Torvalds 		return;
23441da177e4SLinus Torvalds 	}
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	/*
23471da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
23481da177e4SLinus Torvalds 	 */
23491da177e4SLinus Torvalds 
23501da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
23511da177e4SLinus Torvalds 
23521da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
23531da177e4SLinus Torvalds 		return;
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
23561da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2359e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
23601da177e4SLinus Torvalds 		return;
23611da177e4SLinus Torvalds 	}
23621da177e4SLinus Torvalds 
23631da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
23641da177e4SLinus Torvalds 
236563159f29SIan Morris 	if (!in6_dev) {
2366e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2367e87cc472SJoe Perches 				    dev->name);
23681da177e4SLinus Torvalds 		return;
23691da177e4SLinus Torvalds 	}
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 	/*
23721da177e4SLinus Torvalds 	 *	Two things going on here:
23731da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
23741da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
23751da177e4SLinus Torvalds 	 */
23761da177e4SLinus Torvalds 
23774bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
23784bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
23794bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
23804bed72e4SYOSHIFUJI Hideaki 
23811da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
23826f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
23836f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
23846f704992SYOSHIFUJI Hideaki 		 * not good.
23851da177e4SLinus Torvalds 		 */
23864bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
23874bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
23884bed72e4SYOSHIFUJI Hideaki 		else
23894bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
23903dd4bc68SYOSHIFUJI Hideaki 
23914bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
23924bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
23931da177e4SLinus Torvalds 
239414ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
239514ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
239614ef37b6SAndreas Hofmeister 					       dev,
239714ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
239814ef37b6SAndreas Hofmeister 					       RTF_GATEWAY | RTF_DEFAULT);
23991da177e4SLinus Torvalds 
240014ef37b6SAndreas Hofmeister 		if (rt) {
24016f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
24021da177e4SLinus Torvalds 			if (valid_lft == 0) {
2403e0a1ad73SThomas Graf 				ip6_del_rt(rt);
24041da177e4SLinus Torvalds 				rt = NULL;
24054bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
24066f704992SYOSHIFUJI Hideaki 				/* not infinity */
24071716a961SGao feng 				rt6_set_expires(rt, jiffies + rt_expires);
24086f704992SYOSHIFUJI Hideaki 			} else {
24091716a961SGao feng 				rt6_clean_expires(rt);
24101da177e4SLinus Torvalds 			}
24111da177e4SLinus Torvalds 		} else if (valid_lft) {
24126f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
24134bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
24144bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
24156f704992SYOSHIFUJI Hideaki 				/* not infinity */
24166f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
24176f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
24186f704992SYOSHIFUJI Hideaki 			}
24191da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
24206f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
24211da177e4SLinus Torvalds 		}
242294e187c0SAmerigo Wang 		ip6_rt_put(rt);
24231da177e4SLinus Torvalds 	}
24241da177e4SLinus Torvalds 
24251da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
24281da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
24291da177e4SLinus Torvalds 		struct in6_addr addr;
24301da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
2431617fe29dSDaniel Borkmann 		bool tokenized = false;
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
24341da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2435f53adae4SDaniel Borkmann 
2436f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2437f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2438f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2439f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2440f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
2441617fe29dSDaniel Borkmann 				tokenized = true;
2442cc9da6ccSBjørn Mork 			} else if (is_addr_mode_generate_stable(in6_dev) &&
2443622c81d5SHannes Frederic Sowa 				   !ipv6_generate_stable_address(&addr, 0,
2444622c81d5SHannes Frederic Sowa 								 in6_dev)) {
244564236f3fSHannes Frederic Sowa 				addr_flags |= IFA_F_STABLE_PRIVACY;
2446622c81d5SHannes Frederic Sowa 				goto ok;
2447f53adae4SDaniel Borkmann 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
24481da177e4SLinus Torvalds 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
24491da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
24501da177e4SLinus Torvalds 				return;
24511da177e4SLinus Torvalds 			}
24521da177e4SLinus Torvalds 			goto ok;
24531da177e4SLinus Torvalds 		}
2454e87cc472SJoe Perches 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
24551da177e4SLinus Torvalds 				    pinfo->prefix_len);
24561da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
24571da177e4SLinus Torvalds 		return;
24581da177e4SLinus Torvalds 
24591da177e4SLinus Torvalds ok:
24601da177e4SLinus Torvalds 
246153b7997fSYOSHIFUJI Hideaki 		ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
24621da177e4SLinus Torvalds 
246363159f29SIan Morris 		if (!ifp && valid_lft) {
24641da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
246595c385b4SNeil Horman 
246695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
246795c385b4SNeil Horman 			if (in6_dev->cnf.optimistic_dad &&
2468e6bff995SNeil Horman 			    !net->ipv6.devconf_all->forwarding && sllao)
24699a1ec461SBjørn Mork 				addr_flags |= IFA_F_OPTIMISTIC;
247095c385b4SNeil Horman #endif
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
24731da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
24741da177e4SLinus Torvalds 			 */
24751da177e4SLinus Torvalds 			if (!max_addresses ||
24761da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
24773f8f5298SJiri Pirko 				ifp = ipv6_add_addr(in6_dev, &addr, NULL,
24783f8f5298SJiri Pirko 						    pinfo->prefix_len,
247995c385b4SNeil Horman 						    addr_type&IPV6_ADDR_SCOPE_MASK,
24808a226b2cSJiri Benc 						    addr_flags, valid_lft,
24818a226b2cSJiri Benc 						    prefered_lft);
24821da177e4SLinus Torvalds 
24833f0d2ba0SYOSHIFUJI Hideaki / 吉藤英明 			if (IS_ERR_OR_NULL(ifp)) {
24841da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
24851da177e4SLinus Torvalds 				return;
24861da177e4SLinus Torvalds 			}
24871da177e4SLinus Torvalds 
24888a226b2cSJiri Benc 			update_lft = 0;
24898a226b2cSJiri Benc 			create = 1;
2490c15b1ccaSHannes Frederic Sowa 			spin_lock_bh(&ifp->lock);
2491c15b1ccaSHannes Frederic Sowa 			ifp->flags |= IFA_F_MANAGETEMPADDR;
24921da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
2493617fe29dSDaniel Borkmann 			ifp->tokenized = tokenized;
2494c15b1ccaSHannes Frederic Sowa 			spin_unlock_bh(&ifp->lock);
2495cf22f9a2SDavid S. Miller 			addrconf_dad_start(ifp);
24961da177e4SLinus Torvalds 		}
24971da177e4SLinus Torvalds 
24981da177e4SLinus Torvalds 		if (ifp) {
2499479840ffSJiri Pirko 			u32 flags;
25001da177e4SLinus Torvalds 			unsigned long now;
25011da177e4SLinus Torvalds 			u32 stored_lft;
25021da177e4SLinus Torvalds 
25031da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
25048e8e676dSHannes Frederic Sowa 			spin_lock_bh(&ifp->lock);
25051da177e4SLinus Torvalds 			now = jiffies;
25061da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
25071da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
25081da177e4SLinus Torvalds 			else
25091da177e4SLinus Torvalds 				stored_lft = 0;
25108a226b2cSJiri Benc 			if (!update_lft && !create && stored_lft) {
25119451a304SFabian Frederick 				const u32 minimum_lft = min_t(u32,
25129451a304SFabian Frederick 					stored_lft, MIN_VALID_LIFETIME);
2513c9d55d5bSPaul Marks 				valid_lft = max(valid_lft, minimum_lft);
2514c9d55d5bSPaul Marks 
2515c9d55d5bSPaul Marks 				/* RFC4862 Section 5.5.3e:
2516c9d55d5bSPaul Marks 				 * "Note that the preferred lifetime of the
2517c9d55d5bSPaul Marks 				 *  corresponding address is always reset to
2518c9d55d5bSPaul Marks 				 *  the Preferred Lifetime in the received
2519c9d55d5bSPaul Marks 				 *  Prefix Information option, regardless of
2520c9d55d5bSPaul Marks 				 *  whether the valid lifetime is also reset or
2521a1ed0526SBrian Haley 				 *  ignored."
2522a1ed0526SBrian Haley 				 *
2523c9d55d5bSPaul Marks 				 * So we should always update prefered_lft here.
2524a1ed0526SBrian Haley 				 */
2525a1ed0526SBrian Haley 				update_lft = 1;
2526a1ed0526SBrian Haley 			}
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds 			if (update_lft) {
25291da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
25301da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
25311da177e4SLinus Torvalds 				ifp->tstamp = now;
25321da177e4SLinus Torvalds 				flags = ifp->flags;
25331da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
25348e8e676dSHannes Frederic Sowa 				spin_unlock_bh(&ifp->lock);
25351da177e4SLinus Torvalds 
25361da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
25371da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
25381da177e4SLinus Torvalds 			} else
25398e8e676dSHannes Frederic Sowa 				spin_unlock_bh(&ifp->lock);
25401da177e4SLinus Torvalds 
254153bd6749SJiri Pirko 			manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
254253bd6749SJiri Pirko 					 create, now);
25435d9efa7eSDavid S. Miller 
25441da177e4SLinus Torvalds 			in6_ifa_put(ifp);
2545c15b1ccaSHannes Frederic Sowa 			addrconf_verify();
25461da177e4SLinus Torvalds 		}
25471da177e4SLinus Torvalds 	}
25481da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
25491da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
25501da177e4SLinus Torvalds }
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds /*
25531da177e4SLinus Torvalds  *	Set destination address.
25541da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
25551da177e4SLinus Torvalds  *	device.
25561da177e4SLinus Torvalds  */
2557af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
25581da177e4SLinus Torvalds {
25591da177e4SLinus Torvalds 	struct in6_ifreq ireq;
25601da177e4SLinus Torvalds 	struct net_device *dev;
25611da177e4SLinus Torvalds 	int err = -EINVAL;
25621da177e4SLinus Torvalds 
25631da177e4SLinus Torvalds 	rtnl_lock();
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds 	err = -EFAULT;
25661da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
25671da177e4SLinus Torvalds 		goto err_exit;
25681da177e4SLinus Torvalds 
2569af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
25701da177e4SLinus Torvalds 
25711da177e4SLinus Torvalds 	err = -ENODEV;
257263159f29SIan Morris 	if (!dev)
25731da177e4SLinus Torvalds 		goto err_exit;
25741da177e4SLinus Torvalds 
257507a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
25761da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
25775bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
25781da177e4SLinus Torvalds 		struct ifreq ifr;
25791da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
25801da177e4SLinus Torvalds 
25811da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
25821da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
25831da177e4SLinus Torvalds 			goto err_exit;
25841da177e4SLinus Torvalds 
25851da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
25861da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
25871da177e4SLinus Torvalds 		p.iph.saddr = 0;
25881da177e4SLinus Torvalds 		p.iph.version = 4;
25891da177e4SLinus Torvalds 		p.iph.ihl = 5;
25901da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
25911da177e4SLinus Torvalds 		p.iph.ttl = 64;
2592d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
25931da177e4SLinus Torvalds 
25945bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
25955bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
25965bc3eb7eSStephen Hemminger 
25975bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
25985bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
25991da177e4SLinus Torvalds 			set_fs(oldfs);
26005bc3eb7eSStephen Hemminger 		} else
26015bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds 		if (err == 0) {
26041da177e4SLinus Torvalds 			err = -ENOBUFS;
2605af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2606af284937SDaniel Lezcano 			if (!dev)
26071da177e4SLinus Torvalds 				goto err_exit;
26081da177e4SLinus Torvalds 			err = dev_open(dev);
26091da177e4SLinus Torvalds 		}
26101da177e4SLinus Torvalds 	}
26110be669bbSJoerg Roedel #endif
26121da177e4SLinus Torvalds 
26131da177e4SLinus Torvalds err_exit:
26141da177e4SLinus Torvalds 	rtnl_unlock();
26151da177e4SLinus Torvalds 	return err;
26161da177e4SLinus Torvalds }
26171da177e4SLinus Torvalds 
261893a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join,
261993a714d6SMadhu Challa 			  const struct in6_addr *addr, int ifindex)
262093a714d6SMadhu Challa {
262193a714d6SMadhu Challa 	int ret;
262293a714d6SMadhu Challa 
262393a714d6SMadhu Challa 	ASSERT_RTNL();
262493a714d6SMadhu Challa 
262593a714d6SMadhu Challa 	lock_sock(sk);
262693a714d6SMadhu Challa 	if (join)
262754ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_join(sk, ifindex, addr);
262893a714d6SMadhu Challa 	else
262954ff9ef3SMarcelo Ricardo Leitner 		ret = ipv6_sock_mc_drop(sk, ifindex, addr);
263093a714d6SMadhu Challa 	release_sock(sk);
263193a714d6SMadhu Challa 
263293a714d6SMadhu Challa 	return ret;
263393a714d6SMadhu Challa }
263493a714d6SMadhu Challa 
26351da177e4SLinus Torvalds /*
26361da177e4SLinus Torvalds  *	Manual configuration of address on an interface
26371da177e4SLinus Torvalds  */
2638479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex,
2639479840ffSJiri Pirko 			  const struct in6_addr *pfx,
2640caeaba79SNicolas Dichtel 			  const struct in6_addr *peer_pfx,
2641479840ffSJiri Pirko 			  unsigned int plen, __u32 ifa_flags,
2642479840ffSJiri Pirko 			  __u32 prefered_lft, __u32 valid_lft)
26431da177e4SLinus Torvalds {
26441da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
26451da177e4SLinus Torvalds 	struct inet6_dev *idev;
26461da177e4SLinus Torvalds 	struct net_device *dev;
264793a714d6SMadhu Challa 	unsigned long timeout;
264893a714d6SMadhu Challa 	clock_t expires;
26491da177e4SLinus Torvalds 	int scope;
26506f704992SYOSHIFUJI Hideaki 	u32 flags;
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds 	ASSERT_RTNL();
26531da177e4SLinus Torvalds 
265424ef0da7SThomas Graf 	if (plen > 128)
265524ef0da7SThomas Graf 		return -EINVAL;
265624ef0da7SThomas Graf 
26570778769dSNoriaki TAKAMIYA 	/* check the lifetime */
26580778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
26590778769dSNoriaki TAKAMIYA 		return -EINVAL;
26600778769dSNoriaki TAKAMIYA 
266153bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
266253bd6749SJiri Pirko 		return -EINVAL;
266353bd6749SJiri Pirko 
2664af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2665af284937SDaniel Lezcano 	if (!dev)
26661da177e4SLinus Torvalds 		return -ENODEV;
26671da177e4SLinus Torvalds 
266864e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
266964e724f6SBrian Haley 	if (IS_ERR(idev))
267064e724f6SBrian Haley 		return PTR_ERR(idev);
26711da177e4SLinus Torvalds 
267293a714d6SMadhu Challa 	if (ifa_flags & IFA_F_MCAUTOJOIN) {
267393a714d6SMadhu Challa 		int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
267493a714d6SMadhu Challa 					 true, pfx, ifindex);
267593a714d6SMadhu Challa 
267693a714d6SMadhu Challa 		if (ret < 0)
267793a714d6SMadhu Challa 			return ret;
267893a714d6SMadhu Challa 	}
267993a714d6SMadhu Challa 
26801da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
26811da177e4SLinus Torvalds 
26824bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
26834bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
26844bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
26854bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
26866f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
26874bed72e4SYOSHIFUJI Hideaki 	} else {
26884bed72e4SYOSHIFUJI Hideaki 		expires = 0;
26894bed72e4SYOSHIFUJI Hideaki 		flags = 0;
26904bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
26916f704992SYOSHIFUJI Hideaki 	}
26920778769dSNoriaki TAKAMIYA 
26934bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
26944bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
26954bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
26960778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
26974bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
26984bed72e4SYOSHIFUJI Hideaki 	}
26990778769dSNoriaki TAKAMIYA 
27008a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
27018a226b2cSJiri Benc 			    valid_lft, prefered_lft);
27020778769dSNoriaki TAKAMIYA 
27031da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
2704761aac73SThomas Haller 		if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
270546d48046SYOSHIFUJI Hideaki 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
27066f704992SYOSHIFUJI Hideaki 					      expires, flags);
2707761aac73SThomas Haller 		}
2708761aac73SThomas Haller 
270995c385b4SNeil Horman 		/*
271095c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
271195c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
271295c385b4SNeil Horman 		 * manually configured addresses
271395c385b4SNeil Horman 		 */
2714cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
271553bd6749SJiri Pirko 		if (ifa_flags & IFA_F_MANAGETEMPADDR)
271653bd6749SJiri Pirko 			manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
271753bd6749SJiri Pirko 					 true, jiffies);
27181da177e4SLinus Torvalds 		in6_ifa_put(ifp);
2719c15b1ccaSHannes Frederic Sowa 		addrconf_verify_rtnl();
27201da177e4SLinus Torvalds 		return 0;
272193a714d6SMadhu Challa 	} else if (ifa_flags & IFA_F_MCAUTOJOIN) {
272293a714d6SMadhu Challa 		ipv6_mc_config(net->ipv6.mc_autojoin_sk,
272393a714d6SMadhu Challa 			       false, pfx, ifindex);
27241da177e4SLinus Torvalds 	}
27251da177e4SLinus Torvalds 
27261da177e4SLinus Torvalds 	return PTR_ERR(ifp);
27271da177e4SLinus Torvalds }
27281da177e4SLinus Torvalds 
27296046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
27306046d5b4SHeiner Kallweit 			  const struct in6_addr *pfx, unsigned int plen)
27311da177e4SLinus Torvalds {
27321da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
27331da177e4SLinus Torvalds 	struct inet6_dev *idev;
27341da177e4SLinus Torvalds 	struct net_device *dev;
27351da177e4SLinus Torvalds 
273624ef0da7SThomas Graf 	if (plen > 128)
273724ef0da7SThomas Graf 		return -EINVAL;
273824ef0da7SThomas Graf 
2739af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2740af284937SDaniel Lezcano 	if (!dev)
27411da177e4SLinus Torvalds 		return -ENODEV;
27421da177e4SLinus Torvalds 
2743e5d08d71SIan Morris 	idev = __in6_dev_get(dev);
274463159f29SIan Morris 	if (!idev)
27451da177e4SLinus Torvalds 		return -ENXIO;
27461da177e4SLinus Torvalds 
27471da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2748502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
27491da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
27501da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
27511da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
27521da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
27531da177e4SLinus Torvalds 
27546046d5b4SHeiner Kallweit 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
27556046d5b4SHeiner Kallweit 			    (ifa_flags & IFA_F_MANAGETEMPADDR))
27566046d5b4SHeiner Kallweit 				manage_tempaddrs(idev, ifp, 0, 0, false,
27576046d5b4SHeiner Kallweit 						 jiffies);
27581da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
27596046d5b4SHeiner Kallweit 			addrconf_verify_rtnl();
276093a714d6SMadhu Challa 			if (ipv6_addr_is_multicast(pfx)) {
276193a714d6SMadhu Challa 				ipv6_mc_config(net->ipv6.mc_autojoin_sk,
276293a714d6SMadhu Challa 					       false, pfx, dev->ifindex);
276393a714d6SMadhu Challa 			}
27641da177e4SLinus Torvalds 			return 0;
27651da177e4SLinus Torvalds 		}
27661da177e4SLinus Torvalds 	}
27671da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
27681da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
27691da177e4SLinus Torvalds }
27701da177e4SLinus Torvalds 
27711da177e4SLinus Torvalds 
2772af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
27731da177e4SLinus Torvalds {
27741da177e4SLinus Torvalds 	struct in6_ifreq ireq;
27751da177e4SLinus Torvalds 	int err;
27761da177e4SLinus Torvalds 
2777af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
27781da177e4SLinus Torvalds 		return -EPERM;
27791da177e4SLinus Torvalds 
27801da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
27811da177e4SLinus Torvalds 		return -EFAULT;
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds 	rtnl_lock();
2784caeaba79SNicolas Dichtel 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2785af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2786af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
27871da177e4SLinus Torvalds 	rtnl_unlock();
27881da177e4SLinus Torvalds 	return err;
27891da177e4SLinus Torvalds }
27901da177e4SLinus Torvalds 
2791af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
27921da177e4SLinus Torvalds {
27931da177e4SLinus Torvalds 	struct in6_ifreq ireq;
27941da177e4SLinus Torvalds 	int err;
27951da177e4SLinus Torvalds 
2796af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
27971da177e4SLinus Torvalds 		return -EPERM;
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
28001da177e4SLinus Torvalds 		return -EFAULT;
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds 	rtnl_lock();
28036046d5b4SHeiner Kallweit 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2804af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
28051da177e4SLinus Torvalds 	rtnl_unlock();
28061da177e4SLinus Torvalds 	return err;
28071da177e4SLinus Torvalds }
28081da177e4SLinus Torvalds 
2809b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2810b5f348e5SIlpo Järvinen 		     int plen, int scope)
2811b5f348e5SIlpo Järvinen {
2812b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
2813b5f348e5SIlpo Järvinen 
28148a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, addr, NULL, plen,
281507edd741SHannes Frederic Sowa 			    scope, IFA_F_PERMANENT,
281607edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2817b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
2818b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
2819b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
2820b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
2821b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2822b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
2823b5f348e5SIlpo Järvinen 	}
2824b5f348e5SIlpo Järvinen }
2825b5f348e5SIlpo Järvinen 
282607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
28271da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
28281da177e4SLinus Torvalds {
28291da177e4SLinus Torvalds 	struct in6_addr addr;
28301da177e4SLinus Torvalds 	struct net_device *dev;
2831c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
2832929c9cf3SNicolas Dichtel 	int scope, plen;
2833f0e2acfaSNicolas Dichtel 	u32 pflags = 0;
28341da177e4SLinus Torvalds 
28351da177e4SLinus Torvalds 	ASSERT_RTNL();
28361da177e4SLinus Torvalds 
28371da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
28381da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
28391da177e4SLinus Torvalds 
28401da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
28411da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
28421da177e4SLinus Torvalds 		scope = IFA_LINK;
2843929c9cf3SNicolas Dichtel 		plen = 64;
28441da177e4SLinus Torvalds 	} else {
28451da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
2846929c9cf3SNicolas Dichtel 		plen = 96;
2847f0e2acfaSNicolas Dichtel 		pflags |= RTF_NONEXTHOP;
28481da177e4SLinus Torvalds 	}
28491da177e4SLinus Torvalds 
28501da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
2851929c9cf3SNicolas Dichtel 		add_addr(idev, &addr, plen, scope);
2852f0e2acfaSNicolas Dichtel 		addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
28531da177e4SLinus Torvalds 		return;
28541da177e4SLinus Torvalds 	}
28551da177e4SLinus Torvalds 
28566fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
2857e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
28581da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
28591da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
28601da177e4SLinus Torvalds 
28611da177e4SLinus Torvalds 			int flag = scope;
28621da177e4SLinus Torvalds 
28631da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
28641da177e4SLinus Torvalds 
28651da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
28681da177e4SLinus Torvalds 					continue;
28691da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
28701da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
28711da177e4SLinus Torvalds 						continue;
28721da177e4SLinus Torvalds 					flag |= IFA_HOST;
28731da177e4SLinus Torvalds 				}
28741da177e4SLinus Torvalds 
2875b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
2876f0e2acfaSNicolas Dichtel 				addrconf_prefix_route(&addr, plen, idev->dev, 0,
2877f0e2acfaSNicolas Dichtel 						      pflags);
28781da177e4SLinus Torvalds 			}
28791da177e4SLinus Torvalds 		}
28801da177e4SLinus Torvalds 	}
28811da177e4SLinus Torvalds }
28820be669bbSJoerg Roedel #endif
28831da177e4SLinus Torvalds 
28841da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
28851da177e4SLinus Torvalds {
28861da177e4SLinus Torvalds 	struct inet6_dev  *idev;
288725fb6ca4SBalakumaran Kannan 	struct net_device *sp_dev;
288825fb6ca4SBalakumaran Kannan 	struct inet6_ifaddr *sp_ifa;
288925fb6ca4SBalakumaran Kannan 	struct rt6_info *sp_rt;
28901da177e4SLinus Torvalds 
28911da177e4SLinus Torvalds 	/* ::1 */
28921da177e4SLinus Torvalds 
28931da177e4SLinus Torvalds 	ASSERT_RTNL();
28941da177e4SLinus Torvalds 
2895e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
289663159f29SIan Morris 	if (!idev) {
289791df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
28981da177e4SLinus Torvalds 		return;
28991da177e4SLinus Torvalds 	}
29001da177e4SLinus Torvalds 
2901b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
290225fb6ca4SBalakumaran Kannan 
290325fb6ca4SBalakumaran Kannan 	/* Add routes to other interface's IPv6 addresses */
290425fb6ca4SBalakumaran Kannan 	for_each_netdev(dev_net(dev), sp_dev) {
290525fb6ca4SBalakumaran Kannan 		if (!strcmp(sp_dev->name, dev->name))
290625fb6ca4SBalakumaran Kannan 			continue;
290725fb6ca4SBalakumaran Kannan 
290825fb6ca4SBalakumaran Kannan 		idev = __in6_dev_get(sp_dev);
290925fb6ca4SBalakumaran Kannan 		if (!idev)
291025fb6ca4SBalakumaran Kannan 			continue;
291125fb6ca4SBalakumaran Kannan 
291225fb6ca4SBalakumaran Kannan 		read_lock_bh(&idev->lock);
291325fb6ca4SBalakumaran Kannan 		list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
291425fb6ca4SBalakumaran Kannan 
291525fb6ca4SBalakumaran Kannan 			if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
291625fb6ca4SBalakumaran Kannan 				continue;
291725fb6ca4SBalakumaran Kannan 
291833d99113SGao feng 			if (sp_ifa->rt) {
291933d99113SGao feng 				/* This dst has been added to garbage list when
292033d99113SGao feng 				 * lo device down, release this obsolete dst and
292133d99113SGao feng 				 * reallocate a new router for ifa.
292233d99113SGao feng 				 */
292333d99113SGao feng 				if (sp_ifa->rt->dst.obsolete > 0) {
292433d99113SGao feng 					ip6_rt_put(sp_ifa->rt);
292533d99113SGao feng 					sp_ifa->rt = NULL;
292633d99113SGao feng 				} else {
2927a881ae1fSGao feng 					continue;
292833d99113SGao feng 				}
292933d99113SGao feng 			}
2930a881ae1fSGao feng 
293157ec0afeSFrançois-Xavier Le Bail 			sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
293225fb6ca4SBalakumaran Kannan 
293325fb6ca4SBalakumaran Kannan 			/* Failure cases are ignored */
2934534c8779SGao feng 			if (!IS_ERR(sp_rt)) {
2935534c8779SGao feng 				sp_ifa->rt = sp_rt;
293625fb6ca4SBalakumaran Kannan 				ip6_ins_rt(sp_rt);
293725fb6ca4SBalakumaran Kannan 			}
2938534c8779SGao feng 		}
293925fb6ca4SBalakumaran Kannan 		read_unlock_bh(&idev->lock);
294025fb6ca4SBalakumaran Kannan 	}
29411da177e4SLinus Torvalds }
29421da177e4SLinus Torvalds 
294364236f3fSHannes Frederic Sowa static void addrconf_add_linklocal(struct inet6_dev *idev,
294464236f3fSHannes Frederic Sowa 				   const struct in6_addr *addr, u32 flags)
29451da177e4SLinus Torvalds {
29461da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
294764236f3fSHannes Frederic Sowa 	u32 addr_flags = flags | IFA_F_PERMANENT;
29481da177e4SLinus Torvalds 
294995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
295095c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
2951702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
295295c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
295395c385b4SNeil Horman #endif
295495c385b4SNeil Horman 
295507edd741SHannes Frederic Sowa 	ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
295607edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
29571da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
295846d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2959cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
29601da177e4SLinus Torvalds 		in6_ifa_put(ifp);
29611da177e4SLinus Torvalds 	}
29621da177e4SLinus Torvalds }
29631da177e4SLinus Torvalds 
2964622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address)
2965622c81d5SHannes Frederic Sowa {
2966622c81d5SHannes Frederic Sowa 	if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
2967622c81d5SHannes Frederic Sowa 		return true;
2968622c81d5SHannes Frederic Sowa 
2969622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0x02005eff) &&
2970622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
2971622c81d5SHannes Frederic Sowa 		return true;
2972622c81d5SHannes Frederic Sowa 
2973622c81d5SHannes Frederic Sowa 	if (address.s6_addr32[2] == htonl(0xfdffffff) &&
2974622c81d5SHannes Frederic Sowa 	    ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
2975622c81d5SHannes Frederic Sowa 		return true;
2976622c81d5SHannes Frederic Sowa 
2977622c81d5SHannes Frederic Sowa 	return false;
2978622c81d5SHannes Frederic Sowa }
2979622c81d5SHannes Frederic Sowa 
2980622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address,
2981622c81d5SHannes Frederic Sowa 					u8 dad_count,
2982622c81d5SHannes Frederic Sowa 					const struct inet6_dev *idev)
2983622c81d5SHannes Frederic Sowa {
2984622c81d5SHannes Frederic Sowa 	static DEFINE_SPINLOCK(lock);
2985622c81d5SHannes Frederic Sowa 	static __u32 digest[SHA_DIGEST_WORDS];
2986622c81d5SHannes Frederic Sowa 	static __u32 workspace[SHA_WORKSPACE_WORDS];
2987622c81d5SHannes Frederic Sowa 
2988622c81d5SHannes Frederic Sowa 	static union {
2989622c81d5SHannes Frederic Sowa 		char __data[SHA_MESSAGE_BYTES];
2990622c81d5SHannes Frederic Sowa 		struct {
2991622c81d5SHannes Frederic Sowa 			struct in6_addr secret;
2992ff40217eSHannes Frederic Sowa 			__be32 prefix[2];
2993622c81d5SHannes Frederic Sowa 			unsigned char hwaddr[MAX_ADDR_LEN];
2994622c81d5SHannes Frederic Sowa 			u8 dad_count;
2995622c81d5SHannes Frederic Sowa 		} __packed;
2996622c81d5SHannes Frederic Sowa 	} data;
2997622c81d5SHannes Frederic Sowa 
2998622c81d5SHannes Frederic Sowa 	struct in6_addr secret;
2999622c81d5SHannes Frederic Sowa 	struct in6_addr temp;
3000622c81d5SHannes Frederic Sowa 	struct net *net = dev_net(idev->dev);
3001622c81d5SHannes Frederic Sowa 
3002622c81d5SHannes Frederic Sowa 	BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3003622c81d5SHannes Frederic Sowa 
3004622c81d5SHannes Frederic Sowa 	if (idev->cnf.stable_secret.initialized)
3005622c81d5SHannes Frederic Sowa 		secret = idev->cnf.stable_secret.secret;
3006622c81d5SHannes Frederic Sowa 	else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3007622c81d5SHannes Frederic Sowa 		secret = net->ipv6.devconf_dflt->stable_secret.secret;
3008622c81d5SHannes Frederic Sowa 	else
3009622c81d5SHannes Frederic Sowa 		return -1;
3010622c81d5SHannes Frederic Sowa 
3011622c81d5SHannes Frederic Sowa retry:
3012622c81d5SHannes Frederic Sowa 	spin_lock_bh(&lock);
3013622c81d5SHannes Frederic Sowa 
3014622c81d5SHannes Frederic Sowa 	sha_init(digest);
3015622c81d5SHannes Frederic Sowa 	memset(&data, 0, sizeof(data));
3016622c81d5SHannes Frederic Sowa 	memset(workspace, 0, sizeof(workspace));
3017622c81d5SHannes Frederic Sowa 	memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3018ff40217eSHannes Frederic Sowa 	data.prefix[0] = address->s6_addr32[0];
3019ff40217eSHannes Frederic Sowa 	data.prefix[1] = address->s6_addr32[1];
3020622c81d5SHannes Frederic Sowa 	data.secret = secret;
3021622c81d5SHannes Frederic Sowa 	data.dad_count = dad_count;
3022622c81d5SHannes Frederic Sowa 
3023622c81d5SHannes Frederic Sowa 	sha_transform(digest, data.__data, workspace);
3024622c81d5SHannes Frederic Sowa 
3025622c81d5SHannes Frederic Sowa 	temp = *address;
3026ff40217eSHannes Frederic Sowa 	temp.s6_addr32[2] = (__force __be32)digest[0];
3027ff40217eSHannes Frederic Sowa 	temp.s6_addr32[3] = (__force __be32)digest[1];
3028622c81d5SHannes Frederic Sowa 
3029622c81d5SHannes Frederic Sowa 	spin_unlock_bh(&lock);
3030622c81d5SHannes Frederic Sowa 
3031622c81d5SHannes Frederic Sowa 	if (ipv6_reserved_interfaceid(temp)) {
3032622c81d5SHannes Frederic Sowa 		dad_count++;
30331855b7c3SHannes Frederic Sowa 		if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3034622c81d5SHannes Frederic Sowa 			return -1;
3035622c81d5SHannes Frederic Sowa 		goto retry;
3036622c81d5SHannes Frederic Sowa 	}
3037622c81d5SHannes Frederic Sowa 
3038622c81d5SHannes Frederic Sowa 	*address = temp;
3039622c81d5SHannes Frederic Sowa 	return 0;
3040622c81d5SHannes Frederic Sowa }
3041622c81d5SHannes Frederic Sowa 
3042cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3043cc9da6ccSBjørn Mork {
3044cc9da6ccSBjørn Mork 	struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3045cc9da6ccSBjørn Mork 
3046cc9da6ccSBjørn Mork 	if (s->initialized)
3047cc9da6ccSBjørn Mork 		return;
3048cc9da6ccSBjørn Mork 	s = &idev->cnf.stable_secret;
3049cc9da6ccSBjørn Mork 	get_random_bytes(&s->secret, sizeof(s->secret));
3050cc9da6ccSBjørn Mork 	s->initialized = true;
3051cc9da6ccSBjørn Mork }
3052cc9da6ccSBjørn Mork 
3053bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3054bc91b0f0SJiri Pirko {
3055bc91b0f0SJiri Pirko 	struct in6_addr addr;
3056bc91b0f0SJiri Pirko 
3057ca254490SDavid Ahern 	/* no link local addresses on L3 master devices */
3058ca254490SDavid Ahern 	if (netif_is_l3_master(idev->dev))
3059ca254490SDavid Ahern 		return;
3060ca254490SDavid Ahern 
3061bc91b0f0SJiri Pirko 	ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3062622c81d5SHannes Frederic Sowa 
3063cc9da6ccSBjørn Mork 	switch (idev->addr_gen_mode) {
3064cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_RANDOM:
3065cc9da6ccSBjørn Mork 		ipv6_gen_mode_random_init(idev);
3066cc9da6ccSBjørn Mork 		/* fallthrough */
3067cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3068622c81d5SHannes Frederic Sowa 		if (!ipv6_generate_stable_address(&addr, 0, idev))
306964236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr,
307064236f3fSHannes Frederic Sowa 					       IFA_F_STABLE_PRIVACY);
3071622c81d5SHannes Frederic Sowa 		else if (prefix_route)
3072622c81d5SHannes Frederic Sowa 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3073cc9da6ccSBjørn Mork 		break;
3074cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_EUI64:
3075bc91b0f0SJiri Pirko 		/* addrconf_add_linklocal also adds a prefix_route and we
3076bc91b0f0SJiri Pirko 		 * only need to care about prefix routes if ipv6_generate_eui64
3077bc91b0f0SJiri Pirko 		 * couldn't generate one.
3078bc91b0f0SJiri Pirko 		 */
3079bc91b0f0SJiri Pirko 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
308064236f3fSHannes Frederic Sowa 			addrconf_add_linklocal(idev, &addr, 0);
3081bc91b0f0SJiri Pirko 		else if (prefix_route)
3082bc91b0f0SJiri Pirko 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3083cc9da6ccSBjørn Mork 		break;
3084cc9da6ccSBjørn Mork 	case IN6_ADDR_GEN_MODE_NONE:
3085cc9da6ccSBjørn Mork 	default:
3086cc9da6ccSBjørn Mork 		/* will not add any link local address */
3087cc9da6ccSBjørn Mork 		break;
3088bc91b0f0SJiri Pirko 	}
3089bc91b0f0SJiri Pirko }
3090bc91b0f0SJiri Pirko 
30911da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
30921da177e4SLinus Torvalds {
30931da177e4SLinus Torvalds 	struct inet6_dev *idev;
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds 	ASSERT_RTNL();
30961da177e4SLinus Torvalds 
309774235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
309874235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
309974235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
310006a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
3101e837735eSNicolas Dichtel 	    (dev->type != ARPHRD_IEEE1394) &&
3102e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_TUNNEL6) &&
3103cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_6LOWPAN) &&
3104cc9da6ccSBjørn Mork 	    (dev->type != ARPHRD_NONE)) {
310574235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
310674235a25SHerbert Xu 		return;
310774235a25SHerbert Xu 	}
310874235a25SHerbert Xu 
31091da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
311064e724f6SBrian Haley 	if (IS_ERR(idev))
31111da177e4SLinus Torvalds 		return;
31121da177e4SLinus Torvalds 
3113cc9da6ccSBjørn Mork 	/* this device type has no EUI support */
3114cc9da6ccSBjørn Mork 	if (dev->type == ARPHRD_NONE &&
3115cc9da6ccSBjørn Mork 	    idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3116cc9da6ccSBjørn Mork 		idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3117cc9da6ccSBjørn Mork 
3118bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, false);
31191da177e4SLinus Torvalds }
31201da177e4SLinus Torvalds 
312107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
31221da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
31231da177e4SLinus Torvalds {
31241da177e4SLinus Torvalds 	struct inet6_dev *idev;
31251da177e4SLinus Torvalds 
31261da177e4SLinus Torvalds 	ASSERT_RTNL();
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds 	/*
31291da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
31301da177e4SLinus Torvalds 	 * addresses... we should configure all of
31311da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
31321da177e4SLinus Torvalds 	 */
31331da177e4SLinus Torvalds 
3134e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
313563159f29SIan Morris 	if (!idev) {
313691df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
31371da177e4SLinus Torvalds 		return;
31381da177e4SLinus Torvalds 	}
31391da177e4SLinus Torvalds 
3140c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
3141bc91b0f0SJiri Pirko 		addrconf_addr_gen(idev, false);
3142c7dc89c0SFred L. Templin 		return;
3143c7dc89c0SFred L. Templin 	}
3144c7dc89c0SFred L. Templin 
31451da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
31461da177e4SLinus Torvalds 
314762b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
31481da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
31491da177e4SLinus Torvalds }
31500be669bbSJoerg Roedel #endif
31511da177e4SLinus Torvalds 
315207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3153aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
3154aee80b54Sstephen hemminger {
3155aee80b54Sstephen hemminger 	struct inet6_dev *idev;
3156aee80b54Sstephen hemminger 
3157aee80b54Sstephen hemminger 	ASSERT_RTNL();
3158aee80b54Sstephen hemminger 
3159e5d08d71SIan Morris 	idev = ipv6_find_idev(dev);
316063159f29SIan Morris 	if (!idev) {
316191df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
3162aee80b54Sstephen hemminger 		return;
3163aee80b54Sstephen hemminger 	}
3164aee80b54Sstephen hemminger 
3165bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, true);
3166d9e4ce65SHannes Frederic Sowa 	if (dev->flags & IFF_POINTOPOINT)
3167d9e4ce65SHannes Frederic Sowa 		addrconf_add_mroute(dev);
3168aee80b54Sstephen hemminger }
3169aee80b54Sstephen hemminger #endif
3170aee80b54Sstephen hemminger 
31711da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
3172351638e7SJiri Pirko 			   void *ptr)
31731da177e4SLinus Torvalds {
3174351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3175748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
3176c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
3177b217d616SHerbert Xu 	int err;
31781da177e4SLinus Torvalds 
31791da177e4SLinus Torvalds 	switch (event) {
318045ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
318174235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
318245ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
3183a317a2f1SWANG Cong 			if (IS_ERR(idev))
3184a317a2f1SWANG Cong 				return notifier_from_errno(PTR_ERR(idev));
318545ba9dd2SYOSHIFUJI Hideaki 		}
318645ba9dd2SYOSHIFUJI Hideaki 		break;
3187bcdd553fSStephen Hemminger 
3188b7b0b1d2SAlexander Duyck 	case NETDEV_CHANGEMTU:
3189b7b0b1d2SAlexander Duyck 		/* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3190b7b0b1d2SAlexander Duyck 		if (dev->mtu < IPV6_MIN_MTU) {
3191b7b0b1d2SAlexander Duyck 			addrconf_ifdown(dev, 1);
3192b7b0b1d2SAlexander Duyck 			break;
3193b7b0b1d2SAlexander Duyck 		}
3194b7b0b1d2SAlexander Duyck 
3195b7b0b1d2SAlexander Duyck 		if (idev) {
3196b7b0b1d2SAlexander Duyck 			rt6_mtu_change(dev, dev->mtu);
3197b7b0b1d2SAlexander Duyck 			idev->cnf.mtu6 = dev->mtu;
3198b7b0b1d2SAlexander Duyck 			break;
3199b7b0b1d2SAlexander Duyck 		}
3200b7b0b1d2SAlexander Duyck 
3201b7b0b1d2SAlexander Duyck 		/* allocate new idev */
3202b7b0b1d2SAlexander Duyck 		idev = ipv6_add_dev(dev);
3203b7b0b1d2SAlexander Duyck 		if (IS_ERR(idev))
3204b7b0b1d2SAlexander Duyck 			break;
3205b7b0b1d2SAlexander Duyck 
3206b7b0b1d2SAlexander Duyck 		/* device is still not ready */
3207b7b0b1d2SAlexander Duyck 		if (!(idev->if_flags & IF_READY))
3208b7b0b1d2SAlexander Duyck 			break;
3209b7b0b1d2SAlexander Duyck 
3210b7b0b1d2SAlexander Duyck 		run_pending = 1;
3211b7b0b1d2SAlexander Duyck 
3212b7b0b1d2SAlexander Duyck 		/* fall through */
3213b7b0b1d2SAlexander Duyck 
32141da177e4SLinus Torvalds 	case NETDEV_UP:
32153c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
3216c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
3217c2edacf8SJay Vosburgh 			break;
3218c2edacf8SJay Vosburgh 
32193ce62a84SWANG Cong 		if (idev && idev->cnf.disable_ipv6)
32203ce62a84SWANG Cong 			break;
32213ce62a84SWANG Cong 
32223c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
3223f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
32243c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
3225f3213831SJoe Perches 				pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
32263c21edbdSYOSHIFUJI Hideaki 					dev->name);
32273c21edbdSYOSHIFUJI Hideaki 				break;
32283c21edbdSYOSHIFUJI Hideaki 			}
322999081049SKristian Slavov 
3230d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
3231d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
3232d31c7b8fSEvgeniy Polyakov 
3233a317a2f1SWANG Cong 			if (!IS_ERR_OR_NULL(idev)) {
323499081049SKristian Slavov 				idev->if_flags |= IF_READY;
3235e3ec6cfcSBenjamin Thery 				run_pending = 1;
3236e3ec6cfcSBenjamin Thery 			}
3237b7b0b1d2SAlexander Duyck 		} else if (event == NETDEV_CHANGE) {
3238f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
32393c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
32403c21edbdSYOSHIFUJI Hideaki 				break;
32413c21edbdSYOSHIFUJI Hideaki 			}
32423c21edbdSYOSHIFUJI Hideaki 
32433c21edbdSYOSHIFUJI Hideaki 			if (idev) {
3244bcdd553fSStephen Hemminger 				if (idev->if_flags & IF_READY)
32453c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
32463c21edbdSYOSHIFUJI Hideaki 					break;
32473c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
32483c21edbdSYOSHIFUJI Hideaki 			}
32493c21edbdSYOSHIFUJI Hideaki 
3250f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
32513c21edbdSYOSHIFUJI Hideaki 				dev->name);
32523c21edbdSYOSHIFUJI Hideaki 
3253c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
32543c21edbdSYOSHIFUJI Hideaki 		}
32553c21edbdSYOSHIFUJI Hideaki 
32561da177e4SLinus Torvalds 		switch (dev->type) {
325707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
32581da177e4SLinus Torvalds 		case ARPHRD_SIT:
32591da177e4SLinus Torvalds 			addrconf_sit_config(dev);
32601da177e4SLinus Torvalds 			break;
32610be669bbSJoerg Roedel #endif
326207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
3263aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
3264aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
3265aee80b54Sstephen hemminger 			break;
3266aee80b54Sstephen hemminger #endif
32671da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
32681da177e4SLinus Torvalds 			init_loopback(dev);
32691da177e4SLinus Torvalds 			break;
32701da177e4SLinus Torvalds 
32711da177e4SLinus Torvalds 		default:
32721da177e4SLinus Torvalds 			addrconf_dev_config(dev);
32731da177e4SLinus Torvalds 			break;
32743ff50b79SStephen Hemminger 		}
3275bcdd553fSStephen Hemminger 
3276a317a2f1SWANG Cong 		if (!IS_ERR_OR_NULL(idev)) {
3277c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
3278c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
3279c5e33bddSYOSHIFUJI Hideaki 
3280bcdd553fSStephen Hemminger 			/*
3281bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
3282bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
3283bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
32841da177e4SLinus Torvalds 			 */
3285bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
3286bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
32871da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
32881da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
32891da177e4SLinus Torvalds 			}
32901da177e4SLinus Torvalds 			idev->tstamp = jiffies;
32911da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
3292bcdd553fSStephen Hemminger 
3293bcdd553fSStephen Hemminger 			/*
3294bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
3295bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
32961da177e4SLinus Torvalds 			 */
32971da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
3298bcdd553fSStephen Hemminger 				addrconf_ifdown(dev, 1);
32991da177e4SLinus Torvalds 		}
33001da177e4SLinus Torvalds 		break;
33011da177e4SLinus Torvalds 
33021da177e4SLinus Torvalds 	case NETDEV_DOWN:
33031da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
33041da177e4SLinus Torvalds 		/*
33051da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
33061da177e4SLinus Torvalds 		 */
33071da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
33081da177e4SLinus Torvalds 		break;
33093c21edbdSYOSHIFUJI Hideaki 
33101da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
33111da177e4SLinus Torvalds 		if (idev) {
33125632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
3313408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
3314a317a2f1SWANG Cong 			err = addrconf_sysctl_register(idev);
3315b217d616SHerbert Xu 			if (err)
3316b217d616SHerbert Xu 				return notifier_from_errno(err);
3317a317a2f1SWANG Cong 			err = snmp6_register_dev(idev);
3318a317a2f1SWANG Cong 			if (err) {
3319a317a2f1SWANG Cong 				addrconf_sysctl_unregister(idev);
3320a317a2f1SWANG Cong 				return notifier_from_errno(err);
3321a317a2f1SWANG Cong 			}
33225632c515SStephen Hemminger 		}
33231da177e4SLinus Torvalds 		break;
3324bcdd553fSStephen Hemminger 
332593d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
332693d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
33273ef0952cSAndrew Lunn 		if (idev)
332893d9b7d7SJiri Pirko 			addrconf_type_change(dev, event);
332975c78500SMoni Shoua 		break;
33303ff50b79SStephen Hemminger 	}
33311da177e4SLinus Torvalds 
33321da177e4SLinus Torvalds 	return NOTIFY_OK;
33331da177e4SLinus Torvalds }
33341da177e4SLinus Torvalds 
33351da177e4SLinus Torvalds /*
33361da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
33371da177e4SLinus Torvalds  */
33381da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
33391da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
33401da177e4SLinus Torvalds };
33411da177e4SLinus Torvalds 
334293d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
334375c78500SMoni Shoua {
334475c78500SMoni Shoua 	struct inet6_dev *idev;
334575c78500SMoni Shoua 	ASSERT_RTNL();
334675c78500SMoni Shoua 
334775c78500SMoni Shoua 	idev = __in6_dev_get(dev);
334875c78500SMoni Shoua 
334993d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
335075c78500SMoni Shoua 		ipv6_mc_remap(idev);
335193d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
335275c78500SMoni Shoua 		ipv6_mc_unmap(idev);
335375c78500SMoni Shoua }
335475c78500SMoni Shoua 
33551da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
33561da177e4SLinus Torvalds {
3357c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
3358502a2ffdSstephen hemminger 	struct inet6_dev *idev;
3359502a2ffdSstephen hemminger 	struct inet6_ifaddr *ifa;
336073a8bd74SDavid S. Miller 	int state, i;
33611da177e4SLinus Torvalds 
33621da177e4SLinus Torvalds 	ASSERT_RTNL();
33631da177e4SLinus Torvalds 
3364f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
336573a8bd74SDavid S. Miller 	neigh_ifdown(&nd_tbl, dev);
33661da177e4SLinus Torvalds 
33671da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
336863159f29SIan Morris 	if (!idev)
33691da177e4SLinus Torvalds 		return -ENODEV;
33701da177e4SLinus Torvalds 
3371bcdd553fSStephen Hemminger 	/*
3372bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3373bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
33741da177e4SLinus Torvalds 	 */
3375439e2385SDenis V. Lunev 	if (how) {
33761da177e4SLinus Torvalds 		idev->dead = 1;
33778814c4b5SYOSHIFUJI Hideaki 
33788814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3379a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
33801da177e4SLinus Torvalds 
33811da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
33821da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
33831da177e4SLinus Torvalds 
33841da177e4SLinus Torvalds 	}
33851da177e4SLinus Torvalds 
338673a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
338773a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
338873a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
338973a8bd74SDavid S. Miller 
339073a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
339173a8bd74SDavid S. Miller restart:
3392b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
339373a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
339473a8bd74SDavid S. Miller 				hlist_del_init_rcu(&ifa->addr_lst);
3395c15b1ccaSHannes Frederic Sowa 				addrconf_del_dad_work(ifa);
339673a8bd74SDavid S. Miller 				goto restart;
339773a8bd74SDavid S. Miller 			}
339873a8bd74SDavid S. Miller 		}
339973a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
340073a8bd74SDavid S. Miller 	}
340173a8bd74SDavid S. Miller 
34021da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
34031da177e4SLinus Torvalds 
3404b7b1bfceSHannes Frederic Sowa 	addrconf_del_rs_timer(idev);
3405b7b1bfceSHannes Frederic Sowa 
3406bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3407439e2385SDenis V. Lunev 	if (!how)
34083c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
34091da177e4SLinus Torvalds 
3410439e2385SDenis V. Lunev 	if (how && del_timer(&idev->regen_timer))
34111da177e4SLinus Torvalds 		in6_dev_put(idev);
34121da177e4SLinus Torvalds 
3413bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3414372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3415372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3416372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3417372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
34181da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
34191da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
34201da177e4SLinus Torvalds 
34211da177e4SLinus Torvalds 		if (ifa->ifpub) {
34221da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
34231da177e4SLinus Torvalds 			ifa->ifpub = NULL;
34241da177e4SLinus Torvalds 		}
34251da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
34261da177e4SLinus Torvalds 		in6_ifa_put(ifa);
34271da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
34281da177e4SLinus Torvalds 	}
34298f37ada5Sstephen hemminger 
3430502a2ffdSstephen hemminger 	while (!list_empty(&idev->addr_list)) {
3431502a2ffdSstephen hemminger 		ifa = list_first_entry(&idev->addr_list,
3432502a2ffdSstephen hemminger 				       struct inet6_ifaddr, if_list);
3433c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifa);
343484e8b803Sstephen hemminger 
3435502a2ffdSstephen hemminger 		list_del(&ifa->if_list);
34361da177e4SLinus Torvalds 
34374c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
343873a8bd74SDavid S. Miller 
34398e8e676dSHannes Frederic Sowa 		spin_lock_bh(&ifa->lock);
34404c5ff6a6SHerbert Xu 		state = ifa->state;
34414c5ff6a6SHerbert Xu 		ifa->state = INET6_IFADDR_STATE_DEAD;
34428e8e676dSHannes Frederic Sowa 		spin_unlock_bh(&ifa->lock);
34434c5ff6a6SHerbert Xu 
34449d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
34451da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
3446f88c91ddSCong Wang 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
34472de79570SLorenzo Colitti 		}
34489d82ca98SJohn Fastabend 		in6_ifa_put(ifa);
344973a8bd74SDavid S. Miller 
34501da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
34511da177e4SLinus Torvalds 	}
34528f37ada5Sstephen hemminger 
34531da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
34541da177e4SLinus Torvalds 
3455381f4dcaSSabrina Dubroca 	/* Step 5: Discard anycast and multicast list */
3456381f4dcaSSabrina Dubroca 	if (how) {
3457381f4dcaSSabrina Dubroca 		ipv6_ac_destroy_dev(idev);
34581da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
3459381f4dcaSSabrina Dubroca 	} else {
34601da177e4SLinus Torvalds 		ipv6_mc_down(idev);
3461381f4dcaSSabrina Dubroca 	}
34621da177e4SLinus Torvalds 
34631da177e4SLinus Torvalds 	idev->tstamp = jiffies;
34641da177e4SLinus Torvalds 
3465bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3466439e2385SDenis V. Lunev 	if (how) {
3467408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
34681da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
34691da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
34701da177e4SLinus Torvalds 		in6_dev_put(idev);
34711da177e4SLinus Torvalds 	}
34721da177e4SLinus Torvalds 	return 0;
34731da177e4SLinus Torvalds }
34741da177e4SLinus Torvalds 
34751da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
34761da177e4SLinus Torvalds {
3477b7b1bfceSHannes Frederic Sowa 	struct inet6_dev *idev = (struct inet6_dev *)data;
3478caf92bc4SCong Wang 	struct net_device *dev = idev->dev;
3479b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
34801da177e4SLinus Torvalds 
3481b7b1bfceSHannes Frederic Sowa 	write_lock(&idev->lock);
34825b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
34831da177e4SLinus Torvalds 		goto out;
34841da177e4SLinus Torvalds 
34859ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
34861da177e4SLinus Torvalds 		goto out;
34875b2a1953Sstephen hemminger 
34885b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
34895b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
34905b2a1953Sstephen hemminger 		goto out;
34911da177e4SLinus Torvalds 
3492b7b1bfceSHannes Frederic Sowa 	if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
3493caf92bc4SCong Wang 		write_unlock(&idev->lock);
3494caf92bc4SCong Wang 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3495caf92bc4SCong Wang 			ndisc_send_rs(dev, &lladdr,
3496b7b1bfceSHannes Frederic Sowa 				      &in6addr_linklocal_allrouters);
3497b7b1bfceSHannes Frederic Sowa 		else
3498caf92bc4SCong Wang 			goto put;
3499b7b1bfceSHannes Frederic Sowa 
3500caf92bc4SCong Wang 		write_lock(&idev->lock);
35011da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
3502b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3503b7b1bfceSHannes Frederic Sowa 					     idev->cnf.rtr_solicits) ?
35045b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_delay :
35055b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_interval);
35061da177e4SLinus Torvalds 	} else {
35071da177e4SLinus Torvalds 		/*
35081da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
35091da177e4SLinus Torvalds 		 * assumption any longer.
35101da177e4SLinus Torvalds 		 */
351191df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
35121da177e4SLinus Torvalds 	}
35131da177e4SLinus Torvalds 
35141da177e4SLinus Torvalds out:
3515b7b1bfceSHannes Frederic Sowa 	write_unlock(&idev->lock);
3516caf92bc4SCong Wang put:
3517b7b1bfceSHannes Frederic Sowa 	in6_dev_put(idev);
35181da177e4SLinus Torvalds }
35191da177e4SLinus Torvalds 
35201da177e4SLinus Torvalds /*
35211da177e4SLinus Torvalds  *	Duplicate Address Detection
35221da177e4SLinus Torvalds  */
35233c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
35243c21edbdSYOSHIFUJI Hideaki {
35253c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
35263c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
35273c21edbdSYOSHIFUJI Hideaki 
352895c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
352995c385b4SNeil Horman 		rand_num = 0;
353095c385b4SNeil Horman 	else
353163862b5bSAruna-Hewapathirane 		rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
353295c385b4SNeil Horman 
3533b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes = idev->cnf.dad_transmits;
3534c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, rand_num);
35353c21edbdSYOSHIFUJI Hideaki }
35363c21edbdSYOSHIFUJI Hideaki 
3537c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
35381da177e4SLinus Torvalds {
35391da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
35401da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
354116186a82Ssubashab@codeaurora.org 	bool notify = false;
35421da177e4SLinus Torvalds 
35431da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
35441da177e4SLinus Torvalds 
354563862b5bSAruna-Hewapathirane 	prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
35461da177e4SLinus Torvalds 
35471da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3548622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3549e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
35501da177e4SLinus Torvalds 		goto out;
35511da177e4SLinus Torvalds 
35521da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
35531b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
355455ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
355555ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3556cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
355721809fafSstephen hemminger 		spin_unlock(&ifp->lock);
35581da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
35591da177e4SLinus Torvalds 
35601da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
35611da177e4SLinus Torvalds 		return;
35621da177e4SLinus Torvalds 	}
35631da177e4SLinus Torvalds 
35646732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
356521809fafSstephen hemminger 		spin_unlock(&ifp->lock);
35666732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
35673c21edbdSYOSHIFUJI Hideaki 		/*
3568590a9887SMasatake YAMATO 		 * If the device is not ready:
35693c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
35703c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
35713c21edbdSYOSHIFUJI Hideaki 		 */
35723c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
3573cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
35746732badeSYOSHIFUJI Hideaki 		return;
35753c21edbdSYOSHIFUJI Hideaki 	}
357695c385b4SNeil Horman 
357795c385b4SNeil Horman 	/*
357895c385b4SNeil Horman 	 * Optimistic nodes can start receiving
357995c385b4SNeil Horman 	 * Frames right away
358095c385b4SNeil Horman 	 */
35817fd2561eSErik Kline 	if (ifp->flags & IFA_F_OPTIMISTIC) {
358295c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
35837fd2561eSErik Kline 		if (ipv6_use_optimistic_addr(idev)) {
35847fd2561eSErik Kline 			/* Because optimistic nodes can use this address,
35857fd2561eSErik Kline 			 * notify listeners. If DAD fails, RTM_DELADDR is sent.
35867fd2561eSErik Kline 			 */
358716186a82Ssubashab@codeaurora.org 			notify = true;
35887fd2561eSErik Kline 		}
35897fd2561eSErik Kline 	}
359095c385b4SNeil Horman 
35916732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
35921da177e4SLinus Torvalds out:
3593622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
35941da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
359516186a82Ssubashab@codeaurora.org 	if (notify)
359616186a82Ssubashab@codeaurora.org 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
35971da177e4SLinus Torvalds }
35981da177e4SLinus Torvalds 
3599c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp)
36001da177e4SLinus Torvalds {
3601c15b1ccaSHannes Frederic Sowa 	bool begin_dad = false;
3602c15b1ccaSHannes Frederic Sowa 
36038e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3604c15b1ccaSHannes Frederic Sowa 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3605c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_PREDAD;
3606c15b1ccaSHannes Frederic Sowa 		begin_dad = true;
3607c15b1ccaSHannes Frederic Sowa 	}
36088e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3609c15b1ccaSHannes Frederic Sowa 
3610c15b1ccaSHannes Frederic Sowa 	if (begin_dad)
3611c15b1ccaSHannes Frederic Sowa 		addrconf_mod_dad_work(ifp, 0);
3612c15b1ccaSHannes Frederic Sowa }
3613c15b1ccaSHannes Frederic Sowa 
3614c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w)
3615c15b1ccaSHannes Frederic Sowa {
3616c15b1ccaSHannes Frederic Sowa 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3617c15b1ccaSHannes Frederic Sowa 						struct inet6_ifaddr,
3618c15b1ccaSHannes Frederic Sowa 						dad_work);
36191da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
36201da177e4SLinus Torvalds 	struct in6_addr mcaddr;
36211da177e4SLinus Torvalds 
3622c15b1ccaSHannes Frederic Sowa 	enum {
3623c15b1ccaSHannes Frederic Sowa 		DAD_PROCESS,
3624c15b1ccaSHannes Frederic Sowa 		DAD_BEGIN,
3625c15b1ccaSHannes Frederic Sowa 		DAD_ABORT,
3626c15b1ccaSHannes Frederic Sowa 	} action = DAD_PROCESS;
3627c15b1ccaSHannes Frederic Sowa 
3628c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
3629c15b1ccaSHannes Frederic Sowa 
36308e8e676dSHannes Frederic Sowa 	spin_lock_bh(&ifp->lock);
3631c15b1ccaSHannes Frederic Sowa 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3632c15b1ccaSHannes Frederic Sowa 		action = DAD_BEGIN;
3633c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_DAD;
3634c15b1ccaSHannes Frederic Sowa 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3635c15b1ccaSHannes Frederic Sowa 		action = DAD_ABORT;
3636c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
3637c15b1ccaSHannes Frederic Sowa 	}
36388e8e676dSHannes Frederic Sowa 	spin_unlock_bh(&ifp->lock);
3639c15b1ccaSHannes Frederic Sowa 
3640c15b1ccaSHannes Frederic Sowa 	if (action == DAD_BEGIN) {
3641c15b1ccaSHannes Frederic Sowa 		addrconf_dad_begin(ifp);
3642c15b1ccaSHannes Frederic Sowa 		goto out;
3643c15b1ccaSHannes Frederic Sowa 	} else if (action == DAD_ABORT) {
3644c15b1ccaSHannes Frederic Sowa 		addrconf_dad_stop(ifp, 1);
3645c15b1ccaSHannes Frederic Sowa 		goto out;
3646c15b1ccaSHannes Frederic Sowa 	}
3647c15b1ccaSHannes Frederic Sowa 
3648b7b1bfceSHannes Frederic Sowa 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
3649f2344a13SHerbert Xu 		goto out;
3650f2344a13SHerbert Xu 
3651c15b1ccaSHannes Frederic Sowa 	write_lock_bh(&idev->lock);
3652122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
3653c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
36541da177e4SLinus Torvalds 		goto out;
36551da177e4SLinus Torvalds 	}
365621809fafSstephen hemminger 
365721809fafSstephen hemminger 	spin_lock(&ifp->lock);
3658622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3659622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
3660c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
3661622ccdf1SHerbert Xu 		goto out;
3662622ccdf1SHerbert Xu 	}
3663622ccdf1SHerbert Xu 
3664b7b1bfceSHannes Frederic Sowa 	if (ifp->dad_probes == 0) {
36651da177e4SLinus Torvalds 		/*
36661da177e4SLinus Torvalds 		 * DAD was successful
36671da177e4SLinus Torvalds 		 */
36681da177e4SLinus Torvalds 
3669cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
367021809fafSstephen hemminger 		spin_unlock(&ifp->lock);
3671c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
36741da177e4SLinus Torvalds 
36751da177e4SLinus Torvalds 		goto out;
36761da177e4SLinus Torvalds 	}
36771da177e4SLinus Torvalds 
3678b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes--;
3679c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp,
36801f9248e5SJiri Pirko 			      NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
368121809fafSstephen hemminger 	spin_unlock(&ifp->lock);
3682c15b1ccaSHannes Frederic Sowa 	write_unlock_bh(&idev->lock);
36831da177e4SLinus Torvalds 
36841da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
36851da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3686304d888bSNicolas Dichtel 	ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any);
36871da177e4SLinus Torvalds out:
36881da177e4SLinus Torvalds 	in6_ifa_put(ifp);
3689c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
36901da177e4SLinus Torvalds }
36911da177e4SLinus Torvalds 
369211ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */
369311ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
369411ffff75SHannes Frederic Sowa {
369511ffff75SHannes Frederic Sowa 	struct inet6_ifaddr *ifpiter;
369611ffff75SHannes Frederic Sowa 	struct inet6_dev *idev = ifp->idev;
369711ffff75SHannes Frederic Sowa 
3698602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3699602582caSHannes Frederic Sowa 		if (ifpiter->scope > IFA_LINK)
3700602582caSHannes Frederic Sowa 			break;
370111ffff75SHannes Frederic Sowa 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
370211ffff75SHannes Frederic Sowa 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
370311ffff75SHannes Frederic Sowa 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
370411ffff75SHannes Frederic Sowa 		    IFA_F_PERMANENT)
370511ffff75SHannes Frederic Sowa 			return false;
370611ffff75SHannes Frederic Sowa 	}
370711ffff75SHannes Frederic Sowa 	return true;
370811ffff75SHannes Frederic Sowa }
370911ffff75SHannes Frederic Sowa 
37101da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
37111da177e4SLinus Torvalds {
37121da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
3713b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
3714b173ee48SHannes Frederic Sowa 	bool send_rs, send_mld;
3715b7b1bfceSHannes Frederic Sowa 
3716c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
37171da177e4SLinus Torvalds 
37181da177e4SLinus Torvalds 	/*
37191da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
37201da177e4SLinus Torvalds 	 */
37211da177e4SLinus Torvalds 
37221da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
37231da177e4SLinus Torvalds 
3724026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
3725026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
37261da177e4SLinus Torvalds 	 */
37271da177e4SLinus Torvalds 
37281ec047ebSHannes Frederic Sowa 	read_lock_bh(&ifp->idev->lock);
372911ffff75SHannes Frederic Sowa 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3730b173ee48SHannes Frederic Sowa 	send_rs = send_mld &&
3731b173ee48SHannes Frederic Sowa 		  ipv6_accept_ra(ifp->idev) &&
3732b173ee48SHannes Frederic Sowa 		  ifp->idev->cnf.rtr_solicits > 0 &&
3733b173ee48SHannes Frederic Sowa 		  (dev->flags&IFF_LOOPBACK) == 0;
37341ec047ebSHannes Frederic Sowa 	read_unlock_bh(&ifp->idev->lock);
37351ec047ebSHannes Frederic Sowa 
3736b173ee48SHannes Frederic Sowa 	/* While dad is in progress mld report's source address is in6_addrany.
3737b173ee48SHannes Frederic Sowa 	 * Resend with proper ll now.
3738b173ee48SHannes Frederic Sowa 	 */
3739b173ee48SHannes Frederic Sowa 	if (send_mld)
3740b173ee48SHannes Frederic Sowa 		ipv6_mc_dad_complete(ifp->idev);
3741b173ee48SHannes Frederic Sowa 
37421ec047ebSHannes Frederic Sowa 	if (send_rs) {
37431da177e4SLinus Torvalds 		/*
37441da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
37451da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
37461da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
37471da177e4SLinus Torvalds 		 */
37481ec047ebSHannes Frederic Sowa 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3749b7b1bfceSHannes Frederic Sowa 			return;
37501ec047ebSHannes Frederic Sowa 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
37511da177e4SLinus Torvalds 
3752b7b1bfceSHannes Frederic Sowa 		write_lock_bh(&ifp->idev->lock);
3753b7b1bfceSHannes Frederic Sowa 		spin_lock(&ifp->lock);
3754b7b1bfceSHannes Frederic Sowa 		ifp->idev->rs_probes = 1;
37551da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
3756b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(ifp->idev,
3757b7b1bfceSHannes Frederic Sowa 				      ifp->idev->cnf.rtr_solicit_interval);
3758b7b1bfceSHannes Frederic Sowa 		spin_unlock(&ifp->lock);
3759b7b1bfceSHannes Frederic Sowa 		write_unlock_bh(&ifp->idev->lock);
37601da177e4SLinus Torvalds 	}
37611da177e4SLinus Torvalds }
37621da177e4SLinus Torvalds 
3763e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev)
3764e21e8467SStephen Hemminger {
3765c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
3766c5e33bddSYOSHIFUJI Hideaki 
3767c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
3768502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
376921809fafSstephen hemminger 		spin_lock(&ifp->lock);
3770f2344a13SHerbert Xu 		if (ifp->flags & IFA_F_TENTATIVE &&
3771f2344a13SHerbert Xu 		    ifp->state == INET6_IFADDR_STATE_DAD)
3772c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
3773f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
3774c5e33bddSYOSHIFUJI Hideaki 	}
3775c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
3776c5e33bddSYOSHIFUJI Hideaki }
3777c5e33bddSYOSHIFUJI Hideaki 
37781da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
37791da177e4SLinus Torvalds struct if6_iter_state {
37803c40090aSDaniel Lezcano 	struct seq_net_private p;
37811da177e4SLinus Torvalds 	int bucket;
37821d578303SMihai Maruseac 	int offset;
37831da177e4SLinus Torvalds };
37841da177e4SLinus Torvalds 
37851d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
37861da177e4SLinus Torvalds {
37871da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
37881da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
37891218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
37901d578303SMihai Maruseac 	int p = 0;
37911da177e4SLinus Torvalds 
37921d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
37931d578303SMihai Maruseac 	if (pos == 0) {
37941d578303SMihai Maruseac 		state->bucket = 0;
37951d578303SMihai Maruseac 		state->offset = 0;
37961d578303SMihai Maruseac 	}
37971d578303SMihai Maruseac 
37981d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3799b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
38001d578303SMihai Maruseac 					 addr_lst) {
38019f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
38029f0d3c27SEric Dumazet 				continue;
38031d578303SMihai Maruseac 			/* sync with offset */
38041d578303SMihai Maruseac 			if (p < state->offset) {
38051d578303SMihai Maruseac 				p++;
38061d578303SMihai Maruseac 				continue;
38071d578303SMihai Maruseac 			}
38081d578303SMihai Maruseac 			state->offset++;
38091da177e4SLinus Torvalds 			return ifa;
38101da177e4SLinus Torvalds 		}
38111d578303SMihai Maruseac 
38121d578303SMihai Maruseac 		/* prepare for next bucket */
38131d578303SMihai Maruseac 		state->offset = 0;
38141d578303SMihai Maruseac 		p = 0;
38151d578303SMihai Maruseac 	}
3816c2e21293Sstephen hemminger 	return NULL;
3817c2e21293Sstephen hemminger }
38181da177e4SLinus Torvalds 
3819c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3820c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
38211da177e4SLinus Torvalds {
38221da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
38231218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
38241da177e4SLinus Torvalds 
3825b67bfe0dSSasha Levin 	hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
38269f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
38279f0d3c27SEric Dumazet 			continue;
38281d578303SMihai Maruseac 		state->offset++;
38291da177e4SLinus Torvalds 		return ifa;
38301d578303SMihai Maruseac 	}
38311da177e4SLinus Torvalds 
3832c2e21293Sstephen hemminger 	while (++state->bucket < IN6_ADDR_HSIZE) {
38331d578303SMihai Maruseac 		state->offset = 0;
3834b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa,
3835c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
38369f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
38379f0d3c27SEric Dumazet 				continue;
38381d578303SMihai Maruseac 			state->offset++;
3839c2e21293Sstephen hemminger 			return ifa;
3840c2e21293Sstephen hemminger 		}
3841c2e21293Sstephen hemminger 	}
3842c2e21293Sstephen hemminger 
3843c2e21293Sstephen hemminger 	return NULL;
3844c2e21293Sstephen hemminger }
3845c2e21293Sstephen hemminger 
38461da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
38474f70eccaSEric Dumazet 	__acquires(rcu_bh)
38481da177e4SLinus Torvalds {
38495c578aedSstephen hemminger 	rcu_read_lock_bh();
38501d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
38511da177e4SLinus Torvalds }
38521da177e4SLinus Torvalds 
38531da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
38541da177e4SLinus Torvalds {
38551da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
38561da177e4SLinus Torvalds 
38571da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
38581da177e4SLinus Torvalds 	++*pos;
38591da177e4SLinus Torvalds 	return ifa;
38601da177e4SLinus Torvalds }
38611da177e4SLinus Torvalds 
38621da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
38634f70eccaSEric Dumazet 	__releases(rcu_bh)
38641da177e4SLinus Torvalds {
38655c578aedSstephen hemminger 	rcu_read_unlock_bh();
38661da177e4SLinus Torvalds }
38671da177e4SLinus Torvalds 
38681da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
38691da177e4SLinus Torvalds {
38701da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3871971a351cSJiri Pirko 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3872b071195dSHarvey Harrison 		   &ifp->addr,
38731da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
38741da177e4SLinus Torvalds 		   ifp->prefix_len,
38751da177e4SLinus Torvalds 		   ifp->scope,
3876971a351cSJiri Pirko 		   (u8) ifp->flags,
38771da177e4SLinus Torvalds 		   ifp->idev->dev->name);
38781da177e4SLinus Torvalds 	return 0;
38791da177e4SLinus Torvalds }
38801da177e4SLinus Torvalds 
388156b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
38821da177e4SLinus Torvalds 	.start	= if6_seq_start,
38831da177e4SLinus Torvalds 	.next	= if6_seq_next,
38841da177e4SLinus Torvalds 	.show	= if6_seq_show,
38851da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
38861da177e4SLinus Torvalds };
38871da177e4SLinus Torvalds 
38881da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
38891da177e4SLinus Torvalds {
38903c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
3891cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
38921da177e4SLinus Torvalds }
38931da177e4SLinus Torvalds 
38949a32144eSArjan van de Ven static const struct file_operations if6_fops = {
38951da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
38961da177e4SLinus Torvalds 	.open		= if6_seq_open,
38971da177e4SLinus Torvalds 	.read		= seq_read,
38981da177e4SLinus Torvalds 	.llseek		= seq_lseek,
38993c40090aSDaniel Lezcano 	.release	= seq_release_net,
39001da177e4SLinus Torvalds };
39011da177e4SLinus Torvalds 
39022c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
39031da177e4SLinus Torvalds {
3904d4beaa66SGao feng 	if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
39051da177e4SLinus Torvalds 		return -ENOMEM;
39061da177e4SLinus Torvalds 	return 0;
39071da177e4SLinus Torvalds }
39081da177e4SLinus Torvalds 
39092c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
39103c40090aSDaniel Lezcano {
3911ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
39123c40090aSDaniel Lezcano }
39133c40090aSDaniel Lezcano 
39143c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
39153c40090aSDaniel Lezcano 	.init = if6_proc_net_init,
39163c40090aSDaniel Lezcano 	.exit = if6_proc_net_exit,
39173c40090aSDaniel Lezcano };
39183c40090aSDaniel Lezcano 
39193c40090aSDaniel Lezcano int __init if6_proc_init(void)
39203c40090aSDaniel Lezcano {
39213c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
39223c40090aSDaniel Lezcano }
39233c40090aSDaniel Lezcano 
39241da177e4SLinus Torvalds void if6_proc_exit(void)
39251da177e4SLinus Torvalds {
39263c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
39271da177e4SLinus Torvalds }
39281da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
39291da177e4SLinus Torvalds 
393007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
39313b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
3932b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
39333b9f9a1cSNoriaki TAKAMIYA {
39343b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
3935c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
3936ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
3937c2e21293Sstephen hemminger 
39385c578aedSstephen hemminger 	rcu_read_lock_bh();
3939b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3940878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
3941389f6612SDaniel Lezcano 			continue;
3942caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
39433b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
39443b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
39453b9f9a1cSNoriaki TAKAMIYA 			break;
39463b9f9a1cSNoriaki TAKAMIYA 		}
39473b9f9a1cSNoriaki TAKAMIYA 	}
39485c578aedSstephen hemminger 	rcu_read_unlock_bh();
39493b9f9a1cSNoriaki TAKAMIYA 	return ret;
39503b9f9a1cSNoriaki TAKAMIYA }
39513b9f9a1cSNoriaki TAKAMIYA #endif
39523b9f9a1cSNoriaki TAKAMIYA 
39531da177e4SLinus Torvalds /*
39541da177e4SLinus Torvalds  *	Periodic address status verification
39551da177e4SLinus Torvalds  */
39561da177e4SLinus Torvalds 
3957c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void)
39581da177e4SLinus Torvalds {
3959b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
39601da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
39611da177e4SLinus Torvalds 	int i;
39621da177e4SLinus Torvalds 
3963c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
3964c15b1ccaSHannes Frederic Sowa 
39655c578aedSstephen hemminger 	rcu_read_lock_bh();
39661da177e4SLinus Torvalds 	now = jiffies;
3967b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
39681da177e4SLinus Torvalds 
3969c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
39701da177e4SLinus Torvalds 
39711da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
39721da177e4SLinus Torvalds restart:
3973c15b1ccaSHannes Frederic Sowa 		hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
39741da177e4SLinus Torvalds 			unsigned long age;
39751da177e4SLinus Torvalds 
3976fad8da3eSYasushi Asano 			/* When setting preferred_lft to a value not zero or
3977fad8da3eSYasushi Asano 			 * infinity, while valid_lft is infinity
3978fad8da3eSYasushi Asano 			 * IFA_F_PERMANENT has a non-infinity life time.
3979fad8da3eSYasushi Asano 			 */
3980fad8da3eSYasushi Asano 			if ((ifp->flags & IFA_F_PERMANENT) &&
3981fad8da3eSYasushi Asano 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
39821da177e4SLinus Torvalds 				continue;
39831da177e4SLinus Torvalds 
39841da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
3985b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
3986b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
39871da177e4SLinus Torvalds 
39888f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
39898f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
39901da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
39911da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
39921da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
39931da177e4SLinus Torvalds 				goto restart;
39948f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
39958f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
39968f27ebb9SYOSHIFUJI Hideaki 				continue;
39971da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
3998a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
39991da177e4SLinus Torvalds 				int deprecate = 0;
40001da177e4SLinus Torvalds 
40011da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
40021da177e4SLinus Torvalds 					deprecate = 1;
40031da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
40041da177e4SLinus Torvalds 				}
40051da177e4SLinus Torvalds 
4006fad8da3eSYasushi Asano 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4007fad8da3eSYasushi Asano 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
40081da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
40091da177e4SLinus Torvalds 
40101da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
40111da177e4SLinus Torvalds 
40121da177e4SLinus Torvalds 				if (deprecate) {
40131da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
40141da177e4SLinus Torvalds 
40151da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
40161da177e4SLinus Torvalds 					in6_ifa_put(ifp);
40171da177e4SLinus Torvalds 					goto restart;
40181da177e4SLinus Torvalds 				}
40191da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
40201da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
402188949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
402288949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
40231f9248e5SJiri Pirko 					NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
402488949cf4Sstephen hemminger 
40251da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
40261da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
40271da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
40281da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
40291da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
40301da177e4SLinus Torvalds 						ifp->regen_count++;
40311da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
40321da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
40331da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
40345c578aedSstephen hemminger 
4035291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
4036291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
4037291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
40381da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
40391da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
40401da177e4SLinus Torvalds 						in6_ifa_put(ifp);
40411da177e4SLinus Torvalds 						goto restart;
40421da177e4SLinus Torvalds 					}
40431da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
40441da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
40451da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
40461da177e4SLinus Torvalds 			} else {
40471da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
40481da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
40491da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
40501da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
40511da177e4SLinus Torvalds 			}
40521da177e4SLinus Torvalds 		}
40531da177e4SLinus Torvalds 	}
40541da177e4SLinus Torvalds 
4055b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
4056b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
4057b2db7564SYOSHIFUJI Hideaki 
4058b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
4059b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4060b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
4061b2db7564SYOSHIFUJI Hideaki 
4062b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4063b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4064b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4065b2db7564SYOSHIFUJI Hideaki 
4066ba3542e1Sdingtianhong 	ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4067ba3542e1Sdingtianhong 	      now, next, next_sec, next_sched);
4068c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
40695c578aedSstephen hemminger 	rcu_read_unlock_bh();
40701da177e4SLinus Torvalds }
40711da177e4SLinus Torvalds 
4072c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w)
4073c15b1ccaSHannes Frederic Sowa {
4074c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
4075c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4076c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
4077c15b1ccaSHannes Frederic Sowa }
4078c15b1ccaSHannes Frederic Sowa 
4079c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void)
4080c15b1ccaSHannes Frederic Sowa {
4081c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4082c15b1ccaSHannes Frederic Sowa }
4083c15b1ccaSHannes Frederic Sowa 
4084caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4085caeaba79SNicolas Dichtel 				     struct in6_addr **peer_pfx)
4086461d8837SThomas Graf {
4087461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
4088461d8837SThomas Graf 
4089caeaba79SNicolas Dichtel 	*peer_pfx = NULL;
4090caeaba79SNicolas Dichtel 
4091461d8837SThomas Graf 	if (addr)
4092461d8837SThomas Graf 		pfx = nla_data(addr);
4093461d8837SThomas Graf 
4094461d8837SThomas Graf 	if (local) {
4095461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4096caeaba79SNicolas Dichtel 			*peer_pfx = pfx;
4097461d8837SThomas Graf 		pfx = nla_data(local);
4098461d8837SThomas Graf 	}
4099461d8837SThomas Graf 
4100461d8837SThomas Graf 	return pfx;
4101461d8837SThomas Graf }
4102461d8837SThomas Graf 
4103ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4104461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
4105461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
4106461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
4107479840ffSJiri Pirko 	[IFA_FLAGS]		= { .len = sizeof(u32) },
4108461d8837SThomas Graf };
4109461d8837SThomas Graf 
41101da177e4SLinus Torvalds static int
4111661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
41121da177e4SLinus Torvalds {
41133b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4114b933f716SThomas Graf 	struct ifaddrmsg *ifm;
4115b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4116caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
41176046d5b4SHeiner Kallweit 	u32 ifa_flags;
4118b933f716SThomas Graf 	int err;
41191da177e4SLinus Torvalds 
4120b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4121b933f716SThomas Graf 	if (err < 0)
4122b933f716SThomas Graf 		return err;
4123b933f716SThomas Graf 
4124b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
4125caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
412663159f29SIan Morris 	if (!pfx)
41271da177e4SLinus Torvalds 		return -EINVAL;
41281da177e4SLinus Torvalds 
41296046d5b4SHeiner Kallweit 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
41306046d5b4SHeiner Kallweit 
41316046d5b4SHeiner Kallweit 	/* We ignore other flags so far. */
41326046d5b4SHeiner Kallweit 	ifa_flags &= IFA_F_MANAGETEMPADDR;
41336046d5b4SHeiner Kallweit 
41346046d5b4SHeiner Kallweit 	return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
41356046d5b4SHeiner Kallweit 			      ifm->ifa_prefixlen);
41361da177e4SLinus Torvalds }
41371da177e4SLinus Torvalds 
4138479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
413955ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
4140081bba5bSNoriaki TAKAMIYA {
41416f704992SYOSHIFUJI Hideaki 	u32 flags;
41426f704992SYOSHIFUJI Hideaki 	clock_t expires;
41434bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
414453bd6749SJiri Pirko 	bool was_managetempaddr;
41455b84efecSThomas Haller 	bool had_prefixroute;
414646d48046SYOSHIFUJI Hideaki 
4147c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4148c15b1ccaSHannes Frederic Sowa 
4149081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
4150081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
4151081bba5bSNoriaki TAKAMIYA 
415253bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR &&
415353bd6749SJiri Pirko 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
415453bd6749SJiri Pirko 		return -EINVAL;
415553bd6749SJiri Pirko 
41564bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
41574bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
41584bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
41594bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
41606f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
41614bed72e4SYOSHIFUJI Hideaki 	} else {
41624bed72e4SYOSHIFUJI Hideaki 		expires = 0;
41634bed72e4SYOSHIFUJI Hideaki 		flags = 0;
41644bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
41656f704992SYOSHIFUJI Hideaki 	}
4166081bba5bSNoriaki TAKAMIYA 
41674bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
41684bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
41694bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
417055ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
41714bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
41724bed72e4SYOSHIFUJI Hideaki 	}
4173081bba5bSNoriaki TAKAMIYA 
4174081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
417553bd6749SJiri Pirko 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
41765b84efecSThomas Haller 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
41775b84efecSThomas Haller 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
417853bd6749SJiri Pirko 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4179761aac73SThomas Haller 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4180761aac73SThomas Haller 			IFA_F_NOPREFIXROUTE);
418153bd6749SJiri Pirko 	ifp->flags |= ifa_flags;
4182081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
4183081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
4184081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
4185081bba5bSNoriaki TAKAMIYA 
4186081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
4187081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
4188081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
4189081bba5bSNoriaki TAKAMIYA 
4190761aac73SThomas Haller 	if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
419146d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
41926f704992SYOSHIFUJI Hideaki 				      expires, flags);
41935b84efecSThomas Haller 	} else if (had_prefixroute) {
41945b84efecSThomas Haller 		enum cleanup_prefix_rt_t action;
41955b84efecSThomas Haller 		unsigned long rt_expires;
41965b84efecSThomas Haller 
41975b84efecSThomas Haller 		write_lock_bh(&ifp->idev->lock);
41985b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &rt_expires);
41995b84efecSThomas Haller 		write_unlock_bh(&ifp->idev->lock);
42005b84efecSThomas Haller 
42015b84efecSThomas Haller 		if (action != CLEANUP_PREFIX_RT_NOP) {
42025b84efecSThomas Haller 			cleanup_prefix_route(ifp, rt_expires,
42035b84efecSThomas Haller 				action == CLEANUP_PREFIX_RT_DEL);
42045b84efecSThomas Haller 		}
4205761aac73SThomas Haller 	}
420653bd6749SJiri Pirko 
420753bd6749SJiri Pirko 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
420853bd6749SJiri Pirko 		if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
420953bd6749SJiri Pirko 			valid_lft = prefered_lft = 0;
421053bd6749SJiri Pirko 		manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
421153bd6749SJiri Pirko 				 !was_managetempaddr, jiffies);
421253bd6749SJiri Pirko 	}
421353bd6749SJiri Pirko 
4214c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4215081bba5bSNoriaki TAKAMIYA 
4216081bba5bSNoriaki TAKAMIYA 	return 0;
4217081bba5bSNoriaki TAKAMIYA }
4218081bba5bSNoriaki TAKAMIYA 
4219081bba5bSNoriaki TAKAMIYA static int
4220661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
42211da177e4SLinus Torvalds {
42223b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4223461d8837SThomas Graf 	struct ifaddrmsg *ifm;
4224461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4225caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
42267198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
42277198f8ceSThomas Graf 	struct net_device *dev;
422855ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4229479840ffSJiri Pirko 	u32 ifa_flags;
4230461d8837SThomas Graf 	int err;
42311da177e4SLinus Torvalds 
4232461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4233461d8837SThomas Graf 	if (err < 0)
4234461d8837SThomas Graf 		return err;
4235461d8837SThomas Graf 
4236461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
4237caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
423863159f29SIan Morris 	if (!pfx)
42391da177e4SLinus Torvalds 		return -EINVAL;
42401da177e4SLinus Torvalds 
4241461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
42420778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
4243461d8837SThomas Graf 
4244461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
42450778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
4246461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
4247461d8837SThomas Graf 	} else {
4248461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
4249461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
42500778769dSNoriaki TAKAMIYA 	}
42510778769dSNoriaki TAKAMIYA 
4252af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
425363159f29SIan Morris 	if (!dev)
42547198f8ceSThomas Graf 		return -ENODEV;
4255081bba5bSNoriaki TAKAMIYA 
4256479840ffSJiri Pirko 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4257479840ffSJiri Pirko 
425855ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
4259761aac73SThomas Haller 	ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
426093a714d6SMadhu Challa 		     IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
426155ebaef1SNoriaki TAKAMIYA 
42621cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
426363159f29SIan Morris 	if (!ifa) {
42647198f8ceSThomas Graf 		/*
42657198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
4266db9c7c39Sstephen hemminger 		 * userspace already relies on not having to provide this.
42677198f8ceSThomas Graf 		 */
4268caeaba79SNicolas Dichtel 		return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4269af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
4270af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
42711da177e4SLinus Torvalds 	}
42721da177e4SLinus Torvalds 
42737198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
42747198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
42757198f8ceSThomas Graf 		err = -EEXIST;
42767198f8ceSThomas Graf 	else
427755ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
42787198f8ceSThomas Graf 
42797198f8ceSThomas Graf 	in6_ifa_put(ifa);
42807198f8ceSThomas Graf 
42817198f8ceSThomas Graf 	return err;
42827198f8ceSThomas Graf }
42837198f8ceSThomas Graf 
4284479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4285101bb229SThomas Graf 			  u8 scope, int ifindex)
4286101bb229SThomas Graf {
4287101bb229SThomas Graf 	struct ifaddrmsg *ifm;
4288101bb229SThomas Graf 
4289101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
4290101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
4291101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
4292101bb229SThomas Graf 	ifm->ifa_flags = flags;
4293101bb229SThomas Graf 	ifm->ifa_scope = scope;
4294101bb229SThomas Graf 	ifm->ifa_index = ifindex;
4295101bb229SThomas Graf }
4296101bb229SThomas Graf 
429785486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
429885486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
429985486af0SThomas Graf {
430085486af0SThomas Graf 	struct ifa_cacheinfo ci;
430185486af0SThomas Graf 
430218a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
430318a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
430485486af0SThomas Graf 	ci.ifa_prefered = preferred;
430585486af0SThomas Graf 	ci.ifa_valid = valid;
430685486af0SThomas Graf 
430785486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
430885486af0SThomas Graf }
430985486af0SThomas Graf 
4310101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
4311101bb229SThomas Graf {
4312101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
4313101bb229SThomas Graf 		return RT_SCOPE_HOST;
4314101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
4315101bb229SThomas Graf 		return RT_SCOPE_LINK;
4316101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
4317101bb229SThomas Graf 		return RT_SCOPE_SITE;
4318101bb229SThomas Graf 	else
4319101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
4320101bb229SThomas Graf }
4321101bb229SThomas Graf 
43220ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
43230ab6803bSThomas Graf {
4324339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4325caeaba79SNicolas Dichtel 	       + nla_total_size(16) /* IFA_LOCAL */
4326339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
4327479840ffSJiri Pirko 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
4328479840ffSJiri Pirko 	       + nla_total_size(4)  /* IFA_FLAGS */;
43290ab6803bSThomas Graf }
4330c5396a31SYOSHIFUJI Hideaki 
43311da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
433215e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
43331da177e4SLinus Torvalds {
43341da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
433585486af0SThomas Graf 	u32 preferred, valid;
43361da177e4SLinus Torvalds 
433715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
433863159f29SIan Morris 	if (!nlh)
433926932566SPatrick McHardy 		return -EMSGSIZE;
43400ab6803bSThomas Graf 
4341101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4342101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
4343101bb229SThomas Graf 
4344fad8da3eSYasushi Asano 	if (!((ifa->flags&IFA_F_PERMANENT) &&
4345fad8da3eSYasushi Asano 	      (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
434685486af0SThomas Graf 		preferred = ifa->prefered_lft;
434785486af0SThomas Graf 		valid = ifa->valid_lft;
434885486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
43491da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
4350a1faa698SJens Rosenboom 			if (preferred > tval)
435185486af0SThomas Graf 				preferred -= tval;
4352a1faa698SJens Rosenboom 			else
4353a1faa698SJens Rosenboom 				preferred = 0;
4354f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
4355f56619fcSBen Hutchings 				if (valid > tval)
435685486af0SThomas Graf 					valid -= tval;
4357f56619fcSBen Hutchings 				else
4358f56619fcSBen Hutchings 					valid = 0;
4359f56619fcSBen Hutchings 			}
43601da177e4SLinus Torvalds 		}
43611da177e4SLinus Torvalds 	} else {
436285486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
436385486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
43641da177e4SLinus Torvalds 	}
436585486af0SThomas Graf 
43667996c799SCong Wang 	if (!ipv6_addr_any(&ifa->peer_addr)) {
4367930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4368930345eaSJiri Benc 		    nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4369caeaba79SNicolas Dichtel 			goto error;
4370caeaba79SNicolas Dichtel 	} else
4371930345eaSJiri Benc 		if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4372caeaba79SNicolas Dichtel 			goto error;
4373caeaba79SNicolas Dichtel 
4374caeaba79SNicolas Dichtel 	if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4375caeaba79SNicolas Dichtel 		goto error;
437685486af0SThomas Graf 
4377479840ffSJiri Pirko 	if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4378479840ffSJiri Pirko 		goto error;
4379479840ffSJiri Pirko 
4380053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4381053c095aSJohannes Berg 	return 0;
4382caeaba79SNicolas Dichtel 
4383caeaba79SNicolas Dichtel error:
4384caeaba79SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
4385caeaba79SNicolas Dichtel 	return -EMSGSIZE;
43861da177e4SLinus Torvalds }
43871da177e4SLinus Torvalds 
43881da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
438915e47304SEric W. Biederman 				u32 portid, u32 seq, int event, u16 flags)
43901da177e4SLinus Torvalds {
43911da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4392101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4393101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
43941da177e4SLinus Torvalds 
43951da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4396101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4397101bb229SThomas Graf 
439815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
439963159f29SIan Morris 	if (!nlh)
440026932566SPatrick McHardy 		return -EMSGSIZE;
44010ab6803bSThomas Graf 
4402101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4403930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
44040ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
440526932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
440626932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
440726932566SPatrick McHardy 		return -EMSGSIZE;
440826932566SPatrick McHardy 	}
440985486af0SThomas Graf 
4410053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4411053c095aSJohannes Berg 	return 0;
44121da177e4SLinus Torvalds }
44131da177e4SLinus Torvalds 
44141da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
441515e47304SEric W. Biederman 				u32 portid, u32 seq, int event, unsigned int flags)
44161da177e4SLinus Torvalds {
44171da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4418101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4419101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
44201da177e4SLinus Torvalds 
44211da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4422101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4423101bb229SThomas Graf 
442415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
442563159f29SIan Morris 	if (!nlh)
442626932566SPatrick McHardy 		return -EMSGSIZE;
44270ab6803bSThomas Graf 
4428101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4429930345eaSJiri Benc 	if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
44300ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
443126932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
443226932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
443326932566SPatrick McHardy 		return -EMSGSIZE;
443426932566SPatrick McHardy 	}
443585486af0SThomas Graf 
4436053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4437053c095aSJohannes Berg 	return 0;
44381da177e4SLinus Torvalds }
44391da177e4SLinus Torvalds 
4440e21e8467SStephen Hemminger enum addr_type_t {
44411da177e4SLinus Torvalds 	UNICAST_ADDR,
44421da177e4SLinus Torvalds 	MULTICAST_ADDR,
44431da177e4SLinus Torvalds 	ANYCAST_ADDR,
44441da177e4SLinus Torvalds };
44451da177e4SLinus Torvalds 
4446234b27c3SEric Dumazet /* called with rcu_read_lock() */
4447234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4448234b27c3SEric Dumazet 			  struct netlink_callback *cb, enum addr_type_t type,
4449234b27c3SEric Dumazet 			  int s_ip_idx, int *p_ip_idx)
44501da177e4SLinus Torvalds {
44511da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
44521da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
4453234b27c3SEric Dumazet 	int err = 1;
4454234b27c3SEric Dumazet 	int ip_idx = *p_ip_idx;
44551da177e4SLinus Torvalds 
44561da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
44571da177e4SLinus Torvalds 	switch (type) {
4458502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
4459502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
4460502a2ffdSstephen hemminger 
4461ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
4462502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
4463502a2ffdSstephen hemminger 			if (++ip_idx < s_ip_idx)
44641da177e4SLinus Torvalds 				continue;
44655d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifaddr(skb, ifa,
446615e47304SEric W. Biederman 						NETLINK_CB(cb->skb).portid,
44675d5619b4SYOSHIFUJI Hideaki 						cb->nlh->nlmsg_seq,
44685d5619b4SYOSHIFUJI Hideaki 						RTM_NEWADDR,
44695d5619b4SYOSHIFUJI Hideaki 						NLM_F_MULTI);
4470053c095aSJohannes Berg 			if (err < 0)
4471bcd32326SEric Dumazet 				break;
447263998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
44731da177e4SLinus Torvalds 		}
44741da177e4SLinus Torvalds 		break;
4475502a2ffdSstephen hemminger 	}
44761da177e4SLinus Torvalds 	case MULTICAST_ADDR:
44771da177e4SLinus Torvalds 		/* multicast address */
44781da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
44791da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
44801da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
44811da177e4SLinus Torvalds 				continue;
44825d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifmcaddr(skb, ifmca,
448315e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
44845d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
44855d5619b4SYOSHIFUJI Hideaki 						  RTM_GETMULTICAST,
44865d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4487053c095aSJohannes Berg 			if (err < 0)
4488bcd32326SEric Dumazet 				break;
44891da177e4SLinus Torvalds 		}
44901da177e4SLinus Torvalds 		break;
44911da177e4SLinus Torvalds 	case ANYCAST_ADDR:
44921da177e4SLinus Torvalds 		/* anycast address */
44931da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
44941da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
44951da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
44961da177e4SLinus Torvalds 				continue;
44975d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifacaddr(skb, ifaca,
449815e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
44995d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
45005d5619b4SYOSHIFUJI Hideaki 						  RTM_GETANYCAST,
45015d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4502053c095aSJohannes Berg 			if (err < 0)
4503bcd32326SEric Dumazet 				break;
45041da177e4SLinus Torvalds 		}
45051da177e4SLinus Torvalds 		break;
45061da177e4SLinus Torvalds 	default:
45071da177e4SLinus Torvalds 		break;
45081da177e4SLinus Torvalds 	}
45091da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
4510234b27c3SEric Dumazet 	*p_ip_idx = ip_idx;
4511234b27c3SEric Dumazet 	return err;
4512234b27c3SEric Dumazet }
45135d5619b4SYOSHIFUJI Hideaki 
4514234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4515234b27c3SEric Dumazet 			   enum addr_type_t type)
4516234b27c3SEric Dumazet {
4517234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
4518234b27c3SEric Dumazet 	int h, s_h;
4519234b27c3SEric Dumazet 	int idx, ip_idx;
4520234b27c3SEric Dumazet 	int s_idx, s_ip_idx;
4521234b27c3SEric Dumazet 	struct net_device *dev;
4522234b27c3SEric Dumazet 	struct inet6_dev *idev;
4523234b27c3SEric Dumazet 	struct hlist_head *head;
4524234b27c3SEric Dumazet 
4525234b27c3SEric Dumazet 	s_h = cb->args[0];
4526234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
4527234b27c3SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
4528234b27c3SEric Dumazet 
4529234b27c3SEric Dumazet 	rcu_read_lock();
453063998ac2SNicolas Dichtel 	cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4531234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4532234b27c3SEric Dumazet 		idx = 0;
4533234b27c3SEric Dumazet 		head = &net->dev_index_head[h];
4534b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
4535234b27c3SEric Dumazet 			if (idx < s_idx)
4536234b27c3SEric Dumazet 				goto cont;
45374b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
4538234b27c3SEric Dumazet 				s_ip_idx = 0;
4539234b27c3SEric Dumazet 			ip_idx = 0;
4540e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
4541e21e8467SStephen Hemminger 			if (!idev)
4542234b27c3SEric Dumazet 				goto cont;
4543234b27c3SEric Dumazet 
4544234b27c3SEric Dumazet 			if (in6_dump_addrs(idev, skb, cb, type,
45457b46a644SDavid S. Miller 					   s_ip_idx, &ip_idx) < 0)
4546234b27c3SEric Dumazet 				goto done;
45477562f876SPavel Emelianov cont:
45487562f876SPavel Emelianov 			idx++;
45491da177e4SLinus Torvalds 		}
4550234b27c3SEric Dumazet 	}
4551234b27c3SEric Dumazet done:
4552234b27c3SEric Dumazet 	rcu_read_unlock();
4553234b27c3SEric Dumazet 	cb->args[0] = h;
4554234b27c3SEric Dumazet 	cb->args[1] = idx;
4555234b27c3SEric Dumazet 	cb->args[2] = ip_idx;
4556234b27c3SEric Dumazet 
45571da177e4SLinus Torvalds 	return skb->len;
45581da177e4SLinus Torvalds }
45591da177e4SLinus Torvalds 
45601da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
45611da177e4SLinus Torvalds {
45621da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
4563b854272bSDenis V. Lunev 
45641da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
45651da177e4SLinus Torvalds }
45661da177e4SLinus Torvalds 
45671da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
45681da177e4SLinus Torvalds {
45691da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
4570b854272bSDenis V. Lunev 
45711da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
45721da177e4SLinus Torvalds }
45731da177e4SLinus Torvalds 
45741da177e4SLinus Torvalds 
45751da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
45761da177e4SLinus Torvalds {
45771da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
4578b854272bSDenis V. Lunev 
45791da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
45801da177e4SLinus Torvalds }
45811da177e4SLinus Torvalds 
4582661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
45836c223828SNoriaki TAKAMIYA {
45843b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
45851b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
45861b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4587caeaba79SNicolas Dichtel 	struct in6_addr *addr = NULL, *peer;
45886c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
45896c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
45906c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
45916c223828SNoriaki TAKAMIYA 	int err;
45926c223828SNoriaki TAKAMIYA 
45931b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
45941b29fc2cSThomas Graf 	if (err < 0)
45951b29fc2cSThomas Graf 		goto errout;
45966c223828SNoriaki TAKAMIYA 
4597caeaba79SNicolas Dichtel 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
459863159f29SIan Morris 	if (!addr) {
45991b29fc2cSThomas Graf 		err = -EINVAL;
46001b29fc2cSThomas Graf 		goto errout;
46011b29fc2cSThomas Graf 	}
46021b29fc2cSThomas Graf 
46031b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
46046c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
46056fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
46066c223828SNoriaki TAKAMIYA 
4607e21e8467SStephen Hemminger 	ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4608e21e8467SStephen Hemminger 	if (!ifa) {
46091b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
46101b29fc2cSThomas Graf 		goto errout;
46116c223828SNoriaki TAKAMIYA 	}
46126c223828SNoriaki TAKAMIYA 
4613e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4614e21e8467SStephen Hemminger 	if (!skb) {
46151b29fc2cSThomas Graf 		err = -ENOBUFS;
46161b29fc2cSThomas Graf 		goto errout_ifa;
46171b29fc2cSThomas Graf 	}
46181b29fc2cSThomas Graf 
461915e47304SEric W. Biederman 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
46206c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
462126932566SPatrick McHardy 	if (err < 0) {
462226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
462326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
462426932566SPatrick McHardy 		kfree_skb(skb);
462526932566SPatrick McHardy 		goto errout_ifa;
462626932566SPatrick McHardy 	}
462715e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
46281b29fc2cSThomas Graf errout_ifa:
46296c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
46301b29fc2cSThomas Graf errout:
46316c223828SNoriaki TAKAMIYA 	return err;
46326c223828SNoriaki TAKAMIYA }
46336c223828SNoriaki TAKAMIYA 
46341da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
46351da177e4SLinus Torvalds {
46361da177e4SLinus Torvalds 	struct sk_buff *skb;
4637c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
46385d620266SThomas Graf 	int err = -ENOBUFS;
46391da177e4SLinus Torvalds 
46400ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
464163159f29SIan Morris 	if (!skb)
46425d620266SThomas Graf 		goto errout;
46435d620266SThomas Graf 
46445d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
464526932566SPatrick McHardy 	if (err < 0) {
464626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
464726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
464826932566SPatrick McHardy 		kfree_skb(skb);
464926932566SPatrick McHardy 		goto errout;
465026932566SPatrick McHardy 	}
46511ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
46521ce85fe4SPablo Neira Ayuso 	return;
46535d620266SThomas Graf errout:
46545d620266SThomas Graf 	if (err < 0)
46556fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
46561da177e4SLinus Torvalds }
46571da177e4SLinus Torvalds 
4658b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
46591da177e4SLinus Torvalds 				__s32 *array, int bytes)
46601da177e4SLinus Torvalds {
466104561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
466204561c1fSThomas Graf 
46631da177e4SLinus Torvalds 	memset(array, 0, bytes);
46641da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
46651da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
46661da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
46671da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
46681da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
46691da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
46701da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
46711da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
467293908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
467393908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
467493908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
467593908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
46761da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4677fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4678fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4679fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4680fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
46811da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
46821da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
46831da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
46841da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
46851da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
46861da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
468765f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
46888013d1d7SHangbin Liu 	array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
4689c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4690930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4691930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
469293908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
469393908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
4694fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
469509c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
469609c884d4SYOSHIFUJI Hideaki #endif
4697930d6ff2SYOSHIFUJI Hideaki #endif
4698fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
46990bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
470095c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
470195c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
47027fd2561eSErik Kline 	array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
470395c385b4SNeil Horman #endif
47047bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
47057bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
47067bc570c8SYOSHIFUJI Hideaki #endif
4707778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
47081b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4709c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
47105cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4711b800c3b9SHannes Frederic Sowa 	array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4712d9333196SBen Greear 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4713c2943f14SHarout Hedeshian 	array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
471435103d11SAndy Gospodarek 	array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
47153d1bec99SHannes Frederic Sowa 	/* we omit DEVCONF_STABLE_SECRET for now */
47163985e8a3SErik Kline 	array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
47171da177e4SLinus Torvalds }
47181da177e4SLinus Torvalds 
4719b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
4720b382b191SThomas Graf {
4721b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
4722b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
4723b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4724b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4725f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4726f53adae4SDaniel Borkmann 	     + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4727b382b191SThomas Graf }
4728b382b191SThomas Graf 
4729339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
4730339bf98fSThomas Graf {
4731339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4732339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4733339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4734339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
4735339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
47360344338bSAndy Gospodarek 	       + nla_total_size(1) /* IFLA_OPERSTATE */
4737b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4738339bf98fSThomas Graf }
4739c5396a31SYOSHIFUJI Hideaki 
4740be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
47417d720c3eSTejun Heo 				      int items, int bytes)
47427f7d9a6bSHerbert Xu {
47437f7d9a6bSHerbert Xu 	int i;
47447f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
47457f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
47467f7d9a6bSHerbert Xu 
47477f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
47487f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
47497f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
4750be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
47517f7d9a6bSHerbert Xu 
47527f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
47537f7d9a6bSHerbert Xu }
47547f7d9a6bSHerbert Xu 
4755698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
4756a3a77372SRaghavendra K T 					int bytes, size_t syncpoff)
47574ce3c183SEric Dumazet {
4758a3a77372SRaghavendra K T 	int i, c;
4759a3a77372SRaghavendra K T 	u64 buff[IPSTATS_MIB_MAX];
4760a3a77372SRaghavendra K T 	int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
4761a3a77372SRaghavendra K T 
47624ce3c183SEric Dumazet 	BUG_ON(pad < 0);
47634ce3c183SEric Dumazet 
4764a3a77372SRaghavendra K T 	memset(buff, 0, sizeof(buff));
4765a3a77372SRaghavendra K T 	buff[0] = IPSTATS_MIB_MAX;
47664ce3c183SEric Dumazet 
4767a3a77372SRaghavendra K T 	for_each_possible_cpu(c) {
4768a3a77372SRaghavendra K T 		for (i = 1; i < IPSTATS_MIB_MAX; i++)
4769a3a77372SRaghavendra K T 			buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
4770a3a77372SRaghavendra K T 	}
4771a3a77372SRaghavendra K T 
4772a3a77372SRaghavendra K T 	memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
4773a3a77372SRaghavendra K T 	memset(&stats[IPSTATS_MIB_MAX], 0, pad);
47744ce3c183SEric Dumazet }
47754ce3c183SEric Dumazet 
47767f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
47777f7d9a6bSHerbert Xu 			     int bytes)
47787f7d9a6bSHerbert Xu {
47797f7d9a6bSHerbert Xu 	switch (attrtype) {
47807f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
4781a3a77372SRaghavendra K T 		__snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
4782a3a77372SRaghavendra K T 				     offsetof(struct ipstats_mib, syncp));
47837f7d9a6bSHerbert Xu 		break;
47847f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
4785be281e55SEric Dumazet 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
47867f7d9a6bSHerbert Xu 		break;
47877f7d9a6bSHerbert Xu 	}
47887f7d9a6bSHerbert Xu }
47897f7d9a6bSHerbert Xu 
4790d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
4791d5566fd7SSowmini Varadhan 				  u32 ext_filter_mask)
47921da177e4SLinus Torvalds {
4793bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
47941da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
47951da177e4SLinus Torvalds 
4796c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4797c78679e8SDavid S. Miller 		goto nla_put_failure;
47981da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
479924912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
480024912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
48011f9248e5SJiri Pirko 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
4802c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4803c78679e8SDavid S. Miller 		goto nla_put_failure;
4804bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
480563159f29SIan Morris 	if (!nla)
480604561c1fSThomas Graf 		goto nla_put_failure;
4807bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
48081da177e4SLinus Torvalds 
4809bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
4810bf99f1bdSYOSHIFUJI Hideaki 
4811d5566fd7SSowmini Varadhan 	if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
4812d5566fd7SSowmini Varadhan 		return 0;
4813d5566fd7SSowmini Varadhan 
4814bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
481563159f29SIan Morris 	if (!nla)
4816bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4817bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4818bf99f1bdSYOSHIFUJI Hideaki 
4819bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
482063159f29SIan Morris 	if (!nla)
4821bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4822bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
48231da177e4SLinus Torvalds 
4824f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
482563159f29SIan Morris 	if (!nla)
4826f53adae4SDaniel Borkmann 		goto nla_put_failure;
4827bc91b0f0SJiri Pirko 
4828bc91b0f0SJiri Pirko 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
4829bc91b0f0SJiri Pirko 		goto nla_put_failure;
4830bc91b0f0SJiri Pirko 
4831f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
4832f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4833f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
4834f53adae4SDaniel Borkmann 
4835b382b191SThomas Graf 	return 0;
4836b382b191SThomas Graf 
4837b382b191SThomas Graf nla_put_failure:
4838b382b191SThomas Graf 	return -EMSGSIZE;
4839b382b191SThomas Graf }
4840b382b191SThomas Graf 
4841b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev,
4842b1974ed0SArad, Ronen 				     u32 ext_filter_mask)
4843b382b191SThomas Graf {
4844b382b191SThomas Graf 	if (!__in6_dev_get(dev))
4845b382b191SThomas Graf 		return 0;
4846b382b191SThomas Graf 
4847b382b191SThomas Graf 	return inet6_ifla6_size();
4848b382b191SThomas Graf }
4849b382b191SThomas Graf 
4850d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
4851d5566fd7SSowmini Varadhan 			      u32 ext_filter_mask)
4852b382b191SThomas Graf {
4853b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
4854b382b191SThomas Graf 
4855b382b191SThomas Graf 	if (!idev)
4856b382b191SThomas Graf 		return -ENODATA;
4857b382b191SThomas Graf 
4858d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
4859b382b191SThomas Graf 		return -EMSGSIZE;
4860b382b191SThomas Graf 
4861b382b191SThomas Graf 	return 0;
4862b382b191SThomas Graf }
4863b382b191SThomas Graf 
4864f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4865f53adae4SDaniel Borkmann {
4866f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
4867f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
4868fc403832SDaniel Borkmann 	bool update_rs = false;
4869dc848292SHannes Frederic Sowa 	struct in6_addr ll_addr;
4870f53adae4SDaniel Borkmann 
4871c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4872c15b1ccaSHannes Frederic Sowa 
487363159f29SIan Morris 	if (!token)
4874f53adae4SDaniel Borkmann 		return -EINVAL;
4875f53adae4SDaniel Borkmann 	if (ipv6_addr_any(token))
4876f53adae4SDaniel Borkmann 		return -EINVAL;
4877f53adae4SDaniel Borkmann 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4878f53adae4SDaniel Borkmann 		return -EINVAL;
4879f53adae4SDaniel Borkmann 	if (!ipv6_accept_ra(idev))
4880f53adae4SDaniel Borkmann 		return -EINVAL;
4881f53adae4SDaniel Borkmann 	if (idev->cnf.rtr_solicits <= 0)
4882f53adae4SDaniel Borkmann 		return -EINVAL;
4883f53adae4SDaniel Borkmann 
4884f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
4885f53adae4SDaniel Borkmann 
4886f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4887f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4888f53adae4SDaniel Borkmann 
4889f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
4890f53adae4SDaniel Borkmann 
4891dc848292SHannes Frederic Sowa 	if (!idev->dead && (idev->if_flags & IF_READY) &&
4892dc848292SHannes Frederic Sowa 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4893dc848292SHannes Frederic Sowa 			     IFA_F_OPTIMISTIC)) {
4894fc403832SDaniel Borkmann 
4895fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
4896fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
4897fc403832SDaniel Borkmann 		 */
4898f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4899fc403832SDaniel Borkmann 		update_rs = true;
4900fc403832SDaniel Borkmann 	}
4901f53adae4SDaniel Borkmann 
4902f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
4903fc403832SDaniel Borkmann 
490477ecaaceSHannes Frederic Sowa 	if (update_rs) {
4905f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
490677ecaaceSHannes Frederic Sowa 		idev->rs_probes = 1;
490777ecaaceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
490877ecaaceSHannes Frederic Sowa 	}
4909f53adae4SDaniel Borkmann 
4910f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
4911f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
4912f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
4913617fe29dSDaniel Borkmann 		if (ifp->tokenized) {
4914f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
4915f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
4916f53adae4SDaniel Borkmann 		}
4917f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
4918f53adae4SDaniel Borkmann 	}
4919f53adae4SDaniel Borkmann 
4920f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
4921b2ed64a9SLubomir Rintel 	inet6_ifinfo_notify(RTM_NEWLINK, idev);
4922c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4923f53adae4SDaniel Borkmann 	return 0;
4924f53adae4SDaniel Borkmann }
4925f53adae4SDaniel Borkmann 
492611b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
492711b1f828SDaniel Borkmann 	[IFLA_INET6_ADDR_GEN_MODE]	= { .type = NLA_U8 },
492811b1f828SDaniel Borkmann 	[IFLA_INET6_TOKEN]		= { .len = sizeof(struct in6_addr) },
492911b1f828SDaniel Borkmann };
493011b1f828SDaniel Borkmann 
493111b1f828SDaniel Borkmann static int inet6_validate_link_af(const struct net_device *dev,
493211b1f828SDaniel Borkmann 				  const struct nlattr *nla)
493311b1f828SDaniel Borkmann {
493411b1f828SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
493511b1f828SDaniel Borkmann 
493611b1f828SDaniel Borkmann 	if (dev && !__in6_dev_get(dev))
493711b1f828SDaniel Borkmann 		return -EAFNOSUPPORT;
493811b1f828SDaniel Borkmann 
493911b1f828SDaniel Borkmann 	return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
494011b1f828SDaniel Borkmann }
494111b1f828SDaniel Borkmann 
4942f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4943f53adae4SDaniel Borkmann {
4944f53adae4SDaniel Borkmann 	int err = -EINVAL;
4945f53adae4SDaniel Borkmann 	struct inet6_dev *idev = __in6_dev_get(dev);
4946f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
4947f53adae4SDaniel Borkmann 
4948f53adae4SDaniel Borkmann 	if (!idev)
4949f53adae4SDaniel Borkmann 		return -EAFNOSUPPORT;
4950f53adae4SDaniel Borkmann 
4951f53adae4SDaniel Borkmann 	if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4952f53adae4SDaniel Borkmann 		BUG();
4953f53adae4SDaniel Borkmann 
4954bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_TOKEN]) {
4955f53adae4SDaniel Borkmann 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4956bc91b0f0SJiri Pirko 		if (err)
4957bc91b0f0SJiri Pirko 			return err;
4958bc91b0f0SJiri Pirko 	}
4959bc91b0f0SJiri Pirko 
4960bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
4961bc91b0f0SJiri Pirko 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
4962bc91b0f0SJiri Pirko 
4963bc91b0f0SJiri Pirko 		if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
4964622c81d5SHannes Frederic Sowa 		    mode != IN6_ADDR_GEN_MODE_NONE &&
4965cc9da6ccSBjørn Mork 		    mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
4966cc9da6ccSBjørn Mork 		    mode != IN6_ADDR_GEN_MODE_RANDOM)
4967bc91b0f0SJiri Pirko 			return -EINVAL;
4968622c81d5SHannes Frederic Sowa 
4969622c81d5SHannes Frederic Sowa 		if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
4970622c81d5SHannes Frederic Sowa 		    !idev->cnf.stable_secret.initialized &&
4971622c81d5SHannes Frederic Sowa 		    !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
4972622c81d5SHannes Frederic Sowa 			return -EINVAL;
4973622c81d5SHannes Frederic Sowa 
4974bc91b0f0SJiri Pirko 		idev->addr_gen_mode = mode;
4975bc91b0f0SJiri Pirko 		err = 0;
4976bc91b0f0SJiri Pirko 	}
4977f53adae4SDaniel Borkmann 
4978f53adae4SDaniel Borkmann 	return err;
4979f53adae4SDaniel Borkmann }
4980f53adae4SDaniel Borkmann 
4981b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
498215e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
4983b382b191SThomas Graf {
4984b382b191SThomas Graf 	struct net_device *dev = idev->dev;
4985b382b191SThomas Graf 	struct ifinfomsg *hdr;
4986b382b191SThomas Graf 	struct nlmsghdr *nlh;
4987b382b191SThomas Graf 	void *protoinfo;
4988b382b191SThomas Graf 
498915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
499063159f29SIan Morris 	if (!nlh)
4991b382b191SThomas Graf 		return -EMSGSIZE;
4992b382b191SThomas Graf 
4993b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
4994b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
4995b382b191SThomas Graf 	hdr->__ifi_pad = 0;
4996b382b191SThomas Graf 	hdr->ifi_type = dev->type;
4997b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
4998b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
4999b382b191SThomas Graf 	hdr->ifi_change = 0;
5000b382b191SThomas Graf 
5001c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5002c78679e8SDavid S. Miller 	    (dev->addr_len &&
5003c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5004c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5005a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
50060344338bSAndy Gospodarek 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
50070344338bSAndy Gospodarek 	    nla_put_u8(skb, IFLA_OPERSTATE,
50080344338bSAndy Gospodarek 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5009c78679e8SDavid S. Miller 		goto nla_put_failure;
5010b382b191SThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
501163159f29SIan Morris 	if (!protoinfo)
5012b382b191SThomas Graf 		goto nla_put_failure;
5013b382b191SThomas Graf 
5014d5566fd7SSowmini Varadhan 	if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5015b382b191SThomas Graf 		goto nla_put_failure;
5016b382b191SThomas Graf 
501704561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
5018053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5019053c095aSJohannes Berg 	return 0;
50201da177e4SLinus Torvalds 
502104561c1fSThomas Graf nla_put_failure:
502226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
502326932566SPatrick McHardy 	return -EMSGSIZE;
50241da177e4SLinus Torvalds }
50251da177e4SLinus Torvalds 
50261da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
50271da177e4SLinus Torvalds {
50283b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
502984d2697dSEric Dumazet 	int h, s_h;
5030434a8a58SDavid S. Miller 	int idx = 0, s_idx;
50311da177e4SLinus Torvalds 	struct net_device *dev;
50321da177e4SLinus Torvalds 	struct inet6_dev *idev;
503384d2697dSEric Dumazet 	struct hlist_head *head;
50341da177e4SLinus Torvalds 
503584d2697dSEric Dumazet 	s_h = cb->args[0];
503684d2697dSEric Dumazet 	s_idx = cb->args[1];
503784d2697dSEric Dumazet 
503884d2697dSEric Dumazet 	rcu_read_lock();
503984d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
50407562f876SPavel Emelianov 		idx = 0;
504184d2697dSEric Dumazet 		head = &net->dev_index_head[h];
5042b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
50431da177e4SLinus Torvalds 			if (idx < s_idx)
50447562f876SPavel Emelianov 				goto cont;
504584d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
504684d2697dSEric Dumazet 			if (!idev)
50477562f876SPavel Emelianov 				goto cont;
504884d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
504915e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
505084d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
5051053c095aSJohannes Berg 					      RTM_NEWLINK, NLM_F_MULTI) < 0)
505284d2697dSEric Dumazet 				goto out;
50537562f876SPavel Emelianov cont:
50547562f876SPavel Emelianov 			idx++;
50551da177e4SLinus Torvalds 		}
505684d2697dSEric Dumazet 	}
505784d2697dSEric Dumazet out:
505884d2697dSEric Dumazet 	rcu_read_unlock();
505984d2697dSEric Dumazet 	cb->args[1] = idx;
506084d2697dSEric Dumazet 	cb->args[0] = h;
50611da177e4SLinus Torvalds 
50621da177e4SLinus Torvalds 	return skb->len;
50631da177e4SLinus Torvalds }
50641da177e4SLinus Torvalds 
50651da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
50661da177e4SLinus Torvalds {
50671da177e4SLinus Torvalds 	struct sk_buff *skb;
5068c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
50698d7a76c9SThomas Graf 	int err = -ENOBUFS;
50701da177e4SLinus Torvalds 
5071339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
507263159f29SIan Morris 	if (!skb)
50738d7a76c9SThomas Graf 		goto errout;
50748d7a76c9SThomas Graf 
50758d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
507626932566SPatrick McHardy 	if (err < 0) {
507726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
507826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
507926932566SPatrick McHardy 		kfree_skb(skb);
508026932566SPatrick McHardy 		goto errout;
508126932566SPatrick McHardy 	}
50825f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
50831ce85fe4SPablo Neira Ayuso 	return;
50848d7a76c9SThomas Graf errout:
50858d7a76c9SThomas Graf 	if (err < 0)
50865f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
50871da177e4SLinus Torvalds }
50881da177e4SLinus Torvalds 
5089339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
5090339bf98fSThomas Graf {
5091339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
5092339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
5093339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
5094339bf98fSThomas Graf }
5095c5396a31SYOSHIFUJI Hideaki 
50961da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
509715e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
5098b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
50991da177e4SLinus Torvalds {
51001da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
51011da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
51021da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
51031da177e4SLinus Torvalds 
510415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
510563159f29SIan Morris 	if (!nlh)
510626932566SPatrick McHardy 		return -EMSGSIZE;
51076051e2f4SThomas Graf 
51086051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
51091da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
51108a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
51118a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
51121da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
51131da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
51141da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
51158a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
51161da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
51171da177e4SLinus Torvalds 	if (pinfo->onlink)
51181da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
51191da177e4SLinus Torvalds 	if (pinfo->autoconf)
51201da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
51211da177e4SLinus Torvalds 
5122c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5123c78679e8SDavid S. Miller 		goto nla_put_failure;
51241da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
51251da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
5126c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5127c78679e8SDavid S. Miller 		goto nla_put_failure;
5128053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5129053c095aSJohannes Berg 	return 0;
51301da177e4SLinus Torvalds 
51316051e2f4SThomas Graf nla_put_failure:
513226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
513326932566SPatrick McHardy 	return -EMSGSIZE;
51341da177e4SLinus Torvalds }
51351da177e4SLinus Torvalds 
51361da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
51371da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
51381da177e4SLinus Torvalds {
51391da177e4SLinus Torvalds 	struct sk_buff *skb;
5140c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
51418c384bfaSThomas Graf 	int err = -ENOBUFS;
51421da177e4SLinus Torvalds 
5143339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
514463159f29SIan Morris 	if (!skb)
51458c384bfaSThomas Graf 		goto errout;
51468c384bfaSThomas Graf 
51478c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
514826932566SPatrick McHardy 	if (err < 0) {
514926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
515026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
515126932566SPatrick McHardy 		kfree_skb(skb);
515226932566SPatrick McHardy 		goto errout;
515326932566SPatrick McHardy 	}
51541ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
51551ce85fe4SPablo Neira Ayuso 	return;
51568c384bfaSThomas Graf errout:
51578c384bfaSThomas Graf 	if (err < 0)
51586fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
51591da177e4SLinus Torvalds }
51601da177e4SLinus Torvalds 
51611da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
51621da177e4SLinus Torvalds {
516363998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
516463998ac2SNicolas Dichtel 
5165c15b1ccaSHannes Frederic Sowa 	if (event)
5166c15b1ccaSHannes Frederic Sowa 		ASSERT_RTNL();
5167c15b1ccaSHannes Frederic Sowa 
51681da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
51691da177e4SLinus Torvalds 
51701da177e4SLinus Torvalds 	switch (event) {
51711da177e4SLinus Torvalds 	case RTM_NEWADDR:
517295c385b4SNeil Horman 		/*
517395c385b4SNeil Horman 		 * If the address was optimistic
517495c385b4SNeil Horman 		 * we inserted the route at the start of
517595c385b4SNeil Horman 		 * our DAD process, so we don't need
517695c385b4SNeil Horman 		 * to do it again
517795c385b4SNeil Horman 		 */
517895c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
517940e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
51801da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
51811da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
51827996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr))
5183caeaba79SNicolas Dichtel 			addrconf_prefix_route(&ifp->peer_addr, 128,
5184caeaba79SNicolas Dichtel 					      ifp->idev->dev, 0, 0);
51851da177e4SLinus Torvalds 		break;
51861da177e4SLinus Torvalds 	case RTM_DELADDR:
51871da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
51881da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
51891da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
51907996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr)) {
5191caeaba79SNicolas Dichtel 			struct rt6_info *rt;
5192caeaba79SNicolas Dichtel 
5193e7478dfcSNicolas Dichtel 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5194e7478dfcSNicolas Dichtel 						       ifp->idev->dev, 0, 0);
51958e3d5be7SMartin KaFai Lau 			if (rt)
51968e3d5be7SMartin KaFai Lau 				ip6_del_rt(rt);
5197caeaba79SNicolas Dichtel 		}
5198d8d1f30bSChangli Gao 		dst_hold(&ifp->rt->dst);
519993fa159aSstephen hemminger 
52008e3d5be7SMartin KaFai Lau 		ip6_del_rt(ifp->rt);
5201705f1c86SHannes Frederic Sowa 
5202705f1c86SHannes Frederic Sowa 		rt_genid_bump_ipv6(net);
52031da177e4SLinus Torvalds 		break;
52041da177e4SLinus Torvalds 	}
520563998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
52061da177e4SLinus Torvalds }
52071da177e4SLinus Torvalds 
52081da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
52091da177e4SLinus Torvalds {
52108814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
52111da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
52121da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
52138814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
52141da177e4SLinus Torvalds }
52151da177e4SLinus Torvalds 
52161da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
52171da177e4SLinus Torvalds 
52181da177e4SLinus Torvalds static
5219fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
52201da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
52211da177e4SLinus Torvalds {
52221da177e4SLinus Torvalds 	int *valp = ctl->data;
52231da177e4SLinus Torvalds 	int val = *valp;
522488af182eSEric W. Biederman 	loff_t pos = *ppos;
5225fe2c6338SJoe Perches 	struct ctl_table lctl;
52261da177e4SLinus Torvalds 	int ret;
52271da177e4SLinus Torvalds 
5228013d97e9SFrancesco Ruggeri 	/*
5229013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
5230013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5231013d97e9SFrancesco Ruggeri 	 */
5232013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5233013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5234013d97e9SFrancesco Ruggeri 
5235013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
52361da177e4SLinus Torvalds 
5237c8fecf22SPavel Emelyanov 	if (write)
5238b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
523988af182eSEric W. Biederman 	if (ret)
524088af182eSEric W. Biederman 		*ppos = pos;
52411da177e4SLinus Torvalds 	return ret;
52421da177e4SLinus Torvalds }
52431da177e4SLinus Torvalds 
524477751427SMarcelo Leitner static
5245d6df198dSPhil Sutter int addrconf_sysctl_hop_limit(struct ctl_table *ctl, int write,
5246d6df198dSPhil Sutter                               void __user *buffer, size_t *lenp, loff_t *ppos)
5247d6df198dSPhil Sutter {
5248d6df198dSPhil Sutter 	struct ctl_table lctl;
5249d6df198dSPhil Sutter 	int min_hl = 1, max_hl = 255;
5250d6df198dSPhil Sutter 
5251d6df198dSPhil Sutter 	lctl = *ctl;
5252d6df198dSPhil Sutter 	lctl.extra1 = &min_hl;
5253d6df198dSPhil Sutter 	lctl.extra2 = &max_hl;
5254d6df198dSPhil Sutter 
5255d6df198dSPhil Sutter 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5256d6df198dSPhil Sutter }
5257d6df198dSPhil Sutter 
5258d6df198dSPhil Sutter static
525977751427SMarcelo Leitner int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
526077751427SMarcelo Leitner 			void __user *buffer, size_t *lenp, loff_t *ppos)
526177751427SMarcelo Leitner {
526277751427SMarcelo Leitner 	struct inet6_dev *idev = ctl->extra1;
526377751427SMarcelo Leitner 	int min_mtu = IPV6_MIN_MTU;
526477751427SMarcelo Leitner 	struct ctl_table lctl;
526577751427SMarcelo Leitner 
526677751427SMarcelo Leitner 	lctl = *ctl;
526777751427SMarcelo Leitner 	lctl.extra1 = &min_mtu;
526877751427SMarcelo Leitner 	lctl.extra2 = idev ? &idev->dev->mtu : NULL;
526977751427SMarcelo Leitner 
527077751427SMarcelo Leitner 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
527177751427SMarcelo Leitner }
527277751427SMarcelo Leitner 
527356d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
527456d417b1SBrian Haley {
527575538c2bSCong Wang 	struct netdev_notifier_info info;
527675538c2bSCong Wang 
527756d417b1SBrian Haley 	if (!idev || !idev->dev)
527856d417b1SBrian Haley 		return;
527956d417b1SBrian Haley 
528075538c2bSCong Wang 	netdev_notifier_info_init(&info, idev->dev);
528156d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
528275538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_DOWN, &info);
528356d417b1SBrian Haley 	else
528475538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_UP, &info);
528556d417b1SBrian Haley }
528656d417b1SBrian Haley 
528756d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
528856d417b1SBrian Haley {
528956d417b1SBrian Haley 	struct net_device *dev;
529056d417b1SBrian Haley 	struct inet6_dev *idev;
529156d417b1SBrian Haley 
529256d417b1SBrian Haley 	rcu_read_lock();
5293c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
529456d417b1SBrian Haley 		idev = __in6_dev_get(dev);
529556d417b1SBrian Haley 		if (idev) {
529656d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
529756d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
529856d417b1SBrian Haley 			if (changed)
529956d417b1SBrian Haley 				dev_disable_change(idev);
530056d417b1SBrian Haley 		}
530156d417b1SBrian Haley 	}
5302c6d14c84SEric Dumazet 	rcu_read_unlock();
530356d417b1SBrian Haley }
530456d417b1SBrian Haley 
5305013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
530656d417b1SBrian Haley {
530756d417b1SBrian Haley 	struct net *net;
5308013d97e9SFrancesco Ruggeri 	int old;
5309013d97e9SFrancesco Ruggeri 
5310013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
5311013d97e9SFrancesco Ruggeri 		return restart_syscall();
531256d417b1SBrian Haley 
531356d417b1SBrian Haley 	net = (struct net *)table->extra2;
5314013d97e9SFrancesco Ruggeri 	old = *p;
5315013d97e9SFrancesco Ruggeri 	*p = newf;
531656d417b1SBrian Haley 
5317013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5318013d97e9SFrancesco Ruggeri 		rtnl_unlock();
531956d417b1SBrian Haley 		return 0;
532088af182eSEric W. Biederman 	}
532156d417b1SBrian Haley 
532256d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
532356d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
532456d417b1SBrian Haley 		addrconf_disable_change(net, newf);
5325013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
532656d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
532756d417b1SBrian Haley 
532856d417b1SBrian Haley 	rtnl_unlock();
532956d417b1SBrian Haley 	return 0;
533056d417b1SBrian Haley }
533156d417b1SBrian Haley 
533256d417b1SBrian Haley static
5333fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
533456d417b1SBrian Haley 			    void __user *buffer, size_t *lenp, loff_t *ppos)
533556d417b1SBrian Haley {
533656d417b1SBrian Haley 	int *valp = ctl->data;
533756d417b1SBrian Haley 	int val = *valp;
533888af182eSEric W. Biederman 	loff_t pos = *ppos;
5339fe2c6338SJoe Perches 	struct ctl_table lctl;
534056d417b1SBrian Haley 	int ret;
534156d417b1SBrian Haley 
5342013d97e9SFrancesco Ruggeri 	/*
5343013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
5344013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
5345013d97e9SFrancesco Ruggeri 	 */
5346013d97e9SFrancesco Ruggeri 	lctl = *ctl;
5347013d97e9SFrancesco Ruggeri 	lctl.data = &val;
5348013d97e9SFrancesco Ruggeri 
5349013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
535056d417b1SBrian Haley 
535156d417b1SBrian Haley 	if (write)
535256d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
535388af182eSEric W. Biederman 	if (ret)
535488af182eSEric W. Biederman 		*ppos = pos;
535556d417b1SBrian Haley 	return ret;
535656d417b1SBrian Haley }
535756d417b1SBrian Haley 
5358c92d5491Sstephen hemminger static
5359c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5360c92d5491Sstephen hemminger 			      void __user *buffer, size_t *lenp, loff_t *ppos)
5361c92d5491Sstephen hemminger {
5362c92d5491Sstephen hemminger 	int *valp = ctl->data;
5363c92d5491Sstephen hemminger 	int ret;
5364c92d5491Sstephen hemminger 	int old, new;
5365c92d5491Sstephen hemminger 
5366c92d5491Sstephen hemminger 	old = *valp;
5367c92d5491Sstephen hemminger 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5368c92d5491Sstephen hemminger 	new = *valp;
5369c92d5491Sstephen hemminger 
5370c92d5491Sstephen hemminger 	if (write && old != new) {
5371c92d5491Sstephen hemminger 		struct net *net = ctl->extra2;
5372c92d5491Sstephen hemminger 
5373c92d5491Sstephen hemminger 		if (!rtnl_trylock())
5374c92d5491Sstephen hemminger 			return restart_syscall();
5375c92d5491Sstephen hemminger 
5376c92d5491Sstephen hemminger 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5377c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5378c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_DEFAULT,
5379c92d5491Sstephen hemminger 						     net->ipv6.devconf_dflt);
5380c92d5491Sstephen hemminger 		else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5381c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5382c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_ALL,
5383c92d5491Sstephen hemminger 						     net->ipv6.devconf_all);
5384c92d5491Sstephen hemminger 		else {
5385c92d5491Sstephen hemminger 			struct inet6_dev *idev = ctl->extra1;
5386c92d5491Sstephen hemminger 
5387c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5388c92d5491Sstephen hemminger 						     idev->dev->ifindex,
5389c92d5491Sstephen hemminger 						     &idev->cnf);
5390c92d5491Sstephen hemminger 		}
5391c92d5491Sstephen hemminger 		rtnl_unlock();
5392c92d5491Sstephen hemminger 	}
5393c92d5491Sstephen hemminger 
5394c92d5491Sstephen hemminger 	return ret;
5395c92d5491Sstephen hemminger }
5396c92d5491Sstephen hemminger 
53973d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
53983d1bec99SHannes Frederic Sowa 					 void __user *buffer, size_t *lenp,
53993d1bec99SHannes Frederic Sowa 					 loff_t *ppos)
54003d1bec99SHannes Frederic Sowa {
54013d1bec99SHannes Frederic Sowa 	int err;
54023d1bec99SHannes Frederic Sowa 	struct in6_addr addr;
54033d1bec99SHannes Frederic Sowa 	char str[IPV6_MAX_STRLEN];
54043d1bec99SHannes Frederic Sowa 	struct ctl_table lctl = *ctl;
5405622c81d5SHannes Frederic Sowa 	struct net *net = ctl->extra2;
54063d1bec99SHannes Frederic Sowa 	struct ipv6_stable_secret *secret = ctl->data;
54073d1bec99SHannes Frederic Sowa 
5408622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5409622c81d5SHannes Frederic Sowa 		return -EIO;
5410622c81d5SHannes Frederic Sowa 
54113d1bec99SHannes Frederic Sowa 	lctl.maxlen = IPV6_MAX_STRLEN;
54123d1bec99SHannes Frederic Sowa 	lctl.data = str;
54133d1bec99SHannes Frederic Sowa 
54143d1bec99SHannes Frederic Sowa 	if (!rtnl_trylock())
54153d1bec99SHannes Frederic Sowa 		return restart_syscall();
54163d1bec99SHannes Frederic Sowa 
54173d1bec99SHannes Frederic Sowa 	if (!write && !secret->initialized) {
54183d1bec99SHannes Frederic Sowa 		err = -EIO;
54193d1bec99SHannes Frederic Sowa 		goto out;
54203d1bec99SHannes Frederic Sowa 	}
54213d1bec99SHannes Frederic Sowa 
54225449a5caSWANG Cong 	err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
54233d1bec99SHannes Frederic Sowa 	if (err >= sizeof(str)) {
54243d1bec99SHannes Frederic Sowa 		err = -EIO;
54253d1bec99SHannes Frederic Sowa 		goto out;
54263d1bec99SHannes Frederic Sowa 	}
54273d1bec99SHannes Frederic Sowa 
54283d1bec99SHannes Frederic Sowa 	err = proc_dostring(&lctl, write, buffer, lenp, ppos);
54293d1bec99SHannes Frederic Sowa 	if (err || !write)
54303d1bec99SHannes Frederic Sowa 		goto out;
54313d1bec99SHannes Frederic Sowa 
54323d1bec99SHannes Frederic Sowa 	if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
54333d1bec99SHannes Frederic Sowa 		err = -EIO;
54343d1bec99SHannes Frederic Sowa 		goto out;
54353d1bec99SHannes Frederic Sowa 	}
54363d1bec99SHannes Frederic Sowa 
54373d1bec99SHannes Frederic Sowa 	secret->initialized = true;
54383d1bec99SHannes Frederic Sowa 	secret->secret = addr;
54393d1bec99SHannes Frederic Sowa 
5440622c81d5SHannes Frederic Sowa 	if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5441622c81d5SHannes Frederic Sowa 		struct net_device *dev;
5442622c81d5SHannes Frederic Sowa 
5443622c81d5SHannes Frederic Sowa 		for_each_netdev(net, dev) {
5444622c81d5SHannes Frederic Sowa 			struct inet6_dev *idev = __in6_dev_get(dev);
5445622c81d5SHannes Frederic Sowa 
5446622c81d5SHannes Frederic Sowa 			if (idev) {
5447622c81d5SHannes Frederic Sowa 				idev->addr_gen_mode =
5448622c81d5SHannes Frederic Sowa 					IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5449622c81d5SHannes Frederic Sowa 			}
5450622c81d5SHannes Frederic Sowa 		}
5451622c81d5SHannes Frederic Sowa 	} else {
5452622c81d5SHannes Frederic Sowa 		struct inet6_dev *idev = ctl->extra1;
5453622c81d5SHannes Frederic Sowa 
5454622c81d5SHannes Frederic Sowa 		idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5455622c81d5SHannes Frederic Sowa 	}
5456622c81d5SHannes Frederic Sowa 
54573d1bec99SHannes Frederic Sowa out:
54583d1bec99SHannes Frederic Sowa 	rtnl_unlock();
54593d1bec99SHannes Frederic Sowa 
54603d1bec99SHannes Frederic Sowa 	return err;
54613d1bec99SHannes Frederic Sowa }
5462c92d5491Sstephen hemminger 
546335103d11SAndy Gospodarek static
546435103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
546535103d11SAndy Gospodarek 						int write,
546635103d11SAndy Gospodarek 						void __user *buffer,
546735103d11SAndy Gospodarek 						size_t *lenp,
546835103d11SAndy Gospodarek 						loff_t *ppos)
546935103d11SAndy Gospodarek {
547035103d11SAndy Gospodarek 	int *valp = ctl->data;
547135103d11SAndy Gospodarek 	int val = *valp;
547235103d11SAndy Gospodarek 	loff_t pos = *ppos;
547335103d11SAndy Gospodarek 	struct ctl_table lctl;
547435103d11SAndy Gospodarek 	int ret;
547535103d11SAndy Gospodarek 
547635103d11SAndy Gospodarek 	/* ctl->data points to idev->cnf.ignore_routes_when_linkdown
547735103d11SAndy Gospodarek 	 * we should not modify it until we get the rtnl lock.
547835103d11SAndy Gospodarek 	 */
547935103d11SAndy Gospodarek 	lctl = *ctl;
548035103d11SAndy Gospodarek 	lctl.data = &val;
548135103d11SAndy Gospodarek 
548235103d11SAndy Gospodarek 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
548335103d11SAndy Gospodarek 
548435103d11SAndy Gospodarek 	if (write)
548535103d11SAndy Gospodarek 		ret = addrconf_fixup_linkdown(ctl, valp, val);
548635103d11SAndy Gospodarek 	if (ret)
548735103d11SAndy Gospodarek 		*ppos = pos;
548835103d11SAndy Gospodarek 	return ret;
548935103d11SAndy Gospodarek }
549035103d11SAndy Gospodarek 
54911da177e4SLinus Torvalds static struct addrconf_sysctl_table
54921da177e4SLinus Torvalds {
54931da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
5494fe2c6338SJoe Perches 	struct ctl_table addrconf_vars[DEVCONF_MAX+1];
5495ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
54961da177e4SLinus Torvalds 	.sysctl_header = NULL,
54971da177e4SLinus Torvalds 	.addrconf_vars = {
54981da177e4SLinus Torvalds 		{
54991da177e4SLinus Torvalds 			.procname	= "forwarding",
55001da177e4SLinus Torvalds 			.data		= &ipv6_devconf.forwarding,
55011da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55021da177e4SLinus Torvalds 			.mode		= 0644,
55036d9f239aSAlexey Dobriyan 			.proc_handler	= addrconf_sysctl_forward,
55041da177e4SLinus Torvalds 		},
55051da177e4SLinus Torvalds 		{
55061da177e4SLinus Torvalds 			.procname	= "hop_limit",
55071da177e4SLinus Torvalds 			.data		= &ipv6_devconf.hop_limit,
55081da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55091da177e4SLinus Torvalds 			.mode		= 0644,
5510d6df198dSPhil Sutter 			.proc_handler	= addrconf_sysctl_hop_limit,
55111da177e4SLinus Torvalds 		},
55121da177e4SLinus Torvalds 		{
55131da177e4SLinus Torvalds 			.procname	= "mtu",
55141da177e4SLinus Torvalds 			.data		= &ipv6_devconf.mtu6,
55151da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55161da177e4SLinus Torvalds 			.mode		= 0644,
551777751427SMarcelo Leitner 			.proc_handler	= addrconf_sysctl_mtu,
55181da177e4SLinus Torvalds 		},
55191da177e4SLinus Torvalds 		{
55201da177e4SLinus Torvalds 			.procname	= "accept_ra",
55211da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_ra,
55221da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55231da177e4SLinus Torvalds 			.mode		= 0644,
55246d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55251da177e4SLinus Torvalds 		},
55261da177e4SLinus Torvalds 		{
55271da177e4SLinus Torvalds 			.procname	= "accept_redirects",
55281da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_redirects,
55291da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55301da177e4SLinus Torvalds 			.mode		= 0644,
55316d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55321da177e4SLinus Torvalds 		},
55331da177e4SLinus Torvalds 		{
55341da177e4SLinus Torvalds 			.procname	= "autoconf",
55351da177e4SLinus Torvalds 			.data		= &ipv6_devconf.autoconf,
55361da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55371da177e4SLinus Torvalds 			.mode		= 0644,
55386d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55391da177e4SLinus Torvalds 		},
55401da177e4SLinus Torvalds 		{
55411da177e4SLinus Torvalds 			.procname	= "dad_transmits",
55421da177e4SLinus Torvalds 			.data		= &ipv6_devconf.dad_transmits,
55431da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55441da177e4SLinus Torvalds 			.mode		= 0644,
55456d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55461da177e4SLinus Torvalds 		},
55471da177e4SLinus Torvalds 		{
55481da177e4SLinus Torvalds 			.procname	= "router_solicitations",
55491da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicits,
55501da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55511da177e4SLinus Torvalds 			.mode		= 0644,
55526d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55531da177e4SLinus Torvalds 		},
55541da177e4SLinus Torvalds 		{
55551da177e4SLinus Torvalds 			.procname	= "router_solicitation_interval",
55561da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_interval,
55571da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55581da177e4SLinus Torvalds 			.mode		= 0644,
55596d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
55601da177e4SLinus Torvalds 		},
55611da177e4SLinus Torvalds 		{
55621da177e4SLinus Torvalds 			.procname	= "router_solicitation_delay",
55631da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_delay,
55641da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55651da177e4SLinus Torvalds 			.mode		= 0644,
55666d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
55671da177e4SLinus Torvalds 		},
55681da177e4SLinus Torvalds 		{
55691da177e4SLinus Torvalds 			.procname	= "force_mld_version",
55701da177e4SLinus Torvalds 			.data		= &ipv6_devconf.force_mld_version,
55711da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55721da177e4SLinus Torvalds 			.mode		= 0644,
55736d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55741da177e4SLinus Torvalds 		},
5575fc4eba58SHannes Frederic Sowa 		{
5576fc4eba58SHannes Frederic Sowa 			.procname	= "mldv1_unsolicited_report_interval",
5577fc4eba58SHannes Frederic Sowa 			.data		=
5578fc4eba58SHannes Frederic Sowa 				&ipv6_devconf.mldv1_unsolicited_report_interval,
5579fc4eba58SHannes Frederic Sowa 			.maxlen		= sizeof(int),
5580fc4eba58SHannes Frederic Sowa 			.mode		= 0644,
5581fc4eba58SHannes Frederic Sowa 			.proc_handler	= proc_dointvec_ms_jiffies,
5582fc4eba58SHannes Frederic Sowa 		},
5583fc4eba58SHannes Frederic Sowa 		{
5584fc4eba58SHannes Frederic Sowa 			.procname	= "mldv2_unsolicited_report_interval",
5585fc4eba58SHannes Frederic Sowa 			.data		=
5586fc4eba58SHannes Frederic Sowa 				&ipv6_devconf.mldv2_unsolicited_report_interval,
5587fc4eba58SHannes Frederic Sowa 			.maxlen		= sizeof(int),
5588fc4eba58SHannes Frederic Sowa 			.mode		= 0644,
5589fc4eba58SHannes Frederic Sowa 			.proc_handler	= proc_dointvec_ms_jiffies,
5590fc4eba58SHannes Frederic Sowa 		},
55911da177e4SLinus Torvalds 		{
55921da177e4SLinus Torvalds 			.procname	= "use_tempaddr",
55931da177e4SLinus Torvalds 			.data		= &ipv6_devconf.use_tempaddr,
55941da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
55951da177e4SLinus Torvalds 			.mode		= 0644,
55966d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
55971da177e4SLinus Torvalds 		},
55981da177e4SLinus Torvalds 		{
55991da177e4SLinus Torvalds 			.procname	= "temp_valid_lft",
56001da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_valid_lft,
56011da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
56021da177e4SLinus Torvalds 			.mode		= 0644,
56036d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
56041da177e4SLinus Torvalds 		},
56051da177e4SLinus Torvalds 		{
56061da177e4SLinus Torvalds 			.procname	= "temp_prefered_lft",
56071da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_prefered_lft,
56081da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
56091da177e4SLinus Torvalds 			.mode		= 0644,
56106d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
56111da177e4SLinus Torvalds 		},
56121da177e4SLinus Torvalds 		{
56131da177e4SLinus Torvalds 			.procname	= "regen_max_retry",
56141da177e4SLinus Torvalds 			.data		= &ipv6_devconf.regen_max_retry,
56151da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
56161da177e4SLinus Torvalds 			.mode		= 0644,
56176d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
56181da177e4SLinus Torvalds 		},
56191da177e4SLinus Torvalds 		{
56201da177e4SLinus Torvalds 			.procname	= "max_desync_factor",
56211da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_desync_factor,
56221da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
56231da177e4SLinus Torvalds 			.mode		= 0644,
56246d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
56251da177e4SLinus Torvalds 		},
56261da177e4SLinus Torvalds 		{
56271da177e4SLinus Torvalds 			.procname	= "max_addresses",
56281da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_addresses,
56291da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
56301da177e4SLinus Torvalds 			.mode		= 0644,
56316d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
56321da177e4SLinus Torvalds 		},
56331da177e4SLinus Torvalds 		{
563465f5c7c1SYOSHIFUJI Hideaki 			.procname	= "accept_ra_defrtr",
563565f5c7c1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_defrtr,
563665f5c7c1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
563765f5c7c1SYOSHIFUJI Hideaki 			.mode		= 0644,
56386d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
563965f5c7c1SYOSHIFUJI Hideaki 		},
564065f5c7c1SYOSHIFUJI Hideaki 		{
56418013d1d7SHangbin Liu 			.procname	= "accept_ra_min_hop_limit",
56428013d1d7SHangbin Liu 			.data		= &ipv6_devconf.accept_ra_min_hop_limit,
56438013d1d7SHangbin Liu 			.maxlen		= sizeof(int),
56448013d1d7SHangbin Liu 			.mode		= 0644,
56458013d1d7SHangbin Liu 			.proc_handler	= proc_dointvec,
56468013d1d7SHangbin Liu 		},
56478013d1d7SHangbin Liu 		{
5648c4fd30ebSYOSHIFUJI Hideaki 			.procname	= "accept_ra_pinfo",
5649c4fd30ebSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_pinfo,
5650c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5651c4fd30ebSYOSHIFUJI Hideaki 			.mode		= 0644,
56526d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
5653c4fd30ebSYOSHIFUJI Hideaki 		},
5654930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
5655930d6ff2SYOSHIFUJI Hideaki 		{
5656930d6ff2SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rtr_pref",
5657930d6ff2SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rtr_pref,
5658930d6ff2SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5659930d6ff2SYOSHIFUJI Hideaki 			.mode		= 0644,
56606d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
5661930d6ff2SYOSHIFUJI Hideaki 		},
566252e16356SYOSHIFUJI Hideaki 		{
566352e16356SYOSHIFUJI Hideaki 			.procname	= "router_probe_interval",
566452e16356SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.rtr_probe_interval,
566552e16356SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
566652e16356SYOSHIFUJI Hideaki 			.mode		= 0644,
56676d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
566852e16356SYOSHIFUJI Hideaki 		},
5669fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
567009c884d4SYOSHIFUJI Hideaki 		{
567109c884d4SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rt_info_max_plen",
567209c884d4SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
567309c884d4SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
567409c884d4SYOSHIFUJI Hideaki 			.mode		= 0644,
56756d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
567609c884d4SYOSHIFUJI Hideaki 		},
567709c884d4SYOSHIFUJI Hideaki #endif
5678930d6ff2SYOSHIFUJI Hideaki #endif
5679c4fd30ebSYOSHIFUJI Hideaki 		{
5680fbea49e1SYOSHIFUJI Hideaki 			.procname	= "proxy_ndp",
5681fbea49e1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.proxy_ndp,
5682fbea49e1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5683fbea49e1SYOSHIFUJI Hideaki 			.mode		= 0644,
5684c92d5491Sstephen hemminger 			.proc_handler	= addrconf_sysctl_proxy_ndp,
5685fbea49e1SYOSHIFUJI Hideaki 		},
5686fbea49e1SYOSHIFUJI Hideaki 		{
56870bcbc926SYOSHIFUJI Hideaki 			.procname	= "accept_source_route",
56880bcbc926SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_source_route,
56890bcbc926SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
56900bcbc926SYOSHIFUJI Hideaki 			.mode		= 0644,
56916d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
56920bcbc926SYOSHIFUJI Hideaki 		},
569395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
569495c385b4SNeil Horman 		{
569595c385b4SNeil Horman 			.procname       = "optimistic_dad",
569695c385b4SNeil Horman 			.data           = &ipv6_devconf.optimistic_dad,
569795c385b4SNeil Horman 			.maxlen         = sizeof(int),
569895c385b4SNeil Horman 			.mode           = 0644,
56996d9f239aSAlexey Dobriyan 			.proc_handler   = proc_dointvec,
570095c385b4SNeil Horman 
570195c385b4SNeil Horman 		},
57027fd2561eSErik Kline 		{
57037fd2561eSErik Kline 			.procname       = "use_optimistic",
57047fd2561eSErik Kline 			.data           = &ipv6_devconf.use_optimistic,
57057fd2561eSErik Kline 			.maxlen         = sizeof(int),
57067fd2561eSErik Kline 			.mode           = 0644,
57077fd2561eSErik Kline 			.proc_handler   = proc_dointvec,
57087fd2561eSErik Kline 
57097fd2561eSErik Kline 		},
571095c385b4SNeil Horman #endif
57117bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
57127bc570c8SYOSHIFUJI Hideaki 		{
57137bc570c8SYOSHIFUJI Hideaki 			.procname	= "mc_forwarding",
57147bc570c8SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.mc_forwarding,
57157bc570c8SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5716a4e6db07SDavid S. Miller 			.mode		= 0444,
57176d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
57187bc570c8SYOSHIFUJI Hideaki 		},
57197bc570c8SYOSHIFUJI Hideaki #endif
57200bcbc926SYOSHIFUJI Hideaki 		{
5721778d80beSYOSHIFUJI Hideaki 			.procname	= "disable_ipv6",
5722778d80beSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.disable_ipv6,
5723778d80beSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5724778d80beSYOSHIFUJI Hideaki 			.mode		= 0644,
572556d417b1SBrian Haley 			.proc_handler	= addrconf_sysctl_disable,
5726778d80beSYOSHIFUJI Hideaki 		},
5727778d80beSYOSHIFUJI Hideaki 		{
57281b34be74SYOSHIFUJI Hideaki 			.procname	= "accept_dad",
57291b34be74SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_dad,
57301b34be74SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
57311b34be74SYOSHIFUJI Hideaki 			.mode		= 0644,
57326d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
57331b34be74SYOSHIFUJI Hideaki 		},
57341b34be74SYOSHIFUJI Hideaki 		{
5735f7734fdfSOctavian Purdila 			.procname       = "force_tllao",
5736f7734fdfSOctavian Purdila 			.data           = &ipv6_devconf.force_tllao,
5737f7734fdfSOctavian Purdila 			.maxlen         = sizeof(int),
5738f7734fdfSOctavian Purdila 			.mode           = 0644,
5739f7734fdfSOctavian Purdila 			.proc_handler   = proc_dointvec
5740f7734fdfSOctavian Purdila 		},
5741f7734fdfSOctavian Purdila 		{
57425cb04436SHannes Frederic Sowa 			.procname       = "ndisc_notify",
57435cb04436SHannes Frederic Sowa 			.data           = &ipv6_devconf.ndisc_notify,
57445cb04436SHannes Frederic Sowa 			.maxlen         = sizeof(int),
57455cb04436SHannes Frederic Sowa 			.mode           = 0644,
57465cb04436SHannes Frederic Sowa 			.proc_handler   = proc_dointvec
57475cb04436SHannes Frederic Sowa 		},
57485cb04436SHannes Frederic Sowa 		{
5749b800c3b9SHannes Frederic Sowa 			.procname	= "suppress_frag_ndisc",
5750b800c3b9SHannes Frederic Sowa 			.data		= &ipv6_devconf.suppress_frag_ndisc,
5751b800c3b9SHannes Frederic Sowa 			.maxlen		= sizeof(int),
5752b800c3b9SHannes Frederic Sowa 			.mode		= 0644,
5753b800c3b9SHannes Frederic Sowa 			.proc_handler	= proc_dointvec
5754b800c3b9SHannes Frederic Sowa 		},
5755b800c3b9SHannes Frederic Sowa 		{
5756d9333196SBen Greear 			.procname	= "accept_ra_from_local",
5757d9333196SBen Greear 			.data		= &ipv6_devconf.accept_ra_from_local,
5758d9333196SBen Greear 			.maxlen		= sizeof(int),
5759d9333196SBen Greear 			.mode		= 0644,
5760d9333196SBen Greear 			.proc_handler	= proc_dointvec,
5761d9333196SBen Greear 		},
5762d9333196SBen Greear 		{
5763c2943f14SHarout Hedeshian 			.procname	= "accept_ra_mtu",
5764c2943f14SHarout Hedeshian 			.data		= &ipv6_devconf.accept_ra_mtu,
5765c2943f14SHarout Hedeshian 			.maxlen		= sizeof(int),
5766c2943f14SHarout Hedeshian 			.mode		= 0644,
5767c2943f14SHarout Hedeshian 			.proc_handler	= proc_dointvec,
5768c2943f14SHarout Hedeshian 		},
5769c2943f14SHarout Hedeshian 		{
57703d1bec99SHannes Frederic Sowa 			.procname	= "stable_secret",
57713d1bec99SHannes Frederic Sowa 			.data		= &ipv6_devconf.stable_secret,
57723d1bec99SHannes Frederic Sowa 			.maxlen		= IPV6_MAX_STRLEN,
57733d1bec99SHannes Frederic Sowa 			.mode		= 0600,
57743d1bec99SHannes Frederic Sowa 			.proc_handler	= addrconf_sysctl_stable_secret,
57753d1bec99SHannes Frederic Sowa 		},
57763d1bec99SHannes Frederic Sowa 		{
57773985e8a3SErik Kline 			.procname       = "use_oif_addrs_only",
57783985e8a3SErik Kline 			.data           = &ipv6_devconf.use_oif_addrs_only,
57793985e8a3SErik Kline 			.maxlen         = sizeof(int),
57803985e8a3SErik Kline 			.mode           = 0644,
57813985e8a3SErik Kline 			.proc_handler   = proc_dointvec,
578235103d11SAndy Gospodarek 		},
578335103d11SAndy Gospodarek 		{
578435103d11SAndy Gospodarek 			.procname	= "ignore_routes_with_linkdown",
578535103d11SAndy Gospodarek 			.data		= &ipv6_devconf.ignore_routes_with_linkdown,
578635103d11SAndy Gospodarek 			.maxlen		= sizeof(int),
578735103d11SAndy Gospodarek 			.mode		= 0644,
578835103d11SAndy Gospodarek 			.proc_handler	= addrconf_sysctl_ignore_routes_with_linkdown,
57893985e8a3SErik Kline 		},
57903985e8a3SErik Kline 		{
5791f8572d8fSEric W. Biederman 			/* sentinel */
57921da177e4SLinus Torvalds 		}
57931da177e4SLinus Torvalds 	},
57941da177e4SLinus Torvalds };
57951da177e4SLinus Torvalds 
5796bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5797f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
57981da177e4SLinus Torvalds {
57991da177e4SLinus Torvalds 	int i;
58001da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
58016105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
58021dab6222SPavel Emelyanov 
5803af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
580463159f29SIan Morris 	if (!t)
5805f68635e6SPavel Emelyanov 		goto out;
5806f68635e6SPavel Emelyanov 
58071da177e4SLinus Torvalds 	for (i = 0; t->addrconf_vars[i].data; i++) {
58081da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
58091da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
5810bff16c2fSPavel Emelyanov 		t->addrconf_vars[i].extra2 = net;
58111da177e4SLinus Torvalds 	}
58121da177e4SLinus Torvalds 
58136105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
58141da177e4SLinus Torvalds 
58156105e293SEric W. Biederman 	t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
581663159f29SIan Morris 	if (!t->sysctl_header)
58176105e293SEric W. Biederman 		goto free;
5818f68635e6SPavel Emelyanov 
58191da177e4SLinus Torvalds 	p->sysctl = t;
582095897312SPavel Emelyanov 	return 0;
58211da177e4SLinus Torvalds 
58221da177e4SLinus Torvalds free:
58231da177e4SLinus Torvalds 	kfree(t);
5824f68635e6SPavel Emelyanov out:
582595897312SPavel Emelyanov 	return -ENOBUFS;
58261da177e4SLinus Torvalds }
58271da177e4SLinus Torvalds 
5828408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5829f52295a9SPavel Emelyanov {
5830408c4768SPavel Emelyanov 	struct addrconf_sysctl_table *t;
5831f52295a9SPavel Emelyanov 
583263159f29SIan Morris 	if (!p->sysctl)
5833408c4768SPavel Emelyanov 		return;
5834408c4768SPavel Emelyanov 
5835408c4768SPavel Emelyanov 	t = p->sysctl;
58361da177e4SLinus Torvalds 	p->sysctl = NULL;
5837ff538818SLucian Adrian Grijincu 	unregister_net_sysctl_table(t->sysctl_header);
58381da177e4SLinus Torvalds 	kfree(t);
58391da177e4SLinus Torvalds }
5840408c4768SPavel Emelyanov 
5841a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev)
5842408c4768SPavel Emelyanov {
5843a317a2f1SWANG Cong 	int err;
5844a317a2f1SWANG Cong 
5845a317a2f1SWANG Cong 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
5846a317a2f1SWANG Cong 		return -EINVAL;
5847a317a2f1SWANG Cong 
5848a317a2f1SWANG Cong 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
5849f8572d8fSEric W. Biederman 				    &ndisc_ifinfo_sysctl_change);
5850a317a2f1SWANG Cong 	if (err)
5851a317a2f1SWANG Cong 		return err;
5852a317a2f1SWANG Cong 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5853f8572d8fSEric W. Biederman 					 idev, &idev->cnf);
5854a317a2f1SWANG Cong 	if (err)
5855a317a2f1SWANG Cong 		neigh_sysctl_unregister(idev->nd_parms);
5856a317a2f1SWANG Cong 
5857a317a2f1SWANG Cong 	return err;
5858408c4768SPavel Emelyanov }
5859408c4768SPavel Emelyanov 
5860408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
5861408c4768SPavel Emelyanov {
5862408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
5863408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
58641da177e4SLinus Torvalds }
58651da177e4SLinus Torvalds 
58661da177e4SLinus Torvalds 
58671da177e4SLinus Torvalds #endif
58681da177e4SLinus Torvalds 
58692c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
5870e0da5a48SPavel Emelyanov {
5871a79ca223SHong Zhiguo 	int err = -ENOMEM;
5872e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
5873e0da5a48SPavel Emelyanov 
5874a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
587563159f29SIan Morris 	if (!all)
5876e0da5a48SPavel Emelyanov 		goto err_alloc_all;
5877e0da5a48SPavel Emelyanov 
5878a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
587963159f29SIan Morris 	if (!dflt)
5880e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
5881a79ca223SHong Zhiguo 
588256d417b1SBrian Haley 	/* these will be inherited by all namespaces */
588356d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
588456d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5885e0da5a48SPavel Emelyanov 
58863d1bec99SHannes Frederic Sowa 	dflt->stable_secret.initialized = false;
58873d1bec99SHannes Frederic Sowa 	all->stable_secret.initialized = false;
58883d1bec99SHannes Frederic Sowa 
5889e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
5890e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
5891e0da5a48SPavel Emelyanov 
5892e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5893f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
5894e0da5a48SPavel Emelyanov 	if (err < 0)
5895e0da5a48SPavel Emelyanov 		goto err_reg_all;
5896e0da5a48SPavel Emelyanov 
5897f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5898e0da5a48SPavel Emelyanov 	if (err < 0)
5899e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
5900e0da5a48SPavel Emelyanov #endif
5901e0da5a48SPavel Emelyanov 	return 0;
5902e0da5a48SPavel Emelyanov 
5903e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5904e0da5a48SPavel Emelyanov err_reg_dflt:
5905e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
5906e0da5a48SPavel Emelyanov err_reg_all:
5907e0da5a48SPavel Emelyanov 	kfree(dflt);
5908e0da5a48SPavel Emelyanov #endif
5909e0da5a48SPavel Emelyanov err_alloc_dflt:
5910e0da5a48SPavel Emelyanov 	kfree(all);
5911e0da5a48SPavel Emelyanov err_alloc_all:
5912e0da5a48SPavel Emelyanov 	return err;
5913e0da5a48SPavel Emelyanov }
5914e0da5a48SPavel Emelyanov 
59152c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
5916e0da5a48SPavel Emelyanov {
5917e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5918e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5919e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
5920e0da5a48SPavel Emelyanov #endif
5921e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_dflt);
5922e0da5a48SPavel Emelyanov 	kfree(net->ipv6.devconf_all);
5923e0da5a48SPavel Emelyanov }
5924e0da5a48SPavel Emelyanov 
5925e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
5926e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
5927e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
5928e0da5a48SPavel Emelyanov };
5929e0da5a48SPavel Emelyanov 
5930207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = {
5931b382b191SThomas Graf 	.family		  = AF_INET6,
5932b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
5933b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
593411b1f828SDaniel Borkmann 	.validate_link_af = inet6_validate_link_af,
5935f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
5936b382b191SThomas Graf };
5937b382b191SThomas Graf 
59381da177e4SLinus Torvalds /*
59391da177e4SLinus Torvalds  *	Init / cleanup code
59401da177e4SLinus Torvalds  */
59411da177e4SLinus Torvalds 
59421da177e4SLinus Torvalds int __init addrconf_init(void)
59431da177e4SLinus Torvalds {
5944a317a2f1SWANG Cong 	struct inet6_dev *idev;
5945c2e21293Sstephen hemminger 	int i, err;
59462a8cc6c8SYOSHIFUJI Hideaki 
5947e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
5948e21e8467SStephen Hemminger 	if (err < 0) {
5949f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
5950f3213831SJoe Perches 			__func__, err);
59512cc6d2bfSNeil Horman 		goto out;
59522a8cc6c8SYOSHIFUJI Hideaki 	}
59531da177e4SLinus Torvalds 
59542cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
59552cc6d2bfSNeil Horman 	if (err < 0)
59562cc6d2bfSNeil Horman 		goto out_addrlabel;
5957e0da5a48SPavel Emelyanov 
5958c15b1ccaSHannes Frederic Sowa 	addrconf_wq = create_workqueue("ipv6_addrconf");
5959c15b1ccaSHannes Frederic Sowa 	if (!addrconf_wq) {
5960c15b1ccaSHannes Frederic Sowa 		err = -ENOMEM;
5961c15b1ccaSHannes Frederic Sowa 		goto out_nowq;
5962c15b1ccaSHannes Frederic Sowa 	}
5963c15b1ccaSHannes Frederic Sowa 
59641da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
59651da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
59661da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
59671da177e4SLinus Torvalds 	 * to other devices which are up.
59681da177e4SLinus Torvalds 	 *
59691da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
59701da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
59711da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
59721da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
59731da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
59741da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
59751da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
59761da177e4SLinus Torvalds 	 *
59771da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
59781da177e4SLinus Torvalds 	 * loopback_dev first by hand.
59791da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
59801da177e4SLinus Torvalds 	 * device and it being up should be removed.
59811da177e4SLinus Torvalds 	 */
59821da177e4SLinus Torvalds 	rtnl_lock();
5983a317a2f1SWANG Cong 	idev = ipv6_add_dev(init_net.loopback_dev);
59841da177e4SLinus Torvalds 	rtnl_unlock();
5985a317a2f1SWANG Cong 	if (IS_ERR(idev)) {
5986a317a2f1SWANG Cong 		err = PTR_ERR(idev);
5987e0da5a48SPavel Emelyanov 		goto errlo;
5988a317a2f1SWANG Cong 	}
59891da177e4SLinus Torvalds 
5990c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
5991c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5992c2e21293Sstephen hemminger 
59931da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
59941da177e4SLinus Torvalds 
5995c15b1ccaSHannes Frederic Sowa 	addrconf_verify();
5996c127ea2cSThomas Graf 
59973678a9d8Sstephen hemminger 	rtnl_af_register(&inet6_ops);
5998b382b191SThomas Graf 
5999c7ac8679SGreg Rose 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6000c7ac8679SGreg Rose 			      NULL);
6001c127ea2cSThomas Graf 	if (err < 0)
6002c127ea2cSThomas Graf 		goto errout;
6003c127ea2cSThomas Graf 
6004c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
6005c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6006c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6007c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6008c7ac8679SGreg Rose 			inet6_dump_ifaddr, NULL);
6009c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6010c7ac8679SGreg Rose 			inet6_dump_ifmcaddr, NULL);
6011c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6012c7ac8679SGreg Rose 			inet6_dump_ifacaddr, NULL);
601376f8f6cbSNicolas Dichtel 	__rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
60147a674200SNicolas Dichtel 			inet6_netconf_dump_devconf, NULL);
6015c127ea2cSThomas Graf 
60162a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
60172a8cc6c8SYOSHIFUJI Hideaki 
60181da177e4SLinus Torvalds 	return 0;
6019c127ea2cSThomas Graf errout:
6020b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
6021c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
6022e0da5a48SPavel Emelyanov errlo:
6023c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
6024c15b1ccaSHannes Frederic Sowa out_nowq:
6025e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
60262cc6d2bfSNeil Horman out_addrlabel:
60272cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
60282cc6d2bfSNeil Horman out:
6029c127ea2cSThomas Graf 	return err;
60301da177e4SLinus Torvalds }
60311da177e4SLinus Torvalds 
603209f7709fSDaniel Lezcano void addrconf_cleanup(void)
60331da177e4SLinus Torvalds {
6034176c39afSDaniel Lezcano 	struct net_device *dev;
60351da177e4SLinus Torvalds 	int i;
60361da177e4SLinus Torvalds 
60371da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
6038e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
60392cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
60401da177e4SLinus Torvalds 
60411da177e4SLinus Torvalds 	rtnl_lock();
60421da177e4SLinus Torvalds 
6043b382b191SThomas Graf 	__rtnl_af_unregister(&inet6_ops);
6044b382b191SThomas Graf 
6045176c39afSDaniel Lezcano 	/* clean dev list */
6046176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
6047176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
6048176c39afSDaniel Lezcano 			continue;
6049176c39afSDaniel Lezcano 		addrconf_ifdown(dev, 1);
6050176c39afSDaniel Lezcano 	}
6051176c39afSDaniel Lezcano 	addrconf_ifdown(init_net.loopback_dev, 2);
6052176c39afSDaniel Lezcano 
60531da177e4SLinus Torvalds 	/*
60541da177e4SLinus Torvalds 	 *	Check hash table.
60551da177e4SLinus Torvalds 	 */
60565c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
6057c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
6058c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
60595c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
6060c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
60611da177e4SLinus Torvalds 	rtnl_unlock();
6062c15b1ccaSHannes Frederic Sowa 
6063c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
60641da177e4SLinus Torvalds }
6065