xref: /openbmc/linux/net/ipv6/addrconf.c (revision f1243c2d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	IPv6 Address [auto]configuration
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	$Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
121da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
131da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
141da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds /*
181da177e4SLinus Torvalds  *	Changes:
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *	Janos Farkas			:	delete timer on ifdown
211da177e4SLinus Torvalds  *	<chexum@bankinf.banki.hu>
221da177e4SLinus Torvalds  *	Andi Kleen			:	kill double kfree on module
231da177e4SLinus Torvalds  *						unload.
241da177e4SLinus Torvalds  *	Maciej W. Rozycki		:	FDDI support
251da177e4SLinus Torvalds  *	sekiya@USAGI			:	Don't send too many RS
261da177e4SLinus Torvalds  *						packets.
271da177e4SLinus Torvalds  *	yoshfuji@USAGI			:       Fixed interval between DAD
281da177e4SLinus Torvalds  *						packets.
291da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	improved accuracy of
301da177e4SLinus Torvalds  *						address validation timer.
311da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	Privacy Extensions (RFC3041)
321da177e4SLinus Torvalds  *						support.
331da177e4SLinus Torvalds  *	Yuji SEKIYA @USAGI		:	Don't assign a same IPv6
341da177e4SLinus Torvalds  *						address on a same interface.
351da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	ARCnet support
361da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	convert /proc/net/if_inet6 to
371da177e4SLinus Torvalds  *						seq_file.
38b1cacb68SYOSHIFUJI Hideaki  *	YOSHIFUJI Hideaki @USAGI	:	improved source address
39b1cacb68SYOSHIFUJI Hideaki  *						selection; consider scope,
40b1cacb68SYOSHIFUJI Hideaki  *						status etc.
411da177e4SLinus Torvalds  */
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <linux/errno.h>
441da177e4SLinus Torvalds #include <linux/types.h>
451da177e4SLinus Torvalds #include <linux/socket.h>
461da177e4SLinus Torvalds #include <linux/sockios.h>
471da177e4SLinus Torvalds #include <linux/net.h>
481da177e4SLinus Torvalds #include <linux/in6.h>
491da177e4SLinus Torvalds #include <linux/netdevice.h>
501823730fSThomas Graf #include <linux/if_addr.h>
511da177e4SLinus Torvalds #include <linux/if_arp.h>
521da177e4SLinus Torvalds #include <linux/if_arcnet.h>
531da177e4SLinus Torvalds #include <linux/if_infiniband.h>
541da177e4SLinus Torvalds #include <linux/route.h>
551da177e4SLinus Torvalds #include <linux/inetdevice.h>
561da177e4SLinus Torvalds #include <linux/init.h>
571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
581da177e4SLinus Torvalds #include <linux/sysctl.h>
591da177e4SLinus Torvalds #endif
604fc268d2SRandy Dunlap #include <linux/capability.h>
611da177e4SLinus Torvalds #include <linux/delay.h>
621da177e4SLinus Torvalds #include <linux/notifier.h>
63543537bdSPaulo Marques #include <linux/string.h>
641da177e4SLinus Torvalds 
65457c4cbcSEric W. Biederman #include <net/net_namespace.h>
661da177e4SLinus Torvalds #include <net/sock.h>
671da177e4SLinus Torvalds #include <net/snmp.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #include <net/ipv6.h>
701da177e4SLinus Torvalds #include <net/protocol.h>
711da177e4SLinus Torvalds #include <net/ndisc.h>
721da177e4SLinus Torvalds #include <net/ip6_route.h>
731da177e4SLinus Torvalds #include <net/addrconf.h>
741da177e4SLinus Torvalds #include <net/tcp.h>
751da177e4SLinus Torvalds #include <net/ip.h>
765d620266SThomas Graf #include <net/netlink.h>
77f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
781da177e4SLinus Torvalds #include <linux/if_tunnel.h>
791da177e4SLinus Torvalds #include <linux/rtnetlink.h>
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
821da177e4SLinus Torvalds #include <linux/random.h>
831da177e4SLinus Torvalds #endif
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds #include <asm/uaccess.h>
867f7d9a6bSHerbert Xu #include <asm/unaligned.h>
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds #include <linux/proc_fs.h>
891da177e4SLinus Torvalds #include <linux/seq_file.h>
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* Set to 3 to get tracing... */
921da177e4SLinus Torvalds #define ACONF_DEBUG 2
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
951da177e4SLinus Torvalds #define ADBG(x) printk x
961da177e4SLinus Torvalds #else
971da177e4SLinus Torvalds #define ADBG(x)
981da177e4SLinus Torvalds #endif
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
1011da177e4SLinus Torvalds #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
104f52295a9SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev);
105408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
106408c4768SPavel Emelyanov #else
107408c4768SPavel Emelyanov static inline void addrconf_sysctl_register(struct inet6_dev *idev)
108408c4768SPavel Emelyanov {
109408c4768SPavel Emelyanov }
110408c4768SPavel Emelyanov 
111408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
112408c4768SPavel Emelyanov {
113408c4768SPavel Emelyanov }
1141da177e4SLinus Torvalds #endif
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1171da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev);
1181da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1191da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data);
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds static int desync_factor = MAX_DESYNC_FACTOR * HZ;
1221da177e4SLinus Torvalds #endif
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /*
1271da177e4SLinus Torvalds  *	Configured unicast address hash table
1281da177e4SLinus Torvalds  */
1291da177e4SLinus Torvalds static struct inet6_ifaddr		*inet6_addr_lst[IN6_ADDR_HSIZE];
1301da177e4SLinus Torvalds static DEFINE_RWLOCK(addrconf_hash_lock);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds static void addrconf_verify(unsigned long);
1331da177e4SLinus Torvalds 
1348d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
1351da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1381da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1411da177e4SLinus Torvalds 
142e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
1431da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data);
1441da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
145c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1461da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1471da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1481da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1511da177e4SLinus Torvalds 				struct prefix_info *pinfo);
15206bfe655SDaniel Lezcano static int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
15306bfe655SDaniel Lezcano 			      struct net_device *dev);
1541da177e4SLinus Torvalds 
155e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
1561da177e4SLinus Torvalds 
157ab32ea5dSBrian Haley struct ipv6_devconf ipv6_devconf __read_mostly = {
1581da177e4SLinus Torvalds 	.forwarding		= 0,
1591da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1601da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1611da177e4SLinus Torvalds 	.accept_ra		= 1,
1621da177e4SLinus Torvalds 	.accept_redirects	= 1,
1631da177e4SLinus Torvalds 	.autoconf		= 1,
1641da177e4SLinus Torvalds 	.force_mld_version	= 0,
1651da177e4SLinus Torvalds 	.dad_transmits		= 1,
1661da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1671da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1681da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1691da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1701da177e4SLinus Torvalds 	.use_tempaddr 		= 0,
1711da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1721da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1731da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1741da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1751da177e4SLinus Torvalds #endif
1761da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
17765f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
178c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
179930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
180930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
18152e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
18209c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
18309c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
18409c884d4SYOSHIFUJI Hideaki #endif
185930d6ff2SYOSHIFUJI Hideaki #endif
186fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
1870bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
1881da177e4SLinus Torvalds };
1891da177e4SLinus Torvalds 
190ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
1911da177e4SLinus Torvalds 	.forwarding		= 0,
1921da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1931da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1941da177e4SLinus Torvalds 	.accept_ra		= 1,
1951da177e4SLinus Torvalds 	.accept_redirects	= 1,
1961da177e4SLinus Torvalds 	.autoconf		= 1,
1971da177e4SLinus Torvalds 	.dad_transmits		= 1,
1981da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1991da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
2001da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2011da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
2021da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2031da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2041da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2051da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2061da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2071da177e4SLinus Torvalds #endif
2081da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
20965f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
210c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
211930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
212930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
21352e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
21409c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
21509c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
21609c884d4SYOSHIFUJI Hideaki #endif
217930d6ff2SYOSHIFUJI Hideaki #endif
218fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2190bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
2201da177e4SLinus Torvalds };
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
2231da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
2241da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
2251da177e4SLinus Torvalds 
226f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
227f24e3d65SMitsuru Chinen static inline int addrconf_qdisc_ok(struct net_device *dev)
228f24e3d65SMitsuru Chinen {
229f24e3d65SMitsuru Chinen 	return (dev->qdisc != &noop_qdisc);
230f24e3d65SMitsuru Chinen }
231f24e3d65SMitsuru Chinen 
2321da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
2351da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds enum addrconf_timer_t
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	AC_NONE,
2411da177e4SLinus Torvalds 	AC_DAD,
2421da177e4SLinus Torvalds 	AC_RS,
2431da177e4SLinus Torvalds };
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
2461da177e4SLinus Torvalds 			       enum addrconf_timer_t what,
2471da177e4SLinus Torvalds 			       unsigned long when)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	if (!del_timer(&ifp->timer))
2501da177e4SLinus Torvalds 		in6_ifa_hold(ifp);
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	switch (what) {
2531da177e4SLinus Torvalds 	case AC_DAD:
2541da177e4SLinus Torvalds 		ifp->timer.function = addrconf_dad_timer;
2551da177e4SLinus Torvalds 		break;
2561da177e4SLinus Torvalds 	case AC_RS:
2571da177e4SLinus Torvalds 		ifp->timer.function = addrconf_rs_timer;
2581da177e4SLinus Torvalds 		break;
2591da177e4SLinus Torvalds 	default:;
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds 	ifp->timer.expires = jiffies + when;
2621da177e4SLinus Torvalds 	add_timer(&ifp->timer);
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
2657f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
2667f7d9a6bSHerbert Xu {
2677f7d9a6bSHerbert Xu 	if (snmp_mib_init((void **)idev->stats.ipv6,
268c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct ipstats_mib)) < 0)
2697f7d9a6bSHerbert Xu 		goto err_ip;
2707f7d9a6bSHerbert Xu 	if (snmp_mib_init((void **)idev->stats.icmpv6,
271c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct icmpv6_mib)) < 0)
2727f7d9a6bSHerbert Xu 		goto err_icmp;
27314878f75SDavid L Stevens 	if (snmp_mib_init((void **)idev->stats.icmpv6msg,
274c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct icmpv6msg_mib)) < 0)
27514878f75SDavid L Stevens 		goto err_icmpmsg;
2767f7d9a6bSHerbert Xu 
2777f7d9a6bSHerbert Xu 	return 0;
2787f7d9a6bSHerbert Xu 
27914878f75SDavid L Stevens err_icmpmsg:
28014878f75SDavid L Stevens 	snmp_mib_free((void **)idev->stats.icmpv6);
2817f7d9a6bSHerbert Xu err_icmp:
2827f7d9a6bSHerbert Xu 	snmp_mib_free((void **)idev->stats.ipv6);
2837f7d9a6bSHerbert Xu err_ip:
284aaf70ec7SPavel Emelyanov 	return -ENOMEM;
2857f7d9a6bSHerbert Xu }
2867f7d9a6bSHerbert Xu 
28716910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev)
2887f7d9a6bSHerbert Xu {
28914878f75SDavid L Stevens 	snmp_mib_free((void **)idev->stats.icmpv6msg);
2907f7d9a6bSHerbert Xu 	snmp_mib_free((void **)idev->stats.icmpv6);
2917f7d9a6bSHerbert Xu 	snmp_mib_free((void **)idev->stats.ipv6);
2927f7d9a6bSHerbert Xu }
2937f7d9a6bSHerbert Xu 
2941da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */
2951da177e4SLinus Torvalds 
2968814c4b5SYOSHIFUJI Hideaki static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
2978814c4b5SYOSHIFUJI Hideaki {
2988814c4b5SYOSHIFUJI Hideaki 	struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
2998814c4b5SYOSHIFUJI Hideaki 	kfree(idev);
3008814c4b5SYOSHIFUJI Hideaki }
3018814c4b5SYOSHIFUJI Hideaki 
3021da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev)
3031da177e4SLinus Torvalds {
3041da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3051da177e4SLinus Torvalds 	BUG_TRAP(idev->addr_list==NULL);
3061da177e4SLinus Torvalds 	BUG_TRAP(idev->mc_list==NULL);
3071da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
3081da177e4SLinus Torvalds 	printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
3091da177e4SLinus Torvalds #endif
3101da177e4SLinus Torvalds 	dev_put(dev);
3111da177e4SLinus Torvalds 	if (!idev->dead) {
3121da177e4SLinus Torvalds 		printk("Freeing alive inet6 device %p\n", idev);
3131da177e4SLinus Torvalds 		return;
3141da177e4SLinus Torvalds 	}
3151da177e4SLinus Torvalds 	snmp6_free_dev(idev);
3168814c4b5SYOSHIFUJI Hideaki 	call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
3197159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy);
3207159039aSYOSHIFUJI Hideaki 
3211da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	struct inet6_dev *ndev;
324d88ae4ccSYOSHIFUJI Hideaki 	struct in6_addr maddr;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	ASSERT_RTNL();
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
3291da177e4SLinus Torvalds 		return NULL;
3301da177e4SLinus Torvalds 
331322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
3321da177e4SLinus Torvalds 
333322f74a4SIngo Oeser 	if (ndev == NULL)
334322f74a4SIngo Oeser 		return NULL;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3371da177e4SLinus Torvalds 	ndev->dev = dev;
338441fc2a2SPavel Emelyanov 	memcpy(&ndev->cnf, dev->nd_net->ipv6.devconf_dflt, sizeof(ndev->cnf));
3391da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3401da177e4SLinus Torvalds 	ndev->cnf.sysctl = NULL;
3411da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
3421da177e4SLinus Torvalds 	if (ndev->nd_parms == NULL) {
3431da177e4SLinus Torvalds 		kfree(ndev);
3441da177e4SLinus Torvalds 		return NULL;
3451da177e4SLinus Torvalds 	}
3461da177e4SLinus Torvalds 	/* We refer to the device */
3471da177e4SLinus Torvalds 	dev_hold(dev);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
3501da177e4SLinus Torvalds 		ADBG((KERN_WARNING
3511da177e4SLinus Torvalds 			"%s(): cannot allocate memory for statistics; dev=%s.\n",
3521da177e4SLinus Torvalds 			__FUNCTION__, dev->name));
3531da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3541da177e4SLinus Torvalds 		ndev->dead = 1;
3551da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3561da177e4SLinus Torvalds 		return NULL;
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
3601da177e4SLinus Torvalds 		ADBG((KERN_WARNING
3611da177e4SLinus Torvalds 			"%s(): cannot create /proc/net/dev_snmp6/%s\n",
3621da177e4SLinus Torvalds 			__FUNCTION__, dev->name));
3631da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3641da177e4SLinus Torvalds 		ndev->dead = 1;
3651da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3661da177e4SLinus Torvalds 		return NULL;
3671da177e4SLinus Torvalds 	}
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3701da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3711da177e4SLinus Torvalds 	 */
3721da177e4SLinus Torvalds 	in6_dev_hold(ndev);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
375b24b8a24SPavel Emelyanov 	setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
3761da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
3771da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
3780be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
3790be669bbSJoerg Roedel 	    dev->type == ARPHRD_SIT ||
3800be669bbSJoerg Roedel #endif
3810be669bbSJoerg Roedel 	    dev->type == ARPHRD_NONE) {
3821da177e4SLinus Torvalds 		printk(KERN_INFO
383a1493d9cSDavid S. Miller 		       "%s: Disabled Privacy Extensions\n",
384a1493d9cSDavid S. Miller 		       dev->name);
3851da177e4SLinus Torvalds 		ndev->cnf.use_tempaddr = -1;
386c7dc89c0SFred L. Templin 
387c7dc89c0SFred L. Templin 		if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
388c7dc89c0SFred L. Templin 			printk(KERN_INFO
389c7dc89c0SFred L. Templin 			       "%s: Disabled Multicast RS\n",
390c7dc89c0SFred L. Templin 			       dev->name);
391c7dc89c0SFred L. Templin 			ndev->cnf.rtr_solicits = 0;
392c7dc89c0SFred L. Templin 		}
3931da177e4SLinus Torvalds 	} else {
3941da177e4SLinus Torvalds 		in6_dev_hold(ndev);
3951da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds #endif
3981da177e4SLinus Torvalds 
399f24e3d65SMitsuru Chinen 	if (netif_running(dev) && addrconf_qdisc_ok(dev))
40053aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
40153aadcc9SHerbert Xu 
4021da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4031da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
404f52295a9SPavel Emelyanov 	addrconf_sysctl_register(ndev);
40530c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
40630c4cf57SDavid L Stevens 	rcu_assign_pointer(dev->ip6_ptr, ndev);
407d88ae4ccSYOSHIFUJI Hideaki 
408d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
409d88ae4ccSYOSHIFUJI Hideaki 	ipv6_addr_all_nodes(&maddr);
410d88ae4ccSYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &maddr);
411d88ae4ccSYOSHIFUJI Hideaki 
4121da177e4SLinus Torvalds 	return ndev;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	struct inet6_dev *idev;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	ASSERT_RTNL();
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL) {
4221da177e4SLinus Torvalds 		if ((idev = ipv6_add_dev(dev)) == NULL)
4231da177e4SLinus Torvalds 			return NULL;
4241da177e4SLinus Torvalds 	}
425c5e33bddSYOSHIFUJI Hideaki 
4261da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4271da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4281da177e4SLinus Torvalds 	return idev;
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
4321da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	struct net_device *dev;
4351da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
4361da177e4SLinus Torvalds 	struct in6_addr addr;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	if (!idev)
4391da177e4SLinus Torvalds 		return;
4401da177e4SLinus Torvalds 	dev = idev->dev;
4411da177e4SLinus Torvalds 	if (dev && (dev->flags & IFF_MULTICAST)) {
4421da177e4SLinus Torvalds 		ipv6_addr_all_routers(&addr);
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
4451da177e4SLinus Torvalds 			ipv6_dev_mc_inc(dev, &addr);
4461da177e4SLinus Torvalds 		else
4471da177e4SLinus Torvalds 			ipv6_dev_mc_dec(dev, &addr);
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds 	for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
4502c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
4512c12a74cSMichal Wrobel 			continue;
4521da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
4531da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
4541da177e4SLinus Torvalds 		else
4551da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
4561da177e4SLinus Torvalds 	}
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 
460e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	struct net_device *dev;
4631da177e4SLinus Torvalds 	struct inet6_dev *idev;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
466bff16c2fSPavel Emelyanov 	for_each_netdev(net, dev) {
4678814c4b5SYOSHIFUJI Hideaki 		rcu_read_lock();
4681da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
4691da177e4SLinus Torvalds 		if (idev) {
470e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
471e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
4721da177e4SLinus Torvalds 			if (changed)
4731da177e4SLinus Torvalds 				dev_forward_change(idev);
4741da177e4SLinus Torvalds 		}
4758814c4b5SYOSHIFUJI Hideaki 		rcu_read_unlock();
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
4781da177e4SLinus Torvalds }
479c8fecf22SPavel Emelyanov 
480c8fecf22SPavel Emelyanov static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
481c8fecf22SPavel Emelyanov {
482bff16c2fSPavel Emelyanov 	struct net *net;
483bff16c2fSPavel Emelyanov 
484bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
485441fc2a2SPavel Emelyanov 	if (p == &net->ipv6.devconf_dflt->forwarding)
486c8fecf22SPavel Emelyanov 		return;
487c8fecf22SPavel Emelyanov 
488e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
489e186932bSPavel Emelyanov 		__s32 newf = net->ipv6.devconf_all->forwarding;
490e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
491e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
492c8fecf22SPavel Emelyanov 	} else if ((!*p) ^ (!old))
493c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
494c8fecf22SPavel Emelyanov 
495c8fecf22SPavel Emelyanov 	if (*p)
496c8fecf22SPavel Emelyanov 		rt6_purge_dflt_routers();
497c8fecf22SPavel Emelyanov }
4981da177e4SLinus Torvalds #endif
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds /* Nobody refers to this ifaddr, destroy it */
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	BUG_TRAP(ifp->if_next==NULL);
5051da177e4SLinus Torvalds 	BUG_TRAP(ifp->lst_next==NULL);
5061da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
5071da177e4SLinus Torvalds 	printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
5081da177e4SLinus Torvalds #endif
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
5131da177e4SLinus Torvalds 		printk("Timer is still running, when freeing ifa=%p\n", ifp);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	if (!ifp->dead) {
5161da177e4SLinus Torvalds 		printk("Freeing alive inet6 address %p\n", ifp);
5171da177e4SLinus Torvalds 		return;
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 	dst_release(&ifp->rt->u.dst);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	kfree(ifp);
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds 
524e55ffac6SBrian Haley static void
525e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
526e55ffac6SBrian Haley {
527e55ffac6SBrian Haley 	struct inet6_ifaddr *ifa, **ifap;
5288a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
529e55ffac6SBrian Haley 
530e55ffac6SBrian Haley 	/*
531e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
532e55ffac6SBrian Haley 	 * global before linklocal.
533e55ffac6SBrian Haley 	 */
534e55ffac6SBrian Haley 	for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
535e55ffac6SBrian Haley 	     ifap = &ifa->if_next) {
5368a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
537e55ffac6SBrian Haley 			break;
538e55ffac6SBrian Haley 	}
539e55ffac6SBrian Haley 
540e55ffac6SBrian Haley 	ifp->if_next = *ifap;
541e55ffac6SBrian Haley 	*ifap = ifp;
542e55ffac6SBrian Haley }
543e55ffac6SBrian Haley 
5441da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds static struct inet6_ifaddr *
5471da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
548e431b8c0SJamal Hadi Salim 	      int scope, u32 flags)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
5511da177e4SLinus Torvalds 	struct rt6_info *rt;
5521da177e4SLinus Torvalds 	int hash;
5531da177e4SLinus Torvalds 	int err = 0;
5541da177e4SLinus Torvalds 
5558814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
5561da177e4SLinus Torvalds 	if (idev->dead) {
5571da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
5581da177e4SLinus Torvalds 		goto out2;
5591da177e4SLinus Torvalds 	}
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	write_lock(&addrconf_hash_lock);
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
56406bfe655SDaniel Lezcano 	if (ipv6_chk_same_addr(&init_net, addr, idev->dev)) {
5651da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: already assigned\n"));
5661da177e4SLinus Torvalds 		err = -EEXIST;
5671da177e4SLinus Torvalds 		goto out;
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds 
570322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	if (ifa == NULL) {
5731da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: malloc failed\n"));
5741da177e4SLinus Torvalds 		err = -ENOBUFS;
5751da177e4SLinus Torvalds 		goto out;
5761da177e4SLinus Torvalds 	}
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	rt = addrconf_dst_alloc(idev, addr, 0);
5791da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
5801da177e4SLinus Torvalds 		err = PTR_ERR(rt);
5811da177e4SLinus Torvalds 		goto out;
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	ipv6_addr_copy(&ifa->addr, addr);
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
5871da177e4SLinus Torvalds 	init_timer(&ifa->timer);
5881da177e4SLinus Torvalds 	ifa->timer.data = (unsigned long) ifa;
5891da177e4SLinus Torvalds 	ifa->scope = scope;
5901da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
5911da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
5921da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
5931da177e4SLinus Torvalds 
59457f5f544SKeir Fraser 	ifa->rt = rt;
59557f5f544SKeir Fraser 
59695c385b4SNeil Horman 	/*
59795c385b4SNeil Horman 	 * part one of RFC 4429, section 3.3
59895c385b4SNeil Horman 	 * We should not configure an address as
59995c385b4SNeil Horman 	 * optimistic if we do not yet know the link
60095c385b4SNeil Horman 	 * layer address of our nexhop router
60195c385b4SNeil Horman 	 */
60295c385b4SNeil Horman 
60395c385b4SNeil Horman 	if (rt->rt6i_nexthop == NULL)
60495c385b4SNeil Horman 		ifa->flags &= ~IFA_F_OPTIMISTIC;
60595c385b4SNeil Horman 
6061da177e4SLinus Torvalds 	ifa->idev = idev;
6071da177e4SLinus Torvalds 	in6_dev_hold(idev);
6081da177e4SLinus Torvalds 	/* For caller */
6091da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	/* Add to big hash table */
6121da177e4SLinus Torvalds 	hash = ipv6_addr_hash(addr);
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	ifa->lst_next = inet6_addr_lst[hash];
6151da177e4SLinus Torvalds 	inet6_addr_lst[hash] = ifa;
6161da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6171da177e4SLinus Torvalds 	write_unlock(&addrconf_hash_lock);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	write_lock(&idev->lock);
6201da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
621e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
6241da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
6251da177e4SLinus Torvalds 		ifa->tmp_next = idev->tempaddr_list;
6261da177e4SLinus Torvalds 		idev->tempaddr_list = ifa;
6271da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds #endif
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6321da177e4SLinus Torvalds 	write_unlock(&idev->lock);
6331da177e4SLinus Torvalds out2:
6348814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
6351da177e4SLinus Torvalds 
636fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
637e041c683SAlan Stern 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
6381da177e4SLinus Torvalds 	else {
6391da177e4SLinus Torvalds 		kfree(ifa);
6401da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
6411da177e4SLinus Torvalds 	}
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	return ifa;
6441da177e4SLinus Torvalds out:
6451da177e4SLinus Torvalds 	write_unlock(&addrconf_hash_lock);
6461da177e4SLinus Torvalds 	goto out2;
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **ifap;
6541da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
6551da177e4SLinus Torvalds 	int hash;
6561da177e4SLinus Torvalds 	int deleted = 0, onlink = 0;
6571da177e4SLinus Torvalds 	unsigned long expires = jiffies;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	hash = ipv6_addr_hash(&ifp->addr);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	ifp->dead = 1;
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
6641da177e4SLinus Torvalds 	for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
6651da177e4SLinus Torvalds 	     ifap = &ifa->lst_next) {
6661da177e4SLinus Torvalds 		if (ifa == ifp) {
6671da177e4SLinus Torvalds 			*ifap = ifa->lst_next;
6681da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
6691da177e4SLinus Torvalds 			ifa->lst_next = NULL;
6701da177e4SLinus Torvalds 			break;
6711da177e4SLinus Torvalds 		}
6721da177e4SLinus Torvalds 	}
6731da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
6761da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
6771da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
6781da177e4SLinus Torvalds 		for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
6791da177e4SLinus Torvalds 		     ifap = &ifa->tmp_next) {
6801da177e4SLinus Torvalds 			if (ifa == ifp) {
6811da177e4SLinus Torvalds 				*ifap = ifa->tmp_next;
6821da177e4SLinus Torvalds 				if (ifp->ifpub) {
6831da177e4SLinus Torvalds 					in6_ifa_put(ifp->ifpub);
6841da177e4SLinus Torvalds 					ifp->ifpub = NULL;
6851da177e4SLinus Torvalds 				}
6861da177e4SLinus Torvalds 				__in6_ifa_put(ifp);
6871da177e4SLinus Torvalds 				ifa->tmp_next = NULL;
6881da177e4SLinus Torvalds 				break;
6891da177e4SLinus Torvalds 			}
6901da177e4SLinus Torvalds 		}
6911da177e4SLinus Torvalds 	}
6921da177e4SLinus Torvalds #endif
6931da177e4SLinus Torvalds 
6941d142804SKristian Slavov 	for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
6951da177e4SLinus Torvalds 		if (ifa == ifp) {
6961da177e4SLinus Torvalds 			*ifap = ifa->if_next;
6971da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
6981da177e4SLinus Torvalds 			ifa->if_next = NULL;
6991da177e4SLinus Torvalds 			if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
7001da177e4SLinus Torvalds 				break;
7011da177e4SLinus Torvalds 			deleted = 1;
7021d142804SKristian Slavov 			continue;
7031da177e4SLinus Torvalds 		} else if (ifp->flags & IFA_F_PERMANENT) {
7041da177e4SLinus Torvalds 			if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
7051da177e4SLinus Torvalds 					      ifp->prefix_len)) {
7061da177e4SLinus Torvalds 				if (ifa->flags & IFA_F_PERMANENT) {
7071da177e4SLinus Torvalds 					onlink = 1;
7081da177e4SLinus Torvalds 					if (deleted)
7091da177e4SLinus Torvalds 						break;
7101da177e4SLinus Torvalds 				} else {
7111da177e4SLinus Torvalds 					unsigned long lifetime;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 					if (!onlink)
7141da177e4SLinus Torvalds 						onlink = -1;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 					spin_lock(&ifa->lock);
7171da177e4SLinus Torvalds 					lifetime = min_t(unsigned long,
7181da177e4SLinus Torvalds 							 ifa->valid_lft, 0x7fffffffUL/HZ);
7191da177e4SLinus Torvalds 					if (time_before(expires,
7201da177e4SLinus Torvalds 							ifa->tstamp + lifetime * HZ))
7211da177e4SLinus Torvalds 						expires = ifa->tstamp + lifetime * HZ;
7221da177e4SLinus Torvalds 					spin_unlock(&ifa->lock);
7231da177e4SLinus Torvalds 				}
7241da177e4SLinus Torvalds 			}
7251da177e4SLinus Torvalds 		}
7261d142804SKristian Slavov 		ifap = &ifa->if_next;
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
7311da177e4SLinus Torvalds 
732e041c683SAlan Stern 	atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	addrconf_del_timer(ifp);
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	/*
7371da177e4SLinus Torvalds 	 * Purge or update corresponding prefix
7381da177e4SLinus Torvalds 	 *
7391da177e4SLinus Torvalds 	 * 1) we don't purge prefix here if address was not permanent.
7401da177e4SLinus Torvalds 	 *    prefix is managed by its own lifetime.
7411da177e4SLinus Torvalds 	 * 2) if there're no addresses, delete prefix.
7421da177e4SLinus Torvalds 	 * 3) if there're still other permanent address(es),
7431da177e4SLinus Torvalds 	 *    corresponding prefix is still permanent.
7441da177e4SLinus Torvalds 	 * 4) otherwise, update prefix lifetime to the
7451da177e4SLinus Torvalds 	 *    longest valid lifetime among the corresponding
7461da177e4SLinus Torvalds 	 *    addresses on the device.
7471da177e4SLinus Torvalds 	 *    Note: subsequent RA will update lifetime.
7481da177e4SLinus Torvalds 	 *
7491da177e4SLinus Torvalds 	 * --yoshfuji
7501da177e4SLinus Torvalds 	 */
7511da177e4SLinus Torvalds 	if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
7521da177e4SLinus Torvalds 		struct in6_addr prefix;
7531da177e4SLinus Torvalds 		struct rt6_info *rt;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 		ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
7561da177e4SLinus Torvalds 		rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 		if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
7591da177e4SLinus Torvalds 			if (onlink == 0) {
760e0a1ad73SThomas Graf 				ip6_del_rt(rt);
7611da177e4SLinus Torvalds 				rt = NULL;
7621da177e4SLinus Torvalds 			} else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
7631da177e4SLinus Torvalds 				rt->rt6i_expires = expires;
7641da177e4SLinus Torvalds 				rt->rt6i_flags |= RTF_EXPIRES;
7651da177e4SLinus Torvalds 			}
7661da177e4SLinus Torvalds 		}
7671da177e4SLinus Torvalds 		dst_release(&rt->u.dst);
7681da177e4SLinus Torvalds 	}
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	in6_ifa_put(ifp);
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
7741da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
7751da177e4SLinus Torvalds {
7761da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
7771da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
7781da177e4SLinus Torvalds 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
7791da177e4SLinus Torvalds 	int tmp_plen;
7801da177e4SLinus Torvalds 	int ret = 0;
7811da177e4SLinus Torvalds 	int max_addresses;
78295c385b4SNeil Horman 	u32 addr_flags;
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	write_lock(&idev->lock);
7851da177e4SLinus Torvalds 	if (ift) {
7861da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
7871da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
7881da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
7891da177e4SLinus Torvalds 		tmpaddr = &addr;
7901da177e4SLinus Torvalds 	} else {
7911da177e4SLinus Torvalds 		tmpaddr = NULL;
7921da177e4SLinus Torvalds 	}
7931da177e4SLinus Torvalds retry:
7941da177e4SLinus Torvalds 	in6_dev_hold(idev);
7951da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
7961da177e4SLinus Torvalds 		write_unlock(&idev->lock);
7971da177e4SLinus Torvalds 		printk(KERN_INFO
7981da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
7991da177e4SLinus Torvalds 		in6_dev_put(idev);
8001da177e4SLinus Torvalds 		ret = -1;
8011da177e4SLinus Torvalds 		goto out;
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
8041da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
8051da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
8061da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
8071da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8081da177e4SLinus Torvalds 		printk(KERN_WARNING
8091da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
8101da177e4SLinus Torvalds 		in6_dev_put(idev);
8111da177e4SLinus Torvalds 		ret = -1;
8121da177e4SLinus Torvalds 		goto out;
8131da177e4SLinus Torvalds 	}
8141da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
8151da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
8161da177e4SLinus Torvalds 	if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
8171da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
8181da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8191da177e4SLinus Torvalds 		printk(KERN_WARNING
8201da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
8211da177e4SLinus Torvalds 		in6_ifa_put(ifp);
8221da177e4SLinus Torvalds 		in6_dev_put(idev);
8231da177e4SLinus Torvalds 		ret = -1;
8241da177e4SLinus Torvalds 		goto out;
8251da177e4SLinus Torvalds 	}
8261da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
8271da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
8281da177e4SLinus Torvalds 			      ifp->valid_lft,
8291da177e4SLinus Torvalds 			      idev->cnf.temp_valid_lft);
8301da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
8311da177e4SLinus Torvalds 				 ifp->prefered_lft,
8321da177e4SLinus Torvalds 				 idev->cnf.temp_prefered_lft - desync_factor / HZ);
8331da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
8341da177e4SLinus Torvalds 	max_addresses = idev->cnf.max_addresses;
8351da177e4SLinus Torvalds 	tmp_cstamp = ifp->cstamp;
8361da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
8371da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	write_unlock(&idev->lock);
84095c385b4SNeil Horman 
84195c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
84295c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
84395c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
84495c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
84595c385b4SNeil Horman 
8461da177e4SLinus Torvalds 	ift = !max_addresses ||
8471da177e4SLinus Torvalds 	      ipv6_count_addresses(idev) < max_addresses ?
8481da177e4SLinus Torvalds 		ipv6_add_addr(idev, &addr, tmp_plen,
84995c385b4SNeil Horman 			      ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
85095c385b4SNeil Horman 			      addr_flags) : NULL;
8511da177e4SLinus Torvalds 	if (!ift || IS_ERR(ift)) {
8521da177e4SLinus Torvalds 		in6_ifa_put(ifp);
8531da177e4SLinus Torvalds 		in6_dev_put(idev);
8541da177e4SLinus Torvalds 		printk(KERN_INFO
8551da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): retry temporary address regeneration.\n");
8561da177e4SLinus Torvalds 		tmpaddr = &addr;
8571da177e4SLinus Torvalds 		write_lock(&idev->lock);
8581da177e4SLinus Torvalds 		goto retry;
8591da177e4SLinus Torvalds 	}
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
8621da177e4SLinus Torvalds 	ift->ifpub = ifp;
8631da177e4SLinus Torvalds 	ift->valid_lft = tmp_valid_lft;
8641da177e4SLinus Torvalds 	ift->prefered_lft = tmp_prefered_lft;
8651da177e4SLinus Torvalds 	ift->cstamp = tmp_cstamp;
8661da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
8671da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	addrconf_dad_start(ift, 0);
8701da177e4SLinus Torvalds 	in6_ifa_put(ift);
8711da177e4SLinus Torvalds 	in6_dev_put(idev);
8721da177e4SLinus Torvalds out:
8731da177e4SLinus Torvalds 	return ret;
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds #endif
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds /*
878072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
8791da177e4SLinus Torvalds  */
880072047e4SYOSHIFUJI Hideaki struct ipv6_saddr_score {
881072047e4SYOSHIFUJI Hideaki 	int		addr_type;
882072047e4SYOSHIFUJI Hideaki 	unsigned int	attrs;
883072047e4SYOSHIFUJI Hideaki 	int		matchlen;
8840d27b427SBrian Haley 	int		scope;
885072047e4SYOSHIFUJI Hideaki 	unsigned int	rule;
886072047e4SYOSHIFUJI Hideaki };
887072047e4SYOSHIFUJI Hideaki 
888072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_LOCAL		0x0001
889072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_PREFERRED	0x0004
890072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_HOA		0x0008
891072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_OIF		0x0010
892072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_LABEL		0x0020
893072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_PRIVACY	0x0040
894072047e4SYOSHIFUJI Hideaki 
895b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
8961da177e4SLinus Torvalds {
897072047e4SYOSHIFUJI Hideaki 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
898072047e4SYOSHIFUJI Hideaki 		    IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
899072047e4SYOSHIFUJI Hideaki 		return 1;
900072047e4SYOSHIFUJI Hideaki 	return 0;
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
903072047e4SYOSHIFUJI Hideaki int ipv6_dev_get_saddr(struct net_device *daddr_dev,
9041da177e4SLinus Torvalds 		       struct in6_addr *daddr, struct in6_addr *saddr)
9051da177e4SLinus Torvalds {
906072047e4SYOSHIFUJI Hideaki 	struct ipv6_saddr_score hiscore;
907072047e4SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa_result = NULL;
908072047e4SYOSHIFUJI Hideaki 	int daddr_type = __ipv6_addr_type(daddr);
909072047e4SYOSHIFUJI Hideaki 	int daddr_scope = __ipv6_addr_src_scope(daddr_type);
910303065a8SYOSHIFUJI Hideaki 	int daddr_ifindex = daddr_dev ? daddr_dev->ifindex : 0;
911303065a8SYOSHIFUJI Hideaki 	u32 daddr_label = ipv6_addr_label(daddr, daddr_type, daddr_ifindex);
912072047e4SYOSHIFUJI Hideaki 	struct net_device *dev;
9131da177e4SLinus Torvalds 
914072047e4SYOSHIFUJI Hideaki 	memset(&hiscore, 0, sizeof(hiscore));
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
9178814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
918072047e4SYOSHIFUJI Hideaki 
919881d966bSEric W. Biederman 	for_each_netdev(&init_net, dev) {
920072047e4SYOSHIFUJI Hideaki 		struct inet6_dev *idev;
921072047e4SYOSHIFUJI Hideaki 		struct inet6_ifaddr *ifa;
922072047e4SYOSHIFUJI Hideaki 
923072047e4SYOSHIFUJI Hideaki 		/* Rule 0: Candidate Source Address (section 4)
924072047e4SYOSHIFUJI Hideaki 		 *  - multicast and link-local destination address,
925072047e4SYOSHIFUJI Hideaki 		 *    the set of candidate source address MUST only
926072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
927072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same link as the outgoing
928072047e4SYOSHIFUJI Hideaki 		 *    interface.
929072047e4SYOSHIFUJI Hideaki 		 * (- For site-local destination addresses, the
930072047e4SYOSHIFUJI Hideaki 		 *    set of candidate source addresses MUST only
931072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
932072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same site as the outgoing
933072047e4SYOSHIFUJI Hideaki 		 *    interface.)
934072047e4SYOSHIFUJI Hideaki 		 */
935072047e4SYOSHIFUJI Hideaki 		if ((daddr_type & IPV6_ADDR_MULTICAST ||
936072047e4SYOSHIFUJI Hideaki 		     daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
937072047e4SYOSHIFUJI Hideaki 		    daddr_dev && dev != daddr_dev)
938072047e4SYOSHIFUJI Hideaki 			continue;
939072047e4SYOSHIFUJI Hideaki 
9401da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
941072047e4SYOSHIFUJI Hideaki 		if (!idev)
942072047e4SYOSHIFUJI Hideaki 			continue;
943072047e4SYOSHIFUJI Hideaki 
9441da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
945072047e4SYOSHIFUJI Hideaki 		for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
946072047e4SYOSHIFUJI Hideaki 			struct ipv6_saddr_score score;
947072047e4SYOSHIFUJI Hideaki 
948072047e4SYOSHIFUJI Hideaki 			score.addr_type = __ipv6_addr_type(&ifa->addr);
949072047e4SYOSHIFUJI Hideaki 
9506b3ae80aSYOSHIFUJI Hideaki 			/* Rule 0:
9516b3ae80aSYOSHIFUJI Hideaki 			 * - Tentative Address (RFC2462 section 5.4)
9526b3ae80aSYOSHIFUJI Hideaki 			 *  - A tentative address is not considered
9536b3ae80aSYOSHIFUJI Hideaki 			 *    "assigned to an interface" in the traditional
95495c385b4SNeil Horman 			 *    sense, unless it is also flagged as optimistic.
9556b3ae80aSYOSHIFUJI Hideaki 			 * - Candidate Source Address (section 4)
956072047e4SYOSHIFUJI Hideaki 			 *  - In any case, anycast addresses, multicast
957072047e4SYOSHIFUJI Hideaki 			 *    addresses, and the unspecified address MUST
958072047e4SYOSHIFUJI Hideaki 			 *    NOT be included in a candidate set.
959072047e4SYOSHIFUJI Hideaki 			 */
96095c385b4SNeil Horman 			if ((ifa->flags & IFA_F_TENTATIVE) &&
96195c385b4SNeil Horman 			    (!(ifa->flags & IFA_F_OPTIMISTIC)))
9626b3ae80aSYOSHIFUJI Hideaki 				continue;
963072047e4SYOSHIFUJI Hideaki 			if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
964072047e4SYOSHIFUJI Hideaki 				     score.addr_type & IPV6_ADDR_MULTICAST)) {
965072047e4SYOSHIFUJI Hideaki 				LIMIT_NETDEBUG(KERN_DEBUG
966072047e4SYOSHIFUJI Hideaki 					       "ADDRCONF: unspecified / multicast address "
967072047e4SYOSHIFUJI Hideaki 					       "assigned as unicast address on %s",
968072047e4SYOSHIFUJI Hideaki 					       dev->name);
9691da177e4SLinus Torvalds 				continue;
970072047e4SYOSHIFUJI Hideaki 			}
971072047e4SYOSHIFUJI Hideaki 
972072047e4SYOSHIFUJI Hideaki 			score.attrs = 0;
973072047e4SYOSHIFUJI Hideaki 			score.matchlen = 0;
974072047e4SYOSHIFUJI Hideaki 			score.scope = 0;
975072047e4SYOSHIFUJI Hideaki 			score.rule = 0;
976072047e4SYOSHIFUJI Hideaki 
977072047e4SYOSHIFUJI Hideaki 			if (ifa_result == NULL) {
978072047e4SYOSHIFUJI Hideaki 				/* record it if the first available entry */
979072047e4SYOSHIFUJI Hideaki 				goto record_it;
980072047e4SYOSHIFUJI Hideaki 			}
981072047e4SYOSHIFUJI Hideaki 
982072047e4SYOSHIFUJI Hideaki 			/* Rule 1: Prefer same address */
983072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 1) {
984072047e4SYOSHIFUJI Hideaki 				if (ipv6_addr_equal(&ifa_result->addr, daddr))
985072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
986072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
987072047e4SYOSHIFUJI Hideaki 			}
988072047e4SYOSHIFUJI Hideaki 			if (ipv6_addr_equal(&ifa->addr, daddr)) {
989072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_LOCAL;
990072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
991072047e4SYOSHIFUJI Hideaki 					score.rule = 1;
992072047e4SYOSHIFUJI Hideaki 					goto record_it;
993072047e4SYOSHIFUJI Hideaki 				}
994072047e4SYOSHIFUJI Hideaki 			} else {
995072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
996072047e4SYOSHIFUJI Hideaki 					continue;
997072047e4SYOSHIFUJI Hideaki 			}
998072047e4SYOSHIFUJI Hideaki 
999072047e4SYOSHIFUJI Hideaki 			/* Rule 2: Prefer appropriate scope */
1000072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 2) {
1001072047e4SYOSHIFUJI Hideaki 				hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
1002072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1003072047e4SYOSHIFUJI Hideaki 			}
1004072047e4SYOSHIFUJI Hideaki 			score.scope = __ipv6_addr_src_scope(score.addr_type);
1005072047e4SYOSHIFUJI Hideaki 			if (hiscore.scope < score.scope) {
1006072047e4SYOSHIFUJI Hideaki 				if (hiscore.scope < daddr_scope) {
1007072047e4SYOSHIFUJI Hideaki 					score.rule = 2;
1008072047e4SYOSHIFUJI Hideaki 					goto record_it;
1009072047e4SYOSHIFUJI Hideaki 				} else
1010072047e4SYOSHIFUJI Hideaki 					continue;
1011072047e4SYOSHIFUJI Hideaki 			} else if (score.scope < hiscore.scope) {
1012072047e4SYOSHIFUJI Hideaki 				if (score.scope < daddr_scope)
1013e55ffac6SBrian Haley 					break; /* addresses sorted by scope */
1014072047e4SYOSHIFUJI Hideaki 				else {
1015072047e4SYOSHIFUJI Hideaki 					score.rule = 2;
1016072047e4SYOSHIFUJI Hideaki 					goto record_it;
1017072047e4SYOSHIFUJI Hideaki 				}
1018072047e4SYOSHIFUJI Hideaki 			}
1019072047e4SYOSHIFUJI Hideaki 
102095c385b4SNeil Horman 			/* Rule 3: Avoid deprecated and optimistic addresses */
1021072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 3) {
1022072047e4SYOSHIFUJI Hideaki 				if (ipv6_saddr_preferred(hiscore.addr_type) ||
102395c385b4SNeil Horman 				   (((ifa_result->flags &
102495c385b4SNeil Horman 				    (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0)))
1025072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1026072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1027072047e4SYOSHIFUJI Hideaki 			}
1028072047e4SYOSHIFUJI Hideaki 			if (ipv6_saddr_preferred(score.addr_type) ||
10296ae5f983SJiri Kosina 			   (((ifa->flags &
103095c385b4SNeil Horman 			    (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0))) {
1031072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1032072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1033072047e4SYOSHIFUJI Hideaki 					score.rule = 3;
1034072047e4SYOSHIFUJI Hideaki 					goto record_it;
1035072047e4SYOSHIFUJI Hideaki 				}
1036072047e4SYOSHIFUJI Hideaki 			} else {
1037072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1038072047e4SYOSHIFUJI Hideaki 					continue;
1039072047e4SYOSHIFUJI Hideaki 			}
1040072047e4SYOSHIFUJI Hideaki 
10413b9f9a1cSNoriaki TAKAMIYA 			/* Rule 4: Prefer home address */
104259fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
10433b9f9a1cSNoriaki TAKAMIYA 			if (hiscore.rule < 4) {
10443b9f9a1cSNoriaki TAKAMIYA 				if (ifa_result->flags & IFA_F_HOMEADDRESS)
10453b9f9a1cSNoriaki TAKAMIYA 					hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
10463b9f9a1cSNoriaki TAKAMIYA 				hiscore.rule++;
10473b9f9a1cSNoriaki TAKAMIYA 			}
10483b9f9a1cSNoriaki TAKAMIYA 			if (ifa->flags & IFA_F_HOMEADDRESS) {
10493b9f9a1cSNoriaki TAKAMIYA 				score.attrs |= IPV6_SADDR_SCORE_HOA;
10503b9f9a1cSNoriaki TAKAMIYA 				if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
10513b9f9a1cSNoriaki TAKAMIYA 					score.rule = 4;
10523b9f9a1cSNoriaki TAKAMIYA 					goto record_it;
10533b9f9a1cSNoriaki TAKAMIYA 				}
10543b9f9a1cSNoriaki TAKAMIYA 			} else {
10553b9f9a1cSNoriaki TAKAMIYA 				if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
10563b9f9a1cSNoriaki TAKAMIYA 					continue;
10573b9f9a1cSNoriaki TAKAMIYA 			}
10583b9f9a1cSNoriaki TAKAMIYA #else
1059220bbd74SYOSHIFUJI Hideaki 			if (hiscore.rule < 4)
1060220bbd74SYOSHIFUJI Hideaki 				hiscore.rule++;
10613b9f9a1cSNoriaki TAKAMIYA #endif
1062072047e4SYOSHIFUJI Hideaki 
1063072047e4SYOSHIFUJI Hideaki 			/* Rule 5: Prefer outgoing interface */
1064072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 5) {
1065072047e4SYOSHIFUJI Hideaki 				if (daddr_dev == NULL ||
1066072047e4SYOSHIFUJI Hideaki 				    daddr_dev == ifa_result->idev->dev)
1067072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1068072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1069072047e4SYOSHIFUJI Hideaki 			}
1070072047e4SYOSHIFUJI Hideaki 			if (daddr_dev == NULL ||
1071072047e4SYOSHIFUJI Hideaki 			    daddr_dev == ifa->idev->dev) {
1072072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_OIF;
1073072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1074072047e4SYOSHIFUJI Hideaki 					score.rule = 5;
1075072047e4SYOSHIFUJI Hideaki 					goto record_it;
1076072047e4SYOSHIFUJI Hideaki 				}
1077072047e4SYOSHIFUJI Hideaki 			} else {
1078072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1079072047e4SYOSHIFUJI Hideaki 					continue;
1080072047e4SYOSHIFUJI Hideaki 			}
1081072047e4SYOSHIFUJI Hideaki 
1082072047e4SYOSHIFUJI Hideaki 			/* Rule 6: Prefer matching label */
1083072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 6) {
1084c1ee656cSYOSHIFUJI Hideaki 				if (ipv6_addr_label(&ifa_result->addr,
1085303065a8SYOSHIFUJI Hideaki 						    hiscore.addr_type,
1086303065a8SYOSHIFUJI Hideaki 						    ifa_result->idev->dev->ifindex) == daddr_label)
1087072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1088072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1089072047e4SYOSHIFUJI Hideaki 			}
1090c1ee656cSYOSHIFUJI Hideaki 			if (ipv6_addr_label(&ifa->addr,
1091303065a8SYOSHIFUJI Hideaki 					    score.addr_type,
1092303065a8SYOSHIFUJI Hideaki 					    ifa->idev->dev->ifindex) == daddr_label) {
1093072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_LABEL;
1094072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1095072047e4SYOSHIFUJI Hideaki 					score.rule = 6;
1096072047e4SYOSHIFUJI Hideaki 					goto record_it;
1097072047e4SYOSHIFUJI Hideaki 				}
1098072047e4SYOSHIFUJI Hideaki 			} else {
1099072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1100072047e4SYOSHIFUJI Hideaki 					continue;
1101072047e4SYOSHIFUJI Hideaki 			}
1102072047e4SYOSHIFUJI Hideaki 
110344fd0261SPeter Chubb #ifdef CONFIG_IPV6_PRIVACY
1104072047e4SYOSHIFUJI Hideaki 			/* Rule 7: Prefer public address
1105072047e4SYOSHIFUJI Hideaki 			 * Note: prefer temprary address if use_tempaddr >= 2
1106072047e4SYOSHIFUJI Hideaki 			 */
1107072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 7) {
1108072047e4SYOSHIFUJI Hideaki 				if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1109072047e4SYOSHIFUJI Hideaki 				    (ifa_result->idev->cnf.use_tempaddr >= 2))
1110072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1111072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1112072047e4SYOSHIFUJI Hideaki 			}
1113072047e4SYOSHIFUJI Hideaki 			if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1114072047e4SYOSHIFUJI Hideaki 			    (ifa->idev->cnf.use_tempaddr >= 2)) {
1115072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1116072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1117072047e4SYOSHIFUJI Hideaki 					score.rule = 7;
1118072047e4SYOSHIFUJI Hideaki 					goto record_it;
1119072047e4SYOSHIFUJI Hideaki 				}
1120072047e4SYOSHIFUJI Hideaki 			} else {
1121072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1122072047e4SYOSHIFUJI Hideaki 					continue;
1123072047e4SYOSHIFUJI Hideaki 			}
11245e2707faSYOSHIFUJI Hideaki #else
11255e2707faSYOSHIFUJI Hideaki 			if (hiscore.rule < 7)
11265e2707faSYOSHIFUJI Hideaki 				hiscore.rule++;
112744fd0261SPeter Chubb #endif
1128072047e4SYOSHIFUJI Hideaki 			/* Rule 8: Use longest matching prefix */
112912da2a43SYan Zheng 			if (hiscore.rule < 8) {
1130072047e4SYOSHIFUJI Hideaki 				hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
113112da2a43SYan Zheng 				hiscore.rule++;
113212da2a43SYan Zheng 			}
1133072047e4SYOSHIFUJI Hideaki 			score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1134072047e4SYOSHIFUJI Hideaki 			if (score.matchlen > hiscore.matchlen) {
1135072047e4SYOSHIFUJI Hideaki 				score.rule = 8;
1136072047e4SYOSHIFUJI Hideaki 				goto record_it;
1137072047e4SYOSHIFUJI Hideaki 			}
1138072047e4SYOSHIFUJI Hideaki #if 0
1139072047e4SYOSHIFUJI Hideaki 			else if (score.matchlen < hiscore.matchlen)
1140072047e4SYOSHIFUJI Hideaki 				continue;
11411da177e4SLinus Torvalds #endif
1142072047e4SYOSHIFUJI Hideaki 
1143072047e4SYOSHIFUJI Hideaki 			/* Final Rule: choose first available one */
11441da177e4SLinus Torvalds 			continue;
1145072047e4SYOSHIFUJI Hideaki record_it:
1146072047e4SYOSHIFUJI Hideaki 			if (ifa_result)
1147072047e4SYOSHIFUJI Hideaki 				in6_ifa_put(ifa_result);
1148072047e4SYOSHIFUJI Hideaki 			in6_ifa_hold(ifa);
1149072047e4SYOSHIFUJI Hideaki 			ifa_result = ifa;
11501da177e4SLinus Torvalds 			hiscore = score;
11511da177e4SLinus Torvalds 		}
11521da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
11531da177e4SLinus Torvalds 	}
11548814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
11551da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
11561da177e4SLinus Torvalds 
1157072047e4SYOSHIFUJI Hideaki 	if (!ifa_result)
1158072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
11591da177e4SLinus Torvalds 
1160072047e4SYOSHIFUJI Hideaki 	ipv6_addr_copy(saddr, &ifa_result->addr);
1161072047e4SYOSHIFUJI Hideaki 	in6_ifa_put(ifa_result);
1162072047e4SYOSHIFUJI Hideaki 	return 0;
11631da177e4SLinus Torvalds }
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds int ipv6_get_saddr(struct dst_entry *dst,
11671da177e4SLinus Torvalds 		   struct in6_addr *daddr, struct in6_addr *saddr)
11681da177e4SLinus Torvalds {
11697a3025b1SYOSHIFUJI Hideaki 	return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
11727159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_get_saddr);
11731da177e4SLinus Torvalds 
117495c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
117595c385b4SNeil Horman 		    unsigned char banned_flags)
11761da177e4SLinus Torvalds {
11771da177e4SLinus Torvalds 	struct inet6_dev *idev;
11781da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
11791da177e4SLinus Torvalds 
11808814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
11811da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) != NULL) {
11821da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
11851da177e4SLinus Torvalds 		for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
118695c385b4SNeil Horman 			if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
11871da177e4SLinus Torvalds 				ipv6_addr_copy(addr, &ifp->addr);
11881da177e4SLinus Torvalds 				err = 0;
11891da177e4SLinus Torvalds 				break;
11901da177e4SLinus Torvalds 			}
11911da177e4SLinus Torvalds 		}
11921da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
11931da177e4SLinus Torvalds 	}
11948814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
11951da177e4SLinus Torvalds 	return err;
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
11991da177e4SLinus Torvalds {
12001da177e4SLinus Torvalds 	int cnt = 0;
12011da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
12041da177e4SLinus Torvalds 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
12051da177e4SLinus Torvalds 		cnt++;
12061da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
12071da177e4SLinus Torvalds 	return cnt;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
1210bfeade08SDaniel Lezcano int ipv6_chk_addr(struct net *net, struct in6_addr *addr,
1211bfeade08SDaniel Lezcano 		  struct net_device *dev, int strict)
12121da177e4SLinus Torvalds {
12131da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
12141da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
12171da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1218bfeade08SDaniel Lezcano 		if (ifp->idev->dev->nd_net != net)
1219bfeade08SDaniel Lezcano 			continue;
12201da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
12211da177e4SLinus Torvalds 		    !(ifp->flags&IFA_F_TENTATIVE)) {
12221da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
12231da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
12241da177e4SLinus Torvalds 				break;
12251da177e4SLinus Torvalds 		}
12261da177e4SLinus Torvalds 	}
12271da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
12281da177e4SLinus Torvalds 	return ifp != NULL;
12291da177e4SLinus Torvalds }
12307159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr);
12317159039aSYOSHIFUJI Hideaki 
12321da177e4SLinus Torvalds static
123306bfe655SDaniel Lezcano int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
123406bfe655SDaniel Lezcano 		       struct net_device *dev)
12351da177e4SLinus Torvalds {
12361da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
12371da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
124006bfe655SDaniel Lezcano 		if (ifp->idev->dev->nd_net != net)
124106bfe655SDaniel Lezcano 			continue;
12421da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
12431da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev)
12441da177e4SLinus Torvalds 				break;
12451da177e4SLinus Torvalds 		}
12461da177e4SLinus Torvalds 	}
12471da177e4SLinus Torvalds 	return ifp != NULL;
12481da177e4SLinus Torvalds }
12491da177e4SLinus Torvalds 
12501cab3da6SDaniel Lezcano struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, struct in6_addr *addr,
12511cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
12521da177e4SLinus Torvalds {
12531da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
12541da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
12571da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
12581cab3da6SDaniel Lezcano 		if (ifp->idev->dev->nd_net != net)
12591cab3da6SDaniel Lezcano 			continue;
12601da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
12611da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
12621da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
12631da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
12641da177e4SLinus Torvalds 				break;
12651da177e4SLinus Torvalds 			}
12661da177e4SLinus Torvalds 		}
12671da177e4SLinus Torvalds 	}
12681da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	return ifp;
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
12741da177e4SLinus Torvalds {
12751da177e4SLinus Torvalds 	const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
12760fa1a53eSArnaldo Carvalho de Melo 	const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
127782103232SAl Viro 	__be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
127882103232SAl Viro 	__be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
12791da177e4SLinus Torvalds 	int sk_ipv6only = ipv6_only_sock(sk);
1280463c84b9SArnaldo Carvalho de Melo 	int sk2_ipv6only = inet_v6_ipv6only(sk2);
12811da177e4SLinus Torvalds 	int addr_type = ipv6_addr_type(sk_rcv_saddr6);
12821da177e4SLinus Torvalds 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	if (!sk2_rcv_saddr && !sk_ipv6only)
12851da177e4SLinus Torvalds 		return 1;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	if (addr_type2 == IPV6_ADDR_ANY &&
12881da177e4SLinus Torvalds 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
12891da177e4SLinus Torvalds 		return 1;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_ANY &&
12921da177e4SLinus Torvalds 	    !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
12931da177e4SLinus Torvalds 		return 1;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	if (sk2_rcv_saddr6 &&
12961da177e4SLinus Torvalds 	    ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
12971da177e4SLinus Torvalds 		return 1;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED &&
13001da177e4SLinus Torvalds 	    !sk2_ipv6only &&
13011da177e4SLinus Torvalds 	    (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
13021da177e4SLinus Torvalds 		return 1;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	return 0;
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
13081da177e4SLinus Torvalds 
13099f5336e2SAdrian Bunk static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
13101da177e4SLinus Torvalds {
13111da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
13121da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
13131da177e4SLinus Torvalds 		addrconf_del_timer(ifp);
13141da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
13151da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
13161da177e4SLinus Torvalds 		in6_ifa_put(ifp);
13171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
13181da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
13191da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
13201da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
13211da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
13221da177e4SLinus Torvalds 		if (ifpub) {
13231da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
13241da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
13251da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
13261da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
13271da177e4SLinus Torvalds 		} else {
13281da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
13291da177e4SLinus Torvalds 		}
13301da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
13311da177e4SLinus Torvalds #endif
13321da177e4SLinus Torvalds 	} else
13331da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
13363c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
13373c21edbdSYOSHIFUJI Hideaki {
13383c21edbdSYOSHIFUJI Hideaki 	if (net_ratelimit())
13393c21edbdSYOSHIFUJI Hideaki 		printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
13403c21edbdSYOSHIFUJI Hideaki 	addrconf_dad_stop(ifp);
13413c21edbdSYOSHIFUJI Hideaki }
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds /* Join to solicited addr multicast group. */
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
13461da177e4SLinus Torvalds {
13471da177e4SLinus Torvalds 	struct in6_addr maddr;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
13501da177e4SLinus Torvalds 		return;
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
13531da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
13541da177e4SLinus Torvalds }
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
13571da177e4SLinus Torvalds {
13581da177e4SLinus Torvalds 	struct in6_addr maddr;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
13611da177e4SLinus Torvalds 		return;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
13641da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
136720380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
13681da177e4SLinus Torvalds {
13691da177e4SLinus Torvalds 	struct in6_addr addr;
13701da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
13711da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
13721da177e4SLinus Torvalds 		return;
13731da177e4SLinus Torvalds 	ipv6_dev_ac_inc(ifp->idev->dev, &addr);
13741da177e4SLinus Torvalds }
13751da177e4SLinus Torvalds 
137620380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
13771da177e4SLinus Torvalds {
13781da177e4SLinus Torvalds 	struct in6_addr addr;
13791da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
13801da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
13811da177e4SLinus Torvalds 		return;
13821da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
13831da177e4SLinus Torvalds }
13841da177e4SLinus Torvalds 
1385073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
13861da177e4SLinus Torvalds {
13871da177e4SLinus Torvalds 	if (dev->addr_len != ETH_ALEN)
13881da177e4SLinus Torvalds 		return -1;
13891da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr, 3);
13901da177e4SLinus Torvalds 	memcpy(eui + 5, dev->dev_addr + 3, 3);
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	/*
13931da177e4SLinus Torvalds 	 * The zSeries OSA network cards can be shared among various
13941da177e4SLinus Torvalds 	 * OS instances, but the OSA cards have only one MAC address.
13951da177e4SLinus Torvalds 	 * This leads to duplicate address conflicts in conjunction
13961da177e4SLinus Torvalds 	 * with IPv6 if more than one instance uses the same card.
13971da177e4SLinus Torvalds 	 *
13981da177e4SLinus Torvalds 	 * The driver for these cards can deliver a unique 16-bit
13991da177e4SLinus Torvalds 	 * identifier for each instance sharing the same card.  It is
14001da177e4SLinus Torvalds 	 * placed instead of 0xFFFE in the interface identifier.  The
14011da177e4SLinus Torvalds 	 * "u" bit of the interface identifier is not inverted in this
14021da177e4SLinus Torvalds 	 * case.  Hence the resulting interface identifier has local
14031da177e4SLinus Torvalds 	 * scope according to RFC2373.
14041da177e4SLinus Torvalds 	 */
14051da177e4SLinus Torvalds 	if (dev->dev_id) {
14061da177e4SLinus Torvalds 		eui[3] = (dev->dev_id >> 8) & 0xFF;
14071da177e4SLinus Torvalds 		eui[4] = dev->dev_id & 0xFF;
14081da177e4SLinus Torvalds 	} else {
14091da177e4SLinus Torvalds 		eui[3] = 0xFF;
14101da177e4SLinus Torvalds 		eui[4] = 0xFE;
14111da177e4SLinus Torvalds 		eui[0] ^= 2;
14121da177e4SLinus Torvalds 	}
14131da177e4SLinus Torvalds 	return 0;
1414073a8e0eSYOSHIFUJI Hideaki }
1415073a8e0eSYOSHIFUJI Hideaki 
1416073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1417073a8e0eSYOSHIFUJI Hideaki {
14181da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
14191da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
14201da177e4SLinus Torvalds 		return -1;
14211da177e4SLinus Torvalds 	memset(eui, 0, 7);
14221da177e4SLinus Torvalds 	eui[7] = *(u8*)dev->dev_addr;
14231da177e4SLinus Torvalds 	return 0;
1424073a8e0eSYOSHIFUJI Hideaki }
1425073a8e0eSYOSHIFUJI Hideaki 
1426073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1427073a8e0eSYOSHIFUJI Hideaki {
14281da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
14291da177e4SLinus Torvalds 		return -1;
14301da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
14311da177e4SLinus Torvalds 	eui[0] |= 2;
14321da177e4SLinus Torvalds 	return 0;
14331da177e4SLinus Torvalds }
1434073a8e0eSYOSHIFUJI Hideaki 
1435073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1436073a8e0eSYOSHIFUJI Hideaki {
1437073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
1438073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
1439073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
1440073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_IEEE802_TR:
1441073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
1442073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
1443073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
1444073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
1445073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
1446c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
1447c7dc89c0SFred L. Templin 		if (dev->priv_flags & IFF_ISATAP)
1448c7dc89c0SFred L. Templin 			return ipv6_isatap_eui64(eui, *(__be32 *)dev->dev_addr);
1449073a8e0eSYOSHIFUJI Hideaki 	}
14501da177e4SLinus Torvalds 	return -1;
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
14541da177e4SLinus Torvalds {
14551da177e4SLinus Torvalds 	int err = -1;
14561da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
14591da177e4SLinus Torvalds 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
14601da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
14611da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
14621da177e4SLinus Torvalds 			err = 0;
14631da177e4SLinus Torvalds 			break;
14641da177e4SLinus Torvalds 		}
14651da177e4SLinus Torvalds 	}
14661da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
14671da177e4SLinus Torvalds 	return err;
14681da177e4SLinus Torvalds }
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
14711da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
14721da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev)
14731da177e4SLinus Torvalds {
14741da177e4SLinus Torvalds regen:
1475955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
14761da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	/*
14791da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
14801da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
14811da177e4SLinus Torvalds 	 *
14821da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
14831da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
1484c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
14851da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
14861da177e4SLinus Torvalds 	 *  - value 0
14871da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
14881da177e4SLinus Torvalds 	 */
14891da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
14901da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
14911da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
14921da177e4SLinus Torvalds 		goto regen;
14931da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
14941da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
14951da177e4SLinus Torvalds 			goto regen;
14961da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
14971da177e4SLinus Torvalds 			goto regen;
14981da177e4SLinus Torvalds 	}
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 	return 0;
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
15041da177e4SLinus Torvalds {
15051da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
15061da177e4SLinus Torvalds 	unsigned long expires;
15071da177e4SLinus Torvalds 
15088814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
15091da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	if (idev->dead)
15121da177e4SLinus Torvalds 		goto out;
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds 	if (__ipv6_regen_rndid(idev) < 0)
15151da177e4SLinus Torvalds 		goto out;
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	expires = jiffies +
15181da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
15191da177e4SLinus Torvalds 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
15201da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
15211da177e4SLinus Torvalds 		printk(KERN_WARNING
15221da177e4SLinus Torvalds 			"ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
15231da177e4SLinus Torvalds 			idev->dev->name);
15241da177e4SLinus Torvalds 		goto out;
15251da177e4SLinus Torvalds 	}
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
15281da177e4SLinus Torvalds 		in6_dev_hold(idev);
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds out:
15311da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
15328814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
15331da177e4SLinus Torvalds 	in6_dev_put(idev);
15341da177e4SLinus Torvalds }
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
15371da177e4SLinus Torvalds 	int ret = 0;
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
15401da177e4SLinus Torvalds 		ret = __ipv6_regen_rndid(idev);
15411da177e4SLinus Torvalds 	return ret;
15421da177e4SLinus Torvalds }
15431da177e4SLinus Torvalds #endif
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds /*
15461da177e4SLinus Torvalds  *	Add prefix route.
15471da177e4SLinus Torvalds  */
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds static void
15501da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1551e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
15521da177e4SLinus Torvalds {
155386872cb5SThomas Graf 	struct fib6_config cfg = {
155486872cb5SThomas Graf 		.fc_table = RT6_TABLE_PREFIX,
155586872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
155686872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
155786872cb5SThomas Graf 		.fc_expires = expires,
155886872cb5SThomas Graf 		.fc_dst_len = plen,
155986872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
1560*f1243c2dSBenjamin Thery 		.fc_nlinfo.nl_net = &init_net,
156186872cb5SThomas Graf 	};
15621da177e4SLinus Torvalds 
156386872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, pfx);
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
15661da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
15671da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
15681da177e4SLinus Torvalds 	 */
15690be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
15701da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
157186872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
15720be669bbSJoerg Roedel #endif
15731da177e4SLinus Torvalds 
157486872cb5SThomas Graf 	ip6_route_add(&cfg);
15751da177e4SLinus Torvalds }
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
15801da177e4SLinus Torvalds {
158186872cb5SThomas Graf 	struct fib6_config cfg = {
158286872cb5SThomas Graf 		.fc_table = RT6_TABLE_LOCAL,
158386872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
158486872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
158586872cb5SThomas Graf 		.fc_dst_len = 8,
158686872cb5SThomas Graf 		.fc_flags = RTF_UP,
1587*f1243c2dSBenjamin Thery 		.fc_nlinfo.nl_net = &init_net,
158886872cb5SThomas Graf 	};
15891da177e4SLinus Torvalds 
159086872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
159186872cb5SThomas Graf 
159286872cb5SThomas Graf 	ip6_route_add(&cfg);
15931da177e4SLinus Torvalds }
15941da177e4SLinus Torvalds 
15950be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
15961da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev)
15971da177e4SLinus Torvalds {
159886872cb5SThomas Graf 	struct fib6_config cfg = {
159986872cb5SThomas Graf 		.fc_table = RT6_TABLE_MAIN,
160086872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
160186872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
160286872cb5SThomas Graf 		.fc_dst_len = 96,
160386872cb5SThomas Graf 		.fc_flags = RTF_UP | RTF_NONEXTHOP,
1604*f1243c2dSBenjamin Thery 		.fc_nlinfo.nl_net = &init_net,
160586872cb5SThomas Graf 	};
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 	/* prefix length - 96 bits "::d.d.d.d" */
160886872cb5SThomas Graf 	ip6_route_add(&cfg);
16091da177e4SLinus Torvalds }
16100be669bbSJoerg Roedel #endif
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev)
16131da177e4SLinus Torvalds {
16141da177e4SLinus Torvalds 	struct in6_addr addr;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
16171da177e4SLinus Torvalds 	addrconf_prefix_route(&addr, 64, dev, 0, 0);
16181da177e4SLinus Torvalds }
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
16211da177e4SLinus Torvalds {
16221da177e4SLinus Torvalds 	struct inet6_dev *idev;
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	ASSERT_RTNL();
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL)
16271da177e4SLinus Torvalds 		return NULL;
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	/* Add default multicast route */
16301da177e4SLinus Torvalds 	addrconf_add_mroute(dev);
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 	/* Add link local route */
16331da177e4SLinus Torvalds 	addrconf_add_lroute(dev);
16341da177e4SLinus Torvalds 	return idev;
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
16381da177e4SLinus Torvalds {
16391da177e4SLinus Torvalds 	struct prefix_info *pinfo;
16401da177e4SLinus Torvalds 	__u32 valid_lft;
16411da177e4SLinus Torvalds 	__u32 prefered_lft;
16421da177e4SLinus Torvalds 	int addr_type;
16431da177e4SLinus Torvalds 	unsigned long rt_expires;
16441da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
16491da177e4SLinus Torvalds 		ADBG(("addrconf: prefix option too short\n"));
16501da177e4SLinus Torvalds 		return;
16511da177e4SLinus Torvalds 	}
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 	/*
16541da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
16551da177e4SLinus Torvalds 	 */
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
16601da177e4SLinus Torvalds 		return;
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
16631da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
16661da177e4SLinus Torvalds 		if (net_ratelimit())
16671da177e4SLinus Torvalds 			printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
16681da177e4SLinus Torvalds 		return;
16691da177e4SLinus Torvalds 	}
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	if (in6_dev == NULL) {
16741da177e4SLinus Torvalds 		if (net_ratelimit())
16751da177e4SLinus Torvalds 			printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
16761da177e4SLinus Torvalds 		return;
16771da177e4SLinus Torvalds 	}
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	/*
16801da177e4SLinus Torvalds 	 *	Two things going on here:
16811da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
16821da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
16831da177e4SLinus Torvalds 	 */
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds 	/* Avoid arithmetic overflow. Really, we could
16861da177e4SLinus Torvalds 	   save rt_expires in seconds, likely valid_lft,
16871da177e4SLinus Torvalds 	   but it would require division in fib gc, that it
16881da177e4SLinus Torvalds 	   not good.
16891da177e4SLinus Torvalds 	 */
16901da177e4SLinus Torvalds 	if (valid_lft >= 0x7FFFFFFF/HZ)
16913dd4bc68SYOSHIFUJI Hideaki 		rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
16921da177e4SLinus Torvalds 	else
16933dd4bc68SYOSHIFUJI Hideaki 		rt_expires = valid_lft * HZ;
16943dd4bc68SYOSHIFUJI Hideaki 
16953dd4bc68SYOSHIFUJI Hideaki 	/*
16963dd4bc68SYOSHIFUJI Hideaki 	 * We convert this (in jiffies) to clock_t later.
16973dd4bc68SYOSHIFUJI Hideaki 	 * Avoid arithmetic overflow there as well.
16983dd4bc68SYOSHIFUJI Hideaki 	 * Overflow can happen only if HZ < USER_HZ.
16993dd4bc68SYOSHIFUJI Hideaki 	 */
17003dd4bc68SYOSHIFUJI Hideaki 	if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
17013dd4bc68SYOSHIFUJI Hideaki 		rt_expires = 0x7FFFFFFF / USER_HZ;
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	if (pinfo->onlink) {
17041da177e4SLinus Torvalds 		struct rt6_info *rt;
17051da177e4SLinus Torvalds 		rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 		if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
17081da177e4SLinus Torvalds 			if (rt->rt6i_flags&RTF_EXPIRES) {
17091da177e4SLinus Torvalds 				if (valid_lft == 0) {
1710e0a1ad73SThomas Graf 					ip6_del_rt(rt);
17111da177e4SLinus Torvalds 					rt = NULL;
17121da177e4SLinus Torvalds 				} else {
17133dd4bc68SYOSHIFUJI Hideaki 					rt->rt6i_expires = jiffies + rt_expires;
17141da177e4SLinus Torvalds 				}
17151da177e4SLinus Torvalds 			}
17161da177e4SLinus Torvalds 		} else if (valid_lft) {
17171da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
17183dd4bc68SYOSHIFUJI Hideaki 					      dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
17191da177e4SLinus Torvalds 		}
17201da177e4SLinus Torvalds 		if (rt)
17211da177e4SLinus Torvalds 			dst_release(&rt->u.dst);
17221da177e4SLinus Torvalds 	}
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
17271da177e4SLinus Torvalds 		struct inet6_ifaddr * ifp;
17281da177e4SLinus Torvalds 		struct in6_addr addr;
17291da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
17321da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
17331da177e4SLinus Torvalds 			if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
17341da177e4SLinus Torvalds 			    ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
17351da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
17361da177e4SLinus Torvalds 				return;
17371da177e4SLinus Torvalds 			}
17381da177e4SLinus Torvalds 			goto ok;
17391da177e4SLinus Torvalds 		}
17401da177e4SLinus Torvalds 		if (net_ratelimit())
17411da177e4SLinus Torvalds 			printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
17421da177e4SLinus Torvalds 			       pinfo->prefix_len);
17431da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
17441da177e4SLinus Torvalds 		return;
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds ok:
17471da177e4SLinus Torvalds 
17481cab3da6SDaniel Lezcano 		ifp = ipv6_get_ifaddr(&init_net, &addr, dev, 1);
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds 		if (ifp == NULL && valid_lft) {
17511da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
175295c385b4SNeil Horman 			u32 addr_flags = 0;
175395c385b4SNeil Horman 
175495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
175595c385b4SNeil Horman 			if (in6_dev->cnf.optimistic_dad &&
175695c385b4SNeil Horman 			    !ipv6_devconf.forwarding)
175795c385b4SNeil Horman 				addr_flags = IFA_F_OPTIMISTIC;
175895c385b4SNeil Horman #endif
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
17611da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
17621da177e4SLinus Torvalds 			 */
17631da177e4SLinus Torvalds 			if (!max_addresses ||
17641da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
17651da177e4SLinus Torvalds 				ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
176695c385b4SNeil Horman 						    addr_type&IPV6_ADDR_SCOPE_MASK,
176795c385b4SNeil Horman 						    addr_flags);
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 			if (!ifp || IS_ERR(ifp)) {
17701da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
17711da177e4SLinus Torvalds 				return;
17721da177e4SLinus Torvalds 			}
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 			update_lft = create = 1;
17751da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
17761da177e4SLinus Torvalds 			addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
17771da177e4SLinus Torvalds 		}
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 		if (ifp) {
17801da177e4SLinus Torvalds 			int flags;
17811da177e4SLinus Torvalds 			unsigned long now;
17821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
17831da177e4SLinus Torvalds 			struct inet6_ifaddr *ift;
17841da177e4SLinus Torvalds #endif
17851da177e4SLinus Torvalds 			u32 stored_lft;
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
17881da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
17891da177e4SLinus Torvalds 			now = jiffies;
17901da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
17911da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
17921da177e4SLinus Torvalds 			else
17931da177e4SLinus Torvalds 				stored_lft = 0;
17941da177e4SLinus Torvalds 			if (!update_lft && stored_lft) {
17951da177e4SLinus Torvalds 				if (valid_lft > MIN_VALID_LIFETIME ||
17961da177e4SLinus Torvalds 				    valid_lft > stored_lft)
17971da177e4SLinus Torvalds 					update_lft = 1;
17981da177e4SLinus Torvalds 				else if (stored_lft <= MIN_VALID_LIFETIME) {
17991da177e4SLinus Torvalds 					/* valid_lft <= stored_lft is always true */
18001da177e4SLinus Torvalds 					/* XXX: IPsec */
18011da177e4SLinus Torvalds 					update_lft = 0;
18021da177e4SLinus Torvalds 				} else {
18031da177e4SLinus Torvalds 					valid_lft = MIN_VALID_LIFETIME;
18041da177e4SLinus Torvalds 					if (valid_lft < prefered_lft)
18051da177e4SLinus Torvalds 						prefered_lft = valid_lft;
18061da177e4SLinus Torvalds 					update_lft = 1;
18071da177e4SLinus Torvalds 				}
18081da177e4SLinus Torvalds 			}
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 			if (update_lft) {
18111da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
18121da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
18131da177e4SLinus Torvalds 				ifp->tstamp = now;
18141da177e4SLinus Torvalds 				flags = ifp->flags;
18151da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
18161da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
18191da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
18201da177e4SLinus Torvalds 			} else
18211da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
18241da177e4SLinus Torvalds 			read_lock_bh(&in6_dev->lock);
18251da177e4SLinus Torvalds 			/* update all temporary addresses in the list */
18261da177e4SLinus Torvalds 			for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
18271da177e4SLinus Torvalds 				/*
18281da177e4SLinus Torvalds 				 * When adjusting the lifetimes of an existing
18291da177e4SLinus Torvalds 				 * temporary address, only lower the lifetimes.
18301da177e4SLinus Torvalds 				 * Implementations must not increase the
18311da177e4SLinus Torvalds 				 * lifetimes of an existing temporary address
18321da177e4SLinus Torvalds 				 * when processing a Prefix Information Option.
18331da177e4SLinus Torvalds 				 */
18341da177e4SLinus Torvalds 				spin_lock(&ift->lock);
18351da177e4SLinus Torvalds 				flags = ift->flags;
18361da177e4SLinus Torvalds 				if (ift->valid_lft > valid_lft &&
18371da177e4SLinus Torvalds 				    ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
18381da177e4SLinus Torvalds 					ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
18391da177e4SLinus Torvalds 				if (ift->prefered_lft > prefered_lft &&
18401da177e4SLinus Torvalds 				    ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
18411da177e4SLinus Torvalds 					ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
18421da177e4SLinus Torvalds 				spin_unlock(&ift->lock);
18431da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
18441da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ift);
18451da177e4SLinus Torvalds 			}
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 			if (create && in6_dev->cnf.use_tempaddr > 0) {
18481da177e4SLinus Torvalds 				/*
18491da177e4SLinus Torvalds 				 * When a new public address is created as described in [ADDRCONF],
18501da177e4SLinus Torvalds 				 * also create a new temporary address.
18511da177e4SLinus Torvalds 				 */
18521da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
18531da177e4SLinus Torvalds 				ipv6_create_tempaddr(ifp, NULL);
18541da177e4SLinus Torvalds 			} else {
18551da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
18561da177e4SLinus Torvalds 			}
18571da177e4SLinus Torvalds #endif
18581da177e4SLinus Torvalds 			in6_ifa_put(ifp);
18591da177e4SLinus Torvalds 			addrconf_verify(0);
18601da177e4SLinus Torvalds 		}
18611da177e4SLinus Torvalds 	}
18621da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
18631da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
18641da177e4SLinus Torvalds }
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds /*
18671da177e4SLinus Torvalds  *	Set destination address.
18681da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
18691da177e4SLinus Torvalds  *	device.
18701da177e4SLinus Torvalds  */
18711da177e4SLinus Torvalds int addrconf_set_dstaddr(void __user *arg)
18721da177e4SLinus Torvalds {
18731da177e4SLinus Torvalds 	struct in6_ifreq ireq;
18741da177e4SLinus Torvalds 	struct net_device *dev;
18751da177e4SLinus Torvalds 	int err = -EINVAL;
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 	rtnl_lock();
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 	err = -EFAULT;
18801da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
18811da177e4SLinus Torvalds 		goto err_exit;
18821da177e4SLinus Torvalds 
1883881d966bSEric W. Biederman 	dev = __dev_get_by_index(&init_net, ireq.ifr6_ifindex);
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 	err = -ENODEV;
18861da177e4SLinus Torvalds 	if (dev == NULL)
18871da177e4SLinus Torvalds 		goto err_exit;
18881da177e4SLinus Torvalds 
18890be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
18901da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
18911da177e4SLinus Torvalds 		struct ifreq ifr;
18921da177e4SLinus Torvalds 		mm_segment_t	oldfs;
18931da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
18961da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
18971da177e4SLinus Torvalds 			goto err_exit;
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
19001da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
19011da177e4SLinus Torvalds 		p.iph.saddr = 0;
19021da177e4SLinus Torvalds 		p.iph.version = 4;
19031da177e4SLinus Torvalds 		p.iph.ihl = 5;
19041da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
19051da177e4SLinus Torvalds 		p.iph.ttl = 64;
1906d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds 		oldfs = get_fs(); set_fs(KERNEL_DS);
19091da177e4SLinus Torvalds 		err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
19101da177e4SLinus Torvalds 		set_fs(oldfs);
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 		if (err == 0) {
19131da177e4SLinus Torvalds 			err = -ENOBUFS;
1914881d966bSEric W. Biederman 			if ((dev = __dev_get_by_name(&init_net, p.name)) == NULL)
19151da177e4SLinus Torvalds 				goto err_exit;
19161da177e4SLinus Torvalds 			err = dev_open(dev);
19171da177e4SLinus Torvalds 		}
19181da177e4SLinus Torvalds 	}
19190be669bbSJoerg Roedel #endif
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds err_exit:
19221da177e4SLinus Torvalds 	rtnl_unlock();
19231da177e4SLinus Torvalds 	return err;
19241da177e4SLinus Torvalds }
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds /*
19271da177e4SLinus Torvalds  *	Manual configuration of address on an interface
19281da177e4SLinus Torvalds  */
19290778769dSNoriaki TAKAMIYA static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
193055ebaef1SNoriaki TAKAMIYA 			  __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
19311da177e4SLinus Torvalds {
19321da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
19331da177e4SLinus Torvalds 	struct inet6_dev *idev;
19341da177e4SLinus Torvalds 	struct net_device *dev;
19351da177e4SLinus Torvalds 	int scope;
193646d48046SYOSHIFUJI Hideaki 	u32 flags = RTF_EXPIRES;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	ASSERT_RTNL();
19391da177e4SLinus Torvalds 
19400778769dSNoriaki TAKAMIYA 	/* check the lifetime */
19410778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
19420778769dSNoriaki TAKAMIYA 		return -EINVAL;
19430778769dSNoriaki TAKAMIYA 
1944881d966bSEric W. Biederman 	if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
19451da177e4SLinus Torvalds 		return -ENODEV;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL)
19481da177e4SLinus Torvalds 		return -ENOBUFS;
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
19511da177e4SLinus Torvalds 
195246d48046SYOSHIFUJI Hideaki 	if (valid_lft == INFINITY_LIFE_TIME) {
19530778769dSNoriaki TAKAMIYA 		ifa_flags |= IFA_F_PERMANENT;
195446d48046SYOSHIFUJI Hideaki 		flags = 0;
195546d48046SYOSHIFUJI Hideaki 	} else if (valid_lft >= 0x7FFFFFFF/HZ)
19560778769dSNoriaki TAKAMIYA 		valid_lft = 0x7FFFFFFF/HZ;
19570778769dSNoriaki TAKAMIYA 
19580778769dSNoriaki TAKAMIYA 	if (prefered_lft == 0)
19590778769dSNoriaki TAKAMIYA 		ifa_flags |= IFA_F_DEPRECATED;
19600778769dSNoriaki TAKAMIYA 	else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
19610778769dSNoriaki TAKAMIYA 		 (prefered_lft != INFINITY_LIFE_TIME))
19620778769dSNoriaki TAKAMIYA 		prefered_lft = 0x7FFFFFFF/HZ;
19630778769dSNoriaki TAKAMIYA 
19640778769dSNoriaki TAKAMIYA 	ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
19650778769dSNoriaki TAKAMIYA 
19661da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
196706aebfb7SHerbert Xu 		spin_lock_bh(&ifp->lock);
19680778769dSNoriaki TAKAMIYA 		ifp->valid_lft = valid_lft;
19690778769dSNoriaki TAKAMIYA 		ifp->prefered_lft = prefered_lft;
19700778769dSNoriaki TAKAMIYA 		ifp->tstamp = jiffies;
197106aebfb7SHerbert Xu 		spin_unlock_bh(&ifp->lock);
19720778769dSNoriaki TAKAMIYA 
197346d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
197446d48046SYOSHIFUJI Hideaki 				      jiffies_to_clock_t(valid_lft * HZ), flags);
197595c385b4SNeil Horman 		/*
197695c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
197795c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
197895c385b4SNeil Horman 		 * manually configured addresses
197995c385b4SNeil Horman 		 */
19801da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
19811da177e4SLinus Torvalds 		in6_ifa_put(ifp);
19820778769dSNoriaki TAKAMIYA 		addrconf_verify(0);
19831da177e4SLinus Torvalds 		return 0;
19841da177e4SLinus Torvalds 	}
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds 	return PTR_ERR(ifp);
19871da177e4SLinus Torvalds }
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
19901da177e4SLinus Torvalds {
19911da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
19921da177e4SLinus Torvalds 	struct inet6_dev *idev;
19931da177e4SLinus Torvalds 	struct net_device *dev;
19941da177e4SLinus Torvalds 
1995881d966bSEric W. Biederman 	if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
19961da177e4SLinus Torvalds 		return -ENODEV;
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL)
19991da177e4SLinus Torvalds 		return -ENXIO;
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
20021da177e4SLinus Torvalds 	for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
20031da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
20041da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
20051da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
20061da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
20071da177e4SLinus Torvalds 
20081da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds 			/* If the last address is deleted administratively,
20111da177e4SLinus Torvalds 			   disable IPv6 on this interface.
20121da177e4SLinus Torvalds 			 */
20131da177e4SLinus Torvalds 			if (idev->addr_list == NULL)
20141da177e4SLinus Torvalds 				addrconf_ifdown(idev->dev, 1);
20151da177e4SLinus Torvalds 			return 0;
20161da177e4SLinus Torvalds 		}
20171da177e4SLinus Torvalds 	}
20181da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
20191da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
20201da177e4SLinus Torvalds }
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds int addrconf_add_ifaddr(void __user *arg)
20241da177e4SLinus Torvalds {
20251da177e4SLinus Torvalds 	struct in6_ifreq ireq;
20261da177e4SLinus Torvalds 	int err;
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
20291da177e4SLinus Torvalds 		return -EPERM;
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
20321da177e4SLinus Torvalds 		return -EFAULT;
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 	rtnl_lock();
20350778769dSNoriaki TAKAMIYA 	err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
203655ebaef1SNoriaki TAKAMIYA 			     IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
20371da177e4SLinus Torvalds 	rtnl_unlock();
20381da177e4SLinus Torvalds 	return err;
20391da177e4SLinus Torvalds }
20401da177e4SLinus Torvalds 
20411da177e4SLinus Torvalds int addrconf_del_ifaddr(void __user *arg)
20421da177e4SLinus Torvalds {
20431da177e4SLinus Torvalds 	struct in6_ifreq ireq;
20441da177e4SLinus Torvalds 	int err;
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
20471da177e4SLinus Torvalds 		return -EPERM;
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
20501da177e4SLinus Torvalds 		return -EFAULT;
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 	rtnl_lock();
20531da177e4SLinus Torvalds 	err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
20541da177e4SLinus Torvalds 	rtnl_unlock();
20551da177e4SLinus Torvalds 	return err;
20561da177e4SLinus Torvalds }
20571da177e4SLinus Torvalds 
20580be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
20591da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
20601da177e4SLinus Torvalds {
20611da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
20621da177e4SLinus Torvalds 	struct in6_addr addr;
20631da177e4SLinus Torvalds 	struct net_device *dev;
20641da177e4SLinus Torvalds 	int scope;
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	ASSERT_RTNL();
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
20691da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
20721da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
20731da177e4SLinus Torvalds 		scope = IFA_LINK;
20741da177e4SLinus Torvalds 	} else {
20751da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
20761da177e4SLinus Torvalds 	}
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
20791da177e4SLinus Torvalds 		ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
20801da177e4SLinus Torvalds 		if (!IS_ERR(ifp)) {
20811da177e4SLinus Torvalds 			spin_lock_bh(&ifp->lock);
20821da177e4SLinus Torvalds 			ifp->flags &= ~IFA_F_TENTATIVE;
20831da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
20841da177e4SLinus Torvalds 			ipv6_ifa_notify(RTM_NEWADDR, ifp);
20851da177e4SLinus Torvalds 			in6_ifa_put(ifp);
20861da177e4SLinus Torvalds 		}
20871da177e4SLinus Torvalds 		return;
20881da177e4SLinus Torvalds 	}
20891da177e4SLinus Torvalds 
2090881d966bSEric W. Biederman 	for_each_netdev(&init_net, dev) {
2091e5ed6399SHerbert Xu 		struct in_device * in_dev = __in_dev_get_rtnl(dev);
20921da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
20931da177e4SLinus Torvalds 			struct in_ifaddr * ifa;
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 			int flag = scope;
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
20981da177e4SLinus Torvalds 				int plen;
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
21031da177e4SLinus Torvalds 					continue;
21041da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
21051da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
21061da177e4SLinus Torvalds 						continue;
21071da177e4SLinus Torvalds 					flag |= IFA_HOST;
21081da177e4SLinus Torvalds 				}
21091da177e4SLinus Torvalds 				if (idev->dev->flags&IFF_POINTOPOINT)
21101da177e4SLinus Torvalds 					plen = 64;
21111da177e4SLinus Torvalds 				else
21121da177e4SLinus Torvalds 					plen = 96;
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds 				ifp = ipv6_add_addr(idev, &addr, plen, flag,
21151da177e4SLinus Torvalds 						    IFA_F_PERMANENT);
21161da177e4SLinus Torvalds 				if (!IS_ERR(ifp)) {
21171da177e4SLinus Torvalds 					spin_lock_bh(&ifp->lock);
21181da177e4SLinus Torvalds 					ifp->flags &= ~IFA_F_TENTATIVE;
21191da177e4SLinus Torvalds 					spin_unlock_bh(&ifp->lock);
21201da177e4SLinus Torvalds 					ipv6_ifa_notify(RTM_NEWADDR, ifp);
21211da177e4SLinus Torvalds 					in6_ifa_put(ifp);
21221da177e4SLinus Torvalds 				}
21231da177e4SLinus Torvalds 			}
21241da177e4SLinus Torvalds 		}
21251da177e4SLinus Torvalds 	}
21261da177e4SLinus Torvalds }
21270be669bbSJoerg Roedel #endif
21281da177e4SLinus Torvalds 
21291da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
21301da177e4SLinus Torvalds {
21311da177e4SLinus Torvalds 	struct inet6_dev  *idev;
21321da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds 	/* ::1 */
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds 	ASSERT_RTNL();
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
21391da177e4SLinus Torvalds 		printk(KERN_DEBUG "init loopback: add_dev failed\n");
21401da177e4SLinus Torvalds 		return;
21411da177e4SLinus Torvalds 	}
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
21441da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
21451da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
21461da177e4SLinus Torvalds 		ifp->flags &= ~IFA_F_TENTATIVE;
21471da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
21481da177e4SLinus Torvalds 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
21491da177e4SLinus Torvalds 		in6_ifa_put(ifp);
21501da177e4SLinus Torvalds 	}
21511da177e4SLinus Torvalds }
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
21541da177e4SLinus Torvalds {
21551da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
215695c385b4SNeil Horman 	u32 addr_flags = IFA_F_PERMANENT;
21571da177e4SLinus Torvalds 
215895c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
215995c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
216095c385b4SNeil Horman 	    !ipv6_devconf.forwarding)
216195c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
216295c385b4SNeil Horman #endif
216395c385b4SNeil Horman 
216495c385b4SNeil Horman 
216595c385b4SNeil Horman 	ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
21661da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
216746d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
21681da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
21691da177e4SLinus Torvalds 		in6_ifa_put(ifp);
21701da177e4SLinus Torvalds 	}
21711da177e4SLinus Torvalds }
21721da177e4SLinus Torvalds 
21731da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
21741da177e4SLinus Torvalds {
21751da177e4SLinus Torvalds 	struct in6_addr addr;
21761da177e4SLinus Torvalds 	struct inet6_dev    * idev;
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds 	ASSERT_RTNL();
21791da177e4SLinus Torvalds 
218074235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
218174235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
218274235a25SHerbert Xu 	    (dev->type != ARPHRD_IEEE802_TR) &&
218374235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
218474235a25SHerbert Xu 	    (dev->type != ARPHRD_INFINIBAND)) {
218574235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
218674235a25SHerbert Xu 		return;
218774235a25SHerbert Xu 	}
218874235a25SHerbert Xu 
21891da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
21901da177e4SLinus Torvalds 	if (idev == NULL)
21911da177e4SLinus Torvalds 		return;
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
21941da177e4SLinus Torvalds 	addr.s6_addr32[0] = htonl(0xFE800000);
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds 	if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
21971da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &addr);
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds 
22000be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
22011da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
22021da177e4SLinus Torvalds {
22031da177e4SLinus Torvalds 	struct inet6_dev *idev;
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds 	ASSERT_RTNL();
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds 	/*
22081da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
22091da177e4SLinus Torvalds 	 * addresses... we should configure all of
22101da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
22111da177e4SLinus Torvalds 	 */
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
22141da177e4SLinus Torvalds 		printk(KERN_DEBUG "init sit: add_dev failed\n");
22151da177e4SLinus Torvalds 		return;
22161da177e4SLinus Torvalds 	}
22171da177e4SLinus Torvalds 
2218c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
2219c7dc89c0SFred L. Templin 		struct in6_addr addr;
2220c7dc89c0SFred L. Templin 
2221c7dc89c0SFred L. Templin 		ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2222c7dc89c0SFred L. Templin 		addrconf_prefix_route(&addr, 64, dev, 0, 0);
2223c7dc89c0SFred L. Templin 		if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2224c7dc89c0SFred L. Templin 			addrconf_add_linklocal(idev, &addr);
2225c7dc89c0SFred L. Templin 		return;
2226c7dc89c0SFred L. Templin 	}
2227c7dc89c0SFred L. Templin 
22281da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds 	if (dev->flags&IFF_POINTOPOINT) {
22311da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
22321da177e4SLinus Torvalds 		addrconf_add_lroute(dev);
22331da177e4SLinus Torvalds 	} else
22341da177e4SLinus Torvalds 		sit_route_add(dev);
22351da177e4SLinus Torvalds }
22360be669bbSJoerg Roedel #endif
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds static inline int
22391da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
22401da177e4SLinus Torvalds {
22411da177e4SLinus Torvalds 	struct in6_addr lladdr;
22421da177e4SLinus Torvalds 
224395c385b4SNeil Horman 	if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
22441da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &lladdr);
22451da177e4SLinus Torvalds 		return 0;
22461da177e4SLinus Torvalds 	}
22471da177e4SLinus Torvalds 	return -1;
22481da177e4SLinus Torvalds }
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
22511da177e4SLinus Torvalds {
22521da177e4SLinus Torvalds 	struct net_device *link_dev;
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds 	/* first try to inherit the link-local address from the link device */
22551da177e4SLinus Torvalds 	if (idev->dev->iflink &&
2256881d966bSEric W. Biederman 	    (link_dev = __dev_get_by_index(&init_net, idev->dev->iflink))) {
22571da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
22581da177e4SLinus Torvalds 			return;
22591da177e4SLinus Torvalds 	}
22601da177e4SLinus Torvalds 	/* then try to inherit it from any device */
2261881d966bSEric W. Biederman 	for_each_netdev(&init_net, link_dev) {
22621da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
22631da177e4SLinus Torvalds 			return;
22641da177e4SLinus Torvalds 	}
22651da177e4SLinus Torvalds 	printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
22661da177e4SLinus Torvalds }
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds /*
22691da177e4SLinus Torvalds  * Autoconfigure tunnel with a link-local address so routing protocols,
22701da177e4SLinus Torvalds  * DHCPv6, MLD etc. can be run over the virtual link
22711da177e4SLinus Torvalds  */
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev)
22741da177e4SLinus Torvalds {
22751da177e4SLinus Torvalds 	struct inet6_dev *idev;
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 	ASSERT_RTNL();
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL) {
22801da177e4SLinus Torvalds 		printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
22811da177e4SLinus Torvalds 		return;
22821da177e4SLinus Torvalds 	}
22831da177e4SLinus Torvalds 	ip6_tnl_add_linklocal(idev);
22841da177e4SLinus Torvalds }
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
22871da177e4SLinus Torvalds 			   void * data)
22881da177e4SLinus Torvalds {
22891da177e4SLinus Torvalds 	struct net_device *dev = (struct net_device *) data;
229074235a25SHerbert Xu 	struct inet6_dev *idev = __in6_dev_get(dev);
2291c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
2292b217d616SHerbert Xu 	int err;
22931da177e4SLinus Torvalds 
2294e9dc8653SEric W. Biederman 	if (dev->nd_net != &init_net)
2295e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
2296e9dc8653SEric W. Biederman 
22971da177e4SLinus Torvalds 	switch(event) {
229845ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
229974235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
230045ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
230145ba9dd2SYOSHIFUJI Hideaki 			if (!idev)
2302b217d616SHerbert Xu 				return notifier_from_errno(-ENOMEM);
230345ba9dd2SYOSHIFUJI Hideaki 		}
230445ba9dd2SYOSHIFUJI Hideaki 		break;
23051da177e4SLinus Torvalds 	case NETDEV_UP:
23063c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
2307c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
2308c2edacf8SJay Vosburgh 			break;
2309c2edacf8SJay Vosburgh 
23103c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
2311f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
23123c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
23133c21edbdSYOSHIFUJI Hideaki 				printk(KERN_INFO
23143c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_UP): %s: "
23153c21edbdSYOSHIFUJI Hideaki 					"link is not ready\n",
23163c21edbdSYOSHIFUJI Hideaki 					dev->name);
23173c21edbdSYOSHIFUJI Hideaki 				break;
23183c21edbdSYOSHIFUJI Hideaki 			}
231999081049SKristian Slavov 
2320d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
2321d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
2322d31c7b8fSEvgeniy Polyakov 
232399081049SKristian Slavov 			if (idev)
232499081049SKristian Slavov 				idev->if_flags |= IF_READY;
23253c21edbdSYOSHIFUJI Hideaki 		} else {
2326f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
23273c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
23283c21edbdSYOSHIFUJI Hideaki 				break;
23293c21edbdSYOSHIFUJI Hideaki 			}
23303c21edbdSYOSHIFUJI Hideaki 
23313c21edbdSYOSHIFUJI Hideaki 			if (idev) {
23323c21edbdSYOSHIFUJI Hideaki 				if (idev->if_flags & IF_READY) {
23333c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
23343c21edbdSYOSHIFUJI Hideaki 					break;
23353c21edbdSYOSHIFUJI Hideaki 				}
23363c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
23373c21edbdSYOSHIFUJI Hideaki 			}
23383c21edbdSYOSHIFUJI Hideaki 
23393c21edbdSYOSHIFUJI Hideaki 			printk(KERN_INFO
23403c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_CHANGE): %s: "
23413c21edbdSYOSHIFUJI Hideaki 					"link becomes ready\n",
23423c21edbdSYOSHIFUJI Hideaki 					dev->name);
23433c21edbdSYOSHIFUJI Hideaki 
2344c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
23453c21edbdSYOSHIFUJI Hideaki 		}
23463c21edbdSYOSHIFUJI Hideaki 
23471da177e4SLinus Torvalds 		switch(dev->type) {
23480be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
23491da177e4SLinus Torvalds 		case ARPHRD_SIT:
23501da177e4SLinus Torvalds 			addrconf_sit_config(dev);
23511da177e4SLinus Torvalds 			break;
23520be669bbSJoerg Roedel #endif
23531da177e4SLinus Torvalds 		case ARPHRD_TUNNEL6:
23541da177e4SLinus Torvalds 			addrconf_ip6_tnl_config(dev);
23551da177e4SLinus Torvalds 			break;
23561da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
23571da177e4SLinus Torvalds 			init_loopback(dev);
23581da177e4SLinus Torvalds 			break;
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds 		default:
23611da177e4SLinus Torvalds 			addrconf_dev_config(dev);
23621da177e4SLinus Torvalds 			break;
23633ff50b79SStephen Hemminger 		}
23641da177e4SLinus Torvalds 		if (idev) {
2365c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
2366c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
2367c5e33bddSYOSHIFUJI Hideaki 
23681da177e4SLinus Torvalds 			/* If the MTU changed during the interface down, when the
23691da177e4SLinus Torvalds 			   interface up, the changed MTU must be reflected in the
23701da177e4SLinus Torvalds 			   idev as well as routers.
23711da177e4SLinus Torvalds 			 */
23721da177e4SLinus Torvalds 			if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
23731da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
23741da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
23751da177e4SLinus Torvalds 			}
23761da177e4SLinus Torvalds 			idev->tstamp = jiffies;
23771da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
23781da177e4SLinus Torvalds 			/* If the changed mtu during down is lower than IPV6_MIN_MTU
23791da177e4SLinus Torvalds 			   stop IPv6 on this interface.
23801da177e4SLinus Torvalds 			 */
23811da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
23821da177e4SLinus Torvalds 				addrconf_ifdown(dev, event != NETDEV_DOWN);
23831da177e4SLinus Torvalds 		}
23841da177e4SLinus Torvalds 		break;
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
23871da177e4SLinus Torvalds 		if (idev && dev->mtu >= IPV6_MIN_MTU) {
23881da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
23891da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
23901da177e4SLinus Torvalds 			break;
23911da177e4SLinus Torvalds 		}
23921da177e4SLinus Torvalds 
2393d31c7b8fSEvgeniy Polyakov 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2394d31c7b8fSEvgeniy Polyakov 			idev = ipv6_add_dev(dev);
2395d31c7b8fSEvgeniy Polyakov 			if (idev)
2396d31c7b8fSEvgeniy Polyakov 				break;
2397d31c7b8fSEvgeniy Polyakov 		}
2398d31c7b8fSEvgeniy Polyakov 
23991da177e4SLinus Torvalds 		/* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds 	case NETDEV_DOWN:
24021da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
24031da177e4SLinus Torvalds 		/*
24041da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
24051da177e4SLinus Torvalds 		 */
24061da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
24071da177e4SLinus Torvalds 		break;
24083c21edbdSYOSHIFUJI Hideaki 
24091da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
24101da177e4SLinus Torvalds 		if (idev) {
24115632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
2412408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
2413f52295a9SPavel Emelyanov 			addrconf_sysctl_register(idev);
2414b217d616SHerbert Xu 			err = snmp6_register_dev(idev);
2415b217d616SHerbert Xu 			if (err)
2416b217d616SHerbert Xu 				return notifier_from_errno(err);
24175632c515SStephen Hemminger 		}
24181da177e4SLinus Torvalds 		break;
24193ff50b79SStephen Hemminger 	}
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 	return NOTIFY_OK;
24221da177e4SLinus Torvalds }
24231da177e4SLinus Torvalds 
24241da177e4SLinus Torvalds /*
24251da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
24261da177e4SLinus Torvalds  */
24271da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
24281da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
24291da177e4SLinus Torvalds 	.priority = 0
24301da177e4SLinus Torvalds };
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
24331da177e4SLinus Torvalds {
24341da177e4SLinus Torvalds 	struct inet6_dev *idev;
24351da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **bifa;
24361da177e4SLinus Torvalds 	int i;
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds 	ASSERT_RTNL();
24391da177e4SLinus Torvalds 
24402774c7abSEric W. Biederman 	if (dev == init_net.loopback_dev && how == 1)
24411da177e4SLinus Torvalds 		how = 0;
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds 	rt6_ifdown(dev);
24441da177e4SLinus Torvalds 	neigh_ifdown(&nd_tbl, dev);
24451da177e4SLinus Torvalds 
24461da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
24471da177e4SLinus Torvalds 	if (idev == NULL)
24481da177e4SLinus Torvalds 		return -ENODEV;
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds 	/* Step 1: remove reference to ipv6 device from parent device.
24511da177e4SLinus Torvalds 		   Do not dev_put!
24521da177e4SLinus Torvalds 	 */
24531da177e4SLinus Torvalds 	if (how == 1) {
24541da177e4SLinus Torvalds 		idev->dead = 1;
24558814c4b5SYOSHIFUJI Hideaki 
24568814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
24578814c4b5SYOSHIFUJI Hideaki 		rcu_assign_pointer(dev->ip6_ptr, NULL);
24581da177e4SLinus Torvalds 
24591da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
24601da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds 	}
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds 	/* Step 2: clear hash table */
24651da177e4SLinus Torvalds 	for (i=0; i<IN6_ADDR_HSIZE; i++) {
24661da177e4SLinus Torvalds 		bifa = &inet6_addr_lst[i];
24671da177e4SLinus Torvalds 
24681da177e4SLinus Torvalds 		write_lock_bh(&addrconf_hash_lock);
24691da177e4SLinus Torvalds 		while ((ifa = *bifa) != NULL) {
24701da177e4SLinus Torvalds 			if (ifa->idev == idev) {
24711da177e4SLinus Torvalds 				*bifa = ifa->lst_next;
24721da177e4SLinus Torvalds 				ifa->lst_next = NULL;
24731da177e4SLinus Torvalds 				addrconf_del_timer(ifa);
24741da177e4SLinus Torvalds 				in6_ifa_put(ifa);
24751da177e4SLinus Torvalds 				continue;
24761da177e4SLinus Torvalds 			}
24771da177e4SLinus Torvalds 			bifa = &ifa->lst_next;
24781da177e4SLinus Torvalds 		}
24791da177e4SLinus Torvalds 		write_unlock_bh(&addrconf_hash_lock);
24801da177e4SLinus Torvalds 	}
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
24831da177e4SLinus Torvalds 
24841da177e4SLinus Torvalds 	/* Step 3: clear flags for stateless addrconf */
24851da177e4SLinus Torvalds 	if (how != 1)
24863c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 	/* Step 4: clear address list */
24891da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
24901da177e4SLinus Torvalds 	if (how == 1 && del_timer(&idev->regen_timer))
24911da177e4SLinus Torvalds 		in6_dev_put(idev);
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds 	/* clear tempaddr list */
24941da177e4SLinus Torvalds 	while ((ifa = idev->tempaddr_list) != NULL) {
24951da177e4SLinus Torvalds 		idev->tempaddr_list = ifa->tmp_next;
24961da177e4SLinus Torvalds 		ifa->tmp_next = NULL;
24971da177e4SLinus Torvalds 		ifa->dead = 1;
24981da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
24991da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds 		if (ifa->ifpub) {
25021da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
25031da177e4SLinus Torvalds 			ifa->ifpub = NULL;
25041da177e4SLinus Torvalds 		}
25051da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
25061da177e4SLinus Torvalds 		in6_ifa_put(ifa);
25071da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
25081da177e4SLinus Torvalds 	}
25091da177e4SLinus Torvalds #endif
25101da177e4SLinus Torvalds 	while ((ifa = idev->addr_list) != NULL) {
25111da177e4SLinus Torvalds 		idev->addr_list = ifa->if_next;
25121da177e4SLinus Torvalds 		ifa->if_next = NULL;
25131da177e4SLinus Torvalds 		ifa->dead = 1;
25141da177e4SLinus Torvalds 		addrconf_del_timer(ifa);
25151da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds 		__ipv6_ifa_notify(RTM_DELADDR, ifa);
2518063ed369SVlad Yasevich 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
25191da177e4SLinus Torvalds 		in6_ifa_put(ifa);
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
25221da177e4SLinus Torvalds 	}
25231da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
25241da177e4SLinus Torvalds 
25251da177e4SLinus Torvalds 	/* Step 5: Discard multicast list */
25261da177e4SLinus Torvalds 
25271da177e4SLinus Torvalds 	if (how == 1)
25281da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
25291da177e4SLinus Torvalds 	else
25301da177e4SLinus Torvalds 		ipv6_mc_down(idev);
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds 	idev->tstamp = jiffies;
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds 	/* Shot the device (if unregistered) */
25351da177e4SLinus Torvalds 
25361da177e4SLinus Torvalds 	if (how == 1) {
2537408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
25381da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
25391da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
25401da177e4SLinus Torvalds 		in6_dev_put(idev);
25411da177e4SLinus Torvalds 	}
25421da177e4SLinus Torvalds 	return 0;
25431da177e4SLinus Torvalds }
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
25461da177e4SLinus Torvalds {
25471da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding)
25501da177e4SLinus Torvalds 		goto out;
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	if (ifp->idev->if_flags & IF_RA_RCVD) {
25531da177e4SLinus Torvalds 		/*
25541da177e4SLinus Torvalds 		 *	Announcement received after solicitation
25551da177e4SLinus Torvalds 		 *	was sent
25561da177e4SLinus Torvalds 		 */
25571da177e4SLinus Torvalds 		goto out;
25581da177e4SLinus Torvalds 	}
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 	spin_lock(&ifp->lock);
25611da177e4SLinus Torvalds 	if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
25621da177e4SLinus Torvalds 		struct in6_addr all_routers;
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
25651da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS,
25661da177e4SLinus Torvalds 				   (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
25671da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_delay :
25681da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_interval);
25691da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
25701da177e4SLinus Torvalds 
25711da177e4SLinus Torvalds 		ipv6_addr_all_routers(&all_routers);
25721da177e4SLinus Torvalds 
25731da177e4SLinus Torvalds 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
25741da177e4SLinus Torvalds 	} else {
25751da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
25761da177e4SLinus Torvalds 		/*
25771da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
25781da177e4SLinus Torvalds 		 * assumption any longer.
25791da177e4SLinus Torvalds 		 */
25801da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: no IPv6 routers present\n",
25811da177e4SLinus Torvalds 		       ifp->idev->dev->name);
25821da177e4SLinus Torvalds 	}
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds out:
25851da177e4SLinus Torvalds 	in6_ifa_put(ifp);
25861da177e4SLinus Torvalds }
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds /*
25891da177e4SLinus Torvalds  *	Duplicate Address Detection
25901da177e4SLinus Torvalds  */
25913c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
25923c21edbdSYOSHIFUJI Hideaki {
25933c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
25943c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
25953c21edbdSYOSHIFUJI Hideaki 
259695c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
259795c385b4SNeil Horman 		rand_num = 0;
259895c385b4SNeil Horman 	else
25993c21edbdSYOSHIFUJI Hideaki 		rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
260095c385b4SNeil Horman 
26013c21edbdSYOSHIFUJI Hideaki 	ifp->probes = idev->cnf.dad_transmits;
26023c21edbdSYOSHIFUJI Hideaki 	addrconf_mod_timer(ifp, AC_DAD, rand_num);
26033c21edbdSYOSHIFUJI Hideaki }
26043c21edbdSYOSHIFUJI Hideaki 
2605e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
26061da177e4SLinus Torvalds {
26071da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
26081da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds 	net_srandom(ifp->addr.s6_addr32[3]);
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
26151da177e4SLinus Torvalds 	if (ifp->dead)
26161da177e4SLinus Torvalds 		goto out;
26171da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
26181da177e4SLinus Torvalds 
26191da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
262055ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
262155ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
262295c385b4SNeil Horman 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
26231da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
26241da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
26271da177e4SLinus Torvalds 		return;
26281da177e4SLinus Torvalds 	}
26291da177e4SLinus Torvalds 
26306732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
26313dd3bf83SYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
26326732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
26333c21edbdSYOSHIFUJI Hideaki 		/*
26343c21edbdSYOSHIFUJI Hideaki 		 * If the defice is not ready:
26353c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
26363c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
26373c21edbdSYOSHIFUJI Hideaki 		 */
26383c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
26393c21edbdSYOSHIFUJI Hideaki 		addrconf_dad_stop(ifp);
26406732badeSYOSHIFUJI Hideaki 		return;
26413c21edbdSYOSHIFUJI Hideaki 	}
264295c385b4SNeil Horman 
264395c385b4SNeil Horman 	/*
264495c385b4SNeil Horman 	 * Optimistic nodes can start receiving
264595c385b4SNeil Horman 	 * Frames right away
264695c385b4SNeil Horman 	 */
264795c385b4SNeil Horman 	if(ifp->flags & IFA_F_OPTIMISTIC)
264895c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
264995c385b4SNeil Horman 
26506732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
26516732badeSYOSHIFUJI Hideaki 	spin_unlock_bh(&ifp->lock);
26521da177e4SLinus Torvalds out:
26531da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
26541da177e4SLinus Torvalds }
26551da177e4SLinus Torvalds 
26561da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data)
26571da177e4SLinus Torvalds {
26581da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
26591da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
26601da177e4SLinus Torvalds 	struct in6_addr unspec;
26611da177e4SLinus Torvalds 	struct in6_addr mcaddr;
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
26641da177e4SLinus Torvalds 	if (idev->dead) {
26651da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
26661da177e4SLinus Torvalds 		goto out;
26671da177e4SLinus Torvalds 	}
26681da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
26691da177e4SLinus Torvalds 	if (ifp->probes == 0) {
26701da177e4SLinus Torvalds 		/*
26711da177e4SLinus Torvalds 		 * DAD was successful
26721da177e4SLinus Torvalds 		 */
26731da177e4SLinus Torvalds 
267495c385b4SNeil Horman 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
26751da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
26761da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
26771da177e4SLinus Torvalds 
26781da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
26791da177e4SLinus Torvalds 
26801da177e4SLinus Torvalds 		goto out;
26811da177e4SLinus Torvalds 	}
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds 	ifp->probes--;
26841da177e4SLinus Torvalds 	addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
26851da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
26861da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
26891da177e4SLinus Torvalds 	memset(&unspec, 0, sizeof(unspec));
26901da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
26911da177e4SLinus Torvalds 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
26921da177e4SLinus Torvalds out:
26931da177e4SLinus Torvalds 	in6_ifa_put(ifp);
26941da177e4SLinus Torvalds }
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
26971da177e4SLinus Torvalds {
26981da177e4SLinus Torvalds 	struct net_device *	dev = ifp->idev->dev;
26991da177e4SLinus Torvalds 
27001da177e4SLinus Torvalds 	/*
27011da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
27021da177e4SLinus Torvalds 	 */
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds 	/* If added prefix is link local and forwarding is off,
27071da177e4SLinus Torvalds 	   start sending router solicitations.
27081da177e4SLinus Torvalds 	 */
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding == 0 &&
27111da177e4SLinus Torvalds 	    ifp->idev->cnf.rtr_solicits > 0 &&
27121da177e4SLinus Torvalds 	    (dev->flags&IFF_LOOPBACK) == 0 &&
27131da177e4SLinus Torvalds 	    (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
27141da177e4SLinus Torvalds 		struct in6_addr all_routers;
27151da177e4SLinus Torvalds 
27161da177e4SLinus Torvalds 		ipv6_addr_all_routers(&all_routers);
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds 		/*
27191da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
27201da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
27211da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
27221da177e4SLinus Torvalds 		 */
27231da177e4SLinus Torvalds 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
27241da177e4SLinus Torvalds 
27251da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
27261da177e4SLinus Torvalds 		ifp->probes = 1;
27271da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
27281da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
27291da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
27301da177e4SLinus Torvalds 	}
27311da177e4SLinus Torvalds }
27321da177e4SLinus Torvalds 
2733c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev) {
2734c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
2735c5e33bddSYOSHIFUJI Hideaki 
2736c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
2737c5e33bddSYOSHIFUJI Hideaki 	for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2738c5e33bddSYOSHIFUJI Hideaki 		spin_lock_bh(&ifp->lock);
2739c5e33bddSYOSHIFUJI Hideaki 		if (!(ifp->flags & IFA_F_TENTATIVE)) {
2740c5e33bddSYOSHIFUJI Hideaki 			spin_unlock_bh(&ifp->lock);
2741c5e33bddSYOSHIFUJI Hideaki 			continue;
2742c5e33bddSYOSHIFUJI Hideaki 		}
2743c5e33bddSYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
2744c5e33bddSYOSHIFUJI Hideaki 		addrconf_dad_kick(ifp);
2745c5e33bddSYOSHIFUJI Hideaki 	}
2746c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
2747c5e33bddSYOSHIFUJI Hideaki }
2748c5e33bddSYOSHIFUJI Hideaki 
27491da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
27501da177e4SLinus Torvalds struct if6_iter_state {
27513c40090aSDaniel Lezcano 	struct seq_net_private p;
27521da177e4SLinus Torvalds 	int bucket;
27531da177e4SLinus Torvalds };
27541da177e4SLinus Torvalds 
27551da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
27561da177e4SLinus Torvalds {
27571da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
27581da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
27593c40090aSDaniel Lezcano 	struct net *net = state->p.net;
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds 	for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
27621da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
27633c40090aSDaniel Lezcano 
27643c40090aSDaniel Lezcano 		while (ifa && ifa->idev->dev->nd_net != net)
27653c40090aSDaniel Lezcano 			ifa = ifa->lst_next;
27661da177e4SLinus Torvalds 		if (ifa)
27671da177e4SLinus Torvalds 			break;
27681da177e4SLinus Torvalds 	}
27691da177e4SLinus Torvalds 	return ifa;
27701da177e4SLinus Torvalds }
27711da177e4SLinus Torvalds 
27721da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
27731da177e4SLinus Torvalds {
27741da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
27753c40090aSDaniel Lezcano 	struct net *net = state->p.net;
27761da177e4SLinus Torvalds 
27771da177e4SLinus Torvalds 	ifa = ifa->lst_next;
27781da177e4SLinus Torvalds try_again:
27793c40090aSDaniel Lezcano 	if (ifa) {
27803c40090aSDaniel Lezcano 		if (ifa->idev->dev->nd_net != net) {
27813c40090aSDaniel Lezcano 			ifa = ifa->lst_next;
27823c40090aSDaniel Lezcano 			goto try_again;
27833c40090aSDaniel Lezcano 		}
27843c40090aSDaniel Lezcano 	}
27853c40090aSDaniel Lezcano 
27861da177e4SLinus Torvalds 	if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
27871da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
27881da177e4SLinus Torvalds 		goto try_again;
27891da177e4SLinus Torvalds 	}
27903c40090aSDaniel Lezcano 
27911da177e4SLinus Torvalds 	return ifa;
27921da177e4SLinus Torvalds }
27931da177e4SLinus Torvalds 
27941da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
27951da177e4SLinus Torvalds {
27961da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = if6_get_first(seq);
27971da177e4SLinus Torvalds 
27981da177e4SLinus Torvalds 	if (ifa)
27991da177e4SLinus Torvalds 		while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
28001da177e4SLinus Torvalds 			--pos;
28011da177e4SLinus Torvalds 	return pos ? NULL : ifa;
28021da177e4SLinus Torvalds }
28031da177e4SLinus Torvalds 
28041da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2805d20b3109SStephen Hemminger 	__acquires(addrconf_hash_lock)
28061da177e4SLinus Torvalds {
28071da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
28081da177e4SLinus Torvalds 	return if6_get_idx(seq, *pos);
28091da177e4SLinus Torvalds }
28101da177e4SLinus Torvalds 
28111da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
28121da177e4SLinus Torvalds {
28131da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
28161da177e4SLinus Torvalds 	++*pos;
28171da177e4SLinus Torvalds 	return ifa;
28181da177e4SLinus Torvalds }
28191da177e4SLinus Torvalds 
28201da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
2821d20b3109SStephen Hemminger 	__releases(addrconf_hash_lock)
28221da177e4SLinus Torvalds {
28231da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
28241da177e4SLinus Torvalds }
28251da177e4SLinus Torvalds 
28261da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
28271da177e4SLinus Torvalds {
28281da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
28291da177e4SLinus Torvalds 	seq_printf(seq,
28309343e79aSYOSHIFUJI Hideaki 		   NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
28311da177e4SLinus Torvalds 		   NIP6(ifp->addr),
28321da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
28331da177e4SLinus Torvalds 		   ifp->prefix_len,
28341da177e4SLinus Torvalds 		   ifp->scope,
28351da177e4SLinus Torvalds 		   ifp->flags,
28361da177e4SLinus Torvalds 		   ifp->idev->dev->name);
28371da177e4SLinus Torvalds 	return 0;
28381da177e4SLinus Torvalds }
28391da177e4SLinus Torvalds 
284056b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
28411da177e4SLinus Torvalds 	.start	= if6_seq_start,
28421da177e4SLinus Torvalds 	.next	= if6_seq_next,
28431da177e4SLinus Torvalds 	.show	= if6_seq_show,
28441da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
28451da177e4SLinus Torvalds };
28461da177e4SLinus Torvalds 
28471da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
28481da177e4SLinus Torvalds {
28493c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
2850cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
28511da177e4SLinus Torvalds }
28521da177e4SLinus Torvalds 
28539a32144eSArjan van de Ven static const struct file_operations if6_fops = {
28541da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
28551da177e4SLinus Torvalds 	.open		= if6_seq_open,
28561da177e4SLinus Torvalds 	.read		= seq_read,
28571da177e4SLinus Torvalds 	.llseek		= seq_lseek,
28583c40090aSDaniel Lezcano 	.release	= seq_release_net,
28591da177e4SLinus Torvalds };
28601da177e4SLinus Torvalds 
28613c40090aSDaniel Lezcano static int if6_proc_net_init(struct net *net)
28621da177e4SLinus Torvalds {
28633c40090aSDaniel Lezcano 	if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
28641da177e4SLinus Torvalds 		return -ENOMEM;
28651da177e4SLinus Torvalds 	return 0;
28661da177e4SLinus Torvalds }
28671da177e4SLinus Torvalds 
28683c40090aSDaniel Lezcano static void if6_proc_net_exit(struct net *net)
28693c40090aSDaniel Lezcano {
28703c40090aSDaniel Lezcano        proc_net_remove(net, "if_inet6");
28713c40090aSDaniel Lezcano }
28723c40090aSDaniel Lezcano 
28733c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
28743c40090aSDaniel Lezcano        .init = if6_proc_net_init,
28753c40090aSDaniel Lezcano        .exit = if6_proc_net_exit,
28763c40090aSDaniel Lezcano };
28773c40090aSDaniel Lezcano 
28783c40090aSDaniel Lezcano int __init if6_proc_init(void)
28793c40090aSDaniel Lezcano {
28803c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
28813c40090aSDaniel Lezcano }
28823c40090aSDaniel Lezcano 
28831da177e4SLinus Torvalds void if6_proc_exit(void)
28841da177e4SLinus Torvalds {
28853c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
28861da177e4SLinus Torvalds }
28871da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
28881da177e4SLinus Torvalds 
288959fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
28903b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
2891389f6612SDaniel Lezcano int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr)
28923b9f9a1cSNoriaki TAKAMIYA {
28933b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
28943b9f9a1cSNoriaki TAKAMIYA 	struct inet6_ifaddr * ifp;
28953b9f9a1cSNoriaki TAKAMIYA 	u8 hash = ipv6_addr_hash(addr);
28963b9f9a1cSNoriaki TAKAMIYA 	read_lock_bh(&addrconf_hash_lock);
28973b9f9a1cSNoriaki TAKAMIYA 	for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2898389f6612SDaniel Lezcano 		if (ifp->idev->dev->nd_net != net)
2899389f6612SDaniel Lezcano 			continue;
29003b9f9a1cSNoriaki TAKAMIYA 		if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
29013b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
29023b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
29033b9f9a1cSNoriaki TAKAMIYA 			break;
29043b9f9a1cSNoriaki TAKAMIYA 		}
29053b9f9a1cSNoriaki TAKAMIYA 	}
29063b9f9a1cSNoriaki TAKAMIYA 	read_unlock_bh(&addrconf_hash_lock);
29073b9f9a1cSNoriaki TAKAMIYA 	return ret;
29083b9f9a1cSNoriaki TAKAMIYA }
29093b9f9a1cSNoriaki TAKAMIYA #endif
29103b9f9a1cSNoriaki TAKAMIYA 
29111da177e4SLinus Torvalds /*
29121da177e4SLinus Torvalds  *	Periodic address status verification
29131da177e4SLinus Torvalds  */
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo)
29161da177e4SLinus Torvalds {
29171da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
29181da177e4SLinus Torvalds 	unsigned long now, next;
29191da177e4SLinus Torvalds 	int i;
29201da177e4SLinus Torvalds 
29211da177e4SLinus Torvalds 	spin_lock_bh(&addrconf_verify_lock);
29221da177e4SLinus Torvalds 	now = jiffies;
29231da177e4SLinus Torvalds 	next = now + ADDR_CHECK_FREQUENCY;
29241da177e4SLinus Torvalds 
29251da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
29261da177e4SLinus Torvalds 
29271da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
29281da177e4SLinus Torvalds 
29291da177e4SLinus Torvalds restart:
29305d5780dfSYan Zheng 		read_lock(&addrconf_hash_lock);
29311da177e4SLinus Torvalds 		for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
29321da177e4SLinus Torvalds 			unsigned long age;
29331da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
29341da177e4SLinus Torvalds 			unsigned long regen_advance;
29351da177e4SLinus Torvalds #endif
29361da177e4SLinus Torvalds 
29371da177e4SLinus Torvalds 			if (ifp->flags & IFA_F_PERMANENT)
29381da177e4SLinus Torvalds 				continue;
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
29411da177e4SLinus Torvalds 			age = (now - ifp->tstamp) / HZ;
29421da177e4SLinus Torvalds 
29431da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
29441da177e4SLinus Torvalds 			regen_advance = ifp->idev->cnf.regen_max_retry *
29451da177e4SLinus Torvalds 					ifp->idev->cnf.dad_transmits *
29461da177e4SLinus Torvalds 					ifp->idev->nd_parms->retrans_time / HZ;
29471da177e4SLinus Torvalds #endif
29481da177e4SLinus Torvalds 
29498f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
29508f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
29511da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
29521da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
29535d5780dfSYan Zheng 				read_unlock(&addrconf_hash_lock);
29541da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
29551da177e4SLinus Torvalds 				goto restart;
29568f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
29578f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
29588f27ebb9SYOSHIFUJI Hideaki 				continue;
29591da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
29601da177e4SLinus Torvalds 				/* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
29611da177e4SLinus Torvalds 				int deprecate = 0;
29621da177e4SLinus Torvalds 
29631da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
29641da177e4SLinus Torvalds 					deprecate = 1;
29651da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
29661da177e4SLinus Torvalds 				}
29671da177e4SLinus Torvalds 
29681da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
29691da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
29701da177e4SLinus Torvalds 
29711da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
29721da177e4SLinus Torvalds 
29731da177e4SLinus Torvalds 				if (deprecate) {
29741da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
29755d5780dfSYan Zheng 					read_unlock(&addrconf_hash_lock);
29761da177e4SLinus Torvalds 
29771da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
29781da177e4SLinus Torvalds 					in6_ifa_put(ifp);
29791da177e4SLinus Torvalds 					goto restart;
29801da177e4SLinus Torvalds 				}
29811da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
29821da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
29831da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
29841da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
29851da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
29861da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
29871da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
29881da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
29891da177e4SLinus Torvalds 						ifp->regen_count++;
29901da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
29911da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
29921da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
29935d5780dfSYan Zheng 						read_unlock(&addrconf_hash_lock);
2994291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
2995291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
2996291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
29971da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
29981da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
29991da177e4SLinus Torvalds 						in6_ifa_put(ifp);
30001da177e4SLinus Torvalds 						goto restart;
30011da177e4SLinus Torvalds 					}
30021da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
30031da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
30041da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
30051da177e4SLinus Torvalds #endif
30061da177e4SLinus Torvalds 			} else {
30071da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
30081da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
30091da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
30101da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
30111da177e4SLinus Torvalds 			}
30121da177e4SLinus Torvalds 		}
30135d5780dfSYan Zheng 		read_unlock(&addrconf_hash_lock);
30141da177e4SLinus Torvalds 	}
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds 	addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
30171da177e4SLinus Torvalds 	add_timer(&addr_chk_timer);
30181da177e4SLinus Torvalds 	spin_unlock_bh(&addrconf_verify_lock);
30191da177e4SLinus Torvalds }
30201da177e4SLinus Torvalds 
3021461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3022461d8837SThomas Graf {
3023461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
3024461d8837SThomas Graf 
3025461d8837SThomas Graf 	if (addr)
3026461d8837SThomas Graf 		pfx = nla_data(addr);
3027461d8837SThomas Graf 
3028461d8837SThomas Graf 	if (local) {
3029461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3030461d8837SThomas Graf 			pfx = NULL;
3031461d8837SThomas Graf 		else
3032461d8837SThomas Graf 			pfx = nla_data(local);
3033461d8837SThomas Graf 	}
3034461d8837SThomas Graf 
3035461d8837SThomas Graf 	return pfx;
3036461d8837SThomas Graf }
3037461d8837SThomas Graf 
3038ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3039461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
3040461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
3041461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
3042461d8837SThomas Graf };
3043461d8837SThomas Graf 
30441da177e4SLinus Torvalds static int
30451da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
30461da177e4SLinus Torvalds {
3047b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
3048b933f716SThomas Graf 	struct ifaddrmsg *ifm;
3049b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
30501da177e4SLinus Torvalds 	struct in6_addr *pfx;
3051b933f716SThomas Graf 	int err;
30521da177e4SLinus Torvalds 
3053b854272bSDenis V. Lunev 	if (net != &init_net)
3054b854272bSDenis V. Lunev 		return -EINVAL;
3055b854272bSDenis V. Lunev 
3056b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3057b933f716SThomas Graf 	if (err < 0)
3058b933f716SThomas Graf 		return err;
3059b933f716SThomas Graf 
3060b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
3061b933f716SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
30621da177e4SLinus Torvalds 	if (pfx == NULL)
30631da177e4SLinus Torvalds 		return -EINVAL;
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds 	return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
30661da177e4SLinus Torvalds }
30671da177e4SLinus Torvalds 
306855ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
306955ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
3070081bba5bSNoriaki TAKAMIYA {
307146d48046SYOSHIFUJI Hideaki 	u32 flags = RTF_EXPIRES;
307246d48046SYOSHIFUJI Hideaki 
3073081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
3074081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
3075081bba5bSNoriaki TAKAMIYA 
307646d48046SYOSHIFUJI Hideaki 	if (valid_lft == INFINITY_LIFE_TIME) {
307755ebaef1SNoriaki TAKAMIYA 		ifa_flags |= IFA_F_PERMANENT;
307846d48046SYOSHIFUJI Hideaki 		flags = 0;
307946d48046SYOSHIFUJI Hideaki 	} else if (valid_lft >= 0x7FFFFFFF/HZ)
3080081bba5bSNoriaki TAKAMIYA 		valid_lft = 0x7FFFFFFF/HZ;
3081081bba5bSNoriaki TAKAMIYA 
3082081bba5bSNoriaki TAKAMIYA 	if (prefered_lft == 0)
308355ebaef1SNoriaki TAKAMIYA 		ifa_flags |= IFA_F_DEPRECATED;
3084081bba5bSNoriaki TAKAMIYA 	else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
3085081bba5bSNoriaki TAKAMIYA 		 (prefered_lft != INFINITY_LIFE_TIME))
3086081bba5bSNoriaki TAKAMIYA 		prefered_lft = 0x7FFFFFFF/HZ;
3087081bba5bSNoriaki TAKAMIYA 
3088081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
30893b9f9a1cSNoriaki TAKAMIYA 	ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3090081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
3091081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
3092081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
3093081bba5bSNoriaki TAKAMIYA 
3094081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
3095081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
3096081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
3097081bba5bSNoriaki TAKAMIYA 
309846d48046SYOSHIFUJI Hideaki 	addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
309946d48046SYOSHIFUJI Hideaki 			      jiffies_to_clock_t(valid_lft * HZ), flags);
3100081bba5bSNoriaki TAKAMIYA 	addrconf_verify(0);
3101081bba5bSNoriaki TAKAMIYA 
3102081bba5bSNoriaki TAKAMIYA 	return 0;
3103081bba5bSNoriaki TAKAMIYA }
3104081bba5bSNoriaki TAKAMIYA 
3105081bba5bSNoriaki TAKAMIYA static int
31061da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
31071da177e4SLinus Torvalds {
3108b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
3109461d8837SThomas Graf 	struct ifaddrmsg *ifm;
3110461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
31111da177e4SLinus Torvalds 	struct in6_addr *pfx;
31127198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
31137198f8ceSThomas Graf 	struct net_device *dev;
311455ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
311555ebaef1SNoriaki TAKAMIYA 	u8 ifa_flags;
3116461d8837SThomas Graf 	int err;
31171da177e4SLinus Torvalds 
3118b854272bSDenis V. Lunev 	if (net != &init_net)
3119b854272bSDenis V. Lunev 		return -EINVAL;
3120b854272bSDenis V. Lunev 
3121461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3122461d8837SThomas Graf 	if (err < 0)
3123461d8837SThomas Graf 		return err;
3124461d8837SThomas Graf 
3125461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
3126461d8837SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
31271da177e4SLinus Torvalds 	if (pfx == NULL)
31281da177e4SLinus Torvalds 		return -EINVAL;
31291da177e4SLinus Torvalds 
3130461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
31310778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
3132461d8837SThomas Graf 
3133461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
31340778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
3135461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
3136461d8837SThomas Graf 	} else {
3137461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
3138461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
31390778769dSNoriaki TAKAMIYA 	}
31400778769dSNoriaki TAKAMIYA 
3141881d966bSEric W. Biederman 	dev =  __dev_get_by_index(&init_net, ifm->ifa_index);
31427198f8ceSThomas Graf 	if (dev == NULL)
31437198f8ceSThomas Graf 		return -ENODEV;
3144081bba5bSNoriaki TAKAMIYA 
314555ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
31463b9f9a1cSNoriaki TAKAMIYA 	ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
314755ebaef1SNoriaki TAKAMIYA 
31481cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
31497198f8ceSThomas Graf 	if (ifa == NULL) {
31507198f8ceSThomas Graf 		/*
31517198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
31527198f8ceSThomas Graf 		 * userspace alreay relies on not having to provide this.
31537198f8ceSThomas Graf 		 */
31540778769dSNoriaki TAKAMIYA 		return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
315555ebaef1SNoriaki TAKAMIYA 				      ifa_flags, preferred_lft, valid_lft);
31561da177e4SLinus Torvalds 	}
31571da177e4SLinus Torvalds 
31587198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
31597198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
31607198f8ceSThomas Graf 		err = -EEXIST;
31617198f8ceSThomas Graf 	else
316255ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
31637198f8ceSThomas Graf 
31647198f8ceSThomas Graf 	in6_ifa_put(ifa);
31657198f8ceSThomas Graf 
31667198f8ceSThomas Graf 	return err;
31677198f8ceSThomas Graf }
31687198f8ceSThomas Graf 
3169101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3170101bb229SThomas Graf 			  u8 scope, int ifindex)
3171101bb229SThomas Graf {
3172101bb229SThomas Graf 	struct ifaddrmsg *ifm;
3173101bb229SThomas Graf 
3174101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
3175101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
3176101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
3177101bb229SThomas Graf 	ifm->ifa_flags = flags;
3178101bb229SThomas Graf 	ifm->ifa_scope = scope;
3179101bb229SThomas Graf 	ifm->ifa_index = ifindex;
3180101bb229SThomas Graf }
3181101bb229SThomas Graf 
318285486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
318385486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
318485486af0SThomas Graf {
318585486af0SThomas Graf 	struct ifa_cacheinfo ci;
318685486af0SThomas Graf 
318785486af0SThomas Graf 	ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
318885486af0SThomas Graf 			+ TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
318985486af0SThomas Graf 	ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
319085486af0SThomas Graf 			+ TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
319185486af0SThomas Graf 	ci.ifa_prefered = preferred;
319285486af0SThomas Graf 	ci.ifa_valid = valid;
319385486af0SThomas Graf 
319485486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
319585486af0SThomas Graf }
319685486af0SThomas Graf 
3197101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
3198101bb229SThomas Graf {
3199101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
3200101bb229SThomas Graf 		return RT_SCOPE_HOST;
3201101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
3202101bb229SThomas Graf 		return RT_SCOPE_LINK;
3203101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
3204101bb229SThomas Graf 		return RT_SCOPE_SITE;
3205101bb229SThomas Graf 	else
3206101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
3207101bb229SThomas Graf }
3208101bb229SThomas Graf 
32090ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
32100ab6803bSThomas Graf {
3211339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3212339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
3213339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct ifa_cacheinfo));
32140ab6803bSThomas Graf }
3215c5396a31SYOSHIFUJI Hideaki 
32161da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3217b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
32181da177e4SLinus Torvalds {
32191da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
322085486af0SThomas Graf 	u32 preferred, valid;
32211da177e4SLinus Torvalds 
32220ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
32230ab6803bSThomas Graf 	if (nlh == NULL)
322426932566SPatrick McHardy 		return -EMSGSIZE;
32250ab6803bSThomas Graf 
3226101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3227101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
3228101bb229SThomas Graf 
32291da177e4SLinus Torvalds 	if (!(ifa->flags&IFA_F_PERMANENT)) {
323085486af0SThomas Graf 		preferred = ifa->prefered_lft;
323185486af0SThomas Graf 		valid = ifa->valid_lft;
323285486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
32331da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
323485486af0SThomas Graf 			preferred -= tval;
323585486af0SThomas Graf 			if (valid != INFINITY_LIFE_TIME)
323685486af0SThomas Graf 				valid -= tval;
32371da177e4SLinus Torvalds 		}
32381da177e4SLinus Torvalds 	} else {
323985486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
324085486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
32411da177e4SLinus Torvalds 	}
324285486af0SThomas Graf 
32430ab6803bSThomas Graf 	if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
324426932566SPatrick McHardy 	    put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
324526932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
324626932566SPatrick McHardy 		return -EMSGSIZE;
324726932566SPatrick McHardy 	}
324885486af0SThomas Graf 
32490ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
32501da177e4SLinus Torvalds }
32511da177e4SLinus Torvalds 
32521da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3253e431b8c0SJamal Hadi Salim 				u32 pid, u32 seq, int event, u16 flags)
32541da177e4SLinus Torvalds {
32551da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3256101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3257101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
32581da177e4SLinus Torvalds 
32591da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3260101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3261101bb229SThomas Graf 
32620ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
32630ab6803bSThomas Graf 	if (nlh == NULL)
326426932566SPatrick McHardy 		return -EMSGSIZE;
32650ab6803bSThomas Graf 
3266101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
32670ab6803bSThomas Graf 	if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
32680ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
326926932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
327026932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
327126932566SPatrick McHardy 		return -EMSGSIZE;
327226932566SPatrick McHardy 	}
327385486af0SThomas Graf 
32740ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
32751da177e4SLinus Torvalds }
32761da177e4SLinus Torvalds 
32771da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3278b6544c0bSJamal Hadi Salim 				u32 pid, u32 seq, int event, unsigned int flags)
32791da177e4SLinus Torvalds {
32801da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3281101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3282101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
32831da177e4SLinus Torvalds 
32841da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3285101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3286101bb229SThomas Graf 
32870ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
32880ab6803bSThomas Graf 	if (nlh == NULL)
328926932566SPatrick McHardy 		return -EMSGSIZE;
32900ab6803bSThomas Graf 
3291101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
32920ab6803bSThomas Graf 	if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
32930ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
329426932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
329526932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
329626932566SPatrick McHardy 		return -EMSGSIZE;
329726932566SPatrick McHardy 	}
329885486af0SThomas Graf 
32990ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
33001da177e4SLinus Torvalds }
33011da177e4SLinus Torvalds 
33021da177e4SLinus Torvalds enum addr_type_t
33031da177e4SLinus Torvalds {
33041da177e4SLinus Torvalds 	UNICAST_ADDR,
33051da177e4SLinus Torvalds 	MULTICAST_ADDR,
33061da177e4SLinus Torvalds 	ANYCAST_ADDR,
33071da177e4SLinus Torvalds };
33081da177e4SLinus Torvalds 
33091da177e4SLinus Torvalds static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
33101da177e4SLinus Torvalds 			   enum addr_type_t type)
33111da177e4SLinus Torvalds {
33121da177e4SLinus Torvalds 	int idx, ip_idx;
33131da177e4SLinus Torvalds 	int s_idx, s_ip_idx;
33141da177e4SLinus Torvalds 	int err = 1;
33151da177e4SLinus Torvalds 	struct net_device *dev;
33161da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
33171da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
33181da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
33191da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
33201da177e4SLinus Torvalds 
33211da177e4SLinus Torvalds 	s_idx = cb->args[0];
33221da177e4SLinus Torvalds 	s_ip_idx = ip_idx = cb->args[1];
33231da177e4SLinus Torvalds 
33247562f876SPavel Emelianov 	idx = 0;
3325881d966bSEric W. Biederman 	for_each_netdev(&init_net, dev) {
33261da177e4SLinus Torvalds 		if (idx < s_idx)
33277562f876SPavel Emelianov 			goto cont;
33281da177e4SLinus Torvalds 		if (idx > s_idx)
33291da177e4SLinus Torvalds 			s_ip_idx = 0;
33301da177e4SLinus Torvalds 		ip_idx = 0;
33311da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
33327562f876SPavel Emelianov 			goto cont;
33331da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
33341da177e4SLinus Torvalds 		switch (type) {
33351da177e4SLinus Torvalds 		case UNICAST_ADDR:
3336ae9cda5dSYOSHIFUJI Hideaki 			/* unicast address incl. temp addr */
33371da177e4SLinus Torvalds 			for (ifa = idev->addr_list; ifa;
33381da177e4SLinus Torvalds 			     ifa = ifa->if_next, ip_idx++) {
33391da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
33401da177e4SLinus Torvalds 					continue;
33415d5619b4SYOSHIFUJI Hideaki 				err = inet6_fill_ifaddr(skb, ifa,
33421da177e4SLinus Torvalds 							NETLINK_CB(cb->skb).pid,
33435d5619b4SYOSHIFUJI Hideaki 							cb->nlh->nlmsg_seq,
33445d5619b4SYOSHIFUJI Hideaki 							RTM_NEWADDR,
33455d5619b4SYOSHIFUJI Hideaki 							NLM_F_MULTI);
33461da177e4SLinus Torvalds 			}
33471da177e4SLinus Torvalds 			break;
33481da177e4SLinus Torvalds 		case MULTICAST_ADDR:
33491da177e4SLinus Torvalds 			/* multicast address */
33501da177e4SLinus Torvalds 			for (ifmca = idev->mc_list; ifmca;
33511da177e4SLinus Torvalds 			     ifmca = ifmca->next, ip_idx++) {
33521da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
33531da177e4SLinus Torvalds 					continue;
33545d5619b4SYOSHIFUJI Hideaki 				err = inet6_fill_ifmcaddr(skb, ifmca,
33551da177e4SLinus Torvalds 							  NETLINK_CB(cb->skb).pid,
33565d5619b4SYOSHIFUJI Hideaki 							  cb->nlh->nlmsg_seq,
33575d5619b4SYOSHIFUJI Hideaki 							  RTM_GETMULTICAST,
33585d5619b4SYOSHIFUJI Hideaki 							  NLM_F_MULTI);
33591da177e4SLinus Torvalds 			}
33601da177e4SLinus Torvalds 			break;
33611da177e4SLinus Torvalds 		case ANYCAST_ADDR:
33621da177e4SLinus Torvalds 			/* anycast address */
33631da177e4SLinus Torvalds 			for (ifaca = idev->ac_list; ifaca;
33641da177e4SLinus Torvalds 			     ifaca = ifaca->aca_next, ip_idx++) {
33651da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
33661da177e4SLinus Torvalds 					continue;
33675d5619b4SYOSHIFUJI Hideaki 				err = inet6_fill_ifacaddr(skb, ifaca,
33681da177e4SLinus Torvalds 							  NETLINK_CB(cb->skb).pid,
33695d5619b4SYOSHIFUJI Hideaki 							  cb->nlh->nlmsg_seq,
33705d5619b4SYOSHIFUJI Hideaki 							  RTM_GETANYCAST,
33715d5619b4SYOSHIFUJI Hideaki 							  NLM_F_MULTI);
33721da177e4SLinus Torvalds 			}
33731da177e4SLinus Torvalds 			break;
33741da177e4SLinus Torvalds 		default:
33751da177e4SLinus Torvalds 			break;
33761da177e4SLinus Torvalds 		}
33771da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
33781da177e4SLinus Torvalds 		in6_dev_put(idev);
33795d5619b4SYOSHIFUJI Hideaki 
33805d5619b4SYOSHIFUJI Hideaki 		if (err <= 0)
33815d5619b4SYOSHIFUJI Hideaki 			break;
33827562f876SPavel Emelianov cont:
33837562f876SPavel Emelianov 		idx++;
33841da177e4SLinus Torvalds 	}
33851da177e4SLinus Torvalds 	cb->args[0] = idx;
33861da177e4SLinus Torvalds 	cb->args[1] = ip_idx;
33871da177e4SLinus Torvalds 	return skb->len;
33881da177e4SLinus Torvalds }
33891da177e4SLinus Torvalds 
33901da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
33911da177e4SLinus Torvalds {
3392b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
33931da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
3394b854272bSDenis V. Lunev 
3395b854272bSDenis V. Lunev 	if (net != &init_net)
3396b854272bSDenis V. Lunev 		return 0;
3397b854272bSDenis V. Lunev 
33981da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
33991da177e4SLinus Torvalds }
34001da177e4SLinus Torvalds 
34011da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
34021da177e4SLinus Torvalds {
3403b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
34041da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
3405b854272bSDenis V. Lunev 
3406b854272bSDenis V. Lunev 	if (net != &init_net)
3407b854272bSDenis V. Lunev 		return 0;
3408b854272bSDenis V. Lunev 
34091da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
34101da177e4SLinus Torvalds }
34111da177e4SLinus Torvalds 
34121da177e4SLinus Torvalds 
34131da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
34141da177e4SLinus Torvalds {
3415b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
34161da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
3417b854272bSDenis V. Lunev 
3418b854272bSDenis V. Lunev 	if (net != &init_net)
3419b854272bSDenis V. Lunev 		return 0;
3420b854272bSDenis V. Lunev 
34211da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
34221da177e4SLinus Torvalds }
34231da177e4SLinus Torvalds 
34241b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
34251b29fc2cSThomas Graf 			     void *arg)
34266c223828SNoriaki TAKAMIYA {
3427b854272bSDenis V. Lunev 	struct net *net = in_skb->sk->sk_net;
34281b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
34291b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
34306c223828SNoriaki TAKAMIYA 	struct in6_addr *addr = NULL;
34316c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
34326c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
34336c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
34346c223828SNoriaki TAKAMIYA 	int err;
34356c223828SNoriaki TAKAMIYA 
3436b854272bSDenis V. Lunev 	if (net != &init_net)
3437b854272bSDenis V. Lunev 		return -EINVAL;
3438b854272bSDenis V. Lunev 
34391b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
34401b29fc2cSThomas Graf 	if (err < 0)
34411b29fc2cSThomas Graf 		goto errout;
34426c223828SNoriaki TAKAMIYA 
34431b29fc2cSThomas Graf 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
34441b29fc2cSThomas Graf 	if (addr == NULL) {
34451b29fc2cSThomas Graf 		err = -EINVAL;
34461b29fc2cSThomas Graf 		goto errout;
34471b29fc2cSThomas Graf 	}
34481b29fc2cSThomas Graf 
34491b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
34506c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
3451881d966bSEric W. Biederman 		dev = __dev_get_by_index(&init_net, ifm->ifa_index);
34526c223828SNoriaki TAKAMIYA 
34531cab3da6SDaniel Lezcano 	if ((ifa = ipv6_get_ifaddr(net, addr, dev, 1)) == NULL) {
34541b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
34551b29fc2cSThomas Graf 		goto errout;
34566c223828SNoriaki TAKAMIYA 	}
34576c223828SNoriaki TAKAMIYA 
34580ab6803bSThomas Graf 	if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
34591b29fc2cSThomas Graf 		err = -ENOBUFS;
34601b29fc2cSThomas Graf 		goto errout_ifa;
34611b29fc2cSThomas Graf 	}
34621b29fc2cSThomas Graf 
34636c223828SNoriaki TAKAMIYA 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
34646c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
346526932566SPatrick McHardy 	if (err < 0) {
346626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
346726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
346826932566SPatrick McHardy 		kfree_skb(skb);
346926932566SPatrick McHardy 		goto errout_ifa;
347026932566SPatrick McHardy 	}
347197c53cacSDenis V. Lunev 	err = rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
34721b29fc2cSThomas Graf errout_ifa:
34736c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
34741b29fc2cSThomas Graf errout:
34756c223828SNoriaki TAKAMIYA 	return err;
34766c223828SNoriaki TAKAMIYA }
34776c223828SNoriaki TAKAMIYA 
34781da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
34791da177e4SLinus Torvalds {
34801da177e4SLinus Torvalds 	struct sk_buff *skb;
34815d620266SThomas Graf 	int err = -ENOBUFS;
34821da177e4SLinus Torvalds 
34830ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
34845d620266SThomas Graf 	if (skb == NULL)
34855d620266SThomas Graf 		goto errout;
34865d620266SThomas Graf 
34875d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
348826932566SPatrick McHardy 	if (err < 0) {
348926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
349026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
349126932566SPatrick McHardy 		kfree_skb(skb);
349226932566SPatrick McHardy 		goto errout;
349326932566SPatrick McHardy 	}
349497c53cacSDenis V. Lunev 	err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
34955d620266SThomas Graf errout:
34965d620266SThomas Graf 	if (err < 0)
349797c53cacSDenis V. Lunev 		rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
34981da177e4SLinus Torvalds }
34991da177e4SLinus Torvalds 
3500b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
35011da177e4SLinus Torvalds 				__s32 *array, int bytes)
35021da177e4SLinus Torvalds {
350304561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
350404561c1fSThomas Graf 
35051da177e4SLinus Torvalds 	memset(array, 0, bytes);
35061da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
35071da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
35081da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
35091da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
35101da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
35111da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
35121da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
35131da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
35141da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
35151da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
35161da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
35171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
35181da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
35191da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
35201da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
35211da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
35221da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
35231da177e4SLinus Torvalds #endif
35241da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
352565f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3526c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3527930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
3528930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
352952e16356SYOSHIFUJI Hideaki 	array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
3530fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
353109c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
353209c884d4SYOSHIFUJI Hideaki #endif
3533930d6ff2SYOSHIFUJI Hideaki #endif
3534fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
35350bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
353695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
353795c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
353895c385b4SNeil Horman #endif
35391da177e4SLinus Torvalds }
35401da177e4SLinus Torvalds 
3541339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
3542339bf98fSThomas Graf {
3543339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3544339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3545339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3546339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
3547339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
3548339bf98fSThomas Graf 	       + nla_total_size( /* IFLA_PROTINFO */
3549339bf98fSThomas Graf 			nla_total_size(4) /* IFLA_INET6_FLAGS */
3550339bf98fSThomas Graf 			+ nla_total_size(sizeof(struct ifla_cacheinfo))
3551339bf98fSThomas Graf 			+ nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3552bf99f1bdSYOSHIFUJI Hideaki 			+ nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3553bf99f1bdSYOSHIFUJI Hideaki 			+ nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
3554339bf98fSThomas Graf 		 );
3555339bf98fSThomas Graf }
3556c5396a31SYOSHIFUJI Hideaki 
35577f7d9a6bSHerbert Xu static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
35587f7d9a6bSHerbert Xu 				      int bytes)
35597f7d9a6bSHerbert Xu {
35607f7d9a6bSHerbert Xu 	int i;
35617f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
35627f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
35637f7d9a6bSHerbert Xu 
35647f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
35657f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
35667f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
35677f7d9a6bSHerbert Xu 		put_unaligned(snmp_fold_field(mib, i), &stats[i]);
35687f7d9a6bSHerbert Xu 
35697f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
35707f7d9a6bSHerbert Xu }
35717f7d9a6bSHerbert Xu 
35727f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
35737f7d9a6bSHerbert Xu 			     int bytes)
35747f7d9a6bSHerbert Xu {
35757f7d9a6bSHerbert Xu 	switch(attrtype) {
35767f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
35777f7d9a6bSHerbert Xu 		__snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
35787f7d9a6bSHerbert Xu 		break;
35797f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
35807f7d9a6bSHerbert Xu 		__snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
35817f7d9a6bSHerbert Xu 		break;
35827f7d9a6bSHerbert Xu 	}
35837f7d9a6bSHerbert Xu }
35847f7d9a6bSHerbert Xu 
35851da177e4SLinus Torvalds static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3586b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
35871da177e4SLinus Torvalds {
35881da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3589bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
359004561c1fSThomas Graf 	struct ifinfomsg *hdr;
35911da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
359204561c1fSThomas Graf 	void *protoinfo;
35931da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
35941da177e4SLinus Torvalds 
359504561c1fSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
359604561c1fSThomas Graf 	if (nlh == NULL)
359726932566SPatrick McHardy 		return -EMSGSIZE;
35981da177e4SLinus Torvalds 
359904561c1fSThomas Graf 	hdr = nlmsg_data(nlh);
360004561c1fSThomas Graf 	hdr->ifi_family = AF_INET6;
360104561c1fSThomas Graf 	hdr->__ifi_pad = 0;
360204561c1fSThomas Graf 	hdr->ifi_type = dev->type;
360304561c1fSThomas Graf 	hdr->ifi_index = dev->ifindex;
360404561c1fSThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
360504561c1fSThomas Graf 	hdr->ifi_change = 0;
360604561c1fSThomas Graf 
360704561c1fSThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
36081da177e4SLinus Torvalds 
36091da177e4SLinus Torvalds 	if (dev->addr_len)
361004561c1fSThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
36111da177e4SLinus Torvalds 
361204561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
36131da177e4SLinus Torvalds 	if (dev->ifindex != dev->iflink)
361404561c1fSThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
36151da177e4SLinus Torvalds 
361604561c1fSThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
361704561c1fSThomas Graf 	if (protoinfo == NULL)
361804561c1fSThomas Graf 		goto nla_put_failure;
36191da177e4SLinus Torvalds 
362004561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
36211da177e4SLinus Torvalds 
36221da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
36231da177e4SLinus Torvalds 	ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
36241da177e4SLinus Torvalds 		    + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
36251da177e4SLinus Torvalds 	ci.reachable_time = idev->nd_parms->reachable_time;
36261da177e4SLinus Torvalds 	ci.retrans_time = idev->nd_parms->retrans_time;
362704561c1fSThomas Graf 	NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
36281da177e4SLinus Torvalds 
3629bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3630bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
363104561c1fSThomas Graf 		goto nla_put_failure;
3632bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
36331da177e4SLinus Torvalds 
3634bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
3635bf99f1bdSYOSHIFUJI Hideaki 
3636bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3637bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
3638bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
3639bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3640bf99f1bdSYOSHIFUJI Hideaki 
3641bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3642bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
3643bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
3644bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
36451da177e4SLinus Torvalds 
364604561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
364704561c1fSThomas Graf 	return nlmsg_end(skb, nlh);
36481da177e4SLinus Torvalds 
364904561c1fSThomas Graf nla_put_failure:
365026932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
365126932566SPatrick McHardy 	return -EMSGSIZE;
36521da177e4SLinus Torvalds }
36531da177e4SLinus Torvalds 
36541da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
36551da177e4SLinus Torvalds {
3656b854272bSDenis V. Lunev 	struct net *net = skb->sk->sk_net;
36571da177e4SLinus Torvalds 	int idx, err;
36581da177e4SLinus Torvalds 	int s_idx = cb->args[0];
36591da177e4SLinus Torvalds 	struct net_device *dev;
36601da177e4SLinus Torvalds 	struct inet6_dev *idev;
36611da177e4SLinus Torvalds 
3662b854272bSDenis V. Lunev 	if (net != &init_net)
3663b854272bSDenis V. Lunev 		return 0;
3664b854272bSDenis V. Lunev 
36651da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
36667562f876SPavel Emelianov 	idx = 0;
3667881d966bSEric W. Biederman 	for_each_netdev(&init_net, dev) {
36681da177e4SLinus Torvalds 		if (idx < s_idx)
36697562f876SPavel Emelianov 			goto cont;
36701da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
36717562f876SPavel Emelianov 			goto cont;
36721da177e4SLinus Torvalds 		err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3673b6544c0bSJamal Hadi Salim 				cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
36741da177e4SLinus Torvalds 		in6_dev_put(idev);
36751da177e4SLinus Torvalds 		if (err <= 0)
36761da177e4SLinus Torvalds 			break;
36777562f876SPavel Emelianov cont:
36787562f876SPavel Emelianov 		idx++;
36791da177e4SLinus Torvalds 	}
36801da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
36811da177e4SLinus Torvalds 	cb->args[0] = idx;
36821da177e4SLinus Torvalds 
36831da177e4SLinus Torvalds 	return skb->len;
36841da177e4SLinus Torvalds }
36851da177e4SLinus Torvalds 
36861da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
36871da177e4SLinus Torvalds {
36881da177e4SLinus Torvalds 	struct sk_buff *skb;
36898d7a76c9SThomas Graf 	int err = -ENOBUFS;
36901da177e4SLinus Torvalds 
3691339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
36928d7a76c9SThomas Graf 	if (skb == NULL)
36938d7a76c9SThomas Graf 		goto errout;
36948d7a76c9SThomas Graf 
36958d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
369626932566SPatrick McHardy 	if (err < 0) {
369726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
369826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
369926932566SPatrick McHardy 		kfree_skb(skb);
370026932566SPatrick McHardy 		goto errout;
370126932566SPatrick McHardy 	}
370297c53cacSDenis V. Lunev 	err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
37038d7a76c9SThomas Graf errout:
37048d7a76c9SThomas Graf 	if (err < 0)
370597c53cacSDenis V. Lunev 		rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
37061da177e4SLinus Torvalds }
37071da177e4SLinus Torvalds 
3708339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
3709339bf98fSThomas Graf {
3710339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
3711339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
3712339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
3713339bf98fSThomas Graf }
3714c5396a31SYOSHIFUJI Hideaki 
37151da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3716b6544c0bSJamal Hadi Salim 			     struct prefix_info *pinfo, u32 pid, u32 seq,
3717b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
37181da177e4SLinus Torvalds {
37191da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
37201da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
37211da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
37221da177e4SLinus Torvalds 
37236051e2f4SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
37246051e2f4SThomas Graf 	if (nlh == NULL)
372526932566SPatrick McHardy 		return -EMSGSIZE;
37266051e2f4SThomas Graf 
37276051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
37281da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
37298a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
37308a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
37311da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
37321da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
37331da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
37348a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
37351da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
37361da177e4SLinus Torvalds 	if (pinfo->onlink)
37371da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
37381da177e4SLinus Torvalds 	if (pinfo->autoconf)
37391da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
37401da177e4SLinus Torvalds 
37416051e2f4SThomas Graf 	NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
37421da177e4SLinus Torvalds 
37431da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
37441da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
37456051e2f4SThomas Graf 	NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
37461da177e4SLinus Torvalds 
37476051e2f4SThomas Graf 	return nlmsg_end(skb, nlh);
37481da177e4SLinus Torvalds 
37496051e2f4SThomas Graf nla_put_failure:
375026932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
375126932566SPatrick McHardy 	return -EMSGSIZE;
37521da177e4SLinus Torvalds }
37531da177e4SLinus Torvalds 
37541da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
37551da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
37561da177e4SLinus Torvalds {
37571da177e4SLinus Torvalds 	struct sk_buff *skb;
37588c384bfaSThomas Graf 	int err = -ENOBUFS;
37591da177e4SLinus Torvalds 
3760339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
37618c384bfaSThomas Graf 	if (skb == NULL)
37628c384bfaSThomas Graf 		goto errout;
37638c384bfaSThomas Graf 
37648c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
376526932566SPatrick McHardy 	if (err < 0) {
376626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
376726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
376826932566SPatrick McHardy 		kfree_skb(skb);
376926932566SPatrick McHardy 		goto errout;
377026932566SPatrick McHardy 	}
377197c53cacSDenis V. Lunev 	err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
37728c384bfaSThomas Graf errout:
37738c384bfaSThomas Graf 	if (err < 0)
377497c53cacSDenis V. Lunev 		rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_PREFIX, err);
37751da177e4SLinus Torvalds }
37761da177e4SLinus Torvalds 
37771da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
37781da177e4SLinus Torvalds {
37791da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
37801da177e4SLinus Torvalds 
37811da177e4SLinus Torvalds 	switch (event) {
37821da177e4SLinus Torvalds 	case RTM_NEWADDR:
378395c385b4SNeil Horman 		/*
378495c385b4SNeil Horman 		 * If the address was optimistic
378595c385b4SNeil Horman 		 * we inserted the route at the start of
378695c385b4SNeil Horman 		 * our DAD process, so we don't need
378795c385b4SNeil Horman 		 * to do it again
378895c385b4SNeil Horman 		 */
378995c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
379040e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
37911da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
37921da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
37931da177e4SLinus Torvalds 		break;
37941da177e4SLinus Torvalds 	case RTM_DELADDR:
37951da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
37961da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
37971da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
37981da177e4SLinus Torvalds 		dst_hold(&ifp->rt->u.dst);
3799e0a1ad73SThomas Graf 		if (ip6_del_rt(ifp->rt))
38001da177e4SLinus Torvalds 			dst_free(&ifp->rt->u.dst);
38011da177e4SLinus Torvalds 		break;
38021da177e4SLinus Torvalds 	}
38031da177e4SLinus Torvalds }
38041da177e4SLinus Torvalds 
38051da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
38061da177e4SLinus Torvalds {
38078814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
38081da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
38091da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
38108814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
38111da177e4SLinus Torvalds }
38121da177e4SLinus Torvalds 
38131da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
38141da177e4SLinus Torvalds 
38151da177e4SLinus Torvalds static
38161da177e4SLinus Torvalds int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
38171da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
38181da177e4SLinus Torvalds {
38191da177e4SLinus Torvalds 	int *valp = ctl->data;
38201da177e4SLinus Torvalds 	int val = *valp;
38211da177e4SLinus Torvalds 	int ret;
38221da177e4SLinus Torvalds 
38231da177e4SLinus Torvalds 	ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
38241da177e4SLinus Torvalds 
3825c8fecf22SPavel Emelyanov 	if (write)
3826c8fecf22SPavel Emelyanov 		addrconf_fixup_forwarding(ctl, valp, val);
38271da177e4SLinus Torvalds 	return ret;
38281da177e4SLinus Torvalds }
38291da177e4SLinus Torvalds 
38301da177e4SLinus Torvalds static int addrconf_sysctl_forward_strategy(ctl_table *table,
38311da177e4SLinus Torvalds 					    int __user *name, int nlen,
38321da177e4SLinus Torvalds 					    void __user *oldval,
38331da177e4SLinus Torvalds 					    size_t __user *oldlenp,
38341f29bcd7SAlexey Dobriyan 					    void __user *newval, size_t newlen)
38351da177e4SLinus Torvalds {
38361da177e4SLinus Torvalds 	int *valp = table->data;
3837c8fecf22SPavel Emelyanov 	int val = *valp;
38381da177e4SLinus Torvalds 	int new;
38391da177e4SLinus Torvalds 
38401da177e4SLinus Torvalds 	if (!newval || !newlen)
38411da177e4SLinus Torvalds 		return 0;
38421da177e4SLinus Torvalds 	if (newlen != sizeof(int))
38431da177e4SLinus Torvalds 		return -EINVAL;
38441da177e4SLinus Torvalds 	if (get_user(new, (int __user *)newval))
38451da177e4SLinus Torvalds 		return -EFAULT;
38461da177e4SLinus Torvalds 	if (new == *valp)
38471da177e4SLinus Torvalds 		return 0;
38481da177e4SLinus Torvalds 	if (oldval && oldlenp) {
38491da177e4SLinus Torvalds 		size_t len;
38501da177e4SLinus Torvalds 		if (get_user(len, oldlenp))
38511da177e4SLinus Torvalds 			return -EFAULT;
38521da177e4SLinus Torvalds 		if (len) {
38531da177e4SLinus Torvalds 			if (len > table->maxlen)
38541da177e4SLinus Torvalds 				len = table->maxlen;
38551da177e4SLinus Torvalds 			if (copy_to_user(oldval, valp, len))
38561da177e4SLinus Torvalds 				return -EFAULT;
38571da177e4SLinus Torvalds 			if (put_user(len, oldlenp))
38581da177e4SLinus Torvalds 				return -EFAULT;
38591da177e4SLinus Torvalds 		}
38601da177e4SLinus Torvalds 	}
38611da177e4SLinus Torvalds 
38621da177e4SLinus Torvalds 	*valp = new;
3863c8fecf22SPavel Emelyanov 	addrconf_fixup_forwarding(table, valp, val);
38641da177e4SLinus Torvalds 	return 1;
38651da177e4SLinus Torvalds }
38661da177e4SLinus Torvalds 
38671da177e4SLinus Torvalds static struct addrconf_sysctl_table
38681da177e4SLinus Torvalds {
38691da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
38701da177e4SLinus Torvalds 	ctl_table addrconf_vars[__NET_IPV6_MAX];
38711dab6222SPavel Emelyanov 	char *dev_name;
3872ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
38731da177e4SLinus Torvalds 	.sysctl_header = NULL,
38741da177e4SLinus Torvalds 	.addrconf_vars = {
38751da177e4SLinus Torvalds 		{
38761da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORWARDING,
38771da177e4SLinus Torvalds 			.procname	=	"forwarding",
38781da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.forwarding,
38791da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
38801da177e4SLinus Torvalds 			.mode		=	0644,
38811da177e4SLinus Torvalds 			.proc_handler	=	&addrconf_sysctl_forward,
38821da177e4SLinus Torvalds 			.strategy	=	&addrconf_sysctl_forward_strategy,
38831da177e4SLinus Torvalds 		},
38841da177e4SLinus Torvalds 		{
38851da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_HOP_LIMIT,
38861da177e4SLinus Torvalds 			.procname	=	"hop_limit",
38871da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.hop_limit,
38881da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
38891da177e4SLinus Torvalds 			.mode		=	0644,
38901da177e4SLinus Torvalds 			.proc_handler	=	proc_dointvec,
38911da177e4SLinus Torvalds 		},
38921da177e4SLinus Torvalds 		{
38931da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MTU,
38941da177e4SLinus Torvalds 			.procname	=	"mtu",
38951da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.mtu6,
38961da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
38971da177e4SLinus Torvalds 			.mode		=	0644,
38981da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
38991da177e4SLinus Torvalds 		},
39001da177e4SLinus Torvalds 		{
39011da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_RA,
39021da177e4SLinus Torvalds 			.procname	=	"accept_ra",
39031da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.accept_ra,
39041da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39051da177e4SLinus Torvalds 			.mode		=	0644,
39061da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39071da177e4SLinus Torvalds 		},
39081da177e4SLinus Torvalds 		{
39091da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_REDIRECTS,
39101da177e4SLinus Torvalds 			.procname	=	"accept_redirects",
39111da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.accept_redirects,
39121da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39131da177e4SLinus Torvalds 			.mode		=	0644,
39141da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39151da177e4SLinus Torvalds 		},
39161da177e4SLinus Torvalds 		{
39171da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_AUTOCONF,
39181da177e4SLinus Torvalds 			.procname	=	"autoconf",
39191da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.autoconf,
39201da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39211da177e4SLinus Torvalds 			.mode		=	0644,
39221da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39231da177e4SLinus Torvalds 		},
39241da177e4SLinus Torvalds 		{
39251da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_DAD_TRANSMITS,
39261da177e4SLinus Torvalds 			.procname	=	"dad_transmits",
39271da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.dad_transmits,
39281da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39291da177e4SLinus Torvalds 			.mode		=	0644,
39301da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39311da177e4SLinus Torvalds 		},
39321da177e4SLinus Torvalds 		{
39331da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICITS,
39341da177e4SLinus Torvalds 			.procname	=	"router_solicitations",
39351da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicits,
39361da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39371da177e4SLinus Torvalds 			.mode		=	0644,
39381da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39391da177e4SLinus Torvalds 		},
39401da177e4SLinus Torvalds 		{
39411da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_INTERVAL,
39421da177e4SLinus Torvalds 			.procname	=	"router_solicitation_interval",
39431da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicit_interval,
39441da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39451da177e4SLinus Torvalds 			.mode		=	0644,
39461da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec_jiffies,
39471da177e4SLinus Torvalds 			.strategy	=	&sysctl_jiffies,
39481da177e4SLinus Torvalds 		},
39491da177e4SLinus Torvalds 		{
39501da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_DELAY,
39511da177e4SLinus Torvalds 			.procname	=	"router_solicitation_delay",
39521da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicit_delay,
39531da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39541da177e4SLinus Torvalds 			.mode		=	0644,
39551da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec_jiffies,
39561da177e4SLinus Torvalds 			.strategy	=	&sysctl_jiffies,
39571da177e4SLinus Torvalds 		},
39581da177e4SLinus Torvalds 		{
39591da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORCE_MLD_VERSION,
39601da177e4SLinus Torvalds 			.procname	=	"force_mld_version",
39611da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.force_mld_version,
39621da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39631da177e4SLinus Torvalds 			.mode		=	0644,
39641da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39651da177e4SLinus Torvalds 		},
39661da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
39671da177e4SLinus Torvalds 		{
39681da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_USE_TEMPADDR,
39691da177e4SLinus Torvalds 			.procname	=	"use_tempaddr",
39701da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.use_tempaddr,
39711da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39721da177e4SLinus Torvalds 			.mode		=	0644,
39731da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39741da177e4SLinus Torvalds 		},
39751da177e4SLinus Torvalds 		{
39761da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_VALID_LFT,
39771da177e4SLinus Torvalds 			.procname	=	"temp_valid_lft",
39781da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.temp_valid_lft,
39791da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39801da177e4SLinus Torvalds 			.mode		=	0644,
39811da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39821da177e4SLinus Torvalds 		},
39831da177e4SLinus Torvalds 		{
39841da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_PREFERED_LFT,
39851da177e4SLinus Torvalds 			.procname	=	"temp_prefered_lft",
39861da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.temp_prefered_lft,
39871da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39881da177e4SLinus Torvalds 			.mode		=	0644,
39891da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39901da177e4SLinus Torvalds 		},
39911da177e4SLinus Torvalds 		{
39921da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_REGEN_MAX_RETRY,
39931da177e4SLinus Torvalds 			.procname	=	"regen_max_retry",
39941da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.regen_max_retry,
39951da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
39961da177e4SLinus Torvalds 			.mode		=	0644,
39971da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
39981da177e4SLinus Torvalds 		},
39991da177e4SLinus Torvalds 		{
40001da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_DESYNC_FACTOR,
40011da177e4SLinus Torvalds 			.procname	=	"max_desync_factor",
40021da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_desync_factor,
40031da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40041da177e4SLinus Torvalds 			.mode		=	0644,
40051da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40061da177e4SLinus Torvalds 		},
40071da177e4SLinus Torvalds #endif
40081da177e4SLinus Torvalds 		{
40091da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_ADDRESSES,
40101da177e4SLinus Torvalds 			.procname	=	"max_addresses",
40111da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_addresses,
40121da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40131da177e4SLinus Torvalds 			.mode		=	0644,
40141da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40151da177e4SLinus Torvalds 		},
40161da177e4SLinus Torvalds 		{
401765f5c7c1SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_DEFRTR,
401865f5c7c1SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_defrtr",
401965f5c7c1SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_defrtr,
402065f5c7c1SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
402165f5c7c1SYOSHIFUJI Hideaki 			.mode		=	0644,
402265f5c7c1SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
402365f5c7c1SYOSHIFUJI Hideaki 		},
402465f5c7c1SYOSHIFUJI Hideaki 		{
4025c4fd30ebSYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_PINFO,
4026c4fd30ebSYOSHIFUJI Hideaki 			.procname	=	"accept_ra_pinfo",
4027c4fd30ebSYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_pinfo,
4028c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4029c4fd30ebSYOSHIFUJI Hideaki 			.mode		=	0644,
4030c4fd30ebSYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4031c4fd30ebSYOSHIFUJI Hideaki 		},
4032930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4033930d6ff2SYOSHIFUJI Hideaki 		{
4034930d6ff2SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RTR_PREF,
4035930d6ff2SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_rtr_pref",
4036930d6ff2SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_rtr_pref,
4037930d6ff2SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4038930d6ff2SYOSHIFUJI Hideaki 			.mode		=	0644,
4039930d6ff2SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4040930d6ff2SYOSHIFUJI Hideaki 		},
404152e16356SYOSHIFUJI Hideaki 		{
404252e16356SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_RTR_PROBE_INTERVAL,
404352e16356SYOSHIFUJI Hideaki 			.procname	=	"router_probe_interval",
404452e16356SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.rtr_probe_interval,
404552e16356SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
404652e16356SYOSHIFUJI Hideaki 			.mode		=	0644,
404752e16356SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec_jiffies,
404852e16356SYOSHIFUJI Hideaki 			.strategy	=	&sysctl_jiffies,
404952e16356SYOSHIFUJI Hideaki 		},
4050fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
405109c884d4SYOSHIFUJI Hideaki 		{
405209c884d4SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
405309c884d4SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_rt_info_max_plen",
405409c884d4SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_rt_info_max_plen,
405509c884d4SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
405609c884d4SYOSHIFUJI Hideaki 			.mode		=	0644,
405709c884d4SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
405809c884d4SYOSHIFUJI Hideaki 		},
405909c884d4SYOSHIFUJI Hideaki #endif
4060930d6ff2SYOSHIFUJI Hideaki #endif
4061c4fd30ebSYOSHIFUJI Hideaki 		{
4062fbea49e1SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_PROXY_NDP,
4063fbea49e1SYOSHIFUJI Hideaki 			.procname	=	"proxy_ndp",
4064fbea49e1SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.proxy_ndp,
4065fbea49e1SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4066fbea49e1SYOSHIFUJI Hideaki 			.mode		=	0644,
4067fbea49e1SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4068fbea49e1SYOSHIFUJI Hideaki 		},
4069fbea49e1SYOSHIFUJI Hideaki 		{
40700bcbc926SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_SOURCE_ROUTE,
40710bcbc926SYOSHIFUJI Hideaki 			.procname	=	"accept_source_route",
40720bcbc926SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_source_route,
40730bcbc926SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
40740bcbc926SYOSHIFUJI Hideaki 			.mode		=	0644,
40750bcbc926SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
40760bcbc926SYOSHIFUJI Hideaki 		},
407795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
407895c385b4SNeil Horman 		{
407995c385b4SNeil Horman 			.ctl_name	=	CTL_UNNUMBERED,
408095c385b4SNeil Horman 			.procname       =       "optimistic_dad",
408195c385b4SNeil Horman 			.data           =       &ipv6_devconf.optimistic_dad,
408295c385b4SNeil Horman 			.maxlen         =       sizeof(int),
408395c385b4SNeil Horman 			.mode           =       0644,
408495c385b4SNeil Horman 			.proc_handler   =       &proc_dointvec,
408595c385b4SNeil Horman 
408695c385b4SNeil Horman 		},
408795c385b4SNeil Horman #endif
40880bcbc926SYOSHIFUJI Hideaki 		{
40891da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
40901da177e4SLinus Torvalds 		}
40911da177e4SLinus Torvalds 	},
40921da177e4SLinus Torvalds };
40931da177e4SLinus Torvalds 
4094bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4095bff16c2fSPavel Emelyanov 		int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p)
40961da177e4SLinus Torvalds {
40971da177e4SLinus Torvalds 	int i;
40981da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
40991da177e4SLinus Torvalds 
41001dab6222SPavel Emelyanov #define ADDRCONF_CTL_PATH_DEV	3
41011dab6222SPavel Emelyanov 
41021dab6222SPavel Emelyanov 	struct ctl_path addrconf_ctl_path[] = {
41031dab6222SPavel Emelyanov 		{ .procname = "net", .ctl_name = CTL_NET, },
41041dab6222SPavel Emelyanov 		{ .procname = "ipv6", .ctl_name = NET_IPV6, },
41051dab6222SPavel Emelyanov 		{ .procname = "conf", .ctl_name = NET_IPV6_CONF, },
41061dab6222SPavel Emelyanov 		{ /* to be set */ },
41071dab6222SPavel Emelyanov 		{ },
41081dab6222SPavel Emelyanov 	};
41091dab6222SPavel Emelyanov 
41101dab6222SPavel Emelyanov 
4111af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
41121da177e4SLinus Torvalds 	if (t == NULL)
4113f68635e6SPavel Emelyanov 		goto out;
4114f68635e6SPavel Emelyanov 
41151da177e4SLinus Torvalds 	for (i=0; t->addrconf_vars[i].data; i++) {
41161da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
41171da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4118bff16c2fSPavel Emelyanov 		t->addrconf_vars[i].extra2 = net;
41191da177e4SLinus Torvalds 	}
41201da177e4SLinus Torvalds 
41211da177e4SLinus Torvalds 	/*
41221da177e4SLinus Torvalds 	 * Make a copy of dev_name, because '.procname' is regarded as const
41231da177e4SLinus Torvalds 	 * by sysctl and we wouldn't want anyone to change it under our feet
41241da177e4SLinus Torvalds 	 * (see SIOCSIFNAME).
41251da177e4SLinus Torvalds 	 */
41261dab6222SPavel Emelyanov 	t->dev_name = kstrdup(dev_name, GFP_KERNEL);
41271dab6222SPavel Emelyanov 	if (!t->dev_name)
41281da177e4SLinus Torvalds 		goto free;
41291da177e4SLinus Torvalds 
41301dab6222SPavel Emelyanov 	addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
41311dab6222SPavel Emelyanov 	addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
41321da177e4SLinus Torvalds 
4133bff16c2fSPavel Emelyanov 	t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
41341dab6222SPavel Emelyanov 			t->addrconf_vars);
41351da177e4SLinus Torvalds 	if (t->sysctl_header == NULL)
41361da177e4SLinus Torvalds 		goto free_procname;
4137f68635e6SPavel Emelyanov 
41381da177e4SLinus Torvalds 	p->sysctl = t;
413995897312SPavel Emelyanov 	return 0;
41401da177e4SLinus Torvalds 
41411da177e4SLinus Torvalds free_procname:
41421dab6222SPavel Emelyanov 	kfree(t->dev_name);
41431da177e4SLinus Torvalds free:
41441da177e4SLinus Torvalds 	kfree(t);
4145f68635e6SPavel Emelyanov out:
414695897312SPavel Emelyanov 	return -ENOBUFS;
41471da177e4SLinus Torvalds }
41481da177e4SLinus Torvalds 
4149408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4150f52295a9SPavel Emelyanov {
4151408c4768SPavel Emelyanov 	struct addrconf_sysctl_table *t;
4152f52295a9SPavel Emelyanov 
4153408c4768SPavel Emelyanov 	if (p->sysctl == NULL)
4154408c4768SPavel Emelyanov 		return;
4155408c4768SPavel Emelyanov 
4156408c4768SPavel Emelyanov 	t = p->sysctl;
41571da177e4SLinus Torvalds 	p->sysctl = NULL;
41581da177e4SLinus Torvalds 	unregister_sysctl_table(t->sysctl_header);
41591dab6222SPavel Emelyanov 	kfree(t->dev_name);
41601da177e4SLinus Torvalds 	kfree(t);
41611da177e4SLinus Torvalds }
4162408c4768SPavel Emelyanov 
4163408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev)
4164408c4768SPavel Emelyanov {
4165408c4768SPavel Emelyanov 	neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6,
4166408c4768SPavel Emelyanov 			      NET_IPV6_NEIGH, "ipv6",
4167408c4768SPavel Emelyanov 			      &ndisc_ifinfo_sysctl_change,
4168408c4768SPavel Emelyanov 			      NULL);
4169bff16c2fSPavel Emelyanov 	__addrconf_sysctl_register(idev->dev->nd_net, idev->dev->name,
4170bff16c2fSPavel Emelyanov 			idev->dev->ifindex, idev, &idev->cnf);
4171408c4768SPavel Emelyanov }
4172408c4768SPavel Emelyanov 
4173408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
4174408c4768SPavel Emelyanov {
4175408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
4176408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
41771da177e4SLinus Torvalds }
41781da177e4SLinus Torvalds 
41791da177e4SLinus Torvalds 
41801da177e4SLinus Torvalds #endif
41811da177e4SLinus Torvalds 
4182e0da5a48SPavel Emelyanov static int addrconf_init_net(struct net *net)
4183e0da5a48SPavel Emelyanov {
4184e0da5a48SPavel Emelyanov 	int err;
4185e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
4186e0da5a48SPavel Emelyanov 
4187e0da5a48SPavel Emelyanov 	err = -ENOMEM;
4188e0da5a48SPavel Emelyanov 	all = &ipv6_devconf;
4189e0da5a48SPavel Emelyanov 	dflt = &ipv6_devconf_dflt;
4190e0da5a48SPavel Emelyanov 
4191e0da5a48SPavel Emelyanov 	if (net != &init_net) {
4192e0da5a48SPavel Emelyanov 		all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
4193e0da5a48SPavel Emelyanov 		if (all == NULL)
4194e0da5a48SPavel Emelyanov 			goto err_alloc_all;
4195e0da5a48SPavel Emelyanov 
4196e0da5a48SPavel Emelyanov 		dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4197e0da5a48SPavel Emelyanov 		if (dflt == NULL)
4198e0da5a48SPavel Emelyanov 			goto err_alloc_dflt;
4199e0da5a48SPavel Emelyanov 	}
4200e0da5a48SPavel Emelyanov 
4201e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
4202e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
4203e0da5a48SPavel Emelyanov 
4204e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4205e0da5a48SPavel Emelyanov 	err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL,
4206e0da5a48SPavel Emelyanov 			NULL, all);
4207e0da5a48SPavel Emelyanov 	if (err < 0)
4208e0da5a48SPavel Emelyanov 		goto err_reg_all;
4209e0da5a48SPavel Emelyanov 
4210e0da5a48SPavel Emelyanov 	err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT,
4211e0da5a48SPavel Emelyanov 			NULL, dflt);
4212e0da5a48SPavel Emelyanov 	if (err < 0)
4213e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
4214e0da5a48SPavel Emelyanov #endif
4215e0da5a48SPavel Emelyanov 	return 0;
4216e0da5a48SPavel Emelyanov 
4217e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4218e0da5a48SPavel Emelyanov err_reg_dflt:
4219e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
4220e0da5a48SPavel Emelyanov err_reg_all:
4221e0da5a48SPavel Emelyanov 	kfree(dflt);
4222e0da5a48SPavel Emelyanov #endif
4223e0da5a48SPavel Emelyanov err_alloc_dflt:
4224e0da5a48SPavel Emelyanov 	kfree(all);
4225e0da5a48SPavel Emelyanov err_alloc_all:
4226e0da5a48SPavel Emelyanov 	return err;
4227e0da5a48SPavel Emelyanov }
4228e0da5a48SPavel Emelyanov 
4229e0da5a48SPavel Emelyanov static void addrconf_exit_net(struct net *net)
4230e0da5a48SPavel Emelyanov {
4231e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4232e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4233e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
4234e0da5a48SPavel Emelyanov #endif
4235e0da5a48SPavel Emelyanov 	if (net != &init_net) {
4236e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_dflt);
4237e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_all);
4238e0da5a48SPavel Emelyanov 	}
4239e0da5a48SPavel Emelyanov }
4240e0da5a48SPavel Emelyanov 
4241e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
4242e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
4243e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
4244e0da5a48SPavel Emelyanov };
4245e0da5a48SPavel Emelyanov 
42461da177e4SLinus Torvalds /*
42471da177e4SLinus Torvalds  *      Device notifier
42481da177e4SLinus Torvalds  */
42491da177e4SLinus Torvalds 
42501da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb)
42511da177e4SLinus Torvalds {
4252e041c683SAlan Stern 	return atomic_notifier_chain_register(&inet6addr_chain, nb);
42531da177e4SLinus Torvalds }
42541da177e4SLinus Torvalds 
42557159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier);
42567159039aSYOSHIFUJI Hideaki 
42571da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb)
42581da177e4SLinus Torvalds {
4259e041c683SAlan Stern 	return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
42601da177e4SLinus Torvalds }
42611da177e4SLinus Torvalds 
42627159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier);
42637159039aSYOSHIFUJI Hideaki 
42641da177e4SLinus Torvalds /*
42651da177e4SLinus Torvalds  *	Init / cleanup code
42661da177e4SLinus Torvalds  */
42671da177e4SLinus Torvalds 
42681da177e4SLinus Torvalds int __init addrconf_init(void)
42691da177e4SLinus Torvalds {
42702a8cc6c8SYOSHIFUJI Hideaki 	int err;
42712a8cc6c8SYOSHIFUJI Hideaki 
42722a8cc6c8SYOSHIFUJI Hideaki 	if ((err = ipv6_addr_label_init()) < 0) {
42732a8cc6c8SYOSHIFUJI Hideaki 		printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
42742a8cc6c8SYOSHIFUJI Hideaki 			err);
42752a8cc6c8SYOSHIFUJI Hideaki 		return err;
42762a8cc6c8SYOSHIFUJI Hideaki 	}
42771da177e4SLinus Torvalds 
4278e0da5a48SPavel Emelyanov 	register_pernet_subsys(&addrconf_ops);
4279e0da5a48SPavel Emelyanov 
42801da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
42811da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
42821da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
42831da177e4SLinus Torvalds 	 * to other devices which are up.
42841da177e4SLinus Torvalds 	 *
42851da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
42861da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
42871da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
42881da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
42891da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
42901da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
42911da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
42921da177e4SLinus Torvalds 	 *
42931da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
42941da177e4SLinus Torvalds 	 * loopback_dev first by hand.
42951da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
42961da177e4SLinus Torvalds 	 * device and it being up should be removed.
42971da177e4SLinus Torvalds 	 */
42981da177e4SLinus Torvalds 	rtnl_lock();
42992774c7abSEric W. Biederman 	if (!ipv6_add_dev(init_net.loopback_dev))
43001da177e4SLinus Torvalds 		err = -ENOMEM;
43011da177e4SLinus Torvalds 	rtnl_unlock();
43021da177e4SLinus Torvalds 	if (err)
4303e0da5a48SPavel Emelyanov 		goto errlo;
43041da177e4SLinus Torvalds 
43052774c7abSEric W. Biederman 	ip6_null_entry.u.dst.dev = init_net.loopback_dev;
43062774c7abSEric W. Biederman 	ip6_null_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
43079a6bf6feSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MULTIPLE_TABLES
43082774c7abSEric W. Biederman 	ip6_prohibit_entry.u.dst.dev = init_net.loopback_dev;
43092774c7abSEric W. Biederman 	ip6_prohibit_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
43102774c7abSEric W. Biederman 	ip6_blk_hole_entry.u.dst.dev = init_net.loopback_dev;
43112774c7abSEric W. Biederman 	ip6_blk_hole_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
43129a6bf6feSYOSHIFUJI Hideaki #endif
4313c62dba90SHerbert Xu 
43141da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
43151da177e4SLinus Torvalds 
43161da177e4SLinus Torvalds 	addrconf_verify(0);
4317c127ea2cSThomas Graf 
4318c127ea2cSThomas Graf 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4319c127ea2cSThomas Graf 	if (err < 0)
4320c127ea2cSThomas Graf 		goto errout;
4321c127ea2cSThomas Graf 
4322c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
4323c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4324c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4325c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4326c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4327c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4328c127ea2cSThomas Graf 
43292a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
43302a8cc6c8SYOSHIFUJI Hideaki 
43311da177e4SLinus Torvalds 	return 0;
4332c127ea2cSThomas Graf errout:
4333c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
4334e0da5a48SPavel Emelyanov errlo:
4335e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
4336c127ea2cSThomas Graf 
4337c127ea2cSThomas Graf 	return err;
43381da177e4SLinus Torvalds }
43391da177e4SLinus Torvalds 
434009f7709fSDaniel Lezcano void addrconf_cleanup(void)
43411da177e4SLinus Torvalds {
43421da177e4SLinus Torvalds 	struct net_device *dev;
43431da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
43441da177e4SLinus Torvalds 	int i;
43451da177e4SLinus Torvalds 
43461da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
43471da177e4SLinus Torvalds 
4348e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
43491da177e4SLinus Torvalds 
43501da177e4SLinus Torvalds 	rtnl_lock();
43511da177e4SLinus Torvalds 
43521da177e4SLinus Torvalds 	/*
43531da177e4SLinus Torvalds 	 *	clean dev list.
43541da177e4SLinus Torvalds 	 */
43551da177e4SLinus Torvalds 
4356881d966bSEric W. Biederman 	for_each_netdev(&init_net, dev) {
4357dffe4f04SMicah Gruber 		if (__in6_dev_get(dev) == NULL)
43581da177e4SLinus Torvalds 			continue;
43591da177e4SLinus Torvalds 		addrconf_ifdown(dev, 1);
43601da177e4SLinus Torvalds 	}
43612774c7abSEric W. Biederman 	addrconf_ifdown(init_net.loopback_dev, 2);
43621da177e4SLinus Torvalds 
43631da177e4SLinus Torvalds 	/*
43641da177e4SLinus Torvalds 	 *	Check hash table.
43651da177e4SLinus Torvalds 	 */
43661da177e4SLinus Torvalds 
43671da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
43681da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
43691da177e4SLinus Torvalds 		for (ifa=inet6_addr_lst[i]; ifa; ) {
43701da177e4SLinus Torvalds 			struct inet6_ifaddr *bifa;
43711da177e4SLinus Torvalds 
43721da177e4SLinus Torvalds 			bifa = ifa;
43731da177e4SLinus Torvalds 			ifa = ifa->lst_next;
43741da177e4SLinus Torvalds 			printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
43751da177e4SLinus Torvalds 			/* Do not free it; something is wrong.
43761da177e4SLinus Torvalds 			   Now we can investigate it with debugger.
43771da177e4SLinus Torvalds 			 */
43781da177e4SLinus Torvalds 		}
43791da177e4SLinus Torvalds 	}
43801da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
43811da177e4SLinus Torvalds 
43821da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
43831da177e4SLinus Torvalds 
43841da177e4SLinus Torvalds 	rtnl_unlock();
43851da177e4SLinus Torvalds }
4386