xref: /openbmc/linux/net/ipv6/addrconf.c (revision 45ba9dd2007da23da5ac21179451c3c9fee30a96)
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 
651da177e4SLinus Torvalds #include <net/sock.h>
661da177e4SLinus Torvalds #include <net/snmp.h>
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #include <net/ipv6.h>
691da177e4SLinus Torvalds #include <net/protocol.h>
701da177e4SLinus Torvalds #include <net/ndisc.h>
711da177e4SLinus Torvalds #include <net/ip6_route.h>
721da177e4SLinus Torvalds #include <net/addrconf.h>
731da177e4SLinus Torvalds #include <net/tcp.h>
741da177e4SLinus Torvalds #include <net/ip.h>
755d620266SThomas Graf #include <net/netlink.h>
761da177e4SLinus Torvalds #include <linux/if_tunnel.h>
771da177e4SLinus Torvalds #include <linux/rtnetlink.h>
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
801da177e4SLinus Torvalds #include <linux/random.h>
811da177e4SLinus Torvalds #endif
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds #include <asm/uaccess.h>
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds #include <linux/proc_fs.h>
861da177e4SLinus Torvalds #include <linux/seq_file.h>
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /* Set to 3 to get tracing... */
891da177e4SLinus Torvalds #define ACONF_DEBUG 2
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
921da177e4SLinus Torvalds #define ADBG(x) printk x
931da177e4SLinus Torvalds #else
941da177e4SLinus Torvalds #define ADBG(x)
951da177e4SLinus Torvalds #endif
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
981da177e4SLinus Torvalds #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1011da177e4SLinus Torvalds static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
1021da177e4SLinus Torvalds static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
1031da177e4SLinus Torvalds #endif
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1061da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev);
1071da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1081da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds static int desync_factor = MAX_DESYNC_FACTOR * HZ;
1111da177e4SLinus Torvalds #endif
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev);
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /*
1161da177e4SLinus Torvalds  *	Configured unicast address hash table
1171da177e4SLinus Torvalds  */
1181da177e4SLinus Torvalds static struct inet6_ifaddr		*inet6_addr_lst[IN6_ADDR_HSIZE];
1191da177e4SLinus Torvalds static DEFINE_RWLOCK(addrconf_hash_lock);
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds static void addrconf_verify(unsigned long);
1221da177e4SLinus Torvalds 
1238d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
1241da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1271da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1301da177e4SLinus Torvalds 
131e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
1321da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data);
1331da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
134c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1351da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1361da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1371da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1401da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1411da177e4SLinus Torvalds static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
1421da177e4SLinus Torvalds 
143e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
1441da177e4SLinus Torvalds 
145ab32ea5dSBrian Haley struct ipv6_devconf ipv6_devconf __read_mostly = {
1461da177e4SLinus Torvalds 	.forwarding		= 0,
1471da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1481da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1491da177e4SLinus Torvalds 	.accept_ra		= 1,
1501da177e4SLinus Torvalds 	.accept_redirects	= 1,
1511da177e4SLinus Torvalds 	.autoconf		= 1,
1521da177e4SLinus Torvalds 	.force_mld_version	= 0,
1531da177e4SLinus Torvalds 	.dad_transmits		= 1,
1541da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1551da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1561da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1571da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1581da177e4SLinus Torvalds 	.use_tempaddr 		= 0,
1591da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1601da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1611da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1621da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1631da177e4SLinus Torvalds #endif
1641da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
16565f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
166c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
167930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
168930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
16952e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
17009c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
17109c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
17209c884d4SYOSHIFUJI Hideaki #endif
173930d6ff2SYOSHIFUJI Hideaki #endif
174fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
1751da177e4SLinus Torvalds };
1761da177e4SLinus Torvalds 
177ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
1781da177e4SLinus Torvalds 	.forwarding		= 0,
1791da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1801da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1811da177e4SLinus Torvalds 	.accept_ra		= 1,
1821da177e4SLinus Torvalds 	.accept_redirects	= 1,
1831da177e4SLinus Torvalds 	.autoconf		= 1,
1841da177e4SLinus Torvalds 	.dad_transmits		= 1,
1851da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1861da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1871da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1881da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1891da177e4SLinus Torvalds 	.use_tempaddr		= 0,
1901da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1911da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1921da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1931da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1941da177e4SLinus Torvalds #endif
1951da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
19665f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
197c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
198930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
199930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
20052e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
20109c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
20209c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
20309c884d4SYOSHIFUJI Hideaki #endif
204930d6ff2SYOSHIFUJI Hideaki #endif
205fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2061da177e4SLinus Torvalds };
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
2091da177e4SLinus Torvalds #if 0
2101da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
2111da177e4SLinus Torvalds #endif
2121da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
2171da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds enum addrconf_timer_t
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	AC_NONE,
2231da177e4SLinus Torvalds 	AC_DAD,
2241da177e4SLinus Torvalds 	AC_RS,
2251da177e4SLinus Torvalds };
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
2281da177e4SLinus Torvalds 			       enum addrconf_timer_t what,
2291da177e4SLinus Torvalds 			       unsigned long when)
2301da177e4SLinus Torvalds {
2311da177e4SLinus Torvalds 	if (!del_timer(&ifp->timer))
2321da177e4SLinus Torvalds 		in6_ifa_hold(ifp);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	switch (what) {
2351da177e4SLinus Torvalds 	case AC_DAD:
2361da177e4SLinus Torvalds 		ifp->timer.function = addrconf_dad_timer;
2371da177e4SLinus Torvalds 		break;
2381da177e4SLinus Torvalds 	case AC_RS:
2391da177e4SLinus Torvalds 		ifp->timer.function = addrconf_rs_timer;
2401da177e4SLinus Torvalds 		break;
2411da177e4SLinus Torvalds 	default:;
2421da177e4SLinus Torvalds 	}
2431da177e4SLinus Torvalds 	ifp->timer.expires = jiffies + when;
2441da177e4SLinus Torvalds 	add_timer(&ifp->timer);
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */
2481da177e4SLinus Torvalds 
2498814c4b5SYOSHIFUJI Hideaki static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
2508814c4b5SYOSHIFUJI Hideaki {
2518814c4b5SYOSHIFUJI Hideaki 	struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
2528814c4b5SYOSHIFUJI Hideaki 	kfree(idev);
2538814c4b5SYOSHIFUJI Hideaki }
2548814c4b5SYOSHIFUJI Hideaki 
2551da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev)
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
2581da177e4SLinus Torvalds 	BUG_TRAP(idev->addr_list==NULL);
2591da177e4SLinus Torvalds 	BUG_TRAP(idev->mc_list==NULL);
2601da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
2611da177e4SLinus Torvalds 	printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
2621da177e4SLinus Torvalds #endif
2631da177e4SLinus Torvalds 	dev_put(dev);
2641da177e4SLinus Torvalds 	if (!idev->dead) {
2651da177e4SLinus Torvalds 		printk("Freeing alive inet6 device %p\n", idev);
2661da177e4SLinus Torvalds 		return;
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 	snmp6_free_dev(idev);
2698814c4b5SYOSHIFUJI Hideaki 	call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	struct inet6_dev *ndev;
275d88ae4ccSYOSHIFUJI Hideaki 	struct in6_addr maddr;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	ASSERT_RTNL();
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
2801da177e4SLinus Torvalds 		return NULL;
2811da177e4SLinus Torvalds 
282322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
2831da177e4SLinus Torvalds 
284322f74a4SIngo Oeser 	if (ndev == NULL)
285322f74a4SIngo Oeser 		return NULL;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
2881da177e4SLinus Torvalds 	ndev->dev = dev;
2891da177e4SLinus Torvalds 	memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
2901da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
2911da177e4SLinus Torvalds 	ndev->cnf.sysctl = NULL;
2921da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
2931da177e4SLinus Torvalds 	if (ndev->nd_parms == NULL) {
2941da177e4SLinus Torvalds 		kfree(ndev);
2951da177e4SLinus Torvalds 		return NULL;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 	/* We refer to the device */
2981da177e4SLinus Torvalds 	dev_hold(dev);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
3011da177e4SLinus Torvalds 		ADBG((KERN_WARNING
3021da177e4SLinus Torvalds 			"%s(): cannot allocate memory for statistics; dev=%s.\n",
3031da177e4SLinus Torvalds 			__FUNCTION__, dev->name));
3041da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3051da177e4SLinus Torvalds 		ndev->dead = 1;
3061da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3071da177e4SLinus Torvalds 		return NULL;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
3111da177e4SLinus Torvalds 		ADBG((KERN_WARNING
3121da177e4SLinus Torvalds 			"%s(): cannot create /proc/net/dev_snmp6/%s\n",
3131da177e4SLinus Torvalds 			__FUNCTION__, dev->name));
3141da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3151da177e4SLinus Torvalds 		ndev->dead = 1;
3161da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3171da177e4SLinus Torvalds 		return NULL;
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3211da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3221da177e4SLinus Torvalds 	 */
3231da177e4SLinus Torvalds 	in6_dev_hold(ndev);
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
3261da177e4SLinus Torvalds 	init_timer(&ndev->regen_timer);
3271da177e4SLinus Torvalds 	ndev->regen_timer.function = ipv6_regen_rndid;
3281da177e4SLinus Torvalds 	ndev->regen_timer.data = (unsigned long) ndev;
3291da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
3301da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
3310be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
3320be669bbSJoerg Roedel 	    dev->type == ARPHRD_SIT ||
3330be669bbSJoerg Roedel #endif
3340be669bbSJoerg Roedel 	    dev->type == ARPHRD_NONE) {
3351da177e4SLinus Torvalds 		printk(KERN_INFO
336a1493d9cSDavid S. Miller 		       "%s: Disabled Privacy Extensions\n",
337a1493d9cSDavid S. Miller 		       dev->name);
3381da177e4SLinus Torvalds 		ndev->cnf.use_tempaddr = -1;
3391da177e4SLinus Torvalds 	} else {
3401da177e4SLinus Torvalds 		in6_dev_hold(ndev);
3411da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds #endif
3441da177e4SLinus Torvalds 
3453c21edbdSYOSHIFUJI Hideaki 	if (netif_carrier_ok(dev))
3463c21edbdSYOSHIFUJI Hideaki 		ndev->if_flags |= IF_READY;
3473c21edbdSYOSHIFUJI Hideaki 
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
3501da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
3511da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
3521da177e4SLinus Torvalds 	neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
3531da177e4SLinus Torvalds 			      NET_IPV6_NEIGH, "ipv6",
3541da177e4SLinus Torvalds 			      &ndisc_ifinfo_sysctl_change,
3551da177e4SLinus Torvalds 			      NULL);
3561da177e4SLinus Torvalds 	addrconf_sysctl_register(ndev, &ndev->cnf);
3571da177e4SLinus Torvalds #endif
35830c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
35930c4cf57SDavid L Stevens 	rcu_assign_pointer(dev->ip6_ptr, ndev);
360d88ae4ccSYOSHIFUJI Hideaki 
361d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
362d88ae4ccSYOSHIFUJI Hideaki 	ipv6_addr_all_nodes(&maddr);
363d88ae4ccSYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &maddr);
364d88ae4ccSYOSHIFUJI Hideaki 
3651da177e4SLinus Torvalds 	return ndev;
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	struct inet6_dev *idev;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	ASSERT_RTNL();
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL) {
3751da177e4SLinus Torvalds 		if ((idev = ipv6_add_dev(dev)) == NULL)
3761da177e4SLinus Torvalds 			return NULL;
3771da177e4SLinus Torvalds 	}
378c5e33bddSYOSHIFUJI Hideaki 
3791da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
3801da177e4SLinus Torvalds 		ipv6_mc_up(idev);
3811da177e4SLinus Torvalds 	return idev;
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
3851da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	struct net_device *dev;
3881da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
3891da177e4SLinus Torvalds 	struct in6_addr addr;
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	if (!idev)
3921da177e4SLinus Torvalds 		return;
3931da177e4SLinus Torvalds 	dev = idev->dev;
3941da177e4SLinus Torvalds 	if (dev && (dev->flags & IFF_MULTICAST)) {
3951da177e4SLinus Torvalds 		ipv6_addr_all_routers(&addr);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
3981da177e4SLinus Torvalds 			ipv6_dev_mc_inc(dev, &addr);
3991da177e4SLinus Torvalds 		else
4001da177e4SLinus Torvalds 			ipv6_dev_mc_dec(dev, &addr);
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 	for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
4031da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
4041da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
4051da177e4SLinus Torvalds 		else
4061da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds static void addrconf_forward_change(void)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	struct net_device *dev;
4141da177e4SLinus Torvalds 	struct inet6_dev *idev;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
4171da177e4SLinus Torvalds 	for (dev=dev_base; dev; dev=dev->next) {
4188814c4b5SYOSHIFUJI Hideaki 		rcu_read_lock();
4191da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
4201da177e4SLinus Torvalds 		if (idev) {
4211da177e4SLinus Torvalds 			int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
4221da177e4SLinus Torvalds 			idev->cnf.forwarding = ipv6_devconf.forwarding;
4231da177e4SLinus Torvalds 			if (changed)
4241da177e4SLinus Torvalds 				dev_forward_change(idev);
4251da177e4SLinus Torvalds 		}
4268814c4b5SYOSHIFUJI Hideaki 		rcu_read_unlock();
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds #endif
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds /* Nobody refers to this ifaddr, destroy it */
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
4351da177e4SLinus Torvalds {
4361da177e4SLinus Torvalds 	BUG_TRAP(ifp->if_next==NULL);
4371da177e4SLinus Torvalds 	BUG_TRAP(ifp->lst_next==NULL);
4381da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
4391da177e4SLinus Torvalds 	printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
4401da177e4SLinus Torvalds #endif
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
4451da177e4SLinus Torvalds 		printk("Timer is still running, when freeing ifa=%p\n", ifp);
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	if (!ifp->dead) {
4481da177e4SLinus Torvalds 		printk("Freeing alive inet6 address %p\n", ifp);
4491da177e4SLinus Torvalds 		return;
4501da177e4SLinus Torvalds 	}
4511da177e4SLinus Torvalds 	dst_release(&ifp->rt->u.dst);
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	kfree(ifp);
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
456e55ffac6SBrian Haley static void
457e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
458e55ffac6SBrian Haley {
459e55ffac6SBrian Haley 	struct inet6_ifaddr *ifa, **ifap;
4608a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
461e55ffac6SBrian Haley 
462e55ffac6SBrian Haley 	/*
463e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
464e55ffac6SBrian Haley 	 * global before linklocal.
465e55ffac6SBrian Haley 	 */
466e55ffac6SBrian Haley 	for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
467e55ffac6SBrian Haley 	     ifap = &ifa->if_next) {
4688a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
469e55ffac6SBrian Haley 			break;
470e55ffac6SBrian Haley 	}
471e55ffac6SBrian Haley 
472e55ffac6SBrian Haley 	ifp->if_next = *ifap;
473e55ffac6SBrian Haley 	*ifap = ifp;
474e55ffac6SBrian Haley }
475e55ffac6SBrian Haley 
4761da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds static struct inet6_ifaddr *
4791da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
480e431b8c0SJamal Hadi Salim 	      int scope, u32 flags)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
4831da177e4SLinus Torvalds 	struct rt6_info *rt;
4841da177e4SLinus Torvalds 	int hash;
4851da177e4SLinus Torvalds 	int err = 0;
4861da177e4SLinus Torvalds 
4878814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
4881da177e4SLinus Torvalds 	if (idev->dead) {
4891da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
4901da177e4SLinus Torvalds 		goto out2;
4911da177e4SLinus Torvalds 	}
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	write_lock(&addrconf_hash_lock);
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
4961da177e4SLinus Torvalds 	if (ipv6_chk_same_addr(addr, idev->dev)) {
4971da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: already assigned\n"));
4981da177e4SLinus Torvalds 		err = -EEXIST;
4991da177e4SLinus Torvalds 		goto out;
5001da177e4SLinus Torvalds 	}
5011da177e4SLinus Torvalds 
502322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	if (ifa == NULL) {
5051da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: malloc failed\n"));
5061da177e4SLinus Torvalds 		err = -ENOBUFS;
5071da177e4SLinus Torvalds 		goto out;
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	rt = addrconf_dst_alloc(idev, addr, 0);
5111da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
5121da177e4SLinus Torvalds 		err = PTR_ERR(rt);
5131da177e4SLinus Torvalds 		goto out;
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	ipv6_addr_copy(&ifa->addr, addr);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
5191da177e4SLinus Torvalds 	init_timer(&ifa->timer);
5201da177e4SLinus Torvalds 	ifa->timer.data = (unsigned long) ifa;
5211da177e4SLinus Torvalds 	ifa->scope = scope;
5221da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
5231da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
5241da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
5251da177e4SLinus Torvalds 
52657f5f544SKeir Fraser 	ifa->rt = rt;
52757f5f544SKeir Fraser 
5281da177e4SLinus Torvalds 	ifa->idev = idev;
5291da177e4SLinus Torvalds 	in6_dev_hold(idev);
5301da177e4SLinus Torvalds 	/* For caller */
5311da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	/* Add to big hash table */
5341da177e4SLinus Torvalds 	hash = ipv6_addr_hash(addr);
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	ifa->lst_next = inet6_addr_lst[hash];
5371da177e4SLinus Torvalds 	inet6_addr_lst[hash] = ifa;
5381da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
5391da177e4SLinus Torvalds 	write_unlock(&addrconf_hash_lock);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	write_lock(&idev->lock);
5421da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
543e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
5461da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
5471da177e4SLinus Torvalds 		ifa->tmp_next = idev->tempaddr_list;
5481da177e4SLinus Torvalds 		idev->tempaddr_list = ifa;
5491da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
5501da177e4SLinus Torvalds 	}
5511da177e4SLinus Torvalds #endif
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
5541da177e4SLinus Torvalds 	write_unlock(&idev->lock);
5551da177e4SLinus Torvalds out2:
5568814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
5571da177e4SLinus Torvalds 
558fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
559e041c683SAlan Stern 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
5601da177e4SLinus Torvalds 	else {
5611da177e4SLinus Torvalds 		kfree(ifa);
5621da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	return ifa;
5661da177e4SLinus Torvalds out:
5671da177e4SLinus Torvalds 	write_unlock(&addrconf_hash_lock);
5681da177e4SLinus Torvalds 	goto out2;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
5741da177e4SLinus Torvalds {
5751da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **ifap;
5761da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
5771da177e4SLinus Torvalds 	int hash;
5781da177e4SLinus Torvalds 	int deleted = 0, onlink = 0;
5791da177e4SLinus Torvalds 	unsigned long expires = jiffies;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	hash = ipv6_addr_hash(&ifp->addr);
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	ifp->dead = 1;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
5861da177e4SLinus Torvalds 	for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
5871da177e4SLinus Torvalds 	     ifap = &ifa->lst_next) {
5881da177e4SLinus Torvalds 		if (ifa == ifp) {
5891da177e4SLinus Torvalds 			*ifap = ifa->lst_next;
5901da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
5911da177e4SLinus Torvalds 			ifa->lst_next = NULL;
5921da177e4SLinus Torvalds 			break;
5931da177e4SLinus Torvalds 		}
5941da177e4SLinus Torvalds 	}
5951da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
5981da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
5991da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
6001da177e4SLinus Torvalds 		for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
6011da177e4SLinus Torvalds 		     ifap = &ifa->tmp_next) {
6021da177e4SLinus Torvalds 			if (ifa == ifp) {
6031da177e4SLinus Torvalds 				*ifap = ifa->tmp_next;
6041da177e4SLinus Torvalds 				if (ifp->ifpub) {
6051da177e4SLinus Torvalds 					in6_ifa_put(ifp->ifpub);
6061da177e4SLinus Torvalds 					ifp->ifpub = NULL;
6071da177e4SLinus Torvalds 				}
6081da177e4SLinus Torvalds 				__in6_ifa_put(ifp);
6091da177e4SLinus Torvalds 				ifa->tmp_next = NULL;
6101da177e4SLinus Torvalds 				break;
6111da177e4SLinus Torvalds 			}
6121da177e4SLinus Torvalds 		}
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds #endif
6151da177e4SLinus Torvalds 
6161d142804SKristian Slavov 	for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
6171da177e4SLinus Torvalds 		if (ifa == ifp) {
6181da177e4SLinus Torvalds 			*ifap = ifa->if_next;
6191da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
6201da177e4SLinus Torvalds 			ifa->if_next = NULL;
6211da177e4SLinus Torvalds 			if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
6221da177e4SLinus Torvalds 				break;
6231da177e4SLinus Torvalds 			deleted = 1;
6241d142804SKristian Slavov 			continue;
6251da177e4SLinus Torvalds 		} else if (ifp->flags & IFA_F_PERMANENT) {
6261da177e4SLinus Torvalds 			if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
6271da177e4SLinus Torvalds 					      ifp->prefix_len)) {
6281da177e4SLinus Torvalds 				if (ifa->flags & IFA_F_PERMANENT) {
6291da177e4SLinus Torvalds 					onlink = 1;
6301da177e4SLinus Torvalds 					if (deleted)
6311da177e4SLinus Torvalds 						break;
6321da177e4SLinus Torvalds 				} else {
6331da177e4SLinus Torvalds 					unsigned long lifetime;
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 					if (!onlink)
6361da177e4SLinus Torvalds 						onlink = -1;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 					spin_lock(&ifa->lock);
6391da177e4SLinus Torvalds 					lifetime = min_t(unsigned long,
6401da177e4SLinus Torvalds 							 ifa->valid_lft, 0x7fffffffUL/HZ);
6411da177e4SLinus Torvalds 					if (time_before(expires,
6421da177e4SLinus Torvalds 							ifa->tstamp + lifetime * HZ))
6431da177e4SLinus Torvalds 						expires = ifa->tstamp + lifetime * HZ;
6441da177e4SLinus Torvalds 					spin_unlock(&ifa->lock);
6451da177e4SLinus Torvalds 				}
6461da177e4SLinus Torvalds 			}
6471da177e4SLinus Torvalds 		}
6481d142804SKristian Slavov 		ifap = &ifa->if_next;
6491da177e4SLinus Torvalds 	}
6501da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
6531da177e4SLinus Torvalds 
654e041c683SAlan Stern 	atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	addrconf_del_timer(ifp);
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	/*
6591da177e4SLinus Torvalds 	 * Purge or update corresponding prefix
6601da177e4SLinus Torvalds 	 *
6611da177e4SLinus Torvalds 	 * 1) we don't purge prefix here if address was not permanent.
6621da177e4SLinus Torvalds 	 *    prefix is managed by its own lifetime.
6631da177e4SLinus Torvalds 	 * 2) if there're no addresses, delete prefix.
6641da177e4SLinus Torvalds 	 * 3) if there're still other permanent address(es),
6651da177e4SLinus Torvalds 	 *    corresponding prefix is still permanent.
6661da177e4SLinus Torvalds 	 * 4) otherwise, update prefix lifetime to the
6671da177e4SLinus Torvalds 	 *    longest valid lifetime among the corresponding
6681da177e4SLinus Torvalds 	 *    addresses on the device.
6691da177e4SLinus Torvalds 	 *    Note: subsequent RA will update lifetime.
6701da177e4SLinus Torvalds 	 *
6711da177e4SLinus Torvalds 	 * --yoshfuji
6721da177e4SLinus Torvalds 	 */
6731da177e4SLinus Torvalds 	if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
6741da177e4SLinus Torvalds 		struct in6_addr prefix;
6751da177e4SLinus Torvalds 		struct rt6_info *rt;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 		ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
6781da177e4SLinus Torvalds 		rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 		if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
6811da177e4SLinus Torvalds 			if (onlink == 0) {
682e0a1ad73SThomas Graf 				ip6_del_rt(rt);
6831da177e4SLinus Torvalds 				rt = NULL;
6841da177e4SLinus Torvalds 			} else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
6851da177e4SLinus Torvalds 				rt->rt6i_expires = expires;
6861da177e4SLinus Torvalds 				rt->rt6i_flags |= RTF_EXPIRES;
6871da177e4SLinus Torvalds 			}
6881da177e4SLinus Torvalds 		}
6891da177e4SLinus Torvalds 		dst_release(&rt->u.dst);
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	in6_ifa_put(ifp);
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
6961da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
6991da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
7001da177e4SLinus Torvalds 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
7011da177e4SLinus Torvalds 	int tmp_plen;
7021da177e4SLinus Torvalds 	int ret = 0;
7031da177e4SLinus Torvalds 	int max_addresses;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	write_lock(&idev->lock);
7061da177e4SLinus Torvalds 	if (ift) {
7071da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
7081da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
7091da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
7101da177e4SLinus Torvalds 		tmpaddr = &addr;
7111da177e4SLinus Torvalds 	} else {
7121da177e4SLinus Torvalds 		tmpaddr = NULL;
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds retry:
7151da177e4SLinus Torvalds 	in6_dev_hold(idev);
7161da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
7171da177e4SLinus Torvalds 		write_unlock(&idev->lock);
7181da177e4SLinus Torvalds 		printk(KERN_INFO
7191da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
7201da177e4SLinus Torvalds 		in6_dev_put(idev);
7211da177e4SLinus Torvalds 		ret = -1;
7221da177e4SLinus Torvalds 		goto out;
7231da177e4SLinus Torvalds 	}
7241da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
7251da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
7261da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
7271da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
7281da177e4SLinus Torvalds 		write_unlock(&idev->lock);
7291da177e4SLinus Torvalds 		printk(KERN_WARNING
7301da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
7311da177e4SLinus Torvalds 		in6_dev_put(idev);
7321da177e4SLinus Torvalds 		ret = -1;
7331da177e4SLinus Torvalds 		goto out;
7341da177e4SLinus Torvalds 	}
7351da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
7361da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
7371da177e4SLinus Torvalds 	if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
7381da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
7391da177e4SLinus Torvalds 		write_unlock(&idev->lock);
7401da177e4SLinus Torvalds 		printk(KERN_WARNING
7411da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
7421da177e4SLinus Torvalds 		in6_ifa_put(ifp);
7431da177e4SLinus Torvalds 		in6_dev_put(idev);
7441da177e4SLinus Torvalds 		ret = -1;
7451da177e4SLinus Torvalds 		goto out;
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
7481da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
7491da177e4SLinus Torvalds 			      ifp->valid_lft,
7501da177e4SLinus Torvalds 			      idev->cnf.temp_valid_lft);
7511da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
7521da177e4SLinus Torvalds 				 ifp->prefered_lft,
7531da177e4SLinus Torvalds 				 idev->cnf.temp_prefered_lft - desync_factor / HZ);
7541da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
7551da177e4SLinus Torvalds 	max_addresses = idev->cnf.max_addresses;
7561da177e4SLinus Torvalds 	tmp_cstamp = ifp->cstamp;
7571da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
7581da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	write_unlock(&idev->lock);
7611da177e4SLinus Torvalds 	ift = !max_addresses ||
7621da177e4SLinus Torvalds 	      ipv6_count_addresses(idev) < max_addresses ?
7631da177e4SLinus Torvalds 		ipv6_add_addr(idev, &addr, tmp_plen,
7641da177e4SLinus Torvalds 			      ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
7651da177e4SLinus Torvalds 	if (!ift || IS_ERR(ift)) {
7661da177e4SLinus Torvalds 		in6_ifa_put(ifp);
7671da177e4SLinus Torvalds 		in6_dev_put(idev);
7681da177e4SLinus Torvalds 		printk(KERN_INFO
7691da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): retry temporary address regeneration.\n");
7701da177e4SLinus Torvalds 		tmpaddr = &addr;
7711da177e4SLinus Torvalds 		write_lock(&idev->lock);
7721da177e4SLinus Torvalds 		goto retry;
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
7761da177e4SLinus Torvalds 	ift->ifpub = ifp;
7771da177e4SLinus Torvalds 	ift->valid_lft = tmp_valid_lft;
7781da177e4SLinus Torvalds 	ift->prefered_lft = tmp_prefered_lft;
7791da177e4SLinus Torvalds 	ift->cstamp = tmp_cstamp;
7801da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
7811da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	addrconf_dad_start(ift, 0);
7841da177e4SLinus Torvalds 	in6_ifa_put(ift);
7851da177e4SLinus Torvalds 	in6_dev_put(idev);
7861da177e4SLinus Torvalds out:
7871da177e4SLinus Torvalds 	return ret;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds #endif
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds /*
792072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
7931da177e4SLinus Torvalds  */
794072047e4SYOSHIFUJI Hideaki struct ipv6_saddr_score {
795072047e4SYOSHIFUJI Hideaki 	int		addr_type;
796072047e4SYOSHIFUJI Hideaki 	unsigned int	attrs;
797072047e4SYOSHIFUJI Hideaki 	int		matchlen;
7980d27b427SBrian Haley 	int		scope;
799072047e4SYOSHIFUJI Hideaki 	unsigned int	rule;
800072047e4SYOSHIFUJI Hideaki };
801072047e4SYOSHIFUJI Hideaki 
802072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_LOCAL		0x0001
803072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_PREFERRED	0x0004
804072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_HOA		0x0008
805072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_OIF		0x0010
806072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_LABEL		0x0020
807072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_PRIVACY	0x0040
808072047e4SYOSHIFUJI Hideaki 
809072047e4SYOSHIFUJI Hideaki static int inline ipv6_saddr_preferred(int type)
8101da177e4SLinus Torvalds {
811072047e4SYOSHIFUJI Hideaki 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
812072047e4SYOSHIFUJI Hideaki 		    IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
813072047e4SYOSHIFUJI Hideaki 		return 1;
814072047e4SYOSHIFUJI Hideaki 	return 0;
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
817072047e4SYOSHIFUJI Hideaki /* static matching label */
818072047e4SYOSHIFUJI Hideaki static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
819072047e4SYOSHIFUJI Hideaki {
820072047e4SYOSHIFUJI Hideaki  /*
821072047e4SYOSHIFUJI Hideaki   * 	prefix (longest match)	label
822072047e4SYOSHIFUJI Hideaki   * 	-----------------------------
823072047e4SYOSHIFUJI Hideaki   * 	::1/128			0
824072047e4SYOSHIFUJI Hideaki   * 	::/0			1
825072047e4SYOSHIFUJI Hideaki   * 	2002::/16		2
826072047e4SYOSHIFUJI Hideaki   * 	::/96			3
827072047e4SYOSHIFUJI Hideaki   * 	::ffff:0:0/96		4
828102128e3SŁukasz Stelmach   *	fc00::/7		5
829102128e3SŁukasz Stelmach   * 	2001::/32		6
830072047e4SYOSHIFUJI Hideaki   */
831072047e4SYOSHIFUJI Hideaki 	if (type & IPV6_ADDR_LOOPBACK)
832072047e4SYOSHIFUJI Hideaki 		return 0;
833072047e4SYOSHIFUJI Hideaki 	else if (type & IPV6_ADDR_COMPATv4)
834072047e4SYOSHIFUJI Hideaki 		return 3;
835072047e4SYOSHIFUJI Hideaki 	else if (type & IPV6_ADDR_MAPPED)
836072047e4SYOSHIFUJI Hideaki 		return 4;
837102128e3SŁukasz Stelmach 	else if (addr->s6_addr32[0] == htonl(0x20010000))
838102128e3SŁukasz Stelmach 		return 6;
839072047e4SYOSHIFUJI Hideaki 	else if (addr->s6_addr16[0] == htons(0x2002))
840072047e4SYOSHIFUJI Hideaki 		return 2;
841102128e3SŁukasz Stelmach 	else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
842102128e3SŁukasz Stelmach 		return 5;
843072047e4SYOSHIFUJI Hideaki 	return 1;
844072047e4SYOSHIFUJI Hideaki }
8451da177e4SLinus Torvalds 
846072047e4SYOSHIFUJI Hideaki int ipv6_dev_get_saddr(struct net_device *daddr_dev,
8471da177e4SLinus Torvalds 		       struct in6_addr *daddr, struct in6_addr *saddr)
8481da177e4SLinus Torvalds {
849072047e4SYOSHIFUJI Hideaki 	struct ipv6_saddr_score hiscore;
850072047e4SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa_result = NULL;
851072047e4SYOSHIFUJI Hideaki 	int daddr_type = __ipv6_addr_type(daddr);
852072047e4SYOSHIFUJI Hideaki 	int daddr_scope = __ipv6_addr_src_scope(daddr_type);
853072047e4SYOSHIFUJI Hideaki 	u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
854072047e4SYOSHIFUJI Hideaki 	struct net_device *dev;
8551da177e4SLinus Torvalds 
856072047e4SYOSHIFUJI Hideaki 	memset(&hiscore, 0, sizeof(hiscore));
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
8598814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
860072047e4SYOSHIFUJI Hideaki 
8611da177e4SLinus Torvalds 	for (dev = dev_base; dev; dev=dev->next) {
862072047e4SYOSHIFUJI Hideaki 		struct inet6_dev *idev;
863072047e4SYOSHIFUJI Hideaki 		struct inet6_ifaddr *ifa;
864072047e4SYOSHIFUJI Hideaki 
865072047e4SYOSHIFUJI Hideaki 		/* Rule 0: Candidate Source Address (section 4)
866072047e4SYOSHIFUJI Hideaki 		 *  - multicast and link-local destination address,
867072047e4SYOSHIFUJI Hideaki 		 *    the set of candidate source address MUST only
868072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
869072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same link as the outgoing
870072047e4SYOSHIFUJI Hideaki 		 *    interface.
871072047e4SYOSHIFUJI Hideaki 		 * (- For site-local destination addresses, the
872072047e4SYOSHIFUJI Hideaki 		 *    set of candidate source addresses MUST only
873072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
874072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same site as the outgoing
875072047e4SYOSHIFUJI Hideaki 		 *    interface.)
876072047e4SYOSHIFUJI Hideaki 		 */
877072047e4SYOSHIFUJI Hideaki 		if ((daddr_type & IPV6_ADDR_MULTICAST ||
878072047e4SYOSHIFUJI Hideaki 		     daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
879072047e4SYOSHIFUJI Hideaki 		    daddr_dev && dev != daddr_dev)
880072047e4SYOSHIFUJI Hideaki 			continue;
881072047e4SYOSHIFUJI Hideaki 
8821da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
883072047e4SYOSHIFUJI Hideaki 		if (!idev)
884072047e4SYOSHIFUJI Hideaki 			continue;
885072047e4SYOSHIFUJI Hideaki 
8861da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
887072047e4SYOSHIFUJI Hideaki 		for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
888072047e4SYOSHIFUJI Hideaki 			struct ipv6_saddr_score score;
889072047e4SYOSHIFUJI Hideaki 
890072047e4SYOSHIFUJI Hideaki 			score.addr_type = __ipv6_addr_type(&ifa->addr);
891072047e4SYOSHIFUJI Hideaki 
8926b3ae80aSYOSHIFUJI Hideaki 			/* Rule 0:
8936b3ae80aSYOSHIFUJI Hideaki 			 * - Tentative Address (RFC2462 section 5.4)
8946b3ae80aSYOSHIFUJI Hideaki 			 *  - A tentative address is not considered
8956b3ae80aSYOSHIFUJI Hideaki 			 *    "assigned to an interface" in the traditional
8966b3ae80aSYOSHIFUJI Hideaki 			 *    sense.
8976b3ae80aSYOSHIFUJI Hideaki 			 * - Candidate Source Address (section 4)
898072047e4SYOSHIFUJI Hideaki 			 *  - In any case, anycast addresses, multicast
899072047e4SYOSHIFUJI Hideaki 			 *    addresses, and the unspecified address MUST
900072047e4SYOSHIFUJI Hideaki 			 *    NOT be included in a candidate set.
901072047e4SYOSHIFUJI Hideaki 			 */
9026b3ae80aSYOSHIFUJI Hideaki 			if (ifa->flags & IFA_F_TENTATIVE)
9036b3ae80aSYOSHIFUJI Hideaki 				continue;
904072047e4SYOSHIFUJI Hideaki 			if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
905072047e4SYOSHIFUJI Hideaki 				     score.addr_type & IPV6_ADDR_MULTICAST)) {
906072047e4SYOSHIFUJI Hideaki 				LIMIT_NETDEBUG(KERN_DEBUG
907072047e4SYOSHIFUJI Hideaki 					       "ADDRCONF: unspecified / multicast address"
908072047e4SYOSHIFUJI Hideaki 					       "assigned as unicast address on %s",
909072047e4SYOSHIFUJI Hideaki 					       dev->name);
9101da177e4SLinus Torvalds 				continue;
911072047e4SYOSHIFUJI Hideaki 			}
912072047e4SYOSHIFUJI Hideaki 
913072047e4SYOSHIFUJI Hideaki 			score.attrs = 0;
914072047e4SYOSHIFUJI Hideaki 			score.matchlen = 0;
915072047e4SYOSHIFUJI Hideaki 			score.scope = 0;
916072047e4SYOSHIFUJI Hideaki 			score.rule = 0;
917072047e4SYOSHIFUJI Hideaki 
918072047e4SYOSHIFUJI Hideaki 			if (ifa_result == NULL) {
919072047e4SYOSHIFUJI Hideaki 				/* record it if the first available entry */
920072047e4SYOSHIFUJI Hideaki 				goto record_it;
921072047e4SYOSHIFUJI Hideaki 			}
922072047e4SYOSHIFUJI Hideaki 
923072047e4SYOSHIFUJI Hideaki 			/* Rule 1: Prefer same address */
924072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 1) {
925072047e4SYOSHIFUJI Hideaki 				if (ipv6_addr_equal(&ifa_result->addr, daddr))
926072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
927072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
928072047e4SYOSHIFUJI Hideaki 			}
929072047e4SYOSHIFUJI Hideaki 			if (ipv6_addr_equal(&ifa->addr, daddr)) {
930072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_LOCAL;
931072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
932072047e4SYOSHIFUJI Hideaki 					score.rule = 1;
933072047e4SYOSHIFUJI Hideaki 					goto record_it;
934072047e4SYOSHIFUJI Hideaki 				}
935072047e4SYOSHIFUJI Hideaki 			} else {
936072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
937072047e4SYOSHIFUJI Hideaki 					continue;
938072047e4SYOSHIFUJI Hideaki 			}
939072047e4SYOSHIFUJI Hideaki 
940072047e4SYOSHIFUJI Hideaki 			/* Rule 2: Prefer appropriate scope */
941072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 2) {
942072047e4SYOSHIFUJI Hideaki 				hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
943072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
944072047e4SYOSHIFUJI Hideaki 			}
945072047e4SYOSHIFUJI Hideaki 			score.scope = __ipv6_addr_src_scope(score.addr_type);
946072047e4SYOSHIFUJI Hideaki 			if (hiscore.scope < score.scope) {
947072047e4SYOSHIFUJI Hideaki 				if (hiscore.scope < daddr_scope) {
948072047e4SYOSHIFUJI Hideaki 					score.rule = 2;
949072047e4SYOSHIFUJI Hideaki 					goto record_it;
950072047e4SYOSHIFUJI Hideaki 				} else
951072047e4SYOSHIFUJI Hideaki 					continue;
952072047e4SYOSHIFUJI Hideaki 			} else if (score.scope < hiscore.scope) {
953072047e4SYOSHIFUJI Hideaki 				if (score.scope < daddr_scope)
954e55ffac6SBrian Haley 					break; /* addresses sorted by scope */
955072047e4SYOSHIFUJI Hideaki 				else {
956072047e4SYOSHIFUJI Hideaki 					score.rule = 2;
957072047e4SYOSHIFUJI Hideaki 					goto record_it;
958072047e4SYOSHIFUJI Hideaki 				}
959072047e4SYOSHIFUJI Hideaki 			}
960072047e4SYOSHIFUJI Hideaki 
961072047e4SYOSHIFUJI Hideaki 			/* Rule 3: Avoid deprecated address */
962072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 3) {
963072047e4SYOSHIFUJI Hideaki 				if (ipv6_saddr_preferred(hiscore.addr_type) ||
964072047e4SYOSHIFUJI Hideaki 				    !(ifa_result->flags & IFA_F_DEPRECATED))
965072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
966072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
967072047e4SYOSHIFUJI Hideaki 			}
968072047e4SYOSHIFUJI Hideaki 			if (ipv6_saddr_preferred(score.addr_type) ||
969072047e4SYOSHIFUJI Hideaki 			    !(ifa->flags & IFA_F_DEPRECATED)) {
970072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
971072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
972072047e4SYOSHIFUJI Hideaki 					score.rule = 3;
973072047e4SYOSHIFUJI Hideaki 					goto record_it;
974072047e4SYOSHIFUJI Hideaki 				}
975072047e4SYOSHIFUJI Hideaki 			} else {
976072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
977072047e4SYOSHIFUJI Hideaki 					continue;
978072047e4SYOSHIFUJI Hideaki 			}
979072047e4SYOSHIFUJI Hideaki 
9803b9f9a1cSNoriaki TAKAMIYA 			/* Rule 4: Prefer home address */
9813b9f9a1cSNoriaki TAKAMIYA #ifdef CONFIG_IPV6_MIP6
9823b9f9a1cSNoriaki TAKAMIYA 			if (hiscore.rule < 4) {
9833b9f9a1cSNoriaki TAKAMIYA 				if (ifa_result->flags & IFA_F_HOMEADDRESS)
9843b9f9a1cSNoriaki TAKAMIYA 					hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
9853b9f9a1cSNoriaki TAKAMIYA 				hiscore.rule++;
9863b9f9a1cSNoriaki TAKAMIYA 			}
9873b9f9a1cSNoriaki TAKAMIYA 			if (ifa->flags & IFA_F_HOMEADDRESS) {
9883b9f9a1cSNoriaki TAKAMIYA 				score.attrs |= IPV6_SADDR_SCORE_HOA;
9893b9f9a1cSNoriaki TAKAMIYA 				if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
9903b9f9a1cSNoriaki TAKAMIYA 					score.rule = 4;
9913b9f9a1cSNoriaki TAKAMIYA 					goto record_it;
9923b9f9a1cSNoriaki TAKAMIYA 				}
9933b9f9a1cSNoriaki TAKAMIYA 			} else {
9943b9f9a1cSNoriaki TAKAMIYA 				if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
9953b9f9a1cSNoriaki TAKAMIYA 					continue;
9963b9f9a1cSNoriaki TAKAMIYA 			}
9973b9f9a1cSNoriaki TAKAMIYA #else
998220bbd74SYOSHIFUJI Hideaki 			if (hiscore.rule < 4)
999220bbd74SYOSHIFUJI Hideaki 				hiscore.rule++;
10003b9f9a1cSNoriaki TAKAMIYA #endif
1001072047e4SYOSHIFUJI Hideaki 
1002072047e4SYOSHIFUJI Hideaki 			/* Rule 5: Prefer outgoing interface */
1003072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 5) {
1004072047e4SYOSHIFUJI Hideaki 				if (daddr_dev == NULL ||
1005072047e4SYOSHIFUJI Hideaki 				    daddr_dev == ifa_result->idev->dev)
1006072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1007072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1008072047e4SYOSHIFUJI Hideaki 			}
1009072047e4SYOSHIFUJI Hideaki 			if (daddr_dev == NULL ||
1010072047e4SYOSHIFUJI Hideaki 			    daddr_dev == ifa->idev->dev) {
1011072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_OIF;
1012072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1013072047e4SYOSHIFUJI Hideaki 					score.rule = 5;
1014072047e4SYOSHIFUJI Hideaki 					goto record_it;
1015072047e4SYOSHIFUJI Hideaki 				}
1016072047e4SYOSHIFUJI Hideaki 			} else {
1017072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1018072047e4SYOSHIFUJI Hideaki 					continue;
1019072047e4SYOSHIFUJI Hideaki 			}
1020072047e4SYOSHIFUJI Hideaki 
1021072047e4SYOSHIFUJI Hideaki 			/* Rule 6: Prefer matching label */
1022072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 6) {
1023072047e4SYOSHIFUJI Hideaki 				if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1024072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1025072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1026072047e4SYOSHIFUJI Hideaki 			}
1027072047e4SYOSHIFUJI Hideaki 			if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1028072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_LABEL;
1029072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1030072047e4SYOSHIFUJI Hideaki 					score.rule = 6;
1031072047e4SYOSHIFUJI Hideaki 					goto record_it;
1032072047e4SYOSHIFUJI Hideaki 				}
1033072047e4SYOSHIFUJI Hideaki 			} else {
1034072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1035072047e4SYOSHIFUJI Hideaki 					continue;
1036072047e4SYOSHIFUJI Hideaki 			}
1037072047e4SYOSHIFUJI Hideaki 
103844fd0261SPeter Chubb #ifdef CONFIG_IPV6_PRIVACY
1039072047e4SYOSHIFUJI Hideaki 			/* Rule 7: Prefer public address
1040072047e4SYOSHIFUJI Hideaki 			 * Note: prefer temprary address if use_tempaddr >= 2
1041072047e4SYOSHIFUJI Hideaki 			 */
1042072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 7) {
1043072047e4SYOSHIFUJI Hideaki 				if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1044072047e4SYOSHIFUJI Hideaki 				    (ifa_result->idev->cnf.use_tempaddr >= 2))
1045072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1046072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1047072047e4SYOSHIFUJI Hideaki 			}
1048072047e4SYOSHIFUJI Hideaki 			if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1049072047e4SYOSHIFUJI Hideaki 			    (ifa->idev->cnf.use_tempaddr >= 2)) {
1050072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1051072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1052072047e4SYOSHIFUJI Hideaki 					score.rule = 7;
1053072047e4SYOSHIFUJI Hideaki 					goto record_it;
1054072047e4SYOSHIFUJI Hideaki 				}
1055072047e4SYOSHIFUJI Hideaki 			} else {
1056072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1057072047e4SYOSHIFUJI Hideaki 					continue;
1058072047e4SYOSHIFUJI Hideaki 			}
10595e2707faSYOSHIFUJI Hideaki #else
10605e2707faSYOSHIFUJI Hideaki 			if (hiscore.rule < 7)
10615e2707faSYOSHIFUJI Hideaki 				hiscore.rule++;
106244fd0261SPeter Chubb #endif
1063072047e4SYOSHIFUJI Hideaki 			/* Rule 8: Use longest matching prefix */
106412da2a43SYan Zheng 			if (hiscore.rule < 8) {
1065072047e4SYOSHIFUJI Hideaki 				hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
106612da2a43SYan Zheng 				hiscore.rule++;
106712da2a43SYan Zheng 			}
1068072047e4SYOSHIFUJI Hideaki 			score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1069072047e4SYOSHIFUJI Hideaki 			if (score.matchlen > hiscore.matchlen) {
1070072047e4SYOSHIFUJI Hideaki 				score.rule = 8;
1071072047e4SYOSHIFUJI Hideaki 				goto record_it;
1072072047e4SYOSHIFUJI Hideaki 			}
1073072047e4SYOSHIFUJI Hideaki #if 0
1074072047e4SYOSHIFUJI Hideaki 			else if (score.matchlen < hiscore.matchlen)
1075072047e4SYOSHIFUJI Hideaki 				continue;
10761da177e4SLinus Torvalds #endif
1077072047e4SYOSHIFUJI Hideaki 
1078072047e4SYOSHIFUJI Hideaki 			/* Final Rule: choose first available one */
10791da177e4SLinus Torvalds 			continue;
1080072047e4SYOSHIFUJI Hideaki record_it:
1081072047e4SYOSHIFUJI Hideaki 			if (ifa_result)
1082072047e4SYOSHIFUJI Hideaki 				in6_ifa_put(ifa_result);
1083072047e4SYOSHIFUJI Hideaki 			in6_ifa_hold(ifa);
1084072047e4SYOSHIFUJI Hideaki 			ifa_result = ifa;
10851da177e4SLinus Torvalds 			hiscore = score;
10861da177e4SLinus Torvalds 		}
10871da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
10881da177e4SLinus Torvalds 	}
10898814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
10901da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
10911da177e4SLinus Torvalds 
1092072047e4SYOSHIFUJI Hideaki 	if (!ifa_result)
1093072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
10941da177e4SLinus Torvalds 
1095072047e4SYOSHIFUJI Hideaki 	ipv6_addr_copy(saddr, &ifa_result->addr);
1096072047e4SYOSHIFUJI Hideaki 	in6_ifa_put(ifa_result);
1097072047e4SYOSHIFUJI Hideaki 	return 0;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds int ipv6_get_saddr(struct dst_entry *dst,
11021da177e4SLinus Torvalds 		   struct in6_addr *daddr, struct in6_addr *saddr)
11031da177e4SLinus Torvalds {
11047a3025b1SYOSHIFUJI Hideaki 	return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
11091da177e4SLinus Torvalds {
11101da177e4SLinus Torvalds 	struct inet6_dev *idev;
11111da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
11121da177e4SLinus Torvalds 
11138814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
11141da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) != NULL) {
11151da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
11181da177e4SLinus Torvalds 		for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
11191da177e4SLinus Torvalds 			if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
11201da177e4SLinus Torvalds 				ipv6_addr_copy(addr, &ifp->addr);
11211da177e4SLinus Torvalds 				err = 0;
11221da177e4SLinus Torvalds 				break;
11231da177e4SLinus Torvalds 			}
11241da177e4SLinus Torvalds 		}
11251da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
11261da177e4SLinus Torvalds 	}
11278814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
11281da177e4SLinus Torvalds 	return err;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
11321da177e4SLinus Torvalds {
11331da177e4SLinus Torvalds 	int cnt = 0;
11341da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
11371da177e4SLinus Torvalds 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
11381da177e4SLinus Torvalds 		cnt++;
11391da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
11401da177e4SLinus Torvalds 	return cnt;
11411da177e4SLinus Torvalds }
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
11441da177e4SLinus Torvalds {
11451da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
11461da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
11491da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
11501da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
11511da177e4SLinus Torvalds 		    !(ifp->flags&IFA_F_TENTATIVE)) {
11521da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
11531da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
11541da177e4SLinus Torvalds 				break;
11551da177e4SLinus Torvalds 		}
11561da177e4SLinus Torvalds 	}
11571da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
11581da177e4SLinus Torvalds 	return ifp != NULL;
11591da177e4SLinus Torvalds }
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds static
11621da177e4SLinus Torvalds int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
11631da177e4SLinus Torvalds {
11641da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
11651da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
11681da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
11691da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev)
11701da177e4SLinus Torvalds 				break;
11711da177e4SLinus Torvalds 		}
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 	return ifp != NULL;
11741da177e4SLinus Torvalds }
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
11771da177e4SLinus Torvalds {
11781da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
11791da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
11821da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
11831da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
11841da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
11851da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
11861da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
11871da177e4SLinus Torvalds 				break;
11881da177e4SLinus Torvalds 			}
11891da177e4SLinus Torvalds 		}
11901da177e4SLinus Torvalds 	}
11911da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	return ifp;
11941da177e4SLinus Torvalds }
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
11971da177e4SLinus Torvalds {
11981da177e4SLinus Torvalds 	const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
11990fa1a53eSArnaldo Carvalho de Melo 	const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
120082103232SAl Viro 	__be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
120182103232SAl Viro 	__be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
12021da177e4SLinus Torvalds 	int sk_ipv6only = ipv6_only_sock(sk);
1203463c84b9SArnaldo Carvalho de Melo 	int sk2_ipv6only = inet_v6_ipv6only(sk2);
12041da177e4SLinus Torvalds 	int addr_type = ipv6_addr_type(sk_rcv_saddr6);
12051da177e4SLinus Torvalds 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	if (!sk2_rcv_saddr && !sk_ipv6only)
12081da177e4SLinus Torvalds 		return 1;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	if (addr_type2 == IPV6_ADDR_ANY &&
12111da177e4SLinus Torvalds 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
12121da177e4SLinus Torvalds 		return 1;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_ANY &&
12151da177e4SLinus Torvalds 	    !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
12161da177e4SLinus Torvalds 		return 1;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	if (sk2_rcv_saddr6 &&
12191da177e4SLinus Torvalds 	    ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
12201da177e4SLinus Torvalds 		return 1;
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED &&
12231da177e4SLinus Torvalds 	    !sk2_ipv6only &&
12241da177e4SLinus Torvalds 	    (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
12251da177e4SLinus Torvalds 		return 1;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	return 0;
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
12311da177e4SLinus Torvalds 
12329f5336e2SAdrian Bunk static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
12331da177e4SLinus Torvalds {
12341da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
12351da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
12361da177e4SLinus Torvalds 		addrconf_del_timer(ifp);
12371da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
12381da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
12391da177e4SLinus Torvalds 		in6_ifa_put(ifp);
12401da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
12411da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
12421da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
12431da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
12441da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
12451da177e4SLinus Torvalds 		if (ifpub) {
12461da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
12471da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
12481da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
12491da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
12501da177e4SLinus Torvalds 		} else {
12511da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
12521da177e4SLinus Torvalds 		}
12531da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
12541da177e4SLinus Torvalds #endif
12551da177e4SLinus Torvalds 	} else
12561da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
12571da177e4SLinus Torvalds }
12581da177e4SLinus Torvalds 
12593c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
12603c21edbdSYOSHIFUJI Hideaki {
12613c21edbdSYOSHIFUJI Hideaki 	if (net_ratelimit())
12623c21edbdSYOSHIFUJI Hideaki 		printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
12633c21edbdSYOSHIFUJI Hideaki 	addrconf_dad_stop(ifp);
12643c21edbdSYOSHIFUJI Hideaki }
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds /* Join to solicited addr multicast group. */
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
12691da177e4SLinus Torvalds {
12701da177e4SLinus Torvalds 	struct in6_addr maddr;
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
12731da177e4SLinus Torvalds 		return;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
12761da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
12801da177e4SLinus Torvalds {
12811da177e4SLinus Torvalds 	struct in6_addr maddr;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
12841da177e4SLinus Torvalds 		return;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
12871da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
12881da177e4SLinus Torvalds }
12891da177e4SLinus Torvalds 
129020380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
12911da177e4SLinus Torvalds {
12921da177e4SLinus Torvalds 	struct in6_addr addr;
12931da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
12941da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
12951da177e4SLinus Torvalds 		return;
12961da177e4SLinus Torvalds 	ipv6_dev_ac_inc(ifp->idev->dev, &addr);
12971da177e4SLinus Torvalds }
12981da177e4SLinus Torvalds 
129920380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
13001da177e4SLinus Torvalds {
13011da177e4SLinus Torvalds 	struct in6_addr addr;
13021da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
13031da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
13041da177e4SLinus Torvalds 		return;
13051da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
13061da177e4SLinus Torvalds }
13071da177e4SLinus Torvalds 
1308073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
13091da177e4SLinus Torvalds {
13101da177e4SLinus Torvalds 	if (dev->addr_len != ETH_ALEN)
13111da177e4SLinus Torvalds 		return -1;
13121da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr, 3);
13131da177e4SLinus Torvalds 	memcpy(eui + 5, dev->dev_addr + 3, 3);
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	/*
13161da177e4SLinus Torvalds 	 * The zSeries OSA network cards can be shared among various
13171da177e4SLinus Torvalds 	 * OS instances, but the OSA cards have only one MAC address.
13181da177e4SLinus Torvalds 	 * This leads to duplicate address conflicts in conjunction
13191da177e4SLinus Torvalds 	 * with IPv6 if more than one instance uses the same card.
13201da177e4SLinus Torvalds 	 *
13211da177e4SLinus Torvalds 	 * The driver for these cards can deliver a unique 16-bit
13221da177e4SLinus Torvalds 	 * identifier for each instance sharing the same card.  It is
13231da177e4SLinus Torvalds 	 * placed instead of 0xFFFE in the interface identifier.  The
13241da177e4SLinus Torvalds 	 * "u" bit of the interface identifier is not inverted in this
13251da177e4SLinus Torvalds 	 * case.  Hence the resulting interface identifier has local
13261da177e4SLinus Torvalds 	 * scope according to RFC2373.
13271da177e4SLinus Torvalds 	 */
13281da177e4SLinus Torvalds 	if (dev->dev_id) {
13291da177e4SLinus Torvalds 		eui[3] = (dev->dev_id >> 8) & 0xFF;
13301da177e4SLinus Torvalds 		eui[4] = dev->dev_id & 0xFF;
13311da177e4SLinus Torvalds 	} else {
13321da177e4SLinus Torvalds 		eui[3] = 0xFF;
13331da177e4SLinus Torvalds 		eui[4] = 0xFE;
13341da177e4SLinus Torvalds 		eui[0] ^= 2;
13351da177e4SLinus Torvalds 	}
13361da177e4SLinus Torvalds 	return 0;
1337073a8e0eSYOSHIFUJI Hideaki }
1338073a8e0eSYOSHIFUJI Hideaki 
1339073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1340073a8e0eSYOSHIFUJI Hideaki {
13411da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
13421da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
13431da177e4SLinus Torvalds 		return -1;
13441da177e4SLinus Torvalds 	memset(eui, 0, 7);
13451da177e4SLinus Torvalds 	eui[7] = *(u8*)dev->dev_addr;
13461da177e4SLinus Torvalds 	return 0;
1347073a8e0eSYOSHIFUJI Hideaki }
1348073a8e0eSYOSHIFUJI Hideaki 
1349073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1350073a8e0eSYOSHIFUJI Hideaki {
13511da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
13521da177e4SLinus Torvalds 		return -1;
13531da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
13541da177e4SLinus Torvalds 	eui[0] |= 2;
13551da177e4SLinus Torvalds 	return 0;
13561da177e4SLinus Torvalds }
1357073a8e0eSYOSHIFUJI Hideaki 
1358073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1359073a8e0eSYOSHIFUJI Hideaki {
1360073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
1361073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
1362073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
1363073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_IEEE802_TR:
1364073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
1365073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
1366073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
1367073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
1368073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
1369073a8e0eSYOSHIFUJI Hideaki 	}
13701da177e4SLinus Torvalds 	return -1;
13711da177e4SLinus Torvalds }
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
13741da177e4SLinus Torvalds {
13751da177e4SLinus Torvalds 	int err = -1;
13761da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
13791da177e4SLinus Torvalds 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
13801da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
13811da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
13821da177e4SLinus Torvalds 			err = 0;
13831da177e4SLinus Torvalds 			break;
13841da177e4SLinus Torvalds 		}
13851da177e4SLinus Torvalds 	}
13861da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
13871da177e4SLinus Torvalds 	return err;
13881da177e4SLinus Torvalds }
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
13911da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
13921da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev)
13931da177e4SLinus Torvalds {
13941da177e4SLinus Torvalds regen:
1395955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
13961da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 	/*
13991da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
14001da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
14011da177e4SLinus Torvalds 	 *
14021da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
14031da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
14041da177e4SLinus Torvalds 	 *  - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
14051da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
14061da177e4SLinus Torvalds 	 *  - value 0
14071da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
14081da177e4SLinus Torvalds 	 */
14091da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
14101da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
14111da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
14121da177e4SLinus Torvalds 		goto regen;
14131da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
14141da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
14151da177e4SLinus Torvalds 			goto regen;
14161da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
14171da177e4SLinus Torvalds 			goto regen;
14181da177e4SLinus Torvalds 	}
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 	return 0;
14211da177e4SLinus Torvalds }
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
14241da177e4SLinus Torvalds {
14251da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
14261da177e4SLinus Torvalds 	unsigned long expires;
14271da177e4SLinus Torvalds 
14288814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
14291da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds 	if (idev->dead)
14321da177e4SLinus Torvalds 		goto out;
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 	if (__ipv6_regen_rndid(idev) < 0)
14351da177e4SLinus Torvalds 		goto out;
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 	expires = jiffies +
14381da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
14391da177e4SLinus Torvalds 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
14401da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
14411da177e4SLinus Torvalds 		printk(KERN_WARNING
14421da177e4SLinus Torvalds 			"ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
14431da177e4SLinus Torvalds 			idev->dev->name);
14441da177e4SLinus Torvalds 		goto out;
14451da177e4SLinus Torvalds 	}
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
14481da177e4SLinus Torvalds 		in6_dev_hold(idev);
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds out:
14511da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
14528814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
14531da177e4SLinus Torvalds 	in6_dev_put(idev);
14541da177e4SLinus Torvalds }
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
14571da177e4SLinus Torvalds 	int ret = 0;
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
14601da177e4SLinus Torvalds 		ret = __ipv6_regen_rndid(idev);
14611da177e4SLinus Torvalds 	return ret;
14621da177e4SLinus Torvalds }
14631da177e4SLinus Torvalds #endif
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds /*
14661da177e4SLinus Torvalds  *	Add prefix route.
14671da177e4SLinus Torvalds  */
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds static void
14701da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1471e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
14721da177e4SLinus Torvalds {
147386872cb5SThomas Graf 	struct fib6_config cfg = {
147486872cb5SThomas Graf 		.fc_table = RT6_TABLE_PREFIX,
147586872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
147686872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
147786872cb5SThomas Graf 		.fc_expires = expires,
147886872cb5SThomas Graf 		.fc_dst_len = plen,
147986872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
148086872cb5SThomas Graf 	};
14811da177e4SLinus Torvalds 
148286872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, pfx);
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
14851da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
14861da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
14871da177e4SLinus Torvalds 	 */
14880be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
14891da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
149086872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
14910be669bbSJoerg Roedel #endif
14921da177e4SLinus Torvalds 
149386872cb5SThomas Graf 	ip6_route_add(&cfg);
14941da177e4SLinus Torvalds }
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
14991da177e4SLinus Torvalds {
150086872cb5SThomas Graf 	struct fib6_config cfg = {
150186872cb5SThomas Graf 		.fc_table = RT6_TABLE_LOCAL,
150286872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
150386872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
150486872cb5SThomas Graf 		.fc_dst_len = 8,
150586872cb5SThomas Graf 		.fc_flags = RTF_UP,
150686872cb5SThomas Graf 	};
15071da177e4SLinus Torvalds 
150886872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
150986872cb5SThomas Graf 
151086872cb5SThomas Graf 	ip6_route_add(&cfg);
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds 
15130be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
15141da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev)
15151da177e4SLinus Torvalds {
151686872cb5SThomas Graf 	struct fib6_config cfg = {
151786872cb5SThomas Graf 		.fc_table = RT6_TABLE_MAIN,
151886872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
151986872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
152086872cb5SThomas Graf 		.fc_dst_len = 96,
152186872cb5SThomas Graf 		.fc_flags = RTF_UP | RTF_NONEXTHOP,
152286872cb5SThomas Graf 	};
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 	/* prefix length - 96 bits "::d.d.d.d" */
152586872cb5SThomas Graf 	ip6_route_add(&cfg);
15261da177e4SLinus Torvalds }
15270be669bbSJoerg Roedel #endif
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev)
15301da177e4SLinus Torvalds {
15311da177e4SLinus Torvalds 	struct in6_addr addr;
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 	ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
15341da177e4SLinus Torvalds 	addrconf_prefix_route(&addr, 64, dev, 0, 0);
15351da177e4SLinus Torvalds }
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
15381da177e4SLinus Torvalds {
15391da177e4SLinus Torvalds 	struct inet6_dev *idev;
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 	ASSERT_RTNL();
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL)
15441da177e4SLinus Torvalds 		return NULL;
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	/* Add default multicast route */
15471da177e4SLinus Torvalds 	addrconf_add_mroute(dev);
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	/* Add link local route */
15501da177e4SLinus Torvalds 	addrconf_add_lroute(dev);
15511da177e4SLinus Torvalds 	return idev;
15521da177e4SLinus Torvalds }
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
15551da177e4SLinus Torvalds {
15561da177e4SLinus Torvalds 	struct prefix_info *pinfo;
15571da177e4SLinus Torvalds 	__u32 valid_lft;
15581da177e4SLinus Torvalds 	__u32 prefered_lft;
15591da177e4SLinus Torvalds 	int addr_type;
15601da177e4SLinus Torvalds 	unsigned long rt_expires;
15611da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
15661da177e4SLinus Torvalds 		ADBG(("addrconf: prefix option too short\n"));
15671da177e4SLinus Torvalds 		return;
15681da177e4SLinus Torvalds 	}
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	/*
15711da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
15721da177e4SLinus Torvalds 	 */
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
15771da177e4SLinus Torvalds 		return;
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
15801da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
15831da177e4SLinus Torvalds 		if (net_ratelimit())
15841da177e4SLinus Torvalds 			printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
15851da177e4SLinus Torvalds 		return;
15861da177e4SLinus Torvalds 	}
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds 	if (in6_dev == NULL) {
15911da177e4SLinus Torvalds 		if (net_ratelimit())
15921da177e4SLinus Torvalds 			printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
15931da177e4SLinus Torvalds 		return;
15941da177e4SLinus Torvalds 	}
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	/*
15971da177e4SLinus Torvalds 	 *	Two things going on here:
15981da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
15991da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
16001da177e4SLinus Torvalds 	 */
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 	/* Avoid arithmetic overflow. Really, we could
16031da177e4SLinus Torvalds 	   save rt_expires in seconds, likely valid_lft,
16041da177e4SLinus Torvalds 	   but it would require division in fib gc, that it
16051da177e4SLinus Torvalds 	   not good.
16061da177e4SLinus Torvalds 	 */
16071da177e4SLinus Torvalds 	if (valid_lft >= 0x7FFFFFFF/HZ)
16083dd4bc68SYOSHIFUJI Hideaki 		rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
16091da177e4SLinus Torvalds 	else
16103dd4bc68SYOSHIFUJI Hideaki 		rt_expires = valid_lft * HZ;
16113dd4bc68SYOSHIFUJI Hideaki 
16123dd4bc68SYOSHIFUJI Hideaki 	/*
16133dd4bc68SYOSHIFUJI Hideaki 	 * We convert this (in jiffies) to clock_t later.
16143dd4bc68SYOSHIFUJI Hideaki 	 * Avoid arithmetic overflow there as well.
16153dd4bc68SYOSHIFUJI Hideaki 	 * Overflow can happen only if HZ < USER_HZ.
16163dd4bc68SYOSHIFUJI Hideaki 	 */
16173dd4bc68SYOSHIFUJI Hideaki 	if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
16183dd4bc68SYOSHIFUJI Hideaki 		rt_expires = 0x7FFFFFFF / USER_HZ;
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	if (pinfo->onlink) {
16211da177e4SLinus Torvalds 		struct rt6_info *rt;
16221da177e4SLinus Torvalds 		rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 		if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
16251da177e4SLinus Torvalds 			if (rt->rt6i_flags&RTF_EXPIRES) {
16261da177e4SLinus Torvalds 				if (valid_lft == 0) {
1627e0a1ad73SThomas Graf 					ip6_del_rt(rt);
16281da177e4SLinus Torvalds 					rt = NULL;
16291da177e4SLinus Torvalds 				} else {
16303dd4bc68SYOSHIFUJI Hideaki 					rt->rt6i_expires = jiffies + rt_expires;
16311da177e4SLinus Torvalds 				}
16321da177e4SLinus Torvalds 			}
16331da177e4SLinus Torvalds 		} else if (valid_lft) {
16341da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
16353dd4bc68SYOSHIFUJI Hideaki 					      dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
16361da177e4SLinus Torvalds 		}
16371da177e4SLinus Torvalds 		if (rt)
16381da177e4SLinus Torvalds 			dst_release(&rt->u.dst);
16391da177e4SLinus Torvalds 	}
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
16441da177e4SLinus Torvalds 		struct inet6_ifaddr * ifp;
16451da177e4SLinus Torvalds 		struct in6_addr addr;
16461da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
16491da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
16501da177e4SLinus Torvalds 			if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
16511da177e4SLinus Torvalds 			    ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
16521da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
16531da177e4SLinus Torvalds 				return;
16541da177e4SLinus Torvalds 			}
16551da177e4SLinus Torvalds 			goto ok;
16561da177e4SLinus Torvalds 		}
16571da177e4SLinus Torvalds 		if (net_ratelimit())
16581da177e4SLinus Torvalds 			printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
16591da177e4SLinus Torvalds 			       pinfo->prefix_len);
16601da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
16611da177e4SLinus Torvalds 		return;
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds ok:
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 		ifp = ipv6_get_ifaddr(&addr, dev, 1);
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 		if (ifp == NULL && valid_lft) {
16681da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
16711da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
16721da177e4SLinus Torvalds 			 */
16731da177e4SLinus Torvalds 			if (!max_addresses ||
16741da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
16751da177e4SLinus Torvalds 				ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
16761da177e4SLinus Torvalds 						    addr_type&IPV6_ADDR_SCOPE_MASK, 0);
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 			if (!ifp || IS_ERR(ifp)) {
16791da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
16801da177e4SLinus Torvalds 				return;
16811da177e4SLinus Torvalds 			}
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 			update_lft = create = 1;
16841da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
16851da177e4SLinus Torvalds 			addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
16861da177e4SLinus Torvalds 		}
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 		if (ifp) {
16891da177e4SLinus Torvalds 			int flags;
16901da177e4SLinus Torvalds 			unsigned long now;
16911da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
16921da177e4SLinus Torvalds 			struct inet6_ifaddr *ift;
16931da177e4SLinus Torvalds #endif
16941da177e4SLinus Torvalds 			u32 stored_lft;
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
16971da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
16981da177e4SLinus Torvalds 			now = jiffies;
16991da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
17001da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
17011da177e4SLinus Torvalds 			else
17021da177e4SLinus Torvalds 				stored_lft = 0;
17031da177e4SLinus Torvalds 			if (!update_lft && stored_lft) {
17041da177e4SLinus Torvalds 				if (valid_lft > MIN_VALID_LIFETIME ||
17051da177e4SLinus Torvalds 				    valid_lft > stored_lft)
17061da177e4SLinus Torvalds 					update_lft = 1;
17071da177e4SLinus Torvalds 				else if (stored_lft <= MIN_VALID_LIFETIME) {
17081da177e4SLinus Torvalds 					/* valid_lft <= stored_lft is always true */
17091da177e4SLinus Torvalds 					/* XXX: IPsec */
17101da177e4SLinus Torvalds 					update_lft = 0;
17111da177e4SLinus Torvalds 				} else {
17121da177e4SLinus Torvalds 					valid_lft = MIN_VALID_LIFETIME;
17131da177e4SLinus Torvalds 					if (valid_lft < prefered_lft)
17141da177e4SLinus Torvalds 						prefered_lft = valid_lft;
17151da177e4SLinus Torvalds 					update_lft = 1;
17161da177e4SLinus Torvalds 				}
17171da177e4SLinus Torvalds 			}
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 			if (update_lft) {
17201da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
17211da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
17221da177e4SLinus Torvalds 				ifp->tstamp = now;
17231da177e4SLinus Torvalds 				flags = ifp->flags;
17241da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
17251da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
17281da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
17291da177e4SLinus Torvalds 			} else
17301da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
17331da177e4SLinus Torvalds 			read_lock_bh(&in6_dev->lock);
17341da177e4SLinus Torvalds 			/* update all temporary addresses in the list */
17351da177e4SLinus Torvalds 			for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
17361da177e4SLinus Torvalds 				/*
17371da177e4SLinus Torvalds 				 * When adjusting the lifetimes of an existing
17381da177e4SLinus Torvalds 				 * temporary address, only lower the lifetimes.
17391da177e4SLinus Torvalds 				 * Implementations must not increase the
17401da177e4SLinus Torvalds 				 * lifetimes of an existing temporary address
17411da177e4SLinus Torvalds 				 * when processing a Prefix Information Option.
17421da177e4SLinus Torvalds 				 */
17431da177e4SLinus Torvalds 				spin_lock(&ift->lock);
17441da177e4SLinus Torvalds 				flags = ift->flags;
17451da177e4SLinus Torvalds 				if (ift->valid_lft > valid_lft &&
17461da177e4SLinus Torvalds 				    ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
17471da177e4SLinus Torvalds 					ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
17481da177e4SLinus Torvalds 				if (ift->prefered_lft > prefered_lft &&
17491da177e4SLinus Torvalds 				    ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
17501da177e4SLinus Torvalds 					ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
17511da177e4SLinus Torvalds 				spin_unlock(&ift->lock);
17521da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
17531da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ift);
17541da177e4SLinus Torvalds 			}
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 			if (create && in6_dev->cnf.use_tempaddr > 0) {
17571da177e4SLinus Torvalds 				/*
17581da177e4SLinus Torvalds 				 * When a new public address is created as described in [ADDRCONF],
17591da177e4SLinus Torvalds 				 * also create a new temporary address.
17601da177e4SLinus Torvalds 				 */
17611da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
17621da177e4SLinus Torvalds 				ipv6_create_tempaddr(ifp, NULL);
17631da177e4SLinus Torvalds 			} else {
17641da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
17651da177e4SLinus Torvalds 			}
17661da177e4SLinus Torvalds #endif
17671da177e4SLinus Torvalds 			in6_ifa_put(ifp);
17681da177e4SLinus Torvalds 			addrconf_verify(0);
17691da177e4SLinus Torvalds 		}
17701da177e4SLinus Torvalds 	}
17711da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
17721da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
17731da177e4SLinus Torvalds }
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds /*
17761da177e4SLinus Torvalds  *	Set destination address.
17771da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
17781da177e4SLinus Torvalds  *	device.
17791da177e4SLinus Torvalds  */
17801da177e4SLinus Torvalds int addrconf_set_dstaddr(void __user *arg)
17811da177e4SLinus Torvalds {
17821da177e4SLinus Torvalds 	struct in6_ifreq ireq;
17831da177e4SLinus Torvalds 	struct net_device *dev;
17841da177e4SLinus Torvalds 	int err = -EINVAL;
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds 	rtnl_lock();
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	err = -EFAULT;
17891da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
17901da177e4SLinus Torvalds 		goto err_exit;
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	dev = __dev_get_by_index(ireq.ifr6_ifindex);
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 	err = -ENODEV;
17951da177e4SLinus Torvalds 	if (dev == NULL)
17961da177e4SLinus Torvalds 		goto err_exit;
17971da177e4SLinus Torvalds 
17980be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
17991da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
18001da177e4SLinus Torvalds 		struct ifreq ifr;
18011da177e4SLinus Torvalds 		mm_segment_t	oldfs;
18021da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
18051da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
18061da177e4SLinus Torvalds 			goto err_exit;
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
18091da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
18101da177e4SLinus Torvalds 		p.iph.saddr = 0;
18111da177e4SLinus Torvalds 		p.iph.version = 4;
18121da177e4SLinus Torvalds 		p.iph.ihl = 5;
18131da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
18141da177e4SLinus Torvalds 		p.iph.ttl = 64;
18151da177e4SLinus Torvalds 		ifr.ifr_ifru.ifru_data = (void __user *)&p;
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds 		oldfs = get_fs(); set_fs(KERNEL_DS);
18181da177e4SLinus Torvalds 		err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
18191da177e4SLinus Torvalds 		set_fs(oldfs);
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 		if (err == 0) {
18221da177e4SLinus Torvalds 			err = -ENOBUFS;
18231da177e4SLinus Torvalds 			if ((dev = __dev_get_by_name(p.name)) == NULL)
18241da177e4SLinus Torvalds 				goto err_exit;
18251da177e4SLinus Torvalds 			err = dev_open(dev);
18261da177e4SLinus Torvalds 		}
18271da177e4SLinus Torvalds 	}
18280be669bbSJoerg Roedel #endif
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds err_exit:
18311da177e4SLinus Torvalds 	rtnl_unlock();
18321da177e4SLinus Torvalds 	return err;
18331da177e4SLinus Torvalds }
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds /*
18361da177e4SLinus Torvalds  *	Manual configuration of address on an interface
18371da177e4SLinus Torvalds  */
18380778769dSNoriaki TAKAMIYA static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
183955ebaef1SNoriaki TAKAMIYA 			  __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
18401da177e4SLinus Torvalds {
18411da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
18421da177e4SLinus Torvalds 	struct inet6_dev *idev;
18431da177e4SLinus Torvalds 	struct net_device *dev;
18441da177e4SLinus Torvalds 	int scope;
184546d48046SYOSHIFUJI Hideaki 	u32 flags = RTF_EXPIRES;
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 	ASSERT_RTNL();
18481da177e4SLinus Torvalds 
18490778769dSNoriaki TAKAMIYA 	/* check the lifetime */
18500778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
18510778769dSNoriaki TAKAMIYA 		return -EINVAL;
18520778769dSNoriaki TAKAMIYA 
18531da177e4SLinus Torvalds 	if ((dev = __dev_get_by_index(ifindex)) == NULL)
18541da177e4SLinus Torvalds 		return -ENODEV;
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL)
18571da177e4SLinus Torvalds 		return -ENOBUFS;
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
18601da177e4SLinus Torvalds 
186146d48046SYOSHIFUJI Hideaki 	if (valid_lft == INFINITY_LIFE_TIME) {
18620778769dSNoriaki TAKAMIYA 		ifa_flags |= IFA_F_PERMANENT;
186346d48046SYOSHIFUJI Hideaki 		flags = 0;
186446d48046SYOSHIFUJI Hideaki 	} else if (valid_lft >= 0x7FFFFFFF/HZ)
18650778769dSNoriaki TAKAMIYA 		valid_lft = 0x7FFFFFFF/HZ;
18660778769dSNoriaki TAKAMIYA 
18670778769dSNoriaki TAKAMIYA 	if (prefered_lft == 0)
18680778769dSNoriaki TAKAMIYA 		ifa_flags |= IFA_F_DEPRECATED;
18690778769dSNoriaki TAKAMIYA 	else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
18700778769dSNoriaki TAKAMIYA 		 (prefered_lft != INFINITY_LIFE_TIME))
18710778769dSNoriaki TAKAMIYA 		prefered_lft = 0x7FFFFFFF/HZ;
18720778769dSNoriaki TAKAMIYA 
18730778769dSNoriaki TAKAMIYA 	ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
18740778769dSNoriaki TAKAMIYA 
18751da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
187606aebfb7SHerbert Xu 		spin_lock_bh(&ifp->lock);
18770778769dSNoriaki TAKAMIYA 		ifp->valid_lft = valid_lft;
18780778769dSNoriaki TAKAMIYA 		ifp->prefered_lft = prefered_lft;
18790778769dSNoriaki TAKAMIYA 		ifp->tstamp = jiffies;
188006aebfb7SHerbert Xu 		spin_unlock_bh(&ifp->lock);
18810778769dSNoriaki TAKAMIYA 
188246d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
188346d48046SYOSHIFUJI Hideaki 				      jiffies_to_clock_t(valid_lft * HZ), flags);
18841da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
18851da177e4SLinus Torvalds 		in6_ifa_put(ifp);
18860778769dSNoriaki TAKAMIYA 		addrconf_verify(0);
18871da177e4SLinus Torvalds 		return 0;
18881da177e4SLinus Torvalds 	}
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds 	return PTR_ERR(ifp);
18911da177e4SLinus Torvalds }
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
18941da177e4SLinus Torvalds {
18951da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
18961da177e4SLinus Torvalds 	struct inet6_dev *idev;
18971da177e4SLinus Torvalds 	struct net_device *dev;
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 	if ((dev = __dev_get_by_index(ifindex)) == NULL)
19001da177e4SLinus Torvalds 		return -ENODEV;
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL)
19031da177e4SLinus Torvalds 		return -ENXIO;
19041da177e4SLinus Torvalds 
19051da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
19061da177e4SLinus Torvalds 	for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
19071da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
19081da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
19091da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
19101da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 			/* If the last address is deleted administratively,
19151da177e4SLinus Torvalds 			   disable IPv6 on this interface.
19161da177e4SLinus Torvalds 			 */
19171da177e4SLinus Torvalds 			if (idev->addr_list == NULL)
19181da177e4SLinus Torvalds 				addrconf_ifdown(idev->dev, 1);
19191da177e4SLinus Torvalds 			return 0;
19201da177e4SLinus Torvalds 		}
19211da177e4SLinus Torvalds 	}
19221da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
19231da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
19241da177e4SLinus Torvalds }
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds int addrconf_add_ifaddr(void __user *arg)
19281da177e4SLinus Torvalds {
19291da177e4SLinus Torvalds 	struct in6_ifreq ireq;
19301da177e4SLinus Torvalds 	int err;
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
19331da177e4SLinus Torvalds 		return -EPERM;
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
19361da177e4SLinus Torvalds 		return -EFAULT;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	rtnl_lock();
19390778769dSNoriaki TAKAMIYA 	err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
194055ebaef1SNoriaki TAKAMIYA 			     IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
19411da177e4SLinus Torvalds 	rtnl_unlock();
19421da177e4SLinus Torvalds 	return err;
19431da177e4SLinus Torvalds }
19441da177e4SLinus Torvalds 
19451da177e4SLinus Torvalds int addrconf_del_ifaddr(void __user *arg)
19461da177e4SLinus Torvalds {
19471da177e4SLinus Torvalds 	struct in6_ifreq ireq;
19481da177e4SLinus Torvalds 	int err;
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
19511da177e4SLinus Torvalds 		return -EPERM;
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
19541da177e4SLinus Torvalds 		return -EFAULT;
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	rtnl_lock();
19571da177e4SLinus Torvalds 	err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
19581da177e4SLinus Torvalds 	rtnl_unlock();
19591da177e4SLinus Torvalds 	return err;
19601da177e4SLinus Torvalds }
19611da177e4SLinus Torvalds 
19620be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
19631da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
19641da177e4SLinus Torvalds {
19651da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
19661da177e4SLinus Torvalds 	struct in6_addr addr;
19671da177e4SLinus Torvalds 	struct net_device *dev;
19681da177e4SLinus Torvalds 	int scope;
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 	ASSERT_RTNL();
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
19731da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
19761da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
19771da177e4SLinus Torvalds 		scope = IFA_LINK;
19781da177e4SLinus Torvalds 	} else {
19791da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
19801da177e4SLinus Torvalds 	}
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
19831da177e4SLinus Torvalds 		ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
19841da177e4SLinus Torvalds 		if (!IS_ERR(ifp)) {
19851da177e4SLinus Torvalds 			spin_lock_bh(&ifp->lock);
19861da177e4SLinus Torvalds 			ifp->flags &= ~IFA_F_TENTATIVE;
19871da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
19881da177e4SLinus Torvalds 			ipv6_ifa_notify(RTM_NEWADDR, ifp);
19891da177e4SLinus Torvalds 			in6_ifa_put(ifp);
19901da177e4SLinus Torvalds 		}
19911da177e4SLinus Torvalds 		return;
19921da177e4SLinus Torvalds 	}
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	for (dev = dev_base; dev != NULL; dev = dev->next) {
1995e5ed6399SHerbert Xu 		struct in_device * in_dev = __in_dev_get_rtnl(dev);
19961da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
19971da177e4SLinus Torvalds 			struct in_ifaddr * ifa;
19981da177e4SLinus Torvalds 
19991da177e4SLinus Torvalds 			int flag = scope;
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
20021da177e4SLinus Torvalds 				int plen;
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
20071da177e4SLinus Torvalds 					continue;
20081da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
20091da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
20101da177e4SLinus Torvalds 						continue;
20111da177e4SLinus Torvalds 					flag |= IFA_HOST;
20121da177e4SLinus Torvalds 				}
20131da177e4SLinus Torvalds 				if (idev->dev->flags&IFF_POINTOPOINT)
20141da177e4SLinus Torvalds 					plen = 64;
20151da177e4SLinus Torvalds 				else
20161da177e4SLinus Torvalds 					plen = 96;
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 				ifp = ipv6_add_addr(idev, &addr, plen, flag,
20191da177e4SLinus Torvalds 						    IFA_F_PERMANENT);
20201da177e4SLinus Torvalds 				if (!IS_ERR(ifp)) {
20211da177e4SLinus Torvalds 					spin_lock_bh(&ifp->lock);
20221da177e4SLinus Torvalds 					ifp->flags &= ~IFA_F_TENTATIVE;
20231da177e4SLinus Torvalds 					spin_unlock_bh(&ifp->lock);
20241da177e4SLinus Torvalds 					ipv6_ifa_notify(RTM_NEWADDR, ifp);
20251da177e4SLinus Torvalds 					in6_ifa_put(ifp);
20261da177e4SLinus Torvalds 				}
20271da177e4SLinus Torvalds 			}
20281da177e4SLinus Torvalds 		}
20291da177e4SLinus Torvalds 	}
20301da177e4SLinus Torvalds }
20310be669bbSJoerg Roedel #endif
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
20341da177e4SLinus Torvalds {
20351da177e4SLinus Torvalds 	struct inet6_dev  *idev;
20361da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	/* ::1 */
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	ASSERT_RTNL();
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
20431da177e4SLinus Torvalds 		printk(KERN_DEBUG "init loopback: add_dev failed\n");
20441da177e4SLinus Torvalds 		return;
20451da177e4SLinus Torvalds 	}
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds 	ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
20481da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
20491da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
20501da177e4SLinus Torvalds 		ifp->flags &= ~IFA_F_TENTATIVE;
20511da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
20521da177e4SLinus Torvalds 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
20531da177e4SLinus Torvalds 		in6_ifa_put(ifp);
20541da177e4SLinus Torvalds 	}
20551da177e4SLinus Torvalds }
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
20581da177e4SLinus Torvalds {
20591da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 	ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
20621da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
206346d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
20641da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
20651da177e4SLinus Torvalds 		in6_ifa_put(ifp);
20661da177e4SLinus Torvalds 	}
20671da177e4SLinus Torvalds }
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
20701da177e4SLinus Torvalds {
20711da177e4SLinus Torvalds 	struct in6_addr addr;
20721da177e4SLinus Torvalds 	struct inet6_dev    * idev;
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 	ASSERT_RTNL();
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds 	if ((dev->type != ARPHRD_ETHER) &&
20771da177e4SLinus Torvalds 	    (dev->type != ARPHRD_FDDI) &&
20781da177e4SLinus Torvalds 	    (dev->type != ARPHRD_IEEE802_TR) &&
20791da177e4SLinus Torvalds 	    (dev->type != ARPHRD_ARCNET) &&
20801da177e4SLinus Torvalds 	    (dev->type != ARPHRD_INFINIBAND)) {
20811da177e4SLinus Torvalds 		/* Alas, we support only Ethernet autoconfiguration. */
20821da177e4SLinus Torvalds 		return;
20831da177e4SLinus Torvalds 	}
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
20861da177e4SLinus Torvalds 	if (idev == NULL)
20871da177e4SLinus Torvalds 		return;
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
20901da177e4SLinus Torvalds 	addr.s6_addr32[0] = htonl(0xFE800000);
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds 	if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
20931da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &addr);
20941da177e4SLinus Torvalds }
20951da177e4SLinus Torvalds 
20960be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
20971da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
20981da177e4SLinus Torvalds {
20991da177e4SLinus Torvalds 	struct inet6_dev *idev;
21001da177e4SLinus Torvalds 
21011da177e4SLinus Torvalds 	ASSERT_RTNL();
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 	/*
21041da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
21051da177e4SLinus Torvalds 	 * addresses... we should configure all of
21061da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
21071da177e4SLinus Torvalds 	 */
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
21101da177e4SLinus Torvalds 		printk(KERN_DEBUG "init sit: add_dev failed\n");
21111da177e4SLinus Torvalds 		return;
21121da177e4SLinus Torvalds 	}
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	if (dev->flags&IFF_POINTOPOINT) {
21171da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
21181da177e4SLinus Torvalds 		addrconf_add_lroute(dev);
21191da177e4SLinus Torvalds 	} else
21201da177e4SLinus Torvalds 		sit_route_add(dev);
21211da177e4SLinus Torvalds }
21220be669bbSJoerg Roedel #endif
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds static inline int
21251da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
21261da177e4SLinus Torvalds {
21271da177e4SLinus Torvalds 	struct in6_addr lladdr;
21281da177e4SLinus Torvalds 
21291da177e4SLinus Torvalds 	if (!ipv6_get_lladdr(link_dev, &lladdr)) {
21301da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &lladdr);
21311da177e4SLinus Torvalds 		return 0;
21321da177e4SLinus Torvalds 	}
21331da177e4SLinus Torvalds 	return -1;
21341da177e4SLinus Torvalds }
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
21371da177e4SLinus Torvalds {
21381da177e4SLinus Torvalds 	struct net_device *link_dev;
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	/* first try to inherit the link-local address from the link device */
21411da177e4SLinus Torvalds 	if (idev->dev->iflink &&
21421da177e4SLinus Torvalds 	    (link_dev = __dev_get_by_index(idev->dev->iflink))) {
21431da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
21441da177e4SLinus Torvalds 			return;
21451da177e4SLinus Torvalds 	}
21461da177e4SLinus Torvalds 	/* then try to inherit it from any device */
21471da177e4SLinus Torvalds 	for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
21481da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
21491da177e4SLinus Torvalds 			return;
21501da177e4SLinus Torvalds 	}
21511da177e4SLinus Torvalds 	printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
21521da177e4SLinus Torvalds }
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds /*
21551da177e4SLinus Torvalds  * Autoconfigure tunnel with a link-local address so routing protocols,
21561da177e4SLinus Torvalds  * DHCPv6, MLD etc. can be run over the virtual link
21571da177e4SLinus Torvalds  */
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev)
21601da177e4SLinus Torvalds {
21611da177e4SLinus Torvalds 	struct inet6_dev *idev;
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 	ASSERT_RTNL();
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL) {
21661da177e4SLinus Torvalds 		printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
21671da177e4SLinus Torvalds 		return;
21681da177e4SLinus Torvalds 	}
21691da177e4SLinus Torvalds 	ip6_tnl_add_linklocal(idev);
21701da177e4SLinus Torvalds }
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
21731da177e4SLinus Torvalds 			   void * data)
21741da177e4SLinus Torvalds {
21751da177e4SLinus Torvalds 	struct net_device *dev = (struct net_device *) data;
21761da177e4SLinus Torvalds 	struct inet6_dev *idev = __in6_dev_get(dev);
2177c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds 	switch(event) {
2180*45ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
2181*45ba9dd2SYOSHIFUJI Hideaki 		if (!idev) {
2182*45ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
2183*45ba9dd2SYOSHIFUJI Hideaki 			if (!idev)
2184*45ba9dd2SYOSHIFUJI Hideaki 				printk(KERN_WARNING "IPv6: add_dev failed for %s\n",
2185*45ba9dd2SYOSHIFUJI Hideaki 					dev->name);
2186*45ba9dd2SYOSHIFUJI Hideaki 		}
2187*45ba9dd2SYOSHIFUJI Hideaki 		break;
21881da177e4SLinus Torvalds 	case NETDEV_UP:
21893c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
21903c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
21913c21edbdSYOSHIFUJI Hideaki 			if (!netif_carrier_ok(dev)) {
21923c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
21933c21edbdSYOSHIFUJI Hideaki 				printk(KERN_INFO
21943c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_UP): %s: "
21953c21edbdSYOSHIFUJI Hideaki 					"link is not ready\n",
21963c21edbdSYOSHIFUJI Hideaki 					dev->name);
21973c21edbdSYOSHIFUJI Hideaki 				break;
21983c21edbdSYOSHIFUJI Hideaki 			}
219999081049SKristian Slavov 
220099081049SKristian Slavov 			if (idev)
220199081049SKristian Slavov 				idev->if_flags |= IF_READY;
22023c21edbdSYOSHIFUJI Hideaki 		} else {
22033c21edbdSYOSHIFUJI Hideaki 			if (!netif_carrier_ok(dev)) {
22043c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
22053c21edbdSYOSHIFUJI Hideaki 				break;
22063c21edbdSYOSHIFUJI Hideaki 			}
22073c21edbdSYOSHIFUJI Hideaki 
22083c21edbdSYOSHIFUJI Hideaki 			if (idev) {
22093c21edbdSYOSHIFUJI Hideaki 				if (idev->if_flags & IF_READY) {
22103c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
22113c21edbdSYOSHIFUJI Hideaki 					break;
22123c21edbdSYOSHIFUJI Hideaki 				}
22133c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
22143c21edbdSYOSHIFUJI Hideaki 			}
22153c21edbdSYOSHIFUJI Hideaki 
22163c21edbdSYOSHIFUJI Hideaki 			printk(KERN_INFO
22173c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_CHANGE): %s: "
22183c21edbdSYOSHIFUJI Hideaki 					"link becomes ready\n",
22193c21edbdSYOSHIFUJI Hideaki 					dev->name);
22203c21edbdSYOSHIFUJI Hideaki 
2221c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
22223c21edbdSYOSHIFUJI Hideaki 		}
22233c21edbdSYOSHIFUJI Hideaki 
22241da177e4SLinus Torvalds 		switch(dev->type) {
22250be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
22261da177e4SLinus Torvalds 		case ARPHRD_SIT:
22271da177e4SLinus Torvalds 			addrconf_sit_config(dev);
22281da177e4SLinus Torvalds 			break;
22290be669bbSJoerg Roedel #endif
22301da177e4SLinus Torvalds 		case ARPHRD_TUNNEL6:
22311da177e4SLinus Torvalds 			addrconf_ip6_tnl_config(dev);
22321da177e4SLinus Torvalds 			break;
22331da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
22341da177e4SLinus Torvalds 			init_loopback(dev);
22351da177e4SLinus Torvalds 			break;
22361da177e4SLinus Torvalds 
22371da177e4SLinus Torvalds 		default:
22381da177e4SLinus Torvalds 			addrconf_dev_config(dev);
22391da177e4SLinus Torvalds 			break;
22401da177e4SLinus Torvalds 		};
22411da177e4SLinus Torvalds 		if (idev) {
2242c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
2243c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
2244c5e33bddSYOSHIFUJI Hideaki 
22451da177e4SLinus Torvalds 			/* If the MTU changed during the interface down, when the
22461da177e4SLinus Torvalds 			   interface up, the changed MTU must be reflected in the
22471da177e4SLinus Torvalds 			   idev as well as routers.
22481da177e4SLinus Torvalds 			 */
22491da177e4SLinus Torvalds 			if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
22501da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
22511da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
22521da177e4SLinus Torvalds 			}
22531da177e4SLinus Torvalds 			idev->tstamp = jiffies;
22541da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
22551da177e4SLinus Torvalds 			/* If the changed mtu during down is lower than IPV6_MIN_MTU
22561da177e4SLinus Torvalds 			   stop IPv6 on this interface.
22571da177e4SLinus Torvalds 			 */
22581da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
22591da177e4SLinus Torvalds 				addrconf_ifdown(dev, event != NETDEV_DOWN);
22601da177e4SLinus Torvalds 		}
22611da177e4SLinus Torvalds 		break;
22621da177e4SLinus Torvalds 
22631da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
22641da177e4SLinus Torvalds 		if ( idev && dev->mtu >= IPV6_MIN_MTU) {
22651da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
22661da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
22671da177e4SLinus Torvalds 			break;
22681da177e4SLinus Torvalds 		}
22691da177e4SLinus Torvalds 
22701da177e4SLinus Torvalds 		/* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 	case NETDEV_DOWN:
22731da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
22741da177e4SLinus Torvalds 		/*
22751da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
22761da177e4SLinus Torvalds 		 */
22771da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
22781da177e4SLinus Torvalds 		break;
22793c21edbdSYOSHIFUJI Hideaki 
22801da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
22811da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
22821da177e4SLinus Torvalds 		if (idev) {
22831da177e4SLinus Torvalds 			addrconf_sysctl_unregister(&idev->cnf);
22841da177e4SLinus Torvalds 			neigh_sysctl_unregister(idev->nd_parms);
22851da177e4SLinus Torvalds 			neigh_sysctl_register(dev, idev->nd_parms,
22861da177e4SLinus Torvalds 					      NET_IPV6, NET_IPV6_NEIGH, "ipv6",
22871da177e4SLinus Torvalds 					      &ndisc_ifinfo_sysctl_change,
22881da177e4SLinus Torvalds 					      NULL);
22891da177e4SLinus Torvalds 			addrconf_sysctl_register(idev, &idev->cnf);
22901da177e4SLinus Torvalds 		}
22911da177e4SLinus Torvalds #endif
22921da177e4SLinus Torvalds 		break;
22931da177e4SLinus Torvalds 	};
22941da177e4SLinus Torvalds 
22951da177e4SLinus Torvalds 	return NOTIFY_OK;
22961da177e4SLinus Torvalds }
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds /*
22991da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
23001da177e4SLinus Torvalds  */
23011da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
23021da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
23031da177e4SLinus Torvalds 	.priority = 0
23041da177e4SLinus Torvalds };
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
23071da177e4SLinus Torvalds {
23081da177e4SLinus Torvalds 	struct inet6_dev *idev;
23091da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **bifa;
23101da177e4SLinus Torvalds 	int i;
23111da177e4SLinus Torvalds 
23121da177e4SLinus Torvalds 	ASSERT_RTNL();
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds 	if (dev == &loopback_dev && how == 1)
23151da177e4SLinus Torvalds 		how = 0;
23161da177e4SLinus Torvalds 
23171da177e4SLinus Torvalds 	rt6_ifdown(dev);
23181da177e4SLinus Torvalds 	neigh_ifdown(&nd_tbl, dev);
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
23211da177e4SLinus Torvalds 	if (idev == NULL)
23221da177e4SLinus Torvalds 		return -ENODEV;
23231da177e4SLinus Torvalds 
23241da177e4SLinus Torvalds 	/* Step 1: remove reference to ipv6 device from parent device.
23251da177e4SLinus Torvalds 		   Do not dev_put!
23261da177e4SLinus Torvalds 	 */
23271da177e4SLinus Torvalds 	if (how == 1) {
23281da177e4SLinus Torvalds 		idev->dead = 1;
23298814c4b5SYOSHIFUJI Hideaki 
23308814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
23318814c4b5SYOSHIFUJI Hideaki 		rcu_assign_pointer(dev->ip6_ptr, NULL);
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
23341da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds 	}
23371da177e4SLinus Torvalds 
23381da177e4SLinus Torvalds 	/* Step 2: clear hash table */
23391da177e4SLinus Torvalds 	for (i=0; i<IN6_ADDR_HSIZE; i++) {
23401da177e4SLinus Torvalds 		bifa = &inet6_addr_lst[i];
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds 		write_lock_bh(&addrconf_hash_lock);
23431da177e4SLinus Torvalds 		while ((ifa = *bifa) != NULL) {
23441da177e4SLinus Torvalds 			if (ifa->idev == idev) {
23451da177e4SLinus Torvalds 				*bifa = ifa->lst_next;
23461da177e4SLinus Torvalds 				ifa->lst_next = NULL;
23471da177e4SLinus Torvalds 				addrconf_del_timer(ifa);
23481da177e4SLinus Torvalds 				in6_ifa_put(ifa);
23491da177e4SLinus Torvalds 				continue;
23501da177e4SLinus Torvalds 			}
23511da177e4SLinus Torvalds 			bifa = &ifa->lst_next;
23521da177e4SLinus Torvalds 		}
23531da177e4SLinus Torvalds 		write_unlock_bh(&addrconf_hash_lock);
23541da177e4SLinus Torvalds 	}
23551da177e4SLinus Torvalds 
23561da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 	/* Step 3: clear flags for stateless addrconf */
23591da177e4SLinus Torvalds 	if (how != 1)
23603c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 	/* Step 4: clear address list */
23631da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
23641da177e4SLinus Torvalds 	if (how == 1 && del_timer(&idev->regen_timer))
23651da177e4SLinus Torvalds 		in6_dev_put(idev);
23661da177e4SLinus Torvalds 
23671da177e4SLinus Torvalds 	/* clear tempaddr list */
23681da177e4SLinus Torvalds 	while ((ifa = idev->tempaddr_list) != NULL) {
23691da177e4SLinus Torvalds 		idev->tempaddr_list = ifa->tmp_next;
23701da177e4SLinus Torvalds 		ifa->tmp_next = NULL;
23711da177e4SLinus Torvalds 		ifa->dead = 1;
23721da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
23731da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
23741da177e4SLinus Torvalds 
23751da177e4SLinus Torvalds 		if (ifa->ifpub) {
23761da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
23771da177e4SLinus Torvalds 			ifa->ifpub = NULL;
23781da177e4SLinus Torvalds 		}
23791da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
23801da177e4SLinus Torvalds 		in6_ifa_put(ifa);
23811da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
23821da177e4SLinus Torvalds 	}
23831da177e4SLinus Torvalds #endif
23841da177e4SLinus Torvalds 	while ((ifa = idev->addr_list) != NULL) {
23851da177e4SLinus Torvalds 		idev->addr_list = ifa->if_next;
23861da177e4SLinus Torvalds 		ifa->if_next = NULL;
23871da177e4SLinus Torvalds 		ifa->dead = 1;
23881da177e4SLinus Torvalds 		addrconf_del_timer(ifa);
23891da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 		__ipv6_ifa_notify(RTM_DELADDR, ifa);
23921da177e4SLinus Torvalds 		in6_ifa_put(ifa);
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
23951da177e4SLinus Torvalds 	}
23961da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	/* Step 5: Discard multicast list */
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds 	if (how == 1)
24011da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
24021da177e4SLinus Torvalds 	else
24031da177e4SLinus Torvalds 		ipv6_mc_down(idev);
24041da177e4SLinus Torvalds 
24051da177e4SLinus Torvalds 	/* Step 5: netlink notification of this interface */
24061da177e4SLinus Torvalds 	idev->tstamp = jiffies;
2407979ad663SYan Zheng 	inet6_ifinfo_notify(RTM_DELLINK, idev);
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds 	/* Shot the device (if unregistered) */
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds 	if (how == 1) {
24121da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
24131da177e4SLinus Torvalds 		addrconf_sysctl_unregister(&idev->cnf);
24141da177e4SLinus Torvalds 		neigh_sysctl_unregister(idev->nd_parms);
24151da177e4SLinus Torvalds #endif
24161da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
24171da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
24181da177e4SLinus Torvalds 		in6_dev_put(idev);
24191da177e4SLinus Torvalds 	}
24201da177e4SLinus Torvalds 	return 0;
24211da177e4SLinus Torvalds }
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
24241da177e4SLinus Torvalds {
24251da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding)
24281da177e4SLinus Torvalds 		goto out;
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 	if (ifp->idev->if_flags & IF_RA_RCVD) {
24311da177e4SLinus Torvalds 		/*
24321da177e4SLinus Torvalds 		 *	Announcement received after solicitation
24331da177e4SLinus Torvalds 		 *	was sent
24341da177e4SLinus Torvalds 		 */
24351da177e4SLinus Torvalds 		goto out;
24361da177e4SLinus Torvalds 	}
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds 	spin_lock(&ifp->lock);
24391da177e4SLinus Torvalds 	if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
24401da177e4SLinus Torvalds 		struct in6_addr all_routers;
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
24431da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS,
24441da177e4SLinus Torvalds 				   (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
24451da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_delay :
24461da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_interval);
24471da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds 		ipv6_addr_all_routers(&all_routers);
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
24521da177e4SLinus Torvalds 	} else {
24531da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
24541da177e4SLinus Torvalds 		/*
24551da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
24561da177e4SLinus Torvalds 		 * assumption any longer.
24571da177e4SLinus Torvalds 		 */
24581da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: no IPv6 routers present\n",
24591da177e4SLinus Torvalds 		       ifp->idev->dev->name);
24601da177e4SLinus Torvalds 	}
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds out:
24631da177e4SLinus Torvalds 	in6_ifa_put(ifp);
24641da177e4SLinus Torvalds }
24651da177e4SLinus Torvalds 
24661da177e4SLinus Torvalds /*
24671da177e4SLinus Torvalds  *	Duplicate Address Detection
24681da177e4SLinus Torvalds  */
24693c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
24703c21edbdSYOSHIFUJI Hideaki {
24713c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
24723c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
24733c21edbdSYOSHIFUJI Hideaki 
24743c21edbdSYOSHIFUJI Hideaki 	rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
24753c21edbdSYOSHIFUJI Hideaki 	ifp->probes = idev->cnf.dad_transmits;
24763c21edbdSYOSHIFUJI Hideaki 	addrconf_mod_timer(ifp, AC_DAD, rand_num);
24773c21edbdSYOSHIFUJI Hideaki }
24783c21edbdSYOSHIFUJI Hideaki 
2479e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
24801da177e4SLinus Torvalds {
24811da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
24821da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
24831da177e4SLinus Torvalds 
24841da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds 	net_srandom(ifp->addr.s6_addr32[3]);
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
24891da177e4SLinus Torvalds 	if (ifp->dead)
24901da177e4SLinus Torvalds 		goto out;
24911da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
249455ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
249555ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
24961da177e4SLinus Torvalds 		ifp->flags &= ~IFA_F_TENTATIVE;
24971da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
24981da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
24991da177e4SLinus Torvalds 
25001da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
25011da177e4SLinus Torvalds 		return;
25021da177e4SLinus Torvalds 	}
25031da177e4SLinus Torvalds 
25046732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
25053dd3bf83SYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
25066732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
25073c21edbdSYOSHIFUJI Hideaki 		/*
25083c21edbdSYOSHIFUJI Hideaki 		 * If the defice is not ready:
25093c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
25103c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
25113c21edbdSYOSHIFUJI Hideaki 		 */
25123c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
25133c21edbdSYOSHIFUJI Hideaki 		addrconf_dad_stop(ifp);
25146732badeSYOSHIFUJI Hideaki 		return;
25153c21edbdSYOSHIFUJI Hideaki 	}
25166732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
25176732badeSYOSHIFUJI Hideaki 	spin_unlock_bh(&ifp->lock);
25181da177e4SLinus Torvalds out:
25191da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
25201da177e4SLinus Torvalds }
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data)
25231da177e4SLinus Torvalds {
25241da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
25251da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
25261da177e4SLinus Torvalds 	struct in6_addr unspec;
25271da177e4SLinus Torvalds 	struct in6_addr mcaddr;
25281da177e4SLinus Torvalds 
25291da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
25301da177e4SLinus Torvalds 	if (idev->dead) {
25311da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
25321da177e4SLinus Torvalds 		goto out;
25331da177e4SLinus Torvalds 	}
25341da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
25351da177e4SLinus Torvalds 	if (ifp->probes == 0) {
25361da177e4SLinus Torvalds 		/*
25371da177e4SLinus Torvalds 		 * DAD was successful
25381da177e4SLinus Torvalds 		 */
25391da177e4SLinus Torvalds 
25401da177e4SLinus Torvalds 		ifp->flags &= ~IFA_F_TENTATIVE;
25411da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
25421da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
25431da177e4SLinus Torvalds 
25441da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
25451da177e4SLinus Torvalds 
25461da177e4SLinus Torvalds 		goto out;
25471da177e4SLinus Torvalds 	}
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 	ifp->probes--;
25501da177e4SLinus Torvalds 	addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
25511da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
25521da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
25551da177e4SLinus Torvalds 	memset(&unspec, 0, sizeof(unspec));
25561da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
25571da177e4SLinus Torvalds 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
25581da177e4SLinus Torvalds out:
25591da177e4SLinus Torvalds 	in6_ifa_put(ifp);
25601da177e4SLinus Torvalds }
25611da177e4SLinus Torvalds 
25621da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
25631da177e4SLinus Torvalds {
25641da177e4SLinus Torvalds 	struct net_device *	dev = ifp->idev->dev;
25651da177e4SLinus Torvalds 
25661da177e4SLinus Torvalds 	/*
25671da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
25681da177e4SLinus Torvalds 	 */
25691da177e4SLinus Torvalds 
25701da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds 	/* If added prefix is link local and forwarding is off,
25731da177e4SLinus Torvalds 	   start sending router solicitations.
25741da177e4SLinus Torvalds 	 */
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding == 0 &&
25771da177e4SLinus Torvalds 	    ifp->idev->cnf.rtr_solicits > 0 &&
25781da177e4SLinus Torvalds 	    (dev->flags&IFF_LOOPBACK) == 0 &&
25791da177e4SLinus Torvalds 	    (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
25801da177e4SLinus Torvalds 		struct in6_addr all_routers;
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 		ipv6_addr_all_routers(&all_routers);
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds 		/*
25851da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
25861da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
25871da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
25881da177e4SLinus Torvalds 		 */
25891da177e4SLinus Torvalds 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
25921da177e4SLinus Torvalds 		ifp->probes = 1;
25931da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
25941da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
25951da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
25961da177e4SLinus Torvalds 	}
25971da177e4SLinus Torvalds }
25981da177e4SLinus Torvalds 
2599c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev) {
2600c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
2601c5e33bddSYOSHIFUJI Hideaki 
2602c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
2603c5e33bddSYOSHIFUJI Hideaki 	for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2604c5e33bddSYOSHIFUJI Hideaki 		spin_lock_bh(&ifp->lock);
2605c5e33bddSYOSHIFUJI Hideaki 		if (!(ifp->flags & IFA_F_TENTATIVE)) {
2606c5e33bddSYOSHIFUJI Hideaki 			spin_unlock_bh(&ifp->lock);
2607c5e33bddSYOSHIFUJI Hideaki 			continue;
2608c5e33bddSYOSHIFUJI Hideaki 		}
2609c5e33bddSYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
2610c5e33bddSYOSHIFUJI Hideaki 		addrconf_dad_kick(ifp);
2611c5e33bddSYOSHIFUJI Hideaki 	}
2612c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
2613c5e33bddSYOSHIFUJI Hideaki }
2614c5e33bddSYOSHIFUJI Hideaki 
26151da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
26161da177e4SLinus Torvalds struct if6_iter_state {
26171da177e4SLinus Torvalds 	int bucket;
26181da177e4SLinus Torvalds };
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
26211da177e4SLinus Torvalds {
26221da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
26231da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds 	for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
26261da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
26271da177e4SLinus Torvalds 		if (ifa)
26281da177e4SLinus Torvalds 			break;
26291da177e4SLinus Torvalds 	}
26301da177e4SLinus Torvalds 	return ifa;
26311da177e4SLinus Torvalds }
26321da177e4SLinus Torvalds 
26331da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
26341da177e4SLinus Torvalds {
26351da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
26361da177e4SLinus Torvalds 
26371da177e4SLinus Torvalds 	ifa = ifa->lst_next;
26381da177e4SLinus Torvalds try_again:
26391da177e4SLinus Torvalds 	if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
26401da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
26411da177e4SLinus Torvalds 		goto try_again;
26421da177e4SLinus Torvalds 	}
26431da177e4SLinus Torvalds 	return ifa;
26441da177e4SLinus Torvalds }
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
26471da177e4SLinus Torvalds {
26481da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = if6_get_first(seq);
26491da177e4SLinus Torvalds 
26501da177e4SLinus Torvalds 	if (ifa)
26511da177e4SLinus Torvalds 		while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
26521da177e4SLinus Torvalds 			--pos;
26531da177e4SLinus Torvalds 	return pos ? NULL : ifa;
26541da177e4SLinus Torvalds }
26551da177e4SLinus Torvalds 
26561da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
26571da177e4SLinus Torvalds {
26581da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
26591da177e4SLinus Torvalds 	return if6_get_idx(seq, *pos);
26601da177e4SLinus Torvalds }
26611da177e4SLinus Torvalds 
26621da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
26631da177e4SLinus Torvalds {
26641da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
26671da177e4SLinus Torvalds 	++*pos;
26681da177e4SLinus Torvalds 	return ifa;
26691da177e4SLinus Torvalds }
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
26721da177e4SLinus Torvalds {
26731da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
26741da177e4SLinus Torvalds }
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
26771da177e4SLinus Torvalds {
26781da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
26791da177e4SLinus Torvalds 	seq_printf(seq,
26809343e79aSYOSHIFUJI Hideaki 		   NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
26811da177e4SLinus Torvalds 		   NIP6(ifp->addr),
26821da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
26831da177e4SLinus Torvalds 		   ifp->prefix_len,
26841da177e4SLinus Torvalds 		   ifp->scope,
26851da177e4SLinus Torvalds 		   ifp->flags,
26861da177e4SLinus Torvalds 		   ifp->idev->dev->name);
26871da177e4SLinus Torvalds 	return 0;
26881da177e4SLinus Torvalds }
26891da177e4SLinus Torvalds 
26901da177e4SLinus Torvalds static struct seq_operations if6_seq_ops = {
26911da177e4SLinus Torvalds 	.start	= if6_seq_start,
26921da177e4SLinus Torvalds 	.next	= if6_seq_next,
26931da177e4SLinus Torvalds 	.show	= if6_seq_show,
26941da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
26951da177e4SLinus Torvalds };
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
26981da177e4SLinus Torvalds {
26991da177e4SLinus Torvalds 	struct seq_file *seq;
27001da177e4SLinus Torvalds 	int rc = -ENOMEM;
2701322f74a4SIngo Oeser 	struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds 	if (!s)
27041da177e4SLinus Torvalds 		goto out;
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds 	rc = seq_open(file, &if6_seq_ops);
27071da177e4SLinus Torvalds 	if (rc)
27081da177e4SLinus Torvalds 		goto out_kfree;
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds 	seq = file->private_data;
27111da177e4SLinus Torvalds 	seq->private = s;
27121da177e4SLinus Torvalds out:
27131da177e4SLinus Torvalds 	return rc;
27141da177e4SLinus Torvalds out_kfree:
27151da177e4SLinus Torvalds 	kfree(s);
27161da177e4SLinus Torvalds 	goto out;
27171da177e4SLinus Torvalds }
27181da177e4SLinus Torvalds 
27199a32144eSArjan van de Ven static const struct file_operations if6_fops = {
27201da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
27211da177e4SLinus Torvalds 	.open		= if6_seq_open,
27221da177e4SLinus Torvalds 	.read		= seq_read,
27231da177e4SLinus Torvalds 	.llseek		= seq_lseek,
27241da177e4SLinus Torvalds 	.release	= seq_release_private,
27251da177e4SLinus Torvalds };
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds int __init if6_proc_init(void)
27281da177e4SLinus Torvalds {
27291da177e4SLinus Torvalds 	if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
27301da177e4SLinus Torvalds 		return -ENOMEM;
27311da177e4SLinus Torvalds 	return 0;
27321da177e4SLinus Torvalds }
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds void if6_proc_exit(void)
27351da177e4SLinus Torvalds {
27361da177e4SLinus Torvalds 	proc_net_remove("if_inet6");
27371da177e4SLinus Torvalds }
27381da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27391da177e4SLinus Torvalds 
27403b9f9a1cSNoriaki TAKAMIYA #ifdef CONFIG_IPV6_MIP6
27413b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
27423b9f9a1cSNoriaki TAKAMIYA int ipv6_chk_home_addr(struct in6_addr *addr)
27433b9f9a1cSNoriaki TAKAMIYA {
27443b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
27453b9f9a1cSNoriaki TAKAMIYA 	struct inet6_ifaddr * ifp;
27463b9f9a1cSNoriaki TAKAMIYA 	u8 hash = ipv6_addr_hash(addr);
27473b9f9a1cSNoriaki TAKAMIYA 	read_lock_bh(&addrconf_hash_lock);
27483b9f9a1cSNoriaki TAKAMIYA 	for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
27493b9f9a1cSNoriaki TAKAMIYA 		if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
27503b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
27513b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
27523b9f9a1cSNoriaki TAKAMIYA 			break;
27533b9f9a1cSNoriaki TAKAMIYA 		}
27543b9f9a1cSNoriaki TAKAMIYA 	}
27553b9f9a1cSNoriaki TAKAMIYA 	read_unlock_bh(&addrconf_hash_lock);
27563b9f9a1cSNoriaki TAKAMIYA 	return ret;
27573b9f9a1cSNoriaki TAKAMIYA }
27583b9f9a1cSNoriaki TAKAMIYA #endif
27593b9f9a1cSNoriaki TAKAMIYA 
27601da177e4SLinus Torvalds /*
27611da177e4SLinus Torvalds  *	Periodic address status verification
27621da177e4SLinus Torvalds  */
27631da177e4SLinus Torvalds 
27641da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo)
27651da177e4SLinus Torvalds {
27661da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
27671da177e4SLinus Torvalds 	unsigned long now, next;
27681da177e4SLinus Torvalds 	int i;
27691da177e4SLinus Torvalds 
27701da177e4SLinus Torvalds 	spin_lock_bh(&addrconf_verify_lock);
27711da177e4SLinus Torvalds 	now = jiffies;
27721da177e4SLinus Torvalds 	next = now + ADDR_CHECK_FREQUENCY;
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
27751da177e4SLinus Torvalds 
27761da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds restart:
27795d5780dfSYan Zheng 		read_lock(&addrconf_hash_lock);
27801da177e4SLinus Torvalds 		for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
27811da177e4SLinus Torvalds 			unsigned long age;
27821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
27831da177e4SLinus Torvalds 			unsigned long regen_advance;
27841da177e4SLinus Torvalds #endif
27851da177e4SLinus Torvalds 
27861da177e4SLinus Torvalds 			if (ifp->flags & IFA_F_PERMANENT)
27871da177e4SLinus Torvalds 				continue;
27881da177e4SLinus Torvalds 
27891da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
27901da177e4SLinus Torvalds 			age = (now - ifp->tstamp) / HZ;
27911da177e4SLinus Torvalds 
27921da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
27931da177e4SLinus Torvalds 			regen_advance = ifp->idev->cnf.regen_max_retry *
27941da177e4SLinus Torvalds 					ifp->idev->cnf.dad_transmits *
27951da177e4SLinus Torvalds 					ifp->idev->nd_parms->retrans_time / HZ;
27961da177e4SLinus Torvalds #endif
27971da177e4SLinus Torvalds 
27988f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
27998f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
28001da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
28011da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
28025d5780dfSYan Zheng 				read_unlock(&addrconf_hash_lock);
28031da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
28041da177e4SLinus Torvalds 				goto restart;
28058f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
28068f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
28078f27ebb9SYOSHIFUJI Hideaki 				continue;
28081da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
28091da177e4SLinus Torvalds 				/* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
28101da177e4SLinus Torvalds 				int deprecate = 0;
28111da177e4SLinus Torvalds 
28121da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
28131da177e4SLinus Torvalds 					deprecate = 1;
28141da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
28151da177e4SLinus Torvalds 				}
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
28181da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
28191da177e4SLinus Torvalds 
28201da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
28211da177e4SLinus Torvalds 
28221da177e4SLinus Torvalds 				if (deprecate) {
28231da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
28245d5780dfSYan Zheng 					read_unlock(&addrconf_hash_lock);
28251da177e4SLinus Torvalds 
28261da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
28271da177e4SLinus Torvalds 					in6_ifa_put(ifp);
28281da177e4SLinus Torvalds 					goto restart;
28291da177e4SLinus Torvalds 				}
28301da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
28311da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
28321da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
28331da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
28341da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
28351da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
28361da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
28371da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
28381da177e4SLinus Torvalds 						ifp->regen_count++;
28391da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
28401da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
28411da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
28425d5780dfSYan Zheng 						read_unlock(&addrconf_hash_lock);
2843291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
2844291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
2845291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
28461da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
28471da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
28481da177e4SLinus Torvalds 						in6_ifa_put(ifp);
28491da177e4SLinus Torvalds 						goto restart;
28501da177e4SLinus Torvalds 					}
28511da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
28521da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
28531da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
28541da177e4SLinus Torvalds #endif
28551da177e4SLinus Torvalds 			} else {
28561da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
28571da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
28581da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
28591da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
28601da177e4SLinus Torvalds 			}
28611da177e4SLinus Torvalds 		}
28625d5780dfSYan Zheng 		read_unlock(&addrconf_hash_lock);
28631da177e4SLinus Torvalds 	}
28641da177e4SLinus Torvalds 
28651da177e4SLinus Torvalds 	addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
28661da177e4SLinus Torvalds 	add_timer(&addr_chk_timer);
28671da177e4SLinus Torvalds 	spin_unlock_bh(&addrconf_verify_lock);
28681da177e4SLinus Torvalds }
28691da177e4SLinus Torvalds 
2870461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2871461d8837SThomas Graf {
2872461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
2873461d8837SThomas Graf 
2874461d8837SThomas Graf 	if (addr)
2875461d8837SThomas Graf 		pfx = nla_data(addr);
2876461d8837SThomas Graf 
2877461d8837SThomas Graf 	if (local) {
2878461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2879461d8837SThomas Graf 			pfx = NULL;
2880461d8837SThomas Graf 		else
2881461d8837SThomas Graf 			pfx = nla_data(local);
2882461d8837SThomas Graf 	}
2883461d8837SThomas Graf 
2884461d8837SThomas Graf 	return pfx;
2885461d8837SThomas Graf }
2886461d8837SThomas Graf 
2887461d8837SThomas Graf static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = {
2888461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
2889461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
2890461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
2891461d8837SThomas Graf };
2892461d8837SThomas Graf 
28931da177e4SLinus Torvalds static int
28941da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
28951da177e4SLinus Torvalds {
2896b933f716SThomas Graf 	struct ifaddrmsg *ifm;
2897b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
28981da177e4SLinus Torvalds 	struct in6_addr *pfx;
2899b933f716SThomas Graf 	int err;
29001da177e4SLinus Torvalds 
2901b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2902b933f716SThomas Graf 	if (err < 0)
2903b933f716SThomas Graf 		return err;
2904b933f716SThomas Graf 
2905b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
2906b933f716SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
29071da177e4SLinus Torvalds 	if (pfx == NULL)
29081da177e4SLinus Torvalds 		return -EINVAL;
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds 	return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
29111da177e4SLinus Torvalds }
29121da177e4SLinus Torvalds 
291355ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
291455ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
2915081bba5bSNoriaki TAKAMIYA {
291646d48046SYOSHIFUJI Hideaki 	u32 flags = RTF_EXPIRES;
291746d48046SYOSHIFUJI Hideaki 
2918081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
2919081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
2920081bba5bSNoriaki TAKAMIYA 
292146d48046SYOSHIFUJI Hideaki 	if (valid_lft == INFINITY_LIFE_TIME) {
292255ebaef1SNoriaki TAKAMIYA 		ifa_flags |= IFA_F_PERMANENT;
292346d48046SYOSHIFUJI Hideaki 		flags = 0;
292446d48046SYOSHIFUJI Hideaki 	} else if (valid_lft >= 0x7FFFFFFF/HZ)
2925081bba5bSNoriaki TAKAMIYA 		valid_lft = 0x7FFFFFFF/HZ;
2926081bba5bSNoriaki TAKAMIYA 
2927081bba5bSNoriaki TAKAMIYA 	if (prefered_lft == 0)
292855ebaef1SNoriaki TAKAMIYA 		ifa_flags |= IFA_F_DEPRECATED;
2929081bba5bSNoriaki TAKAMIYA 	else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
2930081bba5bSNoriaki TAKAMIYA 		 (prefered_lft != INFINITY_LIFE_TIME))
2931081bba5bSNoriaki TAKAMIYA 		prefered_lft = 0x7FFFFFFF/HZ;
2932081bba5bSNoriaki TAKAMIYA 
2933081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
29343b9f9a1cSNoriaki TAKAMIYA 	ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
2935081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
2936081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
2937081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
2938081bba5bSNoriaki TAKAMIYA 
2939081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
2940081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
2941081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
2942081bba5bSNoriaki TAKAMIYA 
294346d48046SYOSHIFUJI Hideaki 	addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
294446d48046SYOSHIFUJI Hideaki 			      jiffies_to_clock_t(valid_lft * HZ), flags);
2945081bba5bSNoriaki TAKAMIYA 	addrconf_verify(0);
2946081bba5bSNoriaki TAKAMIYA 
2947081bba5bSNoriaki TAKAMIYA 	return 0;
2948081bba5bSNoriaki TAKAMIYA }
2949081bba5bSNoriaki TAKAMIYA 
2950081bba5bSNoriaki TAKAMIYA static int
29511da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
29521da177e4SLinus Torvalds {
2953461d8837SThomas Graf 	struct ifaddrmsg *ifm;
2954461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
29551da177e4SLinus Torvalds 	struct in6_addr *pfx;
29567198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
29577198f8ceSThomas Graf 	struct net_device *dev;
295855ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
295955ebaef1SNoriaki TAKAMIYA 	u8 ifa_flags;
2960461d8837SThomas Graf 	int err;
29611da177e4SLinus Torvalds 
2962461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2963461d8837SThomas Graf 	if (err < 0)
2964461d8837SThomas Graf 		return err;
2965461d8837SThomas Graf 
2966461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
2967461d8837SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
29681da177e4SLinus Torvalds 	if (pfx == NULL)
29691da177e4SLinus Torvalds 		return -EINVAL;
29701da177e4SLinus Torvalds 
2971461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
29720778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
2973461d8837SThomas Graf 
2974461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
29750778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
2976461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
2977461d8837SThomas Graf 	} else {
2978461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
2979461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
29800778769dSNoriaki TAKAMIYA 	}
29810778769dSNoriaki TAKAMIYA 
29827198f8ceSThomas Graf 	dev =  __dev_get_by_index(ifm->ifa_index);
29837198f8ceSThomas Graf 	if (dev == NULL)
29847198f8ceSThomas Graf 		return -ENODEV;
2985081bba5bSNoriaki TAKAMIYA 
298655ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
29873b9f9a1cSNoriaki TAKAMIYA 	ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
298855ebaef1SNoriaki TAKAMIYA 
29897198f8ceSThomas Graf 	ifa = ipv6_get_ifaddr(pfx, dev, 1);
29907198f8ceSThomas Graf 	if (ifa == NULL) {
29917198f8ceSThomas Graf 		/*
29927198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
29937198f8ceSThomas Graf 		 * userspace alreay relies on not having to provide this.
29947198f8ceSThomas Graf 		 */
29950778769dSNoriaki TAKAMIYA 		return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
299655ebaef1SNoriaki TAKAMIYA 				      ifa_flags, preferred_lft, valid_lft);
29971da177e4SLinus Torvalds 	}
29981da177e4SLinus Torvalds 
29997198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
30007198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
30017198f8ceSThomas Graf 		err = -EEXIST;
30027198f8ceSThomas Graf 	else
300355ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
30047198f8ceSThomas Graf 
30057198f8ceSThomas Graf 	in6_ifa_put(ifa);
30067198f8ceSThomas Graf 
30077198f8ceSThomas Graf 	return err;
30087198f8ceSThomas Graf }
30097198f8ceSThomas Graf 
3010101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3011101bb229SThomas Graf 			  u8 scope, int ifindex)
3012101bb229SThomas Graf {
3013101bb229SThomas Graf 	struct ifaddrmsg *ifm;
3014101bb229SThomas Graf 
3015101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
3016101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
3017101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
3018101bb229SThomas Graf 	ifm->ifa_flags = flags;
3019101bb229SThomas Graf 	ifm->ifa_scope = scope;
3020101bb229SThomas Graf 	ifm->ifa_index = ifindex;
3021101bb229SThomas Graf }
3022101bb229SThomas Graf 
302385486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
302485486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
302585486af0SThomas Graf {
302685486af0SThomas Graf 	struct ifa_cacheinfo ci;
302785486af0SThomas Graf 
302885486af0SThomas Graf 	ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
302985486af0SThomas Graf 			+ TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
303085486af0SThomas Graf 	ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
303185486af0SThomas Graf 			+ TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
303285486af0SThomas Graf 	ci.ifa_prefered = preferred;
303385486af0SThomas Graf 	ci.ifa_valid = valid;
303485486af0SThomas Graf 
303585486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
303685486af0SThomas Graf }
303785486af0SThomas Graf 
3038101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
3039101bb229SThomas Graf {
3040101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
3041101bb229SThomas Graf 		return RT_SCOPE_HOST;
3042101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
3043101bb229SThomas Graf 		return RT_SCOPE_LINK;
3044101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
3045101bb229SThomas Graf 		return RT_SCOPE_SITE;
3046101bb229SThomas Graf 	else
3047101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
3048101bb229SThomas Graf }
3049101bb229SThomas Graf 
30500ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
30510ab6803bSThomas Graf {
3052339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3053339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
3054339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct ifa_cacheinfo));
30550ab6803bSThomas Graf }
3056c5396a31SYOSHIFUJI Hideaki 
30571da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3058b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
30591da177e4SLinus Torvalds {
30601da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
306185486af0SThomas Graf 	u32 preferred, valid;
30621da177e4SLinus Torvalds 
30630ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
30640ab6803bSThomas Graf 	if (nlh == NULL)
306526932566SPatrick McHardy 		return -EMSGSIZE;
30660ab6803bSThomas Graf 
3067101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3068101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
3069101bb229SThomas Graf 
30701da177e4SLinus Torvalds 	if (!(ifa->flags&IFA_F_PERMANENT)) {
307185486af0SThomas Graf 		preferred = ifa->prefered_lft;
307285486af0SThomas Graf 		valid = ifa->valid_lft;
307385486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
30741da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
307585486af0SThomas Graf 			preferred -= tval;
307685486af0SThomas Graf 			if (valid != INFINITY_LIFE_TIME)
307785486af0SThomas Graf 				valid -= tval;
30781da177e4SLinus Torvalds 		}
30791da177e4SLinus Torvalds 	} else {
308085486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
308185486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
30821da177e4SLinus Torvalds 	}
308385486af0SThomas Graf 
30840ab6803bSThomas Graf 	if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
308526932566SPatrick McHardy 	    put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
308626932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
308726932566SPatrick McHardy 		return -EMSGSIZE;
308826932566SPatrick McHardy 	}
308985486af0SThomas Graf 
30900ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
30911da177e4SLinus Torvalds }
30921da177e4SLinus Torvalds 
30931da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3094e431b8c0SJamal Hadi Salim 				u32 pid, u32 seq, int event, u16 flags)
30951da177e4SLinus Torvalds {
30961da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3097101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3098101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
30991da177e4SLinus Torvalds 
31001da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3101101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3102101bb229SThomas Graf 
31030ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
31040ab6803bSThomas Graf 	if (nlh == NULL)
310526932566SPatrick McHardy 		return -EMSGSIZE;
31060ab6803bSThomas Graf 
3107101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
31080ab6803bSThomas Graf 	if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
31090ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
311026932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
311126932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
311226932566SPatrick McHardy 		return -EMSGSIZE;
311326932566SPatrick McHardy 	}
311485486af0SThomas Graf 
31150ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
31161da177e4SLinus Torvalds }
31171da177e4SLinus Torvalds 
31181da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3119b6544c0bSJamal Hadi Salim 				u32 pid, u32 seq, int event, unsigned int flags)
31201da177e4SLinus Torvalds {
31211da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3122101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3123101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
31241da177e4SLinus Torvalds 
31251da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3126101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3127101bb229SThomas Graf 
31280ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
31290ab6803bSThomas Graf 	if (nlh == NULL)
313026932566SPatrick McHardy 		return -EMSGSIZE;
31310ab6803bSThomas Graf 
3132101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
31330ab6803bSThomas Graf 	if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
31340ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
313526932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
313626932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
313726932566SPatrick McHardy 		return -EMSGSIZE;
313826932566SPatrick McHardy 	}
313985486af0SThomas Graf 
31400ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
31411da177e4SLinus Torvalds }
31421da177e4SLinus Torvalds 
31431da177e4SLinus Torvalds enum addr_type_t
31441da177e4SLinus Torvalds {
31451da177e4SLinus Torvalds 	UNICAST_ADDR,
31461da177e4SLinus Torvalds 	MULTICAST_ADDR,
31471da177e4SLinus Torvalds 	ANYCAST_ADDR,
31481da177e4SLinus Torvalds };
31491da177e4SLinus Torvalds 
31501da177e4SLinus Torvalds static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
31511da177e4SLinus Torvalds 			   enum addr_type_t type)
31521da177e4SLinus Torvalds {
31531da177e4SLinus Torvalds 	int idx, ip_idx;
31541da177e4SLinus Torvalds 	int s_idx, s_ip_idx;
31551da177e4SLinus Torvalds 	int err = 1;
31561da177e4SLinus Torvalds 	struct net_device *dev;
31571da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
31581da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
31591da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
31601da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
31611da177e4SLinus Torvalds 
31621da177e4SLinus Torvalds 	s_idx = cb->args[0];
31631da177e4SLinus Torvalds 	s_ip_idx = ip_idx = cb->args[1];
31641da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
31651da177e4SLinus Torvalds 
31661da177e4SLinus Torvalds 	for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
31671da177e4SLinus Torvalds 		if (idx < s_idx)
31681da177e4SLinus Torvalds 			continue;
31691da177e4SLinus Torvalds 		if (idx > s_idx)
31701da177e4SLinus Torvalds 			s_ip_idx = 0;
31711da177e4SLinus Torvalds 		ip_idx = 0;
31721da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
31731da177e4SLinus Torvalds 			continue;
31741da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
31751da177e4SLinus Torvalds 		switch (type) {
31761da177e4SLinus Torvalds 		case UNICAST_ADDR:
3177ae9cda5dSYOSHIFUJI Hideaki 			/* unicast address incl. temp addr */
31781da177e4SLinus Torvalds 			for (ifa = idev->addr_list; ifa;
31791da177e4SLinus Torvalds 			     ifa = ifa->if_next, ip_idx++) {
31801da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
31811da177e4SLinus Torvalds 					continue;
31821da177e4SLinus Torvalds 				if ((err = inet6_fill_ifaddr(skb, ifa,
31831da177e4SLinus Torvalds 				    NETLINK_CB(cb->skb).pid,
3184b6544c0bSJamal Hadi Salim 				    cb->nlh->nlmsg_seq, RTM_NEWADDR,
3185b6544c0bSJamal Hadi Salim 				    NLM_F_MULTI)) <= 0)
31861da177e4SLinus Torvalds 					goto done;
31871da177e4SLinus Torvalds 			}
31881da177e4SLinus Torvalds 			break;
31891da177e4SLinus Torvalds 		case MULTICAST_ADDR:
31901da177e4SLinus Torvalds 			/* multicast address */
31911da177e4SLinus Torvalds 			for (ifmca = idev->mc_list; ifmca;
31921da177e4SLinus Torvalds 			     ifmca = ifmca->next, ip_idx++) {
31931da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
31941da177e4SLinus Torvalds 					continue;
31951da177e4SLinus Torvalds 				if ((err = inet6_fill_ifmcaddr(skb, ifmca,
31961da177e4SLinus Torvalds 				    NETLINK_CB(cb->skb).pid,
3197b6544c0bSJamal Hadi Salim 				    cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3198b6544c0bSJamal Hadi Salim 				    NLM_F_MULTI)) <= 0)
31991da177e4SLinus Torvalds 					goto done;
32001da177e4SLinus Torvalds 			}
32011da177e4SLinus Torvalds 			break;
32021da177e4SLinus Torvalds 		case ANYCAST_ADDR:
32031da177e4SLinus Torvalds 			/* anycast address */
32041da177e4SLinus Torvalds 			for (ifaca = idev->ac_list; ifaca;
32051da177e4SLinus Torvalds 			     ifaca = ifaca->aca_next, ip_idx++) {
32061da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
32071da177e4SLinus Torvalds 					continue;
32081da177e4SLinus Torvalds 				if ((err = inet6_fill_ifacaddr(skb, ifaca,
32091da177e4SLinus Torvalds 				    NETLINK_CB(cb->skb).pid,
3210b6544c0bSJamal Hadi Salim 				    cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3211b6544c0bSJamal Hadi Salim 				    NLM_F_MULTI)) <= 0)
32121da177e4SLinus Torvalds 					goto done;
32131da177e4SLinus Torvalds 			}
32141da177e4SLinus Torvalds 			break;
32151da177e4SLinus Torvalds 		default:
32161da177e4SLinus Torvalds 			break;
32171da177e4SLinus Torvalds 		}
32181da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
32191da177e4SLinus Torvalds 		in6_dev_put(idev);
32201da177e4SLinus Torvalds 	}
32211da177e4SLinus Torvalds done:
32221da177e4SLinus Torvalds 	if (err <= 0) {
32231da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
32241da177e4SLinus Torvalds 		in6_dev_put(idev);
32251da177e4SLinus Torvalds 	}
32261da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
32271da177e4SLinus Torvalds 	cb->args[0] = idx;
32281da177e4SLinus Torvalds 	cb->args[1] = ip_idx;
32291da177e4SLinus Torvalds 	return skb->len;
32301da177e4SLinus Torvalds }
32311da177e4SLinus Torvalds 
32321da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
32331da177e4SLinus Torvalds {
32341da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
32351da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
32361da177e4SLinus Torvalds }
32371da177e4SLinus Torvalds 
32381da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
32391da177e4SLinus Torvalds {
32401da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
32411da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
32421da177e4SLinus Torvalds }
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 
32451da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
32461da177e4SLinus Torvalds {
32471da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
32481da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
32491da177e4SLinus Torvalds }
32501da177e4SLinus Torvalds 
32511b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
32521b29fc2cSThomas Graf 			     void *arg)
32536c223828SNoriaki TAKAMIYA {
32541b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
32551b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
32566c223828SNoriaki TAKAMIYA 	struct in6_addr *addr = NULL;
32576c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
32586c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
32596c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
32606c223828SNoriaki TAKAMIYA 	int err;
32616c223828SNoriaki TAKAMIYA 
32621b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
32631b29fc2cSThomas Graf 	if (err < 0)
32641b29fc2cSThomas Graf 		goto errout;
32656c223828SNoriaki TAKAMIYA 
32661b29fc2cSThomas Graf 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
32671b29fc2cSThomas Graf 	if (addr == NULL) {
32681b29fc2cSThomas Graf 		err = -EINVAL;
32691b29fc2cSThomas Graf 		goto errout;
32701b29fc2cSThomas Graf 	}
32711b29fc2cSThomas Graf 
32721b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
32736c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
32746c223828SNoriaki TAKAMIYA 		dev = __dev_get_by_index(ifm->ifa_index);
32756c223828SNoriaki TAKAMIYA 
32761b29fc2cSThomas Graf 	if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
32771b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
32781b29fc2cSThomas Graf 		goto errout;
32796c223828SNoriaki TAKAMIYA 	}
32806c223828SNoriaki TAKAMIYA 
32810ab6803bSThomas Graf 	if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
32821b29fc2cSThomas Graf 		err = -ENOBUFS;
32831b29fc2cSThomas Graf 		goto errout_ifa;
32841b29fc2cSThomas Graf 	}
32851b29fc2cSThomas Graf 
32866c223828SNoriaki TAKAMIYA 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
32876c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
328826932566SPatrick McHardy 	if (err < 0) {
328926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
329026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
329126932566SPatrick McHardy 		kfree_skb(skb);
329226932566SPatrick McHardy 		goto errout_ifa;
329326932566SPatrick McHardy 	}
32942942e900SThomas Graf 	err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
32951b29fc2cSThomas Graf errout_ifa:
32966c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
32971b29fc2cSThomas Graf errout:
32986c223828SNoriaki TAKAMIYA 	return err;
32996c223828SNoriaki TAKAMIYA }
33006c223828SNoriaki TAKAMIYA 
33011da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
33021da177e4SLinus Torvalds {
33031da177e4SLinus Torvalds 	struct sk_buff *skb;
33045d620266SThomas Graf 	int err = -ENOBUFS;
33051da177e4SLinus Torvalds 
33060ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
33075d620266SThomas Graf 	if (skb == NULL)
33085d620266SThomas Graf 		goto errout;
33095d620266SThomas Graf 
33105d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
331126932566SPatrick McHardy 	if (err < 0) {
331226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
331326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
331426932566SPatrick McHardy 		kfree_skb(skb);
331526932566SPatrick McHardy 		goto errout;
331626932566SPatrick McHardy 	}
33175d620266SThomas Graf 	err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
33185d620266SThomas Graf errout:
33195d620266SThomas Graf 	if (err < 0)
33205d620266SThomas Graf 		rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
33211da177e4SLinus Torvalds }
33221da177e4SLinus Torvalds 
33231da177e4SLinus Torvalds static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
33241da177e4SLinus Torvalds 				__s32 *array, int bytes)
33251da177e4SLinus Torvalds {
332604561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
332704561c1fSThomas Graf 
33281da177e4SLinus Torvalds 	memset(array, 0, bytes);
33291da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
33301da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
33311da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
33321da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
33331da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
33341da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
33351da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
33361da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
33371da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
33381da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
33391da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
33401da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
33411da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
33421da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
33431da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
33441da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
33451da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
33461da177e4SLinus Torvalds #endif
33471da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
334865f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3349c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3350930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
3351930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
335252e16356SYOSHIFUJI Hideaki 	array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
3353fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
335409c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
335509c884d4SYOSHIFUJI Hideaki #endif
3356930d6ff2SYOSHIFUJI Hideaki #endif
3357fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
33581da177e4SLinus Torvalds }
33591da177e4SLinus Torvalds 
3360339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
3361339bf98fSThomas Graf {
3362339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3363339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3364339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3365339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
3366339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
3367339bf98fSThomas Graf 	       + nla_total_size( /* IFLA_PROTINFO */
3368339bf98fSThomas Graf 			nla_total_size(4) /* IFLA_INET6_FLAGS */
3369339bf98fSThomas Graf 			+ nla_total_size(sizeof(struct ifla_cacheinfo))
3370339bf98fSThomas Graf 			+ nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3371339bf98fSThomas Graf 		 );
3372339bf98fSThomas Graf }
3373c5396a31SYOSHIFUJI Hideaki 
33741da177e4SLinus Torvalds static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3375b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
33761da177e4SLinus Torvalds {
33771da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
337804561c1fSThomas Graf 	struct nlattr *conf;
337904561c1fSThomas Graf 	struct ifinfomsg *hdr;
33801da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
338104561c1fSThomas Graf 	void *protoinfo;
33821da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
33831da177e4SLinus Torvalds 
338404561c1fSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
338504561c1fSThomas Graf 	if (nlh == NULL)
338626932566SPatrick McHardy 		return -EMSGSIZE;
33871da177e4SLinus Torvalds 
338804561c1fSThomas Graf 	hdr = nlmsg_data(nlh);
338904561c1fSThomas Graf 	hdr->ifi_family = AF_INET6;
339004561c1fSThomas Graf 	hdr->__ifi_pad = 0;
339104561c1fSThomas Graf 	hdr->ifi_type = dev->type;
339204561c1fSThomas Graf 	hdr->ifi_index = dev->ifindex;
339304561c1fSThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
339404561c1fSThomas Graf 	hdr->ifi_change = 0;
339504561c1fSThomas Graf 
339604561c1fSThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
33971da177e4SLinus Torvalds 
33981da177e4SLinus Torvalds 	if (dev->addr_len)
339904561c1fSThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
34001da177e4SLinus Torvalds 
340104561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
34021da177e4SLinus Torvalds 	if (dev->ifindex != dev->iflink)
340304561c1fSThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
34041da177e4SLinus Torvalds 
340504561c1fSThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
340604561c1fSThomas Graf 	if (protoinfo == NULL)
340704561c1fSThomas Graf 		goto nla_put_failure;
34081da177e4SLinus Torvalds 
340904561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
34101da177e4SLinus Torvalds 
34111da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
34121da177e4SLinus Torvalds 	ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
34131da177e4SLinus Torvalds 		    + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
34141da177e4SLinus Torvalds 	ci.reachable_time = idev->nd_parms->reachable_time;
34151da177e4SLinus Torvalds 	ci.retrans_time = idev->nd_parms->retrans_time;
341604561c1fSThomas Graf 	NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
34171da177e4SLinus Torvalds 
341804561c1fSThomas Graf 	conf = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
341904561c1fSThomas Graf 	if (conf == NULL)
342004561c1fSThomas Graf 		goto nla_put_failure;
342104561c1fSThomas Graf 	ipv6_store_devconf(&idev->cnf, nla_data(conf), nla_len(conf));
34221da177e4SLinus Torvalds 
34231da177e4SLinus Torvalds 	/* XXX - Statistics/MC not implemented */
34241da177e4SLinus Torvalds 
342504561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
342604561c1fSThomas Graf 	return nlmsg_end(skb, nlh);
34271da177e4SLinus Torvalds 
342804561c1fSThomas Graf nla_put_failure:
342926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
343026932566SPatrick McHardy 	return -EMSGSIZE;
34311da177e4SLinus Torvalds }
34321da177e4SLinus Torvalds 
34331da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
34341da177e4SLinus Torvalds {
34351da177e4SLinus Torvalds 	int idx, err;
34361da177e4SLinus Torvalds 	int s_idx = cb->args[0];
34371da177e4SLinus Torvalds 	struct net_device *dev;
34381da177e4SLinus Torvalds 	struct inet6_dev *idev;
34391da177e4SLinus Torvalds 
34401da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
34411da177e4SLinus Torvalds 	for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
34421da177e4SLinus Torvalds 		if (idx < s_idx)
34431da177e4SLinus Torvalds 			continue;
34441da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
34451da177e4SLinus Torvalds 			continue;
34461da177e4SLinus Torvalds 		err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3447b6544c0bSJamal Hadi Salim 				cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
34481da177e4SLinus Torvalds 		in6_dev_put(idev);
34491da177e4SLinus Torvalds 		if (err <= 0)
34501da177e4SLinus Torvalds 			break;
34511da177e4SLinus Torvalds 	}
34521da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
34531da177e4SLinus Torvalds 	cb->args[0] = idx;
34541da177e4SLinus Torvalds 
34551da177e4SLinus Torvalds 	return skb->len;
34561da177e4SLinus Torvalds }
34571da177e4SLinus Torvalds 
34581da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
34591da177e4SLinus Torvalds {
34601da177e4SLinus Torvalds 	struct sk_buff *skb;
34618d7a76c9SThomas Graf 	int err = -ENOBUFS;
34621da177e4SLinus Torvalds 
3463339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
34648d7a76c9SThomas Graf 	if (skb == NULL)
34658d7a76c9SThomas Graf 		goto errout;
34668d7a76c9SThomas Graf 
34678d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
346826932566SPatrick McHardy 	if (err < 0) {
346926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
347026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
347126932566SPatrick McHardy 		kfree_skb(skb);
347226932566SPatrick McHardy 		goto errout;
347326932566SPatrick McHardy 	}
34748d7a76c9SThomas Graf 	err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
34758d7a76c9SThomas Graf errout:
34768d7a76c9SThomas Graf 	if (err < 0)
34778d7a76c9SThomas Graf 		rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
34781da177e4SLinus Torvalds }
34791da177e4SLinus Torvalds 
3480339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
3481339bf98fSThomas Graf {
3482339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
3483339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
3484339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
3485339bf98fSThomas Graf }
3486c5396a31SYOSHIFUJI Hideaki 
34871da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3488b6544c0bSJamal Hadi Salim 			     struct prefix_info *pinfo, u32 pid, u32 seq,
3489b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
34901da177e4SLinus Torvalds {
34911da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
34921da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
34931da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
34941da177e4SLinus Torvalds 
34956051e2f4SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
34966051e2f4SThomas Graf 	if (nlh == NULL)
349726932566SPatrick McHardy 		return -EMSGSIZE;
34986051e2f4SThomas Graf 
34996051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
35001da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
35018a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
35028a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
35031da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
35041da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
35051da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
35068a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
35071da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
35081da177e4SLinus Torvalds 	if (pinfo->onlink)
35091da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
35101da177e4SLinus Torvalds 	if (pinfo->autoconf)
35111da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
35121da177e4SLinus Torvalds 
35136051e2f4SThomas Graf 	NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
35141da177e4SLinus Torvalds 
35151da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
35161da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
35176051e2f4SThomas Graf 	NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
35181da177e4SLinus Torvalds 
35196051e2f4SThomas Graf 	return nlmsg_end(skb, nlh);
35201da177e4SLinus Torvalds 
35216051e2f4SThomas Graf nla_put_failure:
352226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
352326932566SPatrick McHardy 	return -EMSGSIZE;
35241da177e4SLinus Torvalds }
35251da177e4SLinus Torvalds 
35261da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
35271da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
35281da177e4SLinus Torvalds {
35291da177e4SLinus Torvalds 	struct sk_buff *skb;
35308c384bfaSThomas Graf 	int err = -ENOBUFS;
35311da177e4SLinus Torvalds 
3532339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
35338c384bfaSThomas Graf 	if (skb == NULL)
35348c384bfaSThomas Graf 		goto errout;
35358c384bfaSThomas Graf 
35368c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
353726932566SPatrick McHardy 	if (err < 0) {
353826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
353926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
354026932566SPatrick McHardy 		kfree_skb(skb);
354126932566SPatrick McHardy 		goto errout;
354226932566SPatrick McHardy 	}
35438c384bfaSThomas Graf 	err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
35448c384bfaSThomas Graf errout:
35458c384bfaSThomas Graf 	if (err < 0)
35468c384bfaSThomas Graf 		rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err);
35471da177e4SLinus Torvalds }
35481da177e4SLinus Torvalds 
3549db46edc6SThomas Graf static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
35501da177e4SLinus Torvalds 	[RTM_GETLINK - RTM_BASE] = { .dumpit	= inet6_dump_ifinfo, },
35511da177e4SLinus Torvalds 	[RTM_NEWADDR - RTM_BASE] = { .doit	= inet6_rtm_newaddr, },
35521da177e4SLinus Torvalds 	[RTM_DELADDR - RTM_BASE] = { .doit	= inet6_rtm_deladdr, },
35536c223828SNoriaki TAKAMIYA 	[RTM_GETADDR - RTM_BASE] = { .doit	= inet6_rtm_getaddr,
35546c223828SNoriaki TAKAMIYA 				     .dumpit	= inet6_dump_ifaddr, },
35551da177e4SLinus Torvalds 	[RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
35561da177e4SLinus Torvalds 	[RTM_GETANYCAST - RTM_BASE] = { .dumpit	= inet6_dump_ifacaddr, },
35571da177e4SLinus Torvalds 	[RTM_NEWROUTE - RTM_BASE] = { .doit	= inet6_rtm_newroute, },
35581da177e4SLinus Torvalds 	[RTM_DELROUTE - RTM_BASE] = { .doit	= inet6_rtm_delroute, },
35591da177e4SLinus Torvalds 	[RTM_GETROUTE - RTM_BASE] = { .doit	= inet6_rtm_getroute,
35601da177e4SLinus Torvalds 				      .dumpit	= inet6_dump_fib, },
35618423a9aaSDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3562101367c2SThomas Graf 	[RTM_GETRULE  - RTM_BASE] = { .dumpit   = fib6_rules_dump,   },
35638423a9aaSDavid S. Miller #endif
35641da177e4SLinus Torvalds };
35651da177e4SLinus Torvalds 
35661da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
35671da177e4SLinus Torvalds {
35681da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
35691da177e4SLinus Torvalds 
35701da177e4SLinus Torvalds 	switch (event) {
35711da177e4SLinus Torvalds 	case RTM_NEWADDR:
357240e22e8fSThomas Graf 		ip6_ins_rt(ifp->rt);
35731da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
35741da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
35751da177e4SLinus Torvalds 		break;
35761da177e4SLinus Torvalds 	case RTM_DELADDR:
35771da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
35781da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
35791da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
35801da177e4SLinus Torvalds 		dst_hold(&ifp->rt->u.dst);
3581e0a1ad73SThomas Graf 		if (ip6_del_rt(ifp->rt))
35821da177e4SLinus Torvalds 			dst_free(&ifp->rt->u.dst);
35831da177e4SLinus Torvalds 		break;
35841da177e4SLinus Torvalds 	}
35851da177e4SLinus Torvalds }
35861da177e4SLinus Torvalds 
35871da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
35881da177e4SLinus Torvalds {
35898814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
35901da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
35911da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
35928814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
35931da177e4SLinus Torvalds }
35941da177e4SLinus Torvalds 
35951da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
35961da177e4SLinus Torvalds 
35971da177e4SLinus Torvalds static
35981da177e4SLinus Torvalds int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
35991da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
36001da177e4SLinus Torvalds {
36011da177e4SLinus Torvalds 	int *valp = ctl->data;
36021da177e4SLinus Torvalds 	int val = *valp;
36031da177e4SLinus Torvalds 	int ret;
36041da177e4SLinus Torvalds 
36051da177e4SLinus Torvalds 	ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
36061da177e4SLinus Torvalds 
36071da177e4SLinus Torvalds 	if (write && valp != &ipv6_devconf_dflt.forwarding) {
36081da177e4SLinus Torvalds 		if (valp != &ipv6_devconf.forwarding) {
36091da177e4SLinus Torvalds 			if ((!*valp) ^ (!val)) {
36101da177e4SLinus Torvalds 				struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
36111da177e4SLinus Torvalds 				if (idev == NULL)
36121da177e4SLinus Torvalds 					return ret;
36131da177e4SLinus Torvalds 				dev_forward_change(idev);
36141da177e4SLinus Torvalds 			}
36151da177e4SLinus Torvalds 		} else {
36161da177e4SLinus Torvalds 			ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
36171da177e4SLinus Torvalds 			addrconf_forward_change();
36181da177e4SLinus Torvalds 		}
36191da177e4SLinus Torvalds 		if (*valp)
36201da177e4SLinus Torvalds 			rt6_purge_dflt_routers();
36211da177e4SLinus Torvalds 	}
36221da177e4SLinus Torvalds 
36231da177e4SLinus Torvalds 	return ret;
36241da177e4SLinus Torvalds }
36251da177e4SLinus Torvalds 
36261da177e4SLinus Torvalds static int addrconf_sysctl_forward_strategy(ctl_table *table,
36271da177e4SLinus Torvalds 					    int __user *name, int nlen,
36281da177e4SLinus Torvalds 					    void __user *oldval,
36291da177e4SLinus Torvalds 					    size_t __user *oldlenp,
36301f29bcd7SAlexey Dobriyan 					    void __user *newval, size_t newlen)
36311da177e4SLinus Torvalds {
36321da177e4SLinus Torvalds 	int *valp = table->data;
36331da177e4SLinus Torvalds 	int new;
36341da177e4SLinus Torvalds 
36351da177e4SLinus Torvalds 	if (!newval || !newlen)
36361da177e4SLinus Torvalds 		return 0;
36371da177e4SLinus Torvalds 	if (newlen != sizeof(int))
36381da177e4SLinus Torvalds 		return -EINVAL;
36391da177e4SLinus Torvalds 	if (get_user(new, (int __user *)newval))
36401da177e4SLinus Torvalds 		return -EFAULT;
36411da177e4SLinus Torvalds 	if (new == *valp)
36421da177e4SLinus Torvalds 		return 0;
36431da177e4SLinus Torvalds 	if (oldval && oldlenp) {
36441da177e4SLinus Torvalds 		size_t len;
36451da177e4SLinus Torvalds 		if (get_user(len, oldlenp))
36461da177e4SLinus Torvalds 			return -EFAULT;
36471da177e4SLinus Torvalds 		if (len) {
36481da177e4SLinus Torvalds 			if (len > table->maxlen)
36491da177e4SLinus Torvalds 				len = table->maxlen;
36501da177e4SLinus Torvalds 			if (copy_to_user(oldval, valp, len))
36511da177e4SLinus Torvalds 				return -EFAULT;
36521da177e4SLinus Torvalds 			if (put_user(len, oldlenp))
36531da177e4SLinus Torvalds 				return -EFAULT;
36541da177e4SLinus Torvalds 		}
36551da177e4SLinus Torvalds 	}
36561da177e4SLinus Torvalds 
36571da177e4SLinus Torvalds 	if (valp != &ipv6_devconf_dflt.forwarding) {
36581da177e4SLinus Torvalds 		if (valp != &ipv6_devconf.forwarding) {
36591da177e4SLinus Torvalds 			struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
36601da177e4SLinus Torvalds 			int changed;
36611da177e4SLinus Torvalds 			if (unlikely(idev == NULL))
36621da177e4SLinus Torvalds 				return -ENODEV;
36631da177e4SLinus Torvalds 			changed = (!*valp) ^ (!new);
36641da177e4SLinus Torvalds 			*valp = new;
36651da177e4SLinus Torvalds 			if (changed)
36661da177e4SLinus Torvalds 				dev_forward_change(idev);
36671da177e4SLinus Torvalds 		} else {
36681da177e4SLinus Torvalds 			*valp = new;
36691da177e4SLinus Torvalds 			addrconf_forward_change();
36701da177e4SLinus Torvalds 		}
36711da177e4SLinus Torvalds 
36721da177e4SLinus Torvalds 		if (*valp)
36731da177e4SLinus Torvalds 			rt6_purge_dflt_routers();
36741da177e4SLinus Torvalds 	} else
36751da177e4SLinus Torvalds 		*valp = new;
36761da177e4SLinus Torvalds 
36771da177e4SLinus Torvalds 	return 1;
36781da177e4SLinus Torvalds }
36791da177e4SLinus Torvalds 
36801da177e4SLinus Torvalds static struct addrconf_sysctl_table
36811da177e4SLinus Torvalds {
36821da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
36831da177e4SLinus Torvalds 	ctl_table addrconf_vars[__NET_IPV6_MAX];
36841da177e4SLinus Torvalds 	ctl_table addrconf_dev[2];
36851da177e4SLinus Torvalds 	ctl_table addrconf_conf_dir[2];
36861da177e4SLinus Torvalds 	ctl_table addrconf_proto_dir[2];
36871da177e4SLinus Torvalds 	ctl_table addrconf_root_dir[2];
3688ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
36891da177e4SLinus Torvalds 	.sysctl_header = NULL,
36901da177e4SLinus Torvalds 	.addrconf_vars = {
36911da177e4SLinus Torvalds 		{
36921da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORWARDING,
36931da177e4SLinus Torvalds 			.procname	=	"forwarding",
36941da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.forwarding,
36951da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
36961da177e4SLinus Torvalds 			.mode		=	0644,
36971da177e4SLinus Torvalds 			.proc_handler	=	&addrconf_sysctl_forward,
36981da177e4SLinus Torvalds 			.strategy	=	&addrconf_sysctl_forward_strategy,
36991da177e4SLinus Torvalds 		},
37001da177e4SLinus Torvalds 		{
37011da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_HOP_LIMIT,
37021da177e4SLinus Torvalds 			.procname	=	"hop_limit",
37031da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.hop_limit,
37041da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
37051da177e4SLinus Torvalds 			.mode		=	0644,
37061da177e4SLinus Torvalds 			.proc_handler	=	proc_dointvec,
37071da177e4SLinus Torvalds 		},
37081da177e4SLinus Torvalds 		{
37091da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MTU,
37101da177e4SLinus Torvalds 			.procname	=	"mtu",
37111da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.mtu6,
37121da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
37131da177e4SLinus Torvalds 			.mode		=	0644,
37141da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
37151da177e4SLinus Torvalds 		},
37161da177e4SLinus Torvalds 		{
37171da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_RA,
37181da177e4SLinus Torvalds 			.procname	=	"accept_ra",
37191da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.accept_ra,
37201da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
37211da177e4SLinus Torvalds 			.mode		=	0644,
37221da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
37231da177e4SLinus Torvalds 		},
37241da177e4SLinus Torvalds 		{
37251da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_REDIRECTS,
37261da177e4SLinus Torvalds 			.procname	=	"accept_redirects",
37271da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.accept_redirects,
37281da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
37291da177e4SLinus Torvalds 			.mode		=	0644,
37301da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
37311da177e4SLinus Torvalds 		},
37321da177e4SLinus Torvalds 		{
37331da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_AUTOCONF,
37341da177e4SLinus Torvalds 			.procname	=	"autoconf",
37351da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.autoconf,
37361da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
37371da177e4SLinus Torvalds 			.mode		=	0644,
37381da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
37391da177e4SLinus Torvalds 		},
37401da177e4SLinus Torvalds 		{
37411da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_DAD_TRANSMITS,
37421da177e4SLinus Torvalds 			.procname	=	"dad_transmits",
37431da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.dad_transmits,
37441da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
37451da177e4SLinus Torvalds 			.mode		=	0644,
37461da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
37471da177e4SLinus Torvalds 		},
37481da177e4SLinus Torvalds 		{
37491da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICITS,
37501da177e4SLinus Torvalds 			.procname	=	"router_solicitations",
37511da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicits,
37521da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
37531da177e4SLinus Torvalds 			.mode		=	0644,
37541da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
37551da177e4SLinus Torvalds 		},
37561da177e4SLinus Torvalds 		{
37571da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_INTERVAL,
37581da177e4SLinus Torvalds 			.procname	=	"router_solicitation_interval",
37591da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicit_interval,
37601da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
37611da177e4SLinus Torvalds 			.mode		=	0644,
37621da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec_jiffies,
37631da177e4SLinus Torvalds 			.strategy	=	&sysctl_jiffies,
37641da177e4SLinus Torvalds 		},
37651da177e4SLinus Torvalds 		{
37661da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_DELAY,
37671da177e4SLinus Torvalds 			.procname	=	"router_solicitation_delay",
37681da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicit_delay,
37691da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
37701da177e4SLinus Torvalds 			.mode		=	0644,
37711da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec_jiffies,
37721da177e4SLinus Torvalds 			.strategy	=	&sysctl_jiffies,
37731da177e4SLinus Torvalds 		},
37741da177e4SLinus Torvalds 		{
37751da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORCE_MLD_VERSION,
37761da177e4SLinus Torvalds 			.procname	=	"force_mld_version",
37771da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.force_mld_version,
37781da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
37791da177e4SLinus Torvalds 			.mode		=	0644,
37801da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
37811da177e4SLinus Torvalds 		},
37821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
37831da177e4SLinus Torvalds 		{
37841da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_USE_TEMPADDR,
37851da177e4SLinus Torvalds 			.procname	=	"use_tempaddr",
37861da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.use_tempaddr,
37871da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
37881da177e4SLinus Torvalds 			.mode		=	0644,
37891da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
37901da177e4SLinus Torvalds 		},
37911da177e4SLinus Torvalds 		{
37921da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_VALID_LFT,
37931da177e4SLinus Torvalds 			.procname	=	"temp_valid_lft",
37941da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.temp_valid_lft,
37951da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
37961da177e4SLinus Torvalds 			.mode		=	0644,
37971da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
37981da177e4SLinus Torvalds 		},
37991da177e4SLinus Torvalds 		{
38001da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_PREFERED_LFT,
38011da177e4SLinus Torvalds 			.procname	=	"temp_prefered_lft",
38021da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.temp_prefered_lft,
38031da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
38041da177e4SLinus Torvalds 			.mode		=	0644,
38051da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
38061da177e4SLinus Torvalds 		},
38071da177e4SLinus Torvalds 		{
38081da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_REGEN_MAX_RETRY,
38091da177e4SLinus Torvalds 			.procname	=	"regen_max_retry",
38101da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.regen_max_retry,
38111da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
38121da177e4SLinus Torvalds 			.mode		=	0644,
38131da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
38141da177e4SLinus Torvalds 		},
38151da177e4SLinus Torvalds 		{
38161da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_DESYNC_FACTOR,
38171da177e4SLinus Torvalds 			.procname	=	"max_desync_factor",
38181da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_desync_factor,
38191da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
38201da177e4SLinus Torvalds 			.mode		=	0644,
38211da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
38221da177e4SLinus Torvalds 		},
38231da177e4SLinus Torvalds #endif
38241da177e4SLinus Torvalds 		{
38251da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_ADDRESSES,
38261da177e4SLinus Torvalds 			.procname	=	"max_addresses",
38271da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_addresses,
38281da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
38291da177e4SLinus Torvalds 			.mode		=	0644,
38301da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
38311da177e4SLinus Torvalds 		},
38321da177e4SLinus Torvalds 		{
383365f5c7c1SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_DEFRTR,
383465f5c7c1SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_defrtr",
383565f5c7c1SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_defrtr,
383665f5c7c1SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
383765f5c7c1SYOSHIFUJI Hideaki 			.mode		=	0644,
383865f5c7c1SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
383965f5c7c1SYOSHIFUJI Hideaki 		},
384065f5c7c1SYOSHIFUJI Hideaki 		{
3841c4fd30ebSYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_PINFO,
3842c4fd30ebSYOSHIFUJI Hideaki 			.procname	=	"accept_ra_pinfo",
3843c4fd30ebSYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_pinfo,
3844c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
3845c4fd30ebSYOSHIFUJI Hideaki 			.mode		=	0644,
3846c4fd30ebSYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
3847c4fd30ebSYOSHIFUJI Hideaki 		},
3848930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
3849930d6ff2SYOSHIFUJI Hideaki 		{
3850930d6ff2SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RTR_PREF,
3851930d6ff2SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_rtr_pref",
3852930d6ff2SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_rtr_pref,
3853930d6ff2SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
3854930d6ff2SYOSHIFUJI Hideaki 			.mode		=	0644,
3855930d6ff2SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
3856930d6ff2SYOSHIFUJI Hideaki 		},
385752e16356SYOSHIFUJI Hideaki 		{
385852e16356SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_RTR_PROBE_INTERVAL,
385952e16356SYOSHIFUJI Hideaki 			.procname	=	"router_probe_interval",
386052e16356SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.rtr_probe_interval,
386152e16356SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
386252e16356SYOSHIFUJI Hideaki 			.mode		=	0644,
386352e16356SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec_jiffies,
386452e16356SYOSHIFUJI Hideaki 			.strategy	=	&sysctl_jiffies,
386552e16356SYOSHIFUJI Hideaki 		},
3866fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
386709c884d4SYOSHIFUJI Hideaki 		{
386809c884d4SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
386909c884d4SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_rt_info_max_plen",
387009c884d4SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_rt_info_max_plen,
387109c884d4SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
387209c884d4SYOSHIFUJI Hideaki 			.mode		=	0644,
387309c884d4SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
387409c884d4SYOSHIFUJI Hideaki 		},
387509c884d4SYOSHIFUJI Hideaki #endif
3876930d6ff2SYOSHIFUJI Hideaki #endif
3877c4fd30ebSYOSHIFUJI Hideaki 		{
3878fbea49e1SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_PROXY_NDP,
3879fbea49e1SYOSHIFUJI Hideaki 			.procname	=	"proxy_ndp",
3880fbea49e1SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.proxy_ndp,
3881fbea49e1SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
3882fbea49e1SYOSHIFUJI Hideaki 			.mode		=	0644,
3883fbea49e1SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
3884fbea49e1SYOSHIFUJI Hideaki 		},
3885fbea49e1SYOSHIFUJI Hideaki 		{
38861da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
38871da177e4SLinus Torvalds 		}
38881da177e4SLinus Torvalds 	},
38891da177e4SLinus Torvalds 	.addrconf_dev = {
38901da177e4SLinus Torvalds 		{
38911da177e4SLinus Torvalds 			.ctl_name	=	NET_PROTO_CONF_ALL,
38921da177e4SLinus Torvalds 			.procname	=	"all",
38931da177e4SLinus Torvalds 			.mode		=	0555,
38941da177e4SLinus Torvalds 			.child		=	addrconf_sysctl.addrconf_vars,
38951da177e4SLinus Torvalds 		},
38961da177e4SLinus Torvalds 		{
38971da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
38981da177e4SLinus Torvalds 		}
38991da177e4SLinus Torvalds 	},
39001da177e4SLinus Torvalds 	.addrconf_conf_dir = {
39011da177e4SLinus Torvalds 		{
39021da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_CONF,
39031da177e4SLinus Torvalds 			.procname	=	"conf",
39041da177e4SLinus Torvalds 			.mode		=	0555,
39051da177e4SLinus Torvalds 			.child		=	addrconf_sysctl.addrconf_dev,
39061da177e4SLinus Torvalds 		},
39071da177e4SLinus Torvalds 		{
39081da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
39091da177e4SLinus Torvalds 		}
39101da177e4SLinus Torvalds 	},
39111da177e4SLinus Torvalds 	.addrconf_proto_dir = {
39121da177e4SLinus Torvalds 		{
39131da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6,
39141da177e4SLinus Torvalds 			.procname	=	"ipv6",
39151da177e4SLinus Torvalds 			.mode		=	0555,
39161da177e4SLinus Torvalds 			.child		=	addrconf_sysctl.addrconf_conf_dir,
39171da177e4SLinus Torvalds 		},
39181da177e4SLinus Torvalds 		{
39191da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
39201da177e4SLinus Torvalds 		}
39211da177e4SLinus Torvalds 	},
39221da177e4SLinus Torvalds 	.addrconf_root_dir = {
39231da177e4SLinus Torvalds 		{
39241da177e4SLinus Torvalds 			.ctl_name	=	CTL_NET,
39251da177e4SLinus Torvalds 			.procname	=	"net",
39261da177e4SLinus Torvalds 			.mode		=	0555,
39271da177e4SLinus Torvalds 			.child		=	addrconf_sysctl.addrconf_proto_dir,
39281da177e4SLinus Torvalds 		},
39291da177e4SLinus Torvalds 		{
39301da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
39311da177e4SLinus Torvalds 		}
39321da177e4SLinus Torvalds 	},
39331da177e4SLinus Torvalds };
39341da177e4SLinus Torvalds 
39351da177e4SLinus Torvalds static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
39361da177e4SLinus Torvalds {
39371da177e4SLinus Torvalds 	int i;
39381da177e4SLinus Torvalds 	struct net_device *dev = idev ? idev->dev : NULL;
39391da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
39401da177e4SLinus Torvalds 	char *dev_name = NULL;
39411da177e4SLinus Torvalds 
3942af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
39431da177e4SLinus Torvalds 	if (t == NULL)
39441da177e4SLinus Torvalds 		return;
39451da177e4SLinus Torvalds 	for (i=0; t->addrconf_vars[i].data; i++) {
39461da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
39471da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
39481da177e4SLinus Torvalds 	}
39491da177e4SLinus Torvalds 	if (dev) {
39501da177e4SLinus Torvalds 		dev_name = dev->name;
39511da177e4SLinus Torvalds 		t->addrconf_dev[0].ctl_name = dev->ifindex;
39521da177e4SLinus Torvalds 	} else {
39531da177e4SLinus Torvalds 		dev_name = "default";
39541da177e4SLinus Torvalds 		t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
39551da177e4SLinus Torvalds 	}
39561da177e4SLinus Torvalds 
39571da177e4SLinus Torvalds 	/*
39581da177e4SLinus Torvalds 	 * Make a copy of dev_name, because '.procname' is regarded as const
39591da177e4SLinus Torvalds 	 * by sysctl and we wouldn't want anyone to change it under our feet
39601da177e4SLinus Torvalds 	 * (see SIOCSIFNAME).
39611da177e4SLinus Torvalds 	 */
3962543537bdSPaulo Marques 	dev_name = kstrdup(dev_name, GFP_KERNEL);
39631da177e4SLinus Torvalds 	if (!dev_name)
39641da177e4SLinus Torvalds 	    goto free;
39651da177e4SLinus Torvalds 
39661da177e4SLinus Torvalds 	t->addrconf_dev[0].procname = dev_name;
39671da177e4SLinus Torvalds 
39681da177e4SLinus Torvalds 	t->addrconf_dev[0].child = t->addrconf_vars;
39691da177e4SLinus Torvalds 	t->addrconf_conf_dir[0].child = t->addrconf_dev;
39701da177e4SLinus Torvalds 	t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
39711da177e4SLinus Torvalds 	t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
39721da177e4SLinus Torvalds 
39730b4d4147SEric W. Biederman 	t->sysctl_header = register_sysctl_table(t->addrconf_root_dir);
39741da177e4SLinus Torvalds 	if (t->sysctl_header == NULL)
39751da177e4SLinus Torvalds 		goto free_procname;
39761da177e4SLinus Torvalds 	else
39771da177e4SLinus Torvalds 		p->sysctl = t;
39781da177e4SLinus Torvalds 	return;
39791da177e4SLinus Torvalds 
39801da177e4SLinus Torvalds 	/* error path */
39811da177e4SLinus Torvalds  free_procname:
39821da177e4SLinus Torvalds 	kfree(dev_name);
39831da177e4SLinus Torvalds  free:
39841da177e4SLinus Torvalds 	kfree(t);
39851da177e4SLinus Torvalds 
39861da177e4SLinus Torvalds 	return;
39871da177e4SLinus Torvalds }
39881da177e4SLinus Torvalds 
39891da177e4SLinus Torvalds static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
39901da177e4SLinus Torvalds {
39911da177e4SLinus Torvalds 	if (p->sysctl) {
39921da177e4SLinus Torvalds 		struct addrconf_sysctl_table *t = p->sysctl;
39931da177e4SLinus Torvalds 		p->sysctl = NULL;
39941da177e4SLinus Torvalds 		unregister_sysctl_table(t->sysctl_header);
39951da177e4SLinus Torvalds 		kfree(t->addrconf_dev[0].procname);
39961da177e4SLinus Torvalds 		kfree(t);
39971da177e4SLinus Torvalds 	}
39981da177e4SLinus Torvalds }
39991da177e4SLinus Torvalds 
40001da177e4SLinus Torvalds 
40011da177e4SLinus Torvalds #endif
40021da177e4SLinus Torvalds 
40031da177e4SLinus Torvalds /*
40041da177e4SLinus Torvalds  *      Device notifier
40051da177e4SLinus Torvalds  */
40061da177e4SLinus Torvalds 
40071da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb)
40081da177e4SLinus Torvalds {
4009e041c683SAlan Stern 	return atomic_notifier_chain_register(&inet6addr_chain, nb);
40101da177e4SLinus Torvalds }
40111da177e4SLinus Torvalds 
40121da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb)
40131da177e4SLinus Torvalds {
4014e041c683SAlan Stern 	return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
40151da177e4SLinus Torvalds }
40161da177e4SLinus Torvalds 
40171da177e4SLinus Torvalds /*
40181da177e4SLinus Torvalds  *	Init / cleanup code
40191da177e4SLinus Torvalds  */
40201da177e4SLinus Torvalds 
40211da177e4SLinus Torvalds int __init addrconf_init(void)
40221da177e4SLinus Torvalds {
40231da177e4SLinus Torvalds 	int err = 0;
40241da177e4SLinus Torvalds 
40251da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
40261da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
40271da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
40281da177e4SLinus Torvalds 	 * to other devices which are up.
40291da177e4SLinus Torvalds 	 *
40301da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
40311da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
40321da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
40331da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
40341da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
40351da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
40361da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
40371da177e4SLinus Torvalds 	 *
40381da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
40391da177e4SLinus Torvalds 	 * loopback_dev first by hand.
40401da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
40411da177e4SLinus Torvalds 	 * device and it being up should be removed.
40421da177e4SLinus Torvalds 	 */
40431da177e4SLinus Torvalds 	rtnl_lock();
40441da177e4SLinus Torvalds 	if (!ipv6_add_dev(&loopback_dev))
40451da177e4SLinus Torvalds 		err = -ENOMEM;
40461da177e4SLinus Torvalds 	rtnl_unlock();
40471da177e4SLinus Torvalds 	if (err)
40481da177e4SLinus Torvalds 		return err;
40491da177e4SLinus Torvalds 
4050c62dba90SHerbert Xu 	ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
4051c62dba90SHerbert Xu 
40521da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
40531da177e4SLinus Torvalds 
40541da177e4SLinus Torvalds 	addrconf_verify(0);
40551da177e4SLinus Torvalds 	rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
40561da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
40571da177e4SLinus Torvalds 	addrconf_sysctl.sysctl_header =
40580b4d4147SEric W. Biederman 		register_sysctl_table(addrconf_sysctl.addrconf_root_dir);
40591da177e4SLinus Torvalds 	addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
40601da177e4SLinus Torvalds #endif
40611da177e4SLinus Torvalds 
40621da177e4SLinus Torvalds 	return 0;
40631da177e4SLinus Torvalds }
40641da177e4SLinus Torvalds 
40651da177e4SLinus Torvalds void __exit addrconf_cleanup(void)
40661da177e4SLinus Torvalds {
40671da177e4SLinus Torvalds 	struct net_device *dev;
40681da177e4SLinus Torvalds 	struct inet6_dev *idev;
40691da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
40701da177e4SLinus Torvalds 	int i;
40711da177e4SLinus Torvalds 
40721da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
40731da177e4SLinus Torvalds 
40741da177e4SLinus Torvalds 	rtnetlink_links[PF_INET6] = NULL;
40751da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
40761da177e4SLinus Torvalds 	addrconf_sysctl_unregister(&ipv6_devconf_dflt);
40771da177e4SLinus Torvalds 	addrconf_sysctl_unregister(&ipv6_devconf);
40781da177e4SLinus Torvalds #endif
40791da177e4SLinus Torvalds 
40801da177e4SLinus Torvalds 	rtnl_lock();
40811da177e4SLinus Torvalds 
40821da177e4SLinus Torvalds 	/*
40831da177e4SLinus Torvalds 	 *	clean dev list.
40841da177e4SLinus Torvalds 	 */
40851da177e4SLinus Torvalds 
40861da177e4SLinus Torvalds 	for (dev=dev_base; dev; dev=dev->next) {
40871da177e4SLinus Torvalds 		if ((idev = __in6_dev_get(dev)) == NULL)
40881da177e4SLinus Torvalds 			continue;
40891da177e4SLinus Torvalds 		addrconf_ifdown(dev, 1);
40901da177e4SLinus Torvalds 	}
40911da177e4SLinus Torvalds 	addrconf_ifdown(&loopback_dev, 2);
40921da177e4SLinus Torvalds 
40931da177e4SLinus Torvalds 	/*
40941da177e4SLinus Torvalds 	 *	Check hash table.
40951da177e4SLinus Torvalds 	 */
40961da177e4SLinus Torvalds 
40971da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
40981da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
40991da177e4SLinus Torvalds 		for (ifa=inet6_addr_lst[i]; ifa; ) {
41001da177e4SLinus Torvalds 			struct inet6_ifaddr *bifa;
41011da177e4SLinus Torvalds 
41021da177e4SLinus Torvalds 			bifa = ifa;
41031da177e4SLinus Torvalds 			ifa = ifa->lst_next;
41041da177e4SLinus Torvalds 			printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
41051da177e4SLinus Torvalds 			/* Do not free it; something is wrong.
41061da177e4SLinus Torvalds 			   Now we can investigate it with debugger.
41071da177e4SLinus Torvalds 			 */
41081da177e4SLinus Torvalds 		}
41091da177e4SLinus Torvalds 	}
41101da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
41111da177e4SLinus Torvalds 
41121da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
41131da177e4SLinus Torvalds 
41141da177e4SLinus Torvalds 	rtnl_unlock();
41151da177e4SLinus Torvalds 
41161da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
41171da177e4SLinus Torvalds 	proc_net_remove("if_inet6");
41181da177e4SLinus Torvalds #endif
41191da177e4SLinus Torvalds }
4120