xref: /openbmc/linux/net/ipv6/addrconf.c (revision e7478dfc4656f4a739ed1b07cfd59c12f8eb112e)
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>
491da177e4SLinus Torvalds #include <linux/in6.h>
501da177e4SLinus Torvalds #include <linux/netdevice.h>
511823730fSThomas Graf #include <linux/if_addr.h>
521da177e4SLinus Torvalds #include <linux/if_arp.h>
531da177e4SLinus Torvalds #include <linux/if_arcnet.h>
541da177e4SLinus Torvalds #include <linux/if_infiniband.h>
551da177e4SLinus Torvalds #include <linux/route.h>
561da177e4SLinus Torvalds #include <linux/inetdevice.h>
571da177e4SLinus Torvalds #include <linux/init.h>
585a0e3ad6STejun Heo #include <linux/slab.h>
591da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
601da177e4SLinus Torvalds #include <linux/sysctl.h>
611da177e4SLinus Torvalds #endif
624fc268d2SRandy Dunlap #include <linux/capability.h>
631da177e4SLinus Torvalds #include <linux/delay.h>
641da177e4SLinus Torvalds #include <linux/notifier.h>
65543537bdSPaulo Marques #include <linux/string.h>
66ddbe5032SEric Dumazet #include <linux/hash.h>
671da177e4SLinus Torvalds 
68457c4cbcSEric W. Biederman #include <net/net_namespace.h>
691da177e4SLinus Torvalds #include <net/sock.h>
701da177e4SLinus Torvalds #include <net/snmp.h>
711da177e4SLinus Torvalds 
7206a4c1c5Salex.bluesman.smirnov@gmail.com #include <net/af_ieee802154.h>
73cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h>
741da177e4SLinus Torvalds #include <net/ipv6.h>
751da177e4SLinus Torvalds #include <net/protocol.h>
761da177e4SLinus Torvalds #include <net/ndisc.h>
771da177e4SLinus Torvalds #include <net/ip6_route.h>
781da177e4SLinus Torvalds #include <net/addrconf.h>
791da177e4SLinus Torvalds #include <net/tcp.h>
801da177e4SLinus Torvalds #include <net/ip.h>
815d620266SThomas Graf #include <net/netlink.h>
82f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
831da177e4SLinus Torvalds #include <linux/if_tunnel.h>
841da177e4SLinus Torvalds #include <linux/rtnetlink.h>
85f3a1bfb1SNicolas Dichtel #include <linux/netconf.h>
861da177e4SLinus Torvalds #include <linux/random.h>
87e21e8467SStephen Hemminger #include <linux/uaccess.h>
887f7d9a6bSHerbert Xu #include <asm/unaligned.h>
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #include <linux/proc_fs.h>
911da177e4SLinus Torvalds #include <linux/seq_file.h>
92bc3b2d7fSPaul Gortmaker #include <linux/export.h>
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* Set to 3 to get tracing... */
951da177e4SLinus Torvalds #define ACONF_DEBUG 2
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
98ba3542e1Sdingtianhong #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
991da177e4SLinus Torvalds #else
100ba3542e1Sdingtianhong #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
1011da177e4SLinus Torvalds #endif
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
10418a31e1eSThomas Graf 
10518a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp)
10618a31e1eSThomas Graf {
10718a31e1eSThomas Graf 	return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
10818a31e1eSThomas Graf }
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
111a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev);
112408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
113408c4768SPavel Emelyanov #else
114a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev)
115408c4768SPavel Emelyanov {
116a317a2f1SWANG Cong 	return 0;
117408c4768SPavel Emelyanov }
118408c4768SPavel Emelyanov 
119408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
120408c4768SPavel Emelyanov {
121408c4768SPavel Emelyanov }
1221da177e4SLinus Torvalds #endif
1231da177e4SLinus Torvalds 
124eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev);
125eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1261da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data);
1271da177e4SLinus Torvalds 
1281b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
1291da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev);
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /*
1321da177e4SLinus Torvalds  *	Configured unicast address hash table
1331da177e4SLinus Torvalds  */
134c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
1355c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock);
1361da177e4SLinus Torvalds 
137c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void);
138c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void);
139c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *);
1401da177e4SLinus Torvalds 
141c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq;
142c15b1ccaSHannes Frederic Sowa static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1451da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1461da177e4SLinus Torvalds 
14793d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev,
14875c78500SMoni Shoua 				 unsigned long event);
1491da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1501da177e4SLinus Torvalds 
15121caa662SRomain Kuntz static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
15221caa662SRomain Kuntz 						  int plen,
15321caa662SRomain Kuntz 						  const struct net_device *dev,
15421caa662SRomain Kuntz 						  u32 flags, u32 noflags);
15521caa662SRomain Kuntz 
156cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp);
157c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w);
1581da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
159c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1601da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1611da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1621da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1651da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1663e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
16706bfe655SDaniel Lezcano 			       struct net_device *dev);
1681da177e4SLinus Torvalds 
169888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1701da177e4SLinus Torvalds 	.forwarding		= 0,
1711da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1721da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1731da177e4SLinus Torvalds 	.accept_ra		= 1,
1741da177e4SLinus Torvalds 	.accept_redirects	= 1,
1751da177e4SLinus Torvalds 	.autoconf		= 1,
1761da177e4SLinus Torvalds 	.force_mld_version	= 0,
177fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
178fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
1791da177e4SLinus Torvalds 	.dad_transmits		= 1,
1801da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1811da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1821da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1831da177e4SLinus Torvalds 	.use_tempaddr 		= 0,
1841da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1851da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1861da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1871da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1881da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
18965f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
190d9333196SBen Greear 	.accept_ra_from_local	= 0,
191c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
192930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
193930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
19452e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
19509c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
19609c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
19709c884d4SYOSHIFUJI Hideaki #endif
198930d6ff2SYOSHIFUJI Hideaki #endif
199fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2000bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
201778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2021b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
203b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
2041da177e4SLinus Torvalds };
2051da177e4SLinus Torvalds 
206ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2071da177e4SLinus Torvalds 	.forwarding		= 0,
2081da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2091da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2101da177e4SLinus Torvalds 	.accept_ra		= 1,
2111da177e4SLinus Torvalds 	.accept_redirects	= 1,
2121da177e4SLinus Torvalds 	.autoconf		= 1,
213fc4eba58SHannes Frederic Sowa 	.force_mld_version	= 0,
214fc4eba58SHannes Frederic Sowa 	.mldv1_unsolicited_report_interval = 10 * HZ,
215fc4eba58SHannes Frederic Sowa 	.mldv2_unsolicited_report_interval = HZ,
2161da177e4SLinus Torvalds 	.dad_transmits		= 1,
2171da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2181da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
2191da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2201da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2211da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2221da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2231da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2241da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2251da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
22665f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
227d9333196SBen Greear 	.accept_ra_from_local	= 0,
228c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
229930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
230930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
23152e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
23209c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
23309c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
23409c884d4SYOSHIFUJI Hideaki #endif
235930d6ff2SYOSHIFUJI Hideaki #endif
236fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2370bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
238778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2391b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
240b800c3b9SHannes Frederic Sowa 	.suppress_frag_ndisc	= 1,
2411da177e4SLinus Torvalds };
2421da177e4SLinus Torvalds 
243f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
24405297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev)
245f24e3d65SMitsuru Chinen {
24605297949SDavid S. Miller 	return !qdisc_tx_is_noop(dev);
247f24e3d65SMitsuru Chinen }
248f24e3d65SMitsuru Chinen 
249b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev)
2501da177e4SLinus Torvalds {
251b7b1bfceSHannes Frederic Sowa 	if (del_timer(&idev->rs_timer))
252b7b1bfceSHannes Frederic Sowa 		__in6_dev_put(idev);
253b7b1bfceSHannes Frederic Sowa }
254b7b1bfceSHannes Frederic Sowa 
255c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
256b7b1bfceSHannes Frederic Sowa {
257c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
2581da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
261b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev,
2621da177e4SLinus Torvalds 				  unsigned long when)
2631da177e4SLinus Torvalds {
264b7b1bfceSHannes Frederic Sowa 	if (!timer_pending(&idev->rs_timer))
265b7b1bfceSHannes Frederic Sowa 		in6_dev_hold(idev);
266b7b1bfceSHannes Frederic Sowa 	mod_timer(&idev->rs_timer, jiffies + when);
2671da177e4SLinus Torvalds }
268b7b1bfceSHannes Frederic Sowa 
269c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
270c15b1ccaSHannes Frederic Sowa 				   unsigned long delay)
271b7b1bfceSHannes Frederic Sowa {
272c15b1ccaSHannes Frederic Sowa 	if (!delayed_work_pending(&ifp->dad_work))
273b7b1bfceSHannes Frederic Sowa 		in6_ifa_hold(ifp);
274c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2777f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
2787f7d9a6bSHerbert Xu {
279827da44cSJohn Stultz 	int i;
280827da44cSJohn Stultz 
281698365faSWANG Cong 	idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
282698365faSWANG Cong 	if (!idev->stats.ipv6)
2837f7d9a6bSHerbert Xu 		goto err_ip;
284827da44cSJohn Stultz 
285827da44cSJohn Stultz 	for_each_possible_cpu(i) {
286827da44cSJohn Stultz 		struct ipstats_mib *addrconf_stats;
287698365faSWANG Cong 		addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
288827da44cSJohn Stultz 		u64_stats_init(&addrconf_stats->syncp);
289827da44cSJohn Stultz 	}
290827da44cSJohn Stultz 
291827da44cSJohn Stultz 
292be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
293be281e55SEric Dumazet 					GFP_KERNEL);
294be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
2957f7d9a6bSHerbert Xu 		goto err_icmp;
296be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
297be281e55SEric Dumazet 					   GFP_KERNEL);
298be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
29914878f75SDavid L Stevens 		goto err_icmpmsg;
3007f7d9a6bSHerbert Xu 
3017f7d9a6bSHerbert Xu 	return 0;
3027f7d9a6bSHerbert Xu 
30314878f75SDavid L Stevens err_icmpmsg:
304be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3057f7d9a6bSHerbert Xu err_icmp:
306698365faSWANG Cong 	free_percpu(idev->stats.ipv6);
3077f7d9a6bSHerbert Xu err_ip:
308aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3097f7d9a6bSHerbert Xu }
3107f7d9a6bSHerbert Xu 
3111da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	struct inet6_dev *ndev;
314a317a2f1SWANG Cong 	int err = -ENOMEM;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	ASSERT_RTNL();
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
319a317a2f1SWANG Cong 		return ERR_PTR(-EINVAL);
3201da177e4SLinus Torvalds 
321322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
322322f74a4SIngo Oeser 	if (ndev == NULL)
323a317a2f1SWANG Cong 		return ERR_PTR(err);
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3261da177e4SLinus Torvalds 	ndev->dev = dev;
327502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
328b7b1bfceSHannes Frederic Sowa 	setup_timer(&ndev->rs_timer, addrconf_rs_timer,
329b7b1bfceSHannes Frederic Sowa 		    (unsigned long)ndev);
330c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3311da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3321da177e4SLinus Torvalds 	ndev->cnf.sysctl = NULL;
3331da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
3341da177e4SLinus Torvalds 	if (ndev->nd_parms == NULL) {
3351da177e4SLinus Torvalds 		kfree(ndev);
336a317a2f1SWANG Cong 		return ERR_PTR(err);
3371da177e4SLinus Torvalds 	}
3380187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
3390187bdfbSBen Hutchings 		dev_disable_lro(dev);
3401da177e4SLinus Torvalds 	/* We refer to the device */
3411da177e4SLinus Torvalds 	dev_hold(dev);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
344ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
345f3213831SJoe Perches 			"%s: cannot allocate memory for statistics; dev=%s.\n",
346ba3542e1Sdingtianhong 			__func__, dev->name);
3471da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3488603e33dSRoy Li 		dev_put(dev);
3498603e33dSRoy Li 		kfree(ndev);
350a317a2f1SWANG Cong 		return ERR_PTR(err);
3511da177e4SLinus Torvalds 	}
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
354ba3542e1Sdingtianhong 		ADBG(KERN_WARNING
355f3213831SJoe Perches 			"%s: cannot create /proc/net/dev_snmp6/%s\n",
356ba3542e1Sdingtianhong 			__func__, dev->name);
357a317a2f1SWANG Cong 		goto err_release;
3581da177e4SLinus Torvalds 	}
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3611da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3621da177e4SLinus Torvalds 	 */
3631da177e4SLinus Torvalds 	in6_dev_hold(ndev);
3641da177e4SLinus Torvalds 
3651b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
3661b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
3671b34be74SYOSHIFUJI Hideaki 
36807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
369c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
370f3213831SJoe Perches 		pr_info("%s: Disabled Multicast RS\n", dev->name);
371c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
372c7dc89c0SFred L. Templin 	}
373b077d7abSYOSHIFUJI Hideaki #endif
374b077d7abSYOSHIFUJI Hideaki 
375372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
3761da177e4SLinus Torvalds 	setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
3771da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
3781da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
3799625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
3801da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
3811da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
3820be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
3831da177e4SLinus Torvalds 	} else {
3841da177e4SLinus Torvalds 		in6_dev_hold(ndev);
3851da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
3861da177e4SLinus Torvalds 	}
3875d9efa7eSDavid S. Miller 
388914faa14SDaniel Borkmann 	ndev->token = in6addr_any;
3891da177e4SLinus Torvalds 
390f24e3d65SMitsuru Chinen 	if (netif_running(dev) && addrconf_qdisc_ok(dev))
39153aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
39253aadcc9SHerbert Xu 
3931da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
3941da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
395a317a2f1SWANG Cong 	err = addrconf_sysctl_register(ndev);
396a317a2f1SWANG Cong 	if (err) {
397a317a2f1SWANG Cong 		ipv6_mc_destroy_dev(ndev);
398a317a2f1SWANG Cong 		del_timer(&ndev->regen_timer);
399a317a2f1SWANG Cong 		goto err_release;
400a317a2f1SWANG Cong 	}
40130c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
402cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip6_ptr, ndev);
403d88ae4ccSYOSHIFUJI Hideaki 
4042c5e8933SHannes Frederic Sowa 	/* Join interface-local all-node multicast group */
4052c5e8933SHannes Frederic Sowa 	ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4062c5e8933SHannes Frederic Sowa 
407d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
408f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
409d88ae4ccSYOSHIFUJI Hideaki 
410d6ddef9eSLi Wei 	/* Join all-router multicast group if forwarding is set */
4118b2aaedeSLi Wei 	if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
412d6ddef9eSLi Wei 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
413d6ddef9eSLi Wei 
4141da177e4SLinus Torvalds 	return ndev;
415a317a2f1SWANG Cong 
416a317a2f1SWANG Cong err_release:
417a317a2f1SWANG Cong 	neigh_parms_release(&nd_tbl, ndev->nd_parms);
418a317a2f1SWANG Cong 	ndev->dead = 1;
419a317a2f1SWANG Cong 	in6_dev_finish_destroy(ndev);
420a317a2f1SWANG Cong 	return ERR_PTR(err);
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
423cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	struct inet6_dev *idev;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	ASSERT_RTNL();
4281da177e4SLinus Torvalds 
429e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
430e21e8467SStephen Hemminger 	if (!idev) {
431e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
432a317a2f1SWANG Cong 		if (IS_ERR(idev))
4331da177e4SLinus Torvalds 			return NULL;
4341da177e4SLinus Torvalds 	}
435c5e33bddSYOSHIFUJI Hideaki 
4361da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4371da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4381da177e4SLinus Torvalds 	return idev;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
441f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
442f3a1bfb1SNicolas Dichtel {
443f3a1bfb1SNicolas Dichtel 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
444f3a1bfb1SNicolas Dichtel 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
445f3a1bfb1SNicolas Dichtel 
44676f8f6cbSNicolas Dichtel 	/* type -1 is used for ALL */
44776f8f6cbSNicolas Dichtel 	if (type == -1 || type == NETCONFA_FORWARDING)
448f3a1bfb1SNicolas Dichtel 		size += nla_total_size(4);
449b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
450d67b8c61SNicolas Dichtel 	if (type == -1 || type == NETCONFA_MC_FORWARDING)
451d67b8c61SNicolas Dichtel 		size += nla_total_size(4);
452b1afce95SDavid S. Miller #endif
453c92d5491Sstephen hemminger 	if (type == -1 || type == NETCONFA_PROXY_NEIGH)
454c92d5491Sstephen hemminger 		size += nla_total_size(4);
455f3a1bfb1SNicolas Dichtel 
456f3a1bfb1SNicolas Dichtel 	return size;
457f3a1bfb1SNicolas Dichtel }
458f3a1bfb1SNicolas Dichtel 
459f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
460f3a1bfb1SNicolas Dichtel 				      struct ipv6_devconf *devconf, u32 portid,
461f3a1bfb1SNicolas Dichtel 				      u32 seq, int event, unsigned int flags,
462f3a1bfb1SNicolas Dichtel 				      int type)
463f3a1bfb1SNicolas Dichtel {
464f3a1bfb1SNicolas Dichtel 	struct nlmsghdr  *nlh;
465f3a1bfb1SNicolas Dichtel 	struct netconfmsg *ncm;
466f3a1bfb1SNicolas Dichtel 
467f3a1bfb1SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
468f3a1bfb1SNicolas Dichtel 			flags);
469f3a1bfb1SNicolas Dichtel 	if (nlh == NULL)
470f3a1bfb1SNicolas Dichtel 		return -EMSGSIZE;
471f3a1bfb1SNicolas Dichtel 
472f3a1bfb1SNicolas Dichtel 	ncm = nlmsg_data(nlh);
473f3a1bfb1SNicolas Dichtel 	ncm->ncm_family = AF_INET6;
474f3a1bfb1SNicolas Dichtel 
475f3a1bfb1SNicolas Dichtel 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
476f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
477f3a1bfb1SNicolas Dichtel 
47876f8f6cbSNicolas Dichtel 	/* type -1 is used for ALL */
47976f8f6cbSNicolas Dichtel 	if ((type == -1 || type == NETCONFA_FORWARDING) &&
480f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
481f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
482b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
483d67b8c61SNicolas Dichtel 	if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
484d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
485d67b8c61SNicolas Dichtel 			devconf->mc_forwarding) < 0)
486d67b8c61SNicolas Dichtel 		goto nla_put_failure;
487b1afce95SDavid S. Miller #endif
488c92d5491Sstephen hemminger 	if ((type == -1 || type == NETCONFA_PROXY_NEIGH) &&
489c92d5491Sstephen hemminger 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
490c92d5491Sstephen hemminger 		goto nla_put_failure;
491c92d5491Sstephen hemminger 
492f3a1bfb1SNicolas Dichtel 	return nlmsg_end(skb, nlh);
493f3a1bfb1SNicolas Dichtel 
494f3a1bfb1SNicolas Dichtel nla_put_failure:
495f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
496f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
497f3a1bfb1SNicolas Dichtel }
498f3a1bfb1SNicolas Dichtel 
499d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
500f3a1bfb1SNicolas Dichtel 				  struct ipv6_devconf *devconf)
501f3a1bfb1SNicolas Dichtel {
502f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
503f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
504f3a1bfb1SNicolas Dichtel 
505f3a1bfb1SNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
506f3a1bfb1SNicolas Dichtel 	if (skb == NULL)
507f3a1bfb1SNicolas Dichtel 		goto errout;
508f3a1bfb1SNicolas Dichtel 
509f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
510f3a1bfb1SNicolas Dichtel 					 RTM_NEWNETCONF, 0, type);
511f3a1bfb1SNicolas Dichtel 	if (err < 0) {
512f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
513f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
514f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
515f3a1bfb1SNicolas Dichtel 		goto errout;
516f3a1bfb1SNicolas Dichtel 	}
517f3a1bfb1SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
518f3a1bfb1SNicolas Dichtel 	return;
519f3a1bfb1SNicolas Dichtel errout:
520f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
521f3a1bfb1SNicolas Dichtel }
522f3a1bfb1SNicolas Dichtel 
52376f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
52476f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
52576f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
526c92d5491Sstephen hemminger 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
52776f8f6cbSNicolas Dichtel };
52876f8f6cbSNicolas Dichtel 
52976f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
530661d2967SThomas Graf 				     struct nlmsghdr *nlh)
53176f8f6cbSNicolas Dichtel {
53276f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
53376f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
53476f8f6cbSNicolas Dichtel 	struct netconfmsg *ncm;
53576f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
53676f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
53776f8f6cbSNicolas Dichtel 	struct inet6_dev *in6_dev;
53876f8f6cbSNicolas Dichtel 	struct net_device *dev;
53976f8f6cbSNicolas Dichtel 	int ifindex;
54076f8f6cbSNicolas Dichtel 	int err;
54176f8f6cbSNicolas Dichtel 
54276f8f6cbSNicolas Dichtel 	err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
54376f8f6cbSNicolas Dichtel 			  devconf_ipv6_policy);
54476f8f6cbSNicolas Dichtel 	if (err < 0)
54576f8f6cbSNicolas Dichtel 		goto errout;
54676f8f6cbSNicolas Dichtel 
54776f8f6cbSNicolas Dichtel 	err = EINVAL;
54876f8f6cbSNicolas Dichtel 	if (!tb[NETCONFA_IFINDEX])
54976f8f6cbSNicolas Dichtel 		goto errout;
55076f8f6cbSNicolas Dichtel 
55176f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
55276f8f6cbSNicolas Dichtel 	switch (ifindex) {
55376f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
55476f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
55576f8f6cbSNicolas Dichtel 		break;
55676f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
55776f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
55876f8f6cbSNicolas Dichtel 		break;
55976f8f6cbSNicolas Dichtel 	default:
56076f8f6cbSNicolas Dichtel 		dev = __dev_get_by_index(net, ifindex);
56176f8f6cbSNicolas Dichtel 		if (dev == NULL)
56276f8f6cbSNicolas Dichtel 			goto errout;
56376f8f6cbSNicolas Dichtel 		in6_dev = __in6_dev_get(dev);
56476f8f6cbSNicolas Dichtel 		if (in6_dev == NULL)
56576f8f6cbSNicolas Dichtel 			goto errout;
56676f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
56776f8f6cbSNicolas Dichtel 		break;
56876f8f6cbSNicolas Dichtel 	}
56976f8f6cbSNicolas Dichtel 
57076f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
57176f8f6cbSNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
57276f8f6cbSNicolas Dichtel 	if (skb == NULL)
57376f8f6cbSNicolas Dichtel 		goto errout;
57476f8f6cbSNicolas Dichtel 
57576f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
57676f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
57776f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
57876f8f6cbSNicolas Dichtel 					 -1);
57976f8f6cbSNicolas Dichtel 	if (err < 0) {
58076f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
58176f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
58276f8f6cbSNicolas Dichtel 		kfree_skb(skb);
58376f8f6cbSNicolas Dichtel 		goto errout;
58476f8f6cbSNicolas Dichtel 	}
58576f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
58676f8f6cbSNicolas Dichtel errout:
58776f8f6cbSNicolas Dichtel 	return err;
58876f8f6cbSNicolas Dichtel }
58976f8f6cbSNicolas Dichtel 
5907a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
5917a674200SNicolas Dichtel 				      struct netlink_callback *cb)
5927a674200SNicolas Dichtel {
5937a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
5947a674200SNicolas Dichtel 	int h, s_h;
5957a674200SNicolas Dichtel 	int idx, s_idx;
5967a674200SNicolas Dichtel 	struct net_device *dev;
5977a674200SNicolas Dichtel 	struct inet6_dev *idev;
5987a674200SNicolas Dichtel 	struct hlist_head *head;
5997a674200SNicolas Dichtel 
6007a674200SNicolas Dichtel 	s_h = cb->args[0];
6017a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
6027a674200SNicolas Dichtel 
6037a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6047a674200SNicolas Dichtel 		idx = 0;
6057a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
6067a674200SNicolas Dichtel 		rcu_read_lock();
60763998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
60863998ac2SNicolas Dichtel 			  net->dev_base_seq;
6097a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
6107a674200SNicolas Dichtel 			if (idx < s_idx)
6117a674200SNicolas Dichtel 				goto cont;
6127a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
6137a674200SNicolas Dichtel 			if (!idev)
6147a674200SNicolas Dichtel 				goto cont;
6157a674200SNicolas Dichtel 
6167a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
6177a674200SNicolas Dichtel 						       &idev->cnf,
6187a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
6197a674200SNicolas Dichtel 						       cb->nlh->nlmsg_seq,
6207a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
6217a674200SNicolas Dichtel 						       NLM_F_MULTI,
6227a674200SNicolas Dichtel 						       -1) <= 0) {
6237a674200SNicolas Dichtel 				rcu_read_unlock();
6247a674200SNicolas Dichtel 				goto done;
6257a674200SNicolas Dichtel 			}
62663998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
6277a674200SNicolas Dichtel cont:
6287a674200SNicolas Dichtel 			idx++;
6297a674200SNicolas Dichtel 		}
6307a674200SNicolas Dichtel 		rcu_read_unlock();
6317a674200SNicolas Dichtel 	}
6327a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
6337a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
6347a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
6357a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6367a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6377a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
6387a674200SNicolas Dichtel 					       -1) <= 0)
6397a674200SNicolas Dichtel 			goto done;
6407a674200SNicolas Dichtel 		else
6417a674200SNicolas Dichtel 			h++;
6427a674200SNicolas Dichtel 	}
6437a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
6447a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
6457a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
6467a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6477a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6487a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
6497a674200SNicolas Dichtel 					       -1) <= 0)
6507a674200SNicolas Dichtel 			goto done;
6517a674200SNicolas Dichtel 		else
6527a674200SNicolas Dichtel 			h++;
6537a674200SNicolas Dichtel 	}
6547a674200SNicolas Dichtel done:
6557a674200SNicolas Dichtel 	cb->args[0] = h;
6567a674200SNicolas Dichtel 	cb->args[1] = idx;
6577a674200SNicolas Dichtel 
6587a674200SNicolas Dichtel 	return skb->len;
6597a674200SNicolas Dichtel }
6607a674200SNicolas Dichtel 
6611da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
6621da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
6631da177e4SLinus Torvalds {
6641da177e4SLinus Torvalds 	struct net_device *dev;
6651da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	if (!idev)
6681da177e4SLinus Torvalds 		return;
6691da177e4SLinus Torvalds 	dev = idev->dev;
6700187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
6710187bdfbSBen Hutchings 		dev_disable_lro(dev);
6721a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
6732c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
674f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
6752c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
6762c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
6772c5e8933SHannes Frederic Sowa 		} else {
678f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
6792c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
6802c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
6812c5e8933SHannes Frederic Sowa 		}
6821da177e4SLinus Torvalds 	}
683502a2ffdSstephen hemminger 
684502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
6852c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
6862c12a74cSMichal Wrobel 			continue;
6871da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
6881da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
6891da177e4SLinus Torvalds 		else
6901da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
6911da177e4SLinus Torvalds 	}
692f3a1bfb1SNicolas Dichtel 	inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
693f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 
697e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	struct net_device *dev;
7001da177e4SLinus Torvalds 	struct inet6_dev *idev;
7011da177e4SLinus Torvalds 
7024acd4945SBen Hutchings 	for_each_netdev(net, dev) {
7031da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
7041da177e4SLinus Torvalds 		if (idev) {
705e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
706e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
7071da177e4SLinus Torvalds 			if (changed)
7081da177e4SLinus Torvalds 				dev_forward_change(idev);
7091da177e4SLinus Torvalds 		}
7101da177e4SLinus Torvalds 	}
7111da177e4SLinus Torvalds }
712c8fecf22SPavel Emelyanov 
713013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
714c8fecf22SPavel Emelyanov {
715bff16c2fSPavel Emelyanov 	struct net *net;
716013d97e9SFrancesco Ruggeri 	int old;
717013d97e9SFrancesco Ruggeri 
718013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
719013d97e9SFrancesco Ruggeri 		return restart_syscall();
720bff16c2fSPavel Emelyanov 
721bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
722013d97e9SFrancesco Ruggeri 	old = *p;
723013d97e9SFrancesco Ruggeri 	*p = newf;
724c8fecf22SPavel Emelyanov 
725013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
726f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
727f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
728f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
729f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
730013d97e9SFrancesco Ruggeri 		rtnl_unlock();
731013d97e9SFrancesco Ruggeri 		return 0;
73288af182eSEric W. Biederman 	}
733b325fddbSStephen Hemminger 
734e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
735e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
736e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
737f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
738f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
739f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
740f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
741013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
742c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
7430187bdfbSBen Hutchings 	rtnl_unlock();
744c8fecf22SPavel Emelyanov 
745013d97e9SFrancesco Ruggeri 	if (newf)
7467b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
747b325fddbSStephen Hemminger 	return 1;
748c8fecf22SPavel Emelyanov }
7491da177e4SLinus Torvalds #endif
7501da177e4SLinus Torvalds 
7515c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
7521da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
7531da177e4SLinus Torvalds {
754c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
755547b792cSIlpo Järvinen 
7561da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
75791df42beSJoe Perches 	pr_debug("%s\n", __func__);
7581da177e4SLinus Torvalds #endif
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
7611da177e4SLinus Torvalds 
762c15b1ccaSHannes Frederic Sowa 	if (cancel_delayed_work(&ifp->dad_work))
763c15b1ccaSHannes Frederic Sowa 		pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
764c15b1ccaSHannes Frederic Sowa 			  ifp);
7651da177e4SLinus Torvalds 
766e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
767f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
7681da177e4SLinus Torvalds 		return;
7691da177e4SLinus Torvalds 	}
77094e187c0SAmerigo Wang 	ip6_rt_put(ifp->rt);
7711da177e4SLinus Torvalds 
772e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
775e55ffac6SBrian Haley static void
776e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
777e55ffac6SBrian Haley {
778502a2ffdSstephen hemminger 	struct list_head *p;
7798a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
780e55ffac6SBrian Haley 
781e55ffac6SBrian Haley 	/*
782e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
783e55ffac6SBrian Haley 	 * global before linklocal.
784e55ffac6SBrian Haley 	 */
785502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
786502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
787502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
7888a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
789e55ffac6SBrian Haley 			break;
790e55ffac6SBrian Haley 	}
791e55ffac6SBrian Haley 
792b54c9b98SDavid S. Miller 	list_add_tail(&ifp->if_list, p);
793e55ffac6SBrian Haley }
794e55ffac6SBrian Haley 
795ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr)
7963eb84f49SYOSHIFUJI Hideaki {
797ddbe5032SEric Dumazet 	return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
7983eb84f49SYOSHIFUJI Hideaki }
7993eb84f49SYOSHIFUJI Hideaki 
8001da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds static struct inet6_ifaddr *
8033f8f5298SJiri Pirko ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
8043f8f5298SJiri Pirko 	      const struct in6_addr *peer_addr, int pfxlen,
8058a226b2cSJiri Benc 	      int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
8061da177e4SLinus Torvalds {
8071da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
8081da177e4SLinus Torvalds 	struct rt6_info *rt;
8093a88a81dSstephen hemminger 	unsigned int hash;
8101da177e4SLinus Torvalds 	int err = 0;
811d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
812d68b8270SYOSHIFUJI Hideaki 
813d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
814d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
815d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
816d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
817d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
8181da177e4SLinus Torvalds 
8198814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
8201da177e4SLinus Torvalds 	if (idev->dead) {
8211da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
8221da177e4SLinus Torvalds 		goto out2;
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds 
82556d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
8269bdd8d40SBrian Haley 		err = -EACCES;
8279bdd8d40SBrian Haley 		goto out2;
8289bdd8d40SBrian Haley 	}
8299bdd8d40SBrian Haley 
8305c578aedSstephen hemminger 	spin_lock(&addrconf_hash_lock);
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
833c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
834ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: already assigned\n");
8351da177e4SLinus Torvalds 		err = -EEXIST;
8361da177e4SLinus Torvalds 		goto out;
8371da177e4SLinus Torvalds 	}
8381da177e4SLinus Torvalds 
839322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	if (ifa == NULL) {
842ba3542e1Sdingtianhong 		ADBG("ipv6_add_addr: malloc failed\n");
8431da177e4SLinus Torvalds 		err = -ENOBUFS;
8441da177e4SLinus Torvalds 		goto out;
8451da177e4SLinus Torvalds 	}
8461da177e4SLinus Torvalds 
8478f031519SDavid S. Miller 	rt = addrconf_dst_alloc(idev, addr, false);
8481da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
8491da177e4SLinus Torvalds 		err = PTR_ERR(rt);
8501da177e4SLinus Torvalds 		goto out;
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 
853bba24896SJiri Pirko 	neigh_parms_data_state_setall(idev->nd_parms);
854bba24896SJiri Pirko 
8554e3fd7a0SAlexey Dobriyan 	ifa->addr = *addr;
8563f8f5298SJiri Pirko 	if (peer_addr)
8573f8f5298SJiri Pirko 		ifa->peer_addr = *peer_addr;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
860e9d3e084SHerbert Xu 	spin_lock_init(&ifa->state_lock);
861c15b1ccaSHannes Frederic Sowa 	INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
862c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
8631da177e4SLinus Torvalds 	ifa->scope = scope;
8641da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
8651da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
8668a226b2cSJiri Benc 	ifa->valid_lft = valid_lft;
8678a226b2cSJiri Benc 	ifa->prefered_lft = prefered_lft;
8681da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
869617fe29dSDaniel Borkmann 	ifa->tokenized = false;
8701da177e4SLinus Torvalds 
87157f5f544SKeir Fraser 	ifa->rt = rt;
87257f5f544SKeir Fraser 
8731da177e4SLinus Torvalds 	ifa->idev = idev;
8741da177e4SLinus Torvalds 	in6_dev_hold(idev);
8751da177e4SLinus Torvalds 	/* For caller */
8761da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	/* Add to big hash table */
879ddbe5032SEric Dumazet 	hash = inet6_addr_hash(addr);
8801da177e4SLinus Torvalds 
8815c578aedSstephen hemminger 	hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
8825c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	write_lock(&idev->lock);
8851da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
886e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
889372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
8901da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
8911da177e4SLinus Torvalds 	}
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
8941da177e4SLinus Torvalds 	write_unlock(&idev->lock);
8951da177e4SLinus Torvalds out2:
8968814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
8971da177e4SLinus Torvalds 
898fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
899f88c91ddSCong Wang 		inet6addr_notifier_call_chain(NETDEV_UP, ifa);
9001da177e4SLinus Torvalds 	else {
9011da177e4SLinus Torvalds 		kfree(ifa);
9021da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
9031da177e4SLinus Torvalds 	}
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	return ifa;
9061da177e4SLinus Torvalds out:
9075c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
9081da177e4SLinus Torvalds 	goto out2;
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
9115b84efecSThomas Haller enum cleanup_prefix_rt_t {
9125b84efecSThomas Haller 	CLEANUP_PREFIX_RT_NOP,    /* no cleanup action for prefix route */
9135b84efecSThomas Haller 	CLEANUP_PREFIX_RT_DEL,    /* delete the prefix route */
9145b84efecSThomas Haller 	CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
9155b84efecSThomas Haller };
9165b84efecSThomas Haller 
9175b84efecSThomas Haller /*
9185b84efecSThomas Haller  * Check, whether the prefix for ifp would still need a prefix route
9195b84efecSThomas Haller  * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
9205b84efecSThomas Haller  * constants.
9215b84efecSThomas Haller  *
9225b84efecSThomas Haller  * 1) we don't purge prefix if address was not permanent.
9235b84efecSThomas Haller  *    prefix is managed by its own lifetime.
9245b84efecSThomas Haller  * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
9255b84efecSThomas Haller  * 3) if there are no addresses, delete prefix.
9265b84efecSThomas Haller  * 4) if there are still other permanent address(es),
9275b84efecSThomas Haller  *    corresponding prefix is still permanent.
9285b84efecSThomas Haller  * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
9295b84efecSThomas Haller  *    don't purge the prefix, assume user space is managing it.
9305b84efecSThomas Haller  * 6) otherwise, update prefix lifetime to the
9315b84efecSThomas Haller  *    longest valid lifetime among the corresponding
9325b84efecSThomas Haller  *    addresses on the device.
9335b84efecSThomas Haller  *    Note: subsequent RA will update lifetime.
9345b84efecSThomas Haller  **/
9355b84efecSThomas Haller static enum cleanup_prefix_rt_t
9365b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
9375b84efecSThomas Haller {
9385b84efecSThomas Haller 	struct inet6_ifaddr *ifa;
9395b84efecSThomas Haller 	struct inet6_dev *idev = ifp->idev;
9405b84efecSThomas Haller 	unsigned long lifetime;
9415b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
9425b84efecSThomas Haller 
9435b84efecSThomas Haller 	*expires = jiffies;
9445b84efecSThomas Haller 
9455b84efecSThomas Haller 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
9465b84efecSThomas Haller 		if (ifa == ifp)
9475b84efecSThomas Haller 			continue;
9485b84efecSThomas Haller 		if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
9495b84efecSThomas Haller 				       ifp->prefix_len))
9505b84efecSThomas Haller 			continue;
9515b84efecSThomas Haller 		if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
9525b84efecSThomas Haller 			return CLEANUP_PREFIX_RT_NOP;
9535b84efecSThomas Haller 
9545b84efecSThomas Haller 		action = CLEANUP_PREFIX_RT_EXPIRE;
9555b84efecSThomas Haller 
9565b84efecSThomas Haller 		spin_lock(&ifa->lock);
9575b84efecSThomas Haller 
9585b84efecSThomas Haller 		lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
9595b84efecSThomas Haller 		/*
9605b84efecSThomas Haller 		 * Note: Because this address is
9615b84efecSThomas Haller 		 * not permanent, lifetime <
9625b84efecSThomas Haller 		 * LONG_MAX / HZ here.
9635b84efecSThomas Haller 		 */
9645b84efecSThomas Haller 		if (time_before(*expires, ifa->tstamp + lifetime * HZ))
9655b84efecSThomas Haller 			*expires = ifa->tstamp + lifetime * HZ;
9665b84efecSThomas Haller 		spin_unlock(&ifa->lock);
9675b84efecSThomas Haller 	}
9685b84efecSThomas Haller 
9695b84efecSThomas Haller 	return action;
9705b84efecSThomas Haller }
9715b84efecSThomas Haller 
9725b84efecSThomas Haller static void
9735b84efecSThomas Haller cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
9745b84efecSThomas Haller {
9755b84efecSThomas Haller 	struct rt6_info *rt;
9765b84efecSThomas Haller 
9775b84efecSThomas Haller 	rt = addrconf_get_prefix_route(&ifp->addr,
9785b84efecSThomas Haller 				       ifp->prefix_len,
9795b84efecSThomas Haller 				       ifp->idev->dev,
9805b84efecSThomas Haller 				       0, RTF_GATEWAY | RTF_DEFAULT);
9815b84efecSThomas Haller 	if (rt) {
9825b84efecSThomas Haller 		if (del_rt)
9835b84efecSThomas Haller 			ip6_del_rt(rt);
9845b84efecSThomas Haller 		else {
9855b84efecSThomas Haller 			if (!(rt->rt6i_flags & RTF_EXPIRES))
9865b84efecSThomas Haller 				rt6_set_expires(rt, expires);
9875b84efecSThomas Haller 			ip6_rt_put(rt);
9885b84efecSThomas Haller 		}
9895b84efecSThomas Haller 	}
9905b84efecSThomas Haller }
9915b84efecSThomas Haller 
9925b84efecSThomas Haller 
9931da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
9961da177e4SLinus Torvalds {
9974c5ff6a6SHerbert Xu 	int state;
9985b84efecSThomas Haller 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
9995b84efecSThomas Haller 	unsigned long expires;
10001da177e4SLinus Torvalds 
1001c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
1002c15b1ccaSHannes Frederic Sowa 
10034c5ff6a6SHerbert Xu 	spin_lock_bh(&ifp->state_lock);
10044c5ff6a6SHerbert Xu 	state = ifp->state;
1005e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
10064c5ff6a6SHerbert Xu 	spin_unlock_bh(&ifp->state_lock);
10074c5ff6a6SHerbert Xu 
10084c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
10094c5ff6a6SHerbert Xu 		goto out;
10101da177e4SLinus Torvalds 
10115c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
10125c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
10135c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
10141da177e4SLinus Torvalds 
10155b84efecSThomas Haller 	write_lock_bh(&ifp->idev->lock);
10165d9efa7eSDavid S. Miller 
10171da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
1018372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
10191da177e4SLinus Torvalds 		if (ifp->ifpub) {
10201da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
10211da177e4SLinus Torvalds 			ifp->ifpub = NULL;
10221da177e4SLinus Torvalds 		}
10231da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
10241da177e4SLinus Torvalds 	}
10251da177e4SLinus Torvalds 
10265b84efecSThomas Haller 	if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
10275b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &expires);
10285b84efecSThomas Haller 
1029502a2ffdSstephen hemminger 	list_del_init(&ifp->if_list);
10301da177e4SLinus Torvalds 	__in6_ifa_put(ifp);
1031502a2ffdSstephen hemminger 
10325b84efecSThomas Haller 	write_unlock_bh(&ifp->idev->lock);
10331da177e4SLinus Torvalds 
1034c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
1035b2238566SAndrey Vagin 
10361da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
10371da177e4SLinus Torvalds 
1038f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
10391da177e4SLinus Torvalds 
10405b84efecSThomas Haller 	if (action != CLEANUP_PREFIX_RT_NOP) {
10415b84efecSThomas Haller 		cleanup_prefix_route(ifp, expires,
10425b84efecSThomas Haller 			action == CLEANUP_PREFIX_RT_DEL);
10431da177e4SLinus Torvalds 	}
10441da177e4SLinus Torvalds 
1045c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1046c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
10474c5ff6a6SHerbert Xu out:
10481da177e4SLinus Torvalds 	in6_ifa_put(ifp);
10491da177e4SLinus Torvalds }
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
10521da177e4SLinus Torvalds {
10531da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
10541da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
105576f793e3SLorenzo Colitti 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1056eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
10571da177e4SLinus Torvalds 	int tmp_plen;
10581da177e4SLinus Torvalds 	int ret = 0;
105995c385b4SNeil Horman 	u32 addr_flags;
106076f793e3SLorenzo Colitti 	unsigned long now = jiffies;
10611da177e4SLinus Torvalds 
106253bd6749SJiri Pirko 	write_lock_bh(&idev->lock);
10631da177e4SLinus Torvalds 	if (ift) {
10641da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
10651da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
10661da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
10671da177e4SLinus Torvalds 		tmpaddr = &addr;
10681da177e4SLinus Torvalds 	} else {
10691da177e4SLinus Torvalds 		tmpaddr = NULL;
10701da177e4SLinus Torvalds 	}
10711da177e4SLinus Torvalds retry:
10721da177e4SLinus Torvalds 	in6_dev_hold(idev);
10731da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
107453bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1075f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
10761da177e4SLinus Torvalds 		in6_dev_put(idev);
10771da177e4SLinus Torvalds 		ret = -1;
10781da177e4SLinus Torvalds 		goto out;
10791da177e4SLinus Torvalds 	}
10801da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
10811da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
10821da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
10831da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
108453bd6749SJiri Pirko 		write_unlock_bh(&idev->lock);
1085f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1086f3213831SJoe Perches 			__func__);
10871da177e4SLinus Torvalds 		in6_dev_put(idev);
10881da177e4SLinus Torvalds 		ret = -1;
10891da177e4SLinus Torvalds 		goto out;
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
10921da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1093eb7e0575SSorin Dumitru 	__ipv6_try_regen_rndid(idev, tmpaddr);
10941da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
109576f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
10961da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
10971da177e4SLinus Torvalds 			      ifp->valid_lft,
10987a876b0eSGlenn Wurster 			      idev->cnf.temp_valid_lft + age);
10991da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
11001da177e4SLinus Torvalds 				 ifp->prefered_lft,
11017a876b0eSGlenn Wurster 				 idev->cnf.temp_prefered_lft + age -
1102784e2710SBen Hutchings 				 idev->cnf.max_desync_factor);
11031da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
11041da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
11051da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
11061da177e4SLinus Torvalds 
1107eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1108eac55bf9SBenoit Boissinot 	                idev->cnf.dad_transmits *
11091f9248e5SJiri Pirko 	                NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
111053bd6749SJiri Pirko 	write_unlock_bh(&idev->lock);
111195c385b4SNeil Horman 
1112eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1113eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1114eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1115eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1116ecab6701SHeiner Kallweit 	 * Use age calculation as in addrconf_verify to avoid unnecessary
1117ecab6701SHeiner Kallweit 	 * temporary addresses being generated.
1118eac55bf9SBenoit Boissinot 	 */
1119ecab6701SHeiner Kallweit 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1120ecab6701SHeiner Kallweit 	if (tmp_prefered_lft <= regen_advance + age) {
1121eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1122eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1123eac55bf9SBenoit Boissinot 		ret = -1;
1124eac55bf9SBenoit Boissinot 		goto out;
1125eac55bf9SBenoit Boissinot 	}
1126eac55bf9SBenoit Boissinot 
112795c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
112895c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
112995c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
113095c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
113195c385b4SNeil Horman 
11324b08a8f1SHannes Frederic Sowa 	ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
11338a226b2cSJiri Benc 			    ipv6_addr_scope(&addr), addr_flags,
11344b08a8f1SHannes Frederic Sowa 			    tmp_valid_lft, tmp_prefered_lft);
11354b08a8f1SHannes Frederic Sowa 	if (IS_ERR(ift)) {
11361da177e4SLinus Torvalds 		in6_ifa_put(ifp);
11371da177e4SLinus Torvalds 		in6_dev_put(idev);
1138f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
11391da177e4SLinus Torvalds 		tmpaddr = &addr;
114053bd6749SJiri Pirko 		write_lock_bh(&idev->lock);
11411da177e4SLinus Torvalds 		goto retry;
11421da177e4SLinus Torvalds 	}
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
11451da177e4SLinus Torvalds 	ift->ifpub = ifp;
114676f793e3SLorenzo Colitti 	ift->cstamp = now;
11471da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
11481da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
11491da177e4SLinus Torvalds 
1150cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
11511da177e4SLinus Torvalds 	in6_ifa_put(ift);
11521da177e4SLinus Torvalds 	in6_dev_put(idev);
11531da177e4SLinus Torvalds out:
11541da177e4SLinus Torvalds 	return ret;
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds /*
1158072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
11591da177e4SLinus Torvalds  */
1160a9b05723SYOSHIFUJI Hideaki enum {
1161a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1162a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1163a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1164a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1165a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1166a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1167a9b05723SYOSHIFUJI Hideaki #endif
1168a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1169a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1170a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1171a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1172a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
1173a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1174072047e4SYOSHIFUJI Hideaki };
1175072047e4SYOSHIFUJI Hideaki 
1176a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1177a9b05723SYOSHIFUJI Hideaki 	int			rule;
1178a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1179a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1180a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1181a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1182a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1183a9b05723SYOSHIFUJI Hideaki };
1184a9b05723SYOSHIFUJI Hideaki 
1185a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
11869acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1187a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1188a9b05723SYOSHIFUJI Hideaki 	int scope;
1189a9b05723SYOSHIFUJI Hideaki 	int label;
11907cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1191a9b05723SYOSHIFUJI Hideaki };
1192072047e4SYOSHIFUJI Hideaki 
1193b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
11941da177e4SLinus Torvalds {
119545bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1196072047e4SYOSHIFUJI Hideaki 		return 1;
1197072047e4SYOSHIFUJI Hideaki 	return 0;
11981da177e4SLinus Torvalds }
11991da177e4SLinus Torvalds 
12003de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
12013de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1202a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1203a9b05723SYOSHIFUJI Hideaki 			       int i)
1204a9b05723SYOSHIFUJI Hideaki {
1205a9b05723SYOSHIFUJI Hideaki 	int ret;
1206a9b05723SYOSHIFUJI Hideaki 
1207a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1208a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1209a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1210a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1211a9b05723SYOSHIFUJI Hideaki 			break;
1212a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1213a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1214a9b05723SYOSHIFUJI Hideaki 			break;
1215a9b05723SYOSHIFUJI Hideaki 		default:
1216a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1217a9b05723SYOSHIFUJI Hideaki 		}
1218a9b05723SYOSHIFUJI Hideaki 		goto out;
1219a9b05723SYOSHIFUJI Hideaki 	}
1220a9b05723SYOSHIFUJI Hideaki 
1221a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1222a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1223a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1224a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1225a9b05723SYOSHIFUJI Hideaki 		break;
1226a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1227a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1228a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1229a9b05723SYOSHIFUJI Hideaki 		break;
1230a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1231a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1232a9b05723SYOSHIFUJI Hideaki 		 *
1233a9b05723SYOSHIFUJI Hideaki 		 *      ret
1234a9b05723SYOSHIFUJI Hideaki 		 *       ^
1235a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1236a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1237a9b05723SYOSHIFUJI Hideaki 		 *       |
1238a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1239a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1240a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1241db9c7c39Sstephen hemminger 		 *  B-15 |    \        if scope is enough for destination.
1242a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1243a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1244a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1245a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1246a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1247a9b05723SYOSHIFUJI Hideaki 		 *
1248a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1249a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1250a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1251a9b05723SYOSHIFUJI Hideaki 		 */
1252a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1253a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1254a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1255a9b05723SYOSHIFUJI Hideaki 		else
1256a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1257a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1258a9b05723SYOSHIFUJI Hideaki 		break;
1259a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
1260a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
1261a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
1262a9b05723SYOSHIFUJI Hideaki 		      !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1263a9b05723SYOSHIFUJI Hideaki 		break;
1264a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1265a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
12667cbca67cSYOSHIFUJI Hideaki 	    {
1267a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
12687cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
12697cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1270a9b05723SYOSHIFUJI Hideaki 		break;
12717cbca67cSYOSHIFUJI Hideaki 	    }
1272a9b05723SYOSHIFUJI Hideaki #endif
1273a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1274a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1275a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1276a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1277a9b05723SYOSHIFUJI Hideaki 		break;
1278a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1279a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
12803de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
12813de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1282a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1283a9b05723SYOSHIFUJI Hideaki 		break;
1284a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
12857cbca67cSYOSHIFUJI Hideaki 	    {
1286a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
128725985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1288a9b05723SYOSHIFUJI Hideaki 		 */
12897cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
12907cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
12917cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
12927cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1293a9b05723SYOSHIFUJI Hideaki 		break;
12947cbca67cSYOSHIFUJI Hideaki 	    }
1295a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1296a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1297a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1298a9b05723SYOSHIFUJI Hideaki 		 */
1299a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1300a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1301a9b05723SYOSHIFUJI Hideaki 		break;
1302a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1303a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
130491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
130591b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
130691b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
130791b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1308a9b05723SYOSHIFUJI Hideaki 		break;
1309a9b05723SYOSHIFUJI Hideaki 	default:
1310a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1311a9b05723SYOSHIFUJI Hideaki 	}
1312a9b05723SYOSHIFUJI Hideaki 
1313a9b05723SYOSHIFUJI Hideaki 	if (ret)
1314a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1315a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1316a9b05723SYOSHIFUJI Hideaki out:
1317a9b05723SYOSHIFUJI Hideaki 	return ret;
1318a9b05723SYOSHIFUJI Hideaki }
1319a9b05723SYOSHIFUJI Hideaki 
1320b3f644fcSPatrick McHardy int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
13219acd9f3aSYOSHIFUJI Hideaki 		       const struct in6_addr *daddr, unsigned int prefs,
13227cbca67cSYOSHIFUJI Hideaki 		       struct in6_addr *saddr)
13231da177e4SLinus Torvalds {
1324a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_score scores[2],
1325a9b05723SYOSHIFUJI Hideaki 				*score = &scores[0], *hiscore = &scores[1];
1326a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_dst dst;
1327072047e4SYOSHIFUJI Hideaki 	struct net_device *dev;
1328a9b05723SYOSHIFUJI Hideaki 	int dst_type;
13291da177e4SLinus Torvalds 
1330a9b05723SYOSHIFUJI Hideaki 	dst_type = __ipv6_addr_type(daddr);
1331a9b05723SYOSHIFUJI Hideaki 	dst.addr = daddr;
1332a9b05723SYOSHIFUJI Hideaki 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1333a9b05723SYOSHIFUJI Hideaki 	dst.scope = __ipv6_addr_src_scope(dst_type);
13343de23255SBenjamin Thery 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
13357cbca67cSYOSHIFUJI Hideaki 	dst.prefs = prefs;
1336a9b05723SYOSHIFUJI Hideaki 
1337a9b05723SYOSHIFUJI Hideaki 	hiscore->rule = -1;
1338a9b05723SYOSHIFUJI Hideaki 	hiscore->ifa = NULL;
13391da177e4SLinus Torvalds 
13408814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1341072047e4SYOSHIFUJI Hideaki 
1342c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
1343072047e4SYOSHIFUJI Hideaki 		struct inet6_dev *idev;
1344072047e4SYOSHIFUJI Hideaki 
1345a9b05723SYOSHIFUJI Hideaki 		/* Candidate Source Address (section 4)
1346072047e4SYOSHIFUJI Hideaki 		 *  - multicast and link-local destination address,
1347072047e4SYOSHIFUJI Hideaki 		 *    the set of candidate source address MUST only
1348072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1349072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same link as the outgoing
1350072047e4SYOSHIFUJI Hideaki 		 *    interface.
1351072047e4SYOSHIFUJI Hideaki 		 * (- For site-local destination addresses, the
1352072047e4SYOSHIFUJI Hideaki 		 *    set of candidate source addresses MUST only
1353072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1354072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same site as the outgoing
1355072047e4SYOSHIFUJI Hideaki 		 *    interface.)
1356072047e4SYOSHIFUJI Hideaki 		 */
1357a9b05723SYOSHIFUJI Hideaki 		if (((dst_type & IPV6_ADDR_MULTICAST) ||
1358a9b05723SYOSHIFUJI Hideaki 		     dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1359a9b05723SYOSHIFUJI Hideaki 		    dst.ifindex && dev->ifindex != dst.ifindex)
1360072047e4SYOSHIFUJI Hideaki 			continue;
1361072047e4SYOSHIFUJI Hideaki 
13621da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
1363072047e4SYOSHIFUJI Hideaki 		if (!idev)
1364072047e4SYOSHIFUJI Hideaki 			continue;
1365072047e4SYOSHIFUJI Hideaki 
13661da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1367502a2ffdSstephen hemminger 		list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1368a9b05723SYOSHIFUJI Hideaki 			int i;
1369072047e4SYOSHIFUJI Hideaki 
1370a9b05723SYOSHIFUJI Hideaki 			/*
13716b3ae80aSYOSHIFUJI Hideaki 			 * - Tentative Address (RFC2462 section 5.4)
13726b3ae80aSYOSHIFUJI Hideaki 			 *  - A tentative address is not considered
13736b3ae80aSYOSHIFUJI Hideaki 			 *    "assigned to an interface" in the traditional
137495c385b4SNeil Horman 			 *    sense, unless it is also flagged as optimistic.
13756b3ae80aSYOSHIFUJI Hideaki 			 * - Candidate Source Address (section 4)
1376072047e4SYOSHIFUJI Hideaki 			 *  - In any case, anycast addresses, multicast
1377072047e4SYOSHIFUJI Hideaki 			 *    addresses, and the unspecified address MUST
1378072047e4SYOSHIFUJI Hideaki 			 *    NOT be included in a candidate set.
1379072047e4SYOSHIFUJI Hideaki 			 */
1380a9b05723SYOSHIFUJI Hideaki 			if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1381a9b05723SYOSHIFUJI Hideaki 			    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
13826b3ae80aSYOSHIFUJI Hideaki 				continue;
1383a9b05723SYOSHIFUJI Hideaki 
1384a9b05723SYOSHIFUJI Hideaki 			score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1385a9b05723SYOSHIFUJI Hideaki 
1386a9b05723SYOSHIFUJI Hideaki 			if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1387a9b05723SYOSHIFUJI Hideaki 				     score->addr_type & IPV6_ADDR_MULTICAST)) {
1388072047e4SYOSHIFUJI Hideaki 				LIMIT_NETDEBUG(KERN_DEBUG
1389072047e4SYOSHIFUJI Hideaki 					       "ADDRCONF: unspecified / multicast address "
1390072047e4SYOSHIFUJI Hideaki 					       "assigned as unicast address on %s",
1391072047e4SYOSHIFUJI Hideaki 					       dev->name);
13921da177e4SLinus Torvalds 				continue;
1393072047e4SYOSHIFUJI Hideaki 			}
1394072047e4SYOSHIFUJI Hideaki 
1395a9b05723SYOSHIFUJI Hideaki 			score->rule = -1;
1396a9b05723SYOSHIFUJI Hideaki 			bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1397072047e4SYOSHIFUJI Hideaki 
1398a9b05723SYOSHIFUJI Hideaki 			for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1399a9b05723SYOSHIFUJI Hideaki 				int minihiscore, miniscore;
1400072047e4SYOSHIFUJI Hideaki 
14013de23255SBenjamin Thery 				minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
14023de23255SBenjamin Thery 				miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1403072047e4SYOSHIFUJI Hideaki 
1404a9b05723SYOSHIFUJI Hideaki 				if (minihiscore > miniscore) {
1405a9b05723SYOSHIFUJI Hideaki 					if (i == IPV6_SADDR_RULE_SCOPE &&
1406a9b05723SYOSHIFUJI Hideaki 					    score->scopedist > 0) {
1407a9b05723SYOSHIFUJI Hideaki 						/*
1408a9b05723SYOSHIFUJI Hideaki 						 * special case:
1409a9b05723SYOSHIFUJI Hideaki 						 * each remaining entry
1410a9b05723SYOSHIFUJI Hideaki 						 * has too small (not enough)
1411a9b05723SYOSHIFUJI Hideaki 						 * scope, because ifa entries
1412a9b05723SYOSHIFUJI Hideaki 						 * are sorted by their scope
1413a9b05723SYOSHIFUJI Hideaki 						 * values.
1414072047e4SYOSHIFUJI Hideaki 						 */
1415a9b05723SYOSHIFUJI Hideaki 						goto try_nextdev;
1416072047e4SYOSHIFUJI Hideaki 					}
1417a9b05723SYOSHIFUJI Hideaki 					break;
1418a9b05723SYOSHIFUJI Hideaki 				} else if (minihiscore < miniscore) {
1419a9b05723SYOSHIFUJI Hideaki 					if (hiscore->ifa)
1420a9b05723SYOSHIFUJI Hideaki 						in6_ifa_put(hiscore->ifa);
142199cd07a5SJuha-Matti Tapio 
1422a9b05723SYOSHIFUJI Hideaki 					in6_ifa_hold(score->ifa);
1423072047e4SYOSHIFUJI Hideaki 
1424a0bffffcSIlpo Järvinen 					swap(hiscore, score);
1425a9b05723SYOSHIFUJI Hideaki 
1426a9b05723SYOSHIFUJI Hideaki 					/* restore our iterator */
1427a9b05723SYOSHIFUJI Hideaki 					score->ifa = hiscore->ifa;
1428a9b05723SYOSHIFUJI Hideaki 
1429a9b05723SYOSHIFUJI Hideaki 					break;
14301da177e4SLinus Torvalds 				}
1431a9b05723SYOSHIFUJI Hideaki 			}
1432a9b05723SYOSHIFUJI Hideaki 		}
1433a9b05723SYOSHIFUJI Hideaki try_nextdev:
14341da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
14351da177e4SLinus Torvalds 	}
14368814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
14371da177e4SLinus Torvalds 
1438a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1439072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
14401da177e4SLinus Torvalds 
14414e3fd7a0SAlexey Dobriyan 	*saddr = hiscore->ifa->addr;
1442a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1443072047e4SYOSHIFUJI Hideaki 	return 0;
14441da177e4SLinus Torvalds }
14455e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
14461da177e4SLinus Torvalds 
14478965779dSAmerigo Wang int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1448479840ffSJiri Pirko 		      u32 banned_flags)
1449b7b1bfceSHannes Frederic Sowa {
1450b7b1bfceSHannes Frederic Sowa 	struct inet6_ifaddr *ifp;
1451b7b1bfceSHannes Frederic Sowa 	int err = -EADDRNOTAVAIL;
1452b7b1bfceSHannes Frederic Sowa 
1453602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1454602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1455602582caSHannes Frederic Sowa 			break;
1456b7b1bfceSHannes Frederic Sowa 		if (ifp->scope == IFA_LINK &&
1457b7b1bfceSHannes Frederic Sowa 		    !(ifp->flags & banned_flags)) {
1458b7b1bfceSHannes Frederic Sowa 			*addr = ifp->addr;
1459b7b1bfceSHannes Frederic Sowa 			err = 0;
1460b7b1bfceSHannes Frederic Sowa 			break;
1461b7b1bfceSHannes Frederic Sowa 		}
1462b7b1bfceSHannes Frederic Sowa 	}
1463b7b1bfceSHannes Frederic Sowa 	return err;
1464b7b1bfceSHannes Frederic Sowa }
1465b7b1bfceSHannes Frederic Sowa 
146695c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1467479840ffSJiri Pirko 		    u32 banned_flags)
14681da177e4SLinus Torvalds {
14691da177e4SLinus Torvalds 	struct inet6_dev *idev;
14701da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
14711da177e4SLinus Torvalds 
14728814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1473e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1474e21e8467SStephen Hemminger 	if (idev) {
14751da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1476b7b1bfceSHannes Frederic Sowa 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
14771da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
14781da177e4SLinus Torvalds 	}
14798814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
14801da177e4SLinus Torvalds 	return err;
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
14841da177e4SLinus Torvalds {
14851da177e4SLinus Torvalds 	int cnt = 0;
14861da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1489502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list)
14901da177e4SLinus Torvalds 		cnt++;
14911da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
14921da177e4SLinus Torvalds 	return cnt;
14931da177e4SLinus Torvalds }
14941da177e4SLinus Torvalds 
1495b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
14962a7851bfSFlorian Westphal 		  const struct net_device *dev, int strict)
14971da177e4SLinus Torvalds {
1498eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1499ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
15001da177e4SLinus Torvalds 
15015c578aedSstephen hemminger 	rcu_read_lock_bh();
1502b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1503878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1504bfeade08SDaniel Lezcano 			continue;
15051da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1506eedf042aSStephen Hemminger 		    !(ifp->flags&IFA_F_TENTATIVE) &&
1507eedf042aSStephen Hemminger 		    (dev == NULL || ifp->idev->dev == dev ||
1508eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
15095c578aedSstephen hemminger 			rcu_read_unlock_bh();
1510eedf042aSStephen Hemminger 			return 1;
1511eedf042aSStephen Hemminger 		}
1512eedf042aSStephen Hemminger 	}
15135c578aedSstephen hemminger 
1514eedf042aSStephen Hemminger 	rcu_read_unlock_bh();
1515eedf042aSStephen Hemminger 	return 0;
15161da177e4SLinus Torvalds }
15177159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr);
15187159039aSYOSHIFUJI Hideaki 
15193e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
152006bfe655SDaniel Lezcano 			       struct net_device *dev)
15211da177e4SLinus Torvalds {
1522ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
15231da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
15241da177e4SLinus Torvalds 
1525b67bfe0dSSasha Levin 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1526878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
152706bfe655SDaniel Lezcano 			continue;
15281da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
15291da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev)
15303e81c6daSDavid S. Miller 				return true;
15311da177e4SLinus Torvalds 		}
15321da177e4SLinus Torvalds 	}
15333e81c6daSDavid S. Miller 	return false;
15341da177e4SLinus Torvalds }
15351da177e4SLinus Torvalds 
15367df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev.
15377df37ff3SCatalin\(ux\) M. BOIE  * If one is found it returns true.
15387df37ff3SCatalin\(ux\) M. BOIE  */
15397df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
15407df37ff3SCatalin\(ux\) M. BOIE 	const unsigned int prefix_len, struct net_device *dev)
15417df37ff3SCatalin\(ux\) M. BOIE {
15427df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_dev *idev;
15437df37ff3SCatalin\(ux\) M. BOIE 	struct inet6_ifaddr *ifa;
15447df37ff3SCatalin\(ux\) M. BOIE 	bool ret = false;
15457df37ff3SCatalin\(ux\) M. BOIE 
15467df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_lock();
15477df37ff3SCatalin\(ux\) M. BOIE 	idev = __in6_dev_get(dev);
15487df37ff3SCatalin\(ux\) M. BOIE 	if (idev) {
15497df37ff3SCatalin\(ux\) M. BOIE 		read_lock_bh(&idev->lock);
15507df37ff3SCatalin\(ux\) M. BOIE 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
15517df37ff3SCatalin\(ux\) M. BOIE 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
15527df37ff3SCatalin\(ux\) M. BOIE 			if (ret)
15537df37ff3SCatalin\(ux\) M. BOIE 				break;
15547df37ff3SCatalin\(ux\) M. BOIE 		}
15557df37ff3SCatalin\(ux\) M. BOIE 		read_unlock_bh(&idev->lock);
15567df37ff3SCatalin\(ux\) M. BOIE 	}
15577df37ff3SCatalin\(ux\) M. BOIE 	rcu_read_unlock();
15587df37ff3SCatalin\(ux\) M. BOIE 
15597df37ff3SCatalin\(ux\) M. BOIE 	return ret;
15607df37ff3SCatalin\(ux\) M. BOIE }
15617df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix);
15627df37ff3SCatalin\(ux\) M. BOIE 
1563b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
156452eeeb84SYOSHIFUJI Hideaki {
156552eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
156652eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
156752eeeb84SYOSHIFUJI Hideaki 	int	onlink;
156852eeeb84SYOSHIFUJI Hideaki 
156952eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
157052eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
157152eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
157252eeeb84SYOSHIFUJI Hideaki 	if (idev) {
157352eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
1574502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
157552eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
157652eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
157752eeeb84SYOSHIFUJI Hideaki 			if (onlink)
157852eeeb84SYOSHIFUJI Hideaki 				break;
157952eeeb84SYOSHIFUJI Hideaki 		}
158052eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
158152eeeb84SYOSHIFUJI Hideaki 	}
158252eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
158352eeeb84SYOSHIFUJI Hideaki 	return onlink;
158452eeeb84SYOSHIFUJI Hideaki }
158552eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
158652eeeb84SYOSHIFUJI Hideaki 
15879acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
15881cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
15891da177e4SLinus Torvalds {
1590b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
1591ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
15921da177e4SLinus Torvalds 
15935c578aedSstephen hemminger 	rcu_read_lock_bh();
1594b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1595878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
15961cab3da6SDaniel Lezcano 			continue;
15971da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
15981da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
15991da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1600b79d1d54SDavid S. Miller 				result = ifp;
16011da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
16021da177e4SLinus Torvalds 				break;
16031da177e4SLinus Torvalds 			}
16041da177e4SLinus Torvalds 		}
16051da177e4SLinus Torvalds 	}
16065c578aedSstephen hemminger 	rcu_read_unlock_bh();
16071da177e4SLinus Torvalds 
1608b79d1d54SDavid S. Miller 	return result;
16091da177e4SLinus Torvalds }
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
16121da177e4SLinus Torvalds 
1613cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
16141da177e4SLinus Torvalds {
16151da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
16161da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
1617c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifp);
16181da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
1619cc411d0bSBrian Haley 		if (dad_failed)
1620cc411d0bSBrian Haley 			ifp->flags |= IFA_F_DADFAILED;
16211da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
1622e2577a06SHerbert Xu 		if (dad_failed)
1623e2577a06SHerbert Xu 			ipv6_ifa_notify(0, ifp);
16241da177e4SLinus Torvalds 		in6_ifa_put(ifp);
16251da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
16261da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
16271da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
16281da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
16291da177e4SLinus Torvalds 		if (ifpub) {
16301da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
16311da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
16321da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
16331da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
16341da177e4SLinus Torvalds 		} else {
16351da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
16361da177e4SLinus Torvalds 		}
16371da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
1638c15b1ccaSHannes Frederic Sowa 	} else {
16391da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
16401da177e4SLinus Torvalds 	}
1641c15b1ccaSHannes Frederic Sowa }
16421da177e4SLinus Torvalds 
1643f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1644f2344a13SHerbert Xu {
1645f2344a13SHerbert Xu 	int err = -ENOENT;
1646f2344a13SHerbert Xu 
1647c15b1ccaSHannes Frederic Sowa 	spin_lock_bh(&ifp->state_lock);
1648f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
1649f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
1650f2344a13SHerbert Xu 		err = 0;
1651f2344a13SHerbert Xu 	}
1652c15b1ccaSHannes Frederic Sowa 	spin_unlock_bh(&ifp->state_lock);
1653f2344a13SHerbert Xu 
1654f2344a13SHerbert Xu 	return err;
1655f2344a13SHerbert Xu }
1656f2344a13SHerbert Xu 
16573c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
16583c21edbdSYOSHIFUJI Hideaki {
16591b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
16609bdd8d40SBrian Haley 
1661853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
1662853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
1663f2344a13SHerbert Xu 		return;
1664853dc2e0SUrsula Braun 	}
1665f2344a13SHerbert Xu 
1666e87cc472SJoe Perches 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
16670522fea6SJens Rosenboom 			     ifp->idev->dev->name, &ifp->addr);
16689bdd8d40SBrian Haley 
16691b34be74SYOSHIFUJI Hideaki 	if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
16701b34be74SYOSHIFUJI Hideaki 		struct in6_addr addr;
16711b34be74SYOSHIFUJI Hideaki 
16721b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[0] = htonl(0xfe800000);
16731b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[1] = 0;
16741b34be74SYOSHIFUJI Hideaki 
16751b34be74SYOSHIFUJI Hideaki 		if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
16761b34be74SYOSHIFUJI Hideaki 		    ipv6_addr_equal(&ifp->addr, &addr)) {
16771b34be74SYOSHIFUJI Hideaki 			/* DAD failed for link-local based on MAC address */
16781b34be74SYOSHIFUJI Hideaki 			idev->cnf.disable_ipv6 = 1;
16799bdd8d40SBrian Haley 
1680f3213831SJoe Perches 			pr_info("%s: IPv6 being disabled!\n",
16819bdd8d40SBrian Haley 				ifp->idev->dev->name);
16821b34be74SYOSHIFUJI Hideaki 		}
16831b34be74SYOSHIFUJI Hideaki 	}
16841b34be74SYOSHIFUJI Hideaki 
1685c15b1ccaSHannes Frederic Sowa 	spin_lock_bh(&ifp->state_lock);
1686c15b1ccaSHannes Frederic Sowa 	/* transition from _POSTDAD to _ERRDAD */
1687c15b1ccaSHannes Frederic Sowa 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
1688c15b1ccaSHannes Frederic Sowa 	spin_unlock_bh(&ifp->state_lock);
1689c15b1ccaSHannes Frederic Sowa 
1690c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, 0);
16913c21edbdSYOSHIFUJI Hideaki }
16921da177e4SLinus Torvalds 
1693a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group.
1694a9ed4a29SSabrina Dubroca  * caller must hold RTNL */
1695b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
16961da177e4SLinus Torvalds {
16971da177e4SLinus Torvalds 	struct in6_addr maddr;
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
17001da177e4SLinus Torvalds 		return;
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
17031da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
17041da177e4SLinus Torvalds }
17051da177e4SLinus Torvalds 
1706a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
1707b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
17081da177e4SLinus Torvalds {
17091da177e4SLinus Torvalds 	struct in6_addr maddr;
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
17121da177e4SLinus Torvalds 		return;
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
17151da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
17161da177e4SLinus Torvalds }
17171da177e4SLinus Torvalds 
1718a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
171920380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
17201da177e4SLinus Torvalds {
17211da177e4SLinus Torvalds 	struct in6_addr addr;
1722c15b1ccaSHannes Frederic Sowa 
172388ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
17242bda8a0cSBjørn Mork 		return;
17251da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
17261da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
17271da177e4SLinus Torvalds 		return;
17281da177e4SLinus Torvalds 	ipv6_dev_ac_inc(ifp->idev->dev, &addr);
17291da177e4SLinus Torvalds }
17301da177e4SLinus Torvalds 
1731a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
173220380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
17331da177e4SLinus Torvalds {
17341da177e4SLinus Torvalds 	struct in6_addr addr;
1735c15b1ccaSHannes Frederic Sowa 
173688ad3149SHannes Frederic Sowa 	if (ifp->prefix_len >= 127) /* RFC 6164 */
173732019e65SYOSHIFUJI Hideaki 		return;
17381da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
17391da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
17401da177e4SLinus Torvalds 		return;
17411da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
17421da177e4SLinus Torvalds }
17431da177e4SLinus Torvalds 
1744073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
17451da177e4SLinus Torvalds {
17461da177e4SLinus Torvalds 	if (dev->addr_len != ETH_ALEN)
17471da177e4SLinus Torvalds 		return -1;
17481da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr, 3);
17491da177e4SLinus Torvalds 	memcpy(eui + 5, dev->dev_addr + 3, 3);
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	/*
17521da177e4SLinus Torvalds 	 * The zSeries OSA network cards can be shared among various
17531da177e4SLinus Torvalds 	 * OS instances, but the OSA cards have only one MAC address.
17541da177e4SLinus Torvalds 	 * This leads to duplicate address conflicts in conjunction
17551da177e4SLinus Torvalds 	 * with IPv6 if more than one instance uses the same card.
17561da177e4SLinus Torvalds 	 *
17571da177e4SLinus Torvalds 	 * The driver for these cards can deliver a unique 16-bit
17581da177e4SLinus Torvalds 	 * identifier for each instance sharing the same card.  It is
17591da177e4SLinus Torvalds 	 * placed instead of 0xFFFE in the interface identifier.  The
17601da177e4SLinus Torvalds 	 * "u" bit of the interface identifier is not inverted in this
17611da177e4SLinus Torvalds 	 * case.  Hence the resulting interface identifier has local
17621da177e4SLinus Torvalds 	 * scope according to RFC2373.
17631da177e4SLinus Torvalds 	 */
17641da177e4SLinus Torvalds 	if (dev->dev_id) {
17651da177e4SLinus Torvalds 		eui[3] = (dev->dev_id >> 8) & 0xFF;
17661da177e4SLinus Torvalds 		eui[4] = dev->dev_id & 0xFF;
17671da177e4SLinus Torvalds 	} else {
17681da177e4SLinus Torvalds 		eui[3] = 0xFF;
17691da177e4SLinus Torvalds 		eui[4] = 0xFE;
17701da177e4SLinus Torvalds 		eui[0] ^= 2;
17711da177e4SLinus Torvalds 	}
17721da177e4SLinus Torvalds 	return 0;
1773073a8e0eSYOSHIFUJI Hideaki }
1774073a8e0eSYOSHIFUJI Hideaki 
177506a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
177606a4c1c5Salex.bluesman.smirnov@gmail.com {
177706a4c1c5Salex.bluesman.smirnov@gmail.com 	if (dev->addr_len != IEEE802154_ADDR_LEN)
177806a4c1c5Salex.bluesman.smirnov@gmail.com 		return -1;
177906a4c1c5Salex.bluesman.smirnov@gmail.com 	memcpy(eui, dev->dev_addr, 8);
17805e98a36eSYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
178106a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
178206a4c1c5Salex.bluesman.smirnov@gmail.com }
178306a4c1c5Salex.bluesman.smirnov@gmail.com 
1784cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1785cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
1786cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	union fwnet_hwaddr *ha;
1787cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1788cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
1789cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
1790cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1791cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	ha = (union fwnet_hwaddr *)dev->dev_addr;
1792cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1793cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1794cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
1795cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
1796cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
1797cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1798073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1799073a8e0eSYOSHIFUJI Hideaki {
18001da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
18011da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
18021da177e4SLinus Torvalds 		return -1;
18031da177e4SLinus Torvalds 	memset(eui, 0, 7);
18041da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
18051da177e4SLinus Torvalds 	return 0;
1806073a8e0eSYOSHIFUJI Hideaki }
1807073a8e0eSYOSHIFUJI Hideaki 
1808073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1809073a8e0eSYOSHIFUJI Hideaki {
18101da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
18111da177e4SLinus Torvalds 		return -1;
18121da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
18131da177e4SLinus Torvalds 	eui[0] |= 2;
18141da177e4SLinus Torvalds 	return 0;
18151da177e4SLinus Torvalds }
1816073a8e0eSYOSHIFUJI Hideaki 
1817c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1818dfd982baSYOSHIFUJI Hideaki {
18199af28511SSascha Hlusiak 	if (addr == 0)
18209af28511SSascha Hlusiak 		return -1;
1821dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1822dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1823dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1824dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1825dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1826dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1827dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
1828dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
1829dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
1830dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
1831dfd982baSYOSHIFUJI Hideaki 	return 0;
1832dfd982baSYOSHIFUJI Hideaki }
1833dfd982baSYOSHIFUJI Hideaki 
1834dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1835dfd982baSYOSHIFUJI Hideaki {
1836dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
1837dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1838dfd982baSYOSHIFUJI Hideaki 	return -1;
1839dfd982baSYOSHIFUJI Hideaki }
1840dfd982baSYOSHIFUJI Hideaki 
1841aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1842aee80b54Sstephen hemminger {
1843aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1844aee80b54Sstephen hemminger }
1845aee80b54Sstephen hemminger 
1846e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
1847e837735eSNicolas Dichtel {
1848e837735eSNicolas Dichtel 	memcpy(eui, dev->perm_addr, 3);
1849e837735eSNicolas Dichtel 	memcpy(eui + 5, dev->perm_addr + 3, 3);
1850e837735eSNicolas Dichtel 	eui[3] = 0xFF;
1851e837735eSNicolas Dichtel 	eui[4] = 0xFE;
1852e837735eSNicolas Dichtel 	eui[0] ^= 2;
1853e837735eSNicolas Dichtel 	return 0;
1854e837735eSNicolas Dichtel }
1855e837735eSNicolas Dichtel 
1856073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1857073a8e0eSYOSHIFUJI Hideaki {
1858073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
1859073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
1860073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
1861073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
1862073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
1863073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
1864073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
1865073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
1866c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
1867dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
1868aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
1869aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
1870e74bccb8SJukka Rissanen 	case ARPHRD_6LOWPAN:
187106a4c1c5Salex.bluesman.smirnov@gmail.com 	case ARPHRD_IEEE802154:
187206a4c1c5Salex.bluesman.smirnov@gmail.com 		return addrconf_ifid_eui64(eui, dev);
1873cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
1874cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
1875e837735eSNicolas Dichtel 	case ARPHRD_TUNNEL6:
1876e837735eSNicolas Dichtel 		return addrconf_ifid_ip6tnl(eui, dev);
1877073a8e0eSYOSHIFUJI Hideaki 	}
18781da177e4SLinus Torvalds 	return -1;
18791da177e4SLinus Torvalds }
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
18821da177e4SLinus Torvalds {
18831da177e4SLinus Torvalds 	int err = -1;
18841da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1887602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1888602582caSHannes Frederic Sowa 		if (ifp->scope > IFA_LINK)
1889602582caSHannes Frederic Sowa 			break;
18901da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
18911da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
18921da177e4SLinus Torvalds 			err = 0;
18931da177e4SLinus Torvalds 			break;
18941da177e4SLinus Torvalds 		}
18951da177e4SLinus Torvalds 	}
18961da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
18971da177e4SLinus Torvalds 	return err;
18981da177e4SLinus Torvalds }
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1901eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev)
19021da177e4SLinus Torvalds {
19031da177e4SLinus Torvalds regen:
1904955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
19051da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	/*
19081da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
19091da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
19101da177e4SLinus Torvalds 	 *
19111da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
19121da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
1913c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
19141da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
19151da177e4SLinus Torvalds 	 *  - value 0
19161da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
19171da177e4SLinus Torvalds 	 */
19181da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
19191da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
19201da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
19211da177e4SLinus Torvalds 		goto regen;
19221da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
19231da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
19241da177e4SLinus Torvalds 			goto regen;
19251da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
19261da177e4SLinus Torvalds 			goto regen;
19271da177e4SLinus Torvalds 	}
19281da177e4SLinus Torvalds }
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
19311da177e4SLinus Torvalds {
19321da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
19331da177e4SLinus Torvalds 	unsigned long expires;
19341da177e4SLinus Torvalds 
19358814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
19361da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	if (idev->dead)
19391da177e4SLinus Torvalds 		goto out;
19401da177e4SLinus Torvalds 
1941eb7e0575SSorin Dumitru 	__ipv6_regen_rndid(idev);
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds 	expires = jiffies +
19441da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
19451f9248e5SJiri Pirko 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
19461f9248e5SJiri Pirko 		NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
1947784e2710SBen Hutchings 		idev->cnf.max_desync_factor * HZ;
19481da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
1949f3213831SJoe Perches 		pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1950f3213831SJoe Perches 			__func__, idev->dev->name);
19511da177e4SLinus Torvalds 		goto out;
19521da177e4SLinus Torvalds 	}
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
19551da177e4SLinus Torvalds 		in6_dev_hold(idev);
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds out:
19581da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
19598814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
19601da177e4SLinus Torvalds 	in6_dev_put(idev);
19611da177e4SLinus Torvalds }
19621da177e4SLinus Torvalds 
1963eb7e0575SSorin Dumitru static void  __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
19648e5e8f30SEldad Zack {
19651da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1966eb7e0575SSorin Dumitru 		__ipv6_regen_rndid(idev);
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds /*
19701da177e4SLinus Torvalds  *	Add prefix route.
19711da177e4SLinus Torvalds  */
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds static void
19741da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1975e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
19761da177e4SLinus Torvalds {
197786872cb5SThomas Graf 	struct fib6_config cfg = {
197886872cb5SThomas Graf 		.fc_table = RT6_TABLE_PREFIX,
197986872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
198086872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
198186872cb5SThomas Graf 		.fc_expires = expires,
198286872cb5SThomas Graf 		.fc_dst_len = plen,
198386872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
1984c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
1985f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
198686872cb5SThomas Graf 	};
19871da177e4SLinus Torvalds 
19884e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
19911da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
19921da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
19931da177e4SLinus Torvalds 	 */
199407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
19951da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
199686872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
19970be669bbSJoerg Roedel #endif
19981da177e4SLinus Torvalds 
199986872cb5SThomas Graf 	ip6_route_add(&cfg);
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds 
200214ef37b6SAndreas Hofmeister 
200314ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
200414ef37b6SAndreas Hofmeister 						  int plen,
200514ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
200614ef37b6SAndreas Hofmeister 						  u32 flags, u32 noflags)
200714ef37b6SAndreas Hofmeister {
200814ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
200914ef37b6SAndreas Hofmeister 	struct rt6_info *rt = NULL;
201014ef37b6SAndreas Hofmeister 	struct fib6_table *table;
201114ef37b6SAndreas Hofmeister 
201214ef37b6SAndreas Hofmeister 	table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
201314ef37b6SAndreas Hofmeister 	if (table == NULL)
201414ef37b6SAndreas Hofmeister 		return NULL;
201514ef37b6SAndreas Hofmeister 
20165744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
201714ef37b6SAndreas Hofmeister 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
201814ef37b6SAndreas Hofmeister 	if (!fn)
201914ef37b6SAndreas Hofmeister 		goto out;
202014ef37b6SAndreas Hofmeister 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2021d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != dev->ifindex)
202214ef37b6SAndreas Hofmeister 			continue;
202314ef37b6SAndreas Hofmeister 		if ((rt->rt6i_flags & flags) != flags)
202414ef37b6SAndreas Hofmeister 			continue;
202585da53bfSRomain Kuntz 		if ((rt->rt6i_flags & noflags) != 0)
202614ef37b6SAndreas Hofmeister 			continue;
202714ef37b6SAndreas Hofmeister 		dst_hold(&rt->dst);
202814ef37b6SAndreas Hofmeister 		break;
202914ef37b6SAndreas Hofmeister 	}
203014ef37b6SAndreas Hofmeister out:
20315744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
203214ef37b6SAndreas Hofmeister 	return rt;
203314ef37b6SAndreas Hofmeister }
203414ef37b6SAndreas Hofmeister 
203514ef37b6SAndreas Hofmeister 
20361da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
20391da177e4SLinus Torvalds {
204086872cb5SThomas Graf 	struct fib6_config cfg = {
204186872cb5SThomas Graf 		.fc_table = RT6_TABLE_LOCAL,
204286872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
204386872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
204486872cb5SThomas Graf 		.fc_dst_len = 8,
204586872cb5SThomas Graf 		.fc_flags = RTF_UP,
2046c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
204786872cb5SThomas Graf 	};
20481da177e4SLinus Torvalds 
204986872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
205086872cb5SThomas Graf 
205186872cb5SThomas Graf 	ip6_route_add(&cfg);
20521da177e4SLinus Torvalds }
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
20551da177e4SLinus Torvalds {
20561da177e4SLinus Torvalds 	struct inet6_dev *idev;
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	ASSERT_RTNL();
20591da177e4SLinus Torvalds 
2060e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2061e21e8467SStephen Hemminger 	if (!idev)
206264e724f6SBrian Haley 		return ERR_PTR(-ENOBUFS);
206364e724f6SBrian Haley 
206464e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
206564e724f6SBrian Haley 		return ERR_PTR(-EACCES);
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds 	/* Add default multicast route */
20684af04abaSLi Wei 	if (!(dev->flags & IFF_LOOPBACK))
20691da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds 	return idev;
20721da177e4SLinus Torvalds }
20731da177e4SLinus Torvalds 
207453bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev,
207553bd6749SJiri Pirko 			     struct inet6_ifaddr *ifp,
207653bd6749SJiri Pirko 			     __u32 valid_lft, __u32 prefered_lft,
207753bd6749SJiri Pirko 			     bool create, unsigned long now)
207853bd6749SJiri Pirko {
207953bd6749SJiri Pirko 	u32 flags;
208053bd6749SJiri Pirko 	struct inet6_ifaddr *ift;
208153bd6749SJiri Pirko 
208253bd6749SJiri Pirko 	read_lock_bh(&idev->lock);
208353bd6749SJiri Pirko 	/* update all temporary addresses in the list */
208453bd6749SJiri Pirko 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
208553bd6749SJiri Pirko 		int age, max_valid, max_prefered;
208653bd6749SJiri Pirko 
208753bd6749SJiri Pirko 		if (ifp != ift->ifpub)
208853bd6749SJiri Pirko 			continue;
208953bd6749SJiri Pirko 
209053bd6749SJiri Pirko 		/* RFC 4941 section 3.3:
209153bd6749SJiri Pirko 		 * If a received option will extend the lifetime of a public
209253bd6749SJiri Pirko 		 * address, the lifetimes of temporary addresses should
209353bd6749SJiri Pirko 		 * be extended, subject to the overall constraint that no
209453bd6749SJiri Pirko 		 * temporary addresses should ever remain "valid" or "preferred"
209553bd6749SJiri Pirko 		 * for a time longer than (TEMP_VALID_LIFETIME) or
209653bd6749SJiri Pirko 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
209753bd6749SJiri Pirko 		 */
209853bd6749SJiri Pirko 		age = (now - ift->cstamp) / HZ;
209953bd6749SJiri Pirko 		max_valid = idev->cnf.temp_valid_lft - age;
210053bd6749SJiri Pirko 		if (max_valid < 0)
210153bd6749SJiri Pirko 			max_valid = 0;
210253bd6749SJiri Pirko 
210353bd6749SJiri Pirko 		max_prefered = idev->cnf.temp_prefered_lft -
210453bd6749SJiri Pirko 			       idev->cnf.max_desync_factor - age;
210553bd6749SJiri Pirko 		if (max_prefered < 0)
210653bd6749SJiri Pirko 			max_prefered = 0;
210753bd6749SJiri Pirko 
210853bd6749SJiri Pirko 		if (valid_lft > max_valid)
210953bd6749SJiri Pirko 			valid_lft = max_valid;
211053bd6749SJiri Pirko 
211153bd6749SJiri Pirko 		if (prefered_lft > max_prefered)
211253bd6749SJiri Pirko 			prefered_lft = max_prefered;
211353bd6749SJiri Pirko 
211453bd6749SJiri Pirko 		spin_lock(&ift->lock);
211553bd6749SJiri Pirko 		flags = ift->flags;
211653bd6749SJiri Pirko 		ift->valid_lft = valid_lft;
211753bd6749SJiri Pirko 		ift->prefered_lft = prefered_lft;
211853bd6749SJiri Pirko 		ift->tstamp = now;
211953bd6749SJiri Pirko 		if (prefered_lft > 0)
212053bd6749SJiri Pirko 			ift->flags &= ~IFA_F_DEPRECATED;
212153bd6749SJiri Pirko 
212253bd6749SJiri Pirko 		spin_unlock(&ift->lock);
212353bd6749SJiri Pirko 		if (!(flags&IFA_F_TENTATIVE))
212453bd6749SJiri Pirko 			ipv6_ifa_notify(0, ift);
212553bd6749SJiri Pirko 	}
212653bd6749SJiri Pirko 
212753bd6749SJiri Pirko 	if ((create || list_empty(&idev->tempaddr_list)) &&
212853bd6749SJiri Pirko 	    idev->cnf.use_tempaddr > 0) {
212953bd6749SJiri Pirko 		/* When a new public address is created as described
213053bd6749SJiri Pirko 		 * in [ADDRCONF], also create a new temporary address.
213153bd6749SJiri Pirko 		 * Also create a temporary address if it's enabled but
213253bd6749SJiri Pirko 		 * no temporary address currently exists.
213353bd6749SJiri Pirko 		 */
213453bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
213553bd6749SJiri Pirko 		ipv6_create_tempaddr(ifp, NULL);
213653bd6749SJiri Pirko 	} else {
213753bd6749SJiri Pirko 		read_unlock_bh(&idev->lock);
213853bd6749SJiri Pirko 	}
213953bd6749SJiri Pirko }
214053bd6749SJiri Pirko 
2141e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
21421da177e4SLinus Torvalds {
21431da177e4SLinus Torvalds 	struct prefix_info *pinfo;
21441da177e4SLinus Torvalds 	__u32 valid_lft;
21451da177e4SLinus Torvalds 	__u32 prefered_lft;
21461da177e4SLinus Torvalds 	int addr_type;
21471da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
214856d417b1SBrian Haley 	struct net *net = dev_net(dev);
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
2153ba3542e1Sdingtianhong 		ADBG("addrconf: prefix option too short\n");
21541da177e4SLinus Torvalds 		return;
21551da177e4SLinus Torvalds 	}
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	/*
21581da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
21591da177e4SLinus Torvalds 	 */
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
21641da177e4SLinus Torvalds 		return;
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
21671da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2170e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
21711da177e4SLinus Torvalds 		return;
21721da177e4SLinus Torvalds 	}
21731da177e4SLinus Torvalds 
21741da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 	if (in6_dev == NULL) {
2177e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2178e87cc472SJoe Perches 				    dev->name);
21791da177e4SLinus Torvalds 		return;
21801da177e4SLinus Torvalds 	}
21811da177e4SLinus Torvalds 
21821da177e4SLinus Torvalds 	/*
21831da177e4SLinus Torvalds 	 *	Two things going on here:
21841da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
21851da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
21861da177e4SLinus Torvalds 	 */
21871da177e4SLinus Torvalds 
21884bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
21894bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
21904bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
21914bed72e4SYOSHIFUJI Hideaki 
21921da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
21936f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
21946f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
21956f704992SYOSHIFUJI Hideaki 		 * not good.
21961da177e4SLinus Torvalds 		 */
21974bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
21984bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
21994bed72e4SYOSHIFUJI Hideaki 		else
22004bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
22013dd4bc68SYOSHIFUJI Hideaki 
22024bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
22034bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
22041da177e4SLinus Torvalds 
220514ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
220614ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
220714ef37b6SAndreas Hofmeister 					       dev,
220814ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
220914ef37b6SAndreas Hofmeister 					       RTF_GATEWAY | RTF_DEFAULT);
22101da177e4SLinus Torvalds 
221114ef37b6SAndreas Hofmeister 		if (rt) {
22126f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
22131da177e4SLinus Torvalds 			if (valid_lft == 0) {
2214e0a1ad73SThomas Graf 				ip6_del_rt(rt);
22151da177e4SLinus Torvalds 				rt = NULL;
22164bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
22176f704992SYOSHIFUJI Hideaki 				/* not infinity */
22181716a961SGao feng 				rt6_set_expires(rt, jiffies + rt_expires);
22196f704992SYOSHIFUJI Hideaki 			} else {
22201716a961SGao feng 				rt6_clean_expires(rt);
22211da177e4SLinus Torvalds 			}
22221da177e4SLinus Torvalds 		} else if (valid_lft) {
22236f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
22244bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
22254bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
22266f704992SYOSHIFUJI Hideaki 				/* not infinity */
22276f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
22286f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
22296f704992SYOSHIFUJI Hideaki 			}
22301da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
22316f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
22321da177e4SLinus Torvalds 		}
223394e187c0SAmerigo Wang 		ip6_rt_put(rt);
22341da177e4SLinus Torvalds 	}
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
22391da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
22401da177e4SLinus Torvalds 		struct in6_addr addr;
22411da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
2242617fe29dSDaniel Borkmann 		bool tokenized = false;
22431da177e4SLinus Torvalds 
22441da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
22451da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2246f53adae4SDaniel Borkmann 
2247f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2248f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2249f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2250f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2251f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
2252617fe29dSDaniel Borkmann 				tokenized = true;
2253f53adae4SDaniel Borkmann 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
22541da177e4SLinus Torvalds 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
22551da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
22561da177e4SLinus Torvalds 				return;
22571da177e4SLinus Torvalds 			}
22581da177e4SLinus Torvalds 			goto ok;
22591da177e4SLinus Torvalds 		}
2260e87cc472SJoe Perches 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
22611da177e4SLinus Torvalds 				    pinfo->prefix_len);
22621da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
22631da177e4SLinus Torvalds 		return;
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds ok:
22661da177e4SLinus Torvalds 
226753b7997fSYOSHIFUJI Hideaki 		ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
22681da177e4SLinus Torvalds 
22691da177e4SLinus Torvalds 		if (ifp == NULL && valid_lft) {
22701da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
227195c385b4SNeil Horman 			u32 addr_flags = 0;
227295c385b4SNeil Horman 
227395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
227495c385b4SNeil Horman 			if (in6_dev->cnf.optimistic_dad &&
2275e6bff995SNeil Horman 			    !net->ipv6.devconf_all->forwarding && sllao)
227695c385b4SNeil Horman 				addr_flags = IFA_F_OPTIMISTIC;
227795c385b4SNeil Horman #endif
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
22801da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
22811da177e4SLinus Torvalds 			 */
22821da177e4SLinus Torvalds 			if (!max_addresses ||
22831da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
22843f8f5298SJiri Pirko 				ifp = ipv6_add_addr(in6_dev, &addr, NULL,
22853f8f5298SJiri Pirko 						    pinfo->prefix_len,
228695c385b4SNeil Horman 						    addr_type&IPV6_ADDR_SCOPE_MASK,
22878a226b2cSJiri Benc 						    addr_flags, valid_lft,
22888a226b2cSJiri Benc 						    prefered_lft);
22891da177e4SLinus Torvalds 
22903f0d2ba0SYOSHIFUJI Hideaki / 吉藤英明 			if (IS_ERR_OR_NULL(ifp)) {
22911da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
22921da177e4SLinus Torvalds 				return;
22931da177e4SLinus Torvalds 			}
22941da177e4SLinus Torvalds 
22958a226b2cSJiri Benc 			update_lft = 0;
22968a226b2cSJiri Benc 			create = 1;
2297c15b1ccaSHannes Frederic Sowa 			spin_lock_bh(&ifp->lock);
2298c15b1ccaSHannes Frederic Sowa 			ifp->flags |= IFA_F_MANAGETEMPADDR;
22991da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
2300617fe29dSDaniel Borkmann 			ifp->tokenized = tokenized;
2301c15b1ccaSHannes Frederic Sowa 			spin_unlock_bh(&ifp->lock);
2302cf22f9a2SDavid S. Miller 			addrconf_dad_start(ifp);
23031da177e4SLinus Torvalds 		}
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds 		if (ifp) {
2306479840ffSJiri Pirko 			u32 flags;
23071da177e4SLinus Torvalds 			unsigned long now;
23081da177e4SLinus Torvalds 			u32 stored_lft;
23091da177e4SLinus Torvalds 
23101da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
23111da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
23121da177e4SLinus Torvalds 			now = jiffies;
23131da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
23141da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
23151da177e4SLinus Torvalds 			else
23161da177e4SLinus Torvalds 				stored_lft = 0;
23178a226b2cSJiri Benc 			if (!update_lft && !create && stored_lft) {
2318c9d55d5bSPaul Marks 				const u32 minimum_lft = min(
2319c9d55d5bSPaul Marks 					stored_lft, (u32)MIN_VALID_LIFETIME);
2320c9d55d5bSPaul Marks 				valid_lft = max(valid_lft, minimum_lft);
2321c9d55d5bSPaul Marks 
2322c9d55d5bSPaul Marks 				/* RFC4862 Section 5.5.3e:
2323c9d55d5bSPaul Marks 				 * "Note that the preferred lifetime of the
2324c9d55d5bSPaul Marks 				 *  corresponding address is always reset to
2325c9d55d5bSPaul Marks 				 *  the Preferred Lifetime in the received
2326c9d55d5bSPaul Marks 				 *  Prefix Information option, regardless of
2327c9d55d5bSPaul Marks 				 *  whether the valid lifetime is also reset or
2328a1ed0526SBrian Haley 				 *  ignored."
2329a1ed0526SBrian Haley 				 *
2330c9d55d5bSPaul Marks 				 * So we should always update prefered_lft here.
2331a1ed0526SBrian Haley 				 */
2332a1ed0526SBrian Haley 				update_lft = 1;
2333a1ed0526SBrian Haley 			}
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds 			if (update_lft) {
23361da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
23371da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
23381da177e4SLinus Torvalds 				ifp->tstamp = now;
23391da177e4SLinus Torvalds 				flags = ifp->flags;
23401da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
23411da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
23441da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
23451da177e4SLinus Torvalds 			} else
23461da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
23471da177e4SLinus Torvalds 
234853bd6749SJiri Pirko 			manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
234953bd6749SJiri Pirko 					 create, now);
23505d9efa7eSDavid S. Miller 
23511da177e4SLinus Torvalds 			in6_ifa_put(ifp);
2352c15b1ccaSHannes Frederic Sowa 			addrconf_verify();
23531da177e4SLinus Torvalds 		}
23541da177e4SLinus Torvalds 	}
23551da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
23561da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
23571da177e4SLinus Torvalds }
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds /*
23601da177e4SLinus Torvalds  *	Set destination address.
23611da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
23621da177e4SLinus Torvalds  *	device.
23631da177e4SLinus Torvalds  */
2364af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
23651da177e4SLinus Torvalds {
23661da177e4SLinus Torvalds 	struct in6_ifreq ireq;
23671da177e4SLinus Torvalds 	struct net_device *dev;
23681da177e4SLinus Torvalds 	int err = -EINVAL;
23691da177e4SLinus Torvalds 
23701da177e4SLinus Torvalds 	rtnl_lock();
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	err = -EFAULT;
23731da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
23741da177e4SLinus Torvalds 		goto err_exit;
23751da177e4SLinus Torvalds 
2376af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds 	err = -ENODEV;
23791da177e4SLinus Torvalds 	if (dev == NULL)
23801da177e4SLinus Torvalds 		goto err_exit;
23811da177e4SLinus Torvalds 
238207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
23831da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
23845bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
23851da177e4SLinus Torvalds 		struct ifreq ifr;
23861da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
23871da177e4SLinus Torvalds 
23881da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
23891da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
23901da177e4SLinus Torvalds 			goto err_exit;
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
23931da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
23941da177e4SLinus Torvalds 		p.iph.saddr = 0;
23951da177e4SLinus Torvalds 		p.iph.version = 4;
23961da177e4SLinus Torvalds 		p.iph.ihl = 5;
23971da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
23981da177e4SLinus Torvalds 		p.iph.ttl = 64;
2399d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
24001da177e4SLinus Torvalds 
24015bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
24025bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
24035bc3eb7eSStephen Hemminger 
24045bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
24055bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
24061da177e4SLinus Torvalds 			set_fs(oldfs);
24075bc3eb7eSStephen Hemminger 		} else
24085bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds 		if (err == 0) {
24111da177e4SLinus Torvalds 			err = -ENOBUFS;
2412af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2413af284937SDaniel Lezcano 			if (!dev)
24141da177e4SLinus Torvalds 				goto err_exit;
24151da177e4SLinus Torvalds 			err = dev_open(dev);
24161da177e4SLinus Torvalds 		}
24171da177e4SLinus Torvalds 	}
24180be669bbSJoerg Roedel #endif
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds err_exit:
24211da177e4SLinus Torvalds 	rtnl_unlock();
24221da177e4SLinus Torvalds 	return err;
24231da177e4SLinus Torvalds }
24241da177e4SLinus Torvalds 
24251da177e4SLinus Torvalds /*
24261da177e4SLinus Torvalds  *	Manual configuration of address on an interface
24271da177e4SLinus Torvalds  */
2428479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex,
2429479840ffSJiri Pirko 			  const struct in6_addr *pfx,
2430caeaba79SNicolas Dichtel 			  const struct in6_addr *peer_pfx,
2431479840ffSJiri Pirko 			  unsigned int plen, __u32 ifa_flags,
2432479840ffSJiri Pirko 			  __u32 prefered_lft, __u32 valid_lft)
24331da177e4SLinus Torvalds {
24341da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
24351da177e4SLinus Torvalds 	struct inet6_dev *idev;
24361da177e4SLinus Torvalds 	struct net_device *dev;
24371da177e4SLinus Torvalds 	int scope;
24386f704992SYOSHIFUJI Hideaki 	u32 flags;
24396f704992SYOSHIFUJI Hideaki 	clock_t expires;
24404bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds 	ASSERT_RTNL();
24431da177e4SLinus Torvalds 
244424ef0da7SThomas Graf 	if (plen > 128)
244524ef0da7SThomas Graf 		return -EINVAL;
244624ef0da7SThomas Graf 
24470778769dSNoriaki TAKAMIYA 	/* check the lifetime */
24480778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
24490778769dSNoriaki TAKAMIYA 		return -EINVAL;
24500778769dSNoriaki TAKAMIYA 
245153bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
245253bd6749SJiri Pirko 		return -EINVAL;
245353bd6749SJiri Pirko 
2454af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2455af284937SDaniel Lezcano 	if (!dev)
24561da177e4SLinus Torvalds 		return -ENODEV;
24571da177e4SLinus Torvalds 
245864e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
245964e724f6SBrian Haley 	if (IS_ERR(idev))
246064e724f6SBrian Haley 		return PTR_ERR(idev);
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
24631da177e4SLinus Torvalds 
24644bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
24654bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
24664bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
24674bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
24686f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
24694bed72e4SYOSHIFUJI Hideaki 	} else {
24704bed72e4SYOSHIFUJI Hideaki 		expires = 0;
24714bed72e4SYOSHIFUJI Hideaki 		flags = 0;
24724bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
24736f704992SYOSHIFUJI Hideaki 	}
24740778769dSNoriaki TAKAMIYA 
24754bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
24764bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
24774bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
24780778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
24794bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
24804bed72e4SYOSHIFUJI Hideaki 	}
24810778769dSNoriaki TAKAMIYA 
24828a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
24838a226b2cSJiri Benc 			    valid_lft, prefered_lft);
24840778769dSNoriaki TAKAMIYA 
24851da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
2486761aac73SThomas Haller 		if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
248746d48046SYOSHIFUJI Hideaki 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
24886f704992SYOSHIFUJI Hideaki 					      expires, flags);
2489761aac73SThomas Haller 		}
2490761aac73SThomas Haller 
249195c385b4SNeil Horman 		/*
249295c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
249395c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
249495c385b4SNeil Horman 		 * manually configured addresses
249595c385b4SNeil Horman 		 */
2496cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
249753bd6749SJiri Pirko 		if (ifa_flags & IFA_F_MANAGETEMPADDR)
249853bd6749SJiri Pirko 			manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
249953bd6749SJiri Pirko 					 true, jiffies);
25001da177e4SLinus Torvalds 		in6_ifa_put(ifp);
2501c15b1ccaSHannes Frederic Sowa 		addrconf_verify_rtnl();
25021da177e4SLinus Torvalds 		return 0;
25031da177e4SLinus Torvalds 	}
25041da177e4SLinus Torvalds 
25051da177e4SLinus Torvalds 	return PTR_ERR(ifp);
25061da177e4SLinus Torvalds }
25071da177e4SLinus Torvalds 
25086046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
25096046d5b4SHeiner Kallweit 			  const struct in6_addr *pfx, unsigned int plen)
25101da177e4SLinus Torvalds {
25111da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
25121da177e4SLinus Torvalds 	struct inet6_dev *idev;
25131da177e4SLinus Torvalds 	struct net_device *dev;
25141da177e4SLinus Torvalds 
251524ef0da7SThomas Graf 	if (plen > 128)
251624ef0da7SThomas Graf 		return -EINVAL;
251724ef0da7SThomas Graf 
2518af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2519af284937SDaniel Lezcano 	if (!dev)
25201da177e4SLinus Torvalds 		return -ENODEV;
25211da177e4SLinus Torvalds 
25227eaa48a4SDavid S. Miller 	if ((idev = __in6_dev_get(dev)) == NULL)
25231da177e4SLinus Torvalds 		return -ENXIO;
25241da177e4SLinus Torvalds 
25251da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2526502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
25271da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
25281da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
25291da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
25301da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
25311da177e4SLinus Torvalds 
25326046d5b4SHeiner Kallweit 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
25336046d5b4SHeiner Kallweit 			    (ifa_flags & IFA_F_MANAGETEMPADDR))
25346046d5b4SHeiner Kallweit 				manage_tempaddrs(idev, ifp, 0, 0, false,
25356046d5b4SHeiner Kallweit 						 jiffies);
25361da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
25376046d5b4SHeiner Kallweit 			addrconf_verify_rtnl();
25381da177e4SLinus Torvalds 			return 0;
25391da177e4SLinus Torvalds 		}
25401da177e4SLinus Torvalds 	}
25411da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
25421da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
25431da177e4SLinus Torvalds }
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 
2546af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
25471da177e4SLinus Torvalds {
25481da177e4SLinus Torvalds 	struct in6_ifreq ireq;
25491da177e4SLinus Torvalds 	int err;
25501da177e4SLinus Torvalds 
2551af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
25521da177e4SLinus Torvalds 		return -EPERM;
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
25551da177e4SLinus Torvalds 		return -EFAULT;
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds 	rtnl_lock();
2558caeaba79SNicolas Dichtel 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2559af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2560af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
25611da177e4SLinus Torvalds 	rtnl_unlock();
25621da177e4SLinus Torvalds 	return err;
25631da177e4SLinus Torvalds }
25641da177e4SLinus Torvalds 
2565af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
25661da177e4SLinus Torvalds {
25671da177e4SLinus Torvalds 	struct in6_ifreq ireq;
25681da177e4SLinus Torvalds 	int err;
25691da177e4SLinus Torvalds 
2570af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
25711da177e4SLinus Torvalds 		return -EPERM;
25721da177e4SLinus Torvalds 
25731da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
25741da177e4SLinus Torvalds 		return -EFAULT;
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 	rtnl_lock();
25776046d5b4SHeiner Kallweit 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2578af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
25791da177e4SLinus Torvalds 	rtnl_unlock();
25801da177e4SLinus Torvalds 	return err;
25811da177e4SLinus Torvalds }
25821da177e4SLinus Torvalds 
2583b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2584b5f348e5SIlpo Järvinen 		     int plen, int scope)
2585b5f348e5SIlpo Järvinen {
2586b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
2587b5f348e5SIlpo Järvinen 
25888a226b2cSJiri Benc 	ifp = ipv6_add_addr(idev, addr, NULL, plen,
258907edd741SHannes Frederic Sowa 			    scope, IFA_F_PERMANENT,
259007edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2591b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
2592b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
2593b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
2594b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
2595b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2596b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
2597b5f348e5SIlpo Järvinen 	}
2598b5f348e5SIlpo Järvinen }
2599b5f348e5SIlpo Järvinen 
260007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
26011da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
26021da177e4SLinus Torvalds {
26031da177e4SLinus Torvalds 	struct in6_addr addr;
26041da177e4SLinus Torvalds 	struct net_device *dev;
2605c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
2606929c9cf3SNicolas Dichtel 	int scope, plen;
2607f0e2acfaSNicolas Dichtel 	u32 pflags = 0;
26081da177e4SLinus Torvalds 
26091da177e4SLinus Torvalds 	ASSERT_RTNL();
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
26121da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
26151da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
26161da177e4SLinus Torvalds 		scope = IFA_LINK;
2617929c9cf3SNicolas Dichtel 		plen = 64;
26181da177e4SLinus Torvalds 	} else {
26191da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
2620929c9cf3SNicolas Dichtel 		plen = 96;
2621f0e2acfaSNicolas Dichtel 		pflags |= RTF_NONEXTHOP;
26221da177e4SLinus Torvalds 	}
26231da177e4SLinus Torvalds 
26241da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
2625929c9cf3SNicolas Dichtel 		add_addr(idev, &addr, plen, scope);
2626f0e2acfaSNicolas Dichtel 		addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
26271da177e4SLinus Torvalds 		return;
26281da177e4SLinus Torvalds 	}
26291da177e4SLinus Torvalds 
26306fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
2631e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
26321da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
26331da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
26341da177e4SLinus Torvalds 
26351da177e4SLinus Torvalds 			int flag = scope;
26361da177e4SLinus Torvalds 
26371da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
26381da177e4SLinus Torvalds 
26391da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
26421da177e4SLinus Torvalds 					continue;
26431da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
26441da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
26451da177e4SLinus Torvalds 						continue;
26461da177e4SLinus Torvalds 					flag |= IFA_HOST;
26471da177e4SLinus Torvalds 				}
26481da177e4SLinus Torvalds 
2649b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
2650f0e2acfaSNicolas Dichtel 				addrconf_prefix_route(&addr, plen, idev->dev, 0,
2651f0e2acfaSNicolas Dichtel 						      pflags);
26521da177e4SLinus Torvalds 			}
26531da177e4SLinus Torvalds 		}
26541da177e4SLinus Torvalds 	}
26551da177e4SLinus Torvalds }
26560be669bbSJoerg Roedel #endif
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
26591da177e4SLinus Torvalds {
26601da177e4SLinus Torvalds 	struct inet6_dev  *idev;
266125fb6ca4SBalakumaran Kannan 	struct net_device *sp_dev;
266225fb6ca4SBalakumaran Kannan 	struct inet6_ifaddr *sp_ifa;
266325fb6ca4SBalakumaran Kannan 	struct rt6_info *sp_rt;
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds 	/* ::1 */
26661da177e4SLinus Torvalds 
26671da177e4SLinus Torvalds 	ASSERT_RTNL();
26681da177e4SLinus Torvalds 
26697eaa48a4SDavid S. Miller 	if ((idev = ipv6_find_idev(dev)) == NULL) {
267091df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
26711da177e4SLinus Torvalds 		return;
26721da177e4SLinus Torvalds 	}
26731da177e4SLinus Torvalds 
2674b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
267525fb6ca4SBalakumaran Kannan 
267625fb6ca4SBalakumaran Kannan 	/* Add routes to other interface's IPv6 addresses */
267725fb6ca4SBalakumaran Kannan 	for_each_netdev(dev_net(dev), sp_dev) {
267825fb6ca4SBalakumaran Kannan 		if (!strcmp(sp_dev->name, dev->name))
267925fb6ca4SBalakumaran Kannan 			continue;
268025fb6ca4SBalakumaran Kannan 
268125fb6ca4SBalakumaran Kannan 		idev = __in6_dev_get(sp_dev);
268225fb6ca4SBalakumaran Kannan 		if (!idev)
268325fb6ca4SBalakumaran Kannan 			continue;
268425fb6ca4SBalakumaran Kannan 
268525fb6ca4SBalakumaran Kannan 		read_lock_bh(&idev->lock);
268625fb6ca4SBalakumaran Kannan 		list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
268725fb6ca4SBalakumaran Kannan 
268825fb6ca4SBalakumaran Kannan 			if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
268925fb6ca4SBalakumaran Kannan 				continue;
269025fb6ca4SBalakumaran Kannan 
269133d99113SGao feng 			if (sp_ifa->rt) {
269233d99113SGao feng 				/* This dst has been added to garbage list when
269333d99113SGao feng 				 * lo device down, release this obsolete dst and
269433d99113SGao feng 				 * reallocate a new router for ifa.
269533d99113SGao feng 				 */
269633d99113SGao feng 				if (sp_ifa->rt->dst.obsolete > 0) {
269733d99113SGao feng 					ip6_rt_put(sp_ifa->rt);
269833d99113SGao feng 					sp_ifa->rt = NULL;
269933d99113SGao feng 				} else {
2700a881ae1fSGao feng 					continue;
270133d99113SGao feng 				}
270233d99113SGao feng 			}
2703a881ae1fSGao feng 
270457ec0afeSFrançois-Xavier Le Bail 			sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
270525fb6ca4SBalakumaran Kannan 
270625fb6ca4SBalakumaran Kannan 			/* Failure cases are ignored */
2707534c8779SGao feng 			if (!IS_ERR(sp_rt)) {
2708534c8779SGao feng 				sp_ifa->rt = sp_rt;
270925fb6ca4SBalakumaran Kannan 				ip6_ins_rt(sp_rt);
271025fb6ca4SBalakumaran Kannan 			}
2711534c8779SGao feng 		}
271225fb6ca4SBalakumaran Kannan 		read_unlock_bh(&idev->lock);
271325fb6ca4SBalakumaran Kannan 	}
27141da177e4SLinus Torvalds }
27151da177e4SLinus Torvalds 
2716b71d1d42SEric Dumazet static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
27171da177e4SLinus Torvalds {
27181da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
271995c385b4SNeil Horman 	u32 addr_flags = IFA_F_PERMANENT;
27201da177e4SLinus Torvalds 
272195c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
272295c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
2723702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
272495c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
272595c385b4SNeil Horman #endif
272695c385b4SNeil Horman 
272795c385b4SNeil Horman 
272807edd741SHannes Frederic Sowa 	ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
272907edd741SHannes Frederic Sowa 			    INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
27301da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
273146d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2732cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
27331da177e4SLinus Torvalds 		in6_ifa_put(ifp);
27341da177e4SLinus Torvalds 	}
27351da177e4SLinus Torvalds }
27361da177e4SLinus Torvalds 
2737bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
2738bc91b0f0SJiri Pirko {
2739bc91b0f0SJiri Pirko 	if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) {
2740bc91b0f0SJiri Pirko 		struct in6_addr addr;
2741bc91b0f0SJiri Pirko 
2742bc91b0f0SJiri Pirko 		ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2743bc91b0f0SJiri Pirko 		/* addrconf_add_linklocal also adds a prefix_route and we
2744bc91b0f0SJiri Pirko 		 * only need to care about prefix routes if ipv6_generate_eui64
2745bc91b0f0SJiri Pirko 		 * couldn't generate one.
2746bc91b0f0SJiri Pirko 		 */
2747bc91b0f0SJiri Pirko 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
2748bc91b0f0SJiri Pirko 			addrconf_add_linklocal(idev, &addr);
2749bc91b0f0SJiri Pirko 		else if (prefix_route)
2750bc91b0f0SJiri Pirko 			addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
2751bc91b0f0SJiri Pirko 	}
2752bc91b0f0SJiri Pirko }
2753bc91b0f0SJiri Pirko 
27541da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
27551da177e4SLinus Torvalds {
27561da177e4SLinus Torvalds 	struct inet6_dev *idev;
27571da177e4SLinus Torvalds 
27581da177e4SLinus Torvalds 	ASSERT_RTNL();
27591da177e4SLinus Torvalds 
276074235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
276174235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
276274235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
276306a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
2764cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	    (dev->type != ARPHRD_IEEE802154) &&
2765e837735eSNicolas Dichtel 	    (dev->type != ARPHRD_IEEE1394) &&
2766e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_TUNNEL6) &&
2767e74bccb8SJukka Rissanen 	    (dev->type != ARPHRD_6LOWPAN)) {
276874235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
276974235a25SHerbert Xu 		return;
277074235a25SHerbert Xu 	}
277174235a25SHerbert Xu 
27721da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
277364e724f6SBrian Haley 	if (IS_ERR(idev))
27741da177e4SLinus Torvalds 		return;
27751da177e4SLinus Torvalds 
2776bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, false);
27771da177e4SLinus Torvalds }
27781da177e4SLinus Torvalds 
277907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
27801da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
27811da177e4SLinus Torvalds {
27821da177e4SLinus Torvalds 	struct inet6_dev *idev;
27831da177e4SLinus Torvalds 
27841da177e4SLinus Torvalds 	ASSERT_RTNL();
27851da177e4SLinus Torvalds 
27861da177e4SLinus Torvalds 	/*
27871da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
27881da177e4SLinus Torvalds 	 * addresses... we should configure all of
27891da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
27901da177e4SLinus Torvalds 	 */
27911da177e4SLinus Torvalds 
27927eaa48a4SDavid S. Miller 	if ((idev = ipv6_find_idev(dev)) == NULL) {
279391df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
27941da177e4SLinus Torvalds 		return;
27951da177e4SLinus Torvalds 	}
27961da177e4SLinus Torvalds 
2797c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
2798bc91b0f0SJiri Pirko 		addrconf_addr_gen(idev, false);
2799c7dc89c0SFred L. Templin 		return;
2800c7dc89c0SFred L. Templin 	}
2801c7dc89c0SFred L. Templin 
28021da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
28031da177e4SLinus Torvalds 
280462b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
28051da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
28061da177e4SLinus Torvalds }
28070be669bbSJoerg Roedel #endif
28081da177e4SLinus Torvalds 
280907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
2810aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
2811aee80b54Sstephen hemminger {
2812aee80b54Sstephen hemminger 	struct inet6_dev *idev;
2813aee80b54Sstephen hemminger 
2814aee80b54Sstephen hemminger 	ASSERT_RTNL();
2815aee80b54Sstephen hemminger 
28167eaa48a4SDavid S. Miller 	if ((idev = ipv6_find_idev(dev)) == NULL) {
281791df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
2818aee80b54Sstephen hemminger 		return;
2819aee80b54Sstephen hemminger 	}
2820aee80b54Sstephen hemminger 
2821bc91b0f0SJiri Pirko 	addrconf_addr_gen(idev, true);
2822aee80b54Sstephen hemminger }
2823aee80b54Sstephen hemminger #endif
2824aee80b54Sstephen hemminger 
28251da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
2826351638e7SJiri Pirko 			   void *ptr)
28271da177e4SLinus Torvalds {
2828351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2829748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
2830c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
2831b217d616SHerbert Xu 	int err;
28321da177e4SLinus Torvalds 
28331da177e4SLinus Torvalds 	switch (event) {
283445ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
283574235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
283645ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
2837a317a2f1SWANG Cong 			if (IS_ERR(idev))
2838a317a2f1SWANG Cong 				return notifier_from_errno(PTR_ERR(idev));
283945ba9dd2SYOSHIFUJI Hideaki 		}
284045ba9dd2SYOSHIFUJI Hideaki 		break;
2841bcdd553fSStephen Hemminger 
28421da177e4SLinus Torvalds 	case NETDEV_UP:
28433c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
2844c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
2845c2edacf8SJay Vosburgh 			break;
2846c2edacf8SJay Vosburgh 
28473c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
2848f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
28493c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
2850f3213831SJoe Perches 				pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
28513c21edbdSYOSHIFUJI Hideaki 					dev->name);
28523c21edbdSYOSHIFUJI Hideaki 				break;
28533c21edbdSYOSHIFUJI Hideaki 			}
285499081049SKristian Slavov 
2855d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
2856d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
2857d31c7b8fSEvgeniy Polyakov 
2858a317a2f1SWANG Cong 			if (!IS_ERR_OR_NULL(idev)) {
285999081049SKristian Slavov 				idev->if_flags |= IF_READY;
2860e3ec6cfcSBenjamin Thery 				run_pending = 1;
2861e3ec6cfcSBenjamin Thery 			}
28623c21edbdSYOSHIFUJI Hideaki 		} else {
2863f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
28643c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
28653c21edbdSYOSHIFUJI Hideaki 				break;
28663c21edbdSYOSHIFUJI Hideaki 			}
28673c21edbdSYOSHIFUJI Hideaki 
28683c21edbdSYOSHIFUJI Hideaki 			if (idev) {
2869bcdd553fSStephen Hemminger 				if (idev->if_flags & IF_READY)
28703c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
28713c21edbdSYOSHIFUJI Hideaki 					break;
28723c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
28733c21edbdSYOSHIFUJI Hideaki 			}
28743c21edbdSYOSHIFUJI Hideaki 
2875f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
28763c21edbdSYOSHIFUJI Hideaki 				dev->name);
28773c21edbdSYOSHIFUJI Hideaki 
2878c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
28793c21edbdSYOSHIFUJI Hideaki 		}
28803c21edbdSYOSHIFUJI Hideaki 
28811da177e4SLinus Torvalds 		switch (dev->type) {
288207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
28831da177e4SLinus Torvalds 		case ARPHRD_SIT:
28841da177e4SLinus Torvalds 			addrconf_sit_config(dev);
28851da177e4SLinus Torvalds 			break;
28860be669bbSJoerg Roedel #endif
288707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
2888aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
2889aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
2890aee80b54Sstephen hemminger 			break;
2891aee80b54Sstephen hemminger #endif
28921da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
28931da177e4SLinus Torvalds 			init_loopback(dev);
28941da177e4SLinus Torvalds 			break;
28951da177e4SLinus Torvalds 
28961da177e4SLinus Torvalds 		default:
28971da177e4SLinus Torvalds 			addrconf_dev_config(dev);
28981da177e4SLinus Torvalds 			break;
28993ff50b79SStephen Hemminger 		}
2900bcdd553fSStephen Hemminger 
2901a317a2f1SWANG Cong 		if (!IS_ERR_OR_NULL(idev)) {
2902c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
2903c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
2904c5e33bddSYOSHIFUJI Hideaki 
2905bcdd553fSStephen Hemminger 			/*
2906bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
2907bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
2908bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
29091da177e4SLinus Torvalds 			 */
2910bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
2911bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
29121da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
29131da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
29141da177e4SLinus Torvalds 			}
29151da177e4SLinus Torvalds 			idev->tstamp = jiffies;
29161da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
2917bcdd553fSStephen Hemminger 
2918bcdd553fSStephen Hemminger 			/*
2919bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
2920bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
29211da177e4SLinus Torvalds 			 */
29221da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
2923bcdd553fSStephen Hemminger 				addrconf_ifdown(dev, 1);
29241da177e4SLinus Torvalds 		}
29251da177e4SLinus Torvalds 		break;
29261da177e4SLinus Torvalds 
29271da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
29281da177e4SLinus Torvalds 		if (idev && dev->mtu >= IPV6_MIN_MTU) {
29291da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
29301da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
29311da177e4SLinus Torvalds 			break;
29321da177e4SLinus Torvalds 		}
29331da177e4SLinus Torvalds 
2934d31c7b8fSEvgeniy Polyakov 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2935d31c7b8fSEvgeniy Polyakov 			idev = ipv6_add_dev(dev);
2936a317a2f1SWANG Cong 			if (!IS_ERR(idev))
2937d31c7b8fSEvgeniy Polyakov 				break;
2938d31c7b8fSEvgeniy Polyakov 		}
2939d31c7b8fSEvgeniy Polyakov 
2940bcdd553fSStephen Hemminger 		/*
2941db9c7c39Sstephen hemminger 		 * if MTU under IPV6_MIN_MTU.
2942bcdd553fSStephen Hemminger 		 * Stop IPv6 on this interface.
2943bcdd553fSStephen Hemminger 		 */
29441da177e4SLinus Torvalds 
29451da177e4SLinus Torvalds 	case NETDEV_DOWN:
29461da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
29471da177e4SLinus Torvalds 		/*
29481da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
29491da177e4SLinus Torvalds 		 */
29501da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
29511da177e4SLinus Torvalds 		break;
29523c21edbdSYOSHIFUJI Hideaki 
29531da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
29541da177e4SLinus Torvalds 		if (idev) {
29555632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
2956408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
2957a317a2f1SWANG Cong 			err = addrconf_sysctl_register(idev);
2958b217d616SHerbert Xu 			if (err)
2959b217d616SHerbert Xu 				return notifier_from_errno(err);
2960a317a2f1SWANG Cong 			err = snmp6_register_dev(idev);
2961a317a2f1SWANG Cong 			if (err) {
2962a317a2f1SWANG Cong 				addrconf_sysctl_unregister(idev);
2963a317a2f1SWANG Cong 				return notifier_from_errno(err);
2964a317a2f1SWANG Cong 			}
29655632c515SStephen Hemminger 		}
29661da177e4SLinus Torvalds 		break;
2967bcdd553fSStephen Hemminger 
296893d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
296993d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
297093d9b7d7SJiri Pirko 		addrconf_type_change(dev, event);
297175c78500SMoni Shoua 		break;
29723ff50b79SStephen Hemminger 	}
29731da177e4SLinus Torvalds 
29741da177e4SLinus Torvalds 	return NOTIFY_OK;
29751da177e4SLinus Torvalds }
29761da177e4SLinus Torvalds 
29771da177e4SLinus Torvalds /*
29781da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
29791da177e4SLinus Torvalds  */
29801da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
29811da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
29821da177e4SLinus Torvalds };
29831da177e4SLinus Torvalds 
298493d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
298575c78500SMoni Shoua {
298675c78500SMoni Shoua 	struct inet6_dev *idev;
298775c78500SMoni Shoua 	ASSERT_RTNL();
298875c78500SMoni Shoua 
298975c78500SMoni Shoua 	idev = __in6_dev_get(dev);
299075c78500SMoni Shoua 
299193d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
299275c78500SMoni Shoua 		ipv6_mc_remap(idev);
299393d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
299475c78500SMoni Shoua 		ipv6_mc_unmap(idev);
299575c78500SMoni Shoua }
299675c78500SMoni Shoua 
29971da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
29981da177e4SLinus Torvalds {
2999c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
3000502a2ffdSstephen hemminger 	struct inet6_dev *idev;
3001502a2ffdSstephen hemminger 	struct inet6_ifaddr *ifa;
300273a8bd74SDavid S. Miller 	int state, i;
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds 	ASSERT_RTNL();
30051da177e4SLinus Torvalds 
3006f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
300773a8bd74SDavid S. Miller 	neigh_ifdown(&nd_tbl, dev);
30081da177e4SLinus Torvalds 
30091da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
30101da177e4SLinus Torvalds 	if (idev == NULL)
30111da177e4SLinus Torvalds 		return -ENODEV;
30121da177e4SLinus Torvalds 
3013bcdd553fSStephen Hemminger 	/*
3014bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3015bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
30161da177e4SLinus Torvalds 	 */
3017439e2385SDenis V. Lunev 	if (how) {
30181da177e4SLinus Torvalds 		idev->dead = 1;
30198814c4b5SYOSHIFUJI Hideaki 
30208814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3021a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
30221da177e4SLinus Torvalds 
30231da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
30241da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
30251da177e4SLinus Torvalds 
30261da177e4SLinus Torvalds 	}
30271da177e4SLinus Torvalds 
302873a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
302973a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
303073a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
303173a8bd74SDavid S. Miller 
303273a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
303373a8bd74SDavid S. Miller 	restart:
3034b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
303573a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
303673a8bd74SDavid S. Miller 				hlist_del_init_rcu(&ifa->addr_lst);
3037c15b1ccaSHannes Frederic Sowa 				addrconf_del_dad_work(ifa);
303873a8bd74SDavid S. Miller 				goto restart;
303973a8bd74SDavid S. Miller 			}
304073a8bd74SDavid S. Miller 		}
304173a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
304273a8bd74SDavid S. Miller 	}
304373a8bd74SDavid S. Miller 
30441da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
30451da177e4SLinus Torvalds 
3046b7b1bfceSHannes Frederic Sowa 	addrconf_del_rs_timer(idev);
3047b7b1bfceSHannes Frederic Sowa 
3048bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3049439e2385SDenis V. Lunev 	if (!how)
30503c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
30511da177e4SLinus Torvalds 
3052439e2385SDenis V. Lunev 	if (how && del_timer(&idev->regen_timer))
30531da177e4SLinus Torvalds 		in6_dev_put(idev);
30541da177e4SLinus Torvalds 
3055bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3056372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3057372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3058372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3059372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
30601da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
30611da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
30621da177e4SLinus Torvalds 
30631da177e4SLinus Torvalds 		if (ifa->ifpub) {
30641da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
30651da177e4SLinus Torvalds 			ifa->ifpub = NULL;
30661da177e4SLinus Torvalds 		}
30671da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
30681da177e4SLinus Torvalds 		in6_ifa_put(ifa);
30691da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
30701da177e4SLinus Torvalds 	}
30718f37ada5Sstephen hemminger 
3072502a2ffdSstephen hemminger 	while (!list_empty(&idev->addr_list)) {
3073502a2ffdSstephen hemminger 		ifa = list_first_entry(&idev->addr_list,
3074502a2ffdSstephen hemminger 				       struct inet6_ifaddr, if_list);
3075c15b1ccaSHannes Frederic Sowa 		addrconf_del_dad_work(ifa);
307684e8b803Sstephen hemminger 
3077502a2ffdSstephen hemminger 		list_del(&ifa->if_list);
30781da177e4SLinus Torvalds 
30794c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
308073a8bd74SDavid S. Miller 
30814c5ff6a6SHerbert Xu 		spin_lock_bh(&ifa->state_lock);
30824c5ff6a6SHerbert Xu 		state = ifa->state;
30834c5ff6a6SHerbert Xu 		ifa->state = INET6_IFADDR_STATE_DEAD;
30844c5ff6a6SHerbert Xu 		spin_unlock_bh(&ifa->state_lock);
30854c5ff6a6SHerbert Xu 
30869d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
30871da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
3088f88c91ddSCong Wang 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
30892de79570SLorenzo Colitti 		}
30909d82ca98SJohn Fastabend 		in6_ifa_put(ifa);
309173a8bd74SDavid S. Miller 
30921da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
30931da177e4SLinus Torvalds 	}
30948f37ada5Sstephen hemminger 
30951da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
30961da177e4SLinus Torvalds 
30971da177e4SLinus Torvalds 	/* Step 5: Discard multicast list */
3098439e2385SDenis V. Lunev 	if (how)
30991da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
31001da177e4SLinus Torvalds 	else
31011da177e4SLinus Torvalds 		ipv6_mc_down(idev);
31021da177e4SLinus Torvalds 
31031da177e4SLinus Torvalds 	idev->tstamp = jiffies;
31041da177e4SLinus Torvalds 
3105bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3106439e2385SDenis V. Lunev 	if (how) {
3107408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
31081da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
31091da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
31101da177e4SLinus Torvalds 		in6_dev_put(idev);
31111da177e4SLinus Torvalds 	}
31121da177e4SLinus Torvalds 	return 0;
31131da177e4SLinus Torvalds }
31141da177e4SLinus Torvalds 
31151da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
31161da177e4SLinus Torvalds {
3117b7b1bfceSHannes Frederic Sowa 	struct inet6_dev *idev = (struct inet6_dev *)data;
3118caf92bc4SCong Wang 	struct net_device *dev = idev->dev;
3119b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
31201da177e4SLinus Torvalds 
3121b7b1bfceSHannes Frederic Sowa 	write_lock(&idev->lock);
31225b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
31231da177e4SLinus Torvalds 		goto out;
31241da177e4SLinus Torvalds 
31259ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
31261da177e4SLinus Torvalds 		goto out;
31275b2a1953Sstephen hemminger 
31285b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
31295b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
31305b2a1953Sstephen hemminger 		goto out;
31311da177e4SLinus Torvalds 
3132b7b1bfceSHannes Frederic Sowa 	if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
3133caf92bc4SCong Wang 		write_unlock(&idev->lock);
3134caf92bc4SCong Wang 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3135caf92bc4SCong Wang 			ndisc_send_rs(dev, &lladdr,
3136b7b1bfceSHannes Frederic Sowa 				      &in6addr_linklocal_allrouters);
3137b7b1bfceSHannes Frederic Sowa 		else
3138caf92bc4SCong Wang 			goto put;
3139b7b1bfceSHannes Frederic Sowa 
3140caf92bc4SCong Wang 		write_lock(&idev->lock);
31411da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
3142b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3143b7b1bfceSHannes Frederic Sowa 					     idev->cnf.rtr_solicits) ?
31445b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_delay :
31455b2a1953Sstephen hemminger 				      idev->cnf.rtr_solicit_interval);
31461da177e4SLinus Torvalds 	} else {
31471da177e4SLinus Torvalds 		/*
31481da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
31491da177e4SLinus Torvalds 		 * assumption any longer.
31501da177e4SLinus Torvalds 		 */
315191df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
31521da177e4SLinus Torvalds 	}
31531da177e4SLinus Torvalds 
31541da177e4SLinus Torvalds out:
3155b7b1bfceSHannes Frederic Sowa 	write_unlock(&idev->lock);
3156caf92bc4SCong Wang put:
3157b7b1bfceSHannes Frederic Sowa 	in6_dev_put(idev);
31581da177e4SLinus Torvalds }
31591da177e4SLinus Torvalds 
31601da177e4SLinus Torvalds /*
31611da177e4SLinus Torvalds  *	Duplicate Address Detection
31621da177e4SLinus Torvalds  */
31633c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
31643c21edbdSYOSHIFUJI Hideaki {
31653c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
31663c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
31673c21edbdSYOSHIFUJI Hideaki 
316895c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
316995c385b4SNeil Horman 		rand_num = 0;
317095c385b4SNeil Horman 	else
317163862b5bSAruna-Hewapathirane 		rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
317295c385b4SNeil Horman 
3173b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes = idev->cnf.dad_transmits;
3174c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp, rand_num);
31753c21edbdSYOSHIFUJI Hideaki }
31763c21edbdSYOSHIFUJI Hideaki 
3177c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
31781da177e4SLinus Torvalds {
31791da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
31801da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
31811da177e4SLinus Torvalds 
31821da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
31831da177e4SLinus Torvalds 
318463862b5bSAruna-Hewapathirane 	prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
31851da177e4SLinus Torvalds 
31861da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3187622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3188e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
31891da177e4SLinus Torvalds 		goto out;
31901da177e4SLinus Torvalds 
31911da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
31921b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
319355ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
319455ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3195cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
319621809fafSstephen hemminger 		spin_unlock(&ifp->lock);
31971da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
31981da177e4SLinus Torvalds 
31991da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
32001da177e4SLinus Torvalds 		return;
32011da177e4SLinus Torvalds 	}
32021da177e4SLinus Torvalds 
32036732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
320421809fafSstephen hemminger 		spin_unlock(&ifp->lock);
32056732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
32063c21edbdSYOSHIFUJI Hideaki 		/*
3207590a9887SMasatake YAMATO 		 * If the device is not ready:
32083c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
32093c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
32103c21edbdSYOSHIFUJI Hideaki 		 */
32113c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
3212cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
32136732badeSYOSHIFUJI Hideaki 		return;
32143c21edbdSYOSHIFUJI Hideaki 	}
321595c385b4SNeil Horman 
321695c385b4SNeil Horman 	/*
321795c385b4SNeil Horman 	 * Optimistic nodes can start receiving
321895c385b4SNeil Horman 	 * Frames right away
321995c385b4SNeil Horman 	 */
322095c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
322195c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
322295c385b4SNeil Horman 
32236732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
32241da177e4SLinus Torvalds out:
3225622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
32261da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
32271da177e4SLinus Torvalds }
32281da177e4SLinus Torvalds 
3229c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp)
32301da177e4SLinus Torvalds {
3231c15b1ccaSHannes Frederic Sowa 	bool begin_dad = false;
3232c15b1ccaSHannes Frederic Sowa 
3233c15b1ccaSHannes Frederic Sowa 	spin_lock_bh(&ifp->state_lock);
3234c15b1ccaSHannes Frederic Sowa 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3235c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_PREDAD;
3236c15b1ccaSHannes Frederic Sowa 		begin_dad = true;
3237c15b1ccaSHannes Frederic Sowa 	}
3238c15b1ccaSHannes Frederic Sowa 	spin_unlock_bh(&ifp->state_lock);
3239c15b1ccaSHannes Frederic Sowa 
3240c15b1ccaSHannes Frederic Sowa 	if (begin_dad)
3241c15b1ccaSHannes Frederic Sowa 		addrconf_mod_dad_work(ifp, 0);
3242c15b1ccaSHannes Frederic Sowa }
3243c15b1ccaSHannes Frederic Sowa 
3244c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w)
3245c15b1ccaSHannes Frederic Sowa {
3246c15b1ccaSHannes Frederic Sowa 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3247c15b1ccaSHannes Frederic Sowa 						struct inet6_ifaddr,
3248c15b1ccaSHannes Frederic Sowa 						dad_work);
32491da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
32501da177e4SLinus Torvalds 	struct in6_addr mcaddr;
32511da177e4SLinus Torvalds 
3252c15b1ccaSHannes Frederic Sowa 	enum {
3253c15b1ccaSHannes Frederic Sowa 		DAD_PROCESS,
3254c15b1ccaSHannes Frederic Sowa 		DAD_BEGIN,
3255c15b1ccaSHannes Frederic Sowa 		DAD_ABORT,
3256c15b1ccaSHannes Frederic Sowa 	} action = DAD_PROCESS;
3257c15b1ccaSHannes Frederic Sowa 
3258c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
3259c15b1ccaSHannes Frederic Sowa 
3260c15b1ccaSHannes Frederic Sowa 	spin_lock_bh(&ifp->state_lock);
3261c15b1ccaSHannes Frederic Sowa 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3262c15b1ccaSHannes Frederic Sowa 		action = DAD_BEGIN;
3263c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_DAD;
3264c15b1ccaSHannes Frederic Sowa 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3265c15b1ccaSHannes Frederic Sowa 		action = DAD_ABORT;
3266c15b1ccaSHannes Frederic Sowa 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
3267c15b1ccaSHannes Frederic Sowa 	}
3268c15b1ccaSHannes Frederic Sowa 	spin_unlock_bh(&ifp->state_lock);
3269c15b1ccaSHannes Frederic Sowa 
3270c15b1ccaSHannes Frederic Sowa 	if (action == DAD_BEGIN) {
3271c15b1ccaSHannes Frederic Sowa 		addrconf_dad_begin(ifp);
3272c15b1ccaSHannes Frederic Sowa 		goto out;
3273c15b1ccaSHannes Frederic Sowa 	} else if (action == DAD_ABORT) {
3274c15b1ccaSHannes Frederic Sowa 		addrconf_dad_stop(ifp, 1);
3275c15b1ccaSHannes Frederic Sowa 		goto out;
3276c15b1ccaSHannes Frederic Sowa 	}
3277c15b1ccaSHannes Frederic Sowa 
3278b7b1bfceSHannes Frederic Sowa 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
3279f2344a13SHerbert Xu 		goto out;
3280f2344a13SHerbert Xu 
3281c15b1ccaSHannes Frederic Sowa 	write_lock_bh(&idev->lock);
3282122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
3283c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
32841da177e4SLinus Torvalds 		goto out;
32851da177e4SLinus Torvalds 	}
328621809fafSstephen hemminger 
328721809fafSstephen hemminger 	spin_lock(&ifp->lock);
3288622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3289622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
3290c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
3291622ccdf1SHerbert Xu 		goto out;
3292622ccdf1SHerbert Xu 	}
3293622ccdf1SHerbert Xu 
3294b7b1bfceSHannes Frederic Sowa 	if (ifp->dad_probes == 0) {
32951da177e4SLinus Torvalds 		/*
32961da177e4SLinus Torvalds 		 * DAD was successful
32971da177e4SLinus Torvalds 		 */
32981da177e4SLinus Torvalds 
3299cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
330021809fafSstephen hemminger 		spin_unlock(&ifp->lock);
3301c15b1ccaSHannes Frederic Sowa 		write_unlock_bh(&idev->lock);
33021da177e4SLinus Torvalds 
33031da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
33041da177e4SLinus Torvalds 
33051da177e4SLinus Torvalds 		goto out;
33061da177e4SLinus Torvalds 	}
33071da177e4SLinus Torvalds 
3308b7b1bfceSHannes Frederic Sowa 	ifp->dad_probes--;
3309c15b1ccaSHannes Frederic Sowa 	addrconf_mod_dad_work(ifp,
33101f9248e5SJiri Pirko 			      NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
331121809fafSstephen hemminger 	spin_unlock(&ifp->lock);
3312c15b1ccaSHannes Frederic Sowa 	write_unlock_bh(&idev->lock);
33131da177e4SLinus Torvalds 
33141da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
33151da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3316d7aabf22SYOSHIFUJI Hideaki 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
33171da177e4SLinus Torvalds out:
33181da177e4SLinus Torvalds 	in6_ifa_put(ifp);
3319c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
33201da177e4SLinus Torvalds }
33211da177e4SLinus Torvalds 
332211ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */
332311ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
332411ffff75SHannes Frederic Sowa {
332511ffff75SHannes Frederic Sowa 	struct inet6_ifaddr *ifpiter;
332611ffff75SHannes Frederic Sowa 	struct inet6_dev *idev = ifp->idev;
332711ffff75SHannes Frederic Sowa 
3328602582caSHannes Frederic Sowa 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3329602582caSHannes Frederic Sowa 		if (ifpiter->scope > IFA_LINK)
3330602582caSHannes Frederic Sowa 			break;
333111ffff75SHannes Frederic Sowa 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
333211ffff75SHannes Frederic Sowa 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
333311ffff75SHannes Frederic Sowa 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
333411ffff75SHannes Frederic Sowa 		    IFA_F_PERMANENT)
333511ffff75SHannes Frederic Sowa 			return false;
333611ffff75SHannes Frederic Sowa 	}
333711ffff75SHannes Frederic Sowa 	return true;
333811ffff75SHannes Frederic Sowa }
333911ffff75SHannes Frederic Sowa 
33401da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
33411da177e4SLinus Torvalds {
33421da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
3343b7b1bfceSHannes Frederic Sowa 	struct in6_addr lladdr;
3344b173ee48SHannes Frederic Sowa 	bool send_rs, send_mld;
3345b7b1bfceSHannes Frederic Sowa 
3346c15b1ccaSHannes Frederic Sowa 	addrconf_del_dad_work(ifp);
33471da177e4SLinus Torvalds 
33481da177e4SLinus Torvalds 	/*
33491da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
33501da177e4SLinus Torvalds 	 */
33511da177e4SLinus Torvalds 
33521da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
33531da177e4SLinus Torvalds 
3354026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
3355026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
33561da177e4SLinus Torvalds 	 */
33571da177e4SLinus Torvalds 
33581ec047ebSHannes Frederic Sowa 	read_lock_bh(&ifp->idev->lock);
335911ffff75SHannes Frederic Sowa 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3360b173ee48SHannes Frederic Sowa 	send_rs = send_mld &&
3361b173ee48SHannes Frederic Sowa 		  ipv6_accept_ra(ifp->idev) &&
3362b173ee48SHannes Frederic Sowa 		  ifp->idev->cnf.rtr_solicits > 0 &&
3363b173ee48SHannes Frederic Sowa 		  (dev->flags&IFF_LOOPBACK) == 0;
33641ec047ebSHannes Frederic Sowa 	read_unlock_bh(&ifp->idev->lock);
33651ec047ebSHannes Frederic Sowa 
3366b173ee48SHannes Frederic Sowa 	/* While dad is in progress mld report's source address is in6_addrany.
3367b173ee48SHannes Frederic Sowa 	 * Resend with proper ll now.
3368b173ee48SHannes Frederic Sowa 	 */
3369b173ee48SHannes Frederic Sowa 	if (send_mld)
3370b173ee48SHannes Frederic Sowa 		ipv6_mc_dad_complete(ifp->idev);
3371b173ee48SHannes Frederic Sowa 
33721ec047ebSHannes Frederic Sowa 	if (send_rs) {
33731da177e4SLinus Torvalds 		/*
33741da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
33751da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
33761da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
33771da177e4SLinus Torvalds 		 */
33781ec047ebSHannes Frederic Sowa 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3379b7b1bfceSHannes Frederic Sowa 			return;
33801ec047ebSHannes Frederic Sowa 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
33811da177e4SLinus Torvalds 
3382b7b1bfceSHannes Frederic Sowa 		write_lock_bh(&ifp->idev->lock);
3383b7b1bfceSHannes Frederic Sowa 		spin_lock(&ifp->lock);
3384b7b1bfceSHannes Frederic Sowa 		ifp->idev->rs_probes = 1;
33851da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
3386b7b1bfceSHannes Frederic Sowa 		addrconf_mod_rs_timer(ifp->idev,
3387b7b1bfceSHannes Frederic Sowa 				      ifp->idev->cnf.rtr_solicit_interval);
3388b7b1bfceSHannes Frederic Sowa 		spin_unlock(&ifp->lock);
3389b7b1bfceSHannes Frederic Sowa 		write_unlock_bh(&ifp->idev->lock);
33901da177e4SLinus Torvalds 	}
33911da177e4SLinus Torvalds }
33921da177e4SLinus Torvalds 
3393e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev)
3394e21e8467SStephen Hemminger {
3395c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
3396c5e33bddSYOSHIFUJI Hideaki 
3397c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
3398502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
339921809fafSstephen hemminger 		spin_lock(&ifp->lock);
3400f2344a13SHerbert Xu 		if (ifp->flags & IFA_F_TENTATIVE &&
3401f2344a13SHerbert Xu 		    ifp->state == INET6_IFADDR_STATE_DAD)
3402c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
3403f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
3404c5e33bddSYOSHIFUJI Hideaki 	}
3405c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
3406c5e33bddSYOSHIFUJI Hideaki }
3407c5e33bddSYOSHIFUJI Hideaki 
34081da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
34091da177e4SLinus Torvalds struct if6_iter_state {
34103c40090aSDaniel Lezcano 	struct seq_net_private p;
34111da177e4SLinus Torvalds 	int bucket;
34121d578303SMihai Maruseac 	int offset;
34131da177e4SLinus Torvalds };
34141da177e4SLinus Torvalds 
34151d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
34161da177e4SLinus Torvalds {
34171da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
34181da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
34191218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
34201d578303SMihai Maruseac 	int p = 0;
34211da177e4SLinus Torvalds 
34221d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
34231d578303SMihai Maruseac 	if (pos == 0) {
34241d578303SMihai Maruseac 		state->bucket = 0;
34251d578303SMihai Maruseac 		state->offset = 0;
34261d578303SMihai Maruseac 	}
34271d578303SMihai Maruseac 
34281d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3429b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
34301d578303SMihai Maruseac 					 addr_lst) {
34319f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
34329f0d3c27SEric Dumazet 				continue;
34331d578303SMihai Maruseac 			/* sync with offset */
34341d578303SMihai Maruseac 			if (p < state->offset) {
34351d578303SMihai Maruseac 				p++;
34361d578303SMihai Maruseac 				continue;
34371d578303SMihai Maruseac 			}
34381d578303SMihai Maruseac 			state->offset++;
34391da177e4SLinus Torvalds 			return ifa;
34401da177e4SLinus Torvalds 		}
34411d578303SMihai Maruseac 
34421d578303SMihai Maruseac 		/* prepare for next bucket */
34431d578303SMihai Maruseac 		state->offset = 0;
34441d578303SMihai Maruseac 		p = 0;
34451d578303SMihai Maruseac 	}
3446c2e21293Sstephen hemminger 	return NULL;
3447c2e21293Sstephen hemminger }
34481da177e4SLinus Torvalds 
3449c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3450c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
34511da177e4SLinus Torvalds {
34521da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
34531218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
34541da177e4SLinus Torvalds 
3455b67bfe0dSSasha Levin 	hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
34569f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
34579f0d3c27SEric Dumazet 			continue;
34581d578303SMihai Maruseac 		state->offset++;
34591da177e4SLinus Torvalds 		return ifa;
34601d578303SMihai Maruseac 	}
34611da177e4SLinus Torvalds 
3462c2e21293Sstephen hemminger 	while (++state->bucket < IN6_ADDR_HSIZE) {
34631d578303SMihai Maruseac 		state->offset = 0;
3464b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa,
3465c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
34669f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
34679f0d3c27SEric Dumazet 				continue;
34681d578303SMihai Maruseac 			state->offset++;
3469c2e21293Sstephen hemminger 			return ifa;
3470c2e21293Sstephen hemminger 		}
3471c2e21293Sstephen hemminger 	}
3472c2e21293Sstephen hemminger 
3473c2e21293Sstephen hemminger 	return NULL;
3474c2e21293Sstephen hemminger }
3475c2e21293Sstephen hemminger 
34761da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
34774f70eccaSEric Dumazet 	__acquires(rcu_bh)
34781da177e4SLinus Torvalds {
34795c578aedSstephen hemminger 	rcu_read_lock_bh();
34801d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
34811da177e4SLinus Torvalds }
34821da177e4SLinus Torvalds 
34831da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
34841da177e4SLinus Torvalds {
34851da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
34861da177e4SLinus Torvalds 
34871da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
34881da177e4SLinus Torvalds 	++*pos;
34891da177e4SLinus Torvalds 	return ifa;
34901da177e4SLinus Torvalds }
34911da177e4SLinus Torvalds 
34921da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
34934f70eccaSEric Dumazet 	__releases(rcu_bh)
34941da177e4SLinus Torvalds {
34955c578aedSstephen hemminger 	rcu_read_unlock_bh();
34961da177e4SLinus Torvalds }
34971da177e4SLinus Torvalds 
34981da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
34991da177e4SLinus Torvalds {
35001da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3501971a351cSJiri Pirko 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3502b071195dSHarvey Harrison 		   &ifp->addr,
35031da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
35041da177e4SLinus Torvalds 		   ifp->prefix_len,
35051da177e4SLinus Torvalds 		   ifp->scope,
3506971a351cSJiri Pirko 		   (u8) ifp->flags,
35071da177e4SLinus Torvalds 		   ifp->idev->dev->name);
35081da177e4SLinus Torvalds 	return 0;
35091da177e4SLinus Torvalds }
35101da177e4SLinus Torvalds 
351156b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
35121da177e4SLinus Torvalds 	.start	= if6_seq_start,
35131da177e4SLinus Torvalds 	.next	= if6_seq_next,
35141da177e4SLinus Torvalds 	.show	= if6_seq_show,
35151da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
35161da177e4SLinus Torvalds };
35171da177e4SLinus Torvalds 
35181da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
35191da177e4SLinus Torvalds {
35203c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
3521cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
35221da177e4SLinus Torvalds }
35231da177e4SLinus Torvalds 
35249a32144eSArjan van de Ven static const struct file_operations if6_fops = {
35251da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
35261da177e4SLinus Torvalds 	.open		= if6_seq_open,
35271da177e4SLinus Torvalds 	.read		= seq_read,
35281da177e4SLinus Torvalds 	.llseek		= seq_lseek,
35293c40090aSDaniel Lezcano 	.release	= seq_release_net,
35301da177e4SLinus Torvalds };
35311da177e4SLinus Torvalds 
35322c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
35331da177e4SLinus Torvalds {
3534d4beaa66SGao feng 	if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
35351da177e4SLinus Torvalds 		return -ENOMEM;
35361da177e4SLinus Torvalds 	return 0;
35371da177e4SLinus Torvalds }
35381da177e4SLinus Torvalds 
35392c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
35403c40090aSDaniel Lezcano {
3541ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
35423c40090aSDaniel Lezcano }
35433c40090aSDaniel Lezcano 
35443c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
35453c40090aSDaniel Lezcano        .init = if6_proc_net_init,
35463c40090aSDaniel Lezcano        .exit = if6_proc_net_exit,
35473c40090aSDaniel Lezcano };
35483c40090aSDaniel Lezcano 
35493c40090aSDaniel Lezcano int __init if6_proc_init(void)
35503c40090aSDaniel Lezcano {
35513c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
35523c40090aSDaniel Lezcano }
35533c40090aSDaniel Lezcano 
35541da177e4SLinus Torvalds void if6_proc_exit(void)
35551da177e4SLinus Torvalds {
35563c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
35571da177e4SLinus Torvalds }
35581da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
35591da177e4SLinus Torvalds 
356007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
35613b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
3562b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
35633b9f9a1cSNoriaki TAKAMIYA {
35643b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
3565c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
3566ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
3567c2e21293Sstephen hemminger 
35685c578aedSstephen hemminger 	rcu_read_lock_bh();
3569b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3570878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
3571389f6612SDaniel Lezcano 			continue;
3572caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
35733b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
35743b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
35753b9f9a1cSNoriaki TAKAMIYA 			break;
35763b9f9a1cSNoriaki TAKAMIYA 		}
35773b9f9a1cSNoriaki TAKAMIYA 	}
35785c578aedSstephen hemminger 	rcu_read_unlock_bh();
35793b9f9a1cSNoriaki TAKAMIYA 	return ret;
35803b9f9a1cSNoriaki TAKAMIYA }
35813b9f9a1cSNoriaki TAKAMIYA #endif
35823b9f9a1cSNoriaki TAKAMIYA 
35831da177e4SLinus Torvalds /*
35841da177e4SLinus Torvalds  *	Periodic address status verification
35851da177e4SLinus Torvalds  */
35861da177e4SLinus Torvalds 
3587c15b1ccaSHannes Frederic Sowa static void addrconf_verify_rtnl(void)
35881da177e4SLinus Torvalds {
3589b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
35901da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
35911da177e4SLinus Torvalds 	int i;
35921da177e4SLinus Torvalds 
3593c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
3594c15b1ccaSHannes Frederic Sowa 
35955c578aedSstephen hemminger 	rcu_read_lock_bh();
35961da177e4SLinus Torvalds 	now = jiffies;
3597b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
35981da177e4SLinus Torvalds 
3599c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
36001da177e4SLinus Torvalds 
36011da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
36021da177e4SLinus Torvalds restart:
3603c15b1ccaSHannes Frederic Sowa 		hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
36041da177e4SLinus Torvalds 			unsigned long age;
36051da177e4SLinus Torvalds 
3606fad8da3eSYasushi Asano 			/* When setting preferred_lft to a value not zero or
3607fad8da3eSYasushi Asano 			 * infinity, while valid_lft is infinity
3608fad8da3eSYasushi Asano 			 * IFA_F_PERMANENT has a non-infinity life time.
3609fad8da3eSYasushi Asano 			 */
3610fad8da3eSYasushi Asano 			if ((ifp->flags & IFA_F_PERMANENT) &&
3611fad8da3eSYasushi Asano 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
36121da177e4SLinus Torvalds 				continue;
36131da177e4SLinus Torvalds 
36141da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
3615b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
3616b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
36171da177e4SLinus Torvalds 
36188f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
36198f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
36201da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
36211da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
36221da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
36231da177e4SLinus Torvalds 				goto restart;
36248f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
36258f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
36268f27ebb9SYOSHIFUJI Hideaki 				continue;
36271da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
3628a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
36291da177e4SLinus Torvalds 				int deprecate = 0;
36301da177e4SLinus Torvalds 
36311da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
36321da177e4SLinus Torvalds 					deprecate = 1;
36331da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
36341da177e4SLinus Torvalds 				}
36351da177e4SLinus Torvalds 
3636fad8da3eSYasushi Asano 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
3637fad8da3eSYasushi Asano 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
36381da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
36391da177e4SLinus Torvalds 
36401da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
36411da177e4SLinus Torvalds 
36421da177e4SLinus Torvalds 				if (deprecate) {
36431da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
36441da177e4SLinus Torvalds 
36451da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
36461da177e4SLinus Torvalds 					in6_ifa_put(ifp);
36471da177e4SLinus Torvalds 					goto restart;
36481da177e4SLinus Torvalds 				}
36491da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
36501da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
365188949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
365288949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
36531f9248e5SJiri Pirko 					NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
365488949cf4Sstephen hemminger 
36551da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
36561da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
36571da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
36581da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
36591da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
36601da177e4SLinus Torvalds 						ifp->regen_count++;
36611da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
36621da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
36631da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
36645c578aedSstephen hemminger 
3665291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
3666291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
3667291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
36681da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
36691da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
36701da177e4SLinus Torvalds 						in6_ifa_put(ifp);
36711da177e4SLinus Torvalds 						goto restart;
36721da177e4SLinus Torvalds 					}
36731da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
36741da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
36751da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
36761da177e4SLinus Torvalds 			} else {
36771da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
36781da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
36791da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
36801da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
36811da177e4SLinus Torvalds 			}
36821da177e4SLinus Torvalds 		}
36831da177e4SLinus Torvalds 	}
36841da177e4SLinus Torvalds 
3685b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
3686b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
3687b2db7564SYOSHIFUJI Hideaki 
3688b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
3689b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3690b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
3691b2db7564SYOSHIFUJI Hideaki 
3692b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3693b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3694b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3695b2db7564SYOSHIFUJI Hideaki 
3696ba3542e1Sdingtianhong 	ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3697ba3542e1Sdingtianhong 	      now, next, next_sec, next_sched);
3698c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
36995c578aedSstephen hemminger 	rcu_read_unlock_bh();
37001da177e4SLinus Torvalds }
37011da177e4SLinus Torvalds 
3702c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w)
3703c15b1ccaSHannes Frederic Sowa {
3704c15b1ccaSHannes Frederic Sowa 	rtnl_lock();
3705c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
3706c15b1ccaSHannes Frederic Sowa 	rtnl_unlock();
3707c15b1ccaSHannes Frederic Sowa }
3708c15b1ccaSHannes Frederic Sowa 
3709c15b1ccaSHannes Frederic Sowa static void addrconf_verify(void)
3710c15b1ccaSHannes Frederic Sowa {
3711c15b1ccaSHannes Frederic Sowa 	mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
3712c15b1ccaSHannes Frederic Sowa }
3713c15b1ccaSHannes Frederic Sowa 
3714caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
3715caeaba79SNicolas Dichtel 				     struct in6_addr **peer_pfx)
3716461d8837SThomas Graf {
3717461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
3718461d8837SThomas Graf 
3719caeaba79SNicolas Dichtel 	*peer_pfx = NULL;
3720caeaba79SNicolas Dichtel 
3721461d8837SThomas Graf 	if (addr)
3722461d8837SThomas Graf 		pfx = nla_data(addr);
3723461d8837SThomas Graf 
3724461d8837SThomas Graf 	if (local) {
3725461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3726caeaba79SNicolas Dichtel 			*peer_pfx = pfx;
3727461d8837SThomas Graf 		pfx = nla_data(local);
3728461d8837SThomas Graf 	}
3729461d8837SThomas Graf 
3730461d8837SThomas Graf 	return pfx;
3731461d8837SThomas Graf }
3732461d8837SThomas Graf 
3733ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3734461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
3735461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
3736461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
3737479840ffSJiri Pirko 	[IFA_FLAGS]		= { .len = sizeof(u32) },
3738461d8837SThomas Graf };
3739461d8837SThomas Graf 
37401da177e4SLinus Torvalds static int
3741661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
37421da177e4SLinus Torvalds {
37433b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3744b933f716SThomas Graf 	struct ifaddrmsg *ifm;
3745b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
3746caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
37476046d5b4SHeiner Kallweit 	u32 ifa_flags;
3748b933f716SThomas Graf 	int err;
37491da177e4SLinus Torvalds 
3750b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3751b933f716SThomas Graf 	if (err < 0)
3752b933f716SThomas Graf 		return err;
3753b933f716SThomas Graf 
3754b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
3755caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
37561da177e4SLinus Torvalds 	if (pfx == NULL)
37571da177e4SLinus Torvalds 		return -EINVAL;
37581da177e4SLinus Torvalds 
37596046d5b4SHeiner Kallweit 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
37606046d5b4SHeiner Kallweit 
37616046d5b4SHeiner Kallweit 	/* We ignore other flags so far. */
37626046d5b4SHeiner Kallweit 	ifa_flags &= IFA_F_MANAGETEMPADDR;
37636046d5b4SHeiner Kallweit 
37646046d5b4SHeiner Kallweit 	return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
37656046d5b4SHeiner Kallweit 			      ifm->ifa_prefixlen);
37661da177e4SLinus Torvalds }
37671da177e4SLinus Torvalds 
3768479840ffSJiri Pirko static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
376955ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
3770081bba5bSNoriaki TAKAMIYA {
37716f704992SYOSHIFUJI Hideaki 	u32 flags;
37726f704992SYOSHIFUJI Hideaki 	clock_t expires;
37734bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
377453bd6749SJiri Pirko 	bool was_managetempaddr;
37755b84efecSThomas Haller 	bool had_prefixroute;
377646d48046SYOSHIFUJI Hideaki 
3777c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
3778c15b1ccaSHannes Frederic Sowa 
3779081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
3780081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
3781081bba5bSNoriaki TAKAMIYA 
378253bd6749SJiri Pirko 	if (ifa_flags & IFA_F_MANAGETEMPADDR &&
378353bd6749SJiri Pirko 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
378453bd6749SJiri Pirko 		return -EINVAL;
378553bd6749SJiri Pirko 
37864bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
37874bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
37884bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
37894bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
37906f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
37914bed72e4SYOSHIFUJI Hideaki 	} else {
37924bed72e4SYOSHIFUJI Hideaki 		expires = 0;
37934bed72e4SYOSHIFUJI Hideaki 		flags = 0;
37944bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
37956f704992SYOSHIFUJI Hideaki 	}
3796081bba5bSNoriaki TAKAMIYA 
37974bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
37984bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
37994bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
380055ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
38014bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
38024bed72e4SYOSHIFUJI Hideaki 	}
3803081bba5bSNoriaki TAKAMIYA 
3804081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
380553bd6749SJiri Pirko 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
38065b84efecSThomas Haller 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
38075b84efecSThomas Haller 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
380853bd6749SJiri Pirko 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
3809761aac73SThomas Haller 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
3810761aac73SThomas Haller 			IFA_F_NOPREFIXROUTE);
381153bd6749SJiri Pirko 	ifp->flags |= ifa_flags;
3812081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
3813081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
3814081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
3815081bba5bSNoriaki TAKAMIYA 
3816081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
3817081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
3818081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
3819081bba5bSNoriaki TAKAMIYA 
3820761aac73SThomas Haller 	if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
382146d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
38226f704992SYOSHIFUJI Hideaki 				      expires, flags);
38235b84efecSThomas Haller 	} else if (had_prefixroute) {
38245b84efecSThomas Haller 		enum cleanup_prefix_rt_t action;
38255b84efecSThomas Haller 		unsigned long rt_expires;
38265b84efecSThomas Haller 
38275b84efecSThomas Haller 		write_lock_bh(&ifp->idev->lock);
38285b84efecSThomas Haller 		action = check_cleanup_prefix_route(ifp, &rt_expires);
38295b84efecSThomas Haller 		write_unlock_bh(&ifp->idev->lock);
38305b84efecSThomas Haller 
38315b84efecSThomas Haller 		if (action != CLEANUP_PREFIX_RT_NOP) {
38325b84efecSThomas Haller 			cleanup_prefix_route(ifp, rt_expires,
38335b84efecSThomas Haller 				action == CLEANUP_PREFIX_RT_DEL);
38345b84efecSThomas Haller 		}
3835761aac73SThomas Haller 	}
383653bd6749SJiri Pirko 
383753bd6749SJiri Pirko 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
383853bd6749SJiri Pirko 		if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
383953bd6749SJiri Pirko 			valid_lft = prefered_lft = 0;
384053bd6749SJiri Pirko 		manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
384153bd6749SJiri Pirko 				 !was_managetempaddr, jiffies);
384253bd6749SJiri Pirko 	}
384353bd6749SJiri Pirko 
3844c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
3845081bba5bSNoriaki TAKAMIYA 
3846081bba5bSNoriaki TAKAMIYA 	return 0;
3847081bba5bSNoriaki TAKAMIYA }
3848081bba5bSNoriaki TAKAMIYA 
3849081bba5bSNoriaki TAKAMIYA static int
3850661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
38511da177e4SLinus Torvalds {
38523b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3853461d8837SThomas Graf 	struct ifaddrmsg *ifm;
3854461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
3855caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
38567198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
38577198f8ceSThomas Graf 	struct net_device *dev;
385855ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3859479840ffSJiri Pirko 	u32 ifa_flags;
3860461d8837SThomas Graf 	int err;
38611da177e4SLinus Torvalds 
3862461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3863461d8837SThomas Graf 	if (err < 0)
3864461d8837SThomas Graf 		return err;
3865461d8837SThomas Graf 
3866461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
3867caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
38681da177e4SLinus Torvalds 	if (pfx == NULL)
38691da177e4SLinus Torvalds 		return -EINVAL;
38701da177e4SLinus Torvalds 
3871461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
38720778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
3873461d8837SThomas Graf 
3874461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
38750778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
3876461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
3877461d8837SThomas Graf 	} else {
3878461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
3879461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
38800778769dSNoriaki TAKAMIYA 	}
38810778769dSNoriaki TAKAMIYA 
3882af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
38837198f8ceSThomas Graf 	if (dev == NULL)
38847198f8ceSThomas Graf 		return -ENODEV;
3885081bba5bSNoriaki TAKAMIYA 
3886479840ffSJiri Pirko 	ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
3887479840ffSJiri Pirko 
388855ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
3889761aac73SThomas Haller 	ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
3890761aac73SThomas Haller 		     IFA_F_NOPREFIXROUTE;
389155ebaef1SNoriaki TAKAMIYA 
38921cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
38937198f8ceSThomas Graf 	if (ifa == NULL) {
38947198f8ceSThomas Graf 		/*
38957198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
3896db9c7c39Sstephen hemminger 		 * userspace already relies on not having to provide this.
38977198f8ceSThomas Graf 		 */
3898caeaba79SNicolas Dichtel 		return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
3899af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
3900af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
39011da177e4SLinus Torvalds 	}
39021da177e4SLinus Torvalds 
39037198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
39047198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
39057198f8ceSThomas Graf 		err = -EEXIST;
39067198f8ceSThomas Graf 	else
390755ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
39087198f8ceSThomas Graf 
39097198f8ceSThomas Graf 	in6_ifa_put(ifa);
39107198f8ceSThomas Graf 
39117198f8ceSThomas Graf 	return err;
39127198f8ceSThomas Graf }
39137198f8ceSThomas Graf 
3914479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
3915101bb229SThomas Graf 			  u8 scope, int ifindex)
3916101bb229SThomas Graf {
3917101bb229SThomas Graf 	struct ifaddrmsg *ifm;
3918101bb229SThomas Graf 
3919101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
3920101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
3921101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
3922101bb229SThomas Graf 	ifm->ifa_flags = flags;
3923101bb229SThomas Graf 	ifm->ifa_scope = scope;
3924101bb229SThomas Graf 	ifm->ifa_index = ifindex;
3925101bb229SThomas Graf }
3926101bb229SThomas Graf 
392785486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
392885486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
392985486af0SThomas Graf {
393085486af0SThomas Graf 	struct ifa_cacheinfo ci;
393185486af0SThomas Graf 
393218a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
393318a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
393485486af0SThomas Graf 	ci.ifa_prefered = preferred;
393585486af0SThomas Graf 	ci.ifa_valid = valid;
393685486af0SThomas Graf 
393785486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
393885486af0SThomas Graf }
393985486af0SThomas Graf 
3940101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
3941101bb229SThomas Graf {
3942101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
3943101bb229SThomas Graf 		return RT_SCOPE_HOST;
3944101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
3945101bb229SThomas Graf 		return RT_SCOPE_LINK;
3946101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
3947101bb229SThomas Graf 		return RT_SCOPE_SITE;
3948101bb229SThomas Graf 	else
3949101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
3950101bb229SThomas Graf }
3951101bb229SThomas Graf 
39520ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
39530ab6803bSThomas Graf {
3954339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3955caeaba79SNicolas Dichtel 	       + nla_total_size(16) /* IFA_LOCAL */
3956339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
3957479840ffSJiri Pirko 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
3958479840ffSJiri Pirko 	       + nla_total_size(4)  /* IFA_FLAGS */;
39590ab6803bSThomas Graf }
3960c5396a31SYOSHIFUJI Hideaki 
39611da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
396215e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
39631da177e4SLinus Torvalds {
39641da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
396585486af0SThomas Graf 	u32 preferred, valid;
39661da177e4SLinus Torvalds 
396715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
39680ab6803bSThomas Graf 	if (nlh == NULL)
396926932566SPatrick McHardy 		return -EMSGSIZE;
39700ab6803bSThomas Graf 
3971101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3972101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
3973101bb229SThomas Graf 
3974fad8da3eSYasushi Asano 	if (!((ifa->flags&IFA_F_PERMANENT) &&
3975fad8da3eSYasushi Asano 	      (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
397685486af0SThomas Graf 		preferred = ifa->prefered_lft;
397785486af0SThomas Graf 		valid = ifa->valid_lft;
397885486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
39791da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
3980a1faa698SJens Rosenboom 			if (preferred > tval)
398185486af0SThomas Graf 				preferred -= tval;
3982a1faa698SJens Rosenboom 			else
3983a1faa698SJens Rosenboom 				preferred = 0;
3984f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
3985f56619fcSBen Hutchings 				if (valid > tval)
398685486af0SThomas Graf 					valid -= tval;
3987f56619fcSBen Hutchings 				else
3988f56619fcSBen Hutchings 					valid = 0;
3989f56619fcSBen Hutchings 			}
39901da177e4SLinus Torvalds 		}
39911da177e4SLinus Torvalds 	} else {
399285486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
399385486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
39941da177e4SLinus Torvalds 	}
399585486af0SThomas Graf 
39967996c799SCong Wang 	if (!ipv6_addr_any(&ifa->peer_addr)) {
3997caeaba79SNicolas Dichtel 		if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 ||
3998caeaba79SNicolas Dichtel 		    nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0)
3999caeaba79SNicolas Dichtel 			goto error;
4000caeaba79SNicolas Dichtel 	} else
4001caeaba79SNicolas Dichtel 		if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0)
4002caeaba79SNicolas Dichtel 			goto error;
4003caeaba79SNicolas Dichtel 
4004caeaba79SNicolas Dichtel 	if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4005caeaba79SNicolas Dichtel 		goto error;
400685486af0SThomas Graf 
4007479840ffSJiri Pirko 	if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4008479840ffSJiri Pirko 		goto error;
4009479840ffSJiri Pirko 
40100ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
4011caeaba79SNicolas Dichtel 
4012caeaba79SNicolas Dichtel error:
4013caeaba79SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
4014caeaba79SNicolas Dichtel 	return -EMSGSIZE;
40151da177e4SLinus Torvalds }
40161da177e4SLinus Torvalds 
40171da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
401815e47304SEric W. Biederman 				u32 portid, u32 seq, int event, u16 flags)
40191da177e4SLinus Torvalds {
40201da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4021101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4022101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
40231da177e4SLinus Torvalds 
40241da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4025101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4026101bb229SThomas Graf 
402715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
40280ab6803bSThomas Graf 	if (nlh == NULL)
402926932566SPatrick McHardy 		return -EMSGSIZE;
40300ab6803bSThomas Graf 
4031101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
40320ab6803bSThomas Graf 	if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
40330ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
403426932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
403526932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
403626932566SPatrick McHardy 		return -EMSGSIZE;
403726932566SPatrick McHardy 	}
403885486af0SThomas Graf 
40390ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
40401da177e4SLinus Torvalds }
40411da177e4SLinus Torvalds 
40421da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
404315e47304SEric W. Biederman 				u32 portid, u32 seq, int event, unsigned int flags)
40441da177e4SLinus Torvalds {
40451da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
4046101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
4047101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
40481da177e4SLinus Torvalds 
40491da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4050101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
4051101bb229SThomas Graf 
405215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
40530ab6803bSThomas Graf 	if (nlh == NULL)
405426932566SPatrick McHardy 		return -EMSGSIZE;
40550ab6803bSThomas Graf 
4056101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
40570ab6803bSThomas Graf 	if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
40580ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
405926932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
406026932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
406126932566SPatrick McHardy 		return -EMSGSIZE;
406226932566SPatrick McHardy 	}
406385486af0SThomas Graf 
40640ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
40651da177e4SLinus Torvalds }
40661da177e4SLinus Torvalds 
4067e21e8467SStephen Hemminger enum addr_type_t {
40681da177e4SLinus Torvalds 	UNICAST_ADDR,
40691da177e4SLinus Torvalds 	MULTICAST_ADDR,
40701da177e4SLinus Torvalds 	ANYCAST_ADDR,
40711da177e4SLinus Torvalds };
40721da177e4SLinus Torvalds 
4073234b27c3SEric Dumazet /* called with rcu_read_lock() */
4074234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4075234b27c3SEric Dumazet 			  struct netlink_callback *cb, enum addr_type_t type,
4076234b27c3SEric Dumazet 			  int s_ip_idx, int *p_ip_idx)
40771da177e4SLinus Torvalds {
40781da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
40791da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
4080234b27c3SEric Dumazet 	int err = 1;
4081234b27c3SEric Dumazet 	int ip_idx = *p_ip_idx;
40821da177e4SLinus Torvalds 
40831da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
40841da177e4SLinus Torvalds 	switch (type) {
4085502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
4086502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
4087502a2ffdSstephen hemminger 
4088ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
4089502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
4090502a2ffdSstephen hemminger 			if (++ip_idx < s_ip_idx)
40911da177e4SLinus Torvalds 				continue;
40925d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifaddr(skb, ifa,
409315e47304SEric W. Biederman 						NETLINK_CB(cb->skb).portid,
40945d5619b4SYOSHIFUJI Hideaki 						cb->nlh->nlmsg_seq,
40955d5619b4SYOSHIFUJI Hideaki 						RTM_NEWADDR,
40965d5619b4SYOSHIFUJI Hideaki 						NLM_F_MULTI);
4097bcd32326SEric Dumazet 			if (err <= 0)
4098bcd32326SEric Dumazet 				break;
409963998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
41001da177e4SLinus Torvalds 		}
41011da177e4SLinus Torvalds 		break;
4102502a2ffdSstephen hemminger 	}
41031da177e4SLinus Torvalds 	case MULTICAST_ADDR:
41041da177e4SLinus Torvalds 		/* multicast address */
41051da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
41061da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
41071da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
41081da177e4SLinus Torvalds 				continue;
41095d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifmcaddr(skb, ifmca,
411015e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
41115d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
41125d5619b4SYOSHIFUJI Hideaki 						  RTM_GETMULTICAST,
41135d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4114bcd32326SEric Dumazet 			if (err <= 0)
4115bcd32326SEric Dumazet 				break;
41161da177e4SLinus Torvalds 		}
41171da177e4SLinus Torvalds 		break;
41181da177e4SLinus Torvalds 	case ANYCAST_ADDR:
41191da177e4SLinus Torvalds 		/* anycast address */
41201da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
41211da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
41221da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
41231da177e4SLinus Torvalds 				continue;
41245d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifacaddr(skb, ifaca,
412515e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
41265d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
41275d5619b4SYOSHIFUJI Hideaki 						  RTM_GETANYCAST,
41285d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
4129bcd32326SEric Dumazet 			if (err <= 0)
4130bcd32326SEric Dumazet 				break;
41311da177e4SLinus Torvalds 		}
41321da177e4SLinus Torvalds 		break;
41331da177e4SLinus Torvalds 	default:
41341da177e4SLinus Torvalds 		break;
41351da177e4SLinus Torvalds 	}
41361da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
4137234b27c3SEric Dumazet 	*p_ip_idx = ip_idx;
4138234b27c3SEric Dumazet 	return err;
4139234b27c3SEric Dumazet }
41405d5619b4SYOSHIFUJI Hideaki 
4141234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4142234b27c3SEric Dumazet 			   enum addr_type_t type)
4143234b27c3SEric Dumazet {
4144234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
4145234b27c3SEric Dumazet 	int h, s_h;
4146234b27c3SEric Dumazet 	int idx, ip_idx;
4147234b27c3SEric Dumazet 	int s_idx, s_ip_idx;
4148234b27c3SEric Dumazet 	struct net_device *dev;
4149234b27c3SEric Dumazet 	struct inet6_dev *idev;
4150234b27c3SEric Dumazet 	struct hlist_head *head;
4151234b27c3SEric Dumazet 
4152234b27c3SEric Dumazet 	s_h = cb->args[0];
4153234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
4154234b27c3SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
4155234b27c3SEric Dumazet 
4156234b27c3SEric Dumazet 	rcu_read_lock();
415763998ac2SNicolas Dichtel 	cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4158234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4159234b27c3SEric Dumazet 		idx = 0;
4160234b27c3SEric Dumazet 		head = &net->dev_index_head[h];
4161b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
4162234b27c3SEric Dumazet 			if (idx < s_idx)
4163234b27c3SEric Dumazet 				goto cont;
41644b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
4165234b27c3SEric Dumazet 				s_ip_idx = 0;
4166234b27c3SEric Dumazet 			ip_idx = 0;
4167e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
4168e21e8467SStephen Hemminger 			if (!idev)
4169234b27c3SEric Dumazet 				goto cont;
4170234b27c3SEric Dumazet 
4171234b27c3SEric Dumazet 			if (in6_dump_addrs(idev, skb, cb, type,
4172234b27c3SEric Dumazet 					   s_ip_idx, &ip_idx) <= 0)
4173234b27c3SEric Dumazet 				goto done;
41747562f876SPavel Emelianov cont:
41757562f876SPavel Emelianov 			idx++;
41761da177e4SLinus Torvalds 		}
4177234b27c3SEric Dumazet 	}
4178234b27c3SEric Dumazet done:
4179234b27c3SEric Dumazet 	rcu_read_unlock();
4180234b27c3SEric Dumazet 	cb->args[0] = h;
4181234b27c3SEric Dumazet 	cb->args[1] = idx;
4182234b27c3SEric Dumazet 	cb->args[2] = ip_idx;
4183234b27c3SEric Dumazet 
41841da177e4SLinus Torvalds 	return skb->len;
41851da177e4SLinus Torvalds }
41861da177e4SLinus Torvalds 
41871da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
41881da177e4SLinus Torvalds {
41891da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
4190b854272bSDenis V. Lunev 
41911da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
41921da177e4SLinus Torvalds }
41931da177e4SLinus Torvalds 
41941da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
41951da177e4SLinus Torvalds {
41961da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
4197b854272bSDenis V. Lunev 
41981da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
41991da177e4SLinus Torvalds }
42001da177e4SLinus Torvalds 
42011da177e4SLinus Torvalds 
42021da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
42031da177e4SLinus Torvalds {
42041da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
4205b854272bSDenis V. Lunev 
42061da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
42071da177e4SLinus Torvalds }
42081da177e4SLinus Torvalds 
4209661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
42106c223828SNoriaki TAKAMIYA {
42113b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
42121b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
42131b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4214caeaba79SNicolas Dichtel 	struct in6_addr *addr = NULL, *peer;
42156c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
42166c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
42176c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
42186c223828SNoriaki TAKAMIYA 	int err;
42196c223828SNoriaki TAKAMIYA 
42201b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
42211b29fc2cSThomas Graf 	if (err < 0)
42221b29fc2cSThomas Graf 		goto errout;
42236c223828SNoriaki TAKAMIYA 
4224caeaba79SNicolas Dichtel 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
42251b29fc2cSThomas Graf 	if (addr == NULL) {
42261b29fc2cSThomas Graf 		err = -EINVAL;
42271b29fc2cSThomas Graf 		goto errout;
42281b29fc2cSThomas Graf 	}
42291b29fc2cSThomas Graf 
42301b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
42316c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
42326fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
42336c223828SNoriaki TAKAMIYA 
4234e21e8467SStephen Hemminger 	ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4235e21e8467SStephen Hemminger 	if (!ifa) {
42361b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
42371b29fc2cSThomas Graf 		goto errout;
42386c223828SNoriaki TAKAMIYA 	}
42396c223828SNoriaki TAKAMIYA 
4240e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4241e21e8467SStephen Hemminger 	if (!skb) {
42421b29fc2cSThomas Graf 		err = -ENOBUFS;
42431b29fc2cSThomas Graf 		goto errout_ifa;
42441b29fc2cSThomas Graf 	}
42451b29fc2cSThomas Graf 
424615e47304SEric W. Biederman 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
42476c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
424826932566SPatrick McHardy 	if (err < 0) {
424926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
425026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
425126932566SPatrick McHardy 		kfree_skb(skb);
425226932566SPatrick McHardy 		goto errout_ifa;
425326932566SPatrick McHardy 	}
425415e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
42551b29fc2cSThomas Graf errout_ifa:
42566c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
42571b29fc2cSThomas Graf errout:
42586c223828SNoriaki TAKAMIYA 	return err;
42596c223828SNoriaki TAKAMIYA }
42606c223828SNoriaki TAKAMIYA 
42611da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
42621da177e4SLinus Torvalds {
42631da177e4SLinus Torvalds 	struct sk_buff *skb;
4264c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
42655d620266SThomas Graf 	int err = -ENOBUFS;
42661da177e4SLinus Torvalds 
42670ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
42685d620266SThomas Graf 	if (skb == NULL)
42695d620266SThomas Graf 		goto errout;
42705d620266SThomas Graf 
42715d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
427226932566SPatrick McHardy 	if (err < 0) {
427326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
427426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
427526932566SPatrick McHardy 		kfree_skb(skb);
427626932566SPatrick McHardy 		goto errout;
427726932566SPatrick McHardy 	}
42781ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
42791ce85fe4SPablo Neira Ayuso 	return;
42805d620266SThomas Graf errout:
42815d620266SThomas Graf 	if (err < 0)
42826fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
42831da177e4SLinus Torvalds }
42841da177e4SLinus Torvalds 
4285b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
42861da177e4SLinus Torvalds 				__s32 *array, int bytes)
42871da177e4SLinus Torvalds {
428804561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
428904561c1fSThomas Graf 
42901da177e4SLinus Torvalds 	memset(array, 0, bytes);
42911da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
42921da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
42931da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
42941da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
42951da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
42961da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
42971da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
42981da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
429993908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
430093908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
430193908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
430293908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
43031da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4304fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4305fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4306fc4eba58SHannes Frederic Sowa 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4307fc4eba58SHannes Frederic Sowa 		jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
43081da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
43091da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
43101da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
43111da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
43121da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
43131da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
431465f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4315c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4316930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4317930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
431893908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
431993908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
4320fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
432109c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
432209c884d4SYOSHIFUJI Hideaki #endif
4323930d6ff2SYOSHIFUJI Hideaki #endif
4324fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
43250bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
432695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
432795c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
432895c385b4SNeil Horman #endif
43297bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
43307bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
43317bc570c8SYOSHIFUJI Hideaki #endif
4332778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
43331b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4334c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
43355cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4336b800c3b9SHannes Frederic Sowa 	array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4337d9333196SBen Greear 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
43381da177e4SLinus Torvalds }
43391da177e4SLinus Torvalds 
4340b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
4341b382b191SThomas Graf {
4342b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
4343b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
4344b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4345b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4346f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4347f53adae4SDaniel Borkmann 	     + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4348b382b191SThomas Graf }
4349b382b191SThomas Graf 
4350339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
4351339bf98fSThomas Graf {
4352339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4353339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4354339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4355339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
4356339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
4357b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4358339bf98fSThomas Graf }
4359c5396a31SYOSHIFUJI Hideaki 
4360be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
43617d720c3eSTejun Heo 				      int items, int bytes)
43627f7d9a6bSHerbert Xu {
43637f7d9a6bSHerbert Xu 	int i;
43647f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
43657f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
43667f7d9a6bSHerbert Xu 
43677f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
43687f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
43697f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
4370be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
43717f7d9a6bSHerbert Xu 
43727f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
43737f7d9a6bSHerbert Xu }
43747f7d9a6bSHerbert Xu 
4375698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
43764ce3c183SEric Dumazet 				      int items, int bytes, size_t syncpoff)
43774ce3c183SEric Dumazet {
43784ce3c183SEric Dumazet 	int i;
43794ce3c183SEric Dumazet 	int pad = bytes - sizeof(u64) * items;
43804ce3c183SEric Dumazet 	BUG_ON(pad < 0);
43814ce3c183SEric Dumazet 
43824ce3c183SEric Dumazet 	/* Use put_unaligned() because stats may not be aligned for u64. */
43834ce3c183SEric Dumazet 	put_unaligned(items, &stats[0]);
43844ce3c183SEric Dumazet 	for (i = 1; i < items; i++)
43854ce3c183SEric Dumazet 		put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
43864ce3c183SEric Dumazet 
43874ce3c183SEric Dumazet 	memset(&stats[items], 0, pad);
43884ce3c183SEric Dumazet }
43894ce3c183SEric Dumazet 
43907f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
43917f7d9a6bSHerbert Xu 			     int bytes)
43927f7d9a6bSHerbert Xu {
43937f7d9a6bSHerbert Xu 	switch (attrtype) {
43947f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
4395698365faSWANG Cong 		__snmp6_fill_stats64(stats, idev->stats.ipv6,
43964ce3c183SEric Dumazet 				     IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
43977f7d9a6bSHerbert Xu 		break;
43987f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
4399be281e55SEric Dumazet 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
44007f7d9a6bSHerbert Xu 		break;
44017f7d9a6bSHerbert Xu 	}
44027f7d9a6bSHerbert Xu }
44037f7d9a6bSHerbert Xu 
4404b382b191SThomas Graf static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
44051da177e4SLinus Torvalds {
4406bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
44071da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
44081da177e4SLinus Torvalds 
4409c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4410c78679e8SDavid S. Miller 		goto nla_put_failure;
44111da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
441224912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
441324912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
44141f9248e5SJiri Pirko 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
4415c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4416c78679e8SDavid S. Miller 		goto nla_put_failure;
4417bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4418bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
441904561c1fSThomas Graf 		goto nla_put_failure;
4420bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
44211da177e4SLinus Torvalds 
4422bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
4423bf99f1bdSYOSHIFUJI Hideaki 
4424bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4425bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
4426bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4427bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4428bf99f1bdSYOSHIFUJI Hideaki 
4429bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4430bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
4431bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4432bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
44331da177e4SLinus Torvalds 
4434f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4435f53adae4SDaniel Borkmann 	if (nla == NULL)
4436f53adae4SDaniel Borkmann 		goto nla_put_failure;
4437bc91b0f0SJiri Pirko 
4438bc91b0f0SJiri Pirko 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
4439bc91b0f0SJiri Pirko 		goto nla_put_failure;
4440bc91b0f0SJiri Pirko 
4441f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
4442f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4443f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
4444f53adae4SDaniel Borkmann 
4445b382b191SThomas Graf 	return 0;
4446b382b191SThomas Graf 
4447b382b191SThomas Graf nla_put_failure:
4448b382b191SThomas Graf 	return -EMSGSIZE;
4449b382b191SThomas Graf }
4450b382b191SThomas Graf 
4451b382b191SThomas Graf static size_t inet6_get_link_af_size(const struct net_device *dev)
4452b382b191SThomas Graf {
4453b382b191SThomas Graf 	if (!__in6_dev_get(dev))
4454b382b191SThomas Graf 		return 0;
4455b382b191SThomas Graf 
4456b382b191SThomas Graf 	return inet6_ifla6_size();
4457b382b191SThomas Graf }
4458b382b191SThomas Graf 
4459b382b191SThomas Graf static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4460b382b191SThomas Graf {
4461b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
4462b382b191SThomas Graf 
4463b382b191SThomas Graf 	if (!idev)
4464b382b191SThomas Graf 		return -ENODATA;
4465b382b191SThomas Graf 
4466b382b191SThomas Graf 	if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4467b382b191SThomas Graf 		return -EMSGSIZE;
4468b382b191SThomas Graf 
4469b382b191SThomas Graf 	return 0;
4470b382b191SThomas Graf }
4471b382b191SThomas Graf 
4472f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4473f53adae4SDaniel Borkmann {
4474f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
4475f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
4476fc403832SDaniel Borkmann 	bool update_rs = false;
4477dc848292SHannes Frederic Sowa 	struct in6_addr ll_addr;
4478f53adae4SDaniel Borkmann 
4479c15b1ccaSHannes Frederic Sowa 	ASSERT_RTNL();
4480c15b1ccaSHannes Frederic Sowa 
4481f53adae4SDaniel Borkmann 	if (token == NULL)
4482f53adae4SDaniel Borkmann 		return -EINVAL;
4483f53adae4SDaniel Borkmann 	if (ipv6_addr_any(token))
4484f53adae4SDaniel Borkmann 		return -EINVAL;
4485f53adae4SDaniel Borkmann 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4486f53adae4SDaniel Borkmann 		return -EINVAL;
4487f53adae4SDaniel Borkmann 	if (!ipv6_accept_ra(idev))
4488f53adae4SDaniel Borkmann 		return -EINVAL;
4489f53adae4SDaniel Borkmann 	if (idev->cnf.rtr_solicits <= 0)
4490f53adae4SDaniel Borkmann 		return -EINVAL;
4491f53adae4SDaniel Borkmann 
4492f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
4493f53adae4SDaniel Borkmann 
4494f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4495f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4496f53adae4SDaniel Borkmann 
4497f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
4498f53adae4SDaniel Borkmann 
4499dc848292SHannes Frederic Sowa 	if (!idev->dead && (idev->if_flags & IF_READY) &&
4500dc848292SHannes Frederic Sowa 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4501dc848292SHannes Frederic Sowa 			     IFA_F_OPTIMISTIC)) {
4502fc403832SDaniel Borkmann 
4503fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
4504fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
4505fc403832SDaniel Borkmann 		 */
4506f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4507fc403832SDaniel Borkmann 		update_rs = true;
4508fc403832SDaniel Borkmann 	}
4509f53adae4SDaniel Borkmann 
4510f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
4511fc403832SDaniel Borkmann 
451277ecaaceSHannes Frederic Sowa 	if (update_rs) {
4513f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
451477ecaaceSHannes Frederic Sowa 		idev->rs_probes = 1;
451577ecaaceSHannes Frederic Sowa 		addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
451677ecaaceSHannes Frederic Sowa 	}
4517f53adae4SDaniel Borkmann 
4518f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
4519f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
4520f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
4521617fe29dSDaniel Borkmann 		if (ifp->tokenized) {
4522f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
4523f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
4524f53adae4SDaniel Borkmann 		}
4525f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
4526f53adae4SDaniel Borkmann 	}
4527f53adae4SDaniel Borkmann 
4528f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
4529c15b1ccaSHannes Frederic Sowa 	addrconf_verify_rtnl();
4530f53adae4SDaniel Borkmann 	return 0;
4531f53adae4SDaniel Borkmann }
4532f53adae4SDaniel Borkmann 
4533f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4534f53adae4SDaniel Borkmann {
4535f53adae4SDaniel Borkmann 	int err = -EINVAL;
4536f53adae4SDaniel Borkmann 	struct inet6_dev *idev = __in6_dev_get(dev);
4537f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
4538f53adae4SDaniel Borkmann 
4539f53adae4SDaniel Borkmann 	if (!idev)
4540f53adae4SDaniel Borkmann 		return -EAFNOSUPPORT;
4541f53adae4SDaniel Borkmann 
4542f53adae4SDaniel Borkmann 	if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4543f53adae4SDaniel Borkmann 		BUG();
4544f53adae4SDaniel Borkmann 
4545bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_TOKEN]) {
4546f53adae4SDaniel Borkmann 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4547bc91b0f0SJiri Pirko 		if (err)
4548bc91b0f0SJiri Pirko 			return err;
4549bc91b0f0SJiri Pirko 	}
4550bc91b0f0SJiri Pirko 
4551bc91b0f0SJiri Pirko 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
4552bc91b0f0SJiri Pirko 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
4553bc91b0f0SJiri Pirko 
4554bc91b0f0SJiri Pirko 		if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
4555bc91b0f0SJiri Pirko 		    mode != IN6_ADDR_GEN_MODE_NONE)
4556bc91b0f0SJiri Pirko 			return -EINVAL;
4557bc91b0f0SJiri Pirko 		idev->addr_gen_mode = mode;
4558bc91b0f0SJiri Pirko 		err = 0;
4559bc91b0f0SJiri Pirko 	}
4560f53adae4SDaniel Borkmann 
4561f53adae4SDaniel Borkmann 	return err;
4562f53adae4SDaniel Borkmann }
4563f53adae4SDaniel Borkmann 
4564b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
456515e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
4566b382b191SThomas Graf {
4567b382b191SThomas Graf 	struct net_device *dev = idev->dev;
4568b382b191SThomas Graf 	struct ifinfomsg *hdr;
4569b382b191SThomas Graf 	struct nlmsghdr *nlh;
4570b382b191SThomas Graf 	void *protoinfo;
4571b382b191SThomas Graf 
457215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4573b382b191SThomas Graf 	if (nlh == NULL)
4574b382b191SThomas Graf 		return -EMSGSIZE;
4575b382b191SThomas Graf 
4576b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
4577b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
4578b382b191SThomas Graf 	hdr->__ifi_pad = 0;
4579b382b191SThomas Graf 	hdr->ifi_type = dev->type;
4580b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
4581b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
4582b382b191SThomas Graf 	hdr->ifi_change = 0;
4583b382b191SThomas Graf 
4584c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4585c78679e8SDavid S. Miller 	    (dev->addr_len &&
4586c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4587c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4588c78679e8SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
4589c78679e8SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4590c78679e8SDavid S. Miller 		goto nla_put_failure;
4591b382b191SThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4592b382b191SThomas Graf 	if (protoinfo == NULL)
4593b382b191SThomas Graf 		goto nla_put_failure;
4594b382b191SThomas Graf 
4595b382b191SThomas Graf 	if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4596b382b191SThomas Graf 		goto nla_put_failure;
4597b382b191SThomas Graf 
459804561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
459904561c1fSThomas Graf 	return nlmsg_end(skb, nlh);
46001da177e4SLinus Torvalds 
460104561c1fSThomas Graf nla_put_failure:
460226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
460326932566SPatrick McHardy 	return -EMSGSIZE;
46041da177e4SLinus Torvalds }
46051da177e4SLinus Torvalds 
46061da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
46071da177e4SLinus Torvalds {
46083b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
460984d2697dSEric Dumazet 	int h, s_h;
4610434a8a58SDavid S. Miller 	int idx = 0, s_idx;
46111da177e4SLinus Torvalds 	struct net_device *dev;
46121da177e4SLinus Torvalds 	struct inet6_dev *idev;
461384d2697dSEric Dumazet 	struct hlist_head *head;
46141da177e4SLinus Torvalds 
461584d2697dSEric Dumazet 	s_h = cb->args[0];
461684d2697dSEric Dumazet 	s_idx = cb->args[1];
461784d2697dSEric Dumazet 
461884d2697dSEric Dumazet 	rcu_read_lock();
461984d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
46207562f876SPavel Emelianov 		idx = 0;
462184d2697dSEric Dumazet 		head = &net->dev_index_head[h];
4622b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
46231da177e4SLinus Torvalds 			if (idx < s_idx)
46247562f876SPavel Emelianov 				goto cont;
462584d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
462684d2697dSEric Dumazet 			if (!idev)
46277562f876SPavel Emelianov 				goto cont;
462884d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
462915e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
463084d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
463184d2697dSEric Dumazet 					      RTM_NEWLINK, NLM_F_MULTI) <= 0)
463284d2697dSEric Dumazet 				goto out;
46337562f876SPavel Emelianov cont:
46347562f876SPavel Emelianov 			idx++;
46351da177e4SLinus Torvalds 		}
463684d2697dSEric Dumazet 	}
463784d2697dSEric Dumazet out:
463884d2697dSEric Dumazet 	rcu_read_unlock();
463984d2697dSEric Dumazet 	cb->args[1] = idx;
464084d2697dSEric Dumazet 	cb->args[0] = h;
46411da177e4SLinus Torvalds 
46421da177e4SLinus Torvalds 	return skb->len;
46431da177e4SLinus Torvalds }
46441da177e4SLinus Torvalds 
46451da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
46461da177e4SLinus Torvalds {
46471da177e4SLinus Torvalds 	struct sk_buff *skb;
4648c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
46498d7a76c9SThomas Graf 	int err = -ENOBUFS;
46501da177e4SLinus Torvalds 
4651339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
46528d7a76c9SThomas Graf 	if (skb == NULL)
46538d7a76c9SThomas Graf 		goto errout;
46548d7a76c9SThomas Graf 
46558d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
465626932566SPatrick McHardy 	if (err < 0) {
465726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
465826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
465926932566SPatrick McHardy 		kfree_skb(skb);
466026932566SPatrick McHardy 		goto errout;
466126932566SPatrick McHardy 	}
46625f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
46631ce85fe4SPablo Neira Ayuso 	return;
46648d7a76c9SThomas Graf errout:
46658d7a76c9SThomas Graf 	if (err < 0)
46665f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
46671da177e4SLinus Torvalds }
46681da177e4SLinus Torvalds 
4669339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
4670339bf98fSThomas Graf {
4671339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
4672339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
4673339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
4674339bf98fSThomas Graf }
4675c5396a31SYOSHIFUJI Hideaki 
46761da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
467715e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
4678b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
46791da177e4SLinus Torvalds {
46801da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
46811da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
46821da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
46831da177e4SLinus Torvalds 
468415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
46856051e2f4SThomas Graf 	if (nlh == NULL)
468626932566SPatrick McHardy 		return -EMSGSIZE;
46876051e2f4SThomas Graf 
46886051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
46891da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
46908a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
46918a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
46921da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
46931da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
46941da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
46958a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
46961da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
46971da177e4SLinus Torvalds 	if (pinfo->onlink)
46981da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
46991da177e4SLinus Torvalds 	if (pinfo->autoconf)
47001da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
47011da177e4SLinus Torvalds 
4702c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4703c78679e8SDavid S. Miller 		goto nla_put_failure;
47041da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
47051da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
4706c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4707c78679e8SDavid S. Miller 		goto nla_put_failure;
47086051e2f4SThomas Graf 	return nlmsg_end(skb, nlh);
47091da177e4SLinus Torvalds 
47106051e2f4SThomas Graf nla_put_failure:
471126932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
471226932566SPatrick McHardy 	return -EMSGSIZE;
47131da177e4SLinus Torvalds }
47141da177e4SLinus Torvalds 
47151da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
47161da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
47171da177e4SLinus Torvalds {
47181da177e4SLinus Torvalds 	struct sk_buff *skb;
4719c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
47208c384bfaSThomas Graf 	int err = -ENOBUFS;
47211da177e4SLinus Torvalds 
4722339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
47238c384bfaSThomas Graf 	if (skb == NULL)
47248c384bfaSThomas Graf 		goto errout;
47258c384bfaSThomas Graf 
47268c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
472726932566SPatrick McHardy 	if (err < 0) {
472826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
472926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
473026932566SPatrick McHardy 		kfree_skb(skb);
473126932566SPatrick McHardy 		goto errout;
473226932566SPatrick McHardy 	}
47331ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
47341ce85fe4SPablo Neira Ayuso 	return;
47358c384bfaSThomas Graf errout:
47368c384bfaSThomas Graf 	if (err < 0)
47376fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
47381da177e4SLinus Torvalds }
47391da177e4SLinus Torvalds 
47401da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
47411da177e4SLinus Torvalds {
474263998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
474363998ac2SNicolas Dichtel 
4744c15b1ccaSHannes Frederic Sowa 	if (event)
4745c15b1ccaSHannes Frederic Sowa 		ASSERT_RTNL();
4746c15b1ccaSHannes Frederic Sowa 
47471da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
47481da177e4SLinus Torvalds 
47491da177e4SLinus Torvalds 	switch (event) {
47501da177e4SLinus Torvalds 	case RTM_NEWADDR:
475195c385b4SNeil Horman 		/*
475295c385b4SNeil Horman 		 * If the address was optimistic
475395c385b4SNeil Horman 		 * we inserted the route at the start of
475495c385b4SNeil Horman 		 * our DAD process, so we don't need
475595c385b4SNeil Horman 		 * to do it again
475695c385b4SNeil Horman 		 */
475795c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
475840e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
47591da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
47601da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
47617996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr))
4762caeaba79SNicolas Dichtel 			addrconf_prefix_route(&ifp->peer_addr, 128,
4763caeaba79SNicolas Dichtel 					      ifp->idev->dev, 0, 0);
47641da177e4SLinus Torvalds 		break;
47651da177e4SLinus Torvalds 	case RTM_DELADDR:
47661da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
47671da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
47681da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
47697996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr)) {
4770caeaba79SNicolas Dichtel 			struct rt6_info *rt;
4771caeaba79SNicolas Dichtel 
4772*e7478dfcSNicolas Dichtel 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
4773*e7478dfcSNicolas Dichtel 						       ifp->idev->dev, 0, 0);
4774f24062b0SNicolas Dichtel 			if (rt && ip6_del_rt(rt))
4775caeaba79SNicolas Dichtel 				dst_free(&rt->dst);
4776caeaba79SNicolas Dichtel 		}
4777d8d1f30bSChangli Gao 		dst_hold(&ifp->rt->dst);
477893fa159aSstephen hemminger 
477973a8bd74SDavid S. Miller 		if (ip6_del_rt(ifp->rt))
4780d8d1f30bSChangli Gao 			dst_free(&ifp->rt->dst);
47811da177e4SLinus Torvalds 		break;
47821da177e4SLinus Torvalds 	}
478363998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
4784439677d7Sfan.du 	rt_genid_bump_ipv6(net);
47851da177e4SLinus Torvalds }
47861da177e4SLinus Torvalds 
47871da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
47881da177e4SLinus Torvalds {
47898814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
47901da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
47911da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
47928814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
47931da177e4SLinus Torvalds }
47941da177e4SLinus Torvalds 
47951da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
47961da177e4SLinus Torvalds 
47971da177e4SLinus Torvalds static
4798fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
47991da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
48001da177e4SLinus Torvalds {
48011da177e4SLinus Torvalds 	int *valp = ctl->data;
48021da177e4SLinus Torvalds 	int val = *valp;
480388af182eSEric W. Biederman 	loff_t pos = *ppos;
4804fe2c6338SJoe Perches 	struct ctl_table lctl;
48051da177e4SLinus Torvalds 	int ret;
48061da177e4SLinus Torvalds 
4807013d97e9SFrancesco Ruggeri 	/*
4808013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
4809013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
4810013d97e9SFrancesco Ruggeri 	 */
4811013d97e9SFrancesco Ruggeri 	lctl = *ctl;
4812013d97e9SFrancesco Ruggeri 	lctl.data = &val;
4813013d97e9SFrancesco Ruggeri 
4814013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
48151da177e4SLinus Torvalds 
4816c8fecf22SPavel Emelyanov 	if (write)
4817b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
481888af182eSEric W. Biederman 	if (ret)
481988af182eSEric W. Biederman 		*ppos = pos;
48201da177e4SLinus Torvalds 	return ret;
48211da177e4SLinus Torvalds }
48221da177e4SLinus Torvalds 
482356d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
482456d417b1SBrian Haley {
482575538c2bSCong Wang 	struct netdev_notifier_info info;
482675538c2bSCong Wang 
482756d417b1SBrian Haley 	if (!idev || !idev->dev)
482856d417b1SBrian Haley 		return;
482956d417b1SBrian Haley 
483075538c2bSCong Wang 	netdev_notifier_info_init(&info, idev->dev);
483156d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
483275538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_DOWN, &info);
483356d417b1SBrian Haley 	else
483475538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_UP, &info);
483556d417b1SBrian Haley }
483656d417b1SBrian Haley 
483756d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
483856d417b1SBrian Haley {
483956d417b1SBrian Haley 	struct net_device *dev;
484056d417b1SBrian Haley 	struct inet6_dev *idev;
484156d417b1SBrian Haley 
484256d417b1SBrian Haley 	rcu_read_lock();
4843c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
484456d417b1SBrian Haley 		idev = __in6_dev_get(dev);
484556d417b1SBrian Haley 		if (idev) {
484656d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
484756d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
484856d417b1SBrian Haley 			if (changed)
484956d417b1SBrian Haley 				dev_disable_change(idev);
485056d417b1SBrian Haley 		}
485156d417b1SBrian Haley 	}
4852c6d14c84SEric Dumazet 	rcu_read_unlock();
485356d417b1SBrian Haley }
485456d417b1SBrian Haley 
4855013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
485656d417b1SBrian Haley {
485756d417b1SBrian Haley 	struct net *net;
4858013d97e9SFrancesco Ruggeri 	int old;
4859013d97e9SFrancesco Ruggeri 
4860013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
4861013d97e9SFrancesco Ruggeri 		return restart_syscall();
486256d417b1SBrian Haley 
486356d417b1SBrian Haley 	net = (struct net *)table->extra2;
4864013d97e9SFrancesco Ruggeri 	old = *p;
4865013d97e9SFrancesco Ruggeri 	*p = newf;
486656d417b1SBrian Haley 
4867013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4868013d97e9SFrancesco Ruggeri 		rtnl_unlock();
486956d417b1SBrian Haley 		return 0;
487088af182eSEric W. Biederman 	}
487156d417b1SBrian Haley 
487256d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
487356d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
487456d417b1SBrian Haley 		addrconf_disable_change(net, newf);
4875013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
487656d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
487756d417b1SBrian Haley 
487856d417b1SBrian Haley 	rtnl_unlock();
487956d417b1SBrian Haley 	return 0;
488056d417b1SBrian Haley }
488156d417b1SBrian Haley 
488256d417b1SBrian Haley static
4883fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
488456d417b1SBrian Haley 			    void __user *buffer, size_t *lenp, loff_t *ppos)
488556d417b1SBrian Haley {
488656d417b1SBrian Haley 	int *valp = ctl->data;
488756d417b1SBrian Haley 	int val = *valp;
488888af182eSEric W. Biederman 	loff_t pos = *ppos;
4889fe2c6338SJoe Perches 	struct ctl_table lctl;
489056d417b1SBrian Haley 	int ret;
489156d417b1SBrian Haley 
4892013d97e9SFrancesco Ruggeri 	/*
4893013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
4894013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
4895013d97e9SFrancesco Ruggeri 	 */
4896013d97e9SFrancesco Ruggeri 	lctl = *ctl;
4897013d97e9SFrancesco Ruggeri 	lctl.data = &val;
4898013d97e9SFrancesco Ruggeri 
4899013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
490056d417b1SBrian Haley 
490156d417b1SBrian Haley 	if (write)
490256d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
490388af182eSEric W. Biederman 	if (ret)
490488af182eSEric W. Biederman 		*ppos = pos;
490556d417b1SBrian Haley 	return ret;
490656d417b1SBrian Haley }
490756d417b1SBrian Haley 
4908c92d5491Sstephen hemminger static
4909c92d5491Sstephen hemminger int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
4910c92d5491Sstephen hemminger 			      void __user *buffer, size_t *lenp, loff_t *ppos)
4911c92d5491Sstephen hemminger {
4912c92d5491Sstephen hemminger 	int *valp = ctl->data;
4913c92d5491Sstephen hemminger 	int ret;
4914c92d5491Sstephen hemminger 	int old, new;
4915c92d5491Sstephen hemminger 
4916c92d5491Sstephen hemminger 	old = *valp;
4917c92d5491Sstephen hemminger 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
4918c92d5491Sstephen hemminger 	new = *valp;
4919c92d5491Sstephen hemminger 
4920c92d5491Sstephen hemminger 	if (write && old != new) {
4921c92d5491Sstephen hemminger 		struct net *net = ctl->extra2;
4922c92d5491Sstephen hemminger 
4923c92d5491Sstephen hemminger 		if (!rtnl_trylock())
4924c92d5491Sstephen hemminger 			return restart_syscall();
4925c92d5491Sstephen hemminger 
4926c92d5491Sstephen hemminger 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
4927c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
4928c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_DEFAULT,
4929c92d5491Sstephen hemminger 						     net->ipv6.devconf_dflt);
4930c92d5491Sstephen hemminger 		else if (valp == &net->ipv6.devconf_all->proxy_ndp)
4931c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
4932c92d5491Sstephen hemminger 						     NETCONFA_IFINDEX_ALL,
4933c92d5491Sstephen hemminger 						     net->ipv6.devconf_all);
4934c92d5491Sstephen hemminger 		else {
4935c92d5491Sstephen hemminger 			struct inet6_dev *idev = ctl->extra1;
4936c92d5491Sstephen hemminger 
4937c92d5491Sstephen hemminger 			inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
4938c92d5491Sstephen hemminger 						     idev->dev->ifindex,
4939c92d5491Sstephen hemminger 						     &idev->cnf);
4940c92d5491Sstephen hemminger 		}
4941c92d5491Sstephen hemminger 		rtnl_unlock();
4942c92d5491Sstephen hemminger 	}
4943c92d5491Sstephen hemminger 
4944c92d5491Sstephen hemminger 	return ret;
4945c92d5491Sstephen hemminger }
4946c92d5491Sstephen hemminger 
4947c92d5491Sstephen hemminger 
49481da177e4SLinus Torvalds static struct addrconf_sysctl_table
49491da177e4SLinus Torvalds {
49501da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
4951fe2c6338SJoe Perches 	struct ctl_table addrconf_vars[DEVCONF_MAX+1];
4952ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
49531da177e4SLinus Torvalds 	.sysctl_header = NULL,
49541da177e4SLinus Torvalds 	.addrconf_vars = {
49551da177e4SLinus Torvalds 		{
49561da177e4SLinus Torvalds 			.procname	= "forwarding",
49571da177e4SLinus Torvalds 			.data		= &ipv6_devconf.forwarding,
49581da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
49591da177e4SLinus Torvalds 			.mode		= 0644,
49606d9f239aSAlexey Dobriyan 			.proc_handler	= addrconf_sysctl_forward,
49611da177e4SLinus Torvalds 		},
49621da177e4SLinus Torvalds 		{
49631da177e4SLinus Torvalds 			.procname	= "hop_limit",
49641da177e4SLinus Torvalds 			.data		= &ipv6_devconf.hop_limit,
49651da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
49661da177e4SLinus Torvalds 			.mode		= 0644,
49671da177e4SLinus Torvalds 			.proc_handler	= proc_dointvec,
49681da177e4SLinus Torvalds 		},
49691da177e4SLinus Torvalds 		{
49701da177e4SLinus Torvalds 			.procname	= "mtu",
49711da177e4SLinus Torvalds 			.data		= &ipv6_devconf.mtu6,
49721da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
49731da177e4SLinus Torvalds 			.mode		= 0644,
49746d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
49751da177e4SLinus Torvalds 		},
49761da177e4SLinus Torvalds 		{
49771da177e4SLinus Torvalds 			.procname	= "accept_ra",
49781da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_ra,
49791da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
49801da177e4SLinus Torvalds 			.mode		= 0644,
49816d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
49821da177e4SLinus Torvalds 		},
49831da177e4SLinus Torvalds 		{
49841da177e4SLinus Torvalds 			.procname	= "accept_redirects",
49851da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_redirects,
49861da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
49871da177e4SLinus Torvalds 			.mode		= 0644,
49886d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
49891da177e4SLinus Torvalds 		},
49901da177e4SLinus Torvalds 		{
49911da177e4SLinus Torvalds 			.procname	= "autoconf",
49921da177e4SLinus Torvalds 			.data		= &ipv6_devconf.autoconf,
49931da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
49941da177e4SLinus Torvalds 			.mode		= 0644,
49956d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
49961da177e4SLinus Torvalds 		},
49971da177e4SLinus Torvalds 		{
49981da177e4SLinus Torvalds 			.procname	= "dad_transmits",
49991da177e4SLinus Torvalds 			.data		= &ipv6_devconf.dad_transmits,
50001da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
50011da177e4SLinus Torvalds 			.mode		= 0644,
50026d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
50031da177e4SLinus Torvalds 		},
50041da177e4SLinus Torvalds 		{
50051da177e4SLinus Torvalds 			.procname	= "router_solicitations",
50061da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicits,
50071da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
50081da177e4SLinus Torvalds 			.mode		= 0644,
50096d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
50101da177e4SLinus Torvalds 		},
50111da177e4SLinus Torvalds 		{
50121da177e4SLinus Torvalds 			.procname	= "router_solicitation_interval",
50131da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_interval,
50141da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
50151da177e4SLinus Torvalds 			.mode		= 0644,
50166d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
50171da177e4SLinus Torvalds 		},
50181da177e4SLinus Torvalds 		{
50191da177e4SLinus Torvalds 			.procname	= "router_solicitation_delay",
50201da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_delay,
50211da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
50221da177e4SLinus Torvalds 			.mode		= 0644,
50236d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
50241da177e4SLinus Torvalds 		},
50251da177e4SLinus Torvalds 		{
50261da177e4SLinus Torvalds 			.procname	= "force_mld_version",
50271da177e4SLinus Torvalds 			.data		= &ipv6_devconf.force_mld_version,
50281da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
50291da177e4SLinus Torvalds 			.mode		= 0644,
50306d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
50311da177e4SLinus Torvalds 		},
5032fc4eba58SHannes Frederic Sowa 		{
5033fc4eba58SHannes Frederic Sowa 			.procname	= "mldv1_unsolicited_report_interval",
5034fc4eba58SHannes Frederic Sowa 			.data		=
5035fc4eba58SHannes Frederic Sowa 				&ipv6_devconf.mldv1_unsolicited_report_interval,
5036fc4eba58SHannes Frederic Sowa 			.maxlen		= sizeof(int),
5037fc4eba58SHannes Frederic Sowa 			.mode		= 0644,
5038fc4eba58SHannes Frederic Sowa 			.proc_handler	= proc_dointvec_ms_jiffies,
5039fc4eba58SHannes Frederic Sowa 		},
5040fc4eba58SHannes Frederic Sowa 		{
5041fc4eba58SHannes Frederic Sowa 			.procname	= "mldv2_unsolicited_report_interval",
5042fc4eba58SHannes Frederic Sowa 			.data		=
5043fc4eba58SHannes Frederic Sowa 				&ipv6_devconf.mldv2_unsolicited_report_interval,
5044fc4eba58SHannes Frederic Sowa 			.maxlen		= sizeof(int),
5045fc4eba58SHannes Frederic Sowa 			.mode		= 0644,
5046fc4eba58SHannes Frederic Sowa 			.proc_handler	= proc_dointvec_ms_jiffies,
5047fc4eba58SHannes Frederic Sowa 		},
50481da177e4SLinus Torvalds 		{
50491da177e4SLinus Torvalds 			.procname	= "use_tempaddr",
50501da177e4SLinus Torvalds 			.data		= &ipv6_devconf.use_tempaddr,
50511da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
50521da177e4SLinus Torvalds 			.mode		= 0644,
50536d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
50541da177e4SLinus Torvalds 		},
50551da177e4SLinus Torvalds 		{
50561da177e4SLinus Torvalds 			.procname	= "temp_valid_lft",
50571da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_valid_lft,
50581da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
50591da177e4SLinus Torvalds 			.mode		= 0644,
50606d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
50611da177e4SLinus Torvalds 		},
50621da177e4SLinus Torvalds 		{
50631da177e4SLinus Torvalds 			.procname	= "temp_prefered_lft",
50641da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_prefered_lft,
50651da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
50661da177e4SLinus Torvalds 			.mode		= 0644,
50676d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
50681da177e4SLinus Torvalds 		},
50691da177e4SLinus Torvalds 		{
50701da177e4SLinus Torvalds 			.procname	= "regen_max_retry",
50711da177e4SLinus Torvalds 			.data		= &ipv6_devconf.regen_max_retry,
50721da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
50731da177e4SLinus Torvalds 			.mode		= 0644,
50746d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
50751da177e4SLinus Torvalds 		},
50761da177e4SLinus Torvalds 		{
50771da177e4SLinus Torvalds 			.procname	= "max_desync_factor",
50781da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_desync_factor,
50791da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
50801da177e4SLinus Torvalds 			.mode		= 0644,
50816d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
50821da177e4SLinus Torvalds 		},
50831da177e4SLinus Torvalds 		{
50841da177e4SLinus Torvalds 			.procname	= "max_addresses",
50851da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_addresses,
50861da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
50871da177e4SLinus Torvalds 			.mode		= 0644,
50886d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
50891da177e4SLinus Torvalds 		},
50901da177e4SLinus Torvalds 		{
509165f5c7c1SYOSHIFUJI Hideaki 			.procname	= "accept_ra_defrtr",
509265f5c7c1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_defrtr,
509365f5c7c1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
509465f5c7c1SYOSHIFUJI Hideaki 			.mode		= 0644,
50956d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
509665f5c7c1SYOSHIFUJI Hideaki 		},
509765f5c7c1SYOSHIFUJI Hideaki 		{
5098c4fd30ebSYOSHIFUJI Hideaki 			.procname	= "accept_ra_pinfo",
5099c4fd30ebSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_pinfo,
5100c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5101c4fd30ebSYOSHIFUJI Hideaki 			.mode		= 0644,
51026d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
5103c4fd30ebSYOSHIFUJI Hideaki 		},
5104930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
5105930d6ff2SYOSHIFUJI Hideaki 		{
5106930d6ff2SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rtr_pref",
5107930d6ff2SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rtr_pref,
5108930d6ff2SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5109930d6ff2SYOSHIFUJI Hideaki 			.mode		= 0644,
51106d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
5111930d6ff2SYOSHIFUJI Hideaki 		},
511252e16356SYOSHIFUJI Hideaki 		{
511352e16356SYOSHIFUJI Hideaki 			.procname	= "router_probe_interval",
511452e16356SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.rtr_probe_interval,
511552e16356SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
511652e16356SYOSHIFUJI Hideaki 			.mode		= 0644,
51176d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
511852e16356SYOSHIFUJI Hideaki 		},
5119fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
512009c884d4SYOSHIFUJI Hideaki 		{
512109c884d4SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rt_info_max_plen",
512209c884d4SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
512309c884d4SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
512409c884d4SYOSHIFUJI Hideaki 			.mode		= 0644,
51256d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
512609c884d4SYOSHIFUJI Hideaki 		},
512709c884d4SYOSHIFUJI Hideaki #endif
5128930d6ff2SYOSHIFUJI Hideaki #endif
5129c4fd30ebSYOSHIFUJI Hideaki 		{
5130fbea49e1SYOSHIFUJI Hideaki 			.procname	= "proxy_ndp",
5131fbea49e1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.proxy_ndp,
5132fbea49e1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5133fbea49e1SYOSHIFUJI Hideaki 			.mode		= 0644,
5134c92d5491Sstephen hemminger 			.proc_handler	= addrconf_sysctl_proxy_ndp,
5135fbea49e1SYOSHIFUJI Hideaki 		},
5136fbea49e1SYOSHIFUJI Hideaki 		{
51370bcbc926SYOSHIFUJI Hideaki 			.procname	= "accept_source_route",
51380bcbc926SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_source_route,
51390bcbc926SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
51400bcbc926SYOSHIFUJI Hideaki 			.mode		= 0644,
51416d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
51420bcbc926SYOSHIFUJI Hideaki 		},
514395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
514495c385b4SNeil Horman 		{
514595c385b4SNeil Horman 			.procname       = "optimistic_dad",
514695c385b4SNeil Horman 			.data           = &ipv6_devconf.optimistic_dad,
514795c385b4SNeil Horman 			.maxlen         = sizeof(int),
514895c385b4SNeil Horman 			.mode           = 0644,
51496d9f239aSAlexey Dobriyan 			.proc_handler   = proc_dointvec,
515095c385b4SNeil Horman 
515195c385b4SNeil Horman 		},
515295c385b4SNeil Horman #endif
51537bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
51547bc570c8SYOSHIFUJI Hideaki 		{
51557bc570c8SYOSHIFUJI Hideaki 			.procname	= "mc_forwarding",
51567bc570c8SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.mc_forwarding,
51577bc570c8SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5158a4e6db07SDavid S. Miller 			.mode		= 0444,
51596d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
51607bc570c8SYOSHIFUJI Hideaki 		},
51617bc570c8SYOSHIFUJI Hideaki #endif
51620bcbc926SYOSHIFUJI Hideaki 		{
5163778d80beSYOSHIFUJI Hideaki 			.procname	= "disable_ipv6",
5164778d80beSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.disable_ipv6,
5165778d80beSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
5166778d80beSYOSHIFUJI Hideaki 			.mode		= 0644,
516756d417b1SBrian Haley 			.proc_handler	= addrconf_sysctl_disable,
5168778d80beSYOSHIFUJI Hideaki 		},
5169778d80beSYOSHIFUJI Hideaki 		{
51701b34be74SYOSHIFUJI Hideaki 			.procname	= "accept_dad",
51711b34be74SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_dad,
51721b34be74SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
51731b34be74SYOSHIFUJI Hideaki 			.mode		= 0644,
51746d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
51751b34be74SYOSHIFUJI Hideaki 		},
51761b34be74SYOSHIFUJI Hideaki 		{
5177f7734fdfSOctavian Purdila 			.procname       = "force_tllao",
5178f7734fdfSOctavian Purdila 			.data           = &ipv6_devconf.force_tllao,
5179f7734fdfSOctavian Purdila 			.maxlen         = sizeof(int),
5180f7734fdfSOctavian Purdila 			.mode           = 0644,
5181f7734fdfSOctavian Purdila 			.proc_handler   = proc_dointvec
5182f7734fdfSOctavian Purdila 		},
5183f7734fdfSOctavian Purdila 		{
51845cb04436SHannes Frederic Sowa 			.procname       = "ndisc_notify",
51855cb04436SHannes Frederic Sowa 			.data           = &ipv6_devconf.ndisc_notify,
51865cb04436SHannes Frederic Sowa 			.maxlen         = sizeof(int),
51875cb04436SHannes Frederic Sowa 			.mode           = 0644,
51885cb04436SHannes Frederic Sowa 			.proc_handler   = proc_dointvec
51895cb04436SHannes Frederic Sowa 		},
51905cb04436SHannes Frederic Sowa 		{
5191b800c3b9SHannes Frederic Sowa 			.procname	= "suppress_frag_ndisc",
5192b800c3b9SHannes Frederic Sowa 			.data		= &ipv6_devconf.suppress_frag_ndisc,
5193b800c3b9SHannes Frederic Sowa 			.maxlen		= sizeof(int),
5194b800c3b9SHannes Frederic Sowa 			.mode		= 0644,
5195b800c3b9SHannes Frederic Sowa 			.proc_handler	= proc_dointvec
5196b800c3b9SHannes Frederic Sowa 		},
5197b800c3b9SHannes Frederic Sowa 		{
5198d9333196SBen Greear 			.procname	= "accept_ra_from_local",
5199d9333196SBen Greear 			.data		= &ipv6_devconf.accept_ra_from_local,
5200d9333196SBen Greear 			.maxlen		= sizeof(int),
5201d9333196SBen Greear 			.mode		= 0644,
5202d9333196SBen Greear 			.proc_handler	= proc_dointvec,
5203d9333196SBen Greear 		},
5204d9333196SBen Greear 		{
5205f8572d8fSEric W. Biederman 			/* sentinel */
52061da177e4SLinus Torvalds 		}
52071da177e4SLinus Torvalds 	},
52081da177e4SLinus Torvalds };
52091da177e4SLinus Torvalds 
5210bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5211f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
52121da177e4SLinus Torvalds {
52131da177e4SLinus Torvalds 	int i;
52141da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
52156105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
52161dab6222SPavel Emelyanov 
5217af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
52181da177e4SLinus Torvalds 	if (t == NULL)
5219f68635e6SPavel Emelyanov 		goto out;
5220f68635e6SPavel Emelyanov 
52211da177e4SLinus Torvalds 	for (i = 0; t->addrconf_vars[i].data; i++) {
52221da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
52231da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
5224bff16c2fSPavel Emelyanov 		t->addrconf_vars[i].extra2 = net;
52251da177e4SLinus Torvalds 	}
52261da177e4SLinus Torvalds 
52276105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
52281da177e4SLinus Torvalds 
52296105e293SEric W. Biederman 	t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
52301da177e4SLinus Torvalds 	if (t->sysctl_header == NULL)
52316105e293SEric W. Biederman 		goto free;
5232f68635e6SPavel Emelyanov 
52331da177e4SLinus Torvalds 	p->sysctl = t;
523495897312SPavel Emelyanov 	return 0;
52351da177e4SLinus Torvalds 
52361da177e4SLinus Torvalds free:
52371da177e4SLinus Torvalds 	kfree(t);
5238f68635e6SPavel Emelyanov out:
523995897312SPavel Emelyanov 	return -ENOBUFS;
52401da177e4SLinus Torvalds }
52411da177e4SLinus Torvalds 
5242408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5243f52295a9SPavel Emelyanov {
5244408c4768SPavel Emelyanov 	struct addrconf_sysctl_table *t;
5245f52295a9SPavel Emelyanov 
5246408c4768SPavel Emelyanov 	if (p->sysctl == NULL)
5247408c4768SPavel Emelyanov 		return;
5248408c4768SPavel Emelyanov 
5249408c4768SPavel Emelyanov 	t = p->sysctl;
52501da177e4SLinus Torvalds 	p->sysctl = NULL;
5251ff538818SLucian Adrian Grijincu 	unregister_net_sysctl_table(t->sysctl_header);
52521da177e4SLinus Torvalds 	kfree(t);
52531da177e4SLinus Torvalds }
5254408c4768SPavel Emelyanov 
5255a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev)
5256408c4768SPavel Emelyanov {
5257a317a2f1SWANG Cong 	int err;
5258a317a2f1SWANG Cong 
5259a317a2f1SWANG Cong 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
5260a317a2f1SWANG Cong 		return -EINVAL;
5261a317a2f1SWANG Cong 
5262a317a2f1SWANG Cong 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
5263f8572d8fSEric W. Biederman 				    &ndisc_ifinfo_sysctl_change);
5264a317a2f1SWANG Cong 	if (err)
5265a317a2f1SWANG Cong 		return err;
5266a317a2f1SWANG Cong 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5267f8572d8fSEric W. Biederman 					 idev, &idev->cnf);
5268a317a2f1SWANG Cong 	if (err)
5269a317a2f1SWANG Cong 		neigh_sysctl_unregister(idev->nd_parms);
5270a317a2f1SWANG Cong 
5271a317a2f1SWANG Cong 	return err;
5272408c4768SPavel Emelyanov }
5273408c4768SPavel Emelyanov 
5274408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
5275408c4768SPavel Emelyanov {
5276408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
5277408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
52781da177e4SLinus Torvalds }
52791da177e4SLinus Torvalds 
52801da177e4SLinus Torvalds 
52811da177e4SLinus Torvalds #endif
52821da177e4SLinus Torvalds 
52832c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
5284e0da5a48SPavel Emelyanov {
5285a79ca223SHong Zhiguo 	int err = -ENOMEM;
5286e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
5287e0da5a48SPavel Emelyanov 
5288a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5289e0da5a48SPavel Emelyanov 	if (all == NULL)
5290e0da5a48SPavel Emelyanov 		goto err_alloc_all;
5291e0da5a48SPavel Emelyanov 
5292a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5293e0da5a48SPavel Emelyanov 	if (dflt == NULL)
5294e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
5295a79ca223SHong Zhiguo 
529656d417b1SBrian Haley 	/* these will be inherited by all namespaces */
529756d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
529856d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5299e0da5a48SPavel Emelyanov 
5300e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
5301e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
5302e0da5a48SPavel Emelyanov 
5303e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5304f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
5305e0da5a48SPavel Emelyanov 	if (err < 0)
5306e0da5a48SPavel Emelyanov 		goto err_reg_all;
5307e0da5a48SPavel Emelyanov 
5308f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5309e0da5a48SPavel Emelyanov 	if (err < 0)
5310e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
5311e0da5a48SPavel Emelyanov #endif
5312e0da5a48SPavel Emelyanov 	return 0;
5313e0da5a48SPavel Emelyanov 
5314e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5315e0da5a48SPavel Emelyanov err_reg_dflt:
5316e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
5317e0da5a48SPavel Emelyanov err_reg_all:
5318e0da5a48SPavel Emelyanov 	kfree(dflt);
5319e0da5a48SPavel Emelyanov #endif
5320e0da5a48SPavel Emelyanov err_alloc_dflt:
5321e0da5a48SPavel Emelyanov 	kfree(all);
5322e0da5a48SPavel Emelyanov err_alloc_all:
5323e0da5a48SPavel Emelyanov 	return err;
5324e0da5a48SPavel Emelyanov }
5325e0da5a48SPavel Emelyanov 
53262c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
5327e0da5a48SPavel Emelyanov {
5328e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5329e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5330e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
5331e0da5a48SPavel Emelyanov #endif
533209ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
5333e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_dflt);
5334e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_all);
5335e0da5a48SPavel Emelyanov 	}
5336e0da5a48SPavel Emelyanov }
5337e0da5a48SPavel Emelyanov 
5338e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
5339e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
5340e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
5341e0da5a48SPavel Emelyanov };
5342e0da5a48SPavel Emelyanov 
5343b382b191SThomas Graf static struct rtnl_af_ops inet6_ops = {
5344b382b191SThomas Graf 	.family		  = AF_INET6,
5345b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
5346b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
5347f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
5348b382b191SThomas Graf };
5349b382b191SThomas Graf 
53501da177e4SLinus Torvalds /*
53511da177e4SLinus Torvalds  *	Init / cleanup code
53521da177e4SLinus Torvalds  */
53531da177e4SLinus Torvalds 
53541da177e4SLinus Torvalds int __init addrconf_init(void)
53551da177e4SLinus Torvalds {
5356a317a2f1SWANG Cong 	struct inet6_dev *idev;
5357c2e21293Sstephen hemminger 	int i, err;
53582a8cc6c8SYOSHIFUJI Hideaki 
5359e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
5360e21e8467SStephen Hemminger 	if (err < 0) {
5361f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
5362f3213831SJoe Perches 			__func__, err);
53632cc6d2bfSNeil Horman 		goto out;
53642a8cc6c8SYOSHIFUJI Hideaki 	}
53651da177e4SLinus Torvalds 
53662cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
53672cc6d2bfSNeil Horman 	if (err < 0)
53682cc6d2bfSNeil Horman 		goto out_addrlabel;
5369e0da5a48SPavel Emelyanov 
5370c15b1ccaSHannes Frederic Sowa 	addrconf_wq = create_workqueue("ipv6_addrconf");
5371c15b1ccaSHannes Frederic Sowa 	if (!addrconf_wq) {
5372c15b1ccaSHannes Frederic Sowa 		err = -ENOMEM;
5373c15b1ccaSHannes Frederic Sowa 		goto out_nowq;
5374c15b1ccaSHannes Frederic Sowa 	}
5375c15b1ccaSHannes Frederic Sowa 
53761da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
53771da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
53781da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
53791da177e4SLinus Torvalds 	 * to other devices which are up.
53801da177e4SLinus Torvalds 	 *
53811da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
53821da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
53831da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
53841da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
53851da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
53861da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
53871da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
53881da177e4SLinus Torvalds 	 *
53891da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
53901da177e4SLinus Torvalds 	 * loopback_dev first by hand.
53911da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
53921da177e4SLinus Torvalds 	 * device and it being up should be removed.
53931da177e4SLinus Torvalds 	 */
53941da177e4SLinus Torvalds 	rtnl_lock();
5395a317a2f1SWANG Cong 	idev = ipv6_add_dev(init_net.loopback_dev);
53961da177e4SLinus Torvalds 	rtnl_unlock();
5397a317a2f1SWANG Cong 	if (IS_ERR(idev)) {
5398a317a2f1SWANG Cong 		err = PTR_ERR(idev);
5399e0da5a48SPavel Emelyanov 		goto errlo;
5400a317a2f1SWANG Cong 	}
54011da177e4SLinus Torvalds 
5402c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
5403c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5404c2e21293Sstephen hemminger 
54051da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
54061da177e4SLinus Torvalds 
5407c15b1ccaSHannes Frederic Sowa 	addrconf_verify();
5408c127ea2cSThomas Graf 
54093678a9d8Sstephen hemminger 	rtnl_af_register(&inet6_ops);
5410b382b191SThomas Graf 
5411c7ac8679SGreg Rose 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5412c7ac8679SGreg Rose 			      NULL);
5413c127ea2cSThomas Graf 	if (err < 0)
5414c127ea2cSThomas Graf 		goto errout;
5415c127ea2cSThomas Graf 
5416c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
5417c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5418c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5419c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5420c7ac8679SGreg Rose 			inet6_dump_ifaddr, NULL);
5421c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5422c7ac8679SGreg Rose 			inet6_dump_ifmcaddr, NULL);
5423c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5424c7ac8679SGreg Rose 			inet6_dump_ifacaddr, NULL);
542576f8f6cbSNicolas Dichtel 	__rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
54267a674200SNicolas Dichtel 			inet6_netconf_dump_devconf, NULL);
5427c127ea2cSThomas Graf 
54282a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
54292a8cc6c8SYOSHIFUJI Hideaki 
54301da177e4SLinus Torvalds 	return 0;
5431c127ea2cSThomas Graf errout:
5432b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
5433c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
5434e0da5a48SPavel Emelyanov errlo:
5435c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
5436c15b1ccaSHannes Frederic Sowa out_nowq:
5437e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
54382cc6d2bfSNeil Horman out_addrlabel:
54392cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
54402cc6d2bfSNeil Horman out:
5441c127ea2cSThomas Graf 	return err;
54421da177e4SLinus Torvalds }
54431da177e4SLinus Torvalds 
544409f7709fSDaniel Lezcano void addrconf_cleanup(void)
54451da177e4SLinus Torvalds {
5446176c39afSDaniel Lezcano 	struct net_device *dev;
54471da177e4SLinus Torvalds 	int i;
54481da177e4SLinus Torvalds 
54491da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
5450e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
54512cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
54521da177e4SLinus Torvalds 
54531da177e4SLinus Torvalds 	rtnl_lock();
54541da177e4SLinus Torvalds 
5455b382b191SThomas Graf 	__rtnl_af_unregister(&inet6_ops);
5456b382b191SThomas Graf 
5457176c39afSDaniel Lezcano 	/* clean dev list */
5458176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
5459176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
5460176c39afSDaniel Lezcano 			continue;
5461176c39afSDaniel Lezcano 		addrconf_ifdown(dev, 1);
5462176c39afSDaniel Lezcano 	}
5463176c39afSDaniel Lezcano 	addrconf_ifdown(init_net.loopback_dev, 2);
5464176c39afSDaniel Lezcano 
54651da177e4SLinus Torvalds 	/*
54661da177e4SLinus Torvalds 	 *	Check hash table.
54671da177e4SLinus Torvalds 	 */
54685c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
5469c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
5470c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
54715c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
5472c15b1ccaSHannes Frederic Sowa 	cancel_delayed_work(&addr_chk_work);
54731da177e4SLinus Torvalds 	rtnl_unlock();
5474c15b1ccaSHannes Frederic Sowa 
5475c15b1ccaSHannes Frederic Sowa 	destroy_workqueue(addrconf_wq);
54761da177e4SLinus Torvalds }
5477