xref: /openbmc/linux/net/ipv6/addrconf.c (revision 888c848ed34bd5f8cb56567624c0d951ab35174e)
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 
411da177e4SLinus Torvalds #include <linux/errno.h>
421da177e4SLinus Torvalds #include <linux/types.h>
431da177e4SLinus Torvalds #include <linux/socket.h>
441da177e4SLinus Torvalds #include <linux/sockios.h>
451da177e4SLinus Torvalds #include <linux/net.h>
461da177e4SLinus Torvalds #include <linux/in6.h>
471da177e4SLinus Torvalds #include <linux/netdevice.h>
481823730fSThomas Graf #include <linux/if_addr.h>
491da177e4SLinus Torvalds #include <linux/if_arp.h>
501da177e4SLinus Torvalds #include <linux/if_arcnet.h>
511da177e4SLinus Torvalds #include <linux/if_infiniband.h>
521da177e4SLinus Torvalds #include <linux/route.h>
531da177e4SLinus Torvalds #include <linux/inetdevice.h>
541da177e4SLinus Torvalds #include <linux/init.h>
551da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
561da177e4SLinus Torvalds #include <linux/sysctl.h>
571da177e4SLinus Torvalds #endif
584fc268d2SRandy Dunlap #include <linux/capability.h>
591da177e4SLinus Torvalds #include <linux/delay.h>
601da177e4SLinus Torvalds #include <linux/notifier.h>
61543537bdSPaulo Marques #include <linux/string.h>
621da177e4SLinus Torvalds 
63457c4cbcSEric W. Biederman #include <net/net_namespace.h>
641da177e4SLinus Torvalds #include <net/sock.h>
651da177e4SLinus Torvalds #include <net/snmp.h>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #include <net/ipv6.h>
681da177e4SLinus Torvalds #include <net/protocol.h>
691da177e4SLinus Torvalds #include <net/ndisc.h>
701da177e4SLinus Torvalds #include <net/ip6_route.h>
711da177e4SLinus Torvalds #include <net/addrconf.h>
721da177e4SLinus Torvalds #include <net/tcp.h>
731da177e4SLinus Torvalds #include <net/ip.h>
745d620266SThomas Graf #include <net/netlink.h>
75f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
761da177e4SLinus Torvalds #include <linux/if_tunnel.h>
771da177e4SLinus Torvalds #include <linux/rtnetlink.h>
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
801da177e4SLinus Torvalds #include <linux/random.h>
811da177e4SLinus Torvalds #endif
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds #include <asm/uaccess.h>
847f7d9a6bSHerbert Xu #include <asm/unaligned.h>
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds #include <linux/proc_fs.h>
871da177e4SLinus Torvalds #include <linux/seq_file.h>
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds /* Set to 3 to get tracing... */
901da177e4SLinus Torvalds #define ACONF_DEBUG 2
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
931da177e4SLinus Torvalds #define ADBG(x) printk x
941da177e4SLinus Torvalds #else
951da177e4SLinus Torvalds #define ADBG(x)
961da177e4SLinus Torvalds #endif
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
991da177e4SLinus Torvalds #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
102f52295a9SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev);
103408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
104408c4768SPavel Emelyanov #else
105408c4768SPavel Emelyanov static inline void addrconf_sysctl_register(struct inet6_dev *idev)
106408c4768SPavel Emelyanov {
107408c4768SPavel Emelyanov }
108408c4768SPavel Emelyanov 
109408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
110408c4768SPavel Emelyanov {
111408c4768SPavel Emelyanov }
1121da177e4SLinus Torvalds #endif
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1151da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev);
1161da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1171da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data);
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds static int desync_factor = MAX_DESYNC_FACTOR * HZ;
1201da177e4SLinus Torvalds #endif
1211da177e4SLinus Torvalds 
1221b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
1231da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /*
1261da177e4SLinus Torvalds  *	Configured unicast address hash table
1271da177e4SLinus Torvalds  */
1281da177e4SLinus Torvalds static struct inet6_ifaddr		*inet6_addr_lst[IN6_ADDR_HSIZE];
1291da177e4SLinus Torvalds static DEFINE_RWLOCK(addrconf_hash_lock);
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds static void addrconf_verify(unsigned long);
1321da177e4SLinus Torvalds 
1338d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
1341da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1371da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1401da177e4SLinus Torvalds 
141e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
1421da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data);
1431da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
144c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1451da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1461da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1471da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1501da177e4SLinus Torvalds 				struct prefix_info *pinfo);
15106bfe655SDaniel Lezcano static int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
15206bfe655SDaniel Lezcano 			      struct net_device *dev);
1531da177e4SLinus Torvalds 
154e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
1551da177e4SLinus Torvalds 
156*888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1571da177e4SLinus Torvalds 	.forwarding		= 0,
1581da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1591da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1601da177e4SLinus Torvalds 	.accept_ra		= 1,
1611da177e4SLinus Torvalds 	.accept_redirects	= 1,
1621da177e4SLinus Torvalds 	.autoconf		= 1,
1631da177e4SLinus Torvalds 	.force_mld_version	= 0,
1641da177e4SLinus Torvalds 	.dad_transmits		= 1,
1651da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1661da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1671da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1681da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1691da177e4SLinus Torvalds 	.use_tempaddr 		= 0,
1701da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1711da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1721da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1731da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1741da177e4SLinus Torvalds #endif
1751da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
17665f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
177c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
178930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
179930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
18052e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
18109c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
18209c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
18309c884d4SYOSHIFUJI Hideaki #endif
184930d6ff2SYOSHIFUJI Hideaki #endif
185fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
1860bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
187778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
1881b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
1891da177e4SLinus Torvalds };
1901da177e4SLinus Torvalds 
191ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
1921da177e4SLinus Torvalds 	.forwarding		= 0,
1931da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1941da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1951da177e4SLinus Torvalds 	.accept_ra		= 1,
1961da177e4SLinus Torvalds 	.accept_redirects	= 1,
1971da177e4SLinus Torvalds 	.autoconf		= 1,
1981da177e4SLinus Torvalds 	.dad_transmits		= 1,
1991da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2001da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
2011da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2021da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
2031da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2041da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2051da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2061da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2071da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2081da177e4SLinus Torvalds #endif
2091da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
21065f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
211c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
212930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
213930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
21452e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
21509c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
21609c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
21709c884d4SYOSHIFUJI Hideaki #endif
218930d6ff2SYOSHIFUJI Hideaki #endif
219fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2200bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
221778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2221b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
2231da177e4SLinus Torvalds };
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
2261da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
2271da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
228f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
229f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
2301da177e4SLinus Torvalds 
231f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
23205297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev)
233f24e3d65SMitsuru Chinen {
23405297949SDavid S. Miller 	return !qdisc_tx_is_noop(dev);
235f24e3d65SMitsuru Chinen }
236f24e3d65SMitsuru Chinen 
2372b5ead46SYOSHIFUJI Hideaki /* Check if a route is valid prefix route */
2382b5ead46SYOSHIFUJI Hideaki static inline int addrconf_is_prefix_route(const struct rt6_info *rt)
2392b5ead46SYOSHIFUJI Hideaki {
2402b5ead46SYOSHIFUJI Hideaki 	return ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0);
2412b5ead46SYOSHIFUJI Hideaki }
2422b5ead46SYOSHIFUJI Hideaki 
2431da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
2461da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds enum addrconf_timer_t
2501da177e4SLinus Torvalds {
2511da177e4SLinus Torvalds 	AC_NONE,
2521da177e4SLinus Torvalds 	AC_DAD,
2531da177e4SLinus Torvalds 	AC_RS,
2541da177e4SLinus Torvalds };
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
2571da177e4SLinus Torvalds 			       enum addrconf_timer_t what,
2581da177e4SLinus Torvalds 			       unsigned long when)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	if (!del_timer(&ifp->timer))
2611da177e4SLinus Torvalds 		in6_ifa_hold(ifp);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	switch (what) {
2641da177e4SLinus Torvalds 	case AC_DAD:
2651da177e4SLinus Torvalds 		ifp->timer.function = addrconf_dad_timer;
2661da177e4SLinus Torvalds 		break;
2671da177e4SLinus Torvalds 	case AC_RS:
2681da177e4SLinus Torvalds 		ifp->timer.function = addrconf_rs_timer;
2691da177e4SLinus Torvalds 		break;
2701da177e4SLinus Torvalds 	default:;
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 	ifp->timer.expires = jiffies + when;
2731da177e4SLinus Torvalds 	add_timer(&ifp->timer);
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
2767f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
2777f7d9a6bSHerbert Xu {
2787f7d9a6bSHerbert Xu 	if (snmp_mib_init((void **)idev->stats.ipv6,
279c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct ipstats_mib)) < 0)
2807f7d9a6bSHerbert Xu 		goto err_ip;
2817f7d9a6bSHerbert Xu 	if (snmp_mib_init((void **)idev->stats.icmpv6,
282c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct icmpv6_mib)) < 0)
2837f7d9a6bSHerbert Xu 		goto err_icmp;
28414878f75SDavid L Stevens 	if (snmp_mib_init((void **)idev->stats.icmpv6msg,
285c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct icmpv6msg_mib)) < 0)
28614878f75SDavid L Stevens 		goto err_icmpmsg;
2877f7d9a6bSHerbert Xu 
2887f7d9a6bSHerbert Xu 	return 0;
2897f7d9a6bSHerbert Xu 
29014878f75SDavid L Stevens err_icmpmsg:
29114878f75SDavid L Stevens 	snmp_mib_free((void **)idev->stats.icmpv6);
2927f7d9a6bSHerbert Xu err_icmp:
2937f7d9a6bSHerbert Xu 	snmp_mib_free((void **)idev->stats.ipv6);
2947f7d9a6bSHerbert Xu err_ip:
295aaf70ec7SPavel Emelyanov 	return -ENOMEM;
2967f7d9a6bSHerbert Xu }
2977f7d9a6bSHerbert Xu 
29816910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev)
2997f7d9a6bSHerbert Xu {
30014878f75SDavid L Stevens 	snmp_mib_free((void **)idev->stats.icmpv6msg);
3017f7d9a6bSHerbert Xu 	snmp_mib_free((void **)idev->stats.icmpv6);
3027f7d9a6bSHerbert Xu 	snmp_mib_free((void **)idev->stats.ipv6);
3037f7d9a6bSHerbert Xu }
3047f7d9a6bSHerbert Xu 
3051da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */
3061da177e4SLinus Torvalds 
3078814c4b5SYOSHIFUJI Hideaki static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
3088814c4b5SYOSHIFUJI Hideaki {
3098814c4b5SYOSHIFUJI Hideaki 	struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
3108814c4b5SYOSHIFUJI Hideaki 	kfree(idev);
3118814c4b5SYOSHIFUJI Hideaki }
3128814c4b5SYOSHIFUJI Hideaki 
3131da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev)
3141da177e4SLinus Torvalds {
3151da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3161da177e4SLinus Torvalds 	BUG_TRAP(idev->addr_list==NULL);
3171da177e4SLinus Torvalds 	BUG_TRAP(idev->mc_list==NULL);
3181da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
3191da177e4SLinus Torvalds 	printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
3201da177e4SLinus Torvalds #endif
3211da177e4SLinus Torvalds 	dev_put(dev);
3221da177e4SLinus Torvalds 	if (!idev->dead) {
3231da177e4SLinus Torvalds 		printk("Freeing alive inet6 device %p\n", idev);
3241da177e4SLinus Torvalds 		return;
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 	snmp6_free_dev(idev);
3278814c4b5SYOSHIFUJI Hideaki 	call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3307159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy);
3317159039aSYOSHIFUJI Hideaki 
3321da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	struct inet6_dev *ndev;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	ASSERT_RTNL();
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
3391da177e4SLinus Torvalds 		return NULL;
3401da177e4SLinus Torvalds 
341322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
3421da177e4SLinus Torvalds 
343322f74a4SIngo Oeser 	if (ndev == NULL)
344322f74a4SIngo Oeser 		return NULL;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3471da177e4SLinus Torvalds 	ndev->dev = dev;
348c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3491da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3501da177e4SLinus Torvalds 	ndev->cnf.sysctl = NULL;
3511da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
3521da177e4SLinus Torvalds 	if (ndev->nd_parms == NULL) {
3531da177e4SLinus Torvalds 		kfree(ndev);
3541da177e4SLinus Torvalds 		return NULL;
3551da177e4SLinus Torvalds 	}
3560187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
3570187bdfbSBen Hutchings 		dev_disable_lro(dev);
3581da177e4SLinus Torvalds 	/* We refer to the device */
3591da177e4SLinus Torvalds 	dev_hold(dev);
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
3621da177e4SLinus Torvalds 		ADBG((KERN_WARNING
3631da177e4SLinus Torvalds 			"%s(): cannot allocate memory for statistics; dev=%s.\n",
3640dc47877SHarvey Harrison 			__func__, dev->name));
3651da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3661da177e4SLinus Torvalds 		ndev->dead = 1;
3671da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3681da177e4SLinus Torvalds 		return NULL;
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
3721da177e4SLinus Torvalds 		ADBG((KERN_WARNING
3731da177e4SLinus Torvalds 			"%s(): cannot create /proc/net/dev_snmp6/%s\n",
3740dc47877SHarvey Harrison 			__func__, dev->name));
3751da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3761da177e4SLinus Torvalds 		ndev->dead = 1;
3771da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3781da177e4SLinus Torvalds 		return NULL;
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3821da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3831da177e4SLinus Torvalds 	 */
3841da177e4SLinus Torvalds 	in6_dev_hold(ndev);
3851da177e4SLinus Torvalds 
3861b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
3871b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
3881b34be74SYOSHIFUJI Hideaki 
3890be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
390c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
391c7dc89c0SFred L. Templin 		printk(KERN_INFO
392c7dc89c0SFred L. Templin 		       "%s: Disabled Multicast RS\n",
393c7dc89c0SFred L. Templin 		       dev->name);
394c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
395c7dc89c0SFred L. Templin 	}
396b077d7abSYOSHIFUJI Hideaki #endif
397b077d7abSYOSHIFUJI Hideaki 
3981da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
3991da177e4SLinus Torvalds 	setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
4001da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
4011da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4029625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4031da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4041da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4051da177e4SLinus Torvalds 		printk(KERN_INFO
4061da177e4SLinus Torvalds 		       "%s: Disabled Privacy Extensions\n",
4071da177e4SLinus Torvalds 		       dev->name);
4081da177e4SLinus Torvalds 		ndev->cnf.use_tempaddr = -1;
4091da177e4SLinus Torvalds 	} else {
4101da177e4SLinus Torvalds 		in6_dev_hold(ndev);
4111da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds #endif
4141da177e4SLinus Torvalds 
415f24e3d65SMitsuru Chinen 	if (netif_running(dev) && addrconf_qdisc_ok(dev))
41653aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
41753aadcc9SHerbert Xu 
4181da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4191da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
420f52295a9SPavel Emelyanov 	addrconf_sysctl_register(ndev);
42130c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
42230c4cf57SDavid L Stevens 	rcu_assign_pointer(dev->ip6_ptr, ndev);
423d88ae4ccSYOSHIFUJI Hideaki 
424d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
425f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
426d88ae4ccSYOSHIFUJI Hideaki 
4271da177e4SLinus Torvalds 	return ndev;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
430cee89473SYOSHIFUJI Hideaki static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds 	struct inet6_dev *idev;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	ASSERT_RTNL();
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL) {
4371da177e4SLinus Torvalds 		if ((idev = ipv6_add_dev(dev)) == NULL)
4381da177e4SLinus Torvalds 			return NULL;
4391da177e4SLinus Torvalds 	}
440c5e33bddSYOSHIFUJI Hideaki 
4411da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4421da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4431da177e4SLinus Torvalds 	return idev;
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
4471da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	struct net_device *dev;
4501da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	if (!idev)
4531da177e4SLinus Torvalds 		return;
4541da177e4SLinus Torvalds 	dev = idev->dev;
4550187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
4560187bdfbSBen Hutchings 		dev_disable_lro(dev);
4571da177e4SLinus Torvalds 	if (dev && (dev->flags & IFF_MULTICAST)) {
4581da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
459f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
4601da177e4SLinus Torvalds 		else
461f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 	for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
4642c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
4652c12a74cSMichal Wrobel 			continue;
4661da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
4671da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
4681da177e4SLinus Torvalds 		else
4691da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 
474e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds 	struct net_device *dev;
4771da177e4SLinus Torvalds 	struct inet6_dev *idev;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
480bff16c2fSPavel Emelyanov 	for_each_netdev(net, dev) {
4818814c4b5SYOSHIFUJI Hideaki 		rcu_read_lock();
4821da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
4831da177e4SLinus Torvalds 		if (idev) {
484e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
485e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
4861da177e4SLinus Torvalds 			if (changed)
4871da177e4SLinus Torvalds 				dev_forward_change(idev);
4881da177e4SLinus Torvalds 		}
4898814c4b5SYOSHIFUJI Hideaki 		rcu_read_unlock();
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
4921da177e4SLinus Torvalds }
493c8fecf22SPavel Emelyanov 
494c8fecf22SPavel Emelyanov static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
495c8fecf22SPavel Emelyanov {
496bff16c2fSPavel Emelyanov 	struct net *net;
497bff16c2fSPavel Emelyanov 
498bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
499441fc2a2SPavel Emelyanov 	if (p == &net->ipv6.devconf_dflt->forwarding)
500c8fecf22SPavel Emelyanov 		return;
501c8fecf22SPavel Emelyanov 
5020187bdfbSBen Hutchings 	rtnl_lock();
503e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
504e186932bSPavel Emelyanov 		__s32 newf = net->ipv6.devconf_all->forwarding;
505e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
506e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
507c8fecf22SPavel Emelyanov 	} else if ((!*p) ^ (!old))
508c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
5090187bdfbSBen Hutchings 	rtnl_unlock();
510c8fecf22SPavel Emelyanov 
511c8fecf22SPavel Emelyanov 	if (*p)
5127b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
513c8fecf22SPavel Emelyanov }
5141da177e4SLinus Torvalds #endif
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds /* Nobody refers to this ifaddr, destroy it */
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
5191da177e4SLinus Torvalds {
5201da177e4SLinus Torvalds 	BUG_TRAP(ifp->if_next==NULL);
5211da177e4SLinus Torvalds 	BUG_TRAP(ifp->lst_next==NULL);
5221da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
5231da177e4SLinus Torvalds 	printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
5241da177e4SLinus Torvalds #endif
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
5291da177e4SLinus Torvalds 		printk("Timer is still running, when freeing ifa=%p\n", ifp);
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	if (!ifp->dead) {
5321da177e4SLinus Torvalds 		printk("Freeing alive inet6 address %p\n", ifp);
5331da177e4SLinus Torvalds 		return;
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds 	dst_release(&ifp->rt->u.dst);
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	kfree(ifp);
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
540e55ffac6SBrian Haley static void
541e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
542e55ffac6SBrian Haley {
543e55ffac6SBrian Haley 	struct inet6_ifaddr *ifa, **ifap;
5448a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
545e55ffac6SBrian Haley 
546e55ffac6SBrian Haley 	/*
547e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
548e55ffac6SBrian Haley 	 * global before linklocal.
549e55ffac6SBrian Haley 	 */
550e55ffac6SBrian Haley 	for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
551e55ffac6SBrian Haley 	     ifap = &ifa->if_next) {
5528a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
553e55ffac6SBrian Haley 			break;
554e55ffac6SBrian Haley 	}
555e55ffac6SBrian Haley 
556e55ffac6SBrian Haley 	ifp->if_next = *ifap;
557e55ffac6SBrian Haley 	*ifap = ifp;
558e55ffac6SBrian Haley }
559e55ffac6SBrian Haley 
5603eb84f49SYOSHIFUJI Hideaki /*
5613eb84f49SYOSHIFUJI Hideaki  *	Hash function taken from net_alias.c
5623eb84f49SYOSHIFUJI Hideaki  */
5633eb84f49SYOSHIFUJI Hideaki static u8 ipv6_addr_hash(const struct in6_addr *addr)
5643eb84f49SYOSHIFUJI Hideaki {
5653eb84f49SYOSHIFUJI Hideaki 	__u32 word;
5663eb84f49SYOSHIFUJI Hideaki 
5673eb84f49SYOSHIFUJI Hideaki 	/*
5683eb84f49SYOSHIFUJI Hideaki 	 * We perform the hash function over the last 64 bits of the address
5693eb84f49SYOSHIFUJI Hideaki 	 * This will include the IEEE address token on links that support it.
5703eb84f49SYOSHIFUJI Hideaki 	 */
5713eb84f49SYOSHIFUJI Hideaki 
5723eb84f49SYOSHIFUJI Hideaki 	word = (__force u32)(addr->s6_addr32[2] ^ addr->s6_addr32[3]);
5733eb84f49SYOSHIFUJI Hideaki 	word ^= (word >> 16);
5743eb84f49SYOSHIFUJI Hideaki 	word ^= (word >> 8);
5753eb84f49SYOSHIFUJI Hideaki 
5763eb84f49SYOSHIFUJI Hideaki 	return ((word ^ (word >> 4)) & 0x0f);
5773eb84f49SYOSHIFUJI Hideaki }
5783eb84f49SYOSHIFUJI Hideaki 
5791da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds static struct inet6_ifaddr *
5821da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
583e431b8c0SJamal Hadi Salim 	      int scope, u32 flags)
5841da177e4SLinus Torvalds {
5851da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
5861da177e4SLinus Torvalds 	struct rt6_info *rt;
5871da177e4SLinus Torvalds 	int hash;
5881da177e4SLinus Torvalds 	int err = 0;
589d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
590d68b8270SYOSHIFUJI Hideaki 
591d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
592d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
593d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
594d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
595d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
5961da177e4SLinus Torvalds 
5978814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
5981da177e4SLinus Torvalds 	if (idev->dead) {
5991da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
6001da177e4SLinus Torvalds 		goto out2;
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	write_lock(&addrconf_hash_lock);
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
606c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
6071da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: already assigned\n"));
6081da177e4SLinus Torvalds 		err = -EEXIST;
6091da177e4SLinus Torvalds 		goto out;
6101da177e4SLinus Torvalds 	}
6111da177e4SLinus Torvalds 
612322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	if (ifa == NULL) {
6151da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: malloc failed\n"));
6161da177e4SLinus Torvalds 		err = -ENOBUFS;
6171da177e4SLinus Torvalds 		goto out;
6181da177e4SLinus Torvalds 	}
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	rt = addrconf_dst_alloc(idev, addr, 0);
6211da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
6221da177e4SLinus Torvalds 		err = PTR_ERR(rt);
6231da177e4SLinus Torvalds 		goto out;
6241da177e4SLinus Torvalds 	}
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	ipv6_addr_copy(&ifa->addr, addr);
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
6291da177e4SLinus Torvalds 	init_timer(&ifa->timer);
6301da177e4SLinus Torvalds 	ifa->timer.data = (unsigned long) ifa;
6311da177e4SLinus Torvalds 	ifa->scope = scope;
6321da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
6331da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
6341da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
6351da177e4SLinus Torvalds 
63657f5f544SKeir Fraser 	ifa->rt = rt;
63757f5f544SKeir Fraser 
63895c385b4SNeil Horman 	/*
63995c385b4SNeil Horman 	 * part one of RFC 4429, section 3.3
64095c385b4SNeil Horman 	 * We should not configure an address as
64195c385b4SNeil Horman 	 * optimistic if we do not yet know the link
64295c385b4SNeil Horman 	 * layer address of our nexhop router
64395c385b4SNeil Horman 	 */
64495c385b4SNeil Horman 
64595c385b4SNeil Horman 	if (rt->rt6i_nexthop == NULL)
64695c385b4SNeil Horman 		ifa->flags &= ~IFA_F_OPTIMISTIC;
64795c385b4SNeil Horman 
6481da177e4SLinus Torvalds 	ifa->idev = idev;
6491da177e4SLinus Torvalds 	in6_dev_hold(idev);
6501da177e4SLinus Torvalds 	/* For caller */
6511da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	/* Add to big hash table */
6541da177e4SLinus Torvalds 	hash = ipv6_addr_hash(addr);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	ifa->lst_next = inet6_addr_lst[hash];
6571da177e4SLinus Torvalds 	inet6_addr_lst[hash] = ifa;
6581da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6591da177e4SLinus Torvalds 	write_unlock(&addrconf_hash_lock);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	write_lock(&idev->lock);
6621da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
663e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
6661da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
6671da177e4SLinus Torvalds 		ifa->tmp_next = idev->tempaddr_list;
6681da177e4SLinus Torvalds 		idev->tempaddr_list = ifa;
6691da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
6701da177e4SLinus Torvalds 	}
6711da177e4SLinus Torvalds #endif
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6741da177e4SLinus Torvalds 	write_unlock(&idev->lock);
6751da177e4SLinus Torvalds out2:
6768814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
6771da177e4SLinus Torvalds 
678fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
679e041c683SAlan Stern 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
6801da177e4SLinus Torvalds 	else {
6811da177e4SLinus Torvalds 		kfree(ifa);
6821da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
6831da177e4SLinus Torvalds 	}
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	return ifa;
6861da177e4SLinus Torvalds out:
6871da177e4SLinus Torvalds 	write_unlock(&addrconf_hash_lock);
6881da177e4SLinus Torvalds 	goto out2;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
6941da177e4SLinus Torvalds {
6951da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **ifap;
6961da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
6971da177e4SLinus Torvalds 	int hash;
6981da177e4SLinus Torvalds 	int deleted = 0, onlink = 0;
6991da177e4SLinus Torvalds 	unsigned long expires = jiffies;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	hash = ipv6_addr_hash(&ifp->addr);
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	ifp->dead = 1;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
7061da177e4SLinus Torvalds 	for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
7071da177e4SLinus Torvalds 	     ifap = &ifa->lst_next) {
7081da177e4SLinus Torvalds 		if (ifa == ifp) {
7091da177e4SLinus Torvalds 			*ifap = ifa->lst_next;
7101da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
7111da177e4SLinus Torvalds 			ifa->lst_next = NULL;
7121da177e4SLinus Torvalds 			break;
7131da177e4SLinus Torvalds 		}
7141da177e4SLinus Torvalds 	}
7151da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
7181da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
7191da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
7201da177e4SLinus Torvalds 		for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
7211da177e4SLinus Torvalds 		     ifap = &ifa->tmp_next) {
7221da177e4SLinus Torvalds 			if (ifa == ifp) {
7231da177e4SLinus Torvalds 				*ifap = ifa->tmp_next;
7241da177e4SLinus Torvalds 				if (ifp->ifpub) {
7251da177e4SLinus Torvalds 					in6_ifa_put(ifp->ifpub);
7261da177e4SLinus Torvalds 					ifp->ifpub = NULL;
7271da177e4SLinus Torvalds 				}
7281da177e4SLinus Torvalds 				__in6_ifa_put(ifp);
7291da177e4SLinus Torvalds 				ifa->tmp_next = NULL;
7301da177e4SLinus Torvalds 				break;
7311da177e4SLinus Torvalds 			}
7321da177e4SLinus Torvalds 		}
7331da177e4SLinus Torvalds 	}
7341da177e4SLinus Torvalds #endif
7351da177e4SLinus Torvalds 
7361d142804SKristian Slavov 	for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
7371da177e4SLinus Torvalds 		if (ifa == ifp) {
7381da177e4SLinus Torvalds 			*ifap = ifa->if_next;
7391da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
7401da177e4SLinus Torvalds 			ifa->if_next = NULL;
7411da177e4SLinus Torvalds 			if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
7421da177e4SLinus Torvalds 				break;
7431da177e4SLinus Torvalds 			deleted = 1;
7441d142804SKristian Slavov 			continue;
7451da177e4SLinus Torvalds 		} else if (ifp->flags & IFA_F_PERMANENT) {
7461da177e4SLinus Torvalds 			if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
7471da177e4SLinus Torvalds 					      ifp->prefix_len)) {
7481da177e4SLinus Torvalds 				if (ifa->flags & IFA_F_PERMANENT) {
7491da177e4SLinus Torvalds 					onlink = 1;
7501da177e4SLinus Torvalds 					if (deleted)
7511da177e4SLinus Torvalds 						break;
7521da177e4SLinus Torvalds 				} else {
7531da177e4SLinus Torvalds 					unsigned long lifetime;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 					if (!onlink)
7561da177e4SLinus Torvalds 						onlink = -1;
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 					spin_lock(&ifa->lock);
7594bed72e4SYOSHIFUJI Hideaki 
7604bed72e4SYOSHIFUJI Hideaki 					lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
7614bed72e4SYOSHIFUJI Hideaki 					/*
7624bed72e4SYOSHIFUJI Hideaki 					 * Note: Because this address is
7634bed72e4SYOSHIFUJI Hideaki 					 * not permanent, lifetime <
7644bed72e4SYOSHIFUJI Hideaki 					 * LONG_MAX / HZ here.
7654bed72e4SYOSHIFUJI Hideaki 					 */
7661da177e4SLinus Torvalds 					if (time_before(expires,
7671da177e4SLinus Torvalds 							ifa->tstamp + lifetime * HZ))
7681da177e4SLinus Torvalds 						expires = ifa->tstamp + lifetime * HZ;
7691da177e4SLinus Torvalds 					spin_unlock(&ifa->lock);
7701da177e4SLinus Torvalds 				}
7711da177e4SLinus Torvalds 			}
7721da177e4SLinus Torvalds 		}
7731d142804SKristian Slavov 		ifap = &ifa->if_next;
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
7761da177e4SLinus Torvalds 
777b2238566SAndrey Vagin 	addrconf_del_timer(ifp);
778b2238566SAndrey Vagin 
7791da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
7801da177e4SLinus Torvalds 
781e041c683SAlan Stern 	atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	/*
7841da177e4SLinus Torvalds 	 * Purge or update corresponding prefix
7851da177e4SLinus Torvalds 	 *
7861da177e4SLinus Torvalds 	 * 1) we don't purge prefix here if address was not permanent.
7871da177e4SLinus Torvalds 	 *    prefix is managed by its own lifetime.
7881da177e4SLinus Torvalds 	 * 2) if there're no addresses, delete prefix.
7891da177e4SLinus Torvalds 	 * 3) if there're still other permanent address(es),
7901da177e4SLinus Torvalds 	 *    corresponding prefix is still permanent.
7911da177e4SLinus Torvalds 	 * 4) otherwise, update prefix lifetime to the
7921da177e4SLinus Torvalds 	 *    longest valid lifetime among the corresponding
7931da177e4SLinus Torvalds 	 *    addresses on the device.
7941da177e4SLinus Torvalds 	 *    Note: subsequent RA will update lifetime.
7951da177e4SLinus Torvalds 	 *
7961da177e4SLinus Torvalds 	 * --yoshfuji
7971da177e4SLinus Torvalds 	 */
7981da177e4SLinus Torvalds 	if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
7991da177e4SLinus Torvalds 		struct in6_addr prefix;
8001da177e4SLinus Torvalds 		struct rt6_info *rt;
801c346dca1SYOSHIFUJI Hideaki 		struct net *net = dev_net(ifp->idev->dev);
8021da177e4SLinus Torvalds 		ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
8036fda7350SBenjamin Thery 		rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1);
8041da177e4SLinus Torvalds 
8052b5ead46SYOSHIFUJI Hideaki 		if (rt && addrconf_is_prefix_route(rt)) {
8061da177e4SLinus Torvalds 			if (onlink == 0) {
807e0a1ad73SThomas Graf 				ip6_del_rt(rt);
8081da177e4SLinus Torvalds 				rt = NULL;
8091da177e4SLinus Torvalds 			} else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
8101da177e4SLinus Torvalds 				rt->rt6i_expires = expires;
8111da177e4SLinus Torvalds 				rt->rt6i_flags |= RTF_EXPIRES;
8121da177e4SLinus Torvalds 			}
8131da177e4SLinus Torvalds 		}
8141da177e4SLinus Torvalds 		dst_release(&rt->u.dst);
8151da177e4SLinus Torvalds 	}
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	in6_ifa_put(ifp);
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
8211da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
8241da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
8251da177e4SLinus Torvalds 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
826eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
8271da177e4SLinus Torvalds 	int tmp_plen;
8281da177e4SLinus Torvalds 	int ret = 0;
8291da177e4SLinus Torvalds 	int max_addresses;
83095c385b4SNeil Horman 	u32 addr_flags;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	write_lock(&idev->lock);
8331da177e4SLinus Torvalds 	if (ift) {
8341da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
8351da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
8361da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
8371da177e4SLinus Torvalds 		tmpaddr = &addr;
8381da177e4SLinus Torvalds 	} else {
8391da177e4SLinus Torvalds 		tmpaddr = NULL;
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds retry:
8421da177e4SLinus Torvalds 	in6_dev_hold(idev);
8431da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
8441da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8451da177e4SLinus Torvalds 		printk(KERN_INFO
8461da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
8471da177e4SLinus Torvalds 		in6_dev_put(idev);
8481da177e4SLinus Torvalds 		ret = -1;
8491da177e4SLinus Torvalds 		goto out;
8501da177e4SLinus Torvalds 	}
8511da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
8521da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
8531da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
8541da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
8551da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8561da177e4SLinus Torvalds 		printk(KERN_WARNING
8571da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
8581da177e4SLinus Torvalds 		in6_dev_put(idev);
8591da177e4SLinus Torvalds 		ret = -1;
8601da177e4SLinus Torvalds 		goto out;
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
8631da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
8641da177e4SLinus Torvalds 	if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
8651da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
8661da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8671da177e4SLinus Torvalds 		printk(KERN_WARNING
8681da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
8691da177e4SLinus Torvalds 		in6_ifa_put(ifp);
8701da177e4SLinus Torvalds 		in6_dev_put(idev);
8711da177e4SLinus Torvalds 		ret = -1;
8721da177e4SLinus Torvalds 		goto out;
8731da177e4SLinus Torvalds 	}
8741da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
8751da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
8761da177e4SLinus Torvalds 			      ifp->valid_lft,
8771da177e4SLinus Torvalds 			      idev->cnf.temp_valid_lft);
8781da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
8791da177e4SLinus Torvalds 				 ifp->prefered_lft,
8801da177e4SLinus Torvalds 				 idev->cnf.temp_prefered_lft - desync_factor / HZ);
8811da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
8821da177e4SLinus Torvalds 	max_addresses = idev->cnf.max_addresses;
8831da177e4SLinus Torvalds 	tmp_cstamp = ifp->cstamp;
8841da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
8851da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
8861da177e4SLinus Torvalds 
887eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
888eac55bf9SBenoit Boissinot 	                idev->cnf.dad_transmits *
889eac55bf9SBenoit Boissinot 	                idev->nd_parms->retrans_time / HZ;
8901da177e4SLinus Torvalds 	write_unlock(&idev->lock);
89195c385b4SNeil Horman 
892eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
893eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
894eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
895eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
896eac55bf9SBenoit Boissinot 	 */
897eac55bf9SBenoit Boissinot 	if (tmp_prefered_lft <= regen_advance) {
898eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
899eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
900eac55bf9SBenoit Boissinot 		ret = -1;
901eac55bf9SBenoit Boissinot 		goto out;
902eac55bf9SBenoit Boissinot 	}
903eac55bf9SBenoit Boissinot 
90495c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
90595c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
90695c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
90795c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
90895c385b4SNeil Horman 
9091da177e4SLinus Torvalds 	ift = !max_addresses ||
9101da177e4SLinus Torvalds 	      ipv6_count_addresses(idev) < max_addresses ?
9111da177e4SLinus Torvalds 		ipv6_add_addr(idev, &addr, tmp_plen,
91295c385b4SNeil Horman 			      ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
91395c385b4SNeil Horman 			      addr_flags) : NULL;
9141da177e4SLinus Torvalds 	if (!ift || IS_ERR(ift)) {
9151da177e4SLinus Torvalds 		in6_ifa_put(ifp);
9161da177e4SLinus Torvalds 		in6_dev_put(idev);
9171da177e4SLinus Torvalds 		printk(KERN_INFO
9181da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): retry temporary address regeneration.\n");
9191da177e4SLinus Torvalds 		tmpaddr = &addr;
9201da177e4SLinus Torvalds 		write_lock(&idev->lock);
9211da177e4SLinus Torvalds 		goto retry;
9221da177e4SLinus Torvalds 	}
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
9251da177e4SLinus Torvalds 	ift->ifpub = ifp;
9261da177e4SLinus Torvalds 	ift->valid_lft = tmp_valid_lft;
9271da177e4SLinus Torvalds 	ift->prefered_lft = tmp_prefered_lft;
9281da177e4SLinus Torvalds 	ift->cstamp = tmp_cstamp;
9291da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
9301da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	addrconf_dad_start(ift, 0);
9331da177e4SLinus Torvalds 	in6_ifa_put(ift);
9341da177e4SLinus Torvalds 	in6_dev_put(idev);
9351da177e4SLinus Torvalds out:
9361da177e4SLinus Torvalds 	return ret;
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds #endif
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds /*
941072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
9421da177e4SLinus Torvalds  */
943a9b05723SYOSHIFUJI Hideaki enum {
944a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
945a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
946a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
947a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
948a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
949a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
950a9b05723SYOSHIFUJI Hideaki #endif
951a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
952a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
953a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY
954a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
955a9b05723SYOSHIFUJI Hideaki #endif
956a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
957a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
958a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
959072047e4SYOSHIFUJI Hideaki };
960072047e4SYOSHIFUJI Hideaki 
961a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
962a9b05723SYOSHIFUJI Hideaki 	int			rule;
963a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
964a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
965a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
966a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
967a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
968a9b05723SYOSHIFUJI Hideaki };
969a9b05723SYOSHIFUJI Hideaki 
970a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
9719acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
972a9b05723SYOSHIFUJI Hideaki 	int ifindex;
973a9b05723SYOSHIFUJI Hideaki 	int scope;
974a9b05723SYOSHIFUJI Hideaki 	int label;
9757cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
976a9b05723SYOSHIFUJI Hideaki };
977072047e4SYOSHIFUJI Hideaki 
978b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
9791da177e4SLinus Torvalds {
980072047e4SYOSHIFUJI Hideaki 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
981072047e4SYOSHIFUJI Hideaki 		    IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
982072047e4SYOSHIFUJI Hideaki 		return 1;
983072047e4SYOSHIFUJI Hideaki 	return 0;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
9863de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
9873de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
988a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
989a9b05723SYOSHIFUJI Hideaki 			       int i)
990a9b05723SYOSHIFUJI Hideaki {
991a9b05723SYOSHIFUJI Hideaki 	int ret;
992a9b05723SYOSHIFUJI Hideaki 
993a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
994a9b05723SYOSHIFUJI Hideaki 		switch (i) {
995a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
996a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
997a9b05723SYOSHIFUJI Hideaki 			break;
998a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
999a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1000a9b05723SYOSHIFUJI Hideaki 			break;
1001a9b05723SYOSHIFUJI Hideaki 		default:
1002a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1003a9b05723SYOSHIFUJI Hideaki 		}
1004a9b05723SYOSHIFUJI Hideaki 		goto out;
1005a9b05723SYOSHIFUJI Hideaki 	}
1006a9b05723SYOSHIFUJI Hideaki 
1007a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1008a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1009a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1010a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1011a9b05723SYOSHIFUJI Hideaki 		break;
1012a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1013a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1014a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1015a9b05723SYOSHIFUJI Hideaki 		break;
1016a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1017a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1018a9b05723SYOSHIFUJI Hideaki 		 *
1019a9b05723SYOSHIFUJI Hideaki 		 *      ret
1020a9b05723SYOSHIFUJI Hideaki 		 *       ^
1021a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1022a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1023a9b05723SYOSHIFUJI Hideaki 		 *       |
1024a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1025a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1026a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1027a9b05723SYOSHIFUJI Hideaki 		 *  B-15 |    \        if scope is enough for destinaion.
1028a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1029a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1030a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1031a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1032a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1033a9b05723SYOSHIFUJI Hideaki 		 *
1034a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1035a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1036a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1037a9b05723SYOSHIFUJI Hideaki 		 */
1038a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1039a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1040a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1041a9b05723SYOSHIFUJI Hideaki 		else
1042a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1043a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1044a9b05723SYOSHIFUJI Hideaki 		break;
1045a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
1046a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
1047a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
1048a9b05723SYOSHIFUJI Hideaki 		      !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1049a9b05723SYOSHIFUJI Hideaki 		break;
1050a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1051a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
10527cbca67cSYOSHIFUJI Hideaki 	    {
1053a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
10547cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
10557cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1056a9b05723SYOSHIFUJI Hideaki 		break;
10577cbca67cSYOSHIFUJI Hideaki 	    }
1058a9b05723SYOSHIFUJI Hideaki #endif
1059a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1060a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1061a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1062a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1063a9b05723SYOSHIFUJI Hideaki 		break;
1064a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1065a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
10663de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
10673de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1068a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1069a9b05723SYOSHIFUJI Hideaki 		break;
1070a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY
1071a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
10727cbca67cSYOSHIFUJI Hideaki 	    {
1073a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
1074a9b05723SYOSHIFUJI Hideaki 		 * Note: prefer temprary address if use_tempaddr >= 2
1075a9b05723SYOSHIFUJI Hideaki 		 */
10767cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
10777cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
10787cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
10797cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1080a9b05723SYOSHIFUJI Hideaki 		break;
10817cbca67cSYOSHIFUJI Hideaki 	    }
1082a9b05723SYOSHIFUJI Hideaki #endif
1083a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1084a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1085a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1086a9b05723SYOSHIFUJI Hideaki 		 */
1087a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1088a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1089a9b05723SYOSHIFUJI Hideaki 		break;
1090a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1091a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
1092a9b05723SYOSHIFUJI Hideaki 		score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
1093a9b05723SYOSHIFUJI Hideaki 						       dst->addr);
1094a9b05723SYOSHIFUJI Hideaki 		break;
1095a9b05723SYOSHIFUJI Hideaki 	default:
1096a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1097a9b05723SYOSHIFUJI Hideaki 	}
1098a9b05723SYOSHIFUJI Hideaki 
1099a9b05723SYOSHIFUJI Hideaki 	if (ret)
1100a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1101a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1102a9b05723SYOSHIFUJI Hideaki out:
1103a9b05723SYOSHIFUJI Hideaki 	return ret;
1104a9b05723SYOSHIFUJI Hideaki }
1105a9b05723SYOSHIFUJI Hideaki 
1106a9b05723SYOSHIFUJI Hideaki int ipv6_dev_get_saddr(struct net_device *dst_dev,
11079acd9f3aSYOSHIFUJI Hideaki 		       const struct in6_addr *daddr, unsigned int prefs,
11087cbca67cSYOSHIFUJI Hideaki 		       struct in6_addr *saddr)
11091da177e4SLinus Torvalds {
1110a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_score scores[2],
1111a9b05723SYOSHIFUJI Hideaki 				*score = &scores[0], *hiscore = &scores[1];
1112c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dst_dev);
1113a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_dst dst;
1114072047e4SYOSHIFUJI Hideaki 	struct net_device *dev;
1115a9b05723SYOSHIFUJI Hideaki 	int dst_type;
11161da177e4SLinus Torvalds 
1117a9b05723SYOSHIFUJI Hideaki 	dst_type = __ipv6_addr_type(daddr);
1118a9b05723SYOSHIFUJI Hideaki 	dst.addr = daddr;
1119a9b05723SYOSHIFUJI Hideaki 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1120a9b05723SYOSHIFUJI Hideaki 	dst.scope = __ipv6_addr_src_scope(dst_type);
11213de23255SBenjamin Thery 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
11227cbca67cSYOSHIFUJI Hideaki 	dst.prefs = prefs;
1123a9b05723SYOSHIFUJI Hideaki 
1124a9b05723SYOSHIFUJI Hideaki 	hiscore->rule = -1;
1125a9b05723SYOSHIFUJI Hideaki 	hiscore->ifa = NULL;
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
11288814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1129072047e4SYOSHIFUJI Hideaki 
11306fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
1131072047e4SYOSHIFUJI Hideaki 		struct inet6_dev *idev;
1132072047e4SYOSHIFUJI Hideaki 
1133a9b05723SYOSHIFUJI Hideaki 		/* Candidate Source Address (section 4)
1134072047e4SYOSHIFUJI Hideaki 		 *  - multicast and link-local destination address,
1135072047e4SYOSHIFUJI Hideaki 		 *    the set of candidate source address MUST only
1136072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1137072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same link as the outgoing
1138072047e4SYOSHIFUJI Hideaki 		 *    interface.
1139072047e4SYOSHIFUJI Hideaki 		 * (- For site-local destination addresses, the
1140072047e4SYOSHIFUJI Hideaki 		 *    set of candidate source addresses MUST only
1141072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1142072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same site as the outgoing
1143072047e4SYOSHIFUJI Hideaki 		 *    interface.)
1144072047e4SYOSHIFUJI Hideaki 		 */
1145a9b05723SYOSHIFUJI Hideaki 		if (((dst_type & IPV6_ADDR_MULTICAST) ||
1146a9b05723SYOSHIFUJI Hideaki 		     dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1147a9b05723SYOSHIFUJI Hideaki 		    dst.ifindex && dev->ifindex != dst.ifindex)
1148072047e4SYOSHIFUJI Hideaki 			continue;
1149072047e4SYOSHIFUJI Hideaki 
11501da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
1151072047e4SYOSHIFUJI Hideaki 		if (!idev)
1152072047e4SYOSHIFUJI Hideaki 			continue;
1153072047e4SYOSHIFUJI Hideaki 
11541da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1155a9b05723SYOSHIFUJI Hideaki 		for (score->ifa = idev->addr_list; score->ifa; score->ifa = score->ifa->if_next) {
1156a9b05723SYOSHIFUJI Hideaki 			int i;
1157072047e4SYOSHIFUJI Hideaki 
1158a9b05723SYOSHIFUJI Hideaki 			/*
11596b3ae80aSYOSHIFUJI Hideaki 			 * - Tentative Address (RFC2462 section 5.4)
11606b3ae80aSYOSHIFUJI Hideaki 			 *  - A tentative address is not considered
11616b3ae80aSYOSHIFUJI Hideaki 			 *    "assigned to an interface" in the traditional
116295c385b4SNeil Horman 			 *    sense, unless it is also flagged as optimistic.
11636b3ae80aSYOSHIFUJI Hideaki 			 * - Candidate Source Address (section 4)
1164072047e4SYOSHIFUJI Hideaki 			 *  - In any case, anycast addresses, multicast
1165072047e4SYOSHIFUJI Hideaki 			 *    addresses, and the unspecified address MUST
1166072047e4SYOSHIFUJI Hideaki 			 *    NOT be included in a candidate set.
1167072047e4SYOSHIFUJI Hideaki 			 */
1168a9b05723SYOSHIFUJI Hideaki 			if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1169a9b05723SYOSHIFUJI Hideaki 			    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
11706b3ae80aSYOSHIFUJI Hideaki 				continue;
1171a9b05723SYOSHIFUJI Hideaki 
1172a9b05723SYOSHIFUJI Hideaki 			score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1173a9b05723SYOSHIFUJI Hideaki 
1174a9b05723SYOSHIFUJI Hideaki 			if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1175a9b05723SYOSHIFUJI Hideaki 				     score->addr_type & IPV6_ADDR_MULTICAST)) {
1176072047e4SYOSHIFUJI Hideaki 				LIMIT_NETDEBUG(KERN_DEBUG
1177072047e4SYOSHIFUJI Hideaki 					       "ADDRCONF: unspecified / multicast address "
1178072047e4SYOSHIFUJI Hideaki 					       "assigned as unicast address on %s",
1179072047e4SYOSHIFUJI Hideaki 					       dev->name);
11801da177e4SLinus Torvalds 				continue;
1181072047e4SYOSHIFUJI Hideaki 			}
1182072047e4SYOSHIFUJI Hideaki 
1183a9b05723SYOSHIFUJI Hideaki 			score->rule = -1;
1184a9b05723SYOSHIFUJI Hideaki 			bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1185072047e4SYOSHIFUJI Hideaki 
1186a9b05723SYOSHIFUJI Hideaki 			for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1187a9b05723SYOSHIFUJI Hideaki 				int minihiscore, miniscore;
1188072047e4SYOSHIFUJI Hideaki 
11893de23255SBenjamin Thery 				minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
11903de23255SBenjamin Thery 				miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1191072047e4SYOSHIFUJI Hideaki 
1192a9b05723SYOSHIFUJI Hideaki 				if (minihiscore > miniscore) {
1193a9b05723SYOSHIFUJI Hideaki 					if (i == IPV6_SADDR_RULE_SCOPE &&
1194a9b05723SYOSHIFUJI Hideaki 					    score->scopedist > 0) {
1195a9b05723SYOSHIFUJI Hideaki 						/*
1196a9b05723SYOSHIFUJI Hideaki 						 * special case:
1197a9b05723SYOSHIFUJI Hideaki 						 * each remaining entry
1198a9b05723SYOSHIFUJI Hideaki 						 * has too small (not enough)
1199a9b05723SYOSHIFUJI Hideaki 						 * scope, because ifa entries
1200a9b05723SYOSHIFUJI Hideaki 						 * are sorted by their scope
1201a9b05723SYOSHIFUJI Hideaki 						 * values.
1202072047e4SYOSHIFUJI Hideaki 						 */
1203a9b05723SYOSHIFUJI Hideaki 						goto try_nextdev;
1204072047e4SYOSHIFUJI Hideaki 					}
1205a9b05723SYOSHIFUJI Hideaki 					break;
1206a9b05723SYOSHIFUJI Hideaki 				} else if (minihiscore < miniscore) {
1207a9b05723SYOSHIFUJI Hideaki 					struct ipv6_saddr_score *tmp;
120899cd07a5SJuha-Matti Tapio 
1209a9b05723SYOSHIFUJI Hideaki 					if (hiscore->ifa)
1210a9b05723SYOSHIFUJI Hideaki 						in6_ifa_put(hiscore->ifa);
121199cd07a5SJuha-Matti Tapio 
1212a9b05723SYOSHIFUJI Hideaki 					in6_ifa_hold(score->ifa);
1213072047e4SYOSHIFUJI Hideaki 
1214a9b05723SYOSHIFUJI Hideaki 					tmp = hiscore;
12151da177e4SLinus Torvalds 					hiscore = score;
1216a9b05723SYOSHIFUJI Hideaki 					score = tmp;
1217a9b05723SYOSHIFUJI Hideaki 
1218a9b05723SYOSHIFUJI Hideaki 					/* restore our iterator */
1219a9b05723SYOSHIFUJI Hideaki 					score->ifa = hiscore->ifa;
1220a9b05723SYOSHIFUJI Hideaki 
1221a9b05723SYOSHIFUJI Hideaki 					break;
12221da177e4SLinus Torvalds 				}
1223a9b05723SYOSHIFUJI Hideaki 			}
1224a9b05723SYOSHIFUJI Hideaki 		}
1225a9b05723SYOSHIFUJI Hideaki try_nextdev:
12261da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
12271da177e4SLinus Torvalds 	}
12288814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
12291da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
12301da177e4SLinus Torvalds 
1231a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1232072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
12331da177e4SLinus Torvalds 
1234a9b05723SYOSHIFUJI Hideaki 	ipv6_addr_copy(saddr, &hiscore->ifa->addr);
1235a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1236072047e4SYOSHIFUJI Hideaki 	return 0;
12371da177e4SLinus Torvalds }
12381da177e4SLinus Torvalds 
12395e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
12401da177e4SLinus Torvalds 
124195c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
124295c385b4SNeil Horman 		    unsigned char banned_flags)
12431da177e4SLinus Torvalds {
12441da177e4SLinus Torvalds 	struct inet6_dev *idev;
12451da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
12461da177e4SLinus Torvalds 
12478814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
12481da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) != NULL) {
12491da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
12521da177e4SLinus Torvalds 		for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
125395c385b4SNeil Horman 			if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
12541da177e4SLinus Torvalds 				ipv6_addr_copy(addr, &ifp->addr);
12551da177e4SLinus Torvalds 				err = 0;
12561da177e4SLinus Torvalds 				break;
12571da177e4SLinus Torvalds 			}
12581da177e4SLinus Torvalds 		}
12591da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
12601da177e4SLinus Torvalds 	}
12618814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
12621da177e4SLinus Torvalds 	return err;
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
12661da177e4SLinus Torvalds {
12671da177e4SLinus Torvalds 	int cnt = 0;
12681da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
12711da177e4SLinus Torvalds 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
12721da177e4SLinus Torvalds 		cnt++;
12731da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
12741da177e4SLinus Torvalds 	return cnt;
12751da177e4SLinus Torvalds }
12761da177e4SLinus Torvalds 
1277bfeade08SDaniel Lezcano int ipv6_chk_addr(struct net *net, struct in6_addr *addr,
1278bfeade08SDaniel Lezcano 		  struct net_device *dev, int strict)
12791da177e4SLinus Torvalds {
12801da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
12811da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
12841da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1285878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1286bfeade08SDaniel Lezcano 			continue;
12871da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
12881da177e4SLinus Torvalds 		    !(ifp->flags&IFA_F_TENTATIVE)) {
12891da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
12901da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
12911da177e4SLinus Torvalds 				break;
12921da177e4SLinus Torvalds 		}
12931da177e4SLinus Torvalds 	}
12941da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
12951da177e4SLinus Torvalds 	return ifp != NULL;
12961da177e4SLinus Torvalds }
12977159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr);
12987159039aSYOSHIFUJI Hideaki 
12991da177e4SLinus Torvalds static
130006bfe655SDaniel Lezcano int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
130106bfe655SDaniel Lezcano 		       struct net_device *dev)
13021da177e4SLinus Torvalds {
13031da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
13041da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1307878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
130806bfe655SDaniel Lezcano 			continue;
13091da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
13101da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev)
13111da177e4SLinus Torvalds 				break;
13121da177e4SLinus Torvalds 		}
13131da177e4SLinus Torvalds 	}
13141da177e4SLinus Torvalds 	return ifp != NULL;
13151da177e4SLinus Torvalds }
13161da177e4SLinus Torvalds 
131752eeeb84SYOSHIFUJI Hideaki int ipv6_chk_prefix(struct in6_addr *addr, struct net_device *dev)
131852eeeb84SYOSHIFUJI Hideaki {
131952eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
132052eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
132152eeeb84SYOSHIFUJI Hideaki 	int	onlink;
132252eeeb84SYOSHIFUJI Hideaki 
132352eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
132452eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
132552eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
132652eeeb84SYOSHIFUJI Hideaki 	if (idev) {
132752eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
132852eeeb84SYOSHIFUJI Hideaki 		for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
132952eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
133052eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
133152eeeb84SYOSHIFUJI Hideaki 			if (onlink)
133252eeeb84SYOSHIFUJI Hideaki 				break;
133352eeeb84SYOSHIFUJI Hideaki 		}
133452eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
133552eeeb84SYOSHIFUJI Hideaki 	}
133652eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
133752eeeb84SYOSHIFUJI Hideaki 	return onlink;
133852eeeb84SYOSHIFUJI Hideaki }
133952eeeb84SYOSHIFUJI Hideaki 
134052eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
134152eeeb84SYOSHIFUJI Hideaki 
13429acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
13431cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
13441da177e4SLinus Torvalds {
13451da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
13461da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
13491da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1350878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
13511cab3da6SDaniel Lezcano 			continue;
13521da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
13531da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
13541da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
13551da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
13561da177e4SLinus Torvalds 				break;
13571da177e4SLinus Torvalds 			}
13581da177e4SLinus Torvalds 		}
13591da177e4SLinus Torvalds 	}
13601da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 	return ifp;
13631da177e4SLinus Torvalds }
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
13661da177e4SLinus Torvalds {
13671da177e4SLinus Torvalds 	const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
13680fa1a53eSArnaldo Carvalho de Melo 	const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
136982103232SAl Viro 	__be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
137082103232SAl Viro 	__be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
13711da177e4SLinus Torvalds 	int sk_ipv6only = ipv6_only_sock(sk);
1372463c84b9SArnaldo Carvalho de Melo 	int sk2_ipv6only = inet_v6_ipv6only(sk2);
13731da177e4SLinus Torvalds 	int addr_type = ipv6_addr_type(sk_rcv_saddr6);
13741da177e4SLinus Torvalds 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	if (!sk2_rcv_saddr && !sk_ipv6only)
13771da177e4SLinus Torvalds 		return 1;
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	if (addr_type2 == IPV6_ADDR_ANY &&
13801da177e4SLinus Torvalds 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
13811da177e4SLinus Torvalds 		return 1;
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_ANY &&
13841da177e4SLinus Torvalds 	    !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
13851da177e4SLinus Torvalds 		return 1;
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	if (sk2_rcv_saddr6 &&
13881da177e4SLinus Torvalds 	    ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
13891da177e4SLinus Torvalds 		return 1;
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED &&
13921da177e4SLinus Torvalds 	    !sk2_ipv6only &&
13931da177e4SLinus Torvalds 	    (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
13941da177e4SLinus Torvalds 		return 1;
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 	return 0;
13971da177e4SLinus Torvalds }
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
14001da177e4SLinus Torvalds 
14019f5336e2SAdrian Bunk static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
14021da177e4SLinus Torvalds {
14031da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
14041da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
14051da177e4SLinus Torvalds 		addrconf_del_timer(ifp);
14061da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
14071da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
14081da177e4SLinus Torvalds 		in6_ifa_put(ifp);
14091da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
14101da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
14111da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
14121da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
14131da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
14141da177e4SLinus Torvalds 		if (ifpub) {
14151da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
14161da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
14171da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
14181da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
14191da177e4SLinus Torvalds 		} else {
14201da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
14211da177e4SLinus Torvalds 		}
14221da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
14231da177e4SLinus Torvalds #endif
14241da177e4SLinus Torvalds 	} else
14251da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
14261da177e4SLinus Torvalds }
14271da177e4SLinus Torvalds 
14283c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
14293c21edbdSYOSHIFUJI Hideaki {
14301b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
14311b34be74SYOSHIFUJI Hideaki 	if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
14321b34be74SYOSHIFUJI Hideaki 		struct in6_addr addr;
14331b34be74SYOSHIFUJI Hideaki 
14341b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[0] = htonl(0xfe800000);
14351b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[1] = 0;
14361b34be74SYOSHIFUJI Hideaki 
14371b34be74SYOSHIFUJI Hideaki 		if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
14381b34be74SYOSHIFUJI Hideaki 		    ipv6_addr_equal(&ifp->addr, &addr)) {
14391b34be74SYOSHIFUJI Hideaki 			/* DAD failed for link-local based on MAC address */
14401b34be74SYOSHIFUJI Hideaki 			idev->cnf.disable_ipv6 = 1;
14411b34be74SYOSHIFUJI Hideaki 		}
14421b34be74SYOSHIFUJI Hideaki 	}
14431b34be74SYOSHIFUJI Hideaki 
14443c21edbdSYOSHIFUJI Hideaki 	if (net_ratelimit())
14453c21edbdSYOSHIFUJI Hideaki 		printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
14463c21edbdSYOSHIFUJI Hideaki 	addrconf_dad_stop(ifp);
14473c21edbdSYOSHIFUJI Hideaki }
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds /* Join to solicited addr multicast group. */
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
14521da177e4SLinus Torvalds {
14531da177e4SLinus Torvalds 	struct in6_addr maddr;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
14561da177e4SLinus Torvalds 		return;
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
14591da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
14601da177e4SLinus Torvalds }
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
14631da177e4SLinus Torvalds {
14641da177e4SLinus Torvalds 	struct in6_addr maddr;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
14671da177e4SLinus Torvalds 		return;
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
14701da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
147320380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
14741da177e4SLinus Torvalds {
14751da177e4SLinus Torvalds 	struct in6_addr addr;
14761da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
14771da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
14781da177e4SLinus Torvalds 		return;
14791da177e4SLinus Torvalds 	ipv6_dev_ac_inc(ifp->idev->dev, &addr);
14801da177e4SLinus Torvalds }
14811da177e4SLinus Torvalds 
148220380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
14831da177e4SLinus Torvalds {
14841da177e4SLinus Torvalds 	struct in6_addr addr;
14851da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
14861da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
14871da177e4SLinus Torvalds 		return;
14881da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds 
1491073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
14921da177e4SLinus Torvalds {
14931da177e4SLinus Torvalds 	if (dev->addr_len != ETH_ALEN)
14941da177e4SLinus Torvalds 		return -1;
14951da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr, 3);
14961da177e4SLinus Torvalds 	memcpy(eui + 5, dev->dev_addr + 3, 3);
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	/*
14991da177e4SLinus Torvalds 	 * The zSeries OSA network cards can be shared among various
15001da177e4SLinus Torvalds 	 * OS instances, but the OSA cards have only one MAC address.
15011da177e4SLinus Torvalds 	 * This leads to duplicate address conflicts in conjunction
15021da177e4SLinus Torvalds 	 * with IPv6 if more than one instance uses the same card.
15031da177e4SLinus Torvalds 	 *
15041da177e4SLinus Torvalds 	 * The driver for these cards can deliver a unique 16-bit
15051da177e4SLinus Torvalds 	 * identifier for each instance sharing the same card.  It is
15061da177e4SLinus Torvalds 	 * placed instead of 0xFFFE in the interface identifier.  The
15071da177e4SLinus Torvalds 	 * "u" bit of the interface identifier is not inverted in this
15081da177e4SLinus Torvalds 	 * case.  Hence the resulting interface identifier has local
15091da177e4SLinus Torvalds 	 * scope according to RFC2373.
15101da177e4SLinus Torvalds 	 */
15111da177e4SLinus Torvalds 	if (dev->dev_id) {
15121da177e4SLinus Torvalds 		eui[3] = (dev->dev_id >> 8) & 0xFF;
15131da177e4SLinus Torvalds 		eui[4] = dev->dev_id & 0xFF;
15141da177e4SLinus Torvalds 	} else {
15151da177e4SLinus Torvalds 		eui[3] = 0xFF;
15161da177e4SLinus Torvalds 		eui[4] = 0xFE;
15171da177e4SLinus Torvalds 		eui[0] ^= 2;
15181da177e4SLinus Torvalds 	}
15191da177e4SLinus Torvalds 	return 0;
1520073a8e0eSYOSHIFUJI Hideaki }
1521073a8e0eSYOSHIFUJI Hideaki 
1522073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1523073a8e0eSYOSHIFUJI Hideaki {
15241da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
15251da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
15261da177e4SLinus Torvalds 		return -1;
15271da177e4SLinus Torvalds 	memset(eui, 0, 7);
15281da177e4SLinus Torvalds 	eui[7] = *(u8*)dev->dev_addr;
15291da177e4SLinus Torvalds 	return 0;
1530073a8e0eSYOSHIFUJI Hideaki }
1531073a8e0eSYOSHIFUJI Hideaki 
1532073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1533073a8e0eSYOSHIFUJI Hideaki {
15341da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
15351da177e4SLinus Torvalds 		return -1;
15361da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
15371da177e4SLinus Torvalds 	eui[0] |= 2;
15381da177e4SLinus Torvalds 	return 0;
15391da177e4SLinus Torvalds }
1540073a8e0eSYOSHIFUJI Hideaki 
1541dfd982baSYOSHIFUJI Hideaki int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1542dfd982baSYOSHIFUJI Hideaki {
1543dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1544dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1545dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1546dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1547dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1548dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1549dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
1550dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
1551dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
1552dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
1553dfd982baSYOSHIFUJI Hideaki 	return 0;
1554dfd982baSYOSHIFUJI Hideaki }
1555dfd982baSYOSHIFUJI Hideaki EXPORT_SYMBOL(__ipv6_isatap_ifid);
1556dfd982baSYOSHIFUJI Hideaki 
1557dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1558dfd982baSYOSHIFUJI Hideaki {
1559dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
1560dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1561dfd982baSYOSHIFUJI Hideaki 	return -1;
1562dfd982baSYOSHIFUJI Hideaki }
1563dfd982baSYOSHIFUJI Hideaki 
1564073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1565073a8e0eSYOSHIFUJI Hideaki {
1566073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
1567073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
1568073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
1569073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_IEEE802_TR:
1570073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
1571073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
1572073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
1573073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
1574073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
1575c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
1576dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
1577073a8e0eSYOSHIFUJI Hideaki 	}
15781da177e4SLinus Torvalds 	return -1;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
15821da177e4SLinus Torvalds {
15831da177e4SLinus Torvalds 	int err = -1;
15841da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
15871da177e4SLinus Torvalds 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
15881da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
15891da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
15901da177e4SLinus Torvalds 			err = 0;
15911da177e4SLinus Torvalds 			break;
15921da177e4SLinus Torvalds 		}
15931da177e4SLinus Torvalds 	}
15941da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
15951da177e4SLinus Torvalds 	return err;
15961da177e4SLinus Torvalds }
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
15991da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
16001da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev)
16011da177e4SLinus Torvalds {
16021da177e4SLinus Torvalds regen:
1603955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
16041da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	/*
16071da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
16081da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
16091da177e4SLinus Torvalds 	 *
16101da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
16111da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
1612c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
16131da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
16141da177e4SLinus Torvalds 	 *  - value 0
16151da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
16161da177e4SLinus Torvalds 	 */
16171da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
16181da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
16191da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
16201da177e4SLinus Torvalds 		goto regen;
16211da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
16221da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
16231da177e4SLinus Torvalds 			goto regen;
16241da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
16251da177e4SLinus Torvalds 			goto regen;
16261da177e4SLinus Torvalds 	}
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds 	return 0;
16291da177e4SLinus Torvalds }
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
16321da177e4SLinus Torvalds {
16331da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
16341da177e4SLinus Torvalds 	unsigned long expires;
16351da177e4SLinus Torvalds 
16368814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
16371da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	if (idev->dead)
16401da177e4SLinus Torvalds 		goto out;
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds 	if (__ipv6_regen_rndid(idev) < 0)
16431da177e4SLinus Torvalds 		goto out;
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	expires = jiffies +
16461da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
16471da177e4SLinus Torvalds 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
16481da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
16491da177e4SLinus Torvalds 		printk(KERN_WARNING
16501da177e4SLinus Torvalds 			"ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
16511da177e4SLinus Torvalds 			idev->dev->name);
16521da177e4SLinus Torvalds 		goto out;
16531da177e4SLinus Torvalds 	}
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
16561da177e4SLinus Torvalds 		in6_dev_hold(idev);
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds out:
16591da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
16608814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
16611da177e4SLinus Torvalds 	in6_dev_put(idev);
16621da177e4SLinus Torvalds }
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
16651da177e4SLinus Torvalds 	int ret = 0;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
16681da177e4SLinus Torvalds 		ret = __ipv6_regen_rndid(idev);
16691da177e4SLinus Torvalds 	return ret;
16701da177e4SLinus Torvalds }
16711da177e4SLinus Torvalds #endif
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds /*
16741da177e4SLinus Torvalds  *	Add prefix route.
16751da177e4SLinus Torvalds  */
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds static void
16781da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1679e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
16801da177e4SLinus Torvalds {
168186872cb5SThomas Graf 	struct fib6_config cfg = {
168286872cb5SThomas Graf 		.fc_table = RT6_TABLE_PREFIX,
168386872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
168486872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
168586872cb5SThomas Graf 		.fc_expires = expires,
168686872cb5SThomas Graf 		.fc_dst_len = plen,
168786872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
1688c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
168986872cb5SThomas Graf 	};
16901da177e4SLinus Torvalds 
169186872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, pfx);
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
16941da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
16951da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
16961da177e4SLinus Torvalds 	 */
16970be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
16981da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
169986872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
17000be669bbSJoerg Roedel #endif
17011da177e4SLinus Torvalds 
170286872cb5SThomas Graf 	ip6_route_add(&cfg);
17031da177e4SLinus Torvalds }
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
17081da177e4SLinus Torvalds {
170986872cb5SThomas Graf 	struct fib6_config cfg = {
171086872cb5SThomas Graf 		.fc_table = RT6_TABLE_LOCAL,
171186872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
171286872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
171386872cb5SThomas Graf 		.fc_dst_len = 8,
171486872cb5SThomas Graf 		.fc_flags = RTF_UP,
1715c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
171686872cb5SThomas Graf 	};
17171da177e4SLinus Torvalds 
171886872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
171986872cb5SThomas Graf 
172086872cb5SThomas Graf 	ip6_route_add(&cfg);
17211da177e4SLinus Torvalds }
17221da177e4SLinus Torvalds 
17230be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
17241da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev)
17251da177e4SLinus Torvalds {
172686872cb5SThomas Graf 	struct fib6_config cfg = {
172786872cb5SThomas Graf 		.fc_table = RT6_TABLE_MAIN,
172886872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
172986872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
173086872cb5SThomas Graf 		.fc_dst_len = 96,
173186872cb5SThomas Graf 		.fc_flags = RTF_UP | RTF_NONEXTHOP,
1732c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
173386872cb5SThomas Graf 	};
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 	/* prefix length - 96 bits "::d.d.d.d" */
173686872cb5SThomas Graf 	ip6_route_add(&cfg);
17371da177e4SLinus Torvalds }
17380be669bbSJoerg Roedel #endif
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev)
17411da177e4SLinus Torvalds {
17421da177e4SLinus Torvalds 	struct in6_addr addr;
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
17451da177e4SLinus Torvalds 	addrconf_prefix_route(&addr, 64, dev, 0, 0);
17461da177e4SLinus Torvalds }
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
17491da177e4SLinus Torvalds {
17501da177e4SLinus Torvalds 	struct inet6_dev *idev;
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	ASSERT_RTNL();
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL)
17551da177e4SLinus Torvalds 		return NULL;
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds 	/* Add default multicast route */
17581da177e4SLinus Torvalds 	addrconf_add_mroute(dev);
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 	/* Add link local route */
17611da177e4SLinus Torvalds 	addrconf_add_lroute(dev);
17621da177e4SLinus Torvalds 	return idev;
17631da177e4SLinus Torvalds }
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
17661da177e4SLinus Torvalds {
17671da177e4SLinus Torvalds 	struct prefix_info *pinfo;
17681da177e4SLinus Torvalds 	__u32 valid_lft;
17691da177e4SLinus Torvalds 	__u32 prefered_lft;
17701da177e4SLinus Torvalds 	int addr_type;
17711da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
17761da177e4SLinus Torvalds 		ADBG(("addrconf: prefix option too short\n"));
17771da177e4SLinus Torvalds 		return;
17781da177e4SLinus Torvalds 	}
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds 	/*
17811da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
17821da177e4SLinus Torvalds 	 */
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
17871da177e4SLinus Torvalds 		return;
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
17901da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
17931da177e4SLinus Torvalds 		if (net_ratelimit())
17941da177e4SLinus Torvalds 			printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
17951da177e4SLinus Torvalds 		return;
17961da177e4SLinus Torvalds 	}
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	if (in6_dev == NULL) {
18011da177e4SLinus Torvalds 		if (net_ratelimit())
18021da177e4SLinus Torvalds 			printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
18031da177e4SLinus Torvalds 		return;
18041da177e4SLinus Torvalds 	}
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	/*
18071da177e4SLinus Torvalds 	 *	Two things going on here:
18081da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
18091da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
18101da177e4SLinus Torvalds 	 */
18111da177e4SLinus Torvalds 
18124bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
18134bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
18144bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
18154bed72e4SYOSHIFUJI Hideaki 
18161da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
18176f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
18186f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
18196f704992SYOSHIFUJI Hideaki 		 * not good.
18201da177e4SLinus Torvalds 		 */
18214bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
18224bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
18234bed72e4SYOSHIFUJI Hideaki 		else
18244bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
18253dd4bc68SYOSHIFUJI Hideaki 
18264bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
18274bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
18281da177e4SLinus Torvalds 
1829c346dca1SYOSHIFUJI Hideaki 		rt = rt6_lookup(dev_net(dev), &pinfo->prefix, NULL,
18306fda7350SBenjamin Thery 				dev->ifindex, 1);
18311da177e4SLinus Torvalds 
18322b5ead46SYOSHIFUJI Hideaki 		if (rt && addrconf_is_prefix_route(rt)) {
18336f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
18341da177e4SLinus Torvalds 			if (valid_lft == 0) {
1835e0a1ad73SThomas Graf 				ip6_del_rt(rt);
18361da177e4SLinus Torvalds 				rt = NULL;
18374bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
18386f704992SYOSHIFUJI Hideaki 				/* not infinity */
18393dd4bc68SYOSHIFUJI Hideaki 				rt->rt6i_expires = jiffies + rt_expires;
18406f704992SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_EXPIRES;
18416f704992SYOSHIFUJI Hideaki 			} else {
18426f704992SYOSHIFUJI Hideaki 				rt->rt6i_flags &= ~RTF_EXPIRES;
18436f704992SYOSHIFUJI Hideaki 				rt->rt6i_expires = 0;
18441da177e4SLinus Torvalds 			}
18451da177e4SLinus Torvalds 		} else if (valid_lft) {
18466f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
18474bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
18484bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
18496f704992SYOSHIFUJI Hideaki 				/* not infinity */
18506f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
18516f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
18526f704992SYOSHIFUJI Hideaki 			}
18531da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
18546f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
18551da177e4SLinus Torvalds 		}
18561da177e4SLinus Torvalds 		if (rt)
18571da177e4SLinus Torvalds 			dst_release(&rt->u.dst);
18581da177e4SLinus Torvalds 	}
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
18631da177e4SLinus Torvalds 		struct inet6_ifaddr * ifp;
18641da177e4SLinus Torvalds 		struct in6_addr addr;
18651da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
186653b7997fSYOSHIFUJI Hideaki 		struct net *net = dev_net(dev);
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
18691da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
18701da177e4SLinus Torvalds 			if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
18711da177e4SLinus Torvalds 			    ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
18721da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
18731da177e4SLinus Torvalds 				return;
18741da177e4SLinus Torvalds 			}
18751da177e4SLinus Torvalds 			goto ok;
18761da177e4SLinus Torvalds 		}
18771da177e4SLinus Torvalds 		if (net_ratelimit())
18781da177e4SLinus Torvalds 			printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
18791da177e4SLinus Torvalds 			       pinfo->prefix_len);
18801da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
18811da177e4SLinus Torvalds 		return;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds ok:
18841da177e4SLinus Torvalds 
188553b7997fSYOSHIFUJI Hideaki 		ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds 		if (ifp == NULL && valid_lft) {
18881da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
188995c385b4SNeil Horman 			u32 addr_flags = 0;
189095c385b4SNeil Horman 
189195c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
189295c385b4SNeil Horman 			if (in6_dev->cnf.optimistic_dad &&
189353b7997fSYOSHIFUJI Hideaki 			    !net->ipv6.devconf_all->forwarding)
189495c385b4SNeil Horman 				addr_flags = IFA_F_OPTIMISTIC;
189595c385b4SNeil Horman #endif
18961da177e4SLinus Torvalds 
18971da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
18981da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
18991da177e4SLinus Torvalds 			 */
19001da177e4SLinus Torvalds 			if (!max_addresses ||
19011da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
19021da177e4SLinus Torvalds 				ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
190395c385b4SNeil Horman 						    addr_type&IPV6_ADDR_SCOPE_MASK,
190495c385b4SNeil Horman 						    addr_flags);
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 			if (!ifp || IS_ERR(ifp)) {
19071da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
19081da177e4SLinus Torvalds 				return;
19091da177e4SLinus Torvalds 			}
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 			update_lft = create = 1;
19121da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
19131da177e4SLinus Torvalds 			addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
19141da177e4SLinus Torvalds 		}
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 		if (ifp) {
19171da177e4SLinus Torvalds 			int flags;
19181da177e4SLinus Torvalds 			unsigned long now;
19191da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
19201da177e4SLinus Torvalds 			struct inet6_ifaddr *ift;
19211da177e4SLinus Torvalds #endif
19221da177e4SLinus Torvalds 			u32 stored_lft;
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
19251da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
19261da177e4SLinus Torvalds 			now = jiffies;
19271da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
19281da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
19291da177e4SLinus Torvalds 			else
19301da177e4SLinus Torvalds 				stored_lft = 0;
19311da177e4SLinus Torvalds 			if (!update_lft && stored_lft) {
19321da177e4SLinus Torvalds 				if (valid_lft > MIN_VALID_LIFETIME ||
19331da177e4SLinus Torvalds 				    valid_lft > stored_lft)
19341da177e4SLinus Torvalds 					update_lft = 1;
19351da177e4SLinus Torvalds 				else if (stored_lft <= MIN_VALID_LIFETIME) {
19361da177e4SLinus Torvalds 					/* valid_lft <= stored_lft is always true */
19371da177e4SLinus Torvalds 					/* XXX: IPsec */
19381da177e4SLinus Torvalds 					update_lft = 0;
19391da177e4SLinus Torvalds 				} else {
19401da177e4SLinus Torvalds 					valid_lft = MIN_VALID_LIFETIME;
19411da177e4SLinus Torvalds 					if (valid_lft < prefered_lft)
19421da177e4SLinus Torvalds 						prefered_lft = valid_lft;
19431da177e4SLinus Torvalds 					update_lft = 1;
19441da177e4SLinus Torvalds 				}
19451da177e4SLinus Torvalds 			}
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 			if (update_lft) {
19481da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
19491da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
19501da177e4SLinus Torvalds 				ifp->tstamp = now;
19511da177e4SLinus Torvalds 				flags = ifp->flags;
19521da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
19531da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
19541da177e4SLinus Torvalds 
19551da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
19561da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
19571da177e4SLinus Torvalds 			} else
19581da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
19611da177e4SLinus Torvalds 			read_lock_bh(&in6_dev->lock);
19621da177e4SLinus Torvalds 			/* update all temporary addresses in the list */
19631da177e4SLinus Torvalds 			for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
19641da177e4SLinus Torvalds 				/*
19651da177e4SLinus Torvalds 				 * When adjusting the lifetimes of an existing
19661da177e4SLinus Torvalds 				 * temporary address, only lower the lifetimes.
19671da177e4SLinus Torvalds 				 * Implementations must not increase the
19681da177e4SLinus Torvalds 				 * lifetimes of an existing temporary address
19691da177e4SLinus Torvalds 				 * when processing a Prefix Information Option.
19701da177e4SLinus Torvalds 				 */
1971c6fbfac2SBenoit Boissinot 				if (ifp != ift->ifpub)
1972c6fbfac2SBenoit Boissinot 					continue;
1973c6fbfac2SBenoit Boissinot 
19741da177e4SLinus Torvalds 				spin_lock(&ift->lock);
19751da177e4SLinus Torvalds 				flags = ift->flags;
19761da177e4SLinus Torvalds 				if (ift->valid_lft > valid_lft &&
19771da177e4SLinus Torvalds 				    ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
19781da177e4SLinus Torvalds 					ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
19791da177e4SLinus Torvalds 				if (ift->prefered_lft > prefered_lft &&
19801da177e4SLinus Torvalds 				    ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
19811da177e4SLinus Torvalds 					ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
19821da177e4SLinus Torvalds 				spin_unlock(&ift->lock);
19831da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
19841da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ift);
19851da177e4SLinus Torvalds 			}
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 			if (create && in6_dev->cnf.use_tempaddr > 0) {
19881da177e4SLinus Torvalds 				/*
19891da177e4SLinus Torvalds 				 * When a new public address is created as described in [ADDRCONF],
19901da177e4SLinus Torvalds 				 * also create a new temporary address.
19911da177e4SLinus Torvalds 				 */
19921da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
19931da177e4SLinus Torvalds 				ipv6_create_tempaddr(ifp, NULL);
19941da177e4SLinus Torvalds 			} else {
19951da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
19961da177e4SLinus Torvalds 			}
19971da177e4SLinus Torvalds #endif
19981da177e4SLinus Torvalds 			in6_ifa_put(ifp);
19991da177e4SLinus Torvalds 			addrconf_verify(0);
20001da177e4SLinus Torvalds 		}
20011da177e4SLinus Torvalds 	}
20021da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
20031da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
20041da177e4SLinus Torvalds }
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds /*
20071da177e4SLinus Torvalds  *	Set destination address.
20081da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
20091da177e4SLinus Torvalds  *	device.
20101da177e4SLinus Torvalds  */
2011af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
20121da177e4SLinus Torvalds {
20131da177e4SLinus Torvalds 	struct in6_ifreq ireq;
20141da177e4SLinus Torvalds 	struct net_device *dev;
20151da177e4SLinus Torvalds 	int err = -EINVAL;
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds 	rtnl_lock();
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds 	err = -EFAULT;
20201da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
20211da177e4SLinus Torvalds 		goto err_exit;
20221da177e4SLinus Torvalds 
2023af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
20241da177e4SLinus Torvalds 
20251da177e4SLinus Torvalds 	err = -ENODEV;
20261da177e4SLinus Torvalds 	if (dev == NULL)
20271da177e4SLinus Torvalds 		goto err_exit;
20281da177e4SLinus Torvalds 
20290be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
20301da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
20311da177e4SLinus Torvalds 		struct ifreq ifr;
20321da177e4SLinus Torvalds 		mm_segment_t	oldfs;
20331da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
20361da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
20371da177e4SLinus Torvalds 			goto err_exit;
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
20401da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
20411da177e4SLinus Torvalds 		p.iph.saddr = 0;
20421da177e4SLinus Torvalds 		p.iph.version = 4;
20431da177e4SLinus Torvalds 		p.iph.ihl = 5;
20441da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
20451da177e4SLinus Torvalds 		p.iph.ttl = 64;
2046d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 		oldfs = get_fs(); set_fs(KERNEL_DS);
20491da177e4SLinus Torvalds 		err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
20501da177e4SLinus Torvalds 		set_fs(oldfs);
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 		if (err == 0) {
20531da177e4SLinus Torvalds 			err = -ENOBUFS;
2054af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2055af284937SDaniel Lezcano 			if (!dev)
20561da177e4SLinus Torvalds 				goto err_exit;
20571da177e4SLinus Torvalds 			err = dev_open(dev);
20581da177e4SLinus Torvalds 		}
20591da177e4SLinus Torvalds 	}
20600be669bbSJoerg Roedel #endif
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds err_exit:
20631da177e4SLinus Torvalds 	rtnl_unlock();
20641da177e4SLinus Torvalds 	return err;
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds /*
20681da177e4SLinus Torvalds  *	Manual configuration of address on an interface
20691da177e4SLinus Torvalds  */
2070af284937SDaniel Lezcano static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx,
207124ef0da7SThomas Graf 			  unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2072af284937SDaniel Lezcano 			  __u32 valid_lft)
20731da177e4SLinus Torvalds {
20741da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
20751da177e4SLinus Torvalds 	struct inet6_dev *idev;
20761da177e4SLinus Torvalds 	struct net_device *dev;
20771da177e4SLinus Torvalds 	int scope;
20786f704992SYOSHIFUJI Hideaki 	u32 flags;
20796f704992SYOSHIFUJI Hideaki 	clock_t expires;
20804bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	ASSERT_RTNL();
20831da177e4SLinus Torvalds 
208424ef0da7SThomas Graf 	if (plen > 128)
208524ef0da7SThomas Graf 		return -EINVAL;
208624ef0da7SThomas Graf 
20870778769dSNoriaki TAKAMIYA 	/* check the lifetime */
20880778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
20890778769dSNoriaki TAKAMIYA 		return -EINVAL;
20900778769dSNoriaki TAKAMIYA 
2091af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2092af284937SDaniel Lezcano 	if (!dev)
20931da177e4SLinus Torvalds 		return -ENODEV;
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL)
20961da177e4SLinus Torvalds 		return -ENOBUFS;
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
20991da177e4SLinus Torvalds 
21004bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
21014bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
21024bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
21034bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
21046f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
21054bed72e4SYOSHIFUJI Hideaki 	} else {
21064bed72e4SYOSHIFUJI Hideaki 		expires = 0;
21074bed72e4SYOSHIFUJI Hideaki 		flags = 0;
21084bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
21096f704992SYOSHIFUJI Hideaki 	}
21100778769dSNoriaki TAKAMIYA 
21114bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
21124bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
21134bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
21140778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
21154bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
21164bed72e4SYOSHIFUJI Hideaki 	}
21170778769dSNoriaki TAKAMIYA 
21180778769dSNoriaki TAKAMIYA 	ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
21190778769dSNoriaki TAKAMIYA 
21201da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
212106aebfb7SHerbert Xu 		spin_lock_bh(&ifp->lock);
21220778769dSNoriaki TAKAMIYA 		ifp->valid_lft = valid_lft;
21230778769dSNoriaki TAKAMIYA 		ifp->prefered_lft = prefered_lft;
21240778769dSNoriaki TAKAMIYA 		ifp->tstamp = jiffies;
212506aebfb7SHerbert Xu 		spin_unlock_bh(&ifp->lock);
21260778769dSNoriaki TAKAMIYA 
212746d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
21286f704992SYOSHIFUJI Hideaki 				      expires, flags);
212995c385b4SNeil Horman 		/*
213095c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
213195c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
213295c385b4SNeil Horman 		 * manually configured addresses
213395c385b4SNeil Horman 		 */
21341da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
21351da177e4SLinus Torvalds 		in6_ifa_put(ifp);
21360778769dSNoriaki TAKAMIYA 		addrconf_verify(0);
21371da177e4SLinus Torvalds 		return 0;
21381da177e4SLinus Torvalds 	}
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	return PTR_ERR(ifp);
21411da177e4SLinus Torvalds }
21421da177e4SLinus Torvalds 
2143af284937SDaniel Lezcano static int inet6_addr_del(struct net *net, int ifindex, struct in6_addr *pfx,
214424ef0da7SThomas Graf 			  unsigned int plen)
21451da177e4SLinus Torvalds {
21461da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
21471da177e4SLinus Torvalds 	struct inet6_dev *idev;
21481da177e4SLinus Torvalds 	struct net_device *dev;
21491da177e4SLinus Torvalds 
215024ef0da7SThomas Graf 	if (plen > 128)
215124ef0da7SThomas Graf 		return -EINVAL;
215224ef0da7SThomas Graf 
2153af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2154af284937SDaniel Lezcano 	if (!dev)
21551da177e4SLinus Torvalds 		return -ENODEV;
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL)
21581da177e4SLinus Torvalds 		return -ENXIO;
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
21611da177e4SLinus Torvalds 	for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
21621da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
21631da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
21641da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
21651da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 			/* If the last address is deleted administratively,
21701da177e4SLinus Torvalds 			   disable IPv6 on this interface.
21711da177e4SLinus Torvalds 			 */
21721da177e4SLinus Torvalds 			if (idev->addr_list == NULL)
21731da177e4SLinus Torvalds 				addrconf_ifdown(idev->dev, 1);
21741da177e4SLinus Torvalds 			return 0;
21751da177e4SLinus Torvalds 		}
21761da177e4SLinus Torvalds 	}
21771da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
21781da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
21791da177e4SLinus Torvalds }
21801da177e4SLinus Torvalds 
21811da177e4SLinus Torvalds 
2182af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
21831da177e4SLinus Torvalds {
21841da177e4SLinus Torvalds 	struct in6_ifreq ireq;
21851da177e4SLinus Torvalds 	int err;
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
21881da177e4SLinus Torvalds 		return -EPERM;
21891da177e4SLinus Torvalds 
21901da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
21911da177e4SLinus Torvalds 		return -EFAULT;
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	rtnl_lock();
2194af284937SDaniel Lezcano 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2195af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2196af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
21971da177e4SLinus Torvalds 	rtnl_unlock();
21981da177e4SLinus Torvalds 	return err;
21991da177e4SLinus Torvalds }
22001da177e4SLinus Torvalds 
2201af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
22021da177e4SLinus Torvalds {
22031da177e4SLinus Torvalds 	struct in6_ifreq ireq;
22041da177e4SLinus Torvalds 	int err;
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
22071da177e4SLinus Torvalds 		return -EPERM;
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
22101da177e4SLinus Torvalds 		return -EFAULT;
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds 	rtnl_lock();
2213af284937SDaniel Lezcano 	err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2214af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
22151da177e4SLinus Torvalds 	rtnl_unlock();
22161da177e4SLinus Torvalds 	return err;
22171da177e4SLinus Torvalds }
22181da177e4SLinus Torvalds 
22190be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
22201da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
22211da177e4SLinus Torvalds {
22221da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
22231da177e4SLinus Torvalds 	struct in6_addr addr;
22241da177e4SLinus Torvalds 	struct net_device *dev;
2225c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
22261da177e4SLinus Torvalds 	int scope;
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds 	ASSERT_RTNL();
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
22311da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
22341da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
22351da177e4SLinus Torvalds 		scope = IFA_LINK;
22361da177e4SLinus Torvalds 	} else {
22371da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
22381da177e4SLinus Torvalds 	}
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
22411da177e4SLinus Torvalds 		ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
22421da177e4SLinus Torvalds 		if (!IS_ERR(ifp)) {
22431da177e4SLinus Torvalds 			spin_lock_bh(&ifp->lock);
22441da177e4SLinus Torvalds 			ifp->flags &= ~IFA_F_TENTATIVE;
22451da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
22461da177e4SLinus Torvalds 			ipv6_ifa_notify(RTM_NEWADDR, ifp);
22471da177e4SLinus Torvalds 			in6_ifa_put(ifp);
22481da177e4SLinus Torvalds 		}
22491da177e4SLinus Torvalds 		return;
22501da177e4SLinus Torvalds 	}
22511da177e4SLinus Torvalds 
22526fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
2253e5ed6399SHerbert Xu 		struct in_device * in_dev = __in_dev_get_rtnl(dev);
22541da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
22551da177e4SLinus Torvalds 			struct in_ifaddr * ifa;
22561da177e4SLinus Torvalds 
22571da177e4SLinus Torvalds 			int flag = scope;
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
22601da177e4SLinus Torvalds 				int plen;
22611da177e4SLinus Torvalds 
22621da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
22631da177e4SLinus Torvalds 
22641da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
22651da177e4SLinus Torvalds 					continue;
22661da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
22671da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
22681da177e4SLinus Torvalds 						continue;
22691da177e4SLinus Torvalds 					flag |= IFA_HOST;
22701da177e4SLinus Torvalds 				}
22711da177e4SLinus Torvalds 				if (idev->dev->flags&IFF_POINTOPOINT)
22721da177e4SLinus Torvalds 					plen = 64;
22731da177e4SLinus Torvalds 				else
22741da177e4SLinus Torvalds 					plen = 96;
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 				ifp = ipv6_add_addr(idev, &addr, plen, flag,
22771da177e4SLinus Torvalds 						    IFA_F_PERMANENT);
22781da177e4SLinus Torvalds 				if (!IS_ERR(ifp)) {
22791da177e4SLinus Torvalds 					spin_lock_bh(&ifp->lock);
22801da177e4SLinus Torvalds 					ifp->flags &= ~IFA_F_TENTATIVE;
22811da177e4SLinus Torvalds 					spin_unlock_bh(&ifp->lock);
22821da177e4SLinus Torvalds 					ipv6_ifa_notify(RTM_NEWADDR, ifp);
22831da177e4SLinus Torvalds 					in6_ifa_put(ifp);
22841da177e4SLinus Torvalds 				}
22851da177e4SLinus Torvalds 			}
22861da177e4SLinus Torvalds 		}
22871da177e4SLinus Torvalds 	}
22881da177e4SLinus Torvalds }
22890be669bbSJoerg Roedel #endif
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
22921da177e4SLinus Torvalds {
22931da177e4SLinus Torvalds 	struct inet6_dev  *idev;
22941da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds 	/* ::1 */
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds 	ASSERT_RTNL();
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
23011da177e4SLinus Torvalds 		printk(KERN_DEBUG "init loopback: add_dev failed\n");
23021da177e4SLinus Torvalds 		return;
23031da177e4SLinus Torvalds 	}
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds 	ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
23061da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
23071da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
23081da177e4SLinus Torvalds 		ifp->flags &= ~IFA_F_TENTATIVE;
23091da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
23101da177e4SLinus Torvalds 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
23111da177e4SLinus Torvalds 		in6_ifa_put(ifp);
23121da177e4SLinus Torvalds 	}
23131da177e4SLinus Torvalds }
23141da177e4SLinus Torvalds 
23151da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
23161da177e4SLinus Torvalds {
23171da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
231895c385b4SNeil Horman 	u32 addr_flags = IFA_F_PERMANENT;
23191da177e4SLinus Torvalds 
232095c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
232195c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
2322702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
232395c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
232495c385b4SNeil Horman #endif
232595c385b4SNeil Horman 
232695c385b4SNeil Horman 
232795c385b4SNeil Horman 	ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
23281da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
232946d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
23301da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
23311da177e4SLinus Torvalds 		in6_ifa_put(ifp);
23321da177e4SLinus Torvalds 	}
23331da177e4SLinus Torvalds }
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
23361da177e4SLinus Torvalds {
23371da177e4SLinus Torvalds 	struct in6_addr addr;
23381da177e4SLinus Torvalds 	struct inet6_dev    * idev;
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds 	ASSERT_RTNL();
23411da177e4SLinus Torvalds 
234274235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
234374235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
234474235a25SHerbert Xu 	    (dev->type != ARPHRD_IEEE802_TR) &&
234574235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
234674235a25SHerbert Xu 	    (dev->type != ARPHRD_INFINIBAND)) {
234774235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
234874235a25SHerbert Xu 		return;
234974235a25SHerbert Xu 	}
235074235a25SHerbert Xu 
23511da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
23521da177e4SLinus Torvalds 	if (idev == NULL)
23531da177e4SLinus Torvalds 		return;
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
23561da177e4SLinus Torvalds 	addr.s6_addr32[0] = htonl(0xFE800000);
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 	if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
23591da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &addr);
23601da177e4SLinus Torvalds }
23611da177e4SLinus Torvalds 
23620be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
23631da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
23641da177e4SLinus Torvalds {
23651da177e4SLinus Torvalds 	struct inet6_dev *idev;
23661da177e4SLinus Torvalds 
23671da177e4SLinus Torvalds 	ASSERT_RTNL();
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds 	/*
23701da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
23711da177e4SLinus Torvalds 	 * addresses... we should configure all of
23721da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
23731da177e4SLinus Torvalds 	 */
23741da177e4SLinus Torvalds 
23751da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
23761da177e4SLinus Torvalds 		printk(KERN_DEBUG "init sit: add_dev failed\n");
23771da177e4SLinus Torvalds 		return;
23781da177e4SLinus Torvalds 	}
23791da177e4SLinus Torvalds 
2380c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
2381c7dc89c0SFred L. Templin 		struct in6_addr addr;
2382c7dc89c0SFred L. Templin 
2383c7dc89c0SFred L. Templin 		ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2384c7dc89c0SFred L. Templin 		addrconf_prefix_route(&addr, 64, dev, 0, 0);
2385c7dc89c0SFred L. Templin 		if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2386c7dc89c0SFred L. Templin 			addrconf_add_linklocal(idev, &addr);
2387c7dc89c0SFred L. Templin 		return;
2388c7dc89c0SFred L. Templin 	}
2389c7dc89c0SFred L. Templin 
23901da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 	if (dev->flags&IFF_POINTOPOINT) {
23931da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
23941da177e4SLinus Torvalds 		addrconf_add_lroute(dev);
23951da177e4SLinus Torvalds 	} else
23961da177e4SLinus Torvalds 		sit_route_add(dev);
23971da177e4SLinus Torvalds }
23980be669bbSJoerg Roedel #endif
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds static inline int
24011da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
24021da177e4SLinus Torvalds {
24031da177e4SLinus Torvalds 	struct in6_addr lladdr;
24041da177e4SLinus Torvalds 
240595c385b4SNeil Horman 	if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
24061da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &lladdr);
24071da177e4SLinus Torvalds 		return 0;
24081da177e4SLinus Torvalds 	}
24091da177e4SLinus Torvalds 	return -1;
24101da177e4SLinus Torvalds }
24111da177e4SLinus Torvalds 
24121da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
24131da177e4SLinus Torvalds {
24141da177e4SLinus Torvalds 	struct net_device *link_dev;
2415c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds 	/* first try to inherit the link-local address from the link device */
24181da177e4SLinus Torvalds 	if (idev->dev->iflink &&
24196fda7350SBenjamin Thery 	    (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
24201da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
24211da177e4SLinus Torvalds 			return;
24221da177e4SLinus Torvalds 	}
24231da177e4SLinus Torvalds 	/* then try to inherit it from any device */
24246fda7350SBenjamin Thery 	for_each_netdev(net, link_dev) {
24251da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
24261da177e4SLinus Torvalds 			return;
24271da177e4SLinus Torvalds 	}
24281da177e4SLinus Torvalds 	printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
24291da177e4SLinus Torvalds }
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds /*
24321da177e4SLinus Torvalds  * Autoconfigure tunnel with a link-local address so routing protocols,
24331da177e4SLinus Torvalds  * DHCPv6, MLD etc. can be run over the virtual link
24341da177e4SLinus Torvalds  */
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev)
24371da177e4SLinus Torvalds {
24381da177e4SLinus Torvalds 	struct inet6_dev *idev;
24391da177e4SLinus Torvalds 
24401da177e4SLinus Torvalds 	ASSERT_RTNL();
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL) {
24431da177e4SLinus Torvalds 		printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
24441da177e4SLinus Torvalds 		return;
24451da177e4SLinus Torvalds 	}
24461da177e4SLinus Torvalds 	ip6_tnl_add_linklocal(idev);
24471da177e4SLinus Torvalds }
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
24501da177e4SLinus Torvalds 			   void * data)
24511da177e4SLinus Torvalds {
24521da177e4SLinus Torvalds 	struct net_device *dev = (struct net_device *) data;
245374235a25SHerbert Xu 	struct inet6_dev *idev = __in6_dev_get(dev);
2454c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
2455b217d616SHerbert Xu 	int err;
24561da177e4SLinus Torvalds 
24571da177e4SLinus Torvalds 	switch(event) {
245845ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
245974235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
246045ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
246145ba9dd2SYOSHIFUJI Hideaki 			if (!idev)
2462b217d616SHerbert Xu 				return notifier_from_errno(-ENOMEM);
246345ba9dd2SYOSHIFUJI Hideaki 		}
246445ba9dd2SYOSHIFUJI Hideaki 		break;
24651da177e4SLinus Torvalds 	case NETDEV_UP:
24663c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
2467c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
2468c2edacf8SJay Vosburgh 			break;
2469c2edacf8SJay Vosburgh 
24703c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
2471f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
24723c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
24733c21edbdSYOSHIFUJI Hideaki 				printk(KERN_INFO
24743c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_UP): %s: "
24753c21edbdSYOSHIFUJI Hideaki 					"link is not ready\n",
24763c21edbdSYOSHIFUJI Hideaki 					dev->name);
24773c21edbdSYOSHIFUJI Hideaki 				break;
24783c21edbdSYOSHIFUJI Hideaki 			}
247999081049SKristian Slavov 
2480d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
2481d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
2482d31c7b8fSEvgeniy Polyakov 
248399081049SKristian Slavov 			if (idev)
248499081049SKristian Slavov 				idev->if_flags |= IF_READY;
24853c21edbdSYOSHIFUJI Hideaki 		} else {
2486f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
24873c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
24883c21edbdSYOSHIFUJI Hideaki 				break;
24893c21edbdSYOSHIFUJI Hideaki 			}
24903c21edbdSYOSHIFUJI Hideaki 
24913c21edbdSYOSHIFUJI Hideaki 			if (idev) {
24923c21edbdSYOSHIFUJI Hideaki 				if (idev->if_flags & IF_READY) {
24933c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
24943c21edbdSYOSHIFUJI Hideaki 					break;
24953c21edbdSYOSHIFUJI Hideaki 				}
24963c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
24973c21edbdSYOSHIFUJI Hideaki 			}
24983c21edbdSYOSHIFUJI Hideaki 
24993c21edbdSYOSHIFUJI Hideaki 			printk(KERN_INFO
25003c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_CHANGE): %s: "
25013c21edbdSYOSHIFUJI Hideaki 					"link becomes ready\n",
25023c21edbdSYOSHIFUJI Hideaki 					dev->name);
25033c21edbdSYOSHIFUJI Hideaki 
2504c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
25053c21edbdSYOSHIFUJI Hideaki 		}
25063c21edbdSYOSHIFUJI Hideaki 
25071da177e4SLinus Torvalds 		switch(dev->type) {
25080be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
25091da177e4SLinus Torvalds 		case ARPHRD_SIT:
25101da177e4SLinus Torvalds 			addrconf_sit_config(dev);
25111da177e4SLinus Torvalds 			break;
25120be669bbSJoerg Roedel #endif
25131da177e4SLinus Torvalds 		case ARPHRD_TUNNEL6:
25141da177e4SLinus Torvalds 			addrconf_ip6_tnl_config(dev);
25151da177e4SLinus Torvalds 			break;
25161da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
25171da177e4SLinus Torvalds 			init_loopback(dev);
25181da177e4SLinus Torvalds 			break;
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 		default:
25211da177e4SLinus Torvalds 			addrconf_dev_config(dev);
25221da177e4SLinus Torvalds 			break;
25233ff50b79SStephen Hemminger 		}
25241da177e4SLinus Torvalds 		if (idev) {
2525c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
2526c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
2527c5e33bddSYOSHIFUJI Hideaki 
25281da177e4SLinus Torvalds 			/* If the MTU changed during the interface down, when the
25291da177e4SLinus Torvalds 			   interface up, the changed MTU must be reflected in the
25301da177e4SLinus Torvalds 			   idev as well as routers.
25311da177e4SLinus Torvalds 			 */
25321da177e4SLinus Torvalds 			if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
25331da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
25341da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
25351da177e4SLinus Torvalds 			}
25361da177e4SLinus Torvalds 			idev->tstamp = jiffies;
25371da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
25381da177e4SLinus Torvalds 			/* If the changed mtu during down is lower than IPV6_MIN_MTU
25391da177e4SLinus Torvalds 			   stop IPv6 on this interface.
25401da177e4SLinus Torvalds 			 */
25411da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
25421da177e4SLinus Torvalds 				addrconf_ifdown(dev, event != NETDEV_DOWN);
25431da177e4SLinus Torvalds 		}
25441da177e4SLinus Torvalds 		break;
25451da177e4SLinus Torvalds 
25461da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
25471da177e4SLinus Torvalds 		if (idev && dev->mtu >= IPV6_MIN_MTU) {
25481da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
25491da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
25501da177e4SLinus Torvalds 			break;
25511da177e4SLinus Torvalds 		}
25521da177e4SLinus Torvalds 
2553d31c7b8fSEvgeniy Polyakov 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2554d31c7b8fSEvgeniy Polyakov 			idev = ipv6_add_dev(dev);
2555d31c7b8fSEvgeniy Polyakov 			if (idev)
2556d31c7b8fSEvgeniy Polyakov 				break;
2557d31c7b8fSEvgeniy Polyakov 		}
2558d31c7b8fSEvgeniy Polyakov 
25591da177e4SLinus Torvalds 		/* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
25601da177e4SLinus Torvalds 
25611da177e4SLinus Torvalds 	case NETDEV_DOWN:
25621da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
25631da177e4SLinus Torvalds 		/*
25641da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
25651da177e4SLinus Torvalds 		 */
25661da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
25671da177e4SLinus Torvalds 		break;
25683c21edbdSYOSHIFUJI Hideaki 
25691da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
25701da177e4SLinus Torvalds 		if (idev) {
25715632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
2572408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
2573f52295a9SPavel Emelyanov 			addrconf_sysctl_register(idev);
2574b217d616SHerbert Xu 			err = snmp6_register_dev(idev);
2575b217d616SHerbert Xu 			if (err)
2576b217d616SHerbert Xu 				return notifier_from_errno(err);
25775632c515SStephen Hemminger 		}
25781da177e4SLinus Torvalds 		break;
25793ff50b79SStephen Hemminger 	}
25801da177e4SLinus Torvalds 
25811da177e4SLinus Torvalds 	return NOTIFY_OK;
25821da177e4SLinus Torvalds }
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds /*
25851da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
25861da177e4SLinus Torvalds  */
25871da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
25881da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
25891da177e4SLinus Torvalds 	.priority = 0
25901da177e4SLinus Torvalds };
25911da177e4SLinus Torvalds 
25921da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
25931da177e4SLinus Torvalds {
25941da177e4SLinus Torvalds 	struct inet6_dev *idev;
25951da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **bifa;
2596c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
25971da177e4SLinus Torvalds 	int i;
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds 	ASSERT_RTNL();
26001da177e4SLinus Torvalds 
2601eb867579SDenis V. Lunev 	if ((dev->flags & IFF_LOOPBACK) && how == 1)
26021da177e4SLinus Torvalds 		how = 0;
26031da177e4SLinus Torvalds 
2604f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
26051da177e4SLinus Torvalds 	neigh_ifdown(&nd_tbl, dev);
26061da177e4SLinus Torvalds 
26071da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
26081da177e4SLinus Torvalds 	if (idev == NULL)
26091da177e4SLinus Torvalds 		return -ENODEV;
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds 	/* Step 1: remove reference to ipv6 device from parent device.
26121da177e4SLinus Torvalds 		   Do not dev_put!
26131da177e4SLinus Torvalds 	 */
2614439e2385SDenis V. Lunev 	if (how) {
26151da177e4SLinus Torvalds 		idev->dead = 1;
26168814c4b5SYOSHIFUJI Hideaki 
26178814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
26188814c4b5SYOSHIFUJI Hideaki 		rcu_assign_pointer(dev->ip6_ptr, NULL);
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
26211da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds 	}
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds 	/* Step 2: clear hash table */
26261da177e4SLinus Torvalds 	for (i=0; i<IN6_ADDR_HSIZE; i++) {
26271da177e4SLinus Torvalds 		bifa = &inet6_addr_lst[i];
26281da177e4SLinus Torvalds 
26291da177e4SLinus Torvalds 		write_lock_bh(&addrconf_hash_lock);
26301da177e4SLinus Torvalds 		while ((ifa = *bifa) != NULL) {
26311da177e4SLinus Torvalds 			if (ifa->idev == idev) {
26321da177e4SLinus Torvalds 				*bifa = ifa->lst_next;
26331da177e4SLinus Torvalds 				ifa->lst_next = NULL;
26341da177e4SLinus Torvalds 				addrconf_del_timer(ifa);
26351da177e4SLinus Torvalds 				in6_ifa_put(ifa);
26361da177e4SLinus Torvalds 				continue;
26371da177e4SLinus Torvalds 			}
26381da177e4SLinus Torvalds 			bifa = &ifa->lst_next;
26391da177e4SLinus Torvalds 		}
26401da177e4SLinus Torvalds 		write_unlock_bh(&addrconf_hash_lock);
26411da177e4SLinus Torvalds 	}
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds 	/* Step 3: clear flags for stateless addrconf */
2646439e2385SDenis V. Lunev 	if (!how)
26473c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds 	/* Step 4: clear address list */
26501da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
2651439e2385SDenis V. Lunev 	if (how && del_timer(&idev->regen_timer))
26521da177e4SLinus Torvalds 		in6_dev_put(idev);
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds 	/* clear tempaddr list */
26551da177e4SLinus Torvalds 	while ((ifa = idev->tempaddr_list) != NULL) {
26561da177e4SLinus Torvalds 		idev->tempaddr_list = ifa->tmp_next;
26571da177e4SLinus Torvalds 		ifa->tmp_next = NULL;
26581da177e4SLinus Torvalds 		ifa->dead = 1;
26591da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
26601da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
26611da177e4SLinus Torvalds 
26621da177e4SLinus Torvalds 		if (ifa->ifpub) {
26631da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
26641da177e4SLinus Torvalds 			ifa->ifpub = NULL;
26651da177e4SLinus Torvalds 		}
26661da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
26671da177e4SLinus Torvalds 		in6_ifa_put(ifa);
26681da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
26691da177e4SLinus Torvalds 	}
26701da177e4SLinus Torvalds #endif
26711da177e4SLinus Torvalds 	while ((ifa = idev->addr_list) != NULL) {
26721da177e4SLinus Torvalds 		idev->addr_list = ifa->if_next;
26731da177e4SLinus Torvalds 		ifa->if_next = NULL;
26741da177e4SLinus Torvalds 		ifa->dead = 1;
26751da177e4SLinus Torvalds 		addrconf_del_timer(ifa);
26761da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
26771da177e4SLinus Torvalds 
26781da177e4SLinus Torvalds 		__ipv6_ifa_notify(RTM_DELADDR, ifa);
2679063ed369SVlad Yasevich 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
26801da177e4SLinus Torvalds 		in6_ifa_put(ifa);
26811da177e4SLinus Torvalds 
26821da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
26831da177e4SLinus Torvalds 	}
26841da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds 	/* Step 5: Discard multicast list */
26871da177e4SLinus Torvalds 
2688439e2385SDenis V. Lunev 	if (how)
26891da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
26901da177e4SLinus Torvalds 	else
26911da177e4SLinus Torvalds 		ipv6_mc_down(idev);
26921da177e4SLinus Torvalds 
26931da177e4SLinus Torvalds 	idev->tstamp = jiffies;
26941da177e4SLinus Torvalds 
26951da177e4SLinus Torvalds 	/* Shot the device (if unregistered) */
26961da177e4SLinus Torvalds 
2697439e2385SDenis V. Lunev 	if (how) {
2698408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
26991da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
27001da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
27011da177e4SLinus Torvalds 		in6_dev_put(idev);
27021da177e4SLinus Torvalds 	}
27031da177e4SLinus Torvalds 	return 0;
27041da177e4SLinus Torvalds }
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
27071da177e4SLinus Torvalds {
27081da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding)
27111da177e4SLinus Torvalds 		goto out;
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds 	if (ifp->idev->if_flags & IF_RA_RCVD) {
27141da177e4SLinus Torvalds 		/*
27151da177e4SLinus Torvalds 		 *	Announcement received after solicitation
27161da177e4SLinus Torvalds 		 *	was sent
27171da177e4SLinus Torvalds 		 */
27181da177e4SLinus Torvalds 		goto out;
27191da177e4SLinus Torvalds 	}
27201da177e4SLinus Torvalds 
27211da177e4SLinus Torvalds 	spin_lock(&ifp->lock);
27221da177e4SLinus Torvalds 	if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
27231da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
27241da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS,
27251da177e4SLinus Torvalds 				   (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
27261da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_delay :
27271da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_interval);
27281da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
27291da177e4SLinus Torvalds 
2730f3ee4010SYOSHIFUJI Hideaki 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
27311da177e4SLinus Torvalds 	} else {
27321da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
27331da177e4SLinus Torvalds 		/*
27341da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
27351da177e4SLinus Torvalds 		 * assumption any longer.
27361da177e4SLinus Torvalds 		 */
27371da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: no IPv6 routers present\n",
27381da177e4SLinus Torvalds 		       ifp->idev->dev->name);
27391da177e4SLinus Torvalds 	}
27401da177e4SLinus Torvalds 
27411da177e4SLinus Torvalds out:
27421da177e4SLinus Torvalds 	in6_ifa_put(ifp);
27431da177e4SLinus Torvalds }
27441da177e4SLinus Torvalds 
27451da177e4SLinus Torvalds /*
27461da177e4SLinus Torvalds  *	Duplicate Address Detection
27471da177e4SLinus Torvalds  */
27483c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
27493c21edbdSYOSHIFUJI Hideaki {
27503c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
27513c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
27523c21edbdSYOSHIFUJI Hideaki 
275395c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
275495c385b4SNeil Horman 		rand_num = 0;
275595c385b4SNeil Horman 	else
27563c21edbdSYOSHIFUJI Hideaki 		rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
275795c385b4SNeil Horman 
27583c21edbdSYOSHIFUJI Hideaki 	ifp->probes = idev->cnf.dad_transmits;
27593c21edbdSYOSHIFUJI Hideaki 	addrconf_mod_timer(ifp, AC_DAD, rand_num);
27603c21edbdSYOSHIFUJI Hideaki }
27613c21edbdSYOSHIFUJI Hideaki 
2762e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
27631da177e4SLinus Torvalds {
27641da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
27651da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
27661da177e4SLinus Torvalds 
27671da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
27681da177e4SLinus Torvalds 
27691da177e4SLinus Torvalds 	net_srandom(ifp->addr.s6_addr32[3]);
27701da177e4SLinus Torvalds 
27711da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
27721da177e4SLinus Torvalds 	if (ifp->dead)
27731da177e4SLinus Torvalds 		goto out;
27741da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
27751da177e4SLinus Torvalds 
27761da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
27771b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
277855ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
277955ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
278095c385b4SNeil Horman 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
27811da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
27821da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
27831da177e4SLinus Torvalds 
27841da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
27851da177e4SLinus Torvalds 		return;
27861da177e4SLinus Torvalds 	}
27871da177e4SLinus Torvalds 
27886732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
27893dd3bf83SYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
27906732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
27913c21edbdSYOSHIFUJI Hideaki 		/*
27923c21edbdSYOSHIFUJI Hideaki 		 * If the defice is not ready:
27933c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
27943c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
27953c21edbdSYOSHIFUJI Hideaki 		 */
27963c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
27973c21edbdSYOSHIFUJI Hideaki 		addrconf_dad_stop(ifp);
27986732badeSYOSHIFUJI Hideaki 		return;
27993c21edbdSYOSHIFUJI Hideaki 	}
280095c385b4SNeil Horman 
280195c385b4SNeil Horman 	/*
280295c385b4SNeil Horman 	 * Optimistic nodes can start receiving
280395c385b4SNeil Horman 	 * Frames right away
280495c385b4SNeil Horman 	 */
280595c385b4SNeil Horman 	if(ifp->flags & IFA_F_OPTIMISTIC)
280695c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
280795c385b4SNeil Horman 
28086732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
28096732badeSYOSHIFUJI Hideaki 	spin_unlock_bh(&ifp->lock);
28101da177e4SLinus Torvalds out:
28111da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
28121da177e4SLinus Torvalds }
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data)
28151da177e4SLinus Torvalds {
28161da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
28171da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
28181da177e4SLinus Torvalds 	struct in6_addr mcaddr;
28191da177e4SLinus Torvalds 
28201da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
28211da177e4SLinus Torvalds 	if (idev->dead) {
28221da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
28231da177e4SLinus Torvalds 		goto out;
28241da177e4SLinus Torvalds 	}
28251b34be74SYOSHIFUJI Hideaki 	if (idev->cnf.accept_dad > 1 && idev->cnf.disable_ipv6) {
28261b34be74SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
28271b34be74SYOSHIFUJI Hideaki 		addrconf_dad_failure(ifp);
28281b34be74SYOSHIFUJI Hideaki 		return;
28291b34be74SYOSHIFUJI Hideaki 	}
28301da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
28311da177e4SLinus Torvalds 	if (ifp->probes == 0) {
28321da177e4SLinus Torvalds 		/*
28331da177e4SLinus Torvalds 		 * DAD was successful
28341da177e4SLinus Torvalds 		 */
28351da177e4SLinus Torvalds 
283695c385b4SNeil Horman 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
28371da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
28381da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
28391da177e4SLinus Torvalds 
28401da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
28411da177e4SLinus Torvalds 
28421da177e4SLinus Torvalds 		goto out;
28431da177e4SLinus Torvalds 	}
28441da177e4SLinus Torvalds 
28451da177e4SLinus Torvalds 	ifp->probes--;
28461da177e4SLinus Torvalds 	addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
28471da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
28481da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
28491da177e4SLinus Torvalds 
28501da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
28511da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2852d7aabf22SYOSHIFUJI Hideaki 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
28531da177e4SLinus Torvalds out:
28541da177e4SLinus Torvalds 	in6_ifa_put(ifp);
28551da177e4SLinus Torvalds }
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
28581da177e4SLinus Torvalds {
28591da177e4SLinus Torvalds 	struct net_device *	dev = ifp->idev->dev;
28601da177e4SLinus Torvalds 
28611da177e4SLinus Torvalds 	/*
28621da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
28631da177e4SLinus Torvalds 	 */
28641da177e4SLinus Torvalds 
28651da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds 	/* If added prefix is link local and forwarding is off,
28681da177e4SLinus Torvalds 	   start sending router solicitations.
28691da177e4SLinus Torvalds 	 */
28701da177e4SLinus Torvalds 
28711da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding == 0 &&
28721da177e4SLinus Torvalds 	    ifp->idev->cnf.rtr_solicits > 0 &&
28731da177e4SLinus Torvalds 	    (dev->flags&IFF_LOOPBACK) == 0 &&
28741da177e4SLinus Torvalds 	    (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
28751da177e4SLinus Torvalds 		/*
28761da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
28771da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
28781da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
28791da177e4SLinus Torvalds 		 */
2880f3ee4010SYOSHIFUJI Hideaki 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
28811da177e4SLinus Torvalds 
28821da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
28831da177e4SLinus Torvalds 		ifp->probes = 1;
28841da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
28851da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
28861da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
28871da177e4SLinus Torvalds 	}
28881da177e4SLinus Torvalds }
28891da177e4SLinus Torvalds 
2890c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev) {
2891c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
2892c5e33bddSYOSHIFUJI Hideaki 
2893c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
2894c5e33bddSYOSHIFUJI Hideaki 	for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2895c5e33bddSYOSHIFUJI Hideaki 		spin_lock_bh(&ifp->lock);
2896c5e33bddSYOSHIFUJI Hideaki 		if (!(ifp->flags & IFA_F_TENTATIVE)) {
2897c5e33bddSYOSHIFUJI Hideaki 			spin_unlock_bh(&ifp->lock);
2898c5e33bddSYOSHIFUJI Hideaki 			continue;
2899c5e33bddSYOSHIFUJI Hideaki 		}
2900c5e33bddSYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
2901c5e33bddSYOSHIFUJI Hideaki 		addrconf_dad_kick(ifp);
2902c5e33bddSYOSHIFUJI Hideaki 	}
2903c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
2904c5e33bddSYOSHIFUJI Hideaki }
2905c5e33bddSYOSHIFUJI Hideaki 
29061da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
29071da177e4SLinus Torvalds struct if6_iter_state {
29083c40090aSDaniel Lezcano 	struct seq_net_private p;
29091da177e4SLinus Torvalds 	int bucket;
29101da177e4SLinus Torvalds };
29111da177e4SLinus Torvalds 
29121da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
29131da177e4SLinus Torvalds {
29141da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
29151da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
29161218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
29171da177e4SLinus Torvalds 
29181da177e4SLinus Torvalds 	for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
29191da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
29203c40090aSDaniel Lezcano 
2921878628fbSYOSHIFUJI Hideaki 		while (ifa && !net_eq(dev_net(ifa->idev->dev), net))
29223c40090aSDaniel Lezcano 			ifa = ifa->lst_next;
29231da177e4SLinus Torvalds 		if (ifa)
29241da177e4SLinus Torvalds 			break;
29251da177e4SLinus Torvalds 	}
29261da177e4SLinus Torvalds 	return ifa;
29271da177e4SLinus Torvalds }
29281da177e4SLinus Torvalds 
29291da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
29301da177e4SLinus Torvalds {
29311da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
29321218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds 	ifa = ifa->lst_next;
29351da177e4SLinus Torvalds try_again:
29363c40090aSDaniel Lezcano 	if (ifa) {
2937878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifa->idev->dev), net)) {
29383c40090aSDaniel Lezcano 			ifa = ifa->lst_next;
29393c40090aSDaniel Lezcano 			goto try_again;
29403c40090aSDaniel Lezcano 		}
29413c40090aSDaniel Lezcano 	}
29423c40090aSDaniel Lezcano 
29431da177e4SLinus Torvalds 	if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
29441da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
29451da177e4SLinus Torvalds 		goto try_again;
29461da177e4SLinus Torvalds 	}
29473c40090aSDaniel Lezcano 
29481da177e4SLinus Torvalds 	return ifa;
29491da177e4SLinus Torvalds }
29501da177e4SLinus Torvalds 
29511da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
29521da177e4SLinus Torvalds {
29531da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = if6_get_first(seq);
29541da177e4SLinus Torvalds 
29551da177e4SLinus Torvalds 	if (ifa)
29561da177e4SLinus Torvalds 		while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
29571da177e4SLinus Torvalds 			--pos;
29581da177e4SLinus Torvalds 	return pos ? NULL : ifa;
29591da177e4SLinus Torvalds }
29601da177e4SLinus Torvalds 
29611da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2962d20b3109SStephen Hemminger 	__acquires(addrconf_hash_lock)
29631da177e4SLinus Torvalds {
29641da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
29651da177e4SLinus Torvalds 	return if6_get_idx(seq, *pos);
29661da177e4SLinus Torvalds }
29671da177e4SLinus Torvalds 
29681da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
29691da177e4SLinus Torvalds {
29701da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
29711da177e4SLinus Torvalds 
29721da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
29731da177e4SLinus Torvalds 	++*pos;
29741da177e4SLinus Torvalds 	return ifa;
29751da177e4SLinus Torvalds }
29761da177e4SLinus Torvalds 
29771da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
2978d20b3109SStephen Hemminger 	__releases(addrconf_hash_lock)
29791da177e4SLinus Torvalds {
29801da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
29811da177e4SLinus Torvalds }
29821da177e4SLinus Torvalds 
29831da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
29841da177e4SLinus Torvalds {
29851da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
29861da177e4SLinus Torvalds 	seq_printf(seq,
29879343e79aSYOSHIFUJI Hideaki 		   NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
29881da177e4SLinus Torvalds 		   NIP6(ifp->addr),
29891da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
29901da177e4SLinus Torvalds 		   ifp->prefix_len,
29911da177e4SLinus Torvalds 		   ifp->scope,
29921da177e4SLinus Torvalds 		   ifp->flags,
29931da177e4SLinus Torvalds 		   ifp->idev->dev->name);
29941da177e4SLinus Torvalds 	return 0;
29951da177e4SLinus Torvalds }
29961da177e4SLinus Torvalds 
299756b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
29981da177e4SLinus Torvalds 	.start	= if6_seq_start,
29991da177e4SLinus Torvalds 	.next	= if6_seq_next,
30001da177e4SLinus Torvalds 	.show	= if6_seq_show,
30011da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
30021da177e4SLinus Torvalds };
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
30051da177e4SLinus Torvalds {
30063c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
3007cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
30081da177e4SLinus Torvalds }
30091da177e4SLinus Torvalds 
30109a32144eSArjan van de Ven static const struct file_operations if6_fops = {
30111da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
30121da177e4SLinus Torvalds 	.open		= if6_seq_open,
30131da177e4SLinus Torvalds 	.read		= seq_read,
30141da177e4SLinus Torvalds 	.llseek		= seq_lseek,
30153c40090aSDaniel Lezcano 	.release	= seq_release_net,
30161da177e4SLinus Torvalds };
30171da177e4SLinus Torvalds 
30183c40090aSDaniel Lezcano static int if6_proc_net_init(struct net *net)
30191da177e4SLinus Torvalds {
30203c40090aSDaniel Lezcano 	if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
30211da177e4SLinus Torvalds 		return -ENOMEM;
30221da177e4SLinus Torvalds 	return 0;
30231da177e4SLinus Torvalds }
30241da177e4SLinus Torvalds 
30253c40090aSDaniel Lezcano static void if6_proc_net_exit(struct net *net)
30263c40090aSDaniel Lezcano {
30273c40090aSDaniel Lezcano        proc_net_remove(net, "if_inet6");
30283c40090aSDaniel Lezcano }
30293c40090aSDaniel Lezcano 
30303c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
30313c40090aSDaniel Lezcano        .init = if6_proc_net_init,
30323c40090aSDaniel Lezcano        .exit = if6_proc_net_exit,
30333c40090aSDaniel Lezcano };
30343c40090aSDaniel Lezcano 
30353c40090aSDaniel Lezcano int __init if6_proc_init(void)
30363c40090aSDaniel Lezcano {
30373c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
30383c40090aSDaniel Lezcano }
30393c40090aSDaniel Lezcano 
30401da177e4SLinus Torvalds void if6_proc_exit(void)
30411da177e4SLinus Torvalds {
30423c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
30431da177e4SLinus Torvalds }
30441da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
30451da177e4SLinus Torvalds 
304659fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
30473b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
3048389f6612SDaniel Lezcano int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr)
30493b9f9a1cSNoriaki TAKAMIYA {
30503b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
30513b9f9a1cSNoriaki TAKAMIYA 	struct inet6_ifaddr * ifp;
30523b9f9a1cSNoriaki TAKAMIYA 	u8 hash = ipv6_addr_hash(addr);
30533b9f9a1cSNoriaki TAKAMIYA 	read_lock_bh(&addrconf_hash_lock);
30543b9f9a1cSNoriaki TAKAMIYA 	for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
3055878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
3056389f6612SDaniel Lezcano 			continue;
3057caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
30583b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
30593b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
30603b9f9a1cSNoriaki TAKAMIYA 			break;
30613b9f9a1cSNoriaki TAKAMIYA 		}
30623b9f9a1cSNoriaki TAKAMIYA 	}
30633b9f9a1cSNoriaki TAKAMIYA 	read_unlock_bh(&addrconf_hash_lock);
30643b9f9a1cSNoriaki TAKAMIYA 	return ret;
30653b9f9a1cSNoriaki TAKAMIYA }
30663b9f9a1cSNoriaki TAKAMIYA #endif
30673b9f9a1cSNoriaki TAKAMIYA 
30681da177e4SLinus Torvalds /*
30691da177e4SLinus Torvalds  *	Periodic address status verification
30701da177e4SLinus Torvalds  */
30711da177e4SLinus Torvalds 
30721da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo)
30731da177e4SLinus Torvalds {
30741da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
30751da177e4SLinus Torvalds 	unsigned long now, next;
30761da177e4SLinus Torvalds 	int i;
30771da177e4SLinus Torvalds 
30781da177e4SLinus Torvalds 	spin_lock_bh(&addrconf_verify_lock);
30791da177e4SLinus Torvalds 	now = jiffies;
30801da177e4SLinus Torvalds 	next = now + ADDR_CHECK_FREQUENCY;
30811da177e4SLinus Torvalds 
30821da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
30851da177e4SLinus Torvalds 
30861da177e4SLinus Torvalds restart:
30875d5780dfSYan Zheng 		read_lock(&addrconf_hash_lock);
30881da177e4SLinus Torvalds 		for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
30891da177e4SLinus Torvalds 			unsigned long age;
30901da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
30911da177e4SLinus Torvalds 			unsigned long regen_advance;
30921da177e4SLinus Torvalds #endif
30931da177e4SLinus Torvalds 
30941da177e4SLinus Torvalds 			if (ifp->flags & IFA_F_PERMANENT)
30951da177e4SLinus Torvalds 				continue;
30961da177e4SLinus Torvalds 
30971da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
30981da177e4SLinus Torvalds 			age = (now - ifp->tstamp) / HZ;
30991da177e4SLinus Torvalds 
31001da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
31011da177e4SLinus Torvalds 			regen_advance = ifp->idev->cnf.regen_max_retry *
31021da177e4SLinus Torvalds 					ifp->idev->cnf.dad_transmits *
31031da177e4SLinus Torvalds 					ifp->idev->nd_parms->retrans_time / HZ;
31041da177e4SLinus Torvalds #endif
31051da177e4SLinus Torvalds 
31068f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
31078f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
31081da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
31091da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
31105d5780dfSYan Zheng 				read_unlock(&addrconf_hash_lock);
31111da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
31121da177e4SLinus Torvalds 				goto restart;
31138f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
31148f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
31158f27ebb9SYOSHIFUJI Hideaki 				continue;
31161da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
31171da177e4SLinus Torvalds 				/* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
31181da177e4SLinus Torvalds 				int deprecate = 0;
31191da177e4SLinus Torvalds 
31201da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
31211da177e4SLinus Torvalds 					deprecate = 1;
31221da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
31231da177e4SLinus Torvalds 				}
31241da177e4SLinus Torvalds 
31251da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
31261da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
31291da177e4SLinus Torvalds 
31301da177e4SLinus Torvalds 				if (deprecate) {
31311da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
31325d5780dfSYan Zheng 					read_unlock(&addrconf_hash_lock);
31331da177e4SLinus Torvalds 
31341da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
31351da177e4SLinus Torvalds 					in6_ifa_put(ifp);
31361da177e4SLinus Torvalds 					goto restart;
31371da177e4SLinus Torvalds 				}
31381da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
31391da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
31401da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
31411da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
31421da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
31431da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
31441da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
31451da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
31461da177e4SLinus Torvalds 						ifp->regen_count++;
31471da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
31481da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
31491da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
31505d5780dfSYan Zheng 						read_unlock(&addrconf_hash_lock);
3151291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
3152291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
3153291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
31541da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
31551da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
31561da177e4SLinus Torvalds 						in6_ifa_put(ifp);
31571da177e4SLinus Torvalds 						goto restart;
31581da177e4SLinus Torvalds 					}
31591da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
31601da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
31611da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
31621da177e4SLinus Torvalds #endif
31631da177e4SLinus Torvalds 			} else {
31641da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
31651da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
31661da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
31671da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
31681da177e4SLinus Torvalds 			}
31691da177e4SLinus Torvalds 		}
31705d5780dfSYan Zheng 		read_unlock(&addrconf_hash_lock);
31711da177e4SLinus Torvalds 	}
31721da177e4SLinus Torvalds 
31731da177e4SLinus Torvalds 	addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
31741da177e4SLinus Torvalds 	add_timer(&addr_chk_timer);
31751da177e4SLinus Torvalds 	spin_unlock_bh(&addrconf_verify_lock);
31761da177e4SLinus Torvalds }
31771da177e4SLinus Torvalds 
3178461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3179461d8837SThomas Graf {
3180461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
3181461d8837SThomas Graf 
3182461d8837SThomas Graf 	if (addr)
3183461d8837SThomas Graf 		pfx = nla_data(addr);
3184461d8837SThomas Graf 
3185461d8837SThomas Graf 	if (local) {
3186461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3187461d8837SThomas Graf 			pfx = NULL;
3188461d8837SThomas Graf 		else
3189461d8837SThomas Graf 			pfx = nla_data(local);
3190461d8837SThomas Graf 	}
3191461d8837SThomas Graf 
3192461d8837SThomas Graf 	return pfx;
3193461d8837SThomas Graf }
3194461d8837SThomas Graf 
3195ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3196461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
3197461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
3198461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
3199461d8837SThomas Graf };
3200461d8837SThomas Graf 
32011da177e4SLinus Torvalds static int
32021da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
32031da177e4SLinus Torvalds {
32043b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3205b933f716SThomas Graf 	struct ifaddrmsg *ifm;
3206b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
32071da177e4SLinus Torvalds 	struct in6_addr *pfx;
3208b933f716SThomas Graf 	int err;
32091da177e4SLinus Torvalds 
3210b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3211b933f716SThomas Graf 	if (err < 0)
3212b933f716SThomas Graf 		return err;
3213b933f716SThomas Graf 
3214b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
3215b933f716SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
32161da177e4SLinus Torvalds 	if (pfx == NULL)
32171da177e4SLinus Torvalds 		return -EINVAL;
32181da177e4SLinus Torvalds 
3219af284937SDaniel Lezcano 	return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
32201da177e4SLinus Torvalds }
32211da177e4SLinus Torvalds 
322255ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
322355ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
3224081bba5bSNoriaki TAKAMIYA {
32256f704992SYOSHIFUJI Hideaki 	u32 flags;
32266f704992SYOSHIFUJI Hideaki 	clock_t expires;
32274bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
322846d48046SYOSHIFUJI Hideaki 
3229081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
3230081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
3231081bba5bSNoriaki TAKAMIYA 
32324bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
32334bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
32344bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
32354bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
32366f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
32374bed72e4SYOSHIFUJI Hideaki 	} else {
32384bed72e4SYOSHIFUJI Hideaki 		expires = 0;
32394bed72e4SYOSHIFUJI Hideaki 		flags = 0;
32404bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
32416f704992SYOSHIFUJI Hideaki 	}
3242081bba5bSNoriaki TAKAMIYA 
32434bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
32444bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
32454bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
324655ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
32474bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
32484bed72e4SYOSHIFUJI Hideaki 	}
3249081bba5bSNoriaki TAKAMIYA 
3250081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
32513b9f9a1cSNoriaki TAKAMIYA 	ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3252081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
3253081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
3254081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
3255081bba5bSNoriaki TAKAMIYA 
3256081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
3257081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
3258081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
3259081bba5bSNoriaki TAKAMIYA 
326046d48046SYOSHIFUJI Hideaki 	addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
32616f704992SYOSHIFUJI Hideaki 			      expires, flags);
3262081bba5bSNoriaki TAKAMIYA 	addrconf_verify(0);
3263081bba5bSNoriaki TAKAMIYA 
3264081bba5bSNoriaki TAKAMIYA 	return 0;
3265081bba5bSNoriaki TAKAMIYA }
3266081bba5bSNoriaki TAKAMIYA 
3267081bba5bSNoriaki TAKAMIYA static int
32681da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
32691da177e4SLinus Torvalds {
32703b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3271461d8837SThomas Graf 	struct ifaddrmsg *ifm;
3272461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
32731da177e4SLinus Torvalds 	struct in6_addr *pfx;
32747198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
32757198f8ceSThomas Graf 	struct net_device *dev;
327655ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
327755ebaef1SNoriaki TAKAMIYA 	u8 ifa_flags;
3278461d8837SThomas Graf 	int err;
32791da177e4SLinus Torvalds 
3280461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3281461d8837SThomas Graf 	if (err < 0)
3282461d8837SThomas Graf 		return err;
3283461d8837SThomas Graf 
3284461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
3285461d8837SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
32861da177e4SLinus Torvalds 	if (pfx == NULL)
32871da177e4SLinus Torvalds 		return -EINVAL;
32881da177e4SLinus Torvalds 
3289461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
32900778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
3291461d8837SThomas Graf 
3292461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
32930778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
3294461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
3295461d8837SThomas Graf 	} else {
3296461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
3297461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
32980778769dSNoriaki TAKAMIYA 	}
32990778769dSNoriaki TAKAMIYA 
3300af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
33017198f8ceSThomas Graf 	if (dev == NULL)
33027198f8ceSThomas Graf 		return -ENODEV;
3303081bba5bSNoriaki TAKAMIYA 
330455ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
33053b9f9a1cSNoriaki TAKAMIYA 	ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
330655ebaef1SNoriaki TAKAMIYA 
33071cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
33087198f8ceSThomas Graf 	if (ifa == NULL) {
33097198f8ceSThomas Graf 		/*
33107198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
33117198f8ceSThomas Graf 		 * userspace alreay relies on not having to provide this.
33127198f8ceSThomas Graf 		 */
3313af284937SDaniel Lezcano 		return inet6_addr_add(net, ifm->ifa_index, pfx,
3314af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
3315af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
33161da177e4SLinus Torvalds 	}
33171da177e4SLinus Torvalds 
33187198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
33197198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
33207198f8ceSThomas Graf 		err = -EEXIST;
33217198f8ceSThomas Graf 	else
332255ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
33237198f8ceSThomas Graf 
33247198f8ceSThomas Graf 	in6_ifa_put(ifa);
33257198f8ceSThomas Graf 
33267198f8ceSThomas Graf 	return err;
33277198f8ceSThomas Graf }
33287198f8ceSThomas Graf 
3329101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3330101bb229SThomas Graf 			  u8 scope, int ifindex)
3331101bb229SThomas Graf {
3332101bb229SThomas Graf 	struct ifaddrmsg *ifm;
3333101bb229SThomas Graf 
3334101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
3335101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
3336101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
3337101bb229SThomas Graf 	ifm->ifa_flags = flags;
3338101bb229SThomas Graf 	ifm->ifa_scope = scope;
3339101bb229SThomas Graf 	ifm->ifa_index = ifindex;
3340101bb229SThomas Graf }
3341101bb229SThomas Graf 
334285486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
334385486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
334485486af0SThomas Graf {
334585486af0SThomas Graf 	struct ifa_cacheinfo ci;
334685486af0SThomas Graf 
334785486af0SThomas Graf 	ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
334885486af0SThomas Graf 			+ TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
334985486af0SThomas Graf 	ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
335085486af0SThomas Graf 			+ TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
335185486af0SThomas Graf 	ci.ifa_prefered = preferred;
335285486af0SThomas Graf 	ci.ifa_valid = valid;
335385486af0SThomas Graf 
335485486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
335585486af0SThomas Graf }
335685486af0SThomas Graf 
3357101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
3358101bb229SThomas Graf {
3359101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
3360101bb229SThomas Graf 		return RT_SCOPE_HOST;
3361101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
3362101bb229SThomas Graf 		return RT_SCOPE_LINK;
3363101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
3364101bb229SThomas Graf 		return RT_SCOPE_SITE;
3365101bb229SThomas Graf 	else
3366101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
3367101bb229SThomas Graf }
3368101bb229SThomas Graf 
33690ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
33700ab6803bSThomas Graf {
3371339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3372339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
3373339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct ifa_cacheinfo));
33740ab6803bSThomas Graf }
3375c5396a31SYOSHIFUJI Hideaki 
33761da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3377b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
33781da177e4SLinus Torvalds {
33791da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
338085486af0SThomas Graf 	u32 preferred, valid;
33811da177e4SLinus Torvalds 
33820ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
33830ab6803bSThomas Graf 	if (nlh == NULL)
338426932566SPatrick McHardy 		return -EMSGSIZE;
33850ab6803bSThomas Graf 
3386101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3387101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
3388101bb229SThomas Graf 
33891da177e4SLinus Torvalds 	if (!(ifa->flags&IFA_F_PERMANENT)) {
339085486af0SThomas Graf 		preferred = ifa->prefered_lft;
339185486af0SThomas Graf 		valid = ifa->valid_lft;
339285486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
33931da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
339485486af0SThomas Graf 			preferred -= tval;
339585486af0SThomas Graf 			if (valid != INFINITY_LIFE_TIME)
339685486af0SThomas Graf 				valid -= tval;
33971da177e4SLinus Torvalds 		}
33981da177e4SLinus Torvalds 	} else {
339985486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
340085486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
34011da177e4SLinus Torvalds 	}
340285486af0SThomas Graf 
34030ab6803bSThomas Graf 	if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
340426932566SPatrick McHardy 	    put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
340526932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
340626932566SPatrick McHardy 		return -EMSGSIZE;
340726932566SPatrick McHardy 	}
340885486af0SThomas Graf 
34090ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
34101da177e4SLinus Torvalds }
34111da177e4SLinus Torvalds 
34121da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3413e431b8c0SJamal Hadi Salim 				u32 pid, u32 seq, int event, u16 flags)
34141da177e4SLinus Torvalds {
34151da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3416101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3417101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
34181da177e4SLinus Torvalds 
34191da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3420101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3421101bb229SThomas Graf 
34220ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
34230ab6803bSThomas Graf 	if (nlh == NULL)
342426932566SPatrick McHardy 		return -EMSGSIZE;
34250ab6803bSThomas Graf 
3426101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
34270ab6803bSThomas Graf 	if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
34280ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
342926932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
343026932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
343126932566SPatrick McHardy 		return -EMSGSIZE;
343226932566SPatrick McHardy 	}
343385486af0SThomas Graf 
34340ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
34351da177e4SLinus Torvalds }
34361da177e4SLinus Torvalds 
34371da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3438b6544c0bSJamal Hadi Salim 				u32 pid, u32 seq, int event, unsigned int flags)
34391da177e4SLinus Torvalds {
34401da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3441101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3442101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
34431da177e4SLinus Torvalds 
34441da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3445101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3446101bb229SThomas Graf 
34470ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
34480ab6803bSThomas Graf 	if (nlh == NULL)
344926932566SPatrick McHardy 		return -EMSGSIZE;
34500ab6803bSThomas Graf 
3451101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
34520ab6803bSThomas Graf 	if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
34530ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
345426932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
345526932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
345626932566SPatrick McHardy 		return -EMSGSIZE;
345726932566SPatrick McHardy 	}
345885486af0SThomas Graf 
34590ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
34601da177e4SLinus Torvalds }
34611da177e4SLinus Torvalds 
34621da177e4SLinus Torvalds enum addr_type_t
34631da177e4SLinus Torvalds {
34641da177e4SLinus Torvalds 	UNICAST_ADDR,
34651da177e4SLinus Torvalds 	MULTICAST_ADDR,
34661da177e4SLinus Torvalds 	ANYCAST_ADDR,
34671da177e4SLinus Torvalds };
34681da177e4SLinus Torvalds 
34691da177e4SLinus Torvalds static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
34701da177e4SLinus Torvalds 			   enum addr_type_t type)
34711da177e4SLinus Torvalds {
34721da177e4SLinus Torvalds 	int idx, ip_idx;
34731da177e4SLinus Torvalds 	int s_idx, s_ip_idx;
34741da177e4SLinus Torvalds 	int err = 1;
34751da177e4SLinus Torvalds 	struct net_device *dev;
34761da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
34771da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
34781da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
34791da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
34803b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
34811da177e4SLinus Torvalds 
34821da177e4SLinus Torvalds 	s_idx = cb->args[0];
34831da177e4SLinus Torvalds 	s_ip_idx = ip_idx = cb->args[1];
34841da177e4SLinus Torvalds 
34857562f876SPavel Emelianov 	idx = 0;
34866fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
34871da177e4SLinus Torvalds 		if (idx < s_idx)
34887562f876SPavel Emelianov 			goto cont;
34891da177e4SLinus Torvalds 		if (idx > s_idx)
34901da177e4SLinus Torvalds 			s_ip_idx = 0;
34911da177e4SLinus Torvalds 		ip_idx = 0;
34921da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
34937562f876SPavel Emelianov 			goto cont;
34941da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
34951da177e4SLinus Torvalds 		switch (type) {
34961da177e4SLinus Torvalds 		case UNICAST_ADDR:
3497ae9cda5dSYOSHIFUJI Hideaki 			/* unicast address incl. temp addr */
34981da177e4SLinus Torvalds 			for (ifa = idev->addr_list; ifa;
34991da177e4SLinus Torvalds 			     ifa = ifa->if_next, ip_idx++) {
35001da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
35011da177e4SLinus Torvalds 					continue;
35025d5619b4SYOSHIFUJI Hideaki 				err = inet6_fill_ifaddr(skb, ifa,
35031da177e4SLinus Torvalds 							NETLINK_CB(cb->skb).pid,
35045d5619b4SYOSHIFUJI Hideaki 							cb->nlh->nlmsg_seq,
35055d5619b4SYOSHIFUJI Hideaki 							RTM_NEWADDR,
35065d5619b4SYOSHIFUJI Hideaki 							NLM_F_MULTI);
35071da177e4SLinus Torvalds 			}
35081da177e4SLinus Torvalds 			break;
35091da177e4SLinus Torvalds 		case MULTICAST_ADDR:
35101da177e4SLinus Torvalds 			/* multicast address */
35111da177e4SLinus Torvalds 			for (ifmca = idev->mc_list; ifmca;
35121da177e4SLinus Torvalds 			     ifmca = ifmca->next, ip_idx++) {
35131da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
35141da177e4SLinus Torvalds 					continue;
35155d5619b4SYOSHIFUJI Hideaki 				err = inet6_fill_ifmcaddr(skb, ifmca,
35161da177e4SLinus Torvalds 							  NETLINK_CB(cb->skb).pid,
35175d5619b4SYOSHIFUJI Hideaki 							  cb->nlh->nlmsg_seq,
35185d5619b4SYOSHIFUJI Hideaki 							  RTM_GETMULTICAST,
35195d5619b4SYOSHIFUJI Hideaki 							  NLM_F_MULTI);
35201da177e4SLinus Torvalds 			}
35211da177e4SLinus Torvalds 			break;
35221da177e4SLinus Torvalds 		case ANYCAST_ADDR:
35231da177e4SLinus Torvalds 			/* anycast address */
35241da177e4SLinus Torvalds 			for (ifaca = idev->ac_list; ifaca;
35251da177e4SLinus Torvalds 			     ifaca = ifaca->aca_next, ip_idx++) {
35261da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
35271da177e4SLinus Torvalds 					continue;
35285d5619b4SYOSHIFUJI Hideaki 				err = inet6_fill_ifacaddr(skb, ifaca,
35291da177e4SLinus Torvalds 							  NETLINK_CB(cb->skb).pid,
35305d5619b4SYOSHIFUJI Hideaki 							  cb->nlh->nlmsg_seq,
35315d5619b4SYOSHIFUJI Hideaki 							  RTM_GETANYCAST,
35325d5619b4SYOSHIFUJI Hideaki 							  NLM_F_MULTI);
35331da177e4SLinus Torvalds 			}
35341da177e4SLinus Torvalds 			break;
35351da177e4SLinus Torvalds 		default:
35361da177e4SLinus Torvalds 			break;
35371da177e4SLinus Torvalds 		}
35381da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
35391da177e4SLinus Torvalds 		in6_dev_put(idev);
35405d5619b4SYOSHIFUJI Hideaki 
35415d5619b4SYOSHIFUJI Hideaki 		if (err <= 0)
35425d5619b4SYOSHIFUJI Hideaki 			break;
35437562f876SPavel Emelianov cont:
35447562f876SPavel Emelianov 		idx++;
35451da177e4SLinus Torvalds 	}
35461da177e4SLinus Torvalds 	cb->args[0] = idx;
35471da177e4SLinus Torvalds 	cb->args[1] = ip_idx;
35481da177e4SLinus Torvalds 	return skb->len;
35491da177e4SLinus Torvalds }
35501da177e4SLinus Torvalds 
35511da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
35521da177e4SLinus Torvalds {
35531da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
3554b854272bSDenis V. Lunev 
35551da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
35561da177e4SLinus Torvalds }
35571da177e4SLinus Torvalds 
35581da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
35591da177e4SLinus Torvalds {
35601da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
3561b854272bSDenis V. Lunev 
35621da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
35631da177e4SLinus Torvalds }
35641da177e4SLinus Torvalds 
35651da177e4SLinus Torvalds 
35661da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
35671da177e4SLinus Torvalds {
35681da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
3569b854272bSDenis V. Lunev 
35701da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
35711da177e4SLinus Torvalds }
35721da177e4SLinus Torvalds 
35731b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
35741b29fc2cSThomas Graf 			     void *arg)
35756c223828SNoriaki TAKAMIYA {
35763b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
35771b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
35781b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
35796c223828SNoriaki TAKAMIYA 	struct in6_addr *addr = NULL;
35806c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
35816c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
35826c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
35836c223828SNoriaki TAKAMIYA 	int err;
35846c223828SNoriaki TAKAMIYA 
35851b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
35861b29fc2cSThomas Graf 	if (err < 0)
35871b29fc2cSThomas Graf 		goto errout;
35886c223828SNoriaki TAKAMIYA 
35891b29fc2cSThomas Graf 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
35901b29fc2cSThomas Graf 	if (addr == NULL) {
35911b29fc2cSThomas Graf 		err = -EINVAL;
35921b29fc2cSThomas Graf 		goto errout;
35931b29fc2cSThomas Graf 	}
35941b29fc2cSThomas Graf 
35951b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
35966c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
35976fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
35986c223828SNoriaki TAKAMIYA 
35991cab3da6SDaniel Lezcano 	if ((ifa = ipv6_get_ifaddr(net, addr, dev, 1)) == NULL) {
36001b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
36011b29fc2cSThomas Graf 		goto errout;
36026c223828SNoriaki TAKAMIYA 	}
36036c223828SNoriaki TAKAMIYA 
36040ab6803bSThomas Graf 	if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
36051b29fc2cSThomas Graf 		err = -ENOBUFS;
36061b29fc2cSThomas Graf 		goto errout_ifa;
36071b29fc2cSThomas Graf 	}
36081b29fc2cSThomas Graf 
36096c223828SNoriaki TAKAMIYA 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
36106c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
361126932566SPatrick McHardy 	if (err < 0) {
361226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
361326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
361426932566SPatrick McHardy 		kfree_skb(skb);
361526932566SPatrick McHardy 		goto errout_ifa;
361626932566SPatrick McHardy 	}
36176fda7350SBenjamin Thery 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
36181b29fc2cSThomas Graf errout_ifa:
36196c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
36201b29fc2cSThomas Graf errout:
36216c223828SNoriaki TAKAMIYA 	return err;
36226c223828SNoriaki TAKAMIYA }
36236c223828SNoriaki TAKAMIYA 
36241da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
36251da177e4SLinus Torvalds {
36261da177e4SLinus Torvalds 	struct sk_buff *skb;
3627c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
36285d620266SThomas Graf 	int err = -ENOBUFS;
36291da177e4SLinus Torvalds 
36300ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
36315d620266SThomas Graf 	if (skb == NULL)
36325d620266SThomas Graf 		goto errout;
36335d620266SThomas Graf 
36345d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
363526932566SPatrick McHardy 	if (err < 0) {
363626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
363726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
363826932566SPatrick McHardy 		kfree_skb(skb);
363926932566SPatrick McHardy 		goto errout;
364026932566SPatrick McHardy 	}
36416fda7350SBenjamin Thery 	err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
36425d620266SThomas Graf errout:
36435d620266SThomas Graf 	if (err < 0)
36446fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
36451da177e4SLinus Torvalds }
36461da177e4SLinus Torvalds 
3647b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
36481da177e4SLinus Torvalds 				__s32 *array, int bytes)
36491da177e4SLinus Torvalds {
365004561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
365104561c1fSThomas Graf 
36521da177e4SLinus Torvalds 	memset(array, 0, bytes);
36531da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
36541da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
36551da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
36561da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
36571da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
36581da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
36591da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
36601da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
36611da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
36621da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
36631da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
36641da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
36651da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
36661da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
36671da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
36681da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
36691da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
36701da177e4SLinus Torvalds #endif
36711da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
367265f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3673c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3674930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
3675930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
367652e16356SYOSHIFUJI Hideaki 	array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
3677fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
367809c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
367909c884d4SYOSHIFUJI Hideaki #endif
3680930d6ff2SYOSHIFUJI Hideaki #endif
3681fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
36820bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
368395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
368495c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
368595c385b4SNeil Horman #endif
36867bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
36877bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
36887bc570c8SYOSHIFUJI Hideaki #endif
3689778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
36901b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
36911da177e4SLinus Torvalds }
36921da177e4SLinus Torvalds 
3693339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
3694339bf98fSThomas Graf {
3695339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3696339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3697339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3698339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
3699339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
3700339bf98fSThomas Graf 	       + nla_total_size( /* IFLA_PROTINFO */
3701339bf98fSThomas Graf 			nla_total_size(4) /* IFLA_INET6_FLAGS */
3702339bf98fSThomas Graf 			+ nla_total_size(sizeof(struct ifla_cacheinfo))
3703339bf98fSThomas Graf 			+ nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3704bf99f1bdSYOSHIFUJI Hideaki 			+ nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3705bf99f1bdSYOSHIFUJI Hideaki 			+ nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
3706339bf98fSThomas Graf 		 );
3707339bf98fSThomas Graf }
3708c5396a31SYOSHIFUJI Hideaki 
37097f7d9a6bSHerbert Xu static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
37107f7d9a6bSHerbert Xu 				      int bytes)
37117f7d9a6bSHerbert Xu {
37127f7d9a6bSHerbert Xu 	int i;
37137f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
37147f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
37157f7d9a6bSHerbert Xu 
37167f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
37177f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
37187f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
37197f7d9a6bSHerbert Xu 		put_unaligned(snmp_fold_field(mib, i), &stats[i]);
37207f7d9a6bSHerbert Xu 
37217f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
37227f7d9a6bSHerbert Xu }
37237f7d9a6bSHerbert Xu 
37247f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
37257f7d9a6bSHerbert Xu 			     int bytes)
37267f7d9a6bSHerbert Xu {
37277f7d9a6bSHerbert Xu 	switch(attrtype) {
37287f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
37297f7d9a6bSHerbert Xu 		__snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
37307f7d9a6bSHerbert Xu 		break;
37317f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
37327f7d9a6bSHerbert Xu 		__snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
37337f7d9a6bSHerbert Xu 		break;
37347f7d9a6bSHerbert Xu 	}
37357f7d9a6bSHerbert Xu }
37367f7d9a6bSHerbert Xu 
37371da177e4SLinus Torvalds static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3738b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
37391da177e4SLinus Torvalds {
37401da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3741bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
374204561c1fSThomas Graf 	struct ifinfomsg *hdr;
37431da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
374404561c1fSThomas Graf 	void *protoinfo;
37451da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
37461da177e4SLinus Torvalds 
374704561c1fSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
374804561c1fSThomas Graf 	if (nlh == NULL)
374926932566SPatrick McHardy 		return -EMSGSIZE;
37501da177e4SLinus Torvalds 
375104561c1fSThomas Graf 	hdr = nlmsg_data(nlh);
375204561c1fSThomas Graf 	hdr->ifi_family = AF_INET6;
375304561c1fSThomas Graf 	hdr->__ifi_pad = 0;
375404561c1fSThomas Graf 	hdr->ifi_type = dev->type;
375504561c1fSThomas Graf 	hdr->ifi_index = dev->ifindex;
375604561c1fSThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
375704561c1fSThomas Graf 	hdr->ifi_change = 0;
375804561c1fSThomas Graf 
375904561c1fSThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
37601da177e4SLinus Torvalds 
37611da177e4SLinus Torvalds 	if (dev->addr_len)
376204561c1fSThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
37631da177e4SLinus Torvalds 
376404561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
37651da177e4SLinus Torvalds 	if (dev->ifindex != dev->iflink)
376604561c1fSThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
37671da177e4SLinus Torvalds 
376804561c1fSThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
376904561c1fSThomas Graf 	if (protoinfo == NULL)
377004561c1fSThomas Graf 		goto nla_put_failure;
37711da177e4SLinus Torvalds 
377204561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
37731da177e4SLinus Torvalds 
37741da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
37751da177e4SLinus Torvalds 	ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
37761da177e4SLinus Torvalds 		    + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
37771da177e4SLinus Torvalds 	ci.reachable_time = idev->nd_parms->reachable_time;
37781da177e4SLinus Torvalds 	ci.retrans_time = idev->nd_parms->retrans_time;
377904561c1fSThomas Graf 	NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
37801da177e4SLinus Torvalds 
3781bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3782bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
378304561c1fSThomas Graf 		goto nla_put_failure;
3784bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
37851da177e4SLinus Torvalds 
3786bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
3787bf99f1bdSYOSHIFUJI Hideaki 
3788bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3789bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
3790bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
3791bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3792bf99f1bdSYOSHIFUJI Hideaki 
3793bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3794bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
3795bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
3796bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
37971da177e4SLinus Torvalds 
379804561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
379904561c1fSThomas Graf 	return nlmsg_end(skb, nlh);
38001da177e4SLinus Torvalds 
380104561c1fSThomas Graf nla_put_failure:
380226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
380326932566SPatrick McHardy 	return -EMSGSIZE;
38041da177e4SLinus Torvalds }
38051da177e4SLinus Torvalds 
38061da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
38071da177e4SLinus Torvalds {
38083b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
38091da177e4SLinus Torvalds 	int idx, err;
38101da177e4SLinus Torvalds 	int s_idx = cb->args[0];
38111da177e4SLinus Torvalds 	struct net_device *dev;
38121da177e4SLinus Torvalds 	struct inet6_dev *idev;
38131da177e4SLinus Torvalds 
38141da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
38157562f876SPavel Emelianov 	idx = 0;
38166fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
38171da177e4SLinus Torvalds 		if (idx < s_idx)
38187562f876SPavel Emelianov 			goto cont;
38191da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
38207562f876SPavel Emelianov 			goto cont;
38211da177e4SLinus Torvalds 		err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3822b6544c0bSJamal Hadi Salim 				cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
38231da177e4SLinus Torvalds 		in6_dev_put(idev);
38241da177e4SLinus Torvalds 		if (err <= 0)
38251da177e4SLinus Torvalds 			break;
38267562f876SPavel Emelianov cont:
38277562f876SPavel Emelianov 		idx++;
38281da177e4SLinus Torvalds 	}
38291da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
38301da177e4SLinus Torvalds 	cb->args[0] = idx;
38311da177e4SLinus Torvalds 
38321da177e4SLinus Torvalds 	return skb->len;
38331da177e4SLinus Torvalds }
38341da177e4SLinus Torvalds 
38351da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
38361da177e4SLinus Torvalds {
38371da177e4SLinus Torvalds 	struct sk_buff *skb;
3838c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
38398d7a76c9SThomas Graf 	int err = -ENOBUFS;
38401da177e4SLinus Torvalds 
3841339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
38428d7a76c9SThomas Graf 	if (skb == NULL)
38438d7a76c9SThomas Graf 		goto errout;
38448d7a76c9SThomas Graf 
38458d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
384626932566SPatrick McHardy 	if (err < 0) {
384726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
384826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
384926932566SPatrick McHardy 		kfree_skb(skb);
385026932566SPatrick McHardy 		goto errout;
385126932566SPatrick McHardy 	}
38526fda7350SBenjamin Thery 	err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
38538d7a76c9SThomas Graf errout:
38548d7a76c9SThomas Graf 	if (err < 0)
38556fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
38561da177e4SLinus Torvalds }
38571da177e4SLinus Torvalds 
3858339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
3859339bf98fSThomas Graf {
3860339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
3861339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
3862339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
3863339bf98fSThomas Graf }
3864c5396a31SYOSHIFUJI Hideaki 
38651da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3866b6544c0bSJamal Hadi Salim 			     struct prefix_info *pinfo, u32 pid, u32 seq,
3867b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
38681da177e4SLinus Torvalds {
38691da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
38701da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
38711da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
38721da177e4SLinus Torvalds 
38736051e2f4SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
38746051e2f4SThomas Graf 	if (nlh == NULL)
387526932566SPatrick McHardy 		return -EMSGSIZE;
38766051e2f4SThomas Graf 
38776051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
38781da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
38798a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
38808a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
38811da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
38821da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
38831da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
38848a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
38851da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
38861da177e4SLinus Torvalds 	if (pinfo->onlink)
38871da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
38881da177e4SLinus Torvalds 	if (pinfo->autoconf)
38891da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
38901da177e4SLinus Torvalds 
38916051e2f4SThomas Graf 	NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
38921da177e4SLinus Torvalds 
38931da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
38941da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
38956051e2f4SThomas Graf 	NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
38961da177e4SLinus Torvalds 
38976051e2f4SThomas Graf 	return nlmsg_end(skb, nlh);
38981da177e4SLinus Torvalds 
38996051e2f4SThomas Graf nla_put_failure:
390026932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
390126932566SPatrick McHardy 	return -EMSGSIZE;
39021da177e4SLinus Torvalds }
39031da177e4SLinus Torvalds 
39041da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
39051da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
39061da177e4SLinus Torvalds {
39071da177e4SLinus Torvalds 	struct sk_buff *skb;
3908c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
39098c384bfaSThomas Graf 	int err = -ENOBUFS;
39101da177e4SLinus Torvalds 
3911339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
39128c384bfaSThomas Graf 	if (skb == NULL)
39138c384bfaSThomas Graf 		goto errout;
39148c384bfaSThomas Graf 
39158c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
391626932566SPatrick McHardy 	if (err < 0) {
391726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
391826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
391926932566SPatrick McHardy 		kfree_skb(skb);
392026932566SPatrick McHardy 		goto errout;
392126932566SPatrick McHardy 	}
39226fda7350SBenjamin Thery 	err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
39238c384bfaSThomas Graf errout:
39248c384bfaSThomas Graf 	if (err < 0)
39256fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
39261da177e4SLinus Torvalds }
39271da177e4SLinus Torvalds 
39281da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
39291da177e4SLinus Torvalds {
39301da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
39311da177e4SLinus Torvalds 
39321da177e4SLinus Torvalds 	switch (event) {
39331da177e4SLinus Torvalds 	case RTM_NEWADDR:
393495c385b4SNeil Horman 		/*
393595c385b4SNeil Horman 		 * If the address was optimistic
393695c385b4SNeil Horman 		 * we inserted the route at the start of
393795c385b4SNeil Horman 		 * our DAD process, so we don't need
393895c385b4SNeil Horman 		 * to do it again
393995c385b4SNeil Horman 		 */
394095c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
394140e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
39421da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
39431da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
39441da177e4SLinus Torvalds 		break;
39451da177e4SLinus Torvalds 	case RTM_DELADDR:
39461da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
39471da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
39481da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
39491da177e4SLinus Torvalds 		dst_hold(&ifp->rt->u.dst);
3950e0a1ad73SThomas Graf 		if (ip6_del_rt(ifp->rt))
39511da177e4SLinus Torvalds 			dst_free(&ifp->rt->u.dst);
39521da177e4SLinus Torvalds 		break;
39531da177e4SLinus Torvalds 	}
39541da177e4SLinus Torvalds }
39551da177e4SLinus Torvalds 
39561da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
39571da177e4SLinus Torvalds {
39588814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
39591da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
39601da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
39618814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
39621da177e4SLinus Torvalds }
39631da177e4SLinus Torvalds 
39641da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
39651da177e4SLinus Torvalds 
39661da177e4SLinus Torvalds static
39671da177e4SLinus Torvalds int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
39681da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
39691da177e4SLinus Torvalds {
39701da177e4SLinus Torvalds 	int *valp = ctl->data;
39711da177e4SLinus Torvalds 	int val = *valp;
39721da177e4SLinus Torvalds 	int ret;
39731da177e4SLinus Torvalds 
39741da177e4SLinus Torvalds 	ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
39751da177e4SLinus Torvalds 
3976c8fecf22SPavel Emelyanov 	if (write)
3977c8fecf22SPavel Emelyanov 		addrconf_fixup_forwarding(ctl, valp, val);
39781da177e4SLinus Torvalds 	return ret;
39791da177e4SLinus Torvalds }
39801da177e4SLinus Torvalds 
39811da177e4SLinus Torvalds static int addrconf_sysctl_forward_strategy(ctl_table *table,
39821da177e4SLinus Torvalds 					    int __user *name, int nlen,
39831da177e4SLinus Torvalds 					    void __user *oldval,
39841da177e4SLinus Torvalds 					    size_t __user *oldlenp,
39851f29bcd7SAlexey Dobriyan 					    void __user *newval, size_t newlen)
39861da177e4SLinus Torvalds {
39871da177e4SLinus Torvalds 	int *valp = table->data;
3988c8fecf22SPavel Emelyanov 	int val = *valp;
39891da177e4SLinus Torvalds 	int new;
39901da177e4SLinus Torvalds 
39911da177e4SLinus Torvalds 	if (!newval || !newlen)
39921da177e4SLinus Torvalds 		return 0;
39931da177e4SLinus Torvalds 	if (newlen != sizeof(int))
39941da177e4SLinus Torvalds 		return -EINVAL;
39951da177e4SLinus Torvalds 	if (get_user(new, (int __user *)newval))
39961da177e4SLinus Torvalds 		return -EFAULT;
39971da177e4SLinus Torvalds 	if (new == *valp)
39981da177e4SLinus Torvalds 		return 0;
39991da177e4SLinus Torvalds 	if (oldval && oldlenp) {
40001da177e4SLinus Torvalds 		size_t len;
40011da177e4SLinus Torvalds 		if (get_user(len, oldlenp))
40021da177e4SLinus Torvalds 			return -EFAULT;
40031da177e4SLinus Torvalds 		if (len) {
40041da177e4SLinus Torvalds 			if (len > table->maxlen)
40051da177e4SLinus Torvalds 				len = table->maxlen;
40061da177e4SLinus Torvalds 			if (copy_to_user(oldval, valp, len))
40071da177e4SLinus Torvalds 				return -EFAULT;
40081da177e4SLinus Torvalds 			if (put_user(len, oldlenp))
40091da177e4SLinus Torvalds 				return -EFAULT;
40101da177e4SLinus Torvalds 		}
40111da177e4SLinus Torvalds 	}
40121da177e4SLinus Torvalds 
40131da177e4SLinus Torvalds 	*valp = new;
4014c8fecf22SPavel Emelyanov 	addrconf_fixup_forwarding(table, valp, val);
40151da177e4SLinus Torvalds 	return 1;
40161da177e4SLinus Torvalds }
40171da177e4SLinus Torvalds 
40181da177e4SLinus Torvalds static struct addrconf_sysctl_table
40191da177e4SLinus Torvalds {
40201da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
4021f6a07b29SYOSHIFUJI Hideaki 	ctl_table addrconf_vars[DEVCONF_MAX+1];
40221dab6222SPavel Emelyanov 	char *dev_name;
4023ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
40241da177e4SLinus Torvalds 	.sysctl_header = NULL,
40251da177e4SLinus Torvalds 	.addrconf_vars = {
40261da177e4SLinus Torvalds 		{
40271da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORWARDING,
40281da177e4SLinus Torvalds 			.procname	=	"forwarding",
40291da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.forwarding,
40301da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40311da177e4SLinus Torvalds 			.mode		=	0644,
40321da177e4SLinus Torvalds 			.proc_handler	=	&addrconf_sysctl_forward,
40331da177e4SLinus Torvalds 			.strategy	=	&addrconf_sysctl_forward_strategy,
40341da177e4SLinus Torvalds 		},
40351da177e4SLinus Torvalds 		{
40361da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_HOP_LIMIT,
40371da177e4SLinus Torvalds 			.procname	=	"hop_limit",
40381da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.hop_limit,
40391da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40401da177e4SLinus Torvalds 			.mode		=	0644,
40411da177e4SLinus Torvalds 			.proc_handler	=	proc_dointvec,
40421da177e4SLinus Torvalds 		},
40431da177e4SLinus Torvalds 		{
40441da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MTU,
40451da177e4SLinus Torvalds 			.procname	=	"mtu",
40461da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.mtu6,
40471da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40481da177e4SLinus Torvalds 			.mode		=	0644,
40491da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40501da177e4SLinus Torvalds 		},
40511da177e4SLinus Torvalds 		{
40521da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_RA,
40531da177e4SLinus Torvalds 			.procname	=	"accept_ra",
40541da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.accept_ra,
40551da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40561da177e4SLinus Torvalds 			.mode		=	0644,
40571da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40581da177e4SLinus Torvalds 		},
40591da177e4SLinus Torvalds 		{
40601da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_REDIRECTS,
40611da177e4SLinus Torvalds 			.procname	=	"accept_redirects",
40621da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.accept_redirects,
40631da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40641da177e4SLinus Torvalds 			.mode		=	0644,
40651da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40661da177e4SLinus Torvalds 		},
40671da177e4SLinus Torvalds 		{
40681da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_AUTOCONF,
40691da177e4SLinus Torvalds 			.procname	=	"autoconf",
40701da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.autoconf,
40711da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40721da177e4SLinus Torvalds 			.mode		=	0644,
40731da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40741da177e4SLinus Torvalds 		},
40751da177e4SLinus Torvalds 		{
40761da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_DAD_TRANSMITS,
40771da177e4SLinus Torvalds 			.procname	=	"dad_transmits",
40781da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.dad_transmits,
40791da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40801da177e4SLinus Torvalds 			.mode		=	0644,
40811da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40821da177e4SLinus Torvalds 		},
40831da177e4SLinus Torvalds 		{
40841da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICITS,
40851da177e4SLinus Torvalds 			.procname	=	"router_solicitations",
40861da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicits,
40871da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40881da177e4SLinus Torvalds 			.mode		=	0644,
40891da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40901da177e4SLinus Torvalds 		},
40911da177e4SLinus Torvalds 		{
40921da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_INTERVAL,
40931da177e4SLinus Torvalds 			.procname	=	"router_solicitation_interval",
40941da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicit_interval,
40951da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40961da177e4SLinus Torvalds 			.mode		=	0644,
40971da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec_jiffies,
40981da177e4SLinus Torvalds 			.strategy	=	&sysctl_jiffies,
40991da177e4SLinus Torvalds 		},
41001da177e4SLinus Torvalds 		{
41011da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_DELAY,
41021da177e4SLinus Torvalds 			.procname	=	"router_solicitation_delay",
41031da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicit_delay,
41041da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41051da177e4SLinus Torvalds 			.mode		=	0644,
41061da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec_jiffies,
41071da177e4SLinus Torvalds 			.strategy	=	&sysctl_jiffies,
41081da177e4SLinus Torvalds 		},
41091da177e4SLinus Torvalds 		{
41101da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORCE_MLD_VERSION,
41111da177e4SLinus Torvalds 			.procname	=	"force_mld_version",
41121da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.force_mld_version,
41131da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41141da177e4SLinus Torvalds 			.mode		=	0644,
41151da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41161da177e4SLinus Torvalds 		},
41171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
41181da177e4SLinus Torvalds 		{
41191da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_USE_TEMPADDR,
41201da177e4SLinus Torvalds 			.procname	=	"use_tempaddr",
41211da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.use_tempaddr,
41221da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41231da177e4SLinus Torvalds 			.mode		=	0644,
41241da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41251da177e4SLinus Torvalds 		},
41261da177e4SLinus Torvalds 		{
41271da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_VALID_LFT,
41281da177e4SLinus Torvalds 			.procname	=	"temp_valid_lft",
41291da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.temp_valid_lft,
41301da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41311da177e4SLinus Torvalds 			.mode		=	0644,
41321da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41331da177e4SLinus Torvalds 		},
41341da177e4SLinus Torvalds 		{
41351da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_PREFERED_LFT,
41361da177e4SLinus Torvalds 			.procname	=	"temp_prefered_lft",
41371da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.temp_prefered_lft,
41381da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41391da177e4SLinus Torvalds 			.mode		=	0644,
41401da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41411da177e4SLinus Torvalds 		},
41421da177e4SLinus Torvalds 		{
41431da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_REGEN_MAX_RETRY,
41441da177e4SLinus Torvalds 			.procname	=	"regen_max_retry",
41451da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.regen_max_retry,
41461da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41471da177e4SLinus Torvalds 			.mode		=	0644,
41481da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41491da177e4SLinus Torvalds 		},
41501da177e4SLinus Torvalds 		{
41511da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_DESYNC_FACTOR,
41521da177e4SLinus Torvalds 			.procname	=	"max_desync_factor",
41531da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_desync_factor,
41541da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41551da177e4SLinus Torvalds 			.mode		=	0644,
41561da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41571da177e4SLinus Torvalds 		},
41581da177e4SLinus Torvalds #endif
41591da177e4SLinus Torvalds 		{
41601da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_ADDRESSES,
41611da177e4SLinus Torvalds 			.procname	=	"max_addresses",
41621da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_addresses,
41631da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41641da177e4SLinus Torvalds 			.mode		=	0644,
41651da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41661da177e4SLinus Torvalds 		},
41671da177e4SLinus Torvalds 		{
416865f5c7c1SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_DEFRTR,
416965f5c7c1SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_defrtr",
417065f5c7c1SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_defrtr,
417165f5c7c1SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
417265f5c7c1SYOSHIFUJI Hideaki 			.mode		=	0644,
417365f5c7c1SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
417465f5c7c1SYOSHIFUJI Hideaki 		},
417565f5c7c1SYOSHIFUJI Hideaki 		{
4176c4fd30ebSYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_PINFO,
4177c4fd30ebSYOSHIFUJI Hideaki 			.procname	=	"accept_ra_pinfo",
4178c4fd30ebSYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_pinfo,
4179c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4180c4fd30ebSYOSHIFUJI Hideaki 			.mode		=	0644,
4181c4fd30ebSYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4182c4fd30ebSYOSHIFUJI Hideaki 		},
4183930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4184930d6ff2SYOSHIFUJI Hideaki 		{
4185930d6ff2SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RTR_PREF,
4186930d6ff2SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_rtr_pref",
4187930d6ff2SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_rtr_pref,
4188930d6ff2SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4189930d6ff2SYOSHIFUJI Hideaki 			.mode		=	0644,
4190930d6ff2SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4191930d6ff2SYOSHIFUJI Hideaki 		},
419252e16356SYOSHIFUJI Hideaki 		{
419352e16356SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_RTR_PROBE_INTERVAL,
419452e16356SYOSHIFUJI Hideaki 			.procname	=	"router_probe_interval",
419552e16356SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.rtr_probe_interval,
419652e16356SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
419752e16356SYOSHIFUJI Hideaki 			.mode		=	0644,
419852e16356SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec_jiffies,
419952e16356SYOSHIFUJI Hideaki 			.strategy	=	&sysctl_jiffies,
420052e16356SYOSHIFUJI Hideaki 		},
4201fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
420209c884d4SYOSHIFUJI Hideaki 		{
420309c884d4SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
420409c884d4SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_rt_info_max_plen",
420509c884d4SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_rt_info_max_plen,
420609c884d4SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
420709c884d4SYOSHIFUJI Hideaki 			.mode		=	0644,
420809c884d4SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
420909c884d4SYOSHIFUJI Hideaki 		},
421009c884d4SYOSHIFUJI Hideaki #endif
4211930d6ff2SYOSHIFUJI Hideaki #endif
4212c4fd30ebSYOSHIFUJI Hideaki 		{
4213fbea49e1SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_PROXY_NDP,
4214fbea49e1SYOSHIFUJI Hideaki 			.procname	=	"proxy_ndp",
4215fbea49e1SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.proxy_ndp,
4216fbea49e1SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4217fbea49e1SYOSHIFUJI Hideaki 			.mode		=	0644,
4218fbea49e1SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4219fbea49e1SYOSHIFUJI Hideaki 		},
4220fbea49e1SYOSHIFUJI Hideaki 		{
42210bcbc926SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_SOURCE_ROUTE,
42220bcbc926SYOSHIFUJI Hideaki 			.procname	=	"accept_source_route",
42230bcbc926SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_source_route,
42240bcbc926SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
42250bcbc926SYOSHIFUJI Hideaki 			.mode		=	0644,
42260bcbc926SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
42270bcbc926SYOSHIFUJI Hideaki 		},
422895c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
422995c385b4SNeil Horman 		{
423095c385b4SNeil Horman 			.ctl_name	=	CTL_UNNUMBERED,
423195c385b4SNeil Horman 			.procname       =       "optimistic_dad",
423295c385b4SNeil Horman 			.data           =       &ipv6_devconf.optimistic_dad,
423395c385b4SNeil Horman 			.maxlen         =       sizeof(int),
423495c385b4SNeil Horman 			.mode           =       0644,
423595c385b4SNeil Horman 			.proc_handler   =       &proc_dointvec,
423695c385b4SNeil Horman 
423795c385b4SNeil Horman 		},
423895c385b4SNeil Horman #endif
42397bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
42407bc570c8SYOSHIFUJI Hideaki 		{
42417bc570c8SYOSHIFUJI Hideaki 			.ctl_name	=	CTL_UNNUMBERED,
42427bc570c8SYOSHIFUJI Hideaki 			.procname	=	"mc_forwarding",
42437bc570c8SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.mc_forwarding,
42447bc570c8SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
42457bc570c8SYOSHIFUJI Hideaki 			.mode		=	0644,
42467bc570c8SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
42477bc570c8SYOSHIFUJI Hideaki 		},
42487bc570c8SYOSHIFUJI Hideaki #endif
42490bcbc926SYOSHIFUJI Hideaki 		{
4250778d80beSYOSHIFUJI Hideaki 			.ctl_name	=	CTL_UNNUMBERED,
4251778d80beSYOSHIFUJI Hideaki 			.procname	=	"disable_ipv6",
4252778d80beSYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.disable_ipv6,
4253778d80beSYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4254778d80beSYOSHIFUJI Hideaki 			.mode		=	0644,
4255778d80beSYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4256778d80beSYOSHIFUJI Hideaki 		},
4257778d80beSYOSHIFUJI Hideaki 		{
42581b34be74SYOSHIFUJI Hideaki 			.ctl_name	=	CTL_UNNUMBERED,
42591b34be74SYOSHIFUJI Hideaki 			.procname	=	"accept_dad",
42601b34be74SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_dad,
42611b34be74SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
42621b34be74SYOSHIFUJI Hideaki 			.mode		=	0644,
42631b34be74SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
42641b34be74SYOSHIFUJI Hideaki 		},
42651b34be74SYOSHIFUJI Hideaki 		{
42661da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
42671da177e4SLinus Torvalds 		}
42681da177e4SLinus Torvalds 	},
42691da177e4SLinus Torvalds };
42701da177e4SLinus Torvalds 
4271bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4272bff16c2fSPavel Emelyanov 		int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p)
42731da177e4SLinus Torvalds {
42741da177e4SLinus Torvalds 	int i;
42751da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
42761da177e4SLinus Torvalds 
42771dab6222SPavel Emelyanov #define ADDRCONF_CTL_PATH_DEV	3
42781dab6222SPavel Emelyanov 
42791dab6222SPavel Emelyanov 	struct ctl_path addrconf_ctl_path[] = {
42801dab6222SPavel Emelyanov 		{ .procname = "net", .ctl_name = CTL_NET, },
42811dab6222SPavel Emelyanov 		{ .procname = "ipv6", .ctl_name = NET_IPV6, },
42821dab6222SPavel Emelyanov 		{ .procname = "conf", .ctl_name = NET_IPV6_CONF, },
42831dab6222SPavel Emelyanov 		{ /* to be set */ },
42841dab6222SPavel Emelyanov 		{ },
42851dab6222SPavel Emelyanov 	};
42861dab6222SPavel Emelyanov 
42871dab6222SPavel Emelyanov 
4288af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
42891da177e4SLinus Torvalds 	if (t == NULL)
4290f68635e6SPavel Emelyanov 		goto out;
4291f68635e6SPavel Emelyanov 
42921da177e4SLinus Torvalds 	for (i=0; t->addrconf_vars[i].data; i++) {
42931da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
42941da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4295bff16c2fSPavel Emelyanov 		t->addrconf_vars[i].extra2 = net;
42961da177e4SLinus Torvalds 	}
42971da177e4SLinus Torvalds 
42981da177e4SLinus Torvalds 	/*
42991da177e4SLinus Torvalds 	 * Make a copy of dev_name, because '.procname' is regarded as const
43001da177e4SLinus Torvalds 	 * by sysctl and we wouldn't want anyone to change it under our feet
43011da177e4SLinus Torvalds 	 * (see SIOCSIFNAME).
43021da177e4SLinus Torvalds 	 */
43031dab6222SPavel Emelyanov 	t->dev_name = kstrdup(dev_name, GFP_KERNEL);
43041dab6222SPavel Emelyanov 	if (!t->dev_name)
43051da177e4SLinus Torvalds 		goto free;
43061da177e4SLinus Torvalds 
43071dab6222SPavel Emelyanov 	addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
43081dab6222SPavel Emelyanov 	addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
43091da177e4SLinus Torvalds 
4310bff16c2fSPavel Emelyanov 	t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
43111dab6222SPavel Emelyanov 			t->addrconf_vars);
43121da177e4SLinus Torvalds 	if (t->sysctl_header == NULL)
43131da177e4SLinus Torvalds 		goto free_procname;
4314f68635e6SPavel Emelyanov 
43151da177e4SLinus Torvalds 	p->sysctl = t;
431695897312SPavel Emelyanov 	return 0;
43171da177e4SLinus Torvalds 
43181da177e4SLinus Torvalds free_procname:
43191dab6222SPavel Emelyanov 	kfree(t->dev_name);
43201da177e4SLinus Torvalds free:
43211da177e4SLinus Torvalds 	kfree(t);
4322f68635e6SPavel Emelyanov out:
432395897312SPavel Emelyanov 	return -ENOBUFS;
43241da177e4SLinus Torvalds }
43251da177e4SLinus Torvalds 
4326408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4327f52295a9SPavel Emelyanov {
4328408c4768SPavel Emelyanov 	struct addrconf_sysctl_table *t;
4329f52295a9SPavel Emelyanov 
4330408c4768SPavel Emelyanov 	if (p->sysctl == NULL)
4331408c4768SPavel Emelyanov 		return;
4332408c4768SPavel Emelyanov 
4333408c4768SPavel Emelyanov 	t = p->sysctl;
43341da177e4SLinus Torvalds 	p->sysctl = NULL;
43351da177e4SLinus Torvalds 	unregister_sysctl_table(t->sysctl_header);
43361dab6222SPavel Emelyanov 	kfree(t->dev_name);
43371da177e4SLinus Torvalds 	kfree(t);
43381da177e4SLinus Torvalds }
4339408c4768SPavel Emelyanov 
4340408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev)
4341408c4768SPavel Emelyanov {
4342408c4768SPavel Emelyanov 	neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6,
4343408c4768SPavel Emelyanov 			      NET_IPV6_NEIGH, "ipv6",
4344408c4768SPavel Emelyanov 			      &ndisc_ifinfo_sysctl_change,
43450686caa3SYOSHIFUJI Hideaki 			      ndisc_ifinfo_sysctl_strategy);
4346c346dca1SYOSHIFUJI Hideaki 	__addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
4347bff16c2fSPavel Emelyanov 			idev->dev->ifindex, idev, &idev->cnf);
4348408c4768SPavel Emelyanov }
4349408c4768SPavel Emelyanov 
4350408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
4351408c4768SPavel Emelyanov {
4352408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
4353408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
43541da177e4SLinus Torvalds }
43551da177e4SLinus Torvalds 
43561da177e4SLinus Torvalds 
43571da177e4SLinus Torvalds #endif
43581da177e4SLinus Torvalds 
4359e0da5a48SPavel Emelyanov static int addrconf_init_net(struct net *net)
4360e0da5a48SPavel Emelyanov {
4361e0da5a48SPavel Emelyanov 	int err;
4362e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
4363e0da5a48SPavel Emelyanov 
4364e0da5a48SPavel Emelyanov 	err = -ENOMEM;
4365e0da5a48SPavel Emelyanov 	all = &ipv6_devconf;
4366e0da5a48SPavel Emelyanov 	dflt = &ipv6_devconf_dflt;
4367e0da5a48SPavel Emelyanov 
4368e0da5a48SPavel Emelyanov 	if (net != &init_net) {
4369e0da5a48SPavel Emelyanov 		all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
4370e0da5a48SPavel Emelyanov 		if (all == NULL)
4371e0da5a48SPavel Emelyanov 			goto err_alloc_all;
4372e0da5a48SPavel Emelyanov 
4373e0da5a48SPavel Emelyanov 		dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4374e0da5a48SPavel Emelyanov 		if (dflt == NULL)
4375e0da5a48SPavel Emelyanov 			goto err_alloc_dflt;
4376e0da5a48SPavel Emelyanov 	}
4377e0da5a48SPavel Emelyanov 
4378e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
4379e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
4380e0da5a48SPavel Emelyanov 
4381e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4382e0da5a48SPavel Emelyanov 	err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL,
4383e0da5a48SPavel Emelyanov 			NULL, all);
4384e0da5a48SPavel Emelyanov 	if (err < 0)
4385e0da5a48SPavel Emelyanov 		goto err_reg_all;
4386e0da5a48SPavel Emelyanov 
4387e0da5a48SPavel Emelyanov 	err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT,
4388e0da5a48SPavel Emelyanov 			NULL, dflt);
4389e0da5a48SPavel Emelyanov 	if (err < 0)
4390e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
4391e0da5a48SPavel Emelyanov #endif
4392e0da5a48SPavel Emelyanov 	return 0;
4393e0da5a48SPavel Emelyanov 
4394e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4395e0da5a48SPavel Emelyanov err_reg_dflt:
4396e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
4397e0da5a48SPavel Emelyanov err_reg_all:
4398e0da5a48SPavel Emelyanov 	kfree(dflt);
4399e0da5a48SPavel Emelyanov #endif
4400e0da5a48SPavel Emelyanov err_alloc_dflt:
4401e0da5a48SPavel Emelyanov 	kfree(all);
4402e0da5a48SPavel Emelyanov err_alloc_all:
4403e0da5a48SPavel Emelyanov 	return err;
4404e0da5a48SPavel Emelyanov }
4405e0da5a48SPavel Emelyanov 
4406e0da5a48SPavel Emelyanov static void addrconf_exit_net(struct net *net)
4407e0da5a48SPavel Emelyanov {
4408e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4409e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4410e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
4411e0da5a48SPavel Emelyanov #endif
4412e0da5a48SPavel Emelyanov 	if (net != &init_net) {
4413e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_dflt);
4414e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_all);
4415e0da5a48SPavel Emelyanov 	}
4416e0da5a48SPavel Emelyanov }
4417e0da5a48SPavel Emelyanov 
4418e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
4419e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
4420e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
4421e0da5a48SPavel Emelyanov };
4422e0da5a48SPavel Emelyanov 
44231da177e4SLinus Torvalds /*
44241da177e4SLinus Torvalds  *      Device notifier
44251da177e4SLinus Torvalds  */
44261da177e4SLinus Torvalds 
44271da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb)
44281da177e4SLinus Torvalds {
4429e041c683SAlan Stern 	return atomic_notifier_chain_register(&inet6addr_chain, nb);
44301da177e4SLinus Torvalds }
44311da177e4SLinus Torvalds 
44327159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier);
44337159039aSYOSHIFUJI Hideaki 
44341da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb)
44351da177e4SLinus Torvalds {
4436e041c683SAlan Stern 	return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
44371da177e4SLinus Torvalds }
44381da177e4SLinus Torvalds 
44397159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier);
44407159039aSYOSHIFUJI Hideaki 
4441af284937SDaniel Lezcano static void addrconf_net_exit(struct net *net)
4442af284937SDaniel Lezcano {
44436fda7350SBenjamin Thery 	struct net_device *dev;
44446fda7350SBenjamin Thery 
44456fda7350SBenjamin Thery 	rtnl_lock();
44466fda7350SBenjamin Thery 	/* clean dev list */
44476fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
44486fda7350SBenjamin Thery 		if (__in6_dev_get(dev) == NULL)
44496fda7350SBenjamin Thery 			continue;
44506fda7350SBenjamin Thery 		addrconf_ifdown(dev, 1);
44516fda7350SBenjamin Thery 	}
44526fda7350SBenjamin Thery 	addrconf_ifdown(net->loopback_dev, 2);
44536fda7350SBenjamin Thery 	rtnl_unlock();
4454af284937SDaniel Lezcano }
4455af284937SDaniel Lezcano 
4456af284937SDaniel Lezcano static struct pernet_operations addrconf_net_ops = {
4457af284937SDaniel Lezcano 	.exit = addrconf_net_exit,
4458af284937SDaniel Lezcano };
4459af284937SDaniel Lezcano 
44601da177e4SLinus Torvalds /*
44611da177e4SLinus Torvalds  *	Init / cleanup code
44621da177e4SLinus Torvalds  */
44631da177e4SLinus Torvalds 
44641da177e4SLinus Torvalds int __init addrconf_init(void)
44651da177e4SLinus Torvalds {
44662a8cc6c8SYOSHIFUJI Hideaki 	int err;
44672a8cc6c8SYOSHIFUJI Hideaki 
44682a8cc6c8SYOSHIFUJI Hideaki 	if ((err = ipv6_addr_label_init()) < 0) {
44692a8cc6c8SYOSHIFUJI Hideaki 		printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
44702a8cc6c8SYOSHIFUJI Hideaki 			err);
44712a8cc6c8SYOSHIFUJI Hideaki 		return err;
44722a8cc6c8SYOSHIFUJI Hideaki 	}
44731da177e4SLinus Torvalds 
4474e0da5a48SPavel Emelyanov 	register_pernet_subsys(&addrconf_ops);
4475e0da5a48SPavel Emelyanov 
44761da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
44771da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
44781da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
44791da177e4SLinus Torvalds 	 * to other devices which are up.
44801da177e4SLinus Torvalds 	 *
44811da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
44821da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
44831da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
44841da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
44851da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
44861da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
44871da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
44881da177e4SLinus Torvalds 	 *
44891da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
44901da177e4SLinus Torvalds 	 * loopback_dev first by hand.
44911da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
44921da177e4SLinus Torvalds 	 * device and it being up should be removed.
44931da177e4SLinus Torvalds 	 */
44941da177e4SLinus Torvalds 	rtnl_lock();
44952774c7abSEric W. Biederman 	if (!ipv6_add_dev(init_net.loopback_dev))
44961da177e4SLinus Torvalds 		err = -ENOMEM;
44971da177e4SLinus Torvalds 	rtnl_unlock();
44981da177e4SLinus Torvalds 	if (err)
4499e0da5a48SPavel Emelyanov 		goto errlo;
45001da177e4SLinus Torvalds 
4501af284937SDaniel Lezcano 	err = register_pernet_device(&addrconf_net_ops);
4502af284937SDaniel Lezcano 	if (err)
4503af284937SDaniel Lezcano 		return err;
4504af284937SDaniel Lezcano 
45051da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
45061da177e4SLinus Torvalds 
45071da177e4SLinus Torvalds 	addrconf_verify(0);
4508c127ea2cSThomas Graf 
4509c127ea2cSThomas Graf 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4510c127ea2cSThomas Graf 	if (err < 0)
4511c127ea2cSThomas Graf 		goto errout;
4512c127ea2cSThomas Graf 
4513c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
4514c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4515c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4516c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4517c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4518c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4519c127ea2cSThomas Graf 
45202a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
45212a8cc6c8SYOSHIFUJI Hideaki 
45221da177e4SLinus Torvalds 	return 0;
4523c127ea2cSThomas Graf errout:
4524c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
4525e0da5a48SPavel Emelyanov errlo:
4526e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
4527c127ea2cSThomas Graf 
4528c127ea2cSThomas Graf 	return err;
45291da177e4SLinus Torvalds }
45301da177e4SLinus Torvalds 
453109f7709fSDaniel Lezcano void addrconf_cleanup(void)
45321da177e4SLinus Torvalds {
45331da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
45341da177e4SLinus Torvalds 	int i;
45351da177e4SLinus Torvalds 
45361da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
4537af284937SDaniel Lezcano 	unregister_pernet_device(&addrconf_net_ops);
45381da177e4SLinus Torvalds 
4539e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
45401da177e4SLinus Torvalds 
45411da177e4SLinus Torvalds 	rtnl_lock();
45421da177e4SLinus Torvalds 
45431da177e4SLinus Torvalds 	/*
45441da177e4SLinus Torvalds 	 *	Check hash table.
45451da177e4SLinus Torvalds 	 */
45461da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
45471da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
45481da177e4SLinus Torvalds 		for (ifa=inet6_addr_lst[i]; ifa; ) {
45491da177e4SLinus Torvalds 			struct inet6_ifaddr *bifa;
45501da177e4SLinus Torvalds 
45511da177e4SLinus Torvalds 			bifa = ifa;
45521da177e4SLinus Torvalds 			ifa = ifa->lst_next;
45531da177e4SLinus Torvalds 			printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
45541da177e4SLinus Torvalds 			/* Do not free it; something is wrong.
45551da177e4SLinus Torvalds 			   Now we can investigate it with debugger.
45561da177e4SLinus Torvalds 			 */
45571da177e4SLinus Torvalds 		}
45581da177e4SLinus Torvalds 	}
45591da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
45601da177e4SLinus Torvalds 
45611da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
45621da177e4SLinus Torvalds 	rtnl_unlock();
4563af284937SDaniel Lezcano 
4564af284937SDaniel Lezcano 	unregister_pernet_subsys(&addrconf_net_ops);
45651da177e4SLinus Torvalds }
4566