xref: /openbmc/linux/net/ipv6/addrconf.c (revision 3c40090a0f5b69deecc5ca615f994957f949333d)
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  *	$Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
121da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
131da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
141da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds /*
181da177e4SLinus Torvalds  *	Changes:
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *	Janos Farkas			:	delete timer on ifdown
211da177e4SLinus Torvalds  *	<chexum@bankinf.banki.hu>
221da177e4SLinus Torvalds  *	Andi Kleen			:	kill double kfree on module
231da177e4SLinus Torvalds  *						unload.
241da177e4SLinus Torvalds  *	Maciej W. Rozycki		:	FDDI support
251da177e4SLinus Torvalds  *	sekiya@USAGI			:	Don't send too many RS
261da177e4SLinus Torvalds  *						packets.
271da177e4SLinus Torvalds  *	yoshfuji@USAGI			:       Fixed interval between DAD
281da177e4SLinus Torvalds  *						packets.
291da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	improved accuracy of
301da177e4SLinus Torvalds  *						address validation timer.
311da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	Privacy Extensions (RFC3041)
321da177e4SLinus Torvalds  *						support.
331da177e4SLinus Torvalds  *	Yuji SEKIYA @USAGI		:	Don't assign a same IPv6
341da177e4SLinus Torvalds  *						address on a same interface.
351da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	ARCnet support
361da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	convert /proc/net/if_inet6 to
371da177e4SLinus Torvalds  *						seq_file.
38b1cacb68SYOSHIFUJI Hideaki  *	YOSHIFUJI Hideaki @USAGI	:	improved source address
39b1cacb68SYOSHIFUJI Hideaki  *						selection; consider scope,
40b1cacb68SYOSHIFUJI Hideaki  *						status etc.
411da177e4SLinus Torvalds  */
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <linux/errno.h>
441da177e4SLinus Torvalds #include <linux/types.h>
451da177e4SLinus Torvalds #include <linux/socket.h>
461da177e4SLinus Torvalds #include <linux/sockios.h>
471da177e4SLinus Torvalds #include <linux/net.h>
481da177e4SLinus Torvalds #include <linux/in6.h>
491da177e4SLinus Torvalds #include <linux/netdevice.h>
501823730fSThomas Graf #include <linux/if_addr.h>
511da177e4SLinus Torvalds #include <linux/if_arp.h>
521da177e4SLinus Torvalds #include <linux/if_arcnet.h>
531da177e4SLinus Torvalds #include <linux/if_infiniband.h>
541da177e4SLinus Torvalds #include <linux/route.h>
551da177e4SLinus Torvalds #include <linux/inetdevice.h>
561da177e4SLinus Torvalds #include <linux/init.h>
571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
581da177e4SLinus Torvalds #include <linux/sysctl.h>
591da177e4SLinus Torvalds #endif
604fc268d2SRandy Dunlap #include <linux/capability.h>
611da177e4SLinus Torvalds #include <linux/delay.h>
621da177e4SLinus Torvalds #include <linux/notifier.h>
63543537bdSPaulo Marques #include <linux/string.h>
641da177e4SLinus Torvalds 
65457c4cbcSEric W. Biederman #include <net/net_namespace.h>
661da177e4SLinus Torvalds #include <net/sock.h>
671da177e4SLinus Torvalds #include <net/snmp.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #include <net/ipv6.h>
701da177e4SLinus Torvalds #include <net/protocol.h>
711da177e4SLinus Torvalds #include <net/ndisc.h>
721da177e4SLinus Torvalds #include <net/ip6_route.h>
731da177e4SLinus Torvalds #include <net/addrconf.h>
741da177e4SLinus Torvalds #include <net/tcp.h>
751da177e4SLinus Torvalds #include <net/ip.h>
765d620266SThomas Graf #include <net/netlink.h>
77f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
781da177e4SLinus Torvalds #include <linux/if_tunnel.h>
791da177e4SLinus Torvalds #include <linux/rtnetlink.h>
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
821da177e4SLinus Torvalds #include <linux/random.h>
831da177e4SLinus Torvalds #endif
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds #include <asm/uaccess.h>
867f7d9a6bSHerbert Xu #include <asm/unaligned.h>
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds #include <linux/proc_fs.h>
891da177e4SLinus Torvalds #include <linux/seq_file.h>
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* Set to 3 to get tracing... */
921da177e4SLinus Torvalds #define ACONF_DEBUG 2
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
951da177e4SLinus Torvalds #define ADBG(x) printk x
961da177e4SLinus Torvalds #else
971da177e4SLinus Torvalds #define ADBG(x)
981da177e4SLinus Torvalds #endif
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
1011da177e4SLinus Torvalds #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
104f52295a9SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev);
105408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
106408c4768SPavel Emelyanov #else
107408c4768SPavel Emelyanov static inline void addrconf_sysctl_register(struct inet6_dev *idev)
108408c4768SPavel Emelyanov {
109408c4768SPavel Emelyanov }
110408c4768SPavel Emelyanov 
111408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
112408c4768SPavel Emelyanov {
113408c4768SPavel Emelyanov }
1141da177e4SLinus Torvalds #endif
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1171da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev);
1181da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1191da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data);
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds static int desync_factor = MAX_DESYNC_FACTOR * HZ;
1221da177e4SLinus Torvalds #endif
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /*
1271da177e4SLinus Torvalds  *	Configured unicast address hash table
1281da177e4SLinus Torvalds  */
1291da177e4SLinus Torvalds static struct inet6_ifaddr		*inet6_addr_lst[IN6_ADDR_HSIZE];
1301da177e4SLinus Torvalds static DEFINE_RWLOCK(addrconf_hash_lock);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds static void addrconf_verify(unsigned long);
1331da177e4SLinus Torvalds 
1348d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
1351da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1381da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1411da177e4SLinus Torvalds 
142e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
1431da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data);
1441da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
145c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1461da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1471da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1481da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1511da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1521da177e4SLinus Torvalds static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
1531da177e4SLinus Torvalds 
154e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
1551da177e4SLinus Torvalds 
156ab32ea5dSBrian Haley 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. */
1871da177e4SLinus Torvalds };
1881da177e4SLinus Torvalds 
189ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
1901da177e4SLinus Torvalds 	.forwarding		= 0,
1911da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1921da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1931da177e4SLinus Torvalds 	.accept_ra		= 1,
1941da177e4SLinus Torvalds 	.accept_redirects	= 1,
1951da177e4SLinus Torvalds 	.autoconf		= 1,
1961da177e4SLinus Torvalds 	.dad_transmits		= 1,
1971da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1981da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1991da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2001da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
2011da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2021da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2031da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2041da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2051da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2061da177e4SLinus Torvalds #endif
2071da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
20865f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
209c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
210930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
211930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
21252e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
21309c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
21409c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
21509c884d4SYOSHIFUJI Hideaki #endif
216930d6ff2SYOSHIFUJI Hideaki #endif
217fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2180bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
2191da177e4SLinus Torvalds };
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
2221da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
2231da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
2241da177e4SLinus Torvalds 
225f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
226f24e3d65SMitsuru Chinen static inline int addrconf_qdisc_ok(struct net_device *dev)
227f24e3d65SMitsuru Chinen {
228f24e3d65SMitsuru Chinen 	return (dev->qdisc != &noop_qdisc);
229f24e3d65SMitsuru Chinen }
230f24e3d65SMitsuru Chinen 
2311da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
2341da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds enum addrconf_timer_t
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	AC_NONE,
2401da177e4SLinus Torvalds 	AC_DAD,
2411da177e4SLinus Torvalds 	AC_RS,
2421da177e4SLinus Torvalds };
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
2451da177e4SLinus Torvalds 			       enum addrconf_timer_t what,
2461da177e4SLinus Torvalds 			       unsigned long when)
2471da177e4SLinus Torvalds {
2481da177e4SLinus Torvalds 	if (!del_timer(&ifp->timer))
2491da177e4SLinus Torvalds 		in6_ifa_hold(ifp);
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	switch (what) {
2521da177e4SLinus Torvalds 	case AC_DAD:
2531da177e4SLinus Torvalds 		ifp->timer.function = addrconf_dad_timer;
2541da177e4SLinus Torvalds 		break;
2551da177e4SLinus Torvalds 	case AC_RS:
2561da177e4SLinus Torvalds 		ifp->timer.function = addrconf_rs_timer;
2571da177e4SLinus Torvalds 		break;
2581da177e4SLinus Torvalds 	default:;
2591da177e4SLinus Torvalds 	}
2601da177e4SLinus Torvalds 	ifp->timer.expires = jiffies + when;
2611da177e4SLinus Torvalds 	add_timer(&ifp->timer);
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
2647f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
2657f7d9a6bSHerbert Xu {
2667f7d9a6bSHerbert Xu 	if (snmp_mib_init((void **)idev->stats.ipv6,
267c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct ipstats_mib)) < 0)
2687f7d9a6bSHerbert Xu 		goto err_ip;
2697f7d9a6bSHerbert Xu 	if (snmp_mib_init((void **)idev->stats.icmpv6,
270c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct icmpv6_mib)) < 0)
2717f7d9a6bSHerbert Xu 		goto err_icmp;
27214878f75SDavid L Stevens 	if (snmp_mib_init((void **)idev->stats.icmpv6msg,
273c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct icmpv6msg_mib)) < 0)
27414878f75SDavid L Stevens 		goto err_icmpmsg;
2757f7d9a6bSHerbert Xu 
2767f7d9a6bSHerbert Xu 	return 0;
2777f7d9a6bSHerbert Xu 
27814878f75SDavid L Stevens err_icmpmsg:
27914878f75SDavid L Stevens 	snmp_mib_free((void **)idev->stats.icmpv6);
2807f7d9a6bSHerbert Xu err_icmp:
2817f7d9a6bSHerbert Xu 	snmp_mib_free((void **)idev->stats.ipv6);
2827f7d9a6bSHerbert Xu err_ip:
283aaf70ec7SPavel Emelyanov 	return -ENOMEM;
2847f7d9a6bSHerbert Xu }
2857f7d9a6bSHerbert Xu 
28616910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev)
2877f7d9a6bSHerbert Xu {
28814878f75SDavid L Stevens 	snmp_mib_free((void **)idev->stats.icmpv6msg);
2897f7d9a6bSHerbert Xu 	snmp_mib_free((void **)idev->stats.icmpv6);
2907f7d9a6bSHerbert Xu 	snmp_mib_free((void **)idev->stats.ipv6);
2917f7d9a6bSHerbert Xu }
2927f7d9a6bSHerbert Xu 
2931da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */
2941da177e4SLinus Torvalds 
2958814c4b5SYOSHIFUJI Hideaki static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
2968814c4b5SYOSHIFUJI Hideaki {
2978814c4b5SYOSHIFUJI Hideaki 	struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
2988814c4b5SYOSHIFUJI Hideaki 	kfree(idev);
2998814c4b5SYOSHIFUJI Hideaki }
3008814c4b5SYOSHIFUJI Hideaki 
3011da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3041da177e4SLinus Torvalds 	BUG_TRAP(idev->addr_list==NULL);
3051da177e4SLinus Torvalds 	BUG_TRAP(idev->mc_list==NULL);
3061da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
3071da177e4SLinus Torvalds 	printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
3081da177e4SLinus Torvalds #endif
3091da177e4SLinus Torvalds 	dev_put(dev);
3101da177e4SLinus Torvalds 	if (!idev->dead) {
3111da177e4SLinus Torvalds 		printk("Freeing alive inet6 device %p\n", idev);
3121da177e4SLinus Torvalds 		return;
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 	snmp6_free_dev(idev);
3158814c4b5SYOSHIFUJI Hideaki 	call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3187159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy);
3197159039aSYOSHIFUJI Hideaki 
3201da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
3211da177e4SLinus Torvalds {
3221da177e4SLinus Torvalds 	struct inet6_dev *ndev;
323d88ae4ccSYOSHIFUJI Hideaki 	struct in6_addr maddr;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	ASSERT_RTNL();
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
3281da177e4SLinus Torvalds 		return NULL;
3291da177e4SLinus Torvalds 
330322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
3311da177e4SLinus Torvalds 
332322f74a4SIngo Oeser 	if (ndev == NULL)
333322f74a4SIngo Oeser 		return NULL;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3361da177e4SLinus Torvalds 	ndev->dev = dev;
337441fc2a2SPavel Emelyanov 	memcpy(&ndev->cnf, dev->nd_net->ipv6.devconf_dflt, sizeof(ndev->cnf));
3381da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3391da177e4SLinus Torvalds 	ndev->cnf.sysctl = NULL;
3401da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
3411da177e4SLinus Torvalds 	if (ndev->nd_parms == NULL) {
3421da177e4SLinus Torvalds 		kfree(ndev);
3431da177e4SLinus Torvalds 		return NULL;
3441da177e4SLinus Torvalds 	}
3451da177e4SLinus Torvalds 	/* We refer to the device */
3461da177e4SLinus Torvalds 	dev_hold(dev);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
3491da177e4SLinus Torvalds 		ADBG((KERN_WARNING
3501da177e4SLinus Torvalds 			"%s(): cannot allocate memory for statistics; dev=%s.\n",
3511da177e4SLinus Torvalds 			__FUNCTION__, dev->name));
3521da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3531da177e4SLinus Torvalds 		ndev->dead = 1;
3541da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3551da177e4SLinus Torvalds 		return NULL;
3561da177e4SLinus Torvalds 	}
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
3591da177e4SLinus Torvalds 		ADBG((KERN_WARNING
3601da177e4SLinus Torvalds 			"%s(): cannot create /proc/net/dev_snmp6/%s\n",
3611da177e4SLinus Torvalds 			__FUNCTION__, dev->name));
3621da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3631da177e4SLinus Torvalds 		ndev->dead = 1;
3641da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3651da177e4SLinus Torvalds 		return NULL;
3661da177e4SLinus Torvalds 	}
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3691da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3701da177e4SLinus Torvalds 	 */
3711da177e4SLinus Torvalds 	in6_dev_hold(ndev);
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
374b24b8a24SPavel Emelyanov 	setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
3751da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
3761da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
3770be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
3780be669bbSJoerg Roedel 	    dev->type == ARPHRD_SIT ||
3790be669bbSJoerg Roedel #endif
3800be669bbSJoerg Roedel 	    dev->type == ARPHRD_NONE) {
3811da177e4SLinus Torvalds 		printk(KERN_INFO
382a1493d9cSDavid S. Miller 		       "%s: Disabled Privacy Extensions\n",
383a1493d9cSDavid S. Miller 		       dev->name);
3841da177e4SLinus Torvalds 		ndev->cnf.use_tempaddr = -1;
385c7dc89c0SFred L. Templin 
386c7dc89c0SFred L. Templin 		if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
387c7dc89c0SFred L. Templin 			printk(KERN_INFO
388c7dc89c0SFred L. Templin 			       "%s: Disabled Multicast RS\n",
389c7dc89c0SFred L. Templin 			       dev->name);
390c7dc89c0SFred L. Templin 			ndev->cnf.rtr_solicits = 0;
391c7dc89c0SFred L. Templin 		}
3921da177e4SLinus Torvalds 	} else {
3931da177e4SLinus Torvalds 		in6_dev_hold(ndev);
3941da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds #endif
3971da177e4SLinus Torvalds 
398f24e3d65SMitsuru Chinen 	if (netif_running(dev) && addrconf_qdisc_ok(dev))
39953aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
40053aadcc9SHerbert Xu 
4011da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4021da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
403f52295a9SPavel Emelyanov 	addrconf_sysctl_register(ndev);
40430c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
40530c4cf57SDavid L Stevens 	rcu_assign_pointer(dev->ip6_ptr, ndev);
406d88ae4ccSYOSHIFUJI Hideaki 
407d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
408d88ae4ccSYOSHIFUJI Hideaki 	ipv6_addr_all_nodes(&maddr);
409d88ae4ccSYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &maddr);
410d88ae4ccSYOSHIFUJI Hideaki 
4111da177e4SLinus Torvalds 	return ndev;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	struct inet6_dev *idev;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	ASSERT_RTNL();
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL) {
4211da177e4SLinus Torvalds 		if ((idev = ipv6_add_dev(dev)) == NULL)
4221da177e4SLinus Torvalds 			return NULL;
4231da177e4SLinus Torvalds 	}
424c5e33bddSYOSHIFUJI Hideaki 
4251da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4261da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4271da177e4SLinus Torvalds 	return idev;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
4311da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	struct net_device *dev;
4341da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
4351da177e4SLinus Torvalds 	struct in6_addr addr;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	if (!idev)
4381da177e4SLinus Torvalds 		return;
4391da177e4SLinus Torvalds 	dev = idev->dev;
4401da177e4SLinus Torvalds 	if (dev && (dev->flags & IFF_MULTICAST)) {
4411da177e4SLinus Torvalds 		ipv6_addr_all_routers(&addr);
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
4441da177e4SLinus Torvalds 			ipv6_dev_mc_inc(dev, &addr);
4451da177e4SLinus Torvalds 		else
4461da177e4SLinus Torvalds 			ipv6_dev_mc_dec(dev, &addr);
4471da177e4SLinus Torvalds 	}
4481da177e4SLinus Torvalds 	for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
4492c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
4502c12a74cSMichal Wrobel 			continue;
4511da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
4521da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
4531da177e4SLinus Torvalds 		else
4541da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 
459e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	struct net_device *dev;
4621da177e4SLinus Torvalds 	struct inet6_dev *idev;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
465bff16c2fSPavel Emelyanov 	for_each_netdev(net, dev) {
4668814c4b5SYOSHIFUJI Hideaki 		rcu_read_lock();
4671da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
4681da177e4SLinus Torvalds 		if (idev) {
469e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
470e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
4711da177e4SLinus Torvalds 			if (changed)
4721da177e4SLinus Torvalds 				dev_forward_change(idev);
4731da177e4SLinus Torvalds 		}
4748814c4b5SYOSHIFUJI Hideaki 		rcu_read_unlock();
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
4771da177e4SLinus Torvalds }
478c8fecf22SPavel Emelyanov 
479c8fecf22SPavel Emelyanov static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
480c8fecf22SPavel Emelyanov {
481bff16c2fSPavel Emelyanov 	struct net *net;
482bff16c2fSPavel Emelyanov 
483bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
484441fc2a2SPavel Emelyanov 	if (p == &net->ipv6.devconf_dflt->forwarding)
485c8fecf22SPavel Emelyanov 		return;
486c8fecf22SPavel Emelyanov 
487e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
488e186932bSPavel Emelyanov 		__s32 newf = net->ipv6.devconf_all->forwarding;
489e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
490e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
491c8fecf22SPavel Emelyanov 	} else if ((!*p) ^ (!old))
492c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
493c8fecf22SPavel Emelyanov 
494c8fecf22SPavel Emelyanov 	if (*p)
495c8fecf22SPavel Emelyanov 		rt6_purge_dflt_routers();
496c8fecf22SPavel Emelyanov }
4971da177e4SLinus Torvalds #endif
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds /* Nobody refers to this ifaddr, destroy it */
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
5021da177e4SLinus Torvalds {
5031da177e4SLinus Torvalds 	BUG_TRAP(ifp->if_next==NULL);
5041da177e4SLinus Torvalds 	BUG_TRAP(ifp->lst_next==NULL);
5051da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
5061da177e4SLinus Torvalds 	printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
5071da177e4SLinus Torvalds #endif
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
5121da177e4SLinus Torvalds 		printk("Timer is still running, when freeing ifa=%p\n", ifp);
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	if (!ifp->dead) {
5151da177e4SLinus Torvalds 		printk("Freeing alive inet6 address %p\n", ifp);
5161da177e4SLinus Torvalds 		return;
5171da177e4SLinus Torvalds 	}
5181da177e4SLinus Torvalds 	dst_release(&ifp->rt->u.dst);
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	kfree(ifp);
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
523e55ffac6SBrian Haley static void
524e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
525e55ffac6SBrian Haley {
526e55ffac6SBrian Haley 	struct inet6_ifaddr *ifa, **ifap;
5278a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
528e55ffac6SBrian Haley 
529e55ffac6SBrian Haley 	/*
530e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
531e55ffac6SBrian Haley 	 * global before linklocal.
532e55ffac6SBrian Haley 	 */
533e55ffac6SBrian Haley 	for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
534e55ffac6SBrian Haley 	     ifap = &ifa->if_next) {
5358a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
536e55ffac6SBrian Haley 			break;
537e55ffac6SBrian Haley 	}
538e55ffac6SBrian Haley 
539e55ffac6SBrian Haley 	ifp->if_next = *ifap;
540e55ffac6SBrian Haley 	*ifap = ifp;
541e55ffac6SBrian Haley }
542e55ffac6SBrian Haley 
5431da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds static struct inet6_ifaddr *
5461da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
547e431b8c0SJamal Hadi Salim 	      int scope, u32 flags)
5481da177e4SLinus Torvalds {
5491da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
5501da177e4SLinus Torvalds 	struct rt6_info *rt;
5511da177e4SLinus Torvalds 	int hash;
5521da177e4SLinus Torvalds 	int err = 0;
5531da177e4SLinus Torvalds 
5548814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
5551da177e4SLinus Torvalds 	if (idev->dead) {
5561da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
5571da177e4SLinus Torvalds 		goto out2;
5581da177e4SLinus Torvalds 	}
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	write_lock(&addrconf_hash_lock);
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
5631da177e4SLinus Torvalds 	if (ipv6_chk_same_addr(addr, idev->dev)) {
5641da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: already assigned\n"));
5651da177e4SLinus Torvalds 		err = -EEXIST;
5661da177e4SLinus Torvalds 		goto out;
5671da177e4SLinus Torvalds 	}
5681da177e4SLinus Torvalds 
569322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	if (ifa == NULL) {
5721da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: malloc failed\n"));
5731da177e4SLinus Torvalds 		err = -ENOBUFS;
5741da177e4SLinus Torvalds 		goto out;
5751da177e4SLinus Torvalds 	}
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	rt = addrconf_dst_alloc(idev, addr, 0);
5781da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
5791da177e4SLinus Torvalds 		err = PTR_ERR(rt);
5801da177e4SLinus Torvalds 		goto out;
5811da177e4SLinus Torvalds 	}
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	ipv6_addr_copy(&ifa->addr, addr);
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
5861da177e4SLinus Torvalds 	init_timer(&ifa->timer);
5871da177e4SLinus Torvalds 	ifa->timer.data = (unsigned long) ifa;
5881da177e4SLinus Torvalds 	ifa->scope = scope;
5891da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
5901da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
5911da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
5921da177e4SLinus Torvalds 
59357f5f544SKeir Fraser 	ifa->rt = rt;
59457f5f544SKeir Fraser 
59595c385b4SNeil Horman 	/*
59695c385b4SNeil Horman 	 * part one of RFC 4429, section 3.3
59795c385b4SNeil Horman 	 * We should not configure an address as
59895c385b4SNeil Horman 	 * optimistic if we do not yet know the link
59995c385b4SNeil Horman 	 * layer address of our nexhop router
60095c385b4SNeil Horman 	 */
60195c385b4SNeil Horman 
60295c385b4SNeil Horman 	if (rt->rt6i_nexthop == NULL)
60395c385b4SNeil Horman 		ifa->flags &= ~IFA_F_OPTIMISTIC;
60495c385b4SNeil Horman 
6051da177e4SLinus Torvalds 	ifa->idev = idev;
6061da177e4SLinus Torvalds 	in6_dev_hold(idev);
6071da177e4SLinus Torvalds 	/* For caller */
6081da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	/* Add to big hash table */
6111da177e4SLinus Torvalds 	hash = ipv6_addr_hash(addr);
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	ifa->lst_next = inet6_addr_lst[hash];
6141da177e4SLinus Torvalds 	inet6_addr_lst[hash] = ifa;
6151da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6161da177e4SLinus Torvalds 	write_unlock(&addrconf_hash_lock);
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	write_lock(&idev->lock);
6191da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
620e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
6231da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
6241da177e4SLinus Torvalds 		ifa->tmp_next = idev->tempaddr_list;
6251da177e4SLinus Torvalds 		idev->tempaddr_list = ifa;
6261da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
6271da177e4SLinus Torvalds 	}
6281da177e4SLinus Torvalds #endif
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6311da177e4SLinus Torvalds 	write_unlock(&idev->lock);
6321da177e4SLinus Torvalds out2:
6338814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
6341da177e4SLinus Torvalds 
635fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
636e041c683SAlan Stern 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
6371da177e4SLinus Torvalds 	else {
6381da177e4SLinus Torvalds 		kfree(ifa);
6391da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
6401da177e4SLinus Torvalds 	}
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	return ifa;
6431da177e4SLinus Torvalds out:
6441da177e4SLinus Torvalds 	write_unlock(&addrconf_hash_lock);
6451da177e4SLinus Torvalds 	goto out2;
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
6511da177e4SLinus Torvalds {
6521da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **ifap;
6531da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
6541da177e4SLinus Torvalds 	int hash;
6551da177e4SLinus Torvalds 	int deleted = 0, onlink = 0;
6561da177e4SLinus Torvalds 	unsigned long expires = jiffies;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	hash = ipv6_addr_hash(&ifp->addr);
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	ifp->dead = 1;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
6631da177e4SLinus Torvalds 	for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
6641da177e4SLinus Torvalds 	     ifap = &ifa->lst_next) {
6651da177e4SLinus Torvalds 		if (ifa == ifp) {
6661da177e4SLinus Torvalds 			*ifap = ifa->lst_next;
6671da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
6681da177e4SLinus Torvalds 			ifa->lst_next = NULL;
6691da177e4SLinus Torvalds 			break;
6701da177e4SLinus Torvalds 		}
6711da177e4SLinus Torvalds 	}
6721da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
6751da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
6761da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
6771da177e4SLinus Torvalds 		for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
6781da177e4SLinus Torvalds 		     ifap = &ifa->tmp_next) {
6791da177e4SLinus Torvalds 			if (ifa == ifp) {
6801da177e4SLinus Torvalds 				*ifap = ifa->tmp_next;
6811da177e4SLinus Torvalds 				if (ifp->ifpub) {
6821da177e4SLinus Torvalds 					in6_ifa_put(ifp->ifpub);
6831da177e4SLinus Torvalds 					ifp->ifpub = NULL;
6841da177e4SLinus Torvalds 				}
6851da177e4SLinus Torvalds 				__in6_ifa_put(ifp);
6861da177e4SLinus Torvalds 				ifa->tmp_next = NULL;
6871da177e4SLinus Torvalds 				break;
6881da177e4SLinus Torvalds 			}
6891da177e4SLinus Torvalds 		}
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds #endif
6921da177e4SLinus Torvalds 
6931d142804SKristian Slavov 	for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
6941da177e4SLinus Torvalds 		if (ifa == ifp) {
6951da177e4SLinus Torvalds 			*ifap = ifa->if_next;
6961da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
6971da177e4SLinus Torvalds 			ifa->if_next = NULL;
6981da177e4SLinus Torvalds 			if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
6991da177e4SLinus Torvalds 				break;
7001da177e4SLinus Torvalds 			deleted = 1;
7011d142804SKristian Slavov 			continue;
7021da177e4SLinus Torvalds 		} else if (ifp->flags & IFA_F_PERMANENT) {
7031da177e4SLinus Torvalds 			if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
7041da177e4SLinus Torvalds 					      ifp->prefix_len)) {
7051da177e4SLinus Torvalds 				if (ifa->flags & IFA_F_PERMANENT) {
7061da177e4SLinus Torvalds 					onlink = 1;
7071da177e4SLinus Torvalds 					if (deleted)
7081da177e4SLinus Torvalds 						break;
7091da177e4SLinus Torvalds 				} else {
7101da177e4SLinus Torvalds 					unsigned long lifetime;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 					if (!onlink)
7131da177e4SLinus Torvalds 						onlink = -1;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 					spin_lock(&ifa->lock);
7161da177e4SLinus Torvalds 					lifetime = min_t(unsigned long,
7171da177e4SLinus Torvalds 							 ifa->valid_lft, 0x7fffffffUL/HZ);
7181da177e4SLinus Torvalds 					if (time_before(expires,
7191da177e4SLinus Torvalds 							ifa->tstamp + lifetime * HZ))
7201da177e4SLinus Torvalds 						expires = ifa->tstamp + lifetime * HZ;
7211da177e4SLinus Torvalds 					spin_unlock(&ifa->lock);
7221da177e4SLinus Torvalds 				}
7231da177e4SLinus Torvalds 			}
7241da177e4SLinus Torvalds 		}
7251d142804SKristian Slavov 		ifap = &ifa->if_next;
7261da177e4SLinus Torvalds 	}
7271da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
7301da177e4SLinus Torvalds 
731e041c683SAlan Stern 	atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	addrconf_del_timer(ifp);
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	/*
7361da177e4SLinus Torvalds 	 * Purge or update corresponding prefix
7371da177e4SLinus Torvalds 	 *
7381da177e4SLinus Torvalds 	 * 1) we don't purge prefix here if address was not permanent.
7391da177e4SLinus Torvalds 	 *    prefix is managed by its own lifetime.
7401da177e4SLinus Torvalds 	 * 2) if there're no addresses, delete prefix.
7411da177e4SLinus Torvalds 	 * 3) if there're still other permanent address(es),
7421da177e4SLinus Torvalds 	 *    corresponding prefix is still permanent.
7431da177e4SLinus Torvalds 	 * 4) otherwise, update prefix lifetime to the
7441da177e4SLinus Torvalds 	 *    longest valid lifetime among the corresponding
7451da177e4SLinus Torvalds 	 *    addresses on the device.
7461da177e4SLinus Torvalds 	 *    Note: subsequent RA will update lifetime.
7471da177e4SLinus Torvalds 	 *
7481da177e4SLinus Torvalds 	 * --yoshfuji
7491da177e4SLinus Torvalds 	 */
7501da177e4SLinus Torvalds 	if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
7511da177e4SLinus Torvalds 		struct in6_addr prefix;
7521da177e4SLinus Torvalds 		struct rt6_info *rt;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 		ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
7551da177e4SLinus Torvalds 		rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 		if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
7581da177e4SLinus Torvalds 			if (onlink == 0) {
759e0a1ad73SThomas Graf 				ip6_del_rt(rt);
7601da177e4SLinus Torvalds 				rt = NULL;
7611da177e4SLinus Torvalds 			} else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
7621da177e4SLinus Torvalds 				rt->rt6i_expires = expires;
7631da177e4SLinus Torvalds 				rt->rt6i_flags |= RTF_EXPIRES;
7641da177e4SLinus Torvalds 			}
7651da177e4SLinus Torvalds 		}
7661da177e4SLinus Torvalds 		dst_release(&rt->u.dst);
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	in6_ifa_put(ifp);
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
7731da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
7741da177e4SLinus Torvalds {
7751da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
7761da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
7771da177e4SLinus Torvalds 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
7781da177e4SLinus Torvalds 	int tmp_plen;
7791da177e4SLinus Torvalds 	int ret = 0;
7801da177e4SLinus Torvalds 	int max_addresses;
78195c385b4SNeil Horman 	u32 addr_flags;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	write_lock(&idev->lock);
7841da177e4SLinus Torvalds 	if (ift) {
7851da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
7861da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
7871da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
7881da177e4SLinus Torvalds 		tmpaddr = &addr;
7891da177e4SLinus Torvalds 	} else {
7901da177e4SLinus Torvalds 		tmpaddr = NULL;
7911da177e4SLinus Torvalds 	}
7921da177e4SLinus Torvalds retry:
7931da177e4SLinus Torvalds 	in6_dev_hold(idev);
7941da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
7951da177e4SLinus Torvalds 		write_unlock(&idev->lock);
7961da177e4SLinus Torvalds 		printk(KERN_INFO
7971da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
7981da177e4SLinus Torvalds 		in6_dev_put(idev);
7991da177e4SLinus Torvalds 		ret = -1;
8001da177e4SLinus Torvalds 		goto out;
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
8031da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
8041da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
8051da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
8061da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8071da177e4SLinus Torvalds 		printk(KERN_WARNING
8081da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
8091da177e4SLinus Torvalds 		in6_dev_put(idev);
8101da177e4SLinus Torvalds 		ret = -1;
8111da177e4SLinus Torvalds 		goto out;
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
8141da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
8151da177e4SLinus Torvalds 	if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
8161da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
8171da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8181da177e4SLinus Torvalds 		printk(KERN_WARNING
8191da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
8201da177e4SLinus Torvalds 		in6_ifa_put(ifp);
8211da177e4SLinus Torvalds 		in6_dev_put(idev);
8221da177e4SLinus Torvalds 		ret = -1;
8231da177e4SLinus Torvalds 		goto out;
8241da177e4SLinus Torvalds 	}
8251da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
8261da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
8271da177e4SLinus Torvalds 			      ifp->valid_lft,
8281da177e4SLinus Torvalds 			      idev->cnf.temp_valid_lft);
8291da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
8301da177e4SLinus Torvalds 				 ifp->prefered_lft,
8311da177e4SLinus Torvalds 				 idev->cnf.temp_prefered_lft - desync_factor / HZ);
8321da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
8331da177e4SLinus Torvalds 	max_addresses = idev->cnf.max_addresses;
8341da177e4SLinus Torvalds 	tmp_cstamp = ifp->cstamp;
8351da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
8361da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	write_unlock(&idev->lock);
83995c385b4SNeil Horman 
84095c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
84195c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
84295c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
84395c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
84495c385b4SNeil Horman 
8451da177e4SLinus Torvalds 	ift = !max_addresses ||
8461da177e4SLinus Torvalds 	      ipv6_count_addresses(idev) < max_addresses ?
8471da177e4SLinus Torvalds 		ipv6_add_addr(idev, &addr, tmp_plen,
84895c385b4SNeil Horman 			      ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
84995c385b4SNeil Horman 			      addr_flags) : NULL;
8501da177e4SLinus Torvalds 	if (!ift || IS_ERR(ift)) {
8511da177e4SLinus Torvalds 		in6_ifa_put(ifp);
8521da177e4SLinus Torvalds 		in6_dev_put(idev);
8531da177e4SLinus Torvalds 		printk(KERN_INFO
8541da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): retry temporary address regeneration.\n");
8551da177e4SLinus Torvalds 		tmpaddr = &addr;
8561da177e4SLinus Torvalds 		write_lock(&idev->lock);
8571da177e4SLinus Torvalds 		goto retry;
8581da177e4SLinus Torvalds 	}
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
8611da177e4SLinus Torvalds 	ift->ifpub = ifp;
8621da177e4SLinus Torvalds 	ift->valid_lft = tmp_valid_lft;
8631da177e4SLinus Torvalds 	ift->prefered_lft = tmp_prefered_lft;
8641da177e4SLinus Torvalds 	ift->cstamp = tmp_cstamp;
8651da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
8661da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	addrconf_dad_start(ift, 0);
8691da177e4SLinus Torvalds 	in6_ifa_put(ift);
8701da177e4SLinus Torvalds 	in6_dev_put(idev);
8711da177e4SLinus Torvalds out:
8721da177e4SLinus Torvalds 	return ret;
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds #endif
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds /*
877072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
8781da177e4SLinus Torvalds  */
879072047e4SYOSHIFUJI Hideaki struct ipv6_saddr_score {
880072047e4SYOSHIFUJI Hideaki 	int		addr_type;
881072047e4SYOSHIFUJI Hideaki 	unsigned int	attrs;
882072047e4SYOSHIFUJI Hideaki 	int		matchlen;
8830d27b427SBrian Haley 	int		scope;
884072047e4SYOSHIFUJI Hideaki 	unsigned int	rule;
885072047e4SYOSHIFUJI Hideaki };
886072047e4SYOSHIFUJI Hideaki 
887072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_LOCAL		0x0001
888072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_PREFERRED	0x0004
889072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_HOA		0x0008
890072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_OIF		0x0010
891072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_LABEL		0x0020
892072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_PRIVACY	0x0040
893072047e4SYOSHIFUJI Hideaki 
894b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
8951da177e4SLinus Torvalds {
896072047e4SYOSHIFUJI Hideaki 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
897072047e4SYOSHIFUJI Hideaki 		    IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
898072047e4SYOSHIFUJI Hideaki 		return 1;
899072047e4SYOSHIFUJI Hideaki 	return 0;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
902072047e4SYOSHIFUJI Hideaki int ipv6_dev_get_saddr(struct net_device *daddr_dev,
9031da177e4SLinus Torvalds 		       struct in6_addr *daddr, struct in6_addr *saddr)
9041da177e4SLinus Torvalds {
905072047e4SYOSHIFUJI Hideaki 	struct ipv6_saddr_score hiscore;
906072047e4SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa_result = NULL;
907072047e4SYOSHIFUJI Hideaki 	int daddr_type = __ipv6_addr_type(daddr);
908072047e4SYOSHIFUJI Hideaki 	int daddr_scope = __ipv6_addr_src_scope(daddr_type);
909303065a8SYOSHIFUJI Hideaki 	int daddr_ifindex = daddr_dev ? daddr_dev->ifindex : 0;
910303065a8SYOSHIFUJI Hideaki 	u32 daddr_label = ipv6_addr_label(daddr, daddr_type, daddr_ifindex);
911072047e4SYOSHIFUJI Hideaki 	struct net_device *dev;
9121da177e4SLinus Torvalds 
913072047e4SYOSHIFUJI Hideaki 	memset(&hiscore, 0, sizeof(hiscore));
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
9168814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
917072047e4SYOSHIFUJI Hideaki 
918881d966bSEric W. Biederman 	for_each_netdev(&init_net, dev) {
919072047e4SYOSHIFUJI Hideaki 		struct inet6_dev *idev;
920072047e4SYOSHIFUJI Hideaki 		struct inet6_ifaddr *ifa;
921072047e4SYOSHIFUJI Hideaki 
922072047e4SYOSHIFUJI Hideaki 		/* Rule 0: Candidate Source Address (section 4)
923072047e4SYOSHIFUJI Hideaki 		 *  - multicast and link-local destination address,
924072047e4SYOSHIFUJI Hideaki 		 *    the set of candidate source address MUST only
925072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
926072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same link as the outgoing
927072047e4SYOSHIFUJI Hideaki 		 *    interface.
928072047e4SYOSHIFUJI Hideaki 		 * (- For site-local destination addresses, the
929072047e4SYOSHIFUJI Hideaki 		 *    set of candidate source addresses MUST only
930072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
931072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same site as the outgoing
932072047e4SYOSHIFUJI Hideaki 		 *    interface.)
933072047e4SYOSHIFUJI Hideaki 		 */
934072047e4SYOSHIFUJI Hideaki 		if ((daddr_type & IPV6_ADDR_MULTICAST ||
935072047e4SYOSHIFUJI Hideaki 		     daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
936072047e4SYOSHIFUJI Hideaki 		    daddr_dev && dev != daddr_dev)
937072047e4SYOSHIFUJI Hideaki 			continue;
938072047e4SYOSHIFUJI Hideaki 
9391da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
940072047e4SYOSHIFUJI Hideaki 		if (!idev)
941072047e4SYOSHIFUJI Hideaki 			continue;
942072047e4SYOSHIFUJI Hideaki 
9431da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
944072047e4SYOSHIFUJI Hideaki 		for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
945072047e4SYOSHIFUJI Hideaki 			struct ipv6_saddr_score score;
946072047e4SYOSHIFUJI Hideaki 
947072047e4SYOSHIFUJI Hideaki 			score.addr_type = __ipv6_addr_type(&ifa->addr);
948072047e4SYOSHIFUJI Hideaki 
9496b3ae80aSYOSHIFUJI Hideaki 			/* Rule 0:
9506b3ae80aSYOSHIFUJI Hideaki 			 * - Tentative Address (RFC2462 section 5.4)
9516b3ae80aSYOSHIFUJI Hideaki 			 *  - A tentative address is not considered
9526b3ae80aSYOSHIFUJI Hideaki 			 *    "assigned to an interface" in the traditional
95395c385b4SNeil Horman 			 *    sense, unless it is also flagged as optimistic.
9546b3ae80aSYOSHIFUJI Hideaki 			 * - Candidate Source Address (section 4)
955072047e4SYOSHIFUJI Hideaki 			 *  - In any case, anycast addresses, multicast
956072047e4SYOSHIFUJI Hideaki 			 *    addresses, and the unspecified address MUST
957072047e4SYOSHIFUJI Hideaki 			 *    NOT be included in a candidate set.
958072047e4SYOSHIFUJI Hideaki 			 */
95995c385b4SNeil Horman 			if ((ifa->flags & IFA_F_TENTATIVE) &&
96095c385b4SNeil Horman 			    (!(ifa->flags & IFA_F_OPTIMISTIC)))
9616b3ae80aSYOSHIFUJI Hideaki 				continue;
962072047e4SYOSHIFUJI Hideaki 			if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
963072047e4SYOSHIFUJI Hideaki 				     score.addr_type & IPV6_ADDR_MULTICAST)) {
964072047e4SYOSHIFUJI Hideaki 				LIMIT_NETDEBUG(KERN_DEBUG
965072047e4SYOSHIFUJI Hideaki 					       "ADDRCONF: unspecified / multicast address "
966072047e4SYOSHIFUJI Hideaki 					       "assigned as unicast address on %s",
967072047e4SYOSHIFUJI Hideaki 					       dev->name);
9681da177e4SLinus Torvalds 				continue;
969072047e4SYOSHIFUJI Hideaki 			}
970072047e4SYOSHIFUJI Hideaki 
971072047e4SYOSHIFUJI Hideaki 			score.attrs = 0;
972072047e4SYOSHIFUJI Hideaki 			score.matchlen = 0;
973072047e4SYOSHIFUJI Hideaki 			score.scope = 0;
974072047e4SYOSHIFUJI Hideaki 			score.rule = 0;
975072047e4SYOSHIFUJI Hideaki 
976072047e4SYOSHIFUJI Hideaki 			if (ifa_result == NULL) {
977072047e4SYOSHIFUJI Hideaki 				/* record it if the first available entry */
978072047e4SYOSHIFUJI Hideaki 				goto record_it;
979072047e4SYOSHIFUJI Hideaki 			}
980072047e4SYOSHIFUJI Hideaki 
981072047e4SYOSHIFUJI Hideaki 			/* Rule 1: Prefer same address */
982072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 1) {
983072047e4SYOSHIFUJI Hideaki 				if (ipv6_addr_equal(&ifa_result->addr, daddr))
984072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
985072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
986072047e4SYOSHIFUJI Hideaki 			}
987072047e4SYOSHIFUJI Hideaki 			if (ipv6_addr_equal(&ifa->addr, daddr)) {
988072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_LOCAL;
989072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
990072047e4SYOSHIFUJI Hideaki 					score.rule = 1;
991072047e4SYOSHIFUJI Hideaki 					goto record_it;
992072047e4SYOSHIFUJI Hideaki 				}
993072047e4SYOSHIFUJI Hideaki 			} else {
994072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
995072047e4SYOSHIFUJI Hideaki 					continue;
996072047e4SYOSHIFUJI Hideaki 			}
997072047e4SYOSHIFUJI Hideaki 
998072047e4SYOSHIFUJI Hideaki 			/* Rule 2: Prefer appropriate scope */
999072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 2) {
1000072047e4SYOSHIFUJI Hideaki 				hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
1001072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1002072047e4SYOSHIFUJI Hideaki 			}
1003072047e4SYOSHIFUJI Hideaki 			score.scope = __ipv6_addr_src_scope(score.addr_type);
1004072047e4SYOSHIFUJI Hideaki 			if (hiscore.scope < score.scope) {
1005072047e4SYOSHIFUJI Hideaki 				if (hiscore.scope < daddr_scope) {
1006072047e4SYOSHIFUJI Hideaki 					score.rule = 2;
1007072047e4SYOSHIFUJI Hideaki 					goto record_it;
1008072047e4SYOSHIFUJI Hideaki 				} else
1009072047e4SYOSHIFUJI Hideaki 					continue;
1010072047e4SYOSHIFUJI Hideaki 			} else if (score.scope < hiscore.scope) {
1011072047e4SYOSHIFUJI Hideaki 				if (score.scope < daddr_scope)
1012e55ffac6SBrian Haley 					break; /* addresses sorted by scope */
1013072047e4SYOSHIFUJI Hideaki 				else {
1014072047e4SYOSHIFUJI Hideaki 					score.rule = 2;
1015072047e4SYOSHIFUJI Hideaki 					goto record_it;
1016072047e4SYOSHIFUJI Hideaki 				}
1017072047e4SYOSHIFUJI Hideaki 			}
1018072047e4SYOSHIFUJI Hideaki 
101995c385b4SNeil Horman 			/* Rule 3: Avoid deprecated and optimistic addresses */
1020072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 3) {
1021072047e4SYOSHIFUJI Hideaki 				if (ipv6_saddr_preferred(hiscore.addr_type) ||
102295c385b4SNeil Horman 				   (((ifa_result->flags &
102395c385b4SNeil Horman 				    (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0)))
1024072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1025072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1026072047e4SYOSHIFUJI Hideaki 			}
1027072047e4SYOSHIFUJI Hideaki 			if (ipv6_saddr_preferred(score.addr_type) ||
10286ae5f983SJiri Kosina 			   (((ifa->flags &
102995c385b4SNeil Horman 			    (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0))) {
1030072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1031072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1032072047e4SYOSHIFUJI Hideaki 					score.rule = 3;
1033072047e4SYOSHIFUJI Hideaki 					goto record_it;
1034072047e4SYOSHIFUJI Hideaki 				}
1035072047e4SYOSHIFUJI Hideaki 			} else {
1036072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1037072047e4SYOSHIFUJI Hideaki 					continue;
1038072047e4SYOSHIFUJI Hideaki 			}
1039072047e4SYOSHIFUJI Hideaki 
10403b9f9a1cSNoriaki TAKAMIYA 			/* Rule 4: Prefer home address */
104159fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
10423b9f9a1cSNoriaki TAKAMIYA 			if (hiscore.rule < 4) {
10433b9f9a1cSNoriaki TAKAMIYA 				if (ifa_result->flags & IFA_F_HOMEADDRESS)
10443b9f9a1cSNoriaki TAKAMIYA 					hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
10453b9f9a1cSNoriaki TAKAMIYA 				hiscore.rule++;
10463b9f9a1cSNoriaki TAKAMIYA 			}
10473b9f9a1cSNoriaki TAKAMIYA 			if (ifa->flags & IFA_F_HOMEADDRESS) {
10483b9f9a1cSNoriaki TAKAMIYA 				score.attrs |= IPV6_SADDR_SCORE_HOA;
10493b9f9a1cSNoriaki TAKAMIYA 				if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
10503b9f9a1cSNoriaki TAKAMIYA 					score.rule = 4;
10513b9f9a1cSNoriaki TAKAMIYA 					goto record_it;
10523b9f9a1cSNoriaki TAKAMIYA 				}
10533b9f9a1cSNoriaki TAKAMIYA 			} else {
10543b9f9a1cSNoriaki TAKAMIYA 				if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
10553b9f9a1cSNoriaki TAKAMIYA 					continue;
10563b9f9a1cSNoriaki TAKAMIYA 			}
10573b9f9a1cSNoriaki TAKAMIYA #else
1058220bbd74SYOSHIFUJI Hideaki 			if (hiscore.rule < 4)
1059220bbd74SYOSHIFUJI Hideaki 				hiscore.rule++;
10603b9f9a1cSNoriaki TAKAMIYA #endif
1061072047e4SYOSHIFUJI Hideaki 
1062072047e4SYOSHIFUJI Hideaki 			/* Rule 5: Prefer outgoing interface */
1063072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 5) {
1064072047e4SYOSHIFUJI Hideaki 				if (daddr_dev == NULL ||
1065072047e4SYOSHIFUJI Hideaki 				    daddr_dev == ifa_result->idev->dev)
1066072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1067072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1068072047e4SYOSHIFUJI Hideaki 			}
1069072047e4SYOSHIFUJI Hideaki 			if (daddr_dev == NULL ||
1070072047e4SYOSHIFUJI Hideaki 			    daddr_dev == ifa->idev->dev) {
1071072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_OIF;
1072072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1073072047e4SYOSHIFUJI Hideaki 					score.rule = 5;
1074072047e4SYOSHIFUJI Hideaki 					goto record_it;
1075072047e4SYOSHIFUJI Hideaki 				}
1076072047e4SYOSHIFUJI Hideaki 			} else {
1077072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1078072047e4SYOSHIFUJI Hideaki 					continue;
1079072047e4SYOSHIFUJI Hideaki 			}
1080072047e4SYOSHIFUJI Hideaki 
1081072047e4SYOSHIFUJI Hideaki 			/* Rule 6: Prefer matching label */
1082072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 6) {
1083c1ee656cSYOSHIFUJI Hideaki 				if (ipv6_addr_label(&ifa_result->addr,
1084303065a8SYOSHIFUJI Hideaki 						    hiscore.addr_type,
1085303065a8SYOSHIFUJI Hideaki 						    ifa_result->idev->dev->ifindex) == daddr_label)
1086072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1087072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1088072047e4SYOSHIFUJI Hideaki 			}
1089c1ee656cSYOSHIFUJI Hideaki 			if (ipv6_addr_label(&ifa->addr,
1090303065a8SYOSHIFUJI Hideaki 					    score.addr_type,
1091303065a8SYOSHIFUJI Hideaki 					    ifa->idev->dev->ifindex) == daddr_label) {
1092072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_LABEL;
1093072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1094072047e4SYOSHIFUJI Hideaki 					score.rule = 6;
1095072047e4SYOSHIFUJI Hideaki 					goto record_it;
1096072047e4SYOSHIFUJI Hideaki 				}
1097072047e4SYOSHIFUJI Hideaki 			} else {
1098072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1099072047e4SYOSHIFUJI Hideaki 					continue;
1100072047e4SYOSHIFUJI Hideaki 			}
1101072047e4SYOSHIFUJI Hideaki 
110244fd0261SPeter Chubb #ifdef CONFIG_IPV6_PRIVACY
1103072047e4SYOSHIFUJI Hideaki 			/* Rule 7: Prefer public address
1104072047e4SYOSHIFUJI Hideaki 			 * Note: prefer temprary address if use_tempaddr >= 2
1105072047e4SYOSHIFUJI Hideaki 			 */
1106072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 7) {
1107072047e4SYOSHIFUJI Hideaki 				if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1108072047e4SYOSHIFUJI Hideaki 				    (ifa_result->idev->cnf.use_tempaddr >= 2))
1109072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1110072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1111072047e4SYOSHIFUJI Hideaki 			}
1112072047e4SYOSHIFUJI Hideaki 			if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1113072047e4SYOSHIFUJI Hideaki 			    (ifa->idev->cnf.use_tempaddr >= 2)) {
1114072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1115072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1116072047e4SYOSHIFUJI Hideaki 					score.rule = 7;
1117072047e4SYOSHIFUJI Hideaki 					goto record_it;
1118072047e4SYOSHIFUJI Hideaki 				}
1119072047e4SYOSHIFUJI Hideaki 			} else {
1120072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1121072047e4SYOSHIFUJI Hideaki 					continue;
1122072047e4SYOSHIFUJI Hideaki 			}
11235e2707faSYOSHIFUJI Hideaki #else
11245e2707faSYOSHIFUJI Hideaki 			if (hiscore.rule < 7)
11255e2707faSYOSHIFUJI Hideaki 				hiscore.rule++;
112644fd0261SPeter Chubb #endif
1127072047e4SYOSHIFUJI Hideaki 			/* Rule 8: Use longest matching prefix */
112812da2a43SYan Zheng 			if (hiscore.rule < 8) {
1129072047e4SYOSHIFUJI Hideaki 				hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
113012da2a43SYan Zheng 				hiscore.rule++;
113112da2a43SYan Zheng 			}
1132072047e4SYOSHIFUJI Hideaki 			score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1133072047e4SYOSHIFUJI Hideaki 			if (score.matchlen > hiscore.matchlen) {
1134072047e4SYOSHIFUJI Hideaki 				score.rule = 8;
1135072047e4SYOSHIFUJI Hideaki 				goto record_it;
1136072047e4SYOSHIFUJI Hideaki 			}
1137072047e4SYOSHIFUJI Hideaki #if 0
1138072047e4SYOSHIFUJI Hideaki 			else if (score.matchlen < hiscore.matchlen)
1139072047e4SYOSHIFUJI Hideaki 				continue;
11401da177e4SLinus Torvalds #endif
1141072047e4SYOSHIFUJI Hideaki 
1142072047e4SYOSHIFUJI Hideaki 			/* Final Rule: choose first available one */
11431da177e4SLinus Torvalds 			continue;
1144072047e4SYOSHIFUJI Hideaki record_it:
1145072047e4SYOSHIFUJI Hideaki 			if (ifa_result)
1146072047e4SYOSHIFUJI Hideaki 				in6_ifa_put(ifa_result);
1147072047e4SYOSHIFUJI Hideaki 			in6_ifa_hold(ifa);
1148072047e4SYOSHIFUJI Hideaki 			ifa_result = ifa;
11491da177e4SLinus Torvalds 			hiscore = score;
11501da177e4SLinus Torvalds 		}
11511da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
11521da177e4SLinus Torvalds 	}
11538814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
11541da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
11551da177e4SLinus Torvalds 
1156072047e4SYOSHIFUJI Hideaki 	if (!ifa_result)
1157072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
11581da177e4SLinus Torvalds 
1159072047e4SYOSHIFUJI Hideaki 	ipv6_addr_copy(saddr, &ifa_result->addr);
1160072047e4SYOSHIFUJI Hideaki 	in6_ifa_put(ifa_result);
1161072047e4SYOSHIFUJI Hideaki 	return 0;
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds int ipv6_get_saddr(struct dst_entry *dst,
11661da177e4SLinus Torvalds 		   struct in6_addr *daddr, struct in6_addr *saddr)
11671da177e4SLinus Torvalds {
11687a3025b1SYOSHIFUJI Hideaki 	return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
11691da177e4SLinus Torvalds }
11701da177e4SLinus Torvalds 
11717159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_get_saddr);
11721da177e4SLinus Torvalds 
117395c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
117495c385b4SNeil Horman 		    unsigned char banned_flags)
11751da177e4SLinus Torvalds {
11761da177e4SLinus Torvalds 	struct inet6_dev *idev;
11771da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
11781da177e4SLinus Torvalds 
11798814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
11801da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) != NULL) {
11811da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
11841da177e4SLinus Torvalds 		for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
118595c385b4SNeil Horman 			if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
11861da177e4SLinus Torvalds 				ipv6_addr_copy(addr, &ifp->addr);
11871da177e4SLinus Torvalds 				err = 0;
11881da177e4SLinus Torvalds 				break;
11891da177e4SLinus Torvalds 			}
11901da177e4SLinus Torvalds 		}
11911da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
11921da177e4SLinus Torvalds 	}
11938814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
11941da177e4SLinus Torvalds 	return err;
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
11981da177e4SLinus Torvalds {
11991da177e4SLinus Torvalds 	int cnt = 0;
12001da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
12031da177e4SLinus Torvalds 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
12041da177e4SLinus Torvalds 		cnt++;
12051da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
12061da177e4SLinus Torvalds 	return cnt;
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
12101da177e4SLinus Torvalds {
12111da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
12121da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
12151da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
12161da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
12171da177e4SLinus Torvalds 		    !(ifp->flags&IFA_F_TENTATIVE)) {
12181da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
12191da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
12201da177e4SLinus Torvalds 				break;
12211da177e4SLinus Torvalds 		}
12221da177e4SLinus Torvalds 	}
12231da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
12241da177e4SLinus Torvalds 	return ifp != NULL;
12251da177e4SLinus Torvalds }
12261da177e4SLinus Torvalds 
12277159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr);
12287159039aSYOSHIFUJI Hideaki 
12291da177e4SLinus Torvalds static
12301da177e4SLinus Torvalds int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
12311da177e4SLinus Torvalds {
12321da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
12331da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
12361da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
12371da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev)
12381da177e4SLinus Torvalds 				break;
12391da177e4SLinus Torvalds 		}
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 	return ifp != NULL;
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
12451da177e4SLinus Torvalds {
12461da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
12471da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
12501da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
12511da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
12521da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
12531da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
12541da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
12551da177e4SLinus Torvalds 				break;
12561da177e4SLinus Torvalds 			}
12571da177e4SLinus Torvalds 		}
12581da177e4SLinus Torvalds 	}
12591da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	return ifp;
12621da177e4SLinus Torvalds }
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
12651da177e4SLinus Torvalds {
12661da177e4SLinus Torvalds 	const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
12670fa1a53eSArnaldo Carvalho de Melo 	const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
126882103232SAl Viro 	__be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
126982103232SAl Viro 	__be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
12701da177e4SLinus Torvalds 	int sk_ipv6only = ipv6_only_sock(sk);
1271463c84b9SArnaldo Carvalho de Melo 	int sk2_ipv6only = inet_v6_ipv6only(sk2);
12721da177e4SLinus Torvalds 	int addr_type = ipv6_addr_type(sk_rcv_saddr6);
12731da177e4SLinus Torvalds 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	if (!sk2_rcv_saddr && !sk_ipv6only)
12761da177e4SLinus Torvalds 		return 1;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	if (addr_type2 == IPV6_ADDR_ANY &&
12791da177e4SLinus Torvalds 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
12801da177e4SLinus Torvalds 		return 1;
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_ANY &&
12831da177e4SLinus Torvalds 	    !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
12841da177e4SLinus Torvalds 		return 1;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	if (sk2_rcv_saddr6 &&
12871da177e4SLinus Torvalds 	    ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
12881da177e4SLinus Torvalds 		return 1;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED &&
12911da177e4SLinus Torvalds 	    !sk2_ipv6only &&
12921da177e4SLinus Torvalds 	    (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
12931da177e4SLinus Torvalds 		return 1;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	return 0;
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
12991da177e4SLinus Torvalds 
13009f5336e2SAdrian Bunk static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
13011da177e4SLinus Torvalds {
13021da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
13031da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
13041da177e4SLinus Torvalds 		addrconf_del_timer(ifp);
13051da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
13061da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
13071da177e4SLinus Torvalds 		in6_ifa_put(ifp);
13081da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
13091da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
13101da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
13111da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
13121da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
13131da177e4SLinus Torvalds 		if (ifpub) {
13141da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
13151da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
13161da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
13171da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
13181da177e4SLinus Torvalds 		} else {
13191da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
13201da177e4SLinus Torvalds 		}
13211da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
13221da177e4SLinus Torvalds #endif
13231da177e4SLinus Torvalds 	} else
13241da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
13251da177e4SLinus Torvalds }
13261da177e4SLinus Torvalds 
13273c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
13283c21edbdSYOSHIFUJI Hideaki {
13293c21edbdSYOSHIFUJI Hideaki 	if (net_ratelimit())
13303c21edbdSYOSHIFUJI Hideaki 		printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
13313c21edbdSYOSHIFUJI Hideaki 	addrconf_dad_stop(ifp);
13323c21edbdSYOSHIFUJI Hideaki }
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds /* Join to solicited addr multicast group. */
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
13371da177e4SLinus Torvalds {
13381da177e4SLinus Torvalds 	struct in6_addr maddr;
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
13411da177e4SLinus Torvalds 		return;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
13441da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
13481da177e4SLinus Torvalds {
13491da177e4SLinus Torvalds 	struct in6_addr maddr;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
13521da177e4SLinus Torvalds 		return;
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
13551da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
13561da177e4SLinus Torvalds }
13571da177e4SLinus Torvalds 
135820380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
13591da177e4SLinus Torvalds {
13601da177e4SLinus Torvalds 	struct in6_addr addr;
13611da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
13621da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
13631da177e4SLinus Torvalds 		return;
13641da177e4SLinus Torvalds 	ipv6_dev_ac_inc(ifp->idev->dev, &addr);
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
136720380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
13681da177e4SLinus Torvalds {
13691da177e4SLinus Torvalds 	struct in6_addr addr;
13701da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
13711da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
13721da177e4SLinus Torvalds 		return;
13731da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
13741da177e4SLinus Torvalds }
13751da177e4SLinus Torvalds 
1376073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
13771da177e4SLinus Torvalds {
13781da177e4SLinus Torvalds 	if (dev->addr_len != ETH_ALEN)
13791da177e4SLinus Torvalds 		return -1;
13801da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr, 3);
13811da177e4SLinus Torvalds 	memcpy(eui + 5, dev->dev_addr + 3, 3);
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	/*
13841da177e4SLinus Torvalds 	 * The zSeries OSA network cards can be shared among various
13851da177e4SLinus Torvalds 	 * OS instances, but the OSA cards have only one MAC address.
13861da177e4SLinus Torvalds 	 * This leads to duplicate address conflicts in conjunction
13871da177e4SLinus Torvalds 	 * with IPv6 if more than one instance uses the same card.
13881da177e4SLinus Torvalds 	 *
13891da177e4SLinus Torvalds 	 * The driver for these cards can deliver a unique 16-bit
13901da177e4SLinus Torvalds 	 * identifier for each instance sharing the same card.  It is
13911da177e4SLinus Torvalds 	 * placed instead of 0xFFFE in the interface identifier.  The
13921da177e4SLinus Torvalds 	 * "u" bit of the interface identifier is not inverted in this
13931da177e4SLinus Torvalds 	 * case.  Hence the resulting interface identifier has local
13941da177e4SLinus Torvalds 	 * scope according to RFC2373.
13951da177e4SLinus Torvalds 	 */
13961da177e4SLinus Torvalds 	if (dev->dev_id) {
13971da177e4SLinus Torvalds 		eui[3] = (dev->dev_id >> 8) & 0xFF;
13981da177e4SLinus Torvalds 		eui[4] = dev->dev_id & 0xFF;
13991da177e4SLinus Torvalds 	} else {
14001da177e4SLinus Torvalds 		eui[3] = 0xFF;
14011da177e4SLinus Torvalds 		eui[4] = 0xFE;
14021da177e4SLinus Torvalds 		eui[0] ^= 2;
14031da177e4SLinus Torvalds 	}
14041da177e4SLinus Torvalds 	return 0;
1405073a8e0eSYOSHIFUJI Hideaki }
1406073a8e0eSYOSHIFUJI Hideaki 
1407073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1408073a8e0eSYOSHIFUJI Hideaki {
14091da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
14101da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
14111da177e4SLinus Torvalds 		return -1;
14121da177e4SLinus Torvalds 	memset(eui, 0, 7);
14131da177e4SLinus Torvalds 	eui[7] = *(u8*)dev->dev_addr;
14141da177e4SLinus Torvalds 	return 0;
1415073a8e0eSYOSHIFUJI Hideaki }
1416073a8e0eSYOSHIFUJI Hideaki 
1417073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1418073a8e0eSYOSHIFUJI Hideaki {
14191da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
14201da177e4SLinus Torvalds 		return -1;
14211da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
14221da177e4SLinus Torvalds 	eui[0] |= 2;
14231da177e4SLinus Torvalds 	return 0;
14241da177e4SLinus Torvalds }
1425073a8e0eSYOSHIFUJI Hideaki 
1426073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1427073a8e0eSYOSHIFUJI Hideaki {
1428073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
1429073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
1430073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
1431073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_IEEE802_TR:
1432073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
1433073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
1434073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
1435073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
1436073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
1437c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
1438c7dc89c0SFred L. Templin 		if (dev->priv_flags & IFF_ISATAP)
1439c7dc89c0SFred L. Templin 			return ipv6_isatap_eui64(eui, *(__be32 *)dev->dev_addr);
1440073a8e0eSYOSHIFUJI Hideaki 	}
14411da177e4SLinus Torvalds 	return -1;
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
14451da177e4SLinus Torvalds {
14461da177e4SLinus Torvalds 	int err = -1;
14471da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
14501da177e4SLinus Torvalds 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
14511da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
14521da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
14531da177e4SLinus Torvalds 			err = 0;
14541da177e4SLinus Torvalds 			break;
14551da177e4SLinus Torvalds 		}
14561da177e4SLinus Torvalds 	}
14571da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
14581da177e4SLinus Torvalds 	return err;
14591da177e4SLinus Torvalds }
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
14621da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
14631da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev)
14641da177e4SLinus Torvalds {
14651da177e4SLinus Torvalds regen:
1466955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
14671da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	/*
14701da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
14711da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
14721da177e4SLinus Torvalds 	 *
14731da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
14741da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
1475c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
14761da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
14771da177e4SLinus Torvalds 	 *  - value 0
14781da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
14791da177e4SLinus Torvalds 	 */
14801da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
14811da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
14821da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
14831da177e4SLinus Torvalds 		goto regen;
14841da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
14851da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
14861da177e4SLinus Torvalds 			goto regen;
14871da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
14881da177e4SLinus Torvalds 			goto regen;
14891da177e4SLinus Torvalds 	}
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	return 0;
14921da177e4SLinus Torvalds }
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
14951da177e4SLinus Torvalds {
14961da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
14971da177e4SLinus Torvalds 	unsigned long expires;
14981da177e4SLinus Torvalds 
14998814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
15001da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 	if (idev->dead)
15031da177e4SLinus Torvalds 		goto out;
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	if (__ipv6_regen_rndid(idev) < 0)
15061da177e4SLinus Torvalds 		goto out;
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 	expires = jiffies +
15091da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
15101da177e4SLinus Torvalds 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
15111da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
15121da177e4SLinus Torvalds 		printk(KERN_WARNING
15131da177e4SLinus Torvalds 			"ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
15141da177e4SLinus Torvalds 			idev->dev->name);
15151da177e4SLinus Torvalds 		goto out;
15161da177e4SLinus Torvalds 	}
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
15191da177e4SLinus Torvalds 		in6_dev_hold(idev);
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds out:
15221da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
15238814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
15241da177e4SLinus Torvalds 	in6_dev_put(idev);
15251da177e4SLinus Torvalds }
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
15281da177e4SLinus Torvalds 	int ret = 0;
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
15311da177e4SLinus Torvalds 		ret = __ipv6_regen_rndid(idev);
15321da177e4SLinus Torvalds 	return ret;
15331da177e4SLinus Torvalds }
15341da177e4SLinus Torvalds #endif
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds /*
15371da177e4SLinus Torvalds  *	Add prefix route.
15381da177e4SLinus Torvalds  */
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds static void
15411da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1542e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
15431da177e4SLinus Torvalds {
154486872cb5SThomas Graf 	struct fib6_config cfg = {
154586872cb5SThomas Graf 		.fc_table = RT6_TABLE_PREFIX,
154686872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
154786872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
154886872cb5SThomas Graf 		.fc_expires = expires,
154986872cb5SThomas Graf 		.fc_dst_len = plen,
155086872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
155186872cb5SThomas Graf 	};
15521da177e4SLinus Torvalds 
155386872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, pfx);
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
15561da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
15571da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
15581da177e4SLinus Torvalds 	 */
15590be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
15601da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
156186872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
15620be669bbSJoerg Roedel #endif
15631da177e4SLinus Torvalds 
156486872cb5SThomas Graf 	ip6_route_add(&cfg);
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
15701da177e4SLinus Torvalds {
157186872cb5SThomas Graf 	struct fib6_config cfg = {
157286872cb5SThomas Graf 		.fc_table = RT6_TABLE_LOCAL,
157386872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
157486872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
157586872cb5SThomas Graf 		.fc_dst_len = 8,
157686872cb5SThomas Graf 		.fc_flags = RTF_UP,
157786872cb5SThomas Graf 	};
15781da177e4SLinus Torvalds 
157986872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
158086872cb5SThomas Graf 
158186872cb5SThomas Graf 	ip6_route_add(&cfg);
15821da177e4SLinus Torvalds }
15831da177e4SLinus Torvalds 
15840be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
15851da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev)
15861da177e4SLinus Torvalds {
158786872cb5SThomas Graf 	struct fib6_config cfg = {
158886872cb5SThomas Graf 		.fc_table = RT6_TABLE_MAIN,
158986872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
159086872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
159186872cb5SThomas Graf 		.fc_dst_len = 96,
159286872cb5SThomas Graf 		.fc_flags = RTF_UP | RTF_NONEXTHOP,
159386872cb5SThomas Graf 	};
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	/* prefix length - 96 bits "::d.d.d.d" */
159686872cb5SThomas Graf 	ip6_route_add(&cfg);
15971da177e4SLinus Torvalds }
15980be669bbSJoerg Roedel #endif
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev)
16011da177e4SLinus Torvalds {
16021da177e4SLinus Torvalds 	struct in6_addr addr;
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
16051da177e4SLinus Torvalds 	addrconf_prefix_route(&addr, 64, dev, 0, 0);
16061da177e4SLinus Torvalds }
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
16091da177e4SLinus Torvalds {
16101da177e4SLinus Torvalds 	struct inet6_dev *idev;
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	ASSERT_RTNL();
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL)
16151da177e4SLinus Torvalds 		return NULL;
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 	/* Add default multicast route */
16181da177e4SLinus Torvalds 	addrconf_add_mroute(dev);
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	/* Add link local route */
16211da177e4SLinus Torvalds 	addrconf_add_lroute(dev);
16221da177e4SLinus Torvalds 	return idev;
16231da177e4SLinus Torvalds }
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
16261da177e4SLinus Torvalds {
16271da177e4SLinus Torvalds 	struct prefix_info *pinfo;
16281da177e4SLinus Torvalds 	__u32 valid_lft;
16291da177e4SLinus Torvalds 	__u32 prefered_lft;
16301da177e4SLinus Torvalds 	int addr_type;
16311da177e4SLinus Torvalds 	unsigned long rt_expires;
16321da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
16371da177e4SLinus Torvalds 		ADBG(("addrconf: prefix option too short\n"));
16381da177e4SLinus Torvalds 		return;
16391da177e4SLinus Torvalds 	}
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	/*
16421da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
16431da177e4SLinus Torvalds 	 */
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
16481da177e4SLinus Torvalds 		return;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
16511da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
16541da177e4SLinus Torvalds 		if (net_ratelimit())
16551da177e4SLinus Torvalds 			printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
16561da177e4SLinus Torvalds 		return;
16571da177e4SLinus Torvalds 	}
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	if (in6_dev == NULL) {
16621da177e4SLinus Torvalds 		if (net_ratelimit())
16631da177e4SLinus Torvalds 			printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
16641da177e4SLinus Torvalds 		return;
16651da177e4SLinus Torvalds 	}
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	/*
16681da177e4SLinus Torvalds 	 *	Two things going on here:
16691da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
16701da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
16711da177e4SLinus Torvalds 	 */
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	/* Avoid arithmetic overflow. Really, we could
16741da177e4SLinus Torvalds 	   save rt_expires in seconds, likely valid_lft,
16751da177e4SLinus Torvalds 	   but it would require division in fib gc, that it
16761da177e4SLinus Torvalds 	   not good.
16771da177e4SLinus Torvalds 	 */
16781da177e4SLinus Torvalds 	if (valid_lft >= 0x7FFFFFFF/HZ)
16793dd4bc68SYOSHIFUJI Hideaki 		rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
16801da177e4SLinus Torvalds 	else
16813dd4bc68SYOSHIFUJI Hideaki 		rt_expires = valid_lft * HZ;
16823dd4bc68SYOSHIFUJI Hideaki 
16833dd4bc68SYOSHIFUJI Hideaki 	/*
16843dd4bc68SYOSHIFUJI Hideaki 	 * We convert this (in jiffies) to clock_t later.
16853dd4bc68SYOSHIFUJI Hideaki 	 * Avoid arithmetic overflow there as well.
16863dd4bc68SYOSHIFUJI Hideaki 	 * Overflow can happen only if HZ < USER_HZ.
16873dd4bc68SYOSHIFUJI Hideaki 	 */
16883dd4bc68SYOSHIFUJI Hideaki 	if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
16893dd4bc68SYOSHIFUJI Hideaki 		rt_expires = 0x7FFFFFFF / USER_HZ;
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds 	if (pinfo->onlink) {
16921da177e4SLinus Torvalds 		struct rt6_info *rt;
16931da177e4SLinus Torvalds 		rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 		if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
16961da177e4SLinus Torvalds 			if (rt->rt6i_flags&RTF_EXPIRES) {
16971da177e4SLinus Torvalds 				if (valid_lft == 0) {
1698e0a1ad73SThomas Graf 					ip6_del_rt(rt);
16991da177e4SLinus Torvalds 					rt = NULL;
17001da177e4SLinus Torvalds 				} else {
17013dd4bc68SYOSHIFUJI Hideaki 					rt->rt6i_expires = jiffies + rt_expires;
17021da177e4SLinus Torvalds 				}
17031da177e4SLinus Torvalds 			}
17041da177e4SLinus Torvalds 		} else if (valid_lft) {
17051da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
17063dd4bc68SYOSHIFUJI Hideaki 					      dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
17071da177e4SLinus Torvalds 		}
17081da177e4SLinus Torvalds 		if (rt)
17091da177e4SLinus Torvalds 			dst_release(&rt->u.dst);
17101da177e4SLinus Torvalds 	}
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
17151da177e4SLinus Torvalds 		struct inet6_ifaddr * ifp;
17161da177e4SLinus Torvalds 		struct in6_addr addr;
17171da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
17201da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
17211da177e4SLinus Torvalds 			if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
17221da177e4SLinus Torvalds 			    ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
17231da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
17241da177e4SLinus Torvalds 				return;
17251da177e4SLinus Torvalds 			}
17261da177e4SLinus Torvalds 			goto ok;
17271da177e4SLinus Torvalds 		}
17281da177e4SLinus Torvalds 		if (net_ratelimit())
17291da177e4SLinus Torvalds 			printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
17301da177e4SLinus Torvalds 			       pinfo->prefix_len);
17311da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
17321da177e4SLinus Torvalds 		return;
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds ok:
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 		ifp = ipv6_get_ifaddr(&addr, dev, 1);
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 		if (ifp == NULL && valid_lft) {
17391da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
174095c385b4SNeil Horman 			u32 addr_flags = 0;
174195c385b4SNeil Horman 
174295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
174395c385b4SNeil Horman 			if (in6_dev->cnf.optimistic_dad &&
174495c385b4SNeil Horman 			    !ipv6_devconf.forwarding)
174595c385b4SNeil Horman 				addr_flags = IFA_F_OPTIMISTIC;
174695c385b4SNeil Horman #endif
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
17491da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
17501da177e4SLinus Torvalds 			 */
17511da177e4SLinus Torvalds 			if (!max_addresses ||
17521da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
17531da177e4SLinus Torvalds 				ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
175495c385b4SNeil Horman 						    addr_type&IPV6_ADDR_SCOPE_MASK,
175595c385b4SNeil Horman 						    addr_flags);
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds 			if (!ifp || IS_ERR(ifp)) {
17581da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
17591da177e4SLinus Torvalds 				return;
17601da177e4SLinus Torvalds 			}
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 			update_lft = create = 1;
17631da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
17641da177e4SLinus Torvalds 			addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
17651da177e4SLinus Torvalds 		}
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 		if (ifp) {
17681da177e4SLinus Torvalds 			int flags;
17691da177e4SLinus Torvalds 			unsigned long now;
17701da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
17711da177e4SLinus Torvalds 			struct inet6_ifaddr *ift;
17721da177e4SLinus Torvalds #endif
17731da177e4SLinus Torvalds 			u32 stored_lft;
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
17761da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
17771da177e4SLinus Torvalds 			now = jiffies;
17781da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
17791da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
17801da177e4SLinus Torvalds 			else
17811da177e4SLinus Torvalds 				stored_lft = 0;
17821da177e4SLinus Torvalds 			if (!update_lft && stored_lft) {
17831da177e4SLinus Torvalds 				if (valid_lft > MIN_VALID_LIFETIME ||
17841da177e4SLinus Torvalds 				    valid_lft > stored_lft)
17851da177e4SLinus Torvalds 					update_lft = 1;
17861da177e4SLinus Torvalds 				else if (stored_lft <= MIN_VALID_LIFETIME) {
17871da177e4SLinus Torvalds 					/* valid_lft <= stored_lft is always true */
17881da177e4SLinus Torvalds 					/* XXX: IPsec */
17891da177e4SLinus Torvalds 					update_lft = 0;
17901da177e4SLinus Torvalds 				} else {
17911da177e4SLinus Torvalds 					valid_lft = MIN_VALID_LIFETIME;
17921da177e4SLinus Torvalds 					if (valid_lft < prefered_lft)
17931da177e4SLinus Torvalds 						prefered_lft = valid_lft;
17941da177e4SLinus Torvalds 					update_lft = 1;
17951da177e4SLinus Torvalds 				}
17961da177e4SLinus Torvalds 			}
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 			if (update_lft) {
17991da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
18001da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
18011da177e4SLinus Torvalds 				ifp->tstamp = now;
18021da177e4SLinus Torvalds 				flags = ifp->flags;
18031da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
18041da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
18071da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
18081da177e4SLinus Torvalds 			} else
18091da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
18121da177e4SLinus Torvalds 			read_lock_bh(&in6_dev->lock);
18131da177e4SLinus Torvalds 			/* update all temporary addresses in the list */
18141da177e4SLinus Torvalds 			for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
18151da177e4SLinus Torvalds 				/*
18161da177e4SLinus Torvalds 				 * When adjusting the lifetimes of an existing
18171da177e4SLinus Torvalds 				 * temporary address, only lower the lifetimes.
18181da177e4SLinus Torvalds 				 * Implementations must not increase the
18191da177e4SLinus Torvalds 				 * lifetimes of an existing temporary address
18201da177e4SLinus Torvalds 				 * when processing a Prefix Information Option.
18211da177e4SLinus Torvalds 				 */
18221da177e4SLinus Torvalds 				spin_lock(&ift->lock);
18231da177e4SLinus Torvalds 				flags = ift->flags;
18241da177e4SLinus Torvalds 				if (ift->valid_lft > valid_lft &&
18251da177e4SLinus Torvalds 				    ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
18261da177e4SLinus Torvalds 					ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
18271da177e4SLinus Torvalds 				if (ift->prefered_lft > prefered_lft &&
18281da177e4SLinus Torvalds 				    ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
18291da177e4SLinus Torvalds 					ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
18301da177e4SLinus Torvalds 				spin_unlock(&ift->lock);
18311da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
18321da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ift);
18331da177e4SLinus Torvalds 			}
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 			if (create && in6_dev->cnf.use_tempaddr > 0) {
18361da177e4SLinus Torvalds 				/*
18371da177e4SLinus Torvalds 				 * When a new public address is created as described in [ADDRCONF],
18381da177e4SLinus Torvalds 				 * also create a new temporary address.
18391da177e4SLinus Torvalds 				 */
18401da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
18411da177e4SLinus Torvalds 				ipv6_create_tempaddr(ifp, NULL);
18421da177e4SLinus Torvalds 			} else {
18431da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
18441da177e4SLinus Torvalds 			}
18451da177e4SLinus Torvalds #endif
18461da177e4SLinus Torvalds 			in6_ifa_put(ifp);
18471da177e4SLinus Torvalds 			addrconf_verify(0);
18481da177e4SLinus Torvalds 		}
18491da177e4SLinus Torvalds 	}
18501da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
18511da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
18521da177e4SLinus Torvalds }
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds /*
18551da177e4SLinus Torvalds  *	Set destination address.
18561da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
18571da177e4SLinus Torvalds  *	device.
18581da177e4SLinus Torvalds  */
18591da177e4SLinus Torvalds int addrconf_set_dstaddr(void __user *arg)
18601da177e4SLinus Torvalds {
18611da177e4SLinus Torvalds 	struct in6_ifreq ireq;
18621da177e4SLinus Torvalds 	struct net_device *dev;
18631da177e4SLinus Torvalds 	int err = -EINVAL;
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds 	rtnl_lock();
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	err = -EFAULT;
18681da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
18691da177e4SLinus Torvalds 		goto err_exit;
18701da177e4SLinus Torvalds 
1871881d966bSEric W. Biederman 	dev = __dev_get_by_index(&init_net, ireq.ifr6_ifindex);
18721da177e4SLinus Torvalds 
18731da177e4SLinus Torvalds 	err = -ENODEV;
18741da177e4SLinus Torvalds 	if (dev == NULL)
18751da177e4SLinus Torvalds 		goto err_exit;
18761da177e4SLinus Torvalds 
18770be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
18781da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
18791da177e4SLinus Torvalds 		struct ifreq ifr;
18801da177e4SLinus Torvalds 		mm_segment_t	oldfs;
18811da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
18841da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
18851da177e4SLinus Torvalds 			goto err_exit;
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
18881da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
18891da177e4SLinus Torvalds 		p.iph.saddr = 0;
18901da177e4SLinus Torvalds 		p.iph.version = 4;
18911da177e4SLinus Torvalds 		p.iph.ihl = 5;
18921da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
18931da177e4SLinus Torvalds 		p.iph.ttl = 64;
18941da177e4SLinus Torvalds 		ifr.ifr_ifru.ifru_data = (void __user *)&p;
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 		oldfs = get_fs(); set_fs(KERNEL_DS);
18971da177e4SLinus Torvalds 		err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
18981da177e4SLinus Torvalds 		set_fs(oldfs);
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 		if (err == 0) {
19011da177e4SLinus Torvalds 			err = -ENOBUFS;
1902881d966bSEric W. Biederman 			if ((dev = __dev_get_by_name(&init_net, p.name)) == NULL)
19031da177e4SLinus Torvalds 				goto err_exit;
19041da177e4SLinus Torvalds 			err = dev_open(dev);
19051da177e4SLinus Torvalds 		}
19061da177e4SLinus Torvalds 	}
19070be669bbSJoerg Roedel #endif
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds err_exit:
19101da177e4SLinus Torvalds 	rtnl_unlock();
19111da177e4SLinus Torvalds 	return err;
19121da177e4SLinus Torvalds }
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds /*
19151da177e4SLinus Torvalds  *	Manual configuration of address on an interface
19161da177e4SLinus Torvalds  */
19170778769dSNoriaki TAKAMIYA static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
191855ebaef1SNoriaki TAKAMIYA 			  __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
19191da177e4SLinus Torvalds {
19201da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
19211da177e4SLinus Torvalds 	struct inet6_dev *idev;
19221da177e4SLinus Torvalds 	struct net_device *dev;
19231da177e4SLinus Torvalds 	int scope;
192446d48046SYOSHIFUJI Hideaki 	u32 flags = RTF_EXPIRES;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	ASSERT_RTNL();
19271da177e4SLinus Torvalds 
19280778769dSNoriaki TAKAMIYA 	/* check the lifetime */
19290778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
19300778769dSNoriaki TAKAMIYA 		return -EINVAL;
19310778769dSNoriaki TAKAMIYA 
1932881d966bSEric W. Biederman 	if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
19331da177e4SLinus Torvalds 		return -ENODEV;
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL)
19361da177e4SLinus Torvalds 		return -ENOBUFS;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
19391da177e4SLinus Torvalds 
194046d48046SYOSHIFUJI Hideaki 	if (valid_lft == INFINITY_LIFE_TIME) {
19410778769dSNoriaki TAKAMIYA 		ifa_flags |= IFA_F_PERMANENT;
194246d48046SYOSHIFUJI Hideaki 		flags = 0;
194346d48046SYOSHIFUJI Hideaki 	} else if (valid_lft >= 0x7FFFFFFF/HZ)
19440778769dSNoriaki TAKAMIYA 		valid_lft = 0x7FFFFFFF/HZ;
19450778769dSNoriaki TAKAMIYA 
19460778769dSNoriaki TAKAMIYA 	if (prefered_lft == 0)
19470778769dSNoriaki TAKAMIYA 		ifa_flags |= IFA_F_DEPRECATED;
19480778769dSNoriaki TAKAMIYA 	else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
19490778769dSNoriaki TAKAMIYA 		 (prefered_lft != INFINITY_LIFE_TIME))
19500778769dSNoriaki TAKAMIYA 		prefered_lft = 0x7FFFFFFF/HZ;
19510778769dSNoriaki TAKAMIYA 
19520778769dSNoriaki TAKAMIYA 	ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
19530778769dSNoriaki TAKAMIYA 
19541da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
195506aebfb7SHerbert Xu 		spin_lock_bh(&ifp->lock);
19560778769dSNoriaki TAKAMIYA 		ifp->valid_lft = valid_lft;
19570778769dSNoriaki TAKAMIYA 		ifp->prefered_lft = prefered_lft;
19580778769dSNoriaki TAKAMIYA 		ifp->tstamp = jiffies;
195906aebfb7SHerbert Xu 		spin_unlock_bh(&ifp->lock);
19600778769dSNoriaki TAKAMIYA 
196146d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
196246d48046SYOSHIFUJI Hideaki 				      jiffies_to_clock_t(valid_lft * HZ), flags);
196395c385b4SNeil Horman 		/*
196495c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
196595c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
196695c385b4SNeil Horman 		 * manually configured addresses
196795c385b4SNeil Horman 		 */
19681da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
19691da177e4SLinus Torvalds 		in6_ifa_put(ifp);
19700778769dSNoriaki TAKAMIYA 		addrconf_verify(0);
19711da177e4SLinus Torvalds 		return 0;
19721da177e4SLinus Torvalds 	}
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	return PTR_ERR(ifp);
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
19781da177e4SLinus Torvalds {
19791da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
19801da177e4SLinus Torvalds 	struct inet6_dev *idev;
19811da177e4SLinus Torvalds 	struct net_device *dev;
19821da177e4SLinus Torvalds 
1983881d966bSEric W. Biederman 	if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
19841da177e4SLinus Torvalds 		return -ENODEV;
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL)
19871da177e4SLinus Torvalds 		return -ENXIO;
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
19901da177e4SLinus Torvalds 	for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
19911da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
19921da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
19931da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
19941da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 			/* If the last address is deleted administratively,
19991da177e4SLinus Torvalds 			   disable IPv6 on this interface.
20001da177e4SLinus Torvalds 			 */
20011da177e4SLinus Torvalds 			if (idev->addr_list == NULL)
20021da177e4SLinus Torvalds 				addrconf_ifdown(idev->dev, 1);
20031da177e4SLinus Torvalds 			return 0;
20041da177e4SLinus Torvalds 		}
20051da177e4SLinus Torvalds 	}
20061da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
20071da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
20081da177e4SLinus Torvalds }
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds int addrconf_add_ifaddr(void __user *arg)
20121da177e4SLinus Torvalds {
20131da177e4SLinus Torvalds 	struct in6_ifreq ireq;
20141da177e4SLinus Torvalds 	int err;
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
20171da177e4SLinus Torvalds 		return -EPERM;
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
20201da177e4SLinus Torvalds 		return -EFAULT;
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 	rtnl_lock();
20230778769dSNoriaki TAKAMIYA 	err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
202455ebaef1SNoriaki TAKAMIYA 			     IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
20251da177e4SLinus Torvalds 	rtnl_unlock();
20261da177e4SLinus Torvalds 	return err;
20271da177e4SLinus Torvalds }
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds int addrconf_del_ifaddr(void __user *arg)
20301da177e4SLinus Torvalds {
20311da177e4SLinus Torvalds 	struct in6_ifreq ireq;
20321da177e4SLinus Torvalds 	int err;
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
20351da177e4SLinus Torvalds 		return -EPERM;
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
20381da177e4SLinus Torvalds 		return -EFAULT;
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	rtnl_lock();
20411da177e4SLinus Torvalds 	err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
20421da177e4SLinus Torvalds 	rtnl_unlock();
20431da177e4SLinus Torvalds 	return err;
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds 
20460be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
20471da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
20481da177e4SLinus Torvalds {
20491da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
20501da177e4SLinus Torvalds 	struct in6_addr addr;
20511da177e4SLinus Torvalds 	struct net_device *dev;
20521da177e4SLinus Torvalds 	int scope;
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds 	ASSERT_RTNL();
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
20571da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
20601da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
20611da177e4SLinus Torvalds 		scope = IFA_LINK;
20621da177e4SLinus Torvalds 	} else {
20631da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
20641da177e4SLinus Torvalds 	}
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
20671da177e4SLinus Torvalds 		ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
20681da177e4SLinus Torvalds 		if (!IS_ERR(ifp)) {
20691da177e4SLinus Torvalds 			spin_lock_bh(&ifp->lock);
20701da177e4SLinus Torvalds 			ifp->flags &= ~IFA_F_TENTATIVE;
20711da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
20721da177e4SLinus Torvalds 			ipv6_ifa_notify(RTM_NEWADDR, ifp);
20731da177e4SLinus Torvalds 			in6_ifa_put(ifp);
20741da177e4SLinus Torvalds 		}
20751da177e4SLinus Torvalds 		return;
20761da177e4SLinus Torvalds 	}
20771da177e4SLinus Torvalds 
2078881d966bSEric W. Biederman 	for_each_netdev(&init_net, dev) {
2079e5ed6399SHerbert Xu 		struct in_device * in_dev = __in_dev_get_rtnl(dev);
20801da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
20811da177e4SLinus Torvalds 			struct in_ifaddr * ifa;
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds 			int flag = scope;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
20861da177e4SLinus Torvalds 				int plen;
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
20911da177e4SLinus Torvalds 					continue;
20921da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
20931da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
20941da177e4SLinus Torvalds 						continue;
20951da177e4SLinus Torvalds 					flag |= IFA_HOST;
20961da177e4SLinus Torvalds 				}
20971da177e4SLinus Torvalds 				if (idev->dev->flags&IFF_POINTOPOINT)
20981da177e4SLinus Torvalds 					plen = 64;
20991da177e4SLinus Torvalds 				else
21001da177e4SLinus Torvalds 					plen = 96;
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 				ifp = ipv6_add_addr(idev, &addr, plen, flag,
21031da177e4SLinus Torvalds 						    IFA_F_PERMANENT);
21041da177e4SLinus Torvalds 				if (!IS_ERR(ifp)) {
21051da177e4SLinus Torvalds 					spin_lock_bh(&ifp->lock);
21061da177e4SLinus Torvalds 					ifp->flags &= ~IFA_F_TENTATIVE;
21071da177e4SLinus Torvalds 					spin_unlock_bh(&ifp->lock);
21081da177e4SLinus Torvalds 					ipv6_ifa_notify(RTM_NEWADDR, ifp);
21091da177e4SLinus Torvalds 					in6_ifa_put(ifp);
21101da177e4SLinus Torvalds 				}
21111da177e4SLinus Torvalds 			}
21121da177e4SLinus Torvalds 		}
21131da177e4SLinus Torvalds 	}
21141da177e4SLinus Torvalds }
21150be669bbSJoerg Roedel #endif
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
21181da177e4SLinus Torvalds {
21191da177e4SLinus Torvalds 	struct inet6_dev  *idev;
21201da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds 	/* ::1 */
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds 	ASSERT_RTNL();
21251da177e4SLinus Torvalds 
21261da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
21271da177e4SLinus Torvalds 		printk(KERN_DEBUG "init loopback: add_dev failed\n");
21281da177e4SLinus Torvalds 		return;
21291da177e4SLinus Torvalds 	}
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 	ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
21321da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
21331da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
21341da177e4SLinus Torvalds 		ifp->flags &= ~IFA_F_TENTATIVE;
21351da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
21361da177e4SLinus Torvalds 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
21371da177e4SLinus Torvalds 		in6_ifa_put(ifp);
21381da177e4SLinus Torvalds 	}
21391da177e4SLinus Torvalds }
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
21421da177e4SLinus Torvalds {
21431da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
214495c385b4SNeil Horman 	u32 addr_flags = IFA_F_PERMANENT;
21451da177e4SLinus Torvalds 
214695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
214795c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
214895c385b4SNeil Horman 	    !ipv6_devconf.forwarding)
214995c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
215095c385b4SNeil Horman #endif
215195c385b4SNeil Horman 
215295c385b4SNeil Horman 
215395c385b4SNeil Horman 	ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
21541da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
215546d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
21561da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
21571da177e4SLinus Torvalds 		in6_ifa_put(ifp);
21581da177e4SLinus Torvalds 	}
21591da177e4SLinus Torvalds }
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
21621da177e4SLinus Torvalds {
21631da177e4SLinus Torvalds 	struct in6_addr addr;
21641da177e4SLinus Torvalds 	struct inet6_dev    * idev;
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 	ASSERT_RTNL();
21671da177e4SLinus Torvalds 
216874235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
216974235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
217074235a25SHerbert Xu 	    (dev->type != ARPHRD_IEEE802_TR) &&
217174235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
217274235a25SHerbert Xu 	    (dev->type != ARPHRD_INFINIBAND)) {
217374235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
217474235a25SHerbert Xu 		return;
217574235a25SHerbert Xu 	}
217674235a25SHerbert Xu 
21771da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
21781da177e4SLinus Torvalds 	if (idev == NULL)
21791da177e4SLinus Torvalds 		return;
21801da177e4SLinus Torvalds 
21811da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
21821da177e4SLinus Torvalds 	addr.s6_addr32[0] = htonl(0xFE800000);
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 	if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
21851da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &addr);
21861da177e4SLinus Torvalds }
21871da177e4SLinus Torvalds 
21880be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
21891da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
21901da177e4SLinus Torvalds {
21911da177e4SLinus Torvalds 	struct inet6_dev *idev;
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	ASSERT_RTNL();
21941da177e4SLinus Torvalds 
21951da177e4SLinus Torvalds 	/*
21961da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
21971da177e4SLinus Torvalds 	 * addresses... we should configure all of
21981da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
21991da177e4SLinus Torvalds 	 */
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
22021da177e4SLinus Torvalds 		printk(KERN_DEBUG "init sit: add_dev failed\n");
22031da177e4SLinus Torvalds 		return;
22041da177e4SLinus Torvalds 	}
22051da177e4SLinus Torvalds 
2206c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
2207c7dc89c0SFred L. Templin 		struct in6_addr addr;
2208c7dc89c0SFred L. Templin 
2209c7dc89c0SFred L. Templin 		ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2210c7dc89c0SFred L. Templin 		addrconf_prefix_route(&addr, 64, dev, 0, 0);
2211c7dc89c0SFred L. Templin 		if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2212c7dc89c0SFred L. Templin 			addrconf_add_linklocal(idev, &addr);
2213c7dc89c0SFred L. Templin 		return;
2214c7dc89c0SFred L. Templin 	}
2215c7dc89c0SFred L. Templin 
22161da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds 	if (dev->flags&IFF_POINTOPOINT) {
22191da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
22201da177e4SLinus Torvalds 		addrconf_add_lroute(dev);
22211da177e4SLinus Torvalds 	} else
22221da177e4SLinus Torvalds 		sit_route_add(dev);
22231da177e4SLinus Torvalds }
22240be669bbSJoerg Roedel #endif
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds static inline int
22271da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
22281da177e4SLinus Torvalds {
22291da177e4SLinus Torvalds 	struct in6_addr lladdr;
22301da177e4SLinus Torvalds 
223195c385b4SNeil Horman 	if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
22321da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &lladdr);
22331da177e4SLinus Torvalds 		return 0;
22341da177e4SLinus Torvalds 	}
22351da177e4SLinus Torvalds 	return -1;
22361da177e4SLinus Torvalds }
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
22391da177e4SLinus Torvalds {
22401da177e4SLinus Torvalds 	struct net_device *link_dev;
22411da177e4SLinus Torvalds 
22421da177e4SLinus Torvalds 	/* first try to inherit the link-local address from the link device */
22431da177e4SLinus Torvalds 	if (idev->dev->iflink &&
2244881d966bSEric W. Biederman 	    (link_dev = __dev_get_by_index(&init_net, idev->dev->iflink))) {
22451da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
22461da177e4SLinus Torvalds 			return;
22471da177e4SLinus Torvalds 	}
22481da177e4SLinus Torvalds 	/* then try to inherit it from any device */
2249881d966bSEric W. Biederman 	for_each_netdev(&init_net, link_dev) {
22501da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
22511da177e4SLinus Torvalds 			return;
22521da177e4SLinus Torvalds 	}
22531da177e4SLinus Torvalds 	printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
22541da177e4SLinus Torvalds }
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds /*
22571da177e4SLinus Torvalds  * Autoconfigure tunnel with a link-local address so routing protocols,
22581da177e4SLinus Torvalds  * DHCPv6, MLD etc. can be run over the virtual link
22591da177e4SLinus Torvalds  */
22601da177e4SLinus Torvalds 
22611da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev)
22621da177e4SLinus Torvalds {
22631da177e4SLinus Torvalds 	struct inet6_dev *idev;
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	ASSERT_RTNL();
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL) {
22681da177e4SLinus Torvalds 		printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
22691da177e4SLinus Torvalds 		return;
22701da177e4SLinus Torvalds 	}
22711da177e4SLinus Torvalds 	ip6_tnl_add_linklocal(idev);
22721da177e4SLinus Torvalds }
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
22751da177e4SLinus Torvalds 			   void * data)
22761da177e4SLinus Torvalds {
22771da177e4SLinus Torvalds 	struct net_device *dev = (struct net_device *) data;
227874235a25SHerbert Xu 	struct inet6_dev *idev = __in6_dev_get(dev);
2279c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
2280b217d616SHerbert Xu 	int err;
22811da177e4SLinus Torvalds 
2282e9dc8653SEric W. Biederman 	if (dev->nd_net != &init_net)
2283e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
2284e9dc8653SEric W. Biederman 
22851da177e4SLinus Torvalds 	switch(event) {
228645ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
228774235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
228845ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
228945ba9dd2SYOSHIFUJI Hideaki 			if (!idev)
2290b217d616SHerbert Xu 				return notifier_from_errno(-ENOMEM);
229145ba9dd2SYOSHIFUJI Hideaki 		}
229245ba9dd2SYOSHIFUJI Hideaki 		break;
22931da177e4SLinus Torvalds 	case NETDEV_UP:
22943c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
2295c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
2296c2edacf8SJay Vosburgh 			break;
2297c2edacf8SJay Vosburgh 
22983c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
2299f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
23003c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
23013c21edbdSYOSHIFUJI Hideaki 				printk(KERN_INFO
23023c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_UP): %s: "
23033c21edbdSYOSHIFUJI Hideaki 					"link is not ready\n",
23043c21edbdSYOSHIFUJI Hideaki 					dev->name);
23053c21edbdSYOSHIFUJI Hideaki 				break;
23063c21edbdSYOSHIFUJI Hideaki 			}
230799081049SKristian Slavov 
2308d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
2309d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
2310d31c7b8fSEvgeniy Polyakov 
231199081049SKristian Slavov 			if (idev)
231299081049SKristian Slavov 				idev->if_flags |= IF_READY;
23133c21edbdSYOSHIFUJI Hideaki 		} else {
2314f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
23153c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
23163c21edbdSYOSHIFUJI Hideaki 				break;
23173c21edbdSYOSHIFUJI Hideaki 			}
23183c21edbdSYOSHIFUJI Hideaki 
23193c21edbdSYOSHIFUJI Hideaki 			if (idev) {
23203c21edbdSYOSHIFUJI Hideaki 				if (idev->if_flags & IF_READY) {
23213c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
23223c21edbdSYOSHIFUJI Hideaki 					break;
23233c21edbdSYOSHIFUJI Hideaki 				}
23243c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
23253c21edbdSYOSHIFUJI Hideaki 			}
23263c21edbdSYOSHIFUJI Hideaki 
23273c21edbdSYOSHIFUJI Hideaki 			printk(KERN_INFO
23283c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_CHANGE): %s: "
23293c21edbdSYOSHIFUJI Hideaki 					"link becomes ready\n",
23303c21edbdSYOSHIFUJI Hideaki 					dev->name);
23313c21edbdSYOSHIFUJI Hideaki 
2332c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
23333c21edbdSYOSHIFUJI Hideaki 		}
23343c21edbdSYOSHIFUJI Hideaki 
23351da177e4SLinus Torvalds 		switch(dev->type) {
23360be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
23371da177e4SLinus Torvalds 		case ARPHRD_SIT:
23381da177e4SLinus Torvalds 			addrconf_sit_config(dev);
23391da177e4SLinus Torvalds 			break;
23400be669bbSJoerg Roedel #endif
23411da177e4SLinus Torvalds 		case ARPHRD_TUNNEL6:
23421da177e4SLinus Torvalds 			addrconf_ip6_tnl_config(dev);
23431da177e4SLinus Torvalds 			break;
23441da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
23451da177e4SLinus Torvalds 			init_loopback(dev);
23461da177e4SLinus Torvalds 			break;
23471da177e4SLinus Torvalds 
23481da177e4SLinus Torvalds 		default:
23491da177e4SLinus Torvalds 			addrconf_dev_config(dev);
23501da177e4SLinus Torvalds 			break;
23513ff50b79SStephen Hemminger 		}
23521da177e4SLinus Torvalds 		if (idev) {
2353c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
2354c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
2355c5e33bddSYOSHIFUJI Hideaki 
23561da177e4SLinus Torvalds 			/* If the MTU changed during the interface down, when the
23571da177e4SLinus Torvalds 			   interface up, the changed MTU must be reflected in the
23581da177e4SLinus Torvalds 			   idev as well as routers.
23591da177e4SLinus Torvalds 			 */
23601da177e4SLinus Torvalds 			if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
23611da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
23621da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
23631da177e4SLinus Torvalds 			}
23641da177e4SLinus Torvalds 			idev->tstamp = jiffies;
23651da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
23661da177e4SLinus Torvalds 			/* If the changed mtu during down is lower than IPV6_MIN_MTU
23671da177e4SLinus Torvalds 			   stop IPv6 on this interface.
23681da177e4SLinus Torvalds 			 */
23691da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
23701da177e4SLinus Torvalds 				addrconf_ifdown(dev, event != NETDEV_DOWN);
23711da177e4SLinus Torvalds 		}
23721da177e4SLinus Torvalds 		break;
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
23751da177e4SLinus Torvalds 		if (idev && dev->mtu >= IPV6_MIN_MTU) {
23761da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
23771da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
23781da177e4SLinus Torvalds 			break;
23791da177e4SLinus Torvalds 		}
23801da177e4SLinus Torvalds 
2381d31c7b8fSEvgeniy Polyakov 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2382d31c7b8fSEvgeniy Polyakov 			idev = ipv6_add_dev(dev);
2383d31c7b8fSEvgeniy Polyakov 			if (idev)
2384d31c7b8fSEvgeniy Polyakov 				break;
2385d31c7b8fSEvgeniy Polyakov 		}
2386d31c7b8fSEvgeniy Polyakov 
23871da177e4SLinus Torvalds 		/* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds 	case NETDEV_DOWN:
23901da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
23911da177e4SLinus Torvalds 		/*
23921da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
23931da177e4SLinus Torvalds 		 */
23941da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
23951da177e4SLinus Torvalds 		break;
23963c21edbdSYOSHIFUJI Hideaki 
23971da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
23981da177e4SLinus Torvalds 		if (idev) {
23995632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
2400408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
2401f52295a9SPavel Emelyanov 			addrconf_sysctl_register(idev);
2402b217d616SHerbert Xu 			err = snmp6_register_dev(idev);
2403b217d616SHerbert Xu 			if (err)
2404b217d616SHerbert Xu 				return notifier_from_errno(err);
24055632c515SStephen Hemminger 		}
24061da177e4SLinus Torvalds 		break;
24073ff50b79SStephen Hemminger 	}
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds 	return NOTIFY_OK;
24101da177e4SLinus Torvalds }
24111da177e4SLinus Torvalds 
24121da177e4SLinus Torvalds /*
24131da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
24141da177e4SLinus Torvalds  */
24151da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
24161da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
24171da177e4SLinus Torvalds 	.priority = 0
24181da177e4SLinus Torvalds };
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
24211da177e4SLinus Torvalds {
24221da177e4SLinus Torvalds 	struct inet6_dev *idev;
24231da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **bifa;
24241da177e4SLinus Torvalds 	int i;
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds 	ASSERT_RTNL();
24271da177e4SLinus Torvalds 
24282774c7abSEric W. Biederman 	if (dev == init_net.loopback_dev && how == 1)
24291da177e4SLinus Torvalds 		how = 0;
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds 	rt6_ifdown(dev);
24321da177e4SLinus Torvalds 	neigh_ifdown(&nd_tbl, dev);
24331da177e4SLinus Torvalds 
24341da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
24351da177e4SLinus Torvalds 	if (idev == NULL)
24361da177e4SLinus Torvalds 		return -ENODEV;
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds 	/* Step 1: remove reference to ipv6 device from parent device.
24391da177e4SLinus Torvalds 		   Do not dev_put!
24401da177e4SLinus Torvalds 	 */
24411da177e4SLinus Torvalds 	if (how == 1) {
24421da177e4SLinus Torvalds 		idev->dead = 1;
24438814c4b5SYOSHIFUJI Hideaki 
24448814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
24458814c4b5SYOSHIFUJI Hideaki 		rcu_assign_pointer(dev->ip6_ptr, NULL);
24461da177e4SLinus Torvalds 
24471da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
24481da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds 	}
24511da177e4SLinus Torvalds 
24521da177e4SLinus Torvalds 	/* Step 2: clear hash table */
24531da177e4SLinus Torvalds 	for (i=0; i<IN6_ADDR_HSIZE; i++) {
24541da177e4SLinus Torvalds 		bifa = &inet6_addr_lst[i];
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds 		write_lock_bh(&addrconf_hash_lock);
24571da177e4SLinus Torvalds 		while ((ifa = *bifa) != NULL) {
24581da177e4SLinus Torvalds 			if (ifa->idev == idev) {
24591da177e4SLinus Torvalds 				*bifa = ifa->lst_next;
24601da177e4SLinus Torvalds 				ifa->lst_next = NULL;
24611da177e4SLinus Torvalds 				addrconf_del_timer(ifa);
24621da177e4SLinus Torvalds 				in6_ifa_put(ifa);
24631da177e4SLinus Torvalds 				continue;
24641da177e4SLinus Torvalds 			}
24651da177e4SLinus Torvalds 			bifa = &ifa->lst_next;
24661da177e4SLinus Torvalds 		}
24671da177e4SLinus Torvalds 		write_unlock_bh(&addrconf_hash_lock);
24681da177e4SLinus Torvalds 	}
24691da177e4SLinus Torvalds 
24701da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds 	/* Step 3: clear flags for stateless addrconf */
24731da177e4SLinus Torvalds 	if (how != 1)
24743c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
24751da177e4SLinus Torvalds 
24761da177e4SLinus Torvalds 	/* Step 4: clear address list */
24771da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
24781da177e4SLinus Torvalds 	if (how == 1 && del_timer(&idev->regen_timer))
24791da177e4SLinus Torvalds 		in6_dev_put(idev);
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds 	/* clear tempaddr list */
24821da177e4SLinus Torvalds 	while ((ifa = idev->tempaddr_list) != NULL) {
24831da177e4SLinus Torvalds 		idev->tempaddr_list = ifa->tmp_next;
24841da177e4SLinus Torvalds 		ifa->tmp_next = NULL;
24851da177e4SLinus Torvalds 		ifa->dead = 1;
24861da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
24871da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
24881da177e4SLinus Torvalds 
24891da177e4SLinus Torvalds 		if (ifa->ifpub) {
24901da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
24911da177e4SLinus Torvalds 			ifa->ifpub = NULL;
24921da177e4SLinus Torvalds 		}
24931da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
24941da177e4SLinus Torvalds 		in6_ifa_put(ifa);
24951da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
24961da177e4SLinus Torvalds 	}
24971da177e4SLinus Torvalds #endif
24981da177e4SLinus Torvalds 	while ((ifa = idev->addr_list) != NULL) {
24991da177e4SLinus Torvalds 		idev->addr_list = ifa->if_next;
25001da177e4SLinus Torvalds 		ifa->if_next = NULL;
25011da177e4SLinus Torvalds 		ifa->dead = 1;
25021da177e4SLinus Torvalds 		addrconf_del_timer(ifa);
25031da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
25041da177e4SLinus Torvalds 
25051da177e4SLinus Torvalds 		__ipv6_ifa_notify(RTM_DELADDR, ifa);
2506063ed369SVlad Yasevich 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
25071da177e4SLinus Torvalds 		in6_ifa_put(ifa);
25081da177e4SLinus Torvalds 
25091da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
25101da177e4SLinus Torvalds 	}
25111da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds 	/* Step 5: Discard multicast list */
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds 	if (how == 1)
25161da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
25171da177e4SLinus Torvalds 	else
25181da177e4SLinus Torvalds 		ipv6_mc_down(idev);
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 	idev->tstamp = jiffies;
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds 	/* Shot the device (if unregistered) */
25231da177e4SLinus Torvalds 
25241da177e4SLinus Torvalds 	if (how == 1) {
2525408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
25261da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
25271da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
25281da177e4SLinus Torvalds 		in6_dev_put(idev);
25291da177e4SLinus Torvalds 	}
25301da177e4SLinus Torvalds 	return 0;
25311da177e4SLinus Torvalds }
25321da177e4SLinus Torvalds 
25331da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
25341da177e4SLinus Torvalds {
25351da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding)
25381da177e4SLinus Torvalds 		goto out;
25391da177e4SLinus Torvalds 
25401da177e4SLinus Torvalds 	if (ifp->idev->if_flags & IF_RA_RCVD) {
25411da177e4SLinus Torvalds 		/*
25421da177e4SLinus Torvalds 		 *	Announcement received after solicitation
25431da177e4SLinus Torvalds 		 *	was sent
25441da177e4SLinus Torvalds 		 */
25451da177e4SLinus Torvalds 		goto out;
25461da177e4SLinus Torvalds 	}
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds 	spin_lock(&ifp->lock);
25491da177e4SLinus Torvalds 	if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
25501da177e4SLinus Torvalds 		struct in6_addr all_routers;
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
25531da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS,
25541da177e4SLinus Torvalds 				   (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
25551da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_delay :
25561da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_interval);
25571da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 		ipv6_addr_all_routers(&all_routers);
25601da177e4SLinus Torvalds 
25611da177e4SLinus Torvalds 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
25621da177e4SLinus Torvalds 	} else {
25631da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
25641da177e4SLinus Torvalds 		/*
25651da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
25661da177e4SLinus Torvalds 		 * assumption any longer.
25671da177e4SLinus Torvalds 		 */
25681da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: no IPv6 routers present\n",
25691da177e4SLinus Torvalds 		       ifp->idev->dev->name);
25701da177e4SLinus Torvalds 	}
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds out:
25731da177e4SLinus Torvalds 	in6_ifa_put(ifp);
25741da177e4SLinus Torvalds }
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds /*
25771da177e4SLinus Torvalds  *	Duplicate Address Detection
25781da177e4SLinus Torvalds  */
25793c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
25803c21edbdSYOSHIFUJI Hideaki {
25813c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
25823c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
25833c21edbdSYOSHIFUJI Hideaki 
258495c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
258595c385b4SNeil Horman 		rand_num = 0;
258695c385b4SNeil Horman 	else
25873c21edbdSYOSHIFUJI Hideaki 		rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
258895c385b4SNeil Horman 
25893c21edbdSYOSHIFUJI Hideaki 	ifp->probes = idev->cnf.dad_transmits;
25903c21edbdSYOSHIFUJI Hideaki 	addrconf_mod_timer(ifp, AC_DAD, rand_num);
25913c21edbdSYOSHIFUJI Hideaki }
25923c21edbdSYOSHIFUJI Hideaki 
2593e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
25941da177e4SLinus Torvalds {
25951da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
25961da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds 	net_srandom(ifp->addr.s6_addr32[3]);
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
26031da177e4SLinus Torvalds 	if (ifp->dead)
26041da177e4SLinus Torvalds 		goto out;
26051da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
26061da177e4SLinus Torvalds 
26071da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
260855ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
260955ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
261095c385b4SNeil Horman 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
26111da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
26121da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
26151da177e4SLinus Torvalds 		return;
26161da177e4SLinus Torvalds 	}
26171da177e4SLinus Torvalds 
26186732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
26193dd3bf83SYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
26206732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
26213c21edbdSYOSHIFUJI Hideaki 		/*
26223c21edbdSYOSHIFUJI Hideaki 		 * If the defice is not ready:
26233c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
26243c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
26253c21edbdSYOSHIFUJI Hideaki 		 */
26263c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
26273c21edbdSYOSHIFUJI Hideaki 		addrconf_dad_stop(ifp);
26286732badeSYOSHIFUJI Hideaki 		return;
26293c21edbdSYOSHIFUJI Hideaki 	}
263095c385b4SNeil Horman 
263195c385b4SNeil Horman 	/*
263295c385b4SNeil Horman 	 * Optimistic nodes can start receiving
263395c385b4SNeil Horman 	 * Frames right away
263495c385b4SNeil Horman 	 */
263595c385b4SNeil Horman 	if(ifp->flags & IFA_F_OPTIMISTIC)
263695c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
263795c385b4SNeil Horman 
26386732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
26396732badeSYOSHIFUJI Hideaki 	spin_unlock_bh(&ifp->lock);
26401da177e4SLinus Torvalds out:
26411da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
26421da177e4SLinus Torvalds }
26431da177e4SLinus Torvalds 
26441da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data)
26451da177e4SLinus Torvalds {
26461da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
26471da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
26481da177e4SLinus Torvalds 	struct in6_addr unspec;
26491da177e4SLinus Torvalds 	struct in6_addr mcaddr;
26501da177e4SLinus Torvalds 
26511da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
26521da177e4SLinus Torvalds 	if (idev->dead) {
26531da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
26541da177e4SLinus Torvalds 		goto out;
26551da177e4SLinus Torvalds 	}
26561da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
26571da177e4SLinus Torvalds 	if (ifp->probes == 0) {
26581da177e4SLinus Torvalds 		/*
26591da177e4SLinus Torvalds 		 * DAD was successful
26601da177e4SLinus Torvalds 		 */
26611da177e4SLinus Torvalds 
266295c385b4SNeil Horman 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
26631da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
26641da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
26671da177e4SLinus Torvalds 
26681da177e4SLinus Torvalds 		goto out;
26691da177e4SLinus Torvalds 	}
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds 	ifp->probes--;
26721da177e4SLinus Torvalds 	addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
26731da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
26741da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
26771da177e4SLinus Torvalds 	memset(&unspec, 0, sizeof(unspec));
26781da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
26791da177e4SLinus Torvalds 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
26801da177e4SLinus Torvalds out:
26811da177e4SLinus Torvalds 	in6_ifa_put(ifp);
26821da177e4SLinus Torvalds }
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
26851da177e4SLinus Torvalds {
26861da177e4SLinus Torvalds 	struct net_device *	dev = ifp->idev->dev;
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds 	/*
26891da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
26901da177e4SLinus Torvalds 	 */
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds 	/* If added prefix is link local and forwarding is off,
26951da177e4SLinus Torvalds 	   start sending router solicitations.
26961da177e4SLinus Torvalds 	 */
26971da177e4SLinus Torvalds 
26981da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding == 0 &&
26991da177e4SLinus Torvalds 	    ifp->idev->cnf.rtr_solicits > 0 &&
27001da177e4SLinus Torvalds 	    (dev->flags&IFF_LOOPBACK) == 0 &&
27011da177e4SLinus Torvalds 	    (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
27021da177e4SLinus Torvalds 		struct in6_addr all_routers;
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds 		ipv6_addr_all_routers(&all_routers);
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds 		/*
27071da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
27081da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
27091da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
27101da177e4SLinus Torvalds 		 */
27111da177e4SLinus Torvalds 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
27141da177e4SLinus Torvalds 		ifp->probes = 1;
27151da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
27161da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
27171da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
27181da177e4SLinus Torvalds 	}
27191da177e4SLinus Torvalds }
27201da177e4SLinus Torvalds 
2721c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev) {
2722c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
2723c5e33bddSYOSHIFUJI Hideaki 
2724c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
2725c5e33bddSYOSHIFUJI Hideaki 	for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2726c5e33bddSYOSHIFUJI Hideaki 		spin_lock_bh(&ifp->lock);
2727c5e33bddSYOSHIFUJI Hideaki 		if (!(ifp->flags & IFA_F_TENTATIVE)) {
2728c5e33bddSYOSHIFUJI Hideaki 			spin_unlock_bh(&ifp->lock);
2729c5e33bddSYOSHIFUJI Hideaki 			continue;
2730c5e33bddSYOSHIFUJI Hideaki 		}
2731c5e33bddSYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
2732c5e33bddSYOSHIFUJI Hideaki 		addrconf_dad_kick(ifp);
2733c5e33bddSYOSHIFUJI Hideaki 	}
2734c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
2735c5e33bddSYOSHIFUJI Hideaki }
2736c5e33bddSYOSHIFUJI Hideaki 
27371da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
27381da177e4SLinus Torvalds struct if6_iter_state {
2739*3c40090aSDaniel Lezcano 	struct seq_net_private p;
27401da177e4SLinus Torvalds 	int bucket;
27411da177e4SLinus Torvalds };
27421da177e4SLinus Torvalds 
27431da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
27441da177e4SLinus Torvalds {
27451da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
27461da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
2747*3c40090aSDaniel Lezcano 	struct net *net = state->p.net;
27481da177e4SLinus Torvalds 
27491da177e4SLinus Torvalds 	for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
27501da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
2751*3c40090aSDaniel Lezcano 
2752*3c40090aSDaniel Lezcano 		while (ifa && ifa->idev->dev->nd_net != net)
2753*3c40090aSDaniel Lezcano 			ifa = ifa->lst_next;
27541da177e4SLinus Torvalds 		if (ifa)
27551da177e4SLinus Torvalds 			break;
27561da177e4SLinus Torvalds 	}
27571da177e4SLinus Torvalds 	return ifa;
27581da177e4SLinus Torvalds }
27591da177e4SLinus Torvalds 
27601da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
27611da177e4SLinus Torvalds {
27621da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
2763*3c40090aSDaniel Lezcano 	struct net *net = state->p.net;
27641da177e4SLinus Torvalds 
27651da177e4SLinus Torvalds 	ifa = ifa->lst_next;
27661da177e4SLinus Torvalds try_again:
2767*3c40090aSDaniel Lezcano 	if (ifa) {
2768*3c40090aSDaniel Lezcano 		if (ifa->idev->dev->nd_net != net) {
2769*3c40090aSDaniel Lezcano 			ifa = ifa->lst_next;
2770*3c40090aSDaniel Lezcano 			goto try_again;
2771*3c40090aSDaniel Lezcano 		}
2772*3c40090aSDaniel Lezcano 	}
2773*3c40090aSDaniel Lezcano 
27741da177e4SLinus Torvalds 	if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
27751da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
27761da177e4SLinus Torvalds 		goto try_again;
27771da177e4SLinus Torvalds 	}
2778*3c40090aSDaniel Lezcano 
27791da177e4SLinus Torvalds 	return ifa;
27801da177e4SLinus Torvalds }
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
27831da177e4SLinus Torvalds {
27841da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = if6_get_first(seq);
27851da177e4SLinus Torvalds 
27861da177e4SLinus Torvalds 	if (ifa)
27871da177e4SLinus Torvalds 		while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
27881da177e4SLinus Torvalds 			--pos;
27891da177e4SLinus Torvalds 	return pos ? NULL : ifa;
27901da177e4SLinus Torvalds }
27911da177e4SLinus Torvalds 
27921da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
27931da177e4SLinus Torvalds {
27941da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
27951da177e4SLinus Torvalds 	return if6_get_idx(seq, *pos);
27961da177e4SLinus Torvalds }
27971da177e4SLinus Torvalds 
27981da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
27991da177e4SLinus Torvalds {
28001da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
28031da177e4SLinus Torvalds 	++*pos;
28041da177e4SLinus Torvalds 	return ifa;
28051da177e4SLinus Torvalds }
28061da177e4SLinus Torvalds 
28071da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
28081da177e4SLinus Torvalds {
28091da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
28101da177e4SLinus Torvalds }
28111da177e4SLinus Torvalds 
28121da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
28131da177e4SLinus Torvalds {
28141da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
28151da177e4SLinus Torvalds 	seq_printf(seq,
28169343e79aSYOSHIFUJI Hideaki 		   NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
28171da177e4SLinus Torvalds 		   NIP6(ifp->addr),
28181da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
28191da177e4SLinus Torvalds 		   ifp->prefix_len,
28201da177e4SLinus Torvalds 		   ifp->scope,
28211da177e4SLinus Torvalds 		   ifp->flags,
28221da177e4SLinus Torvalds 		   ifp->idev->dev->name);
28231da177e4SLinus Torvalds 	return 0;
28241da177e4SLinus Torvalds }
28251da177e4SLinus Torvalds 
282656b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
28271da177e4SLinus Torvalds 	.start	= if6_seq_start,
28281da177e4SLinus Torvalds 	.next	= if6_seq_next,
28291da177e4SLinus Torvalds 	.show	= if6_seq_show,
28301da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
28311da177e4SLinus Torvalds };
28321da177e4SLinus Torvalds 
28331da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
28341da177e4SLinus Torvalds {
2835*3c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
2836cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
28371da177e4SLinus Torvalds }
28381da177e4SLinus Torvalds 
28399a32144eSArjan van de Ven static const struct file_operations if6_fops = {
28401da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
28411da177e4SLinus Torvalds 	.open		= if6_seq_open,
28421da177e4SLinus Torvalds 	.read		= seq_read,
28431da177e4SLinus Torvalds 	.llseek		= seq_lseek,
2844*3c40090aSDaniel Lezcano 	.release	= seq_release_net,
28451da177e4SLinus Torvalds };
28461da177e4SLinus Torvalds 
2847*3c40090aSDaniel Lezcano static int if6_proc_net_init(struct net *net)
28481da177e4SLinus Torvalds {
2849*3c40090aSDaniel Lezcano 	if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
28501da177e4SLinus Torvalds 		return -ENOMEM;
28511da177e4SLinus Torvalds 	return 0;
28521da177e4SLinus Torvalds }
28531da177e4SLinus Torvalds 
2854*3c40090aSDaniel Lezcano static void if6_proc_net_exit(struct net *net)
2855*3c40090aSDaniel Lezcano {
2856*3c40090aSDaniel Lezcano        proc_net_remove(net, "if_inet6");
2857*3c40090aSDaniel Lezcano }
2858*3c40090aSDaniel Lezcano 
2859*3c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
2860*3c40090aSDaniel Lezcano        .init = if6_proc_net_init,
2861*3c40090aSDaniel Lezcano        .exit = if6_proc_net_exit,
2862*3c40090aSDaniel Lezcano };
2863*3c40090aSDaniel Lezcano 
2864*3c40090aSDaniel Lezcano int __init if6_proc_init(void)
2865*3c40090aSDaniel Lezcano {
2866*3c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
2867*3c40090aSDaniel Lezcano }
2868*3c40090aSDaniel Lezcano 
28691da177e4SLinus Torvalds void if6_proc_exit(void)
28701da177e4SLinus Torvalds {
2871*3c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
28721da177e4SLinus Torvalds }
28731da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
28741da177e4SLinus Torvalds 
287559fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
28763b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
28773b9f9a1cSNoriaki TAKAMIYA int ipv6_chk_home_addr(struct in6_addr *addr)
28783b9f9a1cSNoriaki TAKAMIYA {
28793b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
28803b9f9a1cSNoriaki TAKAMIYA 	struct inet6_ifaddr * ifp;
28813b9f9a1cSNoriaki TAKAMIYA 	u8 hash = ipv6_addr_hash(addr);
28823b9f9a1cSNoriaki TAKAMIYA 	read_lock_bh(&addrconf_hash_lock);
28833b9f9a1cSNoriaki TAKAMIYA 	for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
28843b9f9a1cSNoriaki TAKAMIYA 		if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
28853b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
28863b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
28873b9f9a1cSNoriaki TAKAMIYA 			break;
28883b9f9a1cSNoriaki TAKAMIYA 		}
28893b9f9a1cSNoriaki TAKAMIYA 	}
28903b9f9a1cSNoriaki TAKAMIYA 	read_unlock_bh(&addrconf_hash_lock);
28913b9f9a1cSNoriaki TAKAMIYA 	return ret;
28923b9f9a1cSNoriaki TAKAMIYA }
28933b9f9a1cSNoriaki TAKAMIYA #endif
28943b9f9a1cSNoriaki TAKAMIYA 
28951da177e4SLinus Torvalds /*
28961da177e4SLinus Torvalds  *	Periodic address status verification
28971da177e4SLinus Torvalds  */
28981da177e4SLinus Torvalds 
28991da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo)
29001da177e4SLinus Torvalds {
29011da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
29021da177e4SLinus Torvalds 	unsigned long now, next;
29031da177e4SLinus Torvalds 	int i;
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds 	spin_lock_bh(&addrconf_verify_lock);
29061da177e4SLinus Torvalds 	now = jiffies;
29071da177e4SLinus Torvalds 	next = now + ADDR_CHECK_FREQUENCY;
29081da177e4SLinus Torvalds 
29091da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
29101da177e4SLinus Torvalds 
29111da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
29121da177e4SLinus Torvalds 
29131da177e4SLinus Torvalds restart:
29145d5780dfSYan Zheng 		read_lock(&addrconf_hash_lock);
29151da177e4SLinus Torvalds 		for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
29161da177e4SLinus Torvalds 			unsigned long age;
29171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
29181da177e4SLinus Torvalds 			unsigned long regen_advance;
29191da177e4SLinus Torvalds #endif
29201da177e4SLinus Torvalds 
29211da177e4SLinus Torvalds 			if (ifp->flags & IFA_F_PERMANENT)
29221da177e4SLinus Torvalds 				continue;
29231da177e4SLinus Torvalds 
29241da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
29251da177e4SLinus Torvalds 			age = (now - ifp->tstamp) / HZ;
29261da177e4SLinus Torvalds 
29271da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
29281da177e4SLinus Torvalds 			regen_advance = ifp->idev->cnf.regen_max_retry *
29291da177e4SLinus Torvalds 					ifp->idev->cnf.dad_transmits *
29301da177e4SLinus Torvalds 					ifp->idev->nd_parms->retrans_time / HZ;
29311da177e4SLinus Torvalds #endif
29321da177e4SLinus Torvalds 
29338f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
29348f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
29351da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
29361da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
29375d5780dfSYan Zheng 				read_unlock(&addrconf_hash_lock);
29381da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
29391da177e4SLinus Torvalds 				goto restart;
29408f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
29418f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
29428f27ebb9SYOSHIFUJI Hideaki 				continue;
29431da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
29441da177e4SLinus Torvalds 				/* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
29451da177e4SLinus Torvalds 				int deprecate = 0;
29461da177e4SLinus Torvalds 
29471da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
29481da177e4SLinus Torvalds 					deprecate = 1;
29491da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
29501da177e4SLinus Torvalds 				}
29511da177e4SLinus Torvalds 
29521da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
29531da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
29541da177e4SLinus Torvalds 
29551da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
29561da177e4SLinus Torvalds 
29571da177e4SLinus Torvalds 				if (deprecate) {
29581da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
29595d5780dfSYan Zheng 					read_unlock(&addrconf_hash_lock);
29601da177e4SLinus Torvalds 
29611da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
29621da177e4SLinus Torvalds 					in6_ifa_put(ifp);
29631da177e4SLinus Torvalds 					goto restart;
29641da177e4SLinus Torvalds 				}
29651da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
29661da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
29671da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
29681da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
29691da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
29701da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
29711da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
29721da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
29731da177e4SLinus Torvalds 						ifp->regen_count++;
29741da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
29751da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
29761da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
29775d5780dfSYan Zheng 						read_unlock(&addrconf_hash_lock);
2978291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
2979291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
2980291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
29811da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
29821da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
29831da177e4SLinus Torvalds 						in6_ifa_put(ifp);
29841da177e4SLinus Torvalds 						goto restart;
29851da177e4SLinus Torvalds 					}
29861da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
29871da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
29881da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
29891da177e4SLinus Torvalds #endif
29901da177e4SLinus Torvalds 			} else {
29911da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
29921da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
29931da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
29941da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
29951da177e4SLinus Torvalds 			}
29961da177e4SLinus Torvalds 		}
29975d5780dfSYan Zheng 		read_unlock(&addrconf_hash_lock);
29981da177e4SLinus Torvalds 	}
29991da177e4SLinus Torvalds 
30001da177e4SLinus Torvalds 	addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
30011da177e4SLinus Torvalds 	add_timer(&addr_chk_timer);
30021da177e4SLinus Torvalds 	spin_unlock_bh(&addrconf_verify_lock);
30031da177e4SLinus Torvalds }
30041da177e4SLinus Torvalds 
3005461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3006461d8837SThomas Graf {
3007461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
3008461d8837SThomas Graf 
3009461d8837SThomas Graf 	if (addr)
3010461d8837SThomas Graf 		pfx = nla_data(addr);
3011461d8837SThomas Graf 
3012461d8837SThomas Graf 	if (local) {
3013461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3014461d8837SThomas Graf 			pfx = NULL;
3015461d8837SThomas Graf 		else
3016461d8837SThomas Graf 			pfx = nla_data(local);
3017461d8837SThomas Graf 	}
3018461d8837SThomas Graf 
3019461d8837SThomas Graf 	return pfx;
3020461d8837SThomas Graf }
3021461d8837SThomas Graf 
3022ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3023461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
3024461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
3025461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
3026461d8837SThomas Graf };
3027461d8837SThomas Graf 
30281da177e4SLinus Torvalds static int
30291da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
30301da177e4SLinus Torvalds {
3031b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
3032b933f716SThomas Graf 	struct ifaddrmsg *ifm;
3033b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
30341da177e4SLinus Torvalds 	struct in6_addr *pfx;
3035b933f716SThomas Graf 	int err;
30361da177e4SLinus Torvalds 
3037b854272bSDenis V. Lunev 	if (net != &init_net)
3038b854272bSDenis V. Lunev 		return -EINVAL;
3039b854272bSDenis V. Lunev 
3040b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3041b933f716SThomas Graf 	if (err < 0)
3042b933f716SThomas Graf 		return err;
3043b933f716SThomas Graf 
3044b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
3045b933f716SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
30461da177e4SLinus Torvalds 	if (pfx == NULL)
30471da177e4SLinus Torvalds 		return -EINVAL;
30481da177e4SLinus Torvalds 
30491da177e4SLinus Torvalds 	return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
30501da177e4SLinus Torvalds }
30511da177e4SLinus Torvalds 
305255ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
305355ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
3054081bba5bSNoriaki TAKAMIYA {
305546d48046SYOSHIFUJI Hideaki 	u32 flags = RTF_EXPIRES;
305646d48046SYOSHIFUJI Hideaki 
3057081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
3058081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
3059081bba5bSNoriaki TAKAMIYA 
306046d48046SYOSHIFUJI Hideaki 	if (valid_lft == INFINITY_LIFE_TIME) {
306155ebaef1SNoriaki TAKAMIYA 		ifa_flags |= IFA_F_PERMANENT;
306246d48046SYOSHIFUJI Hideaki 		flags = 0;
306346d48046SYOSHIFUJI Hideaki 	} else if (valid_lft >= 0x7FFFFFFF/HZ)
3064081bba5bSNoriaki TAKAMIYA 		valid_lft = 0x7FFFFFFF/HZ;
3065081bba5bSNoriaki TAKAMIYA 
3066081bba5bSNoriaki TAKAMIYA 	if (prefered_lft == 0)
306755ebaef1SNoriaki TAKAMIYA 		ifa_flags |= IFA_F_DEPRECATED;
3068081bba5bSNoriaki TAKAMIYA 	else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
3069081bba5bSNoriaki TAKAMIYA 		 (prefered_lft != INFINITY_LIFE_TIME))
3070081bba5bSNoriaki TAKAMIYA 		prefered_lft = 0x7FFFFFFF/HZ;
3071081bba5bSNoriaki TAKAMIYA 
3072081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
30733b9f9a1cSNoriaki TAKAMIYA 	ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3074081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
3075081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
3076081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
3077081bba5bSNoriaki TAKAMIYA 
3078081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
3079081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
3080081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
3081081bba5bSNoriaki TAKAMIYA 
308246d48046SYOSHIFUJI Hideaki 	addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
308346d48046SYOSHIFUJI Hideaki 			      jiffies_to_clock_t(valid_lft * HZ), flags);
3084081bba5bSNoriaki TAKAMIYA 	addrconf_verify(0);
3085081bba5bSNoriaki TAKAMIYA 
3086081bba5bSNoriaki TAKAMIYA 	return 0;
3087081bba5bSNoriaki TAKAMIYA }
3088081bba5bSNoriaki TAKAMIYA 
3089081bba5bSNoriaki TAKAMIYA static int
30901da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
30911da177e4SLinus Torvalds {
3092b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
3093461d8837SThomas Graf 	struct ifaddrmsg *ifm;
3094461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
30951da177e4SLinus Torvalds 	struct in6_addr *pfx;
30967198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
30977198f8ceSThomas Graf 	struct net_device *dev;
309855ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
309955ebaef1SNoriaki TAKAMIYA 	u8 ifa_flags;
3100461d8837SThomas Graf 	int err;
31011da177e4SLinus Torvalds 
3102b854272bSDenis V. Lunev 	if (net != &init_net)
3103b854272bSDenis V. Lunev 		return -EINVAL;
3104b854272bSDenis V. Lunev 
3105461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3106461d8837SThomas Graf 	if (err < 0)
3107461d8837SThomas Graf 		return err;
3108461d8837SThomas Graf 
3109461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
3110461d8837SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
31111da177e4SLinus Torvalds 	if (pfx == NULL)
31121da177e4SLinus Torvalds 		return -EINVAL;
31131da177e4SLinus Torvalds 
3114461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
31150778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
3116461d8837SThomas Graf 
3117461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
31180778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
3119461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
3120461d8837SThomas Graf 	} else {
3121461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
3122461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
31230778769dSNoriaki TAKAMIYA 	}
31240778769dSNoriaki TAKAMIYA 
3125881d966bSEric W. Biederman 	dev =  __dev_get_by_index(&init_net, ifm->ifa_index);
31267198f8ceSThomas Graf 	if (dev == NULL)
31277198f8ceSThomas Graf 		return -ENODEV;
3128081bba5bSNoriaki TAKAMIYA 
312955ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
31303b9f9a1cSNoriaki TAKAMIYA 	ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
313155ebaef1SNoriaki TAKAMIYA 
31327198f8ceSThomas Graf 	ifa = ipv6_get_ifaddr(pfx, dev, 1);
31337198f8ceSThomas Graf 	if (ifa == NULL) {
31347198f8ceSThomas Graf 		/*
31357198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
31367198f8ceSThomas Graf 		 * userspace alreay relies on not having to provide this.
31377198f8ceSThomas Graf 		 */
31380778769dSNoriaki TAKAMIYA 		return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
313955ebaef1SNoriaki TAKAMIYA 				      ifa_flags, preferred_lft, valid_lft);
31401da177e4SLinus Torvalds 	}
31411da177e4SLinus Torvalds 
31427198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
31437198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
31447198f8ceSThomas Graf 		err = -EEXIST;
31457198f8ceSThomas Graf 	else
314655ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
31477198f8ceSThomas Graf 
31487198f8ceSThomas Graf 	in6_ifa_put(ifa);
31497198f8ceSThomas Graf 
31507198f8ceSThomas Graf 	return err;
31517198f8ceSThomas Graf }
31527198f8ceSThomas Graf 
3153101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3154101bb229SThomas Graf 			  u8 scope, int ifindex)
3155101bb229SThomas Graf {
3156101bb229SThomas Graf 	struct ifaddrmsg *ifm;
3157101bb229SThomas Graf 
3158101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
3159101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
3160101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
3161101bb229SThomas Graf 	ifm->ifa_flags = flags;
3162101bb229SThomas Graf 	ifm->ifa_scope = scope;
3163101bb229SThomas Graf 	ifm->ifa_index = ifindex;
3164101bb229SThomas Graf }
3165101bb229SThomas Graf 
316685486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
316785486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
316885486af0SThomas Graf {
316985486af0SThomas Graf 	struct ifa_cacheinfo ci;
317085486af0SThomas Graf 
317185486af0SThomas Graf 	ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
317285486af0SThomas Graf 			+ TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
317385486af0SThomas Graf 	ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
317485486af0SThomas Graf 			+ TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
317585486af0SThomas Graf 	ci.ifa_prefered = preferred;
317685486af0SThomas Graf 	ci.ifa_valid = valid;
317785486af0SThomas Graf 
317885486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
317985486af0SThomas Graf }
318085486af0SThomas Graf 
3181101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
3182101bb229SThomas Graf {
3183101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
3184101bb229SThomas Graf 		return RT_SCOPE_HOST;
3185101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
3186101bb229SThomas Graf 		return RT_SCOPE_LINK;
3187101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
3188101bb229SThomas Graf 		return RT_SCOPE_SITE;
3189101bb229SThomas Graf 	else
3190101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
3191101bb229SThomas Graf }
3192101bb229SThomas Graf 
31930ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
31940ab6803bSThomas Graf {
3195339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3196339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
3197339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct ifa_cacheinfo));
31980ab6803bSThomas Graf }
3199c5396a31SYOSHIFUJI Hideaki 
32001da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3201b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
32021da177e4SLinus Torvalds {
32031da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
320485486af0SThomas Graf 	u32 preferred, valid;
32051da177e4SLinus Torvalds 
32060ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
32070ab6803bSThomas Graf 	if (nlh == NULL)
320826932566SPatrick McHardy 		return -EMSGSIZE;
32090ab6803bSThomas Graf 
3210101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3211101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
3212101bb229SThomas Graf 
32131da177e4SLinus Torvalds 	if (!(ifa->flags&IFA_F_PERMANENT)) {
321485486af0SThomas Graf 		preferred = ifa->prefered_lft;
321585486af0SThomas Graf 		valid = ifa->valid_lft;
321685486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
32171da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
321885486af0SThomas Graf 			preferred -= tval;
321985486af0SThomas Graf 			if (valid != INFINITY_LIFE_TIME)
322085486af0SThomas Graf 				valid -= tval;
32211da177e4SLinus Torvalds 		}
32221da177e4SLinus Torvalds 	} else {
322385486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
322485486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
32251da177e4SLinus Torvalds 	}
322685486af0SThomas Graf 
32270ab6803bSThomas Graf 	if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
322826932566SPatrick McHardy 	    put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
322926932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
323026932566SPatrick McHardy 		return -EMSGSIZE;
323126932566SPatrick McHardy 	}
323285486af0SThomas Graf 
32330ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
32341da177e4SLinus Torvalds }
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3237e431b8c0SJamal Hadi Salim 				u32 pid, u32 seq, int event, u16 flags)
32381da177e4SLinus Torvalds {
32391da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3240101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3241101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
32421da177e4SLinus Torvalds 
32431da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3244101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3245101bb229SThomas Graf 
32460ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
32470ab6803bSThomas Graf 	if (nlh == NULL)
324826932566SPatrick McHardy 		return -EMSGSIZE;
32490ab6803bSThomas Graf 
3250101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
32510ab6803bSThomas Graf 	if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
32520ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
325326932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
325426932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
325526932566SPatrick McHardy 		return -EMSGSIZE;
325626932566SPatrick McHardy 	}
325785486af0SThomas Graf 
32580ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
32591da177e4SLinus Torvalds }
32601da177e4SLinus Torvalds 
32611da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3262b6544c0bSJamal Hadi Salim 				u32 pid, u32 seq, int event, unsigned int flags)
32631da177e4SLinus Torvalds {
32641da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3265101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3266101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
32671da177e4SLinus Torvalds 
32681da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3269101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3270101bb229SThomas Graf 
32710ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
32720ab6803bSThomas Graf 	if (nlh == NULL)
327326932566SPatrick McHardy 		return -EMSGSIZE;
32740ab6803bSThomas Graf 
3275101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
32760ab6803bSThomas Graf 	if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
32770ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
327826932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
327926932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
328026932566SPatrick McHardy 		return -EMSGSIZE;
328126932566SPatrick McHardy 	}
328285486af0SThomas Graf 
32830ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
32841da177e4SLinus Torvalds }
32851da177e4SLinus Torvalds 
32861da177e4SLinus Torvalds enum addr_type_t
32871da177e4SLinus Torvalds {
32881da177e4SLinus Torvalds 	UNICAST_ADDR,
32891da177e4SLinus Torvalds 	MULTICAST_ADDR,
32901da177e4SLinus Torvalds 	ANYCAST_ADDR,
32911da177e4SLinus Torvalds };
32921da177e4SLinus Torvalds 
32931da177e4SLinus Torvalds static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
32941da177e4SLinus Torvalds 			   enum addr_type_t type)
32951da177e4SLinus Torvalds {
32961da177e4SLinus Torvalds 	int idx, ip_idx;
32971da177e4SLinus Torvalds 	int s_idx, s_ip_idx;
32981da177e4SLinus Torvalds 	int err = 1;
32991da177e4SLinus Torvalds 	struct net_device *dev;
33001da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
33011da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
33021da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
33031da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
33041da177e4SLinus Torvalds 
33051da177e4SLinus Torvalds 	s_idx = cb->args[0];
33061da177e4SLinus Torvalds 	s_ip_idx = ip_idx = cb->args[1];
33071da177e4SLinus Torvalds 
33087562f876SPavel Emelianov 	idx = 0;
3309881d966bSEric W. Biederman 	for_each_netdev(&init_net, dev) {
33101da177e4SLinus Torvalds 		if (idx < s_idx)
33117562f876SPavel Emelianov 			goto cont;
33121da177e4SLinus Torvalds 		if (idx > s_idx)
33131da177e4SLinus Torvalds 			s_ip_idx = 0;
33141da177e4SLinus Torvalds 		ip_idx = 0;
33151da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
33167562f876SPavel Emelianov 			goto cont;
33171da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
33181da177e4SLinus Torvalds 		switch (type) {
33191da177e4SLinus Torvalds 		case UNICAST_ADDR:
3320ae9cda5dSYOSHIFUJI Hideaki 			/* unicast address incl. temp addr */
33211da177e4SLinus Torvalds 			for (ifa = idev->addr_list; ifa;
33221da177e4SLinus Torvalds 			     ifa = ifa->if_next, ip_idx++) {
33231da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
33241da177e4SLinus Torvalds 					continue;
33251da177e4SLinus Torvalds 				if ((err = inet6_fill_ifaddr(skb, ifa,
33261da177e4SLinus Torvalds 				    NETLINK_CB(cb->skb).pid,
3327b6544c0bSJamal Hadi Salim 				    cb->nlh->nlmsg_seq, RTM_NEWADDR,
3328b6544c0bSJamal Hadi Salim 				    NLM_F_MULTI)) <= 0)
33291da177e4SLinus Torvalds 					goto done;
33301da177e4SLinus Torvalds 			}
33311da177e4SLinus Torvalds 			break;
33321da177e4SLinus Torvalds 		case MULTICAST_ADDR:
33331da177e4SLinus Torvalds 			/* multicast address */
33341da177e4SLinus Torvalds 			for (ifmca = idev->mc_list; ifmca;
33351da177e4SLinus Torvalds 			     ifmca = ifmca->next, ip_idx++) {
33361da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
33371da177e4SLinus Torvalds 					continue;
33381da177e4SLinus Torvalds 				if ((err = inet6_fill_ifmcaddr(skb, ifmca,
33391da177e4SLinus Torvalds 				    NETLINK_CB(cb->skb).pid,
3340b6544c0bSJamal Hadi Salim 				    cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3341b6544c0bSJamal Hadi Salim 				    NLM_F_MULTI)) <= 0)
33421da177e4SLinus Torvalds 					goto done;
33431da177e4SLinus Torvalds 			}
33441da177e4SLinus Torvalds 			break;
33451da177e4SLinus Torvalds 		case ANYCAST_ADDR:
33461da177e4SLinus Torvalds 			/* anycast address */
33471da177e4SLinus Torvalds 			for (ifaca = idev->ac_list; ifaca;
33481da177e4SLinus Torvalds 			     ifaca = ifaca->aca_next, ip_idx++) {
33491da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
33501da177e4SLinus Torvalds 					continue;
33511da177e4SLinus Torvalds 				if ((err = inet6_fill_ifacaddr(skb, ifaca,
33521da177e4SLinus Torvalds 				    NETLINK_CB(cb->skb).pid,
3353b6544c0bSJamal Hadi Salim 				    cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3354b6544c0bSJamal Hadi Salim 				    NLM_F_MULTI)) <= 0)
33551da177e4SLinus Torvalds 					goto done;
33561da177e4SLinus Torvalds 			}
33571da177e4SLinus Torvalds 			break;
33581da177e4SLinus Torvalds 		default:
33591da177e4SLinus Torvalds 			break;
33601da177e4SLinus Torvalds 		}
33611da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
33621da177e4SLinus Torvalds 		in6_dev_put(idev);
33637562f876SPavel Emelianov cont:
33647562f876SPavel Emelianov 		idx++;
33651da177e4SLinus Torvalds 	}
33661da177e4SLinus Torvalds done:
33671da177e4SLinus Torvalds 	if (err <= 0) {
33681da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
33691da177e4SLinus Torvalds 		in6_dev_put(idev);
33701da177e4SLinus Torvalds 	}
33711da177e4SLinus Torvalds 	cb->args[0] = idx;
33721da177e4SLinus Torvalds 	cb->args[1] = ip_idx;
33731da177e4SLinus Torvalds 	return skb->len;
33741da177e4SLinus Torvalds }
33751da177e4SLinus Torvalds 
33761da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
33771da177e4SLinus Torvalds {
3378b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
33791da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
3380b854272bSDenis V. Lunev 
3381b854272bSDenis V. Lunev 	if (net != &init_net)
3382b854272bSDenis V. Lunev 		return 0;
3383b854272bSDenis V. Lunev 
33841da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
33851da177e4SLinus Torvalds }
33861da177e4SLinus Torvalds 
33871da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
33881da177e4SLinus Torvalds {
3389b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
33901da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
3391b854272bSDenis V. Lunev 
3392b854272bSDenis V. Lunev 	if (net != &init_net)
3393b854272bSDenis V. Lunev 		return 0;
3394b854272bSDenis V. Lunev 
33951da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
33961da177e4SLinus Torvalds }
33971da177e4SLinus Torvalds 
33981da177e4SLinus Torvalds 
33991da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
34001da177e4SLinus Torvalds {
3401b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
34021da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
3403b854272bSDenis V. Lunev 
3404b854272bSDenis V. Lunev 	if (net != &init_net)
3405b854272bSDenis V. Lunev 		return 0;
3406b854272bSDenis V. Lunev 
34071da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
34081da177e4SLinus Torvalds }
34091da177e4SLinus Torvalds 
34101b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
34111b29fc2cSThomas Graf 			     void *arg)
34126c223828SNoriaki TAKAMIYA {
3413b854272bSDenis V. Lunev 	struct net *net = in_skb->sk->sk_net;
34141b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
34151b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
34166c223828SNoriaki TAKAMIYA 	struct in6_addr *addr = NULL;
34176c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
34186c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
34196c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
34206c223828SNoriaki TAKAMIYA 	int err;
34216c223828SNoriaki TAKAMIYA 
3422b854272bSDenis V. Lunev 	if (net != &init_net)
3423b854272bSDenis V. Lunev 		return -EINVAL;
3424b854272bSDenis V. Lunev 
34251b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
34261b29fc2cSThomas Graf 	if (err < 0)
34271b29fc2cSThomas Graf 		goto errout;
34286c223828SNoriaki TAKAMIYA 
34291b29fc2cSThomas Graf 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
34301b29fc2cSThomas Graf 	if (addr == NULL) {
34311b29fc2cSThomas Graf 		err = -EINVAL;
34321b29fc2cSThomas Graf 		goto errout;
34331b29fc2cSThomas Graf 	}
34341b29fc2cSThomas Graf 
34351b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
34366c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
3437881d966bSEric W. Biederman 		dev = __dev_get_by_index(&init_net, ifm->ifa_index);
34386c223828SNoriaki TAKAMIYA 
34391b29fc2cSThomas Graf 	if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
34401b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
34411b29fc2cSThomas Graf 		goto errout;
34426c223828SNoriaki TAKAMIYA 	}
34436c223828SNoriaki TAKAMIYA 
34440ab6803bSThomas Graf 	if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
34451b29fc2cSThomas Graf 		err = -ENOBUFS;
34461b29fc2cSThomas Graf 		goto errout_ifa;
34471b29fc2cSThomas Graf 	}
34481b29fc2cSThomas Graf 
34496c223828SNoriaki TAKAMIYA 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
34506c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
345126932566SPatrick McHardy 	if (err < 0) {
345226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
345326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
345426932566SPatrick McHardy 		kfree_skb(skb);
345526932566SPatrick McHardy 		goto errout_ifa;
345626932566SPatrick McHardy 	}
345797c53cacSDenis V. Lunev 	err = rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
34581b29fc2cSThomas Graf errout_ifa:
34596c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
34601b29fc2cSThomas Graf errout:
34616c223828SNoriaki TAKAMIYA 	return err;
34626c223828SNoriaki TAKAMIYA }
34636c223828SNoriaki TAKAMIYA 
34641da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
34651da177e4SLinus Torvalds {
34661da177e4SLinus Torvalds 	struct sk_buff *skb;
34675d620266SThomas Graf 	int err = -ENOBUFS;
34681da177e4SLinus Torvalds 
34690ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
34705d620266SThomas Graf 	if (skb == NULL)
34715d620266SThomas Graf 		goto errout;
34725d620266SThomas Graf 
34735d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
347426932566SPatrick McHardy 	if (err < 0) {
347526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
347626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
347726932566SPatrick McHardy 		kfree_skb(skb);
347826932566SPatrick McHardy 		goto errout;
347926932566SPatrick McHardy 	}
348097c53cacSDenis V. Lunev 	err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
34815d620266SThomas Graf errout:
34825d620266SThomas Graf 	if (err < 0)
348397c53cacSDenis V. Lunev 		rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
34841da177e4SLinus Torvalds }
34851da177e4SLinus Torvalds 
3486b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
34871da177e4SLinus Torvalds 				__s32 *array, int bytes)
34881da177e4SLinus Torvalds {
348904561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
349004561c1fSThomas Graf 
34911da177e4SLinus Torvalds 	memset(array, 0, bytes);
34921da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
34931da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
34941da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
34951da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
34961da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
34971da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
34981da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
34991da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
35001da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
35011da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
35021da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
35031da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
35041da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
35051da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
35061da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
35071da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
35081da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
35091da177e4SLinus Torvalds #endif
35101da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
351165f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3512c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3513930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
3514930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
351552e16356SYOSHIFUJI Hideaki 	array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
3516fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
351709c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
351809c884d4SYOSHIFUJI Hideaki #endif
3519930d6ff2SYOSHIFUJI Hideaki #endif
3520fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
35210bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
352295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
352395c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
352495c385b4SNeil Horman #endif
35251da177e4SLinus Torvalds }
35261da177e4SLinus Torvalds 
3527339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
3528339bf98fSThomas Graf {
3529339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3530339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3531339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3532339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
3533339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
3534339bf98fSThomas Graf 	       + nla_total_size( /* IFLA_PROTINFO */
3535339bf98fSThomas Graf 			nla_total_size(4) /* IFLA_INET6_FLAGS */
3536339bf98fSThomas Graf 			+ nla_total_size(sizeof(struct ifla_cacheinfo))
3537339bf98fSThomas Graf 			+ nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3538bf99f1bdSYOSHIFUJI Hideaki 			+ nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3539bf99f1bdSYOSHIFUJI Hideaki 			+ nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
3540339bf98fSThomas Graf 		 );
3541339bf98fSThomas Graf }
3542c5396a31SYOSHIFUJI Hideaki 
35437f7d9a6bSHerbert Xu static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
35447f7d9a6bSHerbert Xu 				      int bytes)
35457f7d9a6bSHerbert Xu {
35467f7d9a6bSHerbert Xu 	int i;
35477f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
35487f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
35497f7d9a6bSHerbert Xu 
35507f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
35517f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
35527f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
35537f7d9a6bSHerbert Xu 		put_unaligned(snmp_fold_field(mib, i), &stats[i]);
35547f7d9a6bSHerbert Xu 
35557f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
35567f7d9a6bSHerbert Xu }
35577f7d9a6bSHerbert Xu 
35587f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
35597f7d9a6bSHerbert Xu 			     int bytes)
35607f7d9a6bSHerbert Xu {
35617f7d9a6bSHerbert Xu 	switch(attrtype) {
35627f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
35637f7d9a6bSHerbert Xu 		__snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
35647f7d9a6bSHerbert Xu 		break;
35657f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
35667f7d9a6bSHerbert Xu 		__snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
35677f7d9a6bSHerbert Xu 		break;
35687f7d9a6bSHerbert Xu 	}
35697f7d9a6bSHerbert Xu }
35707f7d9a6bSHerbert Xu 
35711da177e4SLinus Torvalds static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3572b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
35731da177e4SLinus Torvalds {
35741da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3575bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
357604561c1fSThomas Graf 	struct ifinfomsg *hdr;
35771da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
357804561c1fSThomas Graf 	void *protoinfo;
35791da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
35801da177e4SLinus Torvalds 
358104561c1fSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
358204561c1fSThomas Graf 	if (nlh == NULL)
358326932566SPatrick McHardy 		return -EMSGSIZE;
35841da177e4SLinus Torvalds 
358504561c1fSThomas Graf 	hdr = nlmsg_data(nlh);
358604561c1fSThomas Graf 	hdr->ifi_family = AF_INET6;
358704561c1fSThomas Graf 	hdr->__ifi_pad = 0;
358804561c1fSThomas Graf 	hdr->ifi_type = dev->type;
358904561c1fSThomas Graf 	hdr->ifi_index = dev->ifindex;
359004561c1fSThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
359104561c1fSThomas Graf 	hdr->ifi_change = 0;
359204561c1fSThomas Graf 
359304561c1fSThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
35941da177e4SLinus Torvalds 
35951da177e4SLinus Torvalds 	if (dev->addr_len)
359604561c1fSThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
35971da177e4SLinus Torvalds 
359804561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
35991da177e4SLinus Torvalds 	if (dev->ifindex != dev->iflink)
360004561c1fSThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
36011da177e4SLinus Torvalds 
360204561c1fSThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
360304561c1fSThomas Graf 	if (protoinfo == NULL)
360404561c1fSThomas Graf 		goto nla_put_failure;
36051da177e4SLinus Torvalds 
360604561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
36071da177e4SLinus Torvalds 
36081da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
36091da177e4SLinus Torvalds 	ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
36101da177e4SLinus Torvalds 		    + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
36111da177e4SLinus Torvalds 	ci.reachable_time = idev->nd_parms->reachable_time;
36121da177e4SLinus Torvalds 	ci.retrans_time = idev->nd_parms->retrans_time;
361304561c1fSThomas Graf 	NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
36141da177e4SLinus Torvalds 
3615bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3616bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
361704561c1fSThomas Graf 		goto nla_put_failure;
3618bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
36191da177e4SLinus Torvalds 
3620bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
3621bf99f1bdSYOSHIFUJI Hideaki 
3622bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3623bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
3624bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
3625bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3626bf99f1bdSYOSHIFUJI Hideaki 
3627bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3628bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
3629bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
3630bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
36311da177e4SLinus Torvalds 
363204561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
363304561c1fSThomas Graf 	return nlmsg_end(skb, nlh);
36341da177e4SLinus Torvalds 
363504561c1fSThomas Graf nla_put_failure:
363626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
363726932566SPatrick McHardy 	return -EMSGSIZE;
36381da177e4SLinus Torvalds }
36391da177e4SLinus Torvalds 
36401da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
36411da177e4SLinus Torvalds {
3642b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
36431da177e4SLinus Torvalds 	int idx, err;
36441da177e4SLinus Torvalds 	int s_idx = cb->args[0];
36451da177e4SLinus Torvalds 	struct net_device *dev;
36461da177e4SLinus Torvalds 	struct inet6_dev *idev;
36471da177e4SLinus Torvalds 
3648b854272bSDenis V. Lunev 	if (net != &init_net)
3649b854272bSDenis V. Lunev 		return 0;
3650b854272bSDenis V. Lunev 
36511da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
36527562f876SPavel Emelianov 	idx = 0;
3653881d966bSEric W. Biederman 	for_each_netdev(&init_net, dev) {
36541da177e4SLinus Torvalds 		if (idx < s_idx)
36557562f876SPavel Emelianov 			goto cont;
36561da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
36577562f876SPavel Emelianov 			goto cont;
36581da177e4SLinus Torvalds 		err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3659b6544c0bSJamal Hadi Salim 				cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
36601da177e4SLinus Torvalds 		in6_dev_put(idev);
36611da177e4SLinus Torvalds 		if (err <= 0)
36621da177e4SLinus Torvalds 			break;
36637562f876SPavel Emelianov cont:
36647562f876SPavel Emelianov 		idx++;
36651da177e4SLinus Torvalds 	}
36661da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
36671da177e4SLinus Torvalds 	cb->args[0] = idx;
36681da177e4SLinus Torvalds 
36691da177e4SLinus Torvalds 	return skb->len;
36701da177e4SLinus Torvalds }
36711da177e4SLinus Torvalds 
36721da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
36731da177e4SLinus Torvalds {
36741da177e4SLinus Torvalds 	struct sk_buff *skb;
36758d7a76c9SThomas Graf 	int err = -ENOBUFS;
36761da177e4SLinus Torvalds 
3677339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
36788d7a76c9SThomas Graf 	if (skb == NULL)
36798d7a76c9SThomas Graf 		goto errout;
36808d7a76c9SThomas Graf 
36818d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
368226932566SPatrick McHardy 	if (err < 0) {
368326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
368426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
368526932566SPatrick McHardy 		kfree_skb(skb);
368626932566SPatrick McHardy 		goto errout;
368726932566SPatrick McHardy 	}
368897c53cacSDenis V. Lunev 	err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
36898d7a76c9SThomas Graf errout:
36908d7a76c9SThomas Graf 	if (err < 0)
369197c53cacSDenis V. Lunev 		rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
36921da177e4SLinus Torvalds }
36931da177e4SLinus Torvalds 
3694339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
3695339bf98fSThomas Graf {
3696339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
3697339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
3698339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
3699339bf98fSThomas Graf }
3700c5396a31SYOSHIFUJI Hideaki 
37011da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3702b6544c0bSJamal Hadi Salim 			     struct prefix_info *pinfo, u32 pid, u32 seq,
3703b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
37041da177e4SLinus Torvalds {
37051da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
37061da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
37071da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
37081da177e4SLinus Torvalds 
37096051e2f4SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
37106051e2f4SThomas Graf 	if (nlh == NULL)
371126932566SPatrick McHardy 		return -EMSGSIZE;
37126051e2f4SThomas Graf 
37136051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
37141da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
37158a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
37168a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
37171da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
37181da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
37191da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
37208a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
37211da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
37221da177e4SLinus Torvalds 	if (pinfo->onlink)
37231da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
37241da177e4SLinus Torvalds 	if (pinfo->autoconf)
37251da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
37261da177e4SLinus Torvalds 
37276051e2f4SThomas Graf 	NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
37281da177e4SLinus Torvalds 
37291da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
37301da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
37316051e2f4SThomas Graf 	NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
37321da177e4SLinus Torvalds 
37336051e2f4SThomas Graf 	return nlmsg_end(skb, nlh);
37341da177e4SLinus Torvalds 
37356051e2f4SThomas Graf nla_put_failure:
373626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
373726932566SPatrick McHardy 	return -EMSGSIZE;
37381da177e4SLinus Torvalds }
37391da177e4SLinus Torvalds 
37401da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
37411da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
37421da177e4SLinus Torvalds {
37431da177e4SLinus Torvalds 	struct sk_buff *skb;
37448c384bfaSThomas Graf 	int err = -ENOBUFS;
37451da177e4SLinus Torvalds 
3746339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
37478c384bfaSThomas Graf 	if (skb == NULL)
37488c384bfaSThomas Graf 		goto errout;
37498c384bfaSThomas Graf 
37508c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
375126932566SPatrick McHardy 	if (err < 0) {
375226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
375326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
375426932566SPatrick McHardy 		kfree_skb(skb);
375526932566SPatrick McHardy 		goto errout;
375626932566SPatrick McHardy 	}
375797c53cacSDenis V. Lunev 	err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
37588c384bfaSThomas Graf errout:
37598c384bfaSThomas Graf 	if (err < 0)
376097c53cacSDenis V. Lunev 		rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_PREFIX, err);
37611da177e4SLinus Torvalds }
37621da177e4SLinus Torvalds 
37631da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
37641da177e4SLinus Torvalds {
37651da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
37661da177e4SLinus Torvalds 
37671da177e4SLinus Torvalds 	switch (event) {
37681da177e4SLinus Torvalds 	case RTM_NEWADDR:
376995c385b4SNeil Horman 		/*
377095c385b4SNeil Horman 		 * If the address was optimistic
377195c385b4SNeil Horman 		 * we inserted the route at the start of
377295c385b4SNeil Horman 		 * our DAD process, so we don't need
377395c385b4SNeil Horman 		 * to do it again
377495c385b4SNeil Horman 		 */
377595c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
377640e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
37771da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
37781da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
37791da177e4SLinus Torvalds 		break;
37801da177e4SLinus Torvalds 	case RTM_DELADDR:
37811da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
37821da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
37831da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
37841da177e4SLinus Torvalds 		dst_hold(&ifp->rt->u.dst);
3785e0a1ad73SThomas Graf 		if (ip6_del_rt(ifp->rt))
37861da177e4SLinus Torvalds 			dst_free(&ifp->rt->u.dst);
37871da177e4SLinus Torvalds 		break;
37881da177e4SLinus Torvalds 	}
37891da177e4SLinus Torvalds }
37901da177e4SLinus Torvalds 
37911da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
37921da177e4SLinus Torvalds {
37938814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
37941da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
37951da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
37968814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
37971da177e4SLinus Torvalds }
37981da177e4SLinus Torvalds 
37991da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
38001da177e4SLinus Torvalds 
38011da177e4SLinus Torvalds static
38021da177e4SLinus Torvalds int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
38031da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
38041da177e4SLinus Torvalds {
38051da177e4SLinus Torvalds 	int *valp = ctl->data;
38061da177e4SLinus Torvalds 	int val = *valp;
38071da177e4SLinus Torvalds 	int ret;
38081da177e4SLinus Torvalds 
38091da177e4SLinus Torvalds 	ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
38101da177e4SLinus Torvalds 
3811c8fecf22SPavel Emelyanov 	if (write)
3812c8fecf22SPavel Emelyanov 		addrconf_fixup_forwarding(ctl, valp, val);
38131da177e4SLinus Torvalds 	return ret;
38141da177e4SLinus Torvalds }
38151da177e4SLinus Torvalds 
38161da177e4SLinus Torvalds static int addrconf_sysctl_forward_strategy(ctl_table *table,
38171da177e4SLinus Torvalds 					    int __user *name, int nlen,
38181da177e4SLinus Torvalds 					    void __user *oldval,
38191da177e4SLinus Torvalds 					    size_t __user *oldlenp,
38201f29bcd7SAlexey Dobriyan 					    void __user *newval, size_t newlen)
38211da177e4SLinus Torvalds {
38221da177e4SLinus Torvalds 	int *valp = table->data;
3823c8fecf22SPavel Emelyanov 	int val = *valp;
38241da177e4SLinus Torvalds 	int new;
38251da177e4SLinus Torvalds 
38261da177e4SLinus Torvalds 	if (!newval || !newlen)
38271da177e4SLinus Torvalds 		return 0;
38281da177e4SLinus Torvalds 	if (newlen != sizeof(int))
38291da177e4SLinus Torvalds 		return -EINVAL;
38301da177e4SLinus Torvalds 	if (get_user(new, (int __user *)newval))
38311da177e4SLinus Torvalds 		return -EFAULT;
38321da177e4SLinus Torvalds 	if (new == *valp)
38331da177e4SLinus Torvalds 		return 0;
38341da177e4SLinus Torvalds 	if (oldval && oldlenp) {
38351da177e4SLinus Torvalds 		size_t len;
38361da177e4SLinus Torvalds 		if (get_user(len, oldlenp))
38371da177e4SLinus Torvalds 			return -EFAULT;
38381da177e4SLinus Torvalds 		if (len) {
38391da177e4SLinus Torvalds 			if (len > table->maxlen)
38401da177e4SLinus Torvalds 				len = table->maxlen;
38411da177e4SLinus Torvalds 			if (copy_to_user(oldval, valp, len))
38421da177e4SLinus Torvalds 				return -EFAULT;
38431da177e4SLinus Torvalds 			if (put_user(len, oldlenp))
38441da177e4SLinus Torvalds 				return -EFAULT;
38451da177e4SLinus Torvalds 		}
38461da177e4SLinus Torvalds 	}
38471da177e4SLinus Torvalds 
38481da177e4SLinus Torvalds 	*valp = new;
3849c8fecf22SPavel Emelyanov 	addrconf_fixup_forwarding(table, valp, val);
38501da177e4SLinus Torvalds 	return 1;
38511da177e4SLinus Torvalds }
38521da177e4SLinus Torvalds 
38531da177e4SLinus Torvalds static struct addrconf_sysctl_table
38541da177e4SLinus Torvalds {
38551da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
38561da177e4SLinus Torvalds 	ctl_table addrconf_vars[__NET_IPV6_MAX];
38571dab6222SPavel Emelyanov 	char *dev_name;
3858ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
38591da177e4SLinus Torvalds 	.sysctl_header = NULL,
38601da177e4SLinus Torvalds 	.addrconf_vars = {
38611da177e4SLinus Torvalds 		{
38621da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORWARDING,
38631da177e4SLinus Torvalds 			.procname	=	"forwarding",
38641da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.forwarding,
38651da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
38661da177e4SLinus Torvalds 			.mode		=	0644,
38671da177e4SLinus Torvalds 			.proc_handler	=	&addrconf_sysctl_forward,
38681da177e4SLinus Torvalds 			.strategy	=	&addrconf_sysctl_forward_strategy,
38691da177e4SLinus Torvalds 		},
38701da177e4SLinus Torvalds 		{
38711da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_HOP_LIMIT,
38721da177e4SLinus Torvalds 			.procname	=	"hop_limit",
38731da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.hop_limit,
38741da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
38751da177e4SLinus Torvalds 			.mode		=	0644,
38761da177e4SLinus Torvalds 			.proc_handler	=	proc_dointvec,
38771da177e4SLinus Torvalds 		},
38781da177e4SLinus Torvalds 		{
38791da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MTU,
38801da177e4SLinus Torvalds 			.procname	=	"mtu",
38811da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.mtu6,
38821da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
38831da177e4SLinus Torvalds 			.mode		=	0644,
38841da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
38851da177e4SLinus Torvalds 		},
38861da177e4SLinus Torvalds 		{
38871da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_RA,
38881da177e4SLinus Torvalds 			.procname	=	"accept_ra",
38891da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.accept_ra,
38901da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
38911da177e4SLinus Torvalds 			.mode		=	0644,
38921da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
38931da177e4SLinus Torvalds 		},
38941da177e4SLinus Torvalds 		{
38951da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_REDIRECTS,
38961da177e4SLinus Torvalds 			.procname	=	"accept_redirects",
38971da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.accept_redirects,
38981da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
38991da177e4SLinus Torvalds 			.mode		=	0644,
39001da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39011da177e4SLinus Torvalds 		},
39021da177e4SLinus Torvalds 		{
39031da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_AUTOCONF,
39041da177e4SLinus Torvalds 			.procname	=	"autoconf",
39051da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.autoconf,
39061da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39071da177e4SLinus Torvalds 			.mode		=	0644,
39081da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39091da177e4SLinus Torvalds 		},
39101da177e4SLinus Torvalds 		{
39111da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_DAD_TRANSMITS,
39121da177e4SLinus Torvalds 			.procname	=	"dad_transmits",
39131da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.dad_transmits,
39141da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39151da177e4SLinus Torvalds 			.mode		=	0644,
39161da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39171da177e4SLinus Torvalds 		},
39181da177e4SLinus Torvalds 		{
39191da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICITS,
39201da177e4SLinus Torvalds 			.procname	=	"router_solicitations",
39211da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicits,
39221da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39231da177e4SLinus Torvalds 			.mode		=	0644,
39241da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39251da177e4SLinus Torvalds 		},
39261da177e4SLinus Torvalds 		{
39271da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_INTERVAL,
39281da177e4SLinus Torvalds 			.procname	=	"router_solicitation_interval",
39291da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicit_interval,
39301da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39311da177e4SLinus Torvalds 			.mode		=	0644,
39321da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec_jiffies,
39331da177e4SLinus Torvalds 			.strategy	=	&sysctl_jiffies,
39341da177e4SLinus Torvalds 		},
39351da177e4SLinus Torvalds 		{
39361da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_DELAY,
39371da177e4SLinus Torvalds 			.procname	=	"router_solicitation_delay",
39381da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicit_delay,
39391da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39401da177e4SLinus Torvalds 			.mode		=	0644,
39411da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec_jiffies,
39421da177e4SLinus Torvalds 			.strategy	=	&sysctl_jiffies,
39431da177e4SLinus Torvalds 		},
39441da177e4SLinus Torvalds 		{
39451da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORCE_MLD_VERSION,
39461da177e4SLinus Torvalds 			.procname	=	"force_mld_version",
39471da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.force_mld_version,
39481da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39491da177e4SLinus Torvalds 			.mode		=	0644,
39501da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39511da177e4SLinus Torvalds 		},
39521da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
39531da177e4SLinus Torvalds 		{
39541da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_USE_TEMPADDR,
39551da177e4SLinus Torvalds 			.procname	=	"use_tempaddr",
39561da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.use_tempaddr,
39571da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39581da177e4SLinus Torvalds 			.mode		=	0644,
39591da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39601da177e4SLinus Torvalds 		},
39611da177e4SLinus Torvalds 		{
39621da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_VALID_LFT,
39631da177e4SLinus Torvalds 			.procname	=	"temp_valid_lft",
39641da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.temp_valid_lft,
39651da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39661da177e4SLinus Torvalds 			.mode		=	0644,
39671da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39681da177e4SLinus Torvalds 		},
39691da177e4SLinus Torvalds 		{
39701da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_PREFERED_LFT,
39711da177e4SLinus Torvalds 			.procname	=	"temp_prefered_lft",
39721da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.temp_prefered_lft,
39731da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39741da177e4SLinus Torvalds 			.mode		=	0644,
39751da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39761da177e4SLinus Torvalds 		},
39771da177e4SLinus Torvalds 		{
39781da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_REGEN_MAX_RETRY,
39791da177e4SLinus Torvalds 			.procname	=	"regen_max_retry",
39801da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.regen_max_retry,
39811da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39821da177e4SLinus Torvalds 			.mode		=	0644,
39831da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39841da177e4SLinus Torvalds 		},
39851da177e4SLinus Torvalds 		{
39861da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_DESYNC_FACTOR,
39871da177e4SLinus Torvalds 			.procname	=	"max_desync_factor",
39881da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_desync_factor,
39891da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39901da177e4SLinus Torvalds 			.mode		=	0644,
39911da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39921da177e4SLinus Torvalds 		},
39931da177e4SLinus Torvalds #endif
39941da177e4SLinus Torvalds 		{
39951da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_ADDRESSES,
39961da177e4SLinus Torvalds 			.procname	=	"max_addresses",
39971da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_addresses,
39981da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39991da177e4SLinus Torvalds 			.mode		=	0644,
40001da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40011da177e4SLinus Torvalds 		},
40021da177e4SLinus Torvalds 		{
400365f5c7c1SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_DEFRTR,
400465f5c7c1SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_defrtr",
400565f5c7c1SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_defrtr,
400665f5c7c1SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
400765f5c7c1SYOSHIFUJI Hideaki 			.mode		=	0644,
400865f5c7c1SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
400965f5c7c1SYOSHIFUJI Hideaki 		},
401065f5c7c1SYOSHIFUJI Hideaki 		{
4011c4fd30ebSYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_PINFO,
4012c4fd30ebSYOSHIFUJI Hideaki 			.procname	=	"accept_ra_pinfo",
4013c4fd30ebSYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_pinfo,
4014c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4015c4fd30ebSYOSHIFUJI Hideaki 			.mode		=	0644,
4016c4fd30ebSYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4017c4fd30ebSYOSHIFUJI Hideaki 		},
4018930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4019930d6ff2SYOSHIFUJI Hideaki 		{
4020930d6ff2SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RTR_PREF,
4021930d6ff2SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_rtr_pref",
4022930d6ff2SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_rtr_pref,
4023930d6ff2SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4024930d6ff2SYOSHIFUJI Hideaki 			.mode		=	0644,
4025930d6ff2SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4026930d6ff2SYOSHIFUJI Hideaki 		},
402752e16356SYOSHIFUJI Hideaki 		{
402852e16356SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_RTR_PROBE_INTERVAL,
402952e16356SYOSHIFUJI Hideaki 			.procname	=	"router_probe_interval",
403052e16356SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.rtr_probe_interval,
403152e16356SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
403252e16356SYOSHIFUJI Hideaki 			.mode		=	0644,
403352e16356SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec_jiffies,
403452e16356SYOSHIFUJI Hideaki 			.strategy	=	&sysctl_jiffies,
403552e16356SYOSHIFUJI Hideaki 		},
4036fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
403709c884d4SYOSHIFUJI Hideaki 		{
403809c884d4SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
403909c884d4SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_rt_info_max_plen",
404009c884d4SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_rt_info_max_plen,
404109c884d4SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
404209c884d4SYOSHIFUJI Hideaki 			.mode		=	0644,
404309c884d4SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
404409c884d4SYOSHIFUJI Hideaki 		},
404509c884d4SYOSHIFUJI Hideaki #endif
4046930d6ff2SYOSHIFUJI Hideaki #endif
4047c4fd30ebSYOSHIFUJI Hideaki 		{
4048fbea49e1SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_PROXY_NDP,
4049fbea49e1SYOSHIFUJI Hideaki 			.procname	=	"proxy_ndp",
4050fbea49e1SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.proxy_ndp,
4051fbea49e1SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4052fbea49e1SYOSHIFUJI Hideaki 			.mode		=	0644,
4053fbea49e1SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4054fbea49e1SYOSHIFUJI Hideaki 		},
4055fbea49e1SYOSHIFUJI Hideaki 		{
40560bcbc926SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_SOURCE_ROUTE,
40570bcbc926SYOSHIFUJI Hideaki 			.procname	=	"accept_source_route",
40580bcbc926SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_source_route,
40590bcbc926SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
40600bcbc926SYOSHIFUJI Hideaki 			.mode		=	0644,
40610bcbc926SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
40620bcbc926SYOSHIFUJI Hideaki 		},
406395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
406495c385b4SNeil Horman 		{
406595c385b4SNeil Horman 			.ctl_name	=	CTL_UNNUMBERED,
406695c385b4SNeil Horman 			.procname       =       "optimistic_dad",
406795c385b4SNeil Horman 			.data           =       &ipv6_devconf.optimistic_dad,
406895c385b4SNeil Horman 			.maxlen         =       sizeof(int),
406995c385b4SNeil Horman 			.mode           =       0644,
407095c385b4SNeil Horman 			.proc_handler   =       &proc_dointvec,
407195c385b4SNeil Horman 
407295c385b4SNeil Horman 		},
407395c385b4SNeil Horman #endif
40740bcbc926SYOSHIFUJI Hideaki 		{
40751da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
40761da177e4SLinus Torvalds 		}
40771da177e4SLinus Torvalds 	},
40781da177e4SLinus Torvalds };
40791da177e4SLinus Torvalds 
4080bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4081bff16c2fSPavel Emelyanov 		int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p)
40821da177e4SLinus Torvalds {
40831da177e4SLinus Torvalds 	int i;
40841da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
40851da177e4SLinus Torvalds 
40861dab6222SPavel Emelyanov #define ADDRCONF_CTL_PATH_DEV	3
40871dab6222SPavel Emelyanov 
40881dab6222SPavel Emelyanov 	struct ctl_path addrconf_ctl_path[] = {
40891dab6222SPavel Emelyanov 		{ .procname = "net", .ctl_name = CTL_NET, },
40901dab6222SPavel Emelyanov 		{ .procname = "ipv6", .ctl_name = NET_IPV6, },
40911dab6222SPavel Emelyanov 		{ .procname = "conf", .ctl_name = NET_IPV6_CONF, },
40921dab6222SPavel Emelyanov 		{ /* to be set */ },
40931dab6222SPavel Emelyanov 		{ },
40941dab6222SPavel Emelyanov 	};
40951dab6222SPavel Emelyanov 
40961dab6222SPavel Emelyanov 
4097af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
40981da177e4SLinus Torvalds 	if (t == NULL)
4099f68635e6SPavel Emelyanov 		goto out;
4100f68635e6SPavel Emelyanov 
41011da177e4SLinus Torvalds 	for (i=0; t->addrconf_vars[i].data; i++) {
41021da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
41031da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4104bff16c2fSPavel Emelyanov 		t->addrconf_vars[i].extra2 = net;
41051da177e4SLinus Torvalds 	}
41061da177e4SLinus Torvalds 
41071da177e4SLinus Torvalds 	/*
41081da177e4SLinus Torvalds 	 * Make a copy of dev_name, because '.procname' is regarded as const
41091da177e4SLinus Torvalds 	 * by sysctl and we wouldn't want anyone to change it under our feet
41101da177e4SLinus Torvalds 	 * (see SIOCSIFNAME).
41111da177e4SLinus Torvalds 	 */
41121dab6222SPavel Emelyanov 	t->dev_name = kstrdup(dev_name, GFP_KERNEL);
41131dab6222SPavel Emelyanov 	if (!t->dev_name)
41141da177e4SLinus Torvalds 		goto free;
41151da177e4SLinus Torvalds 
41161dab6222SPavel Emelyanov 	addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
41171dab6222SPavel Emelyanov 	addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
41181da177e4SLinus Torvalds 
4119bff16c2fSPavel Emelyanov 	t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
41201dab6222SPavel Emelyanov 			t->addrconf_vars);
41211da177e4SLinus Torvalds 	if (t->sysctl_header == NULL)
41221da177e4SLinus Torvalds 		goto free_procname;
4123f68635e6SPavel Emelyanov 
41241da177e4SLinus Torvalds 	p->sysctl = t;
412595897312SPavel Emelyanov 	return 0;
41261da177e4SLinus Torvalds 
41271da177e4SLinus Torvalds free_procname:
41281dab6222SPavel Emelyanov 	kfree(t->dev_name);
41291da177e4SLinus Torvalds free:
41301da177e4SLinus Torvalds 	kfree(t);
4131f68635e6SPavel Emelyanov out:
413295897312SPavel Emelyanov 	return -ENOBUFS;
41331da177e4SLinus Torvalds }
41341da177e4SLinus Torvalds 
4135408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4136f52295a9SPavel Emelyanov {
4137408c4768SPavel Emelyanov 	struct addrconf_sysctl_table *t;
4138f52295a9SPavel Emelyanov 
4139408c4768SPavel Emelyanov 	if (p->sysctl == NULL)
4140408c4768SPavel Emelyanov 		return;
4141408c4768SPavel Emelyanov 
4142408c4768SPavel Emelyanov 	t = p->sysctl;
41431da177e4SLinus Torvalds 	p->sysctl = NULL;
41441da177e4SLinus Torvalds 	unregister_sysctl_table(t->sysctl_header);
41451dab6222SPavel Emelyanov 	kfree(t->dev_name);
41461da177e4SLinus Torvalds 	kfree(t);
41471da177e4SLinus Torvalds }
4148408c4768SPavel Emelyanov 
4149408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev)
4150408c4768SPavel Emelyanov {
4151408c4768SPavel Emelyanov 	neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6,
4152408c4768SPavel Emelyanov 			      NET_IPV6_NEIGH, "ipv6",
4153408c4768SPavel Emelyanov 			      &ndisc_ifinfo_sysctl_change,
4154408c4768SPavel Emelyanov 			      NULL);
4155bff16c2fSPavel Emelyanov 	__addrconf_sysctl_register(idev->dev->nd_net, idev->dev->name,
4156bff16c2fSPavel Emelyanov 			idev->dev->ifindex, idev, &idev->cnf);
4157408c4768SPavel Emelyanov }
4158408c4768SPavel Emelyanov 
4159408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
4160408c4768SPavel Emelyanov {
4161408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
4162408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
41631da177e4SLinus Torvalds }
41641da177e4SLinus Torvalds 
41651da177e4SLinus Torvalds 
41661da177e4SLinus Torvalds #endif
41671da177e4SLinus Torvalds 
4168e0da5a48SPavel Emelyanov static int addrconf_init_net(struct net *net)
4169e0da5a48SPavel Emelyanov {
4170e0da5a48SPavel Emelyanov 	int err;
4171e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
4172e0da5a48SPavel Emelyanov 
4173e0da5a48SPavel Emelyanov 	err = -ENOMEM;
4174e0da5a48SPavel Emelyanov 	all = &ipv6_devconf;
4175e0da5a48SPavel Emelyanov 	dflt = &ipv6_devconf_dflt;
4176e0da5a48SPavel Emelyanov 
4177e0da5a48SPavel Emelyanov 	if (net != &init_net) {
4178e0da5a48SPavel Emelyanov 		all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
4179e0da5a48SPavel Emelyanov 		if (all == NULL)
4180e0da5a48SPavel Emelyanov 			goto err_alloc_all;
4181e0da5a48SPavel Emelyanov 
4182e0da5a48SPavel Emelyanov 		dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4183e0da5a48SPavel Emelyanov 		if (dflt == NULL)
4184e0da5a48SPavel Emelyanov 			goto err_alloc_dflt;
4185e0da5a48SPavel Emelyanov 	}
4186e0da5a48SPavel Emelyanov 
4187e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
4188e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
4189e0da5a48SPavel Emelyanov 
4190e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4191e0da5a48SPavel Emelyanov 	err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL,
4192e0da5a48SPavel Emelyanov 			NULL, all);
4193e0da5a48SPavel Emelyanov 	if (err < 0)
4194e0da5a48SPavel Emelyanov 		goto err_reg_all;
4195e0da5a48SPavel Emelyanov 
4196e0da5a48SPavel Emelyanov 	err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT,
4197e0da5a48SPavel Emelyanov 			NULL, dflt);
4198e0da5a48SPavel Emelyanov 	if (err < 0)
4199e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
4200e0da5a48SPavel Emelyanov #endif
4201e0da5a48SPavel Emelyanov 	return 0;
4202e0da5a48SPavel Emelyanov 
4203e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4204e0da5a48SPavel Emelyanov err_reg_dflt:
4205e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
4206e0da5a48SPavel Emelyanov err_reg_all:
4207e0da5a48SPavel Emelyanov 	kfree(dflt);
4208e0da5a48SPavel Emelyanov #endif
4209e0da5a48SPavel Emelyanov err_alloc_dflt:
4210e0da5a48SPavel Emelyanov 	kfree(all);
4211e0da5a48SPavel Emelyanov err_alloc_all:
4212e0da5a48SPavel Emelyanov 	return err;
4213e0da5a48SPavel Emelyanov }
4214e0da5a48SPavel Emelyanov 
4215e0da5a48SPavel Emelyanov static void addrconf_exit_net(struct net *net)
4216e0da5a48SPavel Emelyanov {
4217e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4218e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4219e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
4220e0da5a48SPavel Emelyanov #endif
4221e0da5a48SPavel Emelyanov 	if (net != &init_net) {
4222e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_dflt);
4223e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_all);
4224e0da5a48SPavel Emelyanov 	}
4225e0da5a48SPavel Emelyanov }
4226e0da5a48SPavel Emelyanov 
4227e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
4228e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
4229e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
4230e0da5a48SPavel Emelyanov };
4231e0da5a48SPavel Emelyanov 
42321da177e4SLinus Torvalds /*
42331da177e4SLinus Torvalds  *      Device notifier
42341da177e4SLinus Torvalds  */
42351da177e4SLinus Torvalds 
42361da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb)
42371da177e4SLinus Torvalds {
4238e041c683SAlan Stern 	return atomic_notifier_chain_register(&inet6addr_chain, nb);
42391da177e4SLinus Torvalds }
42401da177e4SLinus Torvalds 
42417159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier);
42427159039aSYOSHIFUJI Hideaki 
42431da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb)
42441da177e4SLinus Torvalds {
4245e041c683SAlan Stern 	return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
42461da177e4SLinus Torvalds }
42471da177e4SLinus Torvalds 
42487159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier);
42497159039aSYOSHIFUJI Hideaki 
42501da177e4SLinus Torvalds /*
42511da177e4SLinus Torvalds  *	Init / cleanup code
42521da177e4SLinus Torvalds  */
42531da177e4SLinus Torvalds 
42541da177e4SLinus Torvalds int __init addrconf_init(void)
42551da177e4SLinus Torvalds {
42562a8cc6c8SYOSHIFUJI Hideaki 	int err;
42572a8cc6c8SYOSHIFUJI Hideaki 
42582a8cc6c8SYOSHIFUJI Hideaki 	if ((err = ipv6_addr_label_init()) < 0) {
42592a8cc6c8SYOSHIFUJI Hideaki 		printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
42602a8cc6c8SYOSHIFUJI Hideaki 			err);
42612a8cc6c8SYOSHIFUJI Hideaki 		return err;
42622a8cc6c8SYOSHIFUJI Hideaki 	}
42631da177e4SLinus Torvalds 
4264e0da5a48SPavel Emelyanov 	register_pernet_subsys(&addrconf_ops);
4265e0da5a48SPavel Emelyanov 
42661da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
42671da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
42681da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
42691da177e4SLinus Torvalds 	 * to other devices which are up.
42701da177e4SLinus Torvalds 	 *
42711da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
42721da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
42731da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
42741da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
42751da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
42761da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
42771da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
42781da177e4SLinus Torvalds 	 *
42791da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
42801da177e4SLinus Torvalds 	 * loopback_dev first by hand.
42811da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
42821da177e4SLinus Torvalds 	 * device and it being up should be removed.
42831da177e4SLinus Torvalds 	 */
42841da177e4SLinus Torvalds 	rtnl_lock();
42852774c7abSEric W. Biederman 	if (!ipv6_add_dev(init_net.loopback_dev))
42861da177e4SLinus Torvalds 		err = -ENOMEM;
42871da177e4SLinus Torvalds 	rtnl_unlock();
42881da177e4SLinus Torvalds 	if (err)
4289e0da5a48SPavel Emelyanov 		goto errlo;
42901da177e4SLinus Torvalds 
42912774c7abSEric W. Biederman 	ip6_null_entry.u.dst.dev = init_net.loopback_dev;
42922774c7abSEric W. Biederman 	ip6_null_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
42939a6bf6feSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MULTIPLE_TABLES
42942774c7abSEric W. Biederman 	ip6_prohibit_entry.u.dst.dev = init_net.loopback_dev;
42952774c7abSEric W. Biederman 	ip6_prohibit_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
42962774c7abSEric W. Biederman 	ip6_blk_hole_entry.u.dst.dev = init_net.loopback_dev;
42972774c7abSEric W. Biederman 	ip6_blk_hole_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
42989a6bf6feSYOSHIFUJI Hideaki #endif
4299c62dba90SHerbert Xu 
43001da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
43011da177e4SLinus Torvalds 
43021da177e4SLinus Torvalds 	addrconf_verify(0);
4303c127ea2cSThomas Graf 
4304c127ea2cSThomas Graf 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4305c127ea2cSThomas Graf 	if (err < 0)
4306c127ea2cSThomas Graf 		goto errout;
4307c127ea2cSThomas Graf 
4308c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
4309c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4310c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4311c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4312c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4313c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4314c127ea2cSThomas Graf 
43152a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
43162a8cc6c8SYOSHIFUJI Hideaki 
43171da177e4SLinus Torvalds 	return 0;
4318c127ea2cSThomas Graf errout:
4319c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
4320e0da5a48SPavel Emelyanov errlo:
4321e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
4322c127ea2cSThomas Graf 
4323c127ea2cSThomas Graf 	return err;
43241da177e4SLinus Torvalds }
43251da177e4SLinus Torvalds 
432609f7709fSDaniel Lezcano void addrconf_cleanup(void)
43271da177e4SLinus Torvalds {
43281da177e4SLinus Torvalds 	struct net_device *dev;
43291da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
43301da177e4SLinus Torvalds 	int i;
43311da177e4SLinus Torvalds 
43321da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
43331da177e4SLinus Torvalds 
4334e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
43351da177e4SLinus Torvalds 
43361da177e4SLinus Torvalds 	rtnl_lock();
43371da177e4SLinus Torvalds 
43381da177e4SLinus Torvalds 	/*
43391da177e4SLinus Torvalds 	 *	clean dev list.
43401da177e4SLinus Torvalds 	 */
43411da177e4SLinus Torvalds 
4342881d966bSEric W. Biederman 	for_each_netdev(&init_net, dev) {
4343dffe4f04SMicah Gruber 		if (__in6_dev_get(dev) == NULL)
43441da177e4SLinus Torvalds 			continue;
43451da177e4SLinus Torvalds 		addrconf_ifdown(dev, 1);
43461da177e4SLinus Torvalds 	}
43472774c7abSEric W. Biederman 	addrconf_ifdown(init_net.loopback_dev, 2);
43481da177e4SLinus Torvalds 
43491da177e4SLinus Torvalds 	/*
43501da177e4SLinus Torvalds 	 *	Check hash table.
43511da177e4SLinus Torvalds 	 */
43521da177e4SLinus Torvalds 
43531da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
43541da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
43551da177e4SLinus Torvalds 		for (ifa=inet6_addr_lst[i]; ifa; ) {
43561da177e4SLinus Torvalds 			struct inet6_ifaddr *bifa;
43571da177e4SLinus Torvalds 
43581da177e4SLinus Torvalds 			bifa = ifa;
43591da177e4SLinus Torvalds 			ifa = ifa->lst_next;
43601da177e4SLinus Torvalds 			printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
43611da177e4SLinus Torvalds 			/* Do not free it; something is wrong.
43621da177e4SLinus Torvalds 			   Now we can investigate it with debugger.
43631da177e4SLinus Torvalds 			 */
43641da177e4SLinus Torvalds 		}
43651da177e4SLinus Torvalds 	}
43661da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
43671da177e4SLinus Torvalds 
43681da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
43691da177e4SLinus Torvalds 
43701da177e4SLinus Torvalds 	rtnl_unlock();
43711da177e4SLinus Torvalds }
4372