xref: /linux/net/ipv6/addrconf.c (revision cd354f1ae75e6466a7e31b727faede57a1f89ca5)
1 /*
2  *	IPv6 Address [auto]configuration
3  *	Linux INET6 implementation
4  *
5  *	Authors:
6  *	Pedro Roque		<roque@di.fc.ul.pt>
7  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
8  *
9  *	$Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
10  *
11  *	This program is free software; you can redistribute it and/or
12  *      modify it under the terms of the GNU General Public License
13  *      as published by the Free Software Foundation; either version
14  *      2 of the License, or (at your option) any later version.
15  */
16 
17 /*
18  *	Changes:
19  *
20  *	Janos Farkas			:	delete timer on ifdown
21  *	<chexum@bankinf.banki.hu>
22  *	Andi Kleen			:	kill double kfree on module
23  *						unload.
24  *	Maciej W. Rozycki		:	FDDI support
25  *	sekiya@USAGI			:	Don't send too many RS
26  *						packets.
27  *	yoshfuji@USAGI			:       Fixed interval between DAD
28  *						packets.
29  *	YOSHIFUJI Hideaki @USAGI	:	improved accuracy of
30  *						address validation timer.
31  *	YOSHIFUJI Hideaki @USAGI	:	Privacy Extensions (RFC3041)
32  *						support.
33  *	Yuji SEKIYA @USAGI		:	Don't assign a same IPv6
34  *						address on a same interface.
35  *	YOSHIFUJI Hideaki @USAGI	:	ARCnet support
36  *	YOSHIFUJI Hideaki @USAGI	:	convert /proc/net/if_inet6 to
37  *						seq_file.
38  *	YOSHIFUJI Hideaki @USAGI	:	improved source address
39  *						selection; consider scope,
40  *						status etc.
41  */
42 
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/socket.h>
46 #include <linux/sockios.h>
47 #include <linux/net.h>
48 #include <linux/in6.h>
49 #include <linux/netdevice.h>
50 #include <linux/if_addr.h>
51 #include <linux/if_arp.h>
52 #include <linux/if_arcnet.h>
53 #include <linux/if_infiniband.h>
54 #include <linux/route.h>
55 #include <linux/inetdevice.h>
56 #include <linux/init.h>
57 #ifdef CONFIG_SYSCTL
58 #include <linux/sysctl.h>
59 #endif
60 #include <linux/capability.h>
61 #include <linux/delay.h>
62 #include <linux/notifier.h>
63 #include <linux/string.h>
64 
65 #include <net/sock.h>
66 #include <net/snmp.h>
67 
68 #include <net/ipv6.h>
69 #include <net/protocol.h>
70 #include <net/ndisc.h>
71 #include <net/ip6_route.h>
72 #include <net/addrconf.h>
73 #include <net/tcp.h>
74 #include <net/ip.h>
75 #include <net/netlink.h>
76 #include <linux/if_tunnel.h>
77 #include <linux/rtnetlink.h>
78 
79 #ifdef CONFIG_IPV6_PRIVACY
80 #include <linux/random.h>
81 #endif
82 
83 #include <asm/uaccess.h>
84 
85 #include <linux/proc_fs.h>
86 #include <linux/seq_file.h>
87 
88 /* Set to 3 to get tracing... */
89 #define ACONF_DEBUG 2
90 
91 #if ACONF_DEBUG >= 3
92 #define ADBG(x) printk x
93 #else
94 #define ADBG(x)
95 #endif
96 
97 #define	INFINITY_LIFE_TIME	0xFFFFFFFF
98 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
99 
100 #ifdef CONFIG_SYSCTL
101 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
102 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
103 #endif
104 
105 #ifdef CONFIG_IPV6_PRIVACY
106 static int __ipv6_regen_rndid(struct inet6_dev *idev);
107 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
108 static void ipv6_regen_rndid(unsigned long data);
109 
110 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
111 #endif
112 
113 static int ipv6_count_addresses(struct inet6_dev *idev);
114 
115 /*
116  *	Configured unicast address hash table
117  */
118 static struct inet6_ifaddr		*inet6_addr_lst[IN6_ADDR_HSIZE];
119 static DEFINE_RWLOCK(addrconf_hash_lock);
120 
121 static void addrconf_verify(unsigned long);
122 
123 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
124 static DEFINE_SPINLOCK(addrconf_verify_lock);
125 
126 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
127 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
128 
129 static int addrconf_ifdown(struct net_device *dev, int how);
130 
131 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
132 static void addrconf_dad_timer(unsigned long data);
133 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
134 static void addrconf_dad_run(struct inet6_dev *idev);
135 static void addrconf_rs_timer(unsigned long data);
136 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
137 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
138 
139 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
140 				struct prefix_info *pinfo);
141 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
142 
143 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
144 
145 struct ipv6_devconf ipv6_devconf __read_mostly = {
146 	.forwarding		= 0,
147 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
148 	.mtu6			= IPV6_MIN_MTU,
149 	.accept_ra		= 1,
150 	.accept_redirects	= 1,
151 	.autoconf		= 1,
152 	.force_mld_version	= 0,
153 	.dad_transmits		= 1,
154 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
155 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
156 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
157 #ifdef CONFIG_IPV6_PRIVACY
158 	.use_tempaddr 		= 0,
159 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
160 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
161 	.regen_max_retry	= REGEN_MAX_RETRY,
162 	.max_desync_factor	= MAX_DESYNC_FACTOR,
163 #endif
164 	.max_addresses		= IPV6_MAX_ADDRESSES,
165 	.accept_ra_defrtr	= 1,
166 	.accept_ra_pinfo	= 1,
167 #ifdef CONFIG_IPV6_ROUTER_PREF
168 	.accept_ra_rtr_pref	= 1,
169 	.rtr_probe_interval	= 60 * HZ,
170 #ifdef CONFIG_IPV6_ROUTE_INFO
171 	.accept_ra_rt_info_max_plen = 0,
172 #endif
173 #endif
174 	.proxy_ndp		= 0,
175 };
176 
177 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
178 	.forwarding		= 0,
179 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
180 	.mtu6			= IPV6_MIN_MTU,
181 	.accept_ra		= 1,
182 	.accept_redirects	= 1,
183 	.autoconf		= 1,
184 	.dad_transmits		= 1,
185 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
186 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
187 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
188 #ifdef CONFIG_IPV6_PRIVACY
189 	.use_tempaddr		= 0,
190 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
191 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
192 	.regen_max_retry	= REGEN_MAX_RETRY,
193 	.max_desync_factor	= MAX_DESYNC_FACTOR,
194 #endif
195 	.max_addresses		= IPV6_MAX_ADDRESSES,
196 	.accept_ra_defrtr	= 1,
197 	.accept_ra_pinfo	= 1,
198 #ifdef CONFIG_IPV6_ROUTER_PREF
199 	.accept_ra_rtr_pref	= 1,
200 	.rtr_probe_interval	= 60 * HZ,
201 #ifdef CONFIG_IPV6_ROUTE_INFO
202 	.accept_ra_rt_info_max_plen = 0,
203 #endif
204 #endif
205 	.proxy_ndp		= 0,
206 };
207 
208 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
209 #if 0
210 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
211 #endif
212 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
213 
214 #define IPV6_ADDR_SCOPE_TYPE(scope)	((scope) << 16)
215 
216 static inline unsigned ipv6_addr_scope2type(unsigned scope)
217 {
218 	switch(scope) {
219 	case IPV6_ADDR_SCOPE_NODELOCAL:
220 		return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
221 			IPV6_ADDR_LOOPBACK);
222 	case IPV6_ADDR_SCOPE_LINKLOCAL:
223 		return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
224 			IPV6_ADDR_LINKLOCAL);
225 	case IPV6_ADDR_SCOPE_SITELOCAL:
226 		return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
227 			IPV6_ADDR_SITELOCAL);
228 	}
229 	return IPV6_ADDR_SCOPE_TYPE(scope);
230 }
231 
232 int __ipv6_addr_type(const struct in6_addr *addr)
233 {
234 	__be32 st;
235 
236 	st = addr->s6_addr32[0];
237 
238 	/* Consider all addresses with the first three bits different of
239 	   000 and 111 as unicasts.
240 	 */
241 	if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
242 	    (st & htonl(0xE0000000)) != htonl(0xE0000000))
243 		return (IPV6_ADDR_UNICAST |
244 			IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
245 
246 	if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
247 		/* multicast */
248 		/* addr-select 3.1 */
249 		return (IPV6_ADDR_MULTICAST |
250 			ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
251 	}
252 
253 	if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
254 		return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
255 			IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL));		/* addr-select 3.1 */
256 	if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
257 		return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
258 			IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL));		/* addr-select 3.1 */
259 
260 	if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
261 		if (addr->s6_addr32[2] == 0) {
262 			if (addr->s6_addr32[3] == 0)
263 				return IPV6_ADDR_ANY;
264 
265 			if (addr->s6_addr32[3] == htonl(0x00000001))
266 				return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
267 					IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL));	/* addr-select 3.4 */
268 
269 			return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
270 				IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));	/* addr-select 3.3 */
271 		}
272 
273 		if (addr->s6_addr32[2] == htonl(0x0000ffff))
274 			return (IPV6_ADDR_MAPPED |
275 				IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));	/* addr-select 3.3 */
276 	}
277 
278 	return (IPV6_ADDR_RESERVED |
279 		IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));	/* addr-select 3.4 */
280 }
281 
282 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
283 {
284 	if (del_timer(&ifp->timer))
285 		__in6_ifa_put(ifp);
286 }
287 
288 enum addrconf_timer_t
289 {
290 	AC_NONE,
291 	AC_DAD,
292 	AC_RS,
293 };
294 
295 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
296 			       enum addrconf_timer_t what,
297 			       unsigned long when)
298 {
299 	if (!del_timer(&ifp->timer))
300 		in6_ifa_hold(ifp);
301 
302 	switch (what) {
303 	case AC_DAD:
304 		ifp->timer.function = addrconf_dad_timer;
305 		break;
306 	case AC_RS:
307 		ifp->timer.function = addrconf_rs_timer;
308 		break;
309 	default:;
310 	}
311 	ifp->timer.expires = jiffies + when;
312 	add_timer(&ifp->timer);
313 }
314 
315 /* Nobody refers to this device, we may destroy it. */
316 
317 static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
318 {
319 	struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
320 	kfree(idev);
321 }
322 
323 void in6_dev_finish_destroy(struct inet6_dev *idev)
324 {
325 	struct net_device *dev = idev->dev;
326 	BUG_TRAP(idev->addr_list==NULL);
327 	BUG_TRAP(idev->mc_list==NULL);
328 #ifdef NET_REFCNT_DEBUG
329 	printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
330 #endif
331 	dev_put(dev);
332 	if (!idev->dead) {
333 		printk("Freeing alive inet6 device %p\n", idev);
334 		return;
335 	}
336 	snmp6_free_dev(idev);
337 	call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
338 }
339 
340 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
341 {
342 	struct inet6_dev *ndev;
343 	struct in6_addr maddr;
344 
345 	ASSERT_RTNL();
346 
347 	if (dev->mtu < IPV6_MIN_MTU)
348 		return NULL;
349 
350 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
351 
352 	if (ndev == NULL)
353 		return NULL;
354 
355 	rwlock_init(&ndev->lock);
356 	ndev->dev = dev;
357 	memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
358 	ndev->cnf.mtu6 = dev->mtu;
359 	ndev->cnf.sysctl = NULL;
360 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
361 	if (ndev->nd_parms == NULL) {
362 		kfree(ndev);
363 		return NULL;
364 	}
365 	/* We refer to the device */
366 	dev_hold(dev);
367 
368 	if (snmp6_alloc_dev(ndev) < 0) {
369 		ADBG((KERN_WARNING
370 			"%s(): cannot allocate memory for statistics; dev=%s.\n",
371 			__FUNCTION__, dev->name));
372 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
373 		ndev->dead = 1;
374 		in6_dev_finish_destroy(ndev);
375 		return NULL;
376 	}
377 
378 	if (snmp6_register_dev(ndev) < 0) {
379 		ADBG((KERN_WARNING
380 			"%s(): cannot create /proc/net/dev_snmp6/%s\n",
381 			__FUNCTION__, dev->name));
382 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
383 		ndev->dead = 1;
384 		in6_dev_finish_destroy(ndev);
385 		return NULL;
386 	}
387 
388 	/* One reference from device.  We must do this before
389 	 * we invoke __ipv6_regen_rndid().
390 	 */
391 	in6_dev_hold(ndev);
392 
393 #ifdef CONFIG_IPV6_PRIVACY
394 	init_timer(&ndev->regen_timer);
395 	ndev->regen_timer.function = ipv6_regen_rndid;
396 	ndev->regen_timer.data = (unsigned long) ndev;
397 	if ((dev->flags&IFF_LOOPBACK) ||
398 	    dev->type == ARPHRD_TUNNEL ||
399 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
400 	    dev->type == ARPHRD_SIT ||
401 #endif
402 	    dev->type == ARPHRD_NONE) {
403 		printk(KERN_INFO
404 		       "%s: Disabled Privacy Extensions\n",
405 		       dev->name);
406 		ndev->cnf.use_tempaddr = -1;
407 	} else {
408 		in6_dev_hold(ndev);
409 		ipv6_regen_rndid((unsigned long) ndev);
410 	}
411 #endif
412 
413 	if (netif_carrier_ok(dev))
414 		ndev->if_flags |= IF_READY;
415 
416 
417 	ipv6_mc_init_dev(ndev);
418 	ndev->tstamp = jiffies;
419 #ifdef CONFIG_SYSCTL
420 	neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
421 			      NET_IPV6_NEIGH, "ipv6",
422 			      &ndisc_ifinfo_sysctl_change,
423 			      NULL);
424 	addrconf_sysctl_register(ndev, &ndev->cnf);
425 #endif
426 	/* protected by rtnl_lock */
427 	rcu_assign_pointer(dev->ip6_ptr, ndev);
428 
429 	/* Join all-node multicast group */
430 	ipv6_addr_all_nodes(&maddr);
431 	ipv6_dev_mc_inc(dev, &maddr);
432 
433 	return ndev;
434 }
435 
436 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
437 {
438 	struct inet6_dev *idev;
439 
440 	ASSERT_RTNL();
441 
442 	if ((idev = __in6_dev_get(dev)) == NULL) {
443 		if ((idev = ipv6_add_dev(dev)) == NULL)
444 			return NULL;
445 	}
446 
447 	if (dev->flags&IFF_UP)
448 		ipv6_mc_up(idev);
449 	return idev;
450 }
451 
452 #ifdef CONFIG_SYSCTL
453 static void dev_forward_change(struct inet6_dev *idev)
454 {
455 	struct net_device *dev;
456 	struct inet6_ifaddr *ifa;
457 	struct in6_addr addr;
458 
459 	if (!idev)
460 		return;
461 	dev = idev->dev;
462 	if (dev && (dev->flags & IFF_MULTICAST)) {
463 		ipv6_addr_all_routers(&addr);
464 
465 		if (idev->cnf.forwarding)
466 			ipv6_dev_mc_inc(dev, &addr);
467 		else
468 			ipv6_dev_mc_dec(dev, &addr);
469 	}
470 	for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
471 		if (idev->cnf.forwarding)
472 			addrconf_join_anycast(ifa);
473 		else
474 			addrconf_leave_anycast(ifa);
475 	}
476 }
477 
478 
479 static void addrconf_forward_change(void)
480 {
481 	struct net_device *dev;
482 	struct inet6_dev *idev;
483 
484 	read_lock(&dev_base_lock);
485 	for (dev=dev_base; dev; dev=dev->next) {
486 		rcu_read_lock();
487 		idev = __in6_dev_get(dev);
488 		if (idev) {
489 			int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
490 			idev->cnf.forwarding = ipv6_devconf.forwarding;
491 			if (changed)
492 				dev_forward_change(idev);
493 		}
494 		rcu_read_unlock();
495 	}
496 	read_unlock(&dev_base_lock);
497 }
498 #endif
499 
500 /* Nobody refers to this ifaddr, destroy it */
501 
502 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
503 {
504 	BUG_TRAP(ifp->if_next==NULL);
505 	BUG_TRAP(ifp->lst_next==NULL);
506 #ifdef NET_REFCNT_DEBUG
507 	printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
508 #endif
509 
510 	in6_dev_put(ifp->idev);
511 
512 	if (del_timer(&ifp->timer))
513 		printk("Timer is still running, when freeing ifa=%p\n", ifp);
514 
515 	if (!ifp->dead) {
516 		printk("Freeing alive inet6 address %p\n", ifp);
517 		return;
518 	}
519 	dst_release(&ifp->rt->u.dst);
520 
521 	kfree(ifp);
522 }
523 
524 static void
525 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
526 {
527 	struct inet6_ifaddr *ifa, **ifap;
528 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
529 
530 	/*
531 	 * Each device address list is sorted in order of scope -
532 	 * global before linklocal.
533 	 */
534 	for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
535 	     ifap = &ifa->if_next) {
536 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
537 			break;
538 	}
539 
540 	ifp->if_next = *ifap;
541 	*ifap = ifp;
542 }
543 
544 /* On success it returns ifp with increased reference count */
545 
546 static struct inet6_ifaddr *
547 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
548 	      int scope, u32 flags)
549 {
550 	struct inet6_ifaddr *ifa = NULL;
551 	struct rt6_info *rt;
552 	int hash;
553 	int err = 0;
554 
555 	rcu_read_lock_bh();
556 	if (idev->dead) {
557 		err = -ENODEV;			/*XXX*/
558 		goto out2;
559 	}
560 
561 	write_lock(&addrconf_hash_lock);
562 
563 	/* Ignore adding duplicate addresses on an interface */
564 	if (ipv6_chk_same_addr(addr, idev->dev)) {
565 		ADBG(("ipv6_add_addr: already assigned\n"));
566 		err = -EEXIST;
567 		goto out;
568 	}
569 
570 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
571 
572 	if (ifa == NULL) {
573 		ADBG(("ipv6_add_addr: malloc failed\n"));
574 		err = -ENOBUFS;
575 		goto out;
576 	}
577 
578 	rt = addrconf_dst_alloc(idev, addr, 0);
579 	if (IS_ERR(rt)) {
580 		err = PTR_ERR(rt);
581 		goto out;
582 	}
583 
584 	ipv6_addr_copy(&ifa->addr, addr);
585 
586 	spin_lock_init(&ifa->lock);
587 	init_timer(&ifa->timer);
588 	ifa->timer.data = (unsigned long) ifa;
589 	ifa->scope = scope;
590 	ifa->prefix_len = pfxlen;
591 	ifa->flags = flags | IFA_F_TENTATIVE;
592 	ifa->cstamp = ifa->tstamp = jiffies;
593 
594 	ifa->rt = rt;
595 
596 	ifa->idev = idev;
597 	in6_dev_hold(idev);
598 	/* For caller */
599 	in6_ifa_hold(ifa);
600 
601 	/* Add to big hash table */
602 	hash = ipv6_addr_hash(addr);
603 
604 	ifa->lst_next = inet6_addr_lst[hash];
605 	inet6_addr_lst[hash] = ifa;
606 	in6_ifa_hold(ifa);
607 	write_unlock(&addrconf_hash_lock);
608 
609 	write_lock(&idev->lock);
610 	/* Add to inet6_dev unicast addr list. */
611 	ipv6_link_dev_addr(idev, ifa);
612 
613 #ifdef CONFIG_IPV6_PRIVACY
614 	if (ifa->flags&IFA_F_TEMPORARY) {
615 		ifa->tmp_next = idev->tempaddr_list;
616 		idev->tempaddr_list = ifa;
617 		in6_ifa_hold(ifa);
618 	}
619 #endif
620 
621 	in6_ifa_hold(ifa);
622 	write_unlock(&idev->lock);
623 out2:
624 	rcu_read_unlock_bh();
625 
626 	if (likely(err == 0))
627 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
628 	else {
629 		kfree(ifa);
630 		ifa = ERR_PTR(err);
631 	}
632 
633 	return ifa;
634 out:
635 	write_unlock(&addrconf_hash_lock);
636 	goto out2;
637 }
638 
639 /* This function wants to get referenced ifp and releases it before return */
640 
641 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
642 {
643 	struct inet6_ifaddr *ifa, **ifap;
644 	struct inet6_dev *idev = ifp->idev;
645 	int hash;
646 	int deleted = 0, onlink = 0;
647 	unsigned long expires = jiffies;
648 
649 	hash = ipv6_addr_hash(&ifp->addr);
650 
651 	ifp->dead = 1;
652 
653 	write_lock_bh(&addrconf_hash_lock);
654 	for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
655 	     ifap = &ifa->lst_next) {
656 		if (ifa == ifp) {
657 			*ifap = ifa->lst_next;
658 			__in6_ifa_put(ifp);
659 			ifa->lst_next = NULL;
660 			break;
661 		}
662 	}
663 	write_unlock_bh(&addrconf_hash_lock);
664 
665 	write_lock_bh(&idev->lock);
666 #ifdef CONFIG_IPV6_PRIVACY
667 	if (ifp->flags&IFA_F_TEMPORARY) {
668 		for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
669 		     ifap = &ifa->tmp_next) {
670 			if (ifa == ifp) {
671 				*ifap = ifa->tmp_next;
672 				if (ifp->ifpub) {
673 					in6_ifa_put(ifp->ifpub);
674 					ifp->ifpub = NULL;
675 				}
676 				__in6_ifa_put(ifp);
677 				ifa->tmp_next = NULL;
678 				break;
679 			}
680 		}
681 	}
682 #endif
683 
684 	for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
685 		if (ifa == ifp) {
686 			*ifap = ifa->if_next;
687 			__in6_ifa_put(ifp);
688 			ifa->if_next = NULL;
689 			if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
690 				break;
691 			deleted = 1;
692 			continue;
693 		} else if (ifp->flags & IFA_F_PERMANENT) {
694 			if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
695 					      ifp->prefix_len)) {
696 				if (ifa->flags & IFA_F_PERMANENT) {
697 					onlink = 1;
698 					if (deleted)
699 						break;
700 				} else {
701 					unsigned long lifetime;
702 
703 					if (!onlink)
704 						onlink = -1;
705 
706 					spin_lock(&ifa->lock);
707 					lifetime = min_t(unsigned long,
708 							 ifa->valid_lft, 0x7fffffffUL/HZ);
709 					if (time_before(expires,
710 							ifa->tstamp + lifetime * HZ))
711 						expires = ifa->tstamp + lifetime * HZ;
712 					spin_unlock(&ifa->lock);
713 				}
714 			}
715 		}
716 		ifap = &ifa->if_next;
717 	}
718 	write_unlock_bh(&idev->lock);
719 
720 	ipv6_ifa_notify(RTM_DELADDR, ifp);
721 
722 	atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
723 
724 	addrconf_del_timer(ifp);
725 
726 	/*
727 	 * Purge or update corresponding prefix
728 	 *
729 	 * 1) we don't purge prefix here if address was not permanent.
730 	 *    prefix is managed by its own lifetime.
731 	 * 2) if there're no addresses, delete prefix.
732 	 * 3) if there're still other permanent address(es),
733 	 *    corresponding prefix is still permanent.
734 	 * 4) otherwise, update prefix lifetime to the
735 	 *    longest valid lifetime among the corresponding
736 	 *    addresses on the device.
737 	 *    Note: subsequent RA will update lifetime.
738 	 *
739 	 * --yoshfuji
740 	 */
741 	if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
742 		struct in6_addr prefix;
743 		struct rt6_info *rt;
744 
745 		ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
746 		rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
747 
748 		if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
749 			if (onlink == 0) {
750 				ip6_del_rt(rt);
751 				rt = NULL;
752 			} else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
753 				rt->rt6i_expires = expires;
754 				rt->rt6i_flags |= RTF_EXPIRES;
755 			}
756 		}
757 		dst_release(&rt->u.dst);
758 	}
759 
760 	in6_ifa_put(ifp);
761 }
762 
763 #ifdef CONFIG_IPV6_PRIVACY
764 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
765 {
766 	struct inet6_dev *idev = ifp->idev;
767 	struct in6_addr addr, *tmpaddr;
768 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
769 	int tmp_plen;
770 	int ret = 0;
771 	int max_addresses;
772 
773 	write_lock(&idev->lock);
774 	if (ift) {
775 		spin_lock_bh(&ift->lock);
776 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
777 		spin_unlock_bh(&ift->lock);
778 		tmpaddr = &addr;
779 	} else {
780 		tmpaddr = NULL;
781 	}
782 retry:
783 	in6_dev_hold(idev);
784 	if (idev->cnf.use_tempaddr <= 0) {
785 		write_unlock(&idev->lock);
786 		printk(KERN_INFO
787 			"ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
788 		in6_dev_put(idev);
789 		ret = -1;
790 		goto out;
791 	}
792 	spin_lock_bh(&ifp->lock);
793 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
794 		idev->cnf.use_tempaddr = -1;	/*XXX*/
795 		spin_unlock_bh(&ifp->lock);
796 		write_unlock(&idev->lock);
797 		printk(KERN_WARNING
798 			"ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
799 		in6_dev_put(idev);
800 		ret = -1;
801 		goto out;
802 	}
803 	in6_ifa_hold(ifp);
804 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
805 	if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
806 		spin_unlock_bh(&ifp->lock);
807 		write_unlock(&idev->lock);
808 		printk(KERN_WARNING
809 			"ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
810 		in6_ifa_put(ifp);
811 		in6_dev_put(idev);
812 		ret = -1;
813 		goto out;
814 	}
815 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
816 	tmp_valid_lft = min_t(__u32,
817 			      ifp->valid_lft,
818 			      idev->cnf.temp_valid_lft);
819 	tmp_prefered_lft = min_t(__u32,
820 				 ifp->prefered_lft,
821 				 idev->cnf.temp_prefered_lft - desync_factor / HZ);
822 	tmp_plen = ifp->prefix_len;
823 	max_addresses = idev->cnf.max_addresses;
824 	tmp_cstamp = ifp->cstamp;
825 	tmp_tstamp = ifp->tstamp;
826 	spin_unlock_bh(&ifp->lock);
827 
828 	write_unlock(&idev->lock);
829 	ift = !max_addresses ||
830 	      ipv6_count_addresses(idev) < max_addresses ?
831 		ipv6_add_addr(idev, &addr, tmp_plen,
832 			      ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
833 	if (!ift || IS_ERR(ift)) {
834 		in6_ifa_put(ifp);
835 		in6_dev_put(idev);
836 		printk(KERN_INFO
837 			"ipv6_create_tempaddr(): retry temporary address regeneration.\n");
838 		tmpaddr = &addr;
839 		write_lock(&idev->lock);
840 		goto retry;
841 	}
842 
843 	spin_lock_bh(&ift->lock);
844 	ift->ifpub = ifp;
845 	ift->valid_lft = tmp_valid_lft;
846 	ift->prefered_lft = tmp_prefered_lft;
847 	ift->cstamp = tmp_cstamp;
848 	ift->tstamp = tmp_tstamp;
849 	spin_unlock_bh(&ift->lock);
850 
851 	addrconf_dad_start(ift, 0);
852 	in6_ifa_put(ift);
853 	in6_dev_put(idev);
854 out:
855 	return ret;
856 }
857 #endif
858 
859 /*
860  *	Choose an appropriate source address (RFC3484)
861  */
862 struct ipv6_saddr_score {
863 	int		addr_type;
864 	unsigned int	attrs;
865 	int		matchlen;
866 	int		scope;
867 	unsigned int	rule;
868 };
869 
870 #define IPV6_SADDR_SCORE_LOCAL		0x0001
871 #define IPV6_SADDR_SCORE_PREFERRED	0x0004
872 #define IPV6_SADDR_SCORE_HOA		0x0008
873 #define IPV6_SADDR_SCORE_OIF		0x0010
874 #define IPV6_SADDR_SCORE_LABEL		0x0020
875 #define IPV6_SADDR_SCORE_PRIVACY	0x0040
876 
877 static int inline ipv6_saddr_preferred(int type)
878 {
879 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
880 		    IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
881 		return 1;
882 	return 0;
883 }
884 
885 /* static matching label */
886 static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
887 {
888  /*
889   * 	prefix (longest match)	label
890   * 	-----------------------------
891   * 	::1/128			0
892   * 	::/0			1
893   * 	2002::/16		2
894   * 	::/96			3
895   * 	::ffff:0:0/96		4
896   *	fc00::/7		5
897   * 	2001::/32		6
898   */
899 	if (type & IPV6_ADDR_LOOPBACK)
900 		return 0;
901 	else if (type & IPV6_ADDR_COMPATv4)
902 		return 3;
903 	else if (type & IPV6_ADDR_MAPPED)
904 		return 4;
905 	else if (addr->s6_addr32[0] == htonl(0x20010000))
906 		return 6;
907 	else if (addr->s6_addr16[0] == htons(0x2002))
908 		return 2;
909 	else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
910 		return 5;
911 	return 1;
912 }
913 
914 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
915 		       struct in6_addr *daddr, struct in6_addr *saddr)
916 {
917 	struct ipv6_saddr_score hiscore;
918 	struct inet6_ifaddr *ifa_result = NULL;
919 	int daddr_type = __ipv6_addr_type(daddr);
920 	int daddr_scope = __ipv6_addr_src_scope(daddr_type);
921 	u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
922 	struct net_device *dev;
923 
924 	memset(&hiscore, 0, sizeof(hiscore));
925 
926 	read_lock(&dev_base_lock);
927 	rcu_read_lock();
928 
929 	for (dev = dev_base; dev; dev=dev->next) {
930 		struct inet6_dev *idev;
931 		struct inet6_ifaddr *ifa;
932 
933 		/* Rule 0: Candidate Source Address (section 4)
934 		 *  - multicast and link-local destination address,
935 		 *    the set of candidate source address MUST only
936 		 *    include addresses assigned to interfaces
937 		 *    belonging to the same link as the outgoing
938 		 *    interface.
939 		 * (- For site-local destination addresses, the
940 		 *    set of candidate source addresses MUST only
941 		 *    include addresses assigned to interfaces
942 		 *    belonging to the same site as the outgoing
943 		 *    interface.)
944 		 */
945 		if ((daddr_type & IPV6_ADDR_MULTICAST ||
946 		     daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
947 		    daddr_dev && dev != daddr_dev)
948 			continue;
949 
950 		idev = __in6_dev_get(dev);
951 		if (!idev)
952 			continue;
953 
954 		read_lock_bh(&idev->lock);
955 		for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
956 			struct ipv6_saddr_score score;
957 
958 			score.addr_type = __ipv6_addr_type(&ifa->addr);
959 
960 			/* Rule 0:
961 			 * - Tentative Address (RFC2462 section 5.4)
962 			 *  - A tentative address is not considered
963 			 *    "assigned to an interface" in the traditional
964 			 *    sense.
965 			 * - Candidate Source Address (section 4)
966 			 *  - In any case, anycast addresses, multicast
967 			 *    addresses, and the unspecified address MUST
968 			 *    NOT be included in a candidate set.
969 			 */
970 			if (ifa->flags & IFA_F_TENTATIVE)
971 				continue;
972 			if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
973 				     score.addr_type & IPV6_ADDR_MULTICAST)) {
974 				LIMIT_NETDEBUG(KERN_DEBUG
975 					       "ADDRCONF: unspecified / multicast address"
976 					       "assigned as unicast address on %s",
977 					       dev->name);
978 				continue;
979 			}
980 
981 			score.attrs = 0;
982 			score.matchlen = 0;
983 			score.scope = 0;
984 			score.rule = 0;
985 
986 			if (ifa_result == NULL) {
987 				/* record it if the first available entry */
988 				goto record_it;
989 			}
990 
991 			/* Rule 1: Prefer same address */
992 			if (hiscore.rule < 1) {
993 				if (ipv6_addr_equal(&ifa_result->addr, daddr))
994 					hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
995 				hiscore.rule++;
996 			}
997 			if (ipv6_addr_equal(&ifa->addr, daddr)) {
998 				score.attrs |= IPV6_SADDR_SCORE_LOCAL;
999 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
1000 					score.rule = 1;
1001 					goto record_it;
1002 				}
1003 			} else {
1004 				if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
1005 					continue;
1006 			}
1007 
1008 			/* Rule 2: Prefer appropriate scope */
1009 			if (hiscore.rule < 2) {
1010 				hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
1011 				hiscore.rule++;
1012 			}
1013 			score.scope = __ipv6_addr_src_scope(score.addr_type);
1014 			if (hiscore.scope < score.scope) {
1015 				if (hiscore.scope < daddr_scope) {
1016 					score.rule = 2;
1017 					goto record_it;
1018 				} else
1019 					continue;
1020 			} else if (score.scope < hiscore.scope) {
1021 				if (score.scope < daddr_scope)
1022 					break; /* addresses sorted by scope */
1023 				else {
1024 					score.rule = 2;
1025 					goto record_it;
1026 				}
1027 			}
1028 
1029 			/* Rule 3: Avoid deprecated address */
1030 			if (hiscore.rule < 3) {
1031 				if (ipv6_saddr_preferred(hiscore.addr_type) ||
1032 				    !(ifa_result->flags & IFA_F_DEPRECATED))
1033 					hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1034 				hiscore.rule++;
1035 			}
1036 			if (ipv6_saddr_preferred(score.addr_type) ||
1037 			    !(ifa->flags & IFA_F_DEPRECATED)) {
1038 				score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1039 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1040 					score.rule = 3;
1041 					goto record_it;
1042 				}
1043 			} else {
1044 				if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1045 					continue;
1046 			}
1047 
1048 			/* Rule 4: Prefer home address */
1049 #ifdef CONFIG_IPV6_MIP6
1050 			if (hiscore.rule < 4) {
1051 				if (ifa_result->flags & IFA_F_HOMEADDRESS)
1052 					hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
1053 				hiscore.rule++;
1054 			}
1055 			if (ifa->flags & IFA_F_HOMEADDRESS) {
1056 				score.attrs |= IPV6_SADDR_SCORE_HOA;
1057 				if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
1058 					score.rule = 4;
1059 					goto record_it;
1060 				}
1061 			} else {
1062 				if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
1063 					continue;
1064 			}
1065 #else
1066 			if (hiscore.rule < 4)
1067 				hiscore.rule++;
1068 #endif
1069 
1070 			/* Rule 5: Prefer outgoing interface */
1071 			if (hiscore.rule < 5) {
1072 				if (daddr_dev == NULL ||
1073 				    daddr_dev == ifa_result->idev->dev)
1074 					hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1075 				hiscore.rule++;
1076 			}
1077 			if (daddr_dev == NULL ||
1078 			    daddr_dev == ifa->idev->dev) {
1079 				score.attrs |= IPV6_SADDR_SCORE_OIF;
1080 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1081 					score.rule = 5;
1082 					goto record_it;
1083 				}
1084 			} else {
1085 				if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1086 					continue;
1087 			}
1088 
1089 			/* Rule 6: Prefer matching label */
1090 			if (hiscore.rule < 6) {
1091 				if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1092 					hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1093 				hiscore.rule++;
1094 			}
1095 			if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1096 				score.attrs |= IPV6_SADDR_SCORE_LABEL;
1097 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1098 					score.rule = 6;
1099 					goto record_it;
1100 				}
1101 			} else {
1102 				if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1103 					continue;
1104 			}
1105 
1106 #ifdef CONFIG_IPV6_PRIVACY
1107 			/* Rule 7: Prefer public address
1108 			 * Note: prefer temprary address if use_tempaddr >= 2
1109 			 */
1110 			if (hiscore.rule < 7) {
1111 				if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1112 				    (ifa_result->idev->cnf.use_tempaddr >= 2))
1113 					hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1114 				hiscore.rule++;
1115 			}
1116 			if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1117 			    (ifa->idev->cnf.use_tempaddr >= 2)) {
1118 				score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1119 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1120 					score.rule = 7;
1121 					goto record_it;
1122 				}
1123 			} else {
1124 				if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1125 					continue;
1126 			}
1127 #else
1128 			if (hiscore.rule < 7)
1129 				hiscore.rule++;
1130 #endif
1131 			/* Rule 8: Use longest matching prefix */
1132 			if (hiscore.rule < 8) {
1133 				hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
1134 				hiscore.rule++;
1135 			}
1136 			score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1137 			if (score.matchlen > hiscore.matchlen) {
1138 				score.rule = 8;
1139 				goto record_it;
1140 			}
1141 #if 0
1142 			else if (score.matchlen < hiscore.matchlen)
1143 				continue;
1144 #endif
1145 
1146 			/* Final Rule: choose first available one */
1147 			continue;
1148 record_it:
1149 			if (ifa_result)
1150 				in6_ifa_put(ifa_result);
1151 			in6_ifa_hold(ifa);
1152 			ifa_result = ifa;
1153 			hiscore = score;
1154 		}
1155 		read_unlock_bh(&idev->lock);
1156 	}
1157 	rcu_read_unlock();
1158 	read_unlock(&dev_base_lock);
1159 
1160 	if (!ifa_result)
1161 		return -EADDRNOTAVAIL;
1162 
1163 	ipv6_addr_copy(saddr, &ifa_result->addr);
1164 	in6_ifa_put(ifa_result);
1165 	return 0;
1166 }
1167 
1168 
1169 int ipv6_get_saddr(struct dst_entry *dst,
1170 		   struct in6_addr *daddr, struct in6_addr *saddr)
1171 {
1172 	return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
1173 }
1174 
1175 
1176 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1177 {
1178 	struct inet6_dev *idev;
1179 	int err = -EADDRNOTAVAIL;
1180 
1181 	rcu_read_lock();
1182 	if ((idev = __in6_dev_get(dev)) != NULL) {
1183 		struct inet6_ifaddr *ifp;
1184 
1185 		read_lock_bh(&idev->lock);
1186 		for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1187 			if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1188 				ipv6_addr_copy(addr, &ifp->addr);
1189 				err = 0;
1190 				break;
1191 			}
1192 		}
1193 		read_unlock_bh(&idev->lock);
1194 	}
1195 	rcu_read_unlock();
1196 	return err;
1197 }
1198 
1199 static int ipv6_count_addresses(struct inet6_dev *idev)
1200 {
1201 	int cnt = 0;
1202 	struct inet6_ifaddr *ifp;
1203 
1204 	read_lock_bh(&idev->lock);
1205 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1206 		cnt++;
1207 	read_unlock_bh(&idev->lock);
1208 	return cnt;
1209 }
1210 
1211 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1212 {
1213 	struct inet6_ifaddr * ifp;
1214 	u8 hash = ipv6_addr_hash(addr);
1215 
1216 	read_lock_bh(&addrconf_hash_lock);
1217 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1218 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1219 		    !(ifp->flags&IFA_F_TENTATIVE)) {
1220 			if (dev == NULL || ifp->idev->dev == dev ||
1221 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1222 				break;
1223 		}
1224 	}
1225 	read_unlock_bh(&addrconf_hash_lock);
1226 	return ifp != NULL;
1227 }
1228 
1229 static
1230 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1231 {
1232 	struct inet6_ifaddr * ifp;
1233 	u8 hash = ipv6_addr_hash(addr);
1234 
1235 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1236 		if (ipv6_addr_equal(&ifp->addr, addr)) {
1237 			if (dev == NULL || ifp->idev->dev == dev)
1238 				break;
1239 		}
1240 	}
1241 	return ifp != NULL;
1242 }
1243 
1244 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1245 {
1246 	struct inet6_ifaddr * ifp;
1247 	u8 hash = ipv6_addr_hash(addr);
1248 
1249 	read_lock_bh(&addrconf_hash_lock);
1250 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1251 		if (ipv6_addr_equal(&ifp->addr, addr)) {
1252 			if (dev == NULL || ifp->idev->dev == dev ||
1253 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1254 				in6_ifa_hold(ifp);
1255 				break;
1256 			}
1257 		}
1258 	}
1259 	read_unlock_bh(&addrconf_hash_lock);
1260 
1261 	return ifp;
1262 }
1263 
1264 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1265 {
1266 	const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1267 	const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
1268 	__be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1269 	__be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1270 	int sk_ipv6only = ipv6_only_sock(sk);
1271 	int sk2_ipv6only = inet_v6_ipv6only(sk2);
1272 	int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1273 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1274 
1275 	if (!sk2_rcv_saddr && !sk_ipv6only)
1276 		return 1;
1277 
1278 	if (addr_type2 == IPV6_ADDR_ANY &&
1279 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1280 		return 1;
1281 
1282 	if (addr_type == IPV6_ADDR_ANY &&
1283 	    !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1284 		return 1;
1285 
1286 	if (sk2_rcv_saddr6 &&
1287 	    ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1288 		return 1;
1289 
1290 	if (addr_type == IPV6_ADDR_MAPPED &&
1291 	    !sk2_ipv6only &&
1292 	    (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1293 		return 1;
1294 
1295 	return 0;
1296 }
1297 
1298 /* Gets referenced address, destroys ifaddr */
1299 
1300 static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1301 {
1302 	if (ifp->flags&IFA_F_PERMANENT) {
1303 		spin_lock_bh(&ifp->lock);
1304 		addrconf_del_timer(ifp);
1305 		ifp->flags |= IFA_F_TENTATIVE;
1306 		spin_unlock_bh(&ifp->lock);
1307 		in6_ifa_put(ifp);
1308 #ifdef CONFIG_IPV6_PRIVACY
1309 	} else if (ifp->flags&IFA_F_TEMPORARY) {
1310 		struct inet6_ifaddr *ifpub;
1311 		spin_lock_bh(&ifp->lock);
1312 		ifpub = ifp->ifpub;
1313 		if (ifpub) {
1314 			in6_ifa_hold(ifpub);
1315 			spin_unlock_bh(&ifp->lock);
1316 			ipv6_create_tempaddr(ifpub, ifp);
1317 			in6_ifa_put(ifpub);
1318 		} else {
1319 			spin_unlock_bh(&ifp->lock);
1320 		}
1321 		ipv6_del_addr(ifp);
1322 #endif
1323 	} else
1324 		ipv6_del_addr(ifp);
1325 }
1326 
1327 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1328 {
1329 	if (net_ratelimit())
1330 		printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1331 	addrconf_dad_stop(ifp);
1332 }
1333 
1334 /* Join to solicited addr multicast group. */
1335 
1336 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1337 {
1338 	struct in6_addr maddr;
1339 
1340 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1341 		return;
1342 
1343 	addrconf_addr_solict_mult(addr, &maddr);
1344 	ipv6_dev_mc_inc(dev, &maddr);
1345 }
1346 
1347 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1348 {
1349 	struct in6_addr maddr;
1350 
1351 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1352 		return;
1353 
1354 	addrconf_addr_solict_mult(addr, &maddr);
1355 	__ipv6_dev_mc_dec(idev, &maddr);
1356 }
1357 
1358 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1359 {
1360 	struct in6_addr addr;
1361 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1362 	if (ipv6_addr_any(&addr))
1363 		return;
1364 	ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1365 }
1366 
1367 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1368 {
1369 	struct in6_addr addr;
1370 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1371 	if (ipv6_addr_any(&addr))
1372 		return;
1373 	__ipv6_dev_ac_dec(ifp->idev, &addr);
1374 }
1375 
1376 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1377 {
1378 	if (dev->addr_len != ETH_ALEN)
1379 		return -1;
1380 	memcpy(eui, dev->dev_addr, 3);
1381 	memcpy(eui + 5, dev->dev_addr + 3, 3);
1382 
1383 	/*
1384 	 * The zSeries OSA network cards can be shared among various
1385 	 * OS instances, but the OSA cards have only one MAC address.
1386 	 * This leads to duplicate address conflicts in conjunction
1387 	 * with IPv6 if more than one instance uses the same card.
1388 	 *
1389 	 * The driver for these cards can deliver a unique 16-bit
1390 	 * identifier for each instance sharing the same card.  It is
1391 	 * placed instead of 0xFFFE in the interface identifier.  The
1392 	 * "u" bit of the interface identifier is not inverted in this
1393 	 * case.  Hence the resulting interface identifier has local
1394 	 * scope according to RFC2373.
1395 	 */
1396 	if (dev->dev_id) {
1397 		eui[3] = (dev->dev_id >> 8) & 0xFF;
1398 		eui[4] = dev->dev_id & 0xFF;
1399 	} else {
1400 		eui[3] = 0xFF;
1401 		eui[4] = 0xFE;
1402 		eui[0] ^= 2;
1403 	}
1404 	return 0;
1405 }
1406 
1407 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1408 {
1409 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
1410 	if (dev->addr_len != ARCNET_ALEN)
1411 		return -1;
1412 	memset(eui, 0, 7);
1413 	eui[7] = *(u8*)dev->dev_addr;
1414 	return 0;
1415 }
1416 
1417 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1418 {
1419 	if (dev->addr_len != INFINIBAND_ALEN)
1420 		return -1;
1421 	memcpy(eui, dev->dev_addr + 12, 8);
1422 	eui[0] |= 2;
1423 	return 0;
1424 }
1425 
1426 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1427 {
1428 	switch (dev->type) {
1429 	case ARPHRD_ETHER:
1430 	case ARPHRD_FDDI:
1431 	case ARPHRD_IEEE802_TR:
1432 		return addrconf_ifid_eui48(eui, dev);
1433 	case ARPHRD_ARCNET:
1434 		return addrconf_ifid_arcnet(eui, dev);
1435 	case ARPHRD_INFINIBAND:
1436 		return addrconf_ifid_infiniband(eui, dev);
1437 	}
1438 	return -1;
1439 }
1440 
1441 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1442 {
1443 	int err = -1;
1444 	struct inet6_ifaddr *ifp;
1445 
1446 	read_lock_bh(&idev->lock);
1447 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1448 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1449 			memcpy(eui, ifp->addr.s6_addr+8, 8);
1450 			err = 0;
1451 			break;
1452 		}
1453 	}
1454 	read_unlock_bh(&idev->lock);
1455 	return err;
1456 }
1457 
1458 #ifdef CONFIG_IPV6_PRIVACY
1459 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1460 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1461 {
1462 regen:
1463 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
1464 	idev->rndid[0] &= ~0x02;
1465 
1466 	/*
1467 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1468 	 * check if generated address is not inappropriate
1469 	 *
1470 	 *  - Reserved subnet anycast (RFC 2526)
1471 	 *	11111101 11....11 1xxxxxxx
1472 	 *  - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1473 	 *	00-00-5E-FE-xx-xx-xx-xx
1474 	 *  - value 0
1475 	 *  - XXX: already assigned to an address on the device
1476 	 */
1477 	if (idev->rndid[0] == 0xfd &&
1478 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1479 	    (idev->rndid[7]&0x80))
1480 		goto regen;
1481 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1482 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1483 			goto regen;
1484 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1485 			goto regen;
1486 	}
1487 
1488 	return 0;
1489 }
1490 
1491 static void ipv6_regen_rndid(unsigned long data)
1492 {
1493 	struct inet6_dev *idev = (struct inet6_dev *) data;
1494 	unsigned long expires;
1495 
1496 	rcu_read_lock_bh();
1497 	write_lock_bh(&idev->lock);
1498 
1499 	if (idev->dead)
1500 		goto out;
1501 
1502 	if (__ipv6_regen_rndid(idev) < 0)
1503 		goto out;
1504 
1505 	expires = jiffies +
1506 		idev->cnf.temp_prefered_lft * HZ -
1507 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1508 	if (time_before(expires, jiffies)) {
1509 		printk(KERN_WARNING
1510 			"ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1511 			idev->dev->name);
1512 		goto out;
1513 	}
1514 
1515 	if (!mod_timer(&idev->regen_timer, expires))
1516 		in6_dev_hold(idev);
1517 
1518 out:
1519 	write_unlock_bh(&idev->lock);
1520 	rcu_read_unlock_bh();
1521 	in6_dev_put(idev);
1522 }
1523 
1524 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1525 	int ret = 0;
1526 
1527 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1528 		ret = __ipv6_regen_rndid(idev);
1529 	return ret;
1530 }
1531 #endif
1532 
1533 /*
1534  *	Add prefix route.
1535  */
1536 
1537 static void
1538 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1539 		      unsigned long expires, u32 flags)
1540 {
1541 	struct fib6_config cfg = {
1542 		.fc_table = RT6_TABLE_PREFIX,
1543 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
1544 		.fc_ifindex = dev->ifindex,
1545 		.fc_expires = expires,
1546 		.fc_dst_len = plen,
1547 		.fc_flags = RTF_UP | flags,
1548 	};
1549 
1550 	ipv6_addr_copy(&cfg.fc_dst, pfx);
1551 
1552 	/* Prevent useless cloning on PtP SIT.
1553 	   This thing is done here expecting that the whole
1554 	   class of non-broadcast devices need not cloning.
1555 	 */
1556 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1557 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1558 		cfg.fc_flags |= RTF_NONEXTHOP;
1559 #endif
1560 
1561 	ip6_route_add(&cfg);
1562 }
1563 
1564 /* Create "default" multicast route to the interface */
1565 
1566 static void addrconf_add_mroute(struct net_device *dev)
1567 {
1568 	struct fib6_config cfg = {
1569 		.fc_table = RT6_TABLE_LOCAL,
1570 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
1571 		.fc_ifindex = dev->ifindex,
1572 		.fc_dst_len = 8,
1573 		.fc_flags = RTF_UP,
1574 	};
1575 
1576 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1577 
1578 	ip6_route_add(&cfg);
1579 }
1580 
1581 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1582 static void sit_route_add(struct net_device *dev)
1583 {
1584 	struct fib6_config cfg = {
1585 		.fc_table = RT6_TABLE_MAIN,
1586 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
1587 		.fc_ifindex = dev->ifindex,
1588 		.fc_dst_len = 96,
1589 		.fc_flags = RTF_UP | RTF_NONEXTHOP,
1590 	};
1591 
1592 	/* prefix length - 96 bits "::d.d.d.d" */
1593 	ip6_route_add(&cfg);
1594 }
1595 #endif
1596 
1597 static void addrconf_add_lroute(struct net_device *dev)
1598 {
1599 	struct in6_addr addr;
1600 
1601 	ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
1602 	addrconf_prefix_route(&addr, 64, dev, 0, 0);
1603 }
1604 
1605 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1606 {
1607 	struct inet6_dev *idev;
1608 
1609 	ASSERT_RTNL();
1610 
1611 	if ((idev = ipv6_find_idev(dev)) == NULL)
1612 		return NULL;
1613 
1614 	/* Add default multicast route */
1615 	addrconf_add_mroute(dev);
1616 
1617 	/* Add link local route */
1618 	addrconf_add_lroute(dev);
1619 	return idev;
1620 }
1621 
1622 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1623 {
1624 	struct prefix_info *pinfo;
1625 	__u32 valid_lft;
1626 	__u32 prefered_lft;
1627 	int addr_type;
1628 	unsigned long rt_expires;
1629 	struct inet6_dev *in6_dev;
1630 
1631 	pinfo = (struct prefix_info *) opt;
1632 
1633 	if (len < sizeof(struct prefix_info)) {
1634 		ADBG(("addrconf: prefix option too short\n"));
1635 		return;
1636 	}
1637 
1638 	/*
1639 	 *	Validation checks ([ADDRCONF], page 19)
1640 	 */
1641 
1642 	addr_type = ipv6_addr_type(&pinfo->prefix);
1643 
1644 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1645 		return;
1646 
1647 	valid_lft = ntohl(pinfo->valid);
1648 	prefered_lft = ntohl(pinfo->prefered);
1649 
1650 	if (prefered_lft > valid_lft) {
1651 		if (net_ratelimit())
1652 			printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1653 		return;
1654 	}
1655 
1656 	in6_dev = in6_dev_get(dev);
1657 
1658 	if (in6_dev == NULL) {
1659 		if (net_ratelimit())
1660 			printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1661 		return;
1662 	}
1663 
1664 	/*
1665 	 *	Two things going on here:
1666 	 *	1) Add routes for on-link prefixes
1667 	 *	2) Configure prefixes with the auto flag set
1668 	 */
1669 
1670 	/* Avoid arithmetic overflow. Really, we could
1671 	   save rt_expires in seconds, likely valid_lft,
1672 	   but it would require division in fib gc, that it
1673 	   not good.
1674 	 */
1675 	if (valid_lft >= 0x7FFFFFFF/HZ)
1676 		rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1677 	else
1678 		rt_expires = valid_lft * HZ;
1679 
1680 	/*
1681 	 * We convert this (in jiffies) to clock_t later.
1682 	 * Avoid arithmetic overflow there as well.
1683 	 * Overflow can happen only if HZ < USER_HZ.
1684 	 */
1685 	if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1686 		rt_expires = 0x7FFFFFFF / USER_HZ;
1687 
1688 	if (pinfo->onlink) {
1689 		struct rt6_info *rt;
1690 		rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1691 
1692 		if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1693 			if (rt->rt6i_flags&RTF_EXPIRES) {
1694 				if (valid_lft == 0) {
1695 					ip6_del_rt(rt);
1696 					rt = NULL;
1697 				} else {
1698 					rt->rt6i_expires = jiffies + rt_expires;
1699 				}
1700 			}
1701 		} else if (valid_lft) {
1702 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1703 					      dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1704 		}
1705 		if (rt)
1706 			dst_release(&rt->u.dst);
1707 	}
1708 
1709 	/* Try to figure out our local address for this prefix */
1710 
1711 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1712 		struct inet6_ifaddr * ifp;
1713 		struct in6_addr addr;
1714 		int create = 0, update_lft = 0;
1715 
1716 		if (pinfo->prefix_len == 64) {
1717 			memcpy(&addr, &pinfo->prefix, 8);
1718 			if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1719 			    ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1720 				in6_dev_put(in6_dev);
1721 				return;
1722 			}
1723 			goto ok;
1724 		}
1725 		if (net_ratelimit())
1726 			printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1727 			       pinfo->prefix_len);
1728 		in6_dev_put(in6_dev);
1729 		return;
1730 
1731 ok:
1732 
1733 		ifp = ipv6_get_ifaddr(&addr, dev, 1);
1734 
1735 		if (ifp == NULL && valid_lft) {
1736 			int max_addresses = in6_dev->cnf.max_addresses;
1737 
1738 			/* Do not allow to create too much of autoconfigured
1739 			 * addresses; this would be too easy way to crash kernel.
1740 			 */
1741 			if (!max_addresses ||
1742 			    ipv6_count_addresses(in6_dev) < max_addresses)
1743 				ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1744 						    addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1745 
1746 			if (!ifp || IS_ERR(ifp)) {
1747 				in6_dev_put(in6_dev);
1748 				return;
1749 			}
1750 
1751 			update_lft = create = 1;
1752 			ifp->cstamp = jiffies;
1753 			addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1754 		}
1755 
1756 		if (ifp) {
1757 			int flags;
1758 			unsigned long now;
1759 #ifdef CONFIG_IPV6_PRIVACY
1760 			struct inet6_ifaddr *ift;
1761 #endif
1762 			u32 stored_lft;
1763 
1764 			/* update lifetime (RFC2462 5.5.3 e) */
1765 			spin_lock(&ifp->lock);
1766 			now = jiffies;
1767 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1768 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1769 			else
1770 				stored_lft = 0;
1771 			if (!update_lft && stored_lft) {
1772 				if (valid_lft > MIN_VALID_LIFETIME ||
1773 				    valid_lft > stored_lft)
1774 					update_lft = 1;
1775 				else if (stored_lft <= MIN_VALID_LIFETIME) {
1776 					/* valid_lft <= stored_lft is always true */
1777 					/* XXX: IPsec */
1778 					update_lft = 0;
1779 				} else {
1780 					valid_lft = MIN_VALID_LIFETIME;
1781 					if (valid_lft < prefered_lft)
1782 						prefered_lft = valid_lft;
1783 					update_lft = 1;
1784 				}
1785 			}
1786 
1787 			if (update_lft) {
1788 				ifp->valid_lft = valid_lft;
1789 				ifp->prefered_lft = prefered_lft;
1790 				ifp->tstamp = now;
1791 				flags = ifp->flags;
1792 				ifp->flags &= ~IFA_F_DEPRECATED;
1793 				spin_unlock(&ifp->lock);
1794 
1795 				if (!(flags&IFA_F_TENTATIVE))
1796 					ipv6_ifa_notify(0, ifp);
1797 			} else
1798 				spin_unlock(&ifp->lock);
1799 
1800 #ifdef CONFIG_IPV6_PRIVACY
1801 			read_lock_bh(&in6_dev->lock);
1802 			/* update all temporary addresses in the list */
1803 			for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1804 				/*
1805 				 * When adjusting the lifetimes of an existing
1806 				 * temporary address, only lower the lifetimes.
1807 				 * Implementations must not increase the
1808 				 * lifetimes of an existing temporary address
1809 				 * when processing a Prefix Information Option.
1810 				 */
1811 				spin_lock(&ift->lock);
1812 				flags = ift->flags;
1813 				if (ift->valid_lft > valid_lft &&
1814 				    ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1815 					ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1816 				if (ift->prefered_lft > prefered_lft &&
1817 				    ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1818 					ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1819 				spin_unlock(&ift->lock);
1820 				if (!(flags&IFA_F_TENTATIVE))
1821 					ipv6_ifa_notify(0, ift);
1822 			}
1823 
1824 			if (create && in6_dev->cnf.use_tempaddr > 0) {
1825 				/*
1826 				 * When a new public address is created as described in [ADDRCONF],
1827 				 * also create a new temporary address.
1828 				 */
1829 				read_unlock_bh(&in6_dev->lock);
1830 				ipv6_create_tempaddr(ifp, NULL);
1831 			} else {
1832 				read_unlock_bh(&in6_dev->lock);
1833 			}
1834 #endif
1835 			in6_ifa_put(ifp);
1836 			addrconf_verify(0);
1837 		}
1838 	}
1839 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1840 	in6_dev_put(in6_dev);
1841 }
1842 
1843 /*
1844  *	Set destination address.
1845  *	Special case for SIT interfaces where we create a new "virtual"
1846  *	device.
1847  */
1848 int addrconf_set_dstaddr(void __user *arg)
1849 {
1850 	struct in6_ifreq ireq;
1851 	struct net_device *dev;
1852 	int err = -EINVAL;
1853 
1854 	rtnl_lock();
1855 
1856 	err = -EFAULT;
1857 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1858 		goto err_exit;
1859 
1860 	dev = __dev_get_by_index(ireq.ifr6_ifindex);
1861 
1862 	err = -ENODEV;
1863 	if (dev == NULL)
1864 		goto err_exit;
1865 
1866 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1867 	if (dev->type == ARPHRD_SIT) {
1868 		struct ifreq ifr;
1869 		mm_segment_t	oldfs;
1870 		struct ip_tunnel_parm p;
1871 
1872 		err = -EADDRNOTAVAIL;
1873 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1874 			goto err_exit;
1875 
1876 		memset(&p, 0, sizeof(p));
1877 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1878 		p.iph.saddr = 0;
1879 		p.iph.version = 4;
1880 		p.iph.ihl = 5;
1881 		p.iph.protocol = IPPROTO_IPV6;
1882 		p.iph.ttl = 64;
1883 		ifr.ifr_ifru.ifru_data = (void __user *)&p;
1884 
1885 		oldfs = get_fs(); set_fs(KERNEL_DS);
1886 		err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1887 		set_fs(oldfs);
1888 
1889 		if (err == 0) {
1890 			err = -ENOBUFS;
1891 			if ((dev = __dev_get_by_name(p.name)) == NULL)
1892 				goto err_exit;
1893 			err = dev_open(dev);
1894 		}
1895 	}
1896 #endif
1897 
1898 err_exit:
1899 	rtnl_unlock();
1900 	return err;
1901 }
1902 
1903 /*
1904  *	Manual configuration of address on an interface
1905  */
1906 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
1907 			  __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
1908 {
1909 	struct inet6_ifaddr *ifp;
1910 	struct inet6_dev *idev;
1911 	struct net_device *dev;
1912 	int scope;
1913 
1914 	ASSERT_RTNL();
1915 
1916 	/* check the lifetime */
1917 	if (!valid_lft || prefered_lft > valid_lft)
1918 		return -EINVAL;
1919 
1920 	if ((dev = __dev_get_by_index(ifindex)) == NULL)
1921 		return -ENODEV;
1922 
1923 	if ((idev = addrconf_add_dev(dev)) == NULL)
1924 		return -ENOBUFS;
1925 
1926 	scope = ipv6_addr_scope(pfx);
1927 
1928 	if (valid_lft == INFINITY_LIFE_TIME)
1929 		ifa_flags |= IFA_F_PERMANENT;
1930 	else if (valid_lft >= 0x7FFFFFFF/HZ)
1931 		valid_lft = 0x7FFFFFFF/HZ;
1932 
1933 	if (prefered_lft == 0)
1934 		ifa_flags |= IFA_F_DEPRECATED;
1935 	else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1936 		 (prefered_lft != INFINITY_LIFE_TIME))
1937 		prefered_lft = 0x7FFFFFFF/HZ;
1938 
1939 	ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1940 
1941 	if (!IS_ERR(ifp)) {
1942 		spin_lock_bh(&ifp->lock);
1943 		ifp->valid_lft = valid_lft;
1944 		ifp->prefered_lft = prefered_lft;
1945 		ifp->tstamp = jiffies;
1946 		spin_unlock_bh(&ifp->lock);
1947 
1948 		addrconf_dad_start(ifp, 0);
1949 		in6_ifa_put(ifp);
1950 		addrconf_verify(0);
1951 		return 0;
1952 	}
1953 
1954 	return PTR_ERR(ifp);
1955 }
1956 
1957 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1958 {
1959 	struct inet6_ifaddr *ifp;
1960 	struct inet6_dev *idev;
1961 	struct net_device *dev;
1962 
1963 	if ((dev = __dev_get_by_index(ifindex)) == NULL)
1964 		return -ENODEV;
1965 
1966 	if ((idev = __in6_dev_get(dev)) == NULL)
1967 		return -ENXIO;
1968 
1969 	read_lock_bh(&idev->lock);
1970 	for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1971 		if (ifp->prefix_len == plen &&
1972 		    ipv6_addr_equal(pfx, &ifp->addr)) {
1973 			in6_ifa_hold(ifp);
1974 			read_unlock_bh(&idev->lock);
1975 
1976 			ipv6_del_addr(ifp);
1977 
1978 			/* If the last address is deleted administratively,
1979 			   disable IPv6 on this interface.
1980 			 */
1981 			if (idev->addr_list == NULL)
1982 				addrconf_ifdown(idev->dev, 1);
1983 			return 0;
1984 		}
1985 	}
1986 	read_unlock_bh(&idev->lock);
1987 	return -EADDRNOTAVAIL;
1988 }
1989 
1990 
1991 int addrconf_add_ifaddr(void __user *arg)
1992 {
1993 	struct in6_ifreq ireq;
1994 	int err;
1995 
1996 	if (!capable(CAP_NET_ADMIN))
1997 		return -EPERM;
1998 
1999 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2000 		return -EFAULT;
2001 
2002 	rtnl_lock();
2003 	err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
2004 			     IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2005 	rtnl_unlock();
2006 	return err;
2007 }
2008 
2009 int addrconf_del_ifaddr(void __user *arg)
2010 {
2011 	struct in6_ifreq ireq;
2012 	int err;
2013 
2014 	if (!capable(CAP_NET_ADMIN))
2015 		return -EPERM;
2016 
2017 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2018 		return -EFAULT;
2019 
2020 	rtnl_lock();
2021 	err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
2022 	rtnl_unlock();
2023 	return err;
2024 }
2025 
2026 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2027 static void sit_add_v4_addrs(struct inet6_dev *idev)
2028 {
2029 	struct inet6_ifaddr * ifp;
2030 	struct in6_addr addr;
2031 	struct net_device *dev;
2032 	int scope;
2033 
2034 	ASSERT_RTNL();
2035 
2036 	memset(&addr, 0, sizeof(struct in6_addr));
2037 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2038 
2039 	if (idev->dev->flags&IFF_POINTOPOINT) {
2040 		addr.s6_addr32[0] = htonl(0xfe800000);
2041 		scope = IFA_LINK;
2042 	} else {
2043 		scope = IPV6_ADDR_COMPATv4;
2044 	}
2045 
2046 	if (addr.s6_addr32[3]) {
2047 		ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2048 		if (!IS_ERR(ifp)) {
2049 			spin_lock_bh(&ifp->lock);
2050 			ifp->flags &= ~IFA_F_TENTATIVE;
2051 			spin_unlock_bh(&ifp->lock);
2052 			ipv6_ifa_notify(RTM_NEWADDR, ifp);
2053 			in6_ifa_put(ifp);
2054 		}
2055 		return;
2056 	}
2057 
2058 	for (dev = dev_base; dev != NULL; dev = dev->next) {
2059 		struct in_device * in_dev = __in_dev_get_rtnl(dev);
2060 		if (in_dev && (dev->flags & IFF_UP)) {
2061 			struct in_ifaddr * ifa;
2062 
2063 			int flag = scope;
2064 
2065 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2066 				int plen;
2067 
2068 				addr.s6_addr32[3] = ifa->ifa_local;
2069 
2070 				if (ifa->ifa_scope == RT_SCOPE_LINK)
2071 					continue;
2072 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2073 					if (idev->dev->flags&IFF_POINTOPOINT)
2074 						continue;
2075 					flag |= IFA_HOST;
2076 				}
2077 				if (idev->dev->flags&IFF_POINTOPOINT)
2078 					plen = 64;
2079 				else
2080 					plen = 96;
2081 
2082 				ifp = ipv6_add_addr(idev, &addr, plen, flag,
2083 						    IFA_F_PERMANENT);
2084 				if (!IS_ERR(ifp)) {
2085 					spin_lock_bh(&ifp->lock);
2086 					ifp->flags &= ~IFA_F_TENTATIVE;
2087 					spin_unlock_bh(&ifp->lock);
2088 					ipv6_ifa_notify(RTM_NEWADDR, ifp);
2089 					in6_ifa_put(ifp);
2090 				}
2091 			}
2092 		}
2093 	}
2094 }
2095 #endif
2096 
2097 static void init_loopback(struct net_device *dev)
2098 {
2099 	struct inet6_dev  *idev;
2100 	struct inet6_ifaddr * ifp;
2101 
2102 	/* ::1 */
2103 
2104 	ASSERT_RTNL();
2105 
2106 	if ((idev = ipv6_find_idev(dev)) == NULL) {
2107 		printk(KERN_DEBUG "init loopback: add_dev failed\n");
2108 		return;
2109 	}
2110 
2111 	ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2112 	if (!IS_ERR(ifp)) {
2113 		spin_lock_bh(&ifp->lock);
2114 		ifp->flags &= ~IFA_F_TENTATIVE;
2115 		spin_unlock_bh(&ifp->lock);
2116 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2117 		in6_ifa_put(ifp);
2118 	}
2119 }
2120 
2121 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2122 {
2123 	struct inet6_ifaddr * ifp;
2124 
2125 	ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2126 	if (!IS_ERR(ifp)) {
2127 		addrconf_dad_start(ifp, 0);
2128 		in6_ifa_put(ifp);
2129 	}
2130 }
2131 
2132 static void addrconf_dev_config(struct net_device *dev)
2133 {
2134 	struct in6_addr addr;
2135 	struct inet6_dev    * idev;
2136 
2137 	ASSERT_RTNL();
2138 
2139 	if ((dev->type != ARPHRD_ETHER) &&
2140 	    (dev->type != ARPHRD_FDDI) &&
2141 	    (dev->type != ARPHRD_IEEE802_TR) &&
2142 	    (dev->type != ARPHRD_ARCNET) &&
2143 	    (dev->type != ARPHRD_INFINIBAND)) {
2144 		/* Alas, we support only Ethernet autoconfiguration. */
2145 		return;
2146 	}
2147 
2148 	idev = addrconf_add_dev(dev);
2149 	if (idev == NULL)
2150 		return;
2151 
2152 	memset(&addr, 0, sizeof(struct in6_addr));
2153 	addr.s6_addr32[0] = htonl(0xFE800000);
2154 
2155 	if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2156 		addrconf_add_linklocal(idev, &addr);
2157 }
2158 
2159 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2160 static void addrconf_sit_config(struct net_device *dev)
2161 {
2162 	struct inet6_dev *idev;
2163 
2164 	ASSERT_RTNL();
2165 
2166 	/*
2167 	 * Configure the tunnel with one of our IPv4
2168 	 * addresses... we should configure all of
2169 	 * our v4 addrs in the tunnel
2170 	 */
2171 
2172 	if ((idev = ipv6_find_idev(dev)) == NULL) {
2173 		printk(KERN_DEBUG "init sit: add_dev failed\n");
2174 		return;
2175 	}
2176 
2177 	sit_add_v4_addrs(idev);
2178 
2179 	if (dev->flags&IFF_POINTOPOINT) {
2180 		addrconf_add_mroute(dev);
2181 		addrconf_add_lroute(dev);
2182 	} else
2183 		sit_route_add(dev);
2184 }
2185 #endif
2186 
2187 static inline int
2188 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2189 {
2190 	struct in6_addr lladdr;
2191 
2192 	if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2193 		addrconf_add_linklocal(idev, &lladdr);
2194 		return 0;
2195 	}
2196 	return -1;
2197 }
2198 
2199 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2200 {
2201 	struct net_device *link_dev;
2202 
2203 	/* first try to inherit the link-local address from the link device */
2204 	if (idev->dev->iflink &&
2205 	    (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2206 		if (!ipv6_inherit_linklocal(idev, link_dev))
2207 			return;
2208 	}
2209 	/* then try to inherit it from any device */
2210 	for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2211 		if (!ipv6_inherit_linklocal(idev, link_dev))
2212 			return;
2213 	}
2214 	printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2215 }
2216 
2217 /*
2218  * Autoconfigure tunnel with a link-local address so routing protocols,
2219  * DHCPv6, MLD etc. can be run over the virtual link
2220  */
2221 
2222 static void addrconf_ip6_tnl_config(struct net_device *dev)
2223 {
2224 	struct inet6_dev *idev;
2225 
2226 	ASSERT_RTNL();
2227 
2228 	if ((idev = addrconf_add_dev(dev)) == NULL) {
2229 		printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2230 		return;
2231 	}
2232 	ip6_tnl_add_linklocal(idev);
2233 }
2234 
2235 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2236 			   void * data)
2237 {
2238 	struct net_device *dev = (struct net_device *) data;
2239 	struct inet6_dev *idev = __in6_dev_get(dev);
2240 	int run_pending = 0;
2241 
2242 	switch(event) {
2243 	case NETDEV_UP:
2244 	case NETDEV_CHANGE:
2245 		if (event == NETDEV_UP) {
2246 			if (!netif_carrier_ok(dev)) {
2247 				/* device is not ready yet. */
2248 				printk(KERN_INFO
2249 					"ADDRCONF(NETDEV_UP): %s: "
2250 					"link is not ready\n",
2251 					dev->name);
2252 				break;
2253 			}
2254 
2255 			if (idev)
2256 				idev->if_flags |= IF_READY;
2257 		} else {
2258 			if (!netif_carrier_ok(dev)) {
2259 				/* device is still not ready. */
2260 				break;
2261 			}
2262 
2263 			if (idev) {
2264 				if (idev->if_flags & IF_READY) {
2265 					/* device is already configured. */
2266 					break;
2267 				}
2268 				idev->if_flags |= IF_READY;
2269 			}
2270 
2271 			printk(KERN_INFO
2272 					"ADDRCONF(NETDEV_CHANGE): %s: "
2273 					"link becomes ready\n",
2274 					dev->name);
2275 
2276 			run_pending = 1;
2277 		}
2278 
2279 		switch(dev->type) {
2280 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2281 		case ARPHRD_SIT:
2282 			addrconf_sit_config(dev);
2283 			break;
2284 #endif
2285 		case ARPHRD_TUNNEL6:
2286 			addrconf_ip6_tnl_config(dev);
2287 			break;
2288 		case ARPHRD_LOOPBACK:
2289 			init_loopback(dev);
2290 			break;
2291 
2292 		default:
2293 			addrconf_dev_config(dev);
2294 			break;
2295 		};
2296 		if (idev) {
2297 			if (run_pending)
2298 				addrconf_dad_run(idev);
2299 
2300 			/* If the MTU changed during the interface down, when the
2301 			   interface up, the changed MTU must be reflected in the
2302 			   idev as well as routers.
2303 			 */
2304 			if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2305 				rt6_mtu_change(dev, dev->mtu);
2306 				idev->cnf.mtu6 = dev->mtu;
2307 			}
2308 			idev->tstamp = jiffies;
2309 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
2310 			/* If the changed mtu during down is lower than IPV6_MIN_MTU
2311 			   stop IPv6 on this interface.
2312 			 */
2313 			if (dev->mtu < IPV6_MIN_MTU)
2314 				addrconf_ifdown(dev, event != NETDEV_DOWN);
2315 		}
2316 		break;
2317 
2318 	case NETDEV_CHANGEMTU:
2319 		if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2320 			rt6_mtu_change(dev, dev->mtu);
2321 			idev->cnf.mtu6 = dev->mtu;
2322 			break;
2323 		}
2324 
2325 		/* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2326 
2327 	case NETDEV_DOWN:
2328 	case NETDEV_UNREGISTER:
2329 		/*
2330 		 *	Remove all addresses from this interface.
2331 		 */
2332 		addrconf_ifdown(dev, event != NETDEV_DOWN);
2333 		break;
2334 
2335 	case NETDEV_CHANGENAME:
2336 #ifdef CONFIG_SYSCTL
2337 		if (idev) {
2338 			addrconf_sysctl_unregister(&idev->cnf);
2339 			neigh_sysctl_unregister(idev->nd_parms);
2340 			neigh_sysctl_register(dev, idev->nd_parms,
2341 					      NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2342 					      &ndisc_ifinfo_sysctl_change,
2343 					      NULL);
2344 			addrconf_sysctl_register(idev, &idev->cnf);
2345 		}
2346 #endif
2347 		break;
2348 	};
2349 
2350 	return NOTIFY_OK;
2351 }
2352 
2353 /*
2354  *	addrconf module should be notified of a device going up
2355  */
2356 static struct notifier_block ipv6_dev_notf = {
2357 	.notifier_call = addrconf_notify,
2358 	.priority = 0
2359 };
2360 
2361 static int addrconf_ifdown(struct net_device *dev, int how)
2362 {
2363 	struct inet6_dev *idev;
2364 	struct inet6_ifaddr *ifa, **bifa;
2365 	int i;
2366 
2367 	ASSERT_RTNL();
2368 
2369 	if (dev == &loopback_dev && how == 1)
2370 		how = 0;
2371 
2372 	rt6_ifdown(dev);
2373 	neigh_ifdown(&nd_tbl, dev);
2374 
2375 	idev = __in6_dev_get(dev);
2376 	if (idev == NULL)
2377 		return -ENODEV;
2378 
2379 	/* Step 1: remove reference to ipv6 device from parent device.
2380 		   Do not dev_put!
2381 	 */
2382 	if (how == 1) {
2383 		idev->dead = 1;
2384 
2385 		/* protected by rtnl_lock */
2386 		rcu_assign_pointer(dev->ip6_ptr, NULL);
2387 
2388 		/* Step 1.5: remove snmp6 entry */
2389 		snmp6_unregister_dev(idev);
2390 
2391 	}
2392 
2393 	/* Step 2: clear hash table */
2394 	for (i=0; i<IN6_ADDR_HSIZE; i++) {
2395 		bifa = &inet6_addr_lst[i];
2396 
2397 		write_lock_bh(&addrconf_hash_lock);
2398 		while ((ifa = *bifa) != NULL) {
2399 			if (ifa->idev == idev) {
2400 				*bifa = ifa->lst_next;
2401 				ifa->lst_next = NULL;
2402 				addrconf_del_timer(ifa);
2403 				in6_ifa_put(ifa);
2404 				continue;
2405 			}
2406 			bifa = &ifa->lst_next;
2407 		}
2408 		write_unlock_bh(&addrconf_hash_lock);
2409 	}
2410 
2411 	write_lock_bh(&idev->lock);
2412 
2413 	/* Step 3: clear flags for stateless addrconf */
2414 	if (how != 1)
2415 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2416 
2417 	/* Step 4: clear address list */
2418 #ifdef CONFIG_IPV6_PRIVACY
2419 	if (how == 1 && del_timer(&idev->regen_timer))
2420 		in6_dev_put(idev);
2421 
2422 	/* clear tempaddr list */
2423 	while ((ifa = idev->tempaddr_list) != NULL) {
2424 		idev->tempaddr_list = ifa->tmp_next;
2425 		ifa->tmp_next = NULL;
2426 		ifa->dead = 1;
2427 		write_unlock_bh(&idev->lock);
2428 		spin_lock_bh(&ifa->lock);
2429 
2430 		if (ifa->ifpub) {
2431 			in6_ifa_put(ifa->ifpub);
2432 			ifa->ifpub = NULL;
2433 		}
2434 		spin_unlock_bh(&ifa->lock);
2435 		in6_ifa_put(ifa);
2436 		write_lock_bh(&idev->lock);
2437 	}
2438 #endif
2439 	while ((ifa = idev->addr_list) != NULL) {
2440 		idev->addr_list = ifa->if_next;
2441 		ifa->if_next = NULL;
2442 		ifa->dead = 1;
2443 		addrconf_del_timer(ifa);
2444 		write_unlock_bh(&idev->lock);
2445 
2446 		__ipv6_ifa_notify(RTM_DELADDR, ifa);
2447 		in6_ifa_put(ifa);
2448 
2449 		write_lock_bh(&idev->lock);
2450 	}
2451 	write_unlock_bh(&idev->lock);
2452 
2453 	/* Step 5: Discard multicast list */
2454 
2455 	if (how == 1)
2456 		ipv6_mc_destroy_dev(idev);
2457 	else
2458 		ipv6_mc_down(idev);
2459 
2460 	/* Step 5: netlink notification of this interface */
2461 	idev->tstamp = jiffies;
2462 	inet6_ifinfo_notify(RTM_DELLINK, idev);
2463 
2464 	/* Shot the device (if unregistered) */
2465 
2466 	if (how == 1) {
2467 #ifdef CONFIG_SYSCTL
2468 		addrconf_sysctl_unregister(&idev->cnf);
2469 		neigh_sysctl_unregister(idev->nd_parms);
2470 #endif
2471 		neigh_parms_release(&nd_tbl, idev->nd_parms);
2472 		neigh_ifdown(&nd_tbl, dev);
2473 		in6_dev_put(idev);
2474 	}
2475 	return 0;
2476 }
2477 
2478 static void addrconf_rs_timer(unsigned long data)
2479 {
2480 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2481 
2482 	if (ifp->idev->cnf.forwarding)
2483 		goto out;
2484 
2485 	if (ifp->idev->if_flags & IF_RA_RCVD) {
2486 		/*
2487 		 *	Announcement received after solicitation
2488 		 *	was sent
2489 		 */
2490 		goto out;
2491 	}
2492 
2493 	spin_lock(&ifp->lock);
2494 	if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2495 		struct in6_addr all_routers;
2496 
2497 		/* The wait after the last probe can be shorter */
2498 		addrconf_mod_timer(ifp, AC_RS,
2499 				   (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2500 				   ifp->idev->cnf.rtr_solicit_delay :
2501 				   ifp->idev->cnf.rtr_solicit_interval);
2502 		spin_unlock(&ifp->lock);
2503 
2504 		ipv6_addr_all_routers(&all_routers);
2505 
2506 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2507 	} else {
2508 		spin_unlock(&ifp->lock);
2509 		/*
2510 		 * Note: we do not support deprecated "all on-link"
2511 		 * assumption any longer.
2512 		 */
2513 		printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2514 		       ifp->idev->dev->name);
2515 	}
2516 
2517 out:
2518 	in6_ifa_put(ifp);
2519 }
2520 
2521 /*
2522  *	Duplicate Address Detection
2523  */
2524 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2525 {
2526 	unsigned long rand_num;
2527 	struct inet6_dev *idev = ifp->idev;
2528 
2529 	rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2530 	ifp->probes = idev->cnf.dad_transmits;
2531 	addrconf_mod_timer(ifp, AC_DAD, rand_num);
2532 }
2533 
2534 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2535 {
2536 	struct inet6_dev *idev = ifp->idev;
2537 	struct net_device *dev = idev->dev;
2538 
2539 	addrconf_join_solict(dev, &ifp->addr);
2540 
2541 	if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2542 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2543 					flags);
2544 
2545 	net_srandom(ifp->addr.s6_addr32[3]);
2546 
2547 	read_lock_bh(&idev->lock);
2548 	if (ifp->dead)
2549 		goto out;
2550 	spin_lock_bh(&ifp->lock);
2551 
2552 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2553 	    !(ifp->flags&IFA_F_TENTATIVE) ||
2554 	    ifp->flags & IFA_F_NODAD) {
2555 		ifp->flags &= ~IFA_F_TENTATIVE;
2556 		spin_unlock_bh(&ifp->lock);
2557 		read_unlock_bh(&idev->lock);
2558 
2559 		addrconf_dad_completed(ifp);
2560 		return;
2561 	}
2562 
2563 	if (!(idev->if_flags & IF_READY)) {
2564 		spin_unlock_bh(&ifp->lock);
2565 		read_unlock_bh(&idev->lock);
2566 		/*
2567 		 * If the defice is not ready:
2568 		 * - keep it tentative if it is a permanent address.
2569 		 * - otherwise, kill it.
2570 		 */
2571 		in6_ifa_hold(ifp);
2572 		addrconf_dad_stop(ifp);
2573 		return;
2574 	}
2575 	addrconf_dad_kick(ifp);
2576 	spin_unlock_bh(&ifp->lock);
2577 out:
2578 	read_unlock_bh(&idev->lock);
2579 }
2580 
2581 static void addrconf_dad_timer(unsigned long data)
2582 {
2583 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2584 	struct inet6_dev *idev = ifp->idev;
2585 	struct in6_addr unspec;
2586 	struct in6_addr mcaddr;
2587 
2588 	read_lock_bh(&idev->lock);
2589 	if (idev->dead) {
2590 		read_unlock_bh(&idev->lock);
2591 		goto out;
2592 	}
2593 	spin_lock_bh(&ifp->lock);
2594 	if (ifp->probes == 0) {
2595 		/*
2596 		 * DAD was successful
2597 		 */
2598 
2599 		ifp->flags &= ~IFA_F_TENTATIVE;
2600 		spin_unlock_bh(&ifp->lock);
2601 		read_unlock_bh(&idev->lock);
2602 
2603 		addrconf_dad_completed(ifp);
2604 
2605 		goto out;
2606 	}
2607 
2608 	ifp->probes--;
2609 	addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2610 	spin_unlock_bh(&ifp->lock);
2611 	read_unlock_bh(&idev->lock);
2612 
2613 	/* send a neighbour solicitation for our addr */
2614 	memset(&unspec, 0, sizeof(unspec));
2615 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2616 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2617 out:
2618 	in6_ifa_put(ifp);
2619 }
2620 
2621 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2622 {
2623 	struct net_device *	dev = ifp->idev->dev;
2624 
2625 	/*
2626 	 *	Configure the address for reception. Now it is valid.
2627 	 */
2628 
2629 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
2630 
2631 	/* If added prefix is link local and forwarding is off,
2632 	   start sending router solicitations.
2633 	 */
2634 
2635 	if (ifp->idev->cnf.forwarding == 0 &&
2636 	    ifp->idev->cnf.rtr_solicits > 0 &&
2637 	    (dev->flags&IFF_LOOPBACK) == 0 &&
2638 	    (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2639 		struct in6_addr all_routers;
2640 
2641 		ipv6_addr_all_routers(&all_routers);
2642 
2643 		/*
2644 		 *	If a host as already performed a random delay
2645 		 *	[...] as part of DAD [...] there is no need
2646 		 *	to delay again before sending the first RS
2647 		 */
2648 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2649 
2650 		spin_lock_bh(&ifp->lock);
2651 		ifp->probes = 1;
2652 		ifp->idev->if_flags |= IF_RS_SENT;
2653 		addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2654 		spin_unlock_bh(&ifp->lock);
2655 	}
2656 }
2657 
2658 static void addrconf_dad_run(struct inet6_dev *idev) {
2659 	struct inet6_ifaddr *ifp;
2660 
2661 	read_lock_bh(&idev->lock);
2662 	for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2663 		spin_lock_bh(&ifp->lock);
2664 		if (!(ifp->flags & IFA_F_TENTATIVE)) {
2665 			spin_unlock_bh(&ifp->lock);
2666 			continue;
2667 		}
2668 		spin_unlock_bh(&ifp->lock);
2669 		addrconf_dad_kick(ifp);
2670 	}
2671 	read_unlock_bh(&idev->lock);
2672 }
2673 
2674 #ifdef CONFIG_PROC_FS
2675 struct if6_iter_state {
2676 	int bucket;
2677 };
2678 
2679 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2680 {
2681 	struct inet6_ifaddr *ifa = NULL;
2682 	struct if6_iter_state *state = seq->private;
2683 
2684 	for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2685 		ifa = inet6_addr_lst[state->bucket];
2686 		if (ifa)
2687 			break;
2688 	}
2689 	return ifa;
2690 }
2691 
2692 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2693 {
2694 	struct if6_iter_state *state = seq->private;
2695 
2696 	ifa = ifa->lst_next;
2697 try_again:
2698 	if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2699 		ifa = inet6_addr_lst[state->bucket];
2700 		goto try_again;
2701 	}
2702 	return ifa;
2703 }
2704 
2705 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2706 {
2707 	struct inet6_ifaddr *ifa = if6_get_first(seq);
2708 
2709 	if (ifa)
2710 		while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2711 			--pos;
2712 	return pos ? NULL : ifa;
2713 }
2714 
2715 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2716 {
2717 	read_lock_bh(&addrconf_hash_lock);
2718 	return if6_get_idx(seq, *pos);
2719 }
2720 
2721 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2722 {
2723 	struct inet6_ifaddr *ifa;
2724 
2725 	ifa = if6_get_next(seq, v);
2726 	++*pos;
2727 	return ifa;
2728 }
2729 
2730 static void if6_seq_stop(struct seq_file *seq, void *v)
2731 {
2732 	read_unlock_bh(&addrconf_hash_lock);
2733 }
2734 
2735 static int if6_seq_show(struct seq_file *seq, void *v)
2736 {
2737 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2738 	seq_printf(seq,
2739 		   NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2740 		   NIP6(ifp->addr),
2741 		   ifp->idev->dev->ifindex,
2742 		   ifp->prefix_len,
2743 		   ifp->scope,
2744 		   ifp->flags,
2745 		   ifp->idev->dev->name);
2746 	return 0;
2747 }
2748 
2749 static struct seq_operations if6_seq_ops = {
2750 	.start	= if6_seq_start,
2751 	.next	= if6_seq_next,
2752 	.show	= if6_seq_show,
2753 	.stop	= if6_seq_stop,
2754 };
2755 
2756 static int if6_seq_open(struct inode *inode, struct file *file)
2757 {
2758 	struct seq_file *seq;
2759 	int rc = -ENOMEM;
2760 	struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2761 
2762 	if (!s)
2763 		goto out;
2764 
2765 	rc = seq_open(file, &if6_seq_ops);
2766 	if (rc)
2767 		goto out_kfree;
2768 
2769 	seq = file->private_data;
2770 	seq->private = s;
2771 out:
2772 	return rc;
2773 out_kfree:
2774 	kfree(s);
2775 	goto out;
2776 }
2777 
2778 static const struct file_operations if6_fops = {
2779 	.owner		= THIS_MODULE,
2780 	.open		= if6_seq_open,
2781 	.read		= seq_read,
2782 	.llseek		= seq_lseek,
2783 	.release	= seq_release_private,
2784 };
2785 
2786 int __init if6_proc_init(void)
2787 {
2788 	if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2789 		return -ENOMEM;
2790 	return 0;
2791 }
2792 
2793 void if6_proc_exit(void)
2794 {
2795 	proc_net_remove("if_inet6");
2796 }
2797 #endif	/* CONFIG_PROC_FS */
2798 
2799 #ifdef CONFIG_IPV6_MIP6
2800 /* Check if address is a home address configured on any interface. */
2801 int ipv6_chk_home_addr(struct in6_addr *addr)
2802 {
2803 	int ret = 0;
2804 	struct inet6_ifaddr * ifp;
2805 	u8 hash = ipv6_addr_hash(addr);
2806 	read_lock_bh(&addrconf_hash_lock);
2807 	for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2808 		if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2809 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
2810 			ret = 1;
2811 			break;
2812 		}
2813 	}
2814 	read_unlock_bh(&addrconf_hash_lock);
2815 	return ret;
2816 }
2817 #endif
2818 
2819 /*
2820  *	Periodic address status verification
2821  */
2822 
2823 static void addrconf_verify(unsigned long foo)
2824 {
2825 	struct inet6_ifaddr *ifp;
2826 	unsigned long now, next;
2827 	int i;
2828 
2829 	spin_lock_bh(&addrconf_verify_lock);
2830 	now = jiffies;
2831 	next = now + ADDR_CHECK_FREQUENCY;
2832 
2833 	del_timer(&addr_chk_timer);
2834 
2835 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
2836 
2837 restart:
2838 		read_lock(&addrconf_hash_lock);
2839 		for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2840 			unsigned long age;
2841 #ifdef CONFIG_IPV6_PRIVACY
2842 			unsigned long regen_advance;
2843 #endif
2844 
2845 			if (ifp->flags & IFA_F_PERMANENT)
2846 				continue;
2847 
2848 			spin_lock(&ifp->lock);
2849 			age = (now - ifp->tstamp) / HZ;
2850 
2851 #ifdef CONFIG_IPV6_PRIVACY
2852 			regen_advance = ifp->idev->cnf.regen_max_retry *
2853 					ifp->idev->cnf.dad_transmits *
2854 					ifp->idev->nd_parms->retrans_time / HZ;
2855 #endif
2856 
2857 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2858 			    age >= ifp->valid_lft) {
2859 				spin_unlock(&ifp->lock);
2860 				in6_ifa_hold(ifp);
2861 				read_unlock(&addrconf_hash_lock);
2862 				ipv6_del_addr(ifp);
2863 				goto restart;
2864 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2865 				spin_unlock(&ifp->lock);
2866 				continue;
2867 			} else if (age >= ifp->prefered_lft) {
2868 				/* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2869 				int deprecate = 0;
2870 
2871 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
2872 					deprecate = 1;
2873 					ifp->flags |= IFA_F_DEPRECATED;
2874 				}
2875 
2876 				if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2877 					next = ifp->tstamp + ifp->valid_lft * HZ;
2878 
2879 				spin_unlock(&ifp->lock);
2880 
2881 				if (deprecate) {
2882 					in6_ifa_hold(ifp);
2883 					read_unlock(&addrconf_hash_lock);
2884 
2885 					ipv6_ifa_notify(0, ifp);
2886 					in6_ifa_put(ifp);
2887 					goto restart;
2888 				}
2889 #ifdef CONFIG_IPV6_PRIVACY
2890 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
2891 				   !(ifp->flags&IFA_F_TENTATIVE)) {
2892 				if (age >= ifp->prefered_lft - regen_advance) {
2893 					struct inet6_ifaddr *ifpub = ifp->ifpub;
2894 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2895 						next = ifp->tstamp + ifp->prefered_lft * HZ;
2896 					if (!ifp->regen_count && ifpub) {
2897 						ifp->regen_count++;
2898 						in6_ifa_hold(ifp);
2899 						in6_ifa_hold(ifpub);
2900 						spin_unlock(&ifp->lock);
2901 						read_unlock(&addrconf_hash_lock);
2902 						spin_lock(&ifpub->lock);
2903 						ifpub->regen_count = 0;
2904 						spin_unlock(&ifpub->lock);
2905 						ipv6_create_tempaddr(ifpub, ifp);
2906 						in6_ifa_put(ifpub);
2907 						in6_ifa_put(ifp);
2908 						goto restart;
2909 					}
2910 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2911 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2912 				spin_unlock(&ifp->lock);
2913 #endif
2914 			} else {
2915 				/* ifp->prefered_lft <= ifp->valid_lft */
2916 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2917 					next = ifp->tstamp + ifp->prefered_lft * HZ;
2918 				spin_unlock(&ifp->lock);
2919 			}
2920 		}
2921 		read_unlock(&addrconf_hash_lock);
2922 	}
2923 
2924 	addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2925 	add_timer(&addr_chk_timer);
2926 	spin_unlock_bh(&addrconf_verify_lock);
2927 }
2928 
2929 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2930 {
2931 	struct in6_addr *pfx = NULL;
2932 
2933 	if (addr)
2934 		pfx = nla_data(addr);
2935 
2936 	if (local) {
2937 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2938 			pfx = NULL;
2939 		else
2940 			pfx = nla_data(local);
2941 	}
2942 
2943 	return pfx;
2944 }
2945 
2946 static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = {
2947 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
2948 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
2949 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
2950 };
2951 
2952 static int
2953 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2954 {
2955 	struct ifaddrmsg *ifm;
2956 	struct nlattr *tb[IFA_MAX+1];
2957 	struct in6_addr *pfx;
2958 	int err;
2959 
2960 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
2961 	if (err < 0)
2962 		return err;
2963 
2964 	ifm = nlmsg_data(nlh);
2965 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
2966 	if (pfx == NULL)
2967 		return -EINVAL;
2968 
2969 	return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2970 }
2971 
2972 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
2973 			     u32 prefered_lft, u32 valid_lft)
2974 {
2975 	if (!valid_lft || (prefered_lft > valid_lft))
2976 		return -EINVAL;
2977 
2978 	if (valid_lft == INFINITY_LIFE_TIME)
2979 		ifa_flags |= IFA_F_PERMANENT;
2980 	else if (valid_lft >= 0x7FFFFFFF/HZ)
2981 		valid_lft = 0x7FFFFFFF/HZ;
2982 
2983 	if (prefered_lft == 0)
2984 		ifa_flags |= IFA_F_DEPRECATED;
2985 	else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
2986 		 (prefered_lft != INFINITY_LIFE_TIME))
2987 		prefered_lft = 0x7FFFFFFF/HZ;
2988 
2989 	spin_lock_bh(&ifp->lock);
2990 	ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
2991 	ifp->tstamp = jiffies;
2992 	ifp->valid_lft = valid_lft;
2993 	ifp->prefered_lft = prefered_lft;
2994 
2995 	spin_unlock_bh(&ifp->lock);
2996 	if (!(ifp->flags&IFA_F_TENTATIVE))
2997 		ipv6_ifa_notify(0, ifp);
2998 
2999 	addrconf_verify(0);
3000 
3001 	return 0;
3002 }
3003 
3004 static int
3005 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3006 {
3007 	struct ifaddrmsg *ifm;
3008 	struct nlattr *tb[IFA_MAX+1];
3009 	struct in6_addr *pfx;
3010 	struct inet6_ifaddr *ifa;
3011 	struct net_device *dev;
3012 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3013 	u8 ifa_flags;
3014 	int err;
3015 
3016 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3017 	if (err < 0)
3018 		return err;
3019 
3020 	ifm = nlmsg_data(nlh);
3021 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3022 	if (pfx == NULL)
3023 		return -EINVAL;
3024 
3025 	if (tb[IFA_CACHEINFO]) {
3026 		struct ifa_cacheinfo *ci;
3027 
3028 		ci = nla_data(tb[IFA_CACHEINFO]);
3029 		valid_lft = ci->ifa_valid;
3030 		preferred_lft = ci->ifa_prefered;
3031 	} else {
3032 		preferred_lft = INFINITY_LIFE_TIME;
3033 		valid_lft = INFINITY_LIFE_TIME;
3034 	}
3035 
3036 	dev =  __dev_get_by_index(ifm->ifa_index);
3037 	if (dev == NULL)
3038 		return -ENODEV;
3039 
3040 	/* We ignore other flags so far. */
3041 	ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
3042 
3043 	ifa = ipv6_get_ifaddr(pfx, dev, 1);
3044 	if (ifa == NULL) {
3045 		/*
3046 		 * It would be best to check for !NLM_F_CREATE here but
3047 		 * userspace alreay relies on not having to provide this.
3048 		 */
3049 		return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
3050 				      ifa_flags, preferred_lft, valid_lft);
3051 	}
3052 
3053 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
3054 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
3055 		err = -EEXIST;
3056 	else
3057 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
3058 
3059 	in6_ifa_put(ifa);
3060 
3061 	return err;
3062 }
3063 
3064 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3065 			  u8 scope, int ifindex)
3066 {
3067 	struct ifaddrmsg *ifm;
3068 
3069 	ifm = nlmsg_data(nlh);
3070 	ifm->ifa_family = AF_INET6;
3071 	ifm->ifa_prefixlen = prefixlen;
3072 	ifm->ifa_flags = flags;
3073 	ifm->ifa_scope = scope;
3074 	ifm->ifa_index = ifindex;
3075 }
3076 
3077 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3078 			 unsigned long tstamp, u32 preferred, u32 valid)
3079 {
3080 	struct ifa_cacheinfo ci;
3081 
3082 	ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3083 			+ TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3084 	ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3085 			+ TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3086 	ci.ifa_prefered = preferred;
3087 	ci.ifa_valid = valid;
3088 
3089 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3090 }
3091 
3092 static inline int rt_scope(int ifa_scope)
3093 {
3094 	if (ifa_scope & IFA_HOST)
3095 		return RT_SCOPE_HOST;
3096 	else if (ifa_scope & IFA_LINK)
3097 		return RT_SCOPE_LINK;
3098 	else if (ifa_scope & IFA_SITE)
3099 		return RT_SCOPE_SITE;
3100 	else
3101 		return RT_SCOPE_UNIVERSE;
3102 }
3103 
3104 static inline int inet6_ifaddr_msgsize(void)
3105 {
3106 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3107 	       + nla_total_size(16) /* IFA_ADDRESS */
3108 	       + nla_total_size(sizeof(struct ifa_cacheinfo));
3109 }
3110 
3111 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3112 			     u32 pid, u32 seq, int event, unsigned int flags)
3113 {
3114 	struct nlmsghdr  *nlh;
3115 	u32 preferred, valid;
3116 
3117 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3118 	if (nlh == NULL)
3119 		return -EMSGSIZE;
3120 
3121 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3122 		      ifa->idev->dev->ifindex);
3123 
3124 	if (!(ifa->flags&IFA_F_PERMANENT)) {
3125 		preferred = ifa->prefered_lft;
3126 		valid = ifa->valid_lft;
3127 		if (preferred != INFINITY_LIFE_TIME) {
3128 			long tval = (jiffies - ifa->tstamp)/HZ;
3129 			preferred -= tval;
3130 			if (valid != INFINITY_LIFE_TIME)
3131 				valid -= tval;
3132 		}
3133 	} else {
3134 		preferred = INFINITY_LIFE_TIME;
3135 		valid = INFINITY_LIFE_TIME;
3136 	}
3137 
3138 	if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
3139 	    put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3140 		nlmsg_cancel(skb, nlh);
3141 		return -EMSGSIZE;
3142 	}
3143 
3144 	return nlmsg_end(skb, nlh);
3145 }
3146 
3147 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3148 				u32 pid, u32 seq, int event, u16 flags)
3149 {
3150 	struct nlmsghdr  *nlh;
3151 	u8 scope = RT_SCOPE_UNIVERSE;
3152 	int ifindex = ifmca->idev->dev->ifindex;
3153 
3154 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3155 		scope = RT_SCOPE_SITE;
3156 
3157 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3158 	if (nlh == NULL)
3159 		return -EMSGSIZE;
3160 
3161 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3162 	if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3163 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
3164 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3165 		nlmsg_cancel(skb, nlh);
3166 		return -EMSGSIZE;
3167 	}
3168 
3169 	return nlmsg_end(skb, nlh);
3170 }
3171 
3172 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3173 				u32 pid, u32 seq, int event, unsigned int flags)
3174 {
3175 	struct nlmsghdr  *nlh;
3176 	u8 scope = RT_SCOPE_UNIVERSE;
3177 	int ifindex = ifaca->aca_idev->dev->ifindex;
3178 
3179 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3180 		scope = RT_SCOPE_SITE;
3181 
3182 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3183 	if (nlh == NULL)
3184 		return -EMSGSIZE;
3185 
3186 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3187 	if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3188 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
3189 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3190 		nlmsg_cancel(skb, nlh);
3191 		return -EMSGSIZE;
3192 	}
3193 
3194 	return nlmsg_end(skb, nlh);
3195 }
3196 
3197 enum addr_type_t
3198 {
3199 	UNICAST_ADDR,
3200 	MULTICAST_ADDR,
3201 	ANYCAST_ADDR,
3202 };
3203 
3204 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3205 			   enum addr_type_t type)
3206 {
3207 	int idx, ip_idx;
3208 	int s_idx, s_ip_idx;
3209 	int err = 1;
3210 	struct net_device *dev;
3211 	struct inet6_dev *idev = NULL;
3212 	struct inet6_ifaddr *ifa;
3213 	struct ifmcaddr6 *ifmca;
3214 	struct ifacaddr6 *ifaca;
3215 
3216 	s_idx = cb->args[0];
3217 	s_ip_idx = ip_idx = cb->args[1];
3218 	read_lock(&dev_base_lock);
3219 
3220 	for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3221 		if (idx < s_idx)
3222 			continue;
3223 		if (idx > s_idx)
3224 			s_ip_idx = 0;
3225 		ip_idx = 0;
3226 		if ((idev = in6_dev_get(dev)) == NULL)
3227 			continue;
3228 		read_lock_bh(&idev->lock);
3229 		switch (type) {
3230 		case UNICAST_ADDR:
3231 			/* unicast address incl. temp addr */
3232 			for (ifa = idev->addr_list; ifa;
3233 			     ifa = ifa->if_next, ip_idx++) {
3234 				if (ip_idx < s_ip_idx)
3235 					continue;
3236 				if ((err = inet6_fill_ifaddr(skb, ifa,
3237 				    NETLINK_CB(cb->skb).pid,
3238 				    cb->nlh->nlmsg_seq, RTM_NEWADDR,
3239 				    NLM_F_MULTI)) <= 0)
3240 					goto done;
3241 			}
3242 			break;
3243 		case MULTICAST_ADDR:
3244 			/* multicast address */
3245 			for (ifmca = idev->mc_list; ifmca;
3246 			     ifmca = ifmca->next, ip_idx++) {
3247 				if (ip_idx < s_ip_idx)
3248 					continue;
3249 				if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3250 				    NETLINK_CB(cb->skb).pid,
3251 				    cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3252 				    NLM_F_MULTI)) <= 0)
3253 					goto done;
3254 			}
3255 			break;
3256 		case ANYCAST_ADDR:
3257 			/* anycast address */
3258 			for (ifaca = idev->ac_list; ifaca;
3259 			     ifaca = ifaca->aca_next, ip_idx++) {
3260 				if (ip_idx < s_ip_idx)
3261 					continue;
3262 				if ((err = inet6_fill_ifacaddr(skb, ifaca,
3263 				    NETLINK_CB(cb->skb).pid,
3264 				    cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3265 				    NLM_F_MULTI)) <= 0)
3266 					goto done;
3267 			}
3268 			break;
3269 		default:
3270 			break;
3271 		}
3272 		read_unlock_bh(&idev->lock);
3273 		in6_dev_put(idev);
3274 	}
3275 done:
3276 	if (err <= 0) {
3277 		read_unlock_bh(&idev->lock);
3278 		in6_dev_put(idev);
3279 	}
3280 	read_unlock(&dev_base_lock);
3281 	cb->args[0] = idx;
3282 	cb->args[1] = ip_idx;
3283 	return skb->len;
3284 }
3285 
3286 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3287 {
3288 	enum addr_type_t type = UNICAST_ADDR;
3289 	return inet6_dump_addr(skb, cb, type);
3290 }
3291 
3292 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3293 {
3294 	enum addr_type_t type = MULTICAST_ADDR;
3295 	return inet6_dump_addr(skb, cb, type);
3296 }
3297 
3298 
3299 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3300 {
3301 	enum addr_type_t type = ANYCAST_ADDR;
3302 	return inet6_dump_addr(skb, cb, type);
3303 }
3304 
3305 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3306 			     void *arg)
3307 {
3308 	struct ifaddrmsg *ifm;
3309 	struct nlattr *tb[IFA_MAX+1];
3310 	struct in6_addr *addr = NULL;
3311 	struct net_device *dev = NULL;
3312 	struct inet6_ifaddr *ifa;
3313 	struct sk_buff *skb;
3314 	int err;
3315 
3316 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3317 	if (err < 0)
3318 		goto errout;
3319 
3320 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3321 	if (addr == NULL) {
3322 		err = -EINVAL;
3323 		goto errout;
3324 	}
3325 
3326 	ifm = nlmsg_data(nlh);
3327 	if (ifm->ifa_index)
3328 		dev = __dev_get_by_index(ifm->ifa_index);
3329 
3330 	if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3331 		err = -EADDRNOTAVAIL;
3332 		goto errout;
3333 	}
3334 
3335 	if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
3336 		err = -ENOBUFS;
3337 		goto errout_ifa;
3338 	}
3339 
3340 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3341 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
3342 	if (err < 0) {
3343 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3344 		WARN_ON(err == -EMSGSIZE);
3345 		kfree_skb(skb);
3346 		goto errout_ifa;
3347 	}
3348 	err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
3349 errout_ifa:
3350 	in6_ifa_put(ifa);
3351 errout:
3352 	return err;
3353 }
3354 
3355 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3356 {
3357 	struct sk_buff *skb;
3358 	int err = -ENOBUFS;
3359 
3360 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
3361 	if (skb == NULL)
3362 		goto errout;
3363 
3364 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
3365 	if (err < 0) {
3366 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3367 		WARN_ON(err == -EMSGSIZE);
3368 		kfree_skb(skb);
3369 		goto errout;
3370 	}
3371 	err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3372 errout:
3373 	if (err < 0)
3374 		rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
3375 }
3376 
3377 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3378 				__s32 *array, int bytes)
3379 {
3380 	BUG_ON(bytes < (DEVCONF_MAX * 4));
3381 
3382 	memset(array, 0, bytes);
3383 	array[DEVCONF_FORWARDING] = cnf->forwarding;
3384 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3385 	array[DEVCONF_MTU6] = cnf->mtu6;
3386 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3387 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3388 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
3389 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3390 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3391 	array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3392 	array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3393 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3394 #ifdef CONFIG_IPV6_PRIVACY
3395 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3396 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3397 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3398 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3399 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3400 #endif
3401 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3402 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3403 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3404 #ifdef CONFIG_IPV6_ROUTER_PREF
3405 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
3406 	array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
3407 #ifdef CONFIG_IPV6_ROUTE_INFO
3408 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3409 #endif
3410 #endif
3411 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
3412 }
3413 
3414 static inline size_t inet6_if_nlmsg_size(void)
3415 {
3416 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3417 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3418 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3419 	       + nla_total_size(4) /* IFLA_MTU */
3420 	       + nla_total_size(4) /* IFLA_LINK */
3421 	       + nla_total_size( /* IFLA_PROTINFO */
3422 			nla_total_size(4) /* IFLA_INET6_FLAGS */
3423 			+ nla_total_size(sizeof(struct ifla_cacheinfo))
3424 			+ nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3425 		 );
3426 }
3427 
3428 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3429 			     u32 pid, u32 seq, int event, unsigned int flags)
3430 {
3431 	struct net_device *dev = idev->dev;
3432 	struct nlattr *conf;
3433 	struct ifinfomsg *hdr;
3434 	struct nlmsghdr *nlh;
3435 	void *protoinfo;
3436 	struct ifla_cacheinfo ci;
3437 
3438 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3439 	if (nlh == NULL)
3440 		return -EMSGSIZE;
3441 
3442 	hdr = nlmsg_data(nlh);
3443 	hdr->ifi_family = AF_INET6;
3444 	hdr->__ifi_pad = 0;
3445 	hdr->ifi_type = dev->type;
3446 	hdr->ifi_index = dev->ifindex;
3447 	hdr->ifi_flags = dev_get_flags(dev);
3448 	hdr->ifi_change = 0;
3449 
3450 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
3451 
3452 	if (dev->addr_len)
3453 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3454 
3455 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
3456 	if (dev->ifindex != dev->iflink)
3457 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
3458 
3459 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3460 	if (protoinfo == NULL)
3461 		goto nla_put_failure;
3462 
3463 	NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
3464 
3465 	ci.max_reasm_len = IPV6_MAXPLEN;
3466 	ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3467 		    + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3468 	ci.reachable_time = idev->nd_parms->reachable_time;
3469 	ci.retrans_time = idev->nd_parms->retrans_time;
3470 	NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3471 
3472 	conf = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3473 	if (conf == NULL)
3474 		goto nla_put_failure;
3475 	ipv6_store_devconf(&idev->cnf, nla_data(conf), nla_len(conf));
3476 
3477 	/* XXX - Statistics/MC not implemented */
3478 
3479 	nla_nest_end(skb, protoinfo);
3480 	return nlmsg_end(skb, nlh);
3481 
3482 nla_put_failure:
3483 	nlmsg_cancel(skb, nlh);
3484 	return -EMSGSIZE;
3485 }
3486 
3487 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3488 {
3489 	int idx, err;
3490 	int s_idx = cb->args[0];
3491 	struct net_device *dev;
3492 	struct inet6_dev *idev;
3493 
3494 	read_lock(&dev_base_lock);
3495 	for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3496 		if (idx < s_idx)
3497 			continue;
3498 		if ((idev = in6_dev_get(dev)) == NULL)
3499 			continue;
3500 		err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3501 				cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3502 		in6_dev_put(idev);
3503 		if (err <= 0)
3504 			break;
3505 	}
3506 	read_unlock(&dev_base_lock);
3507 	cb->args[0] = idx;
3508 
3509 	return skb->len;
3510 }
3511 
3512 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3513 {
3514 	struct sk_buff *skb;
3515 	int err = -ENOBUFS;
3516 
3517 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
3518 	if (skb == NULL)
3519 		goto errout;
3520 
3521 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
3522 	if (err < 0) {
3523 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3524 		WARN_ON(err == -EMSGSIZE);
3525 		kfree_skb(skb);
3526 		goto errout;
3527 	}
3528 	err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3529 errout:
3530 	if (err < 0)
3531 		rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
3532 }
3533 
3534 static inline size_t inet6_prefix_nlmsg_size(void)
3535 {
3536 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
3537 	       + nla_total_size(sizeof(struct in6_addr))
3538 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
3539 }
3540 
3541 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3542 			     struct prefix_info *pinfo, u32 pid, u32 seq,
3543 			     int event, unsigned int flags)
3544 {
3545 	struct prefixmsg *pmsg;
3546 	struct nlmsghdr *nlh;
3547 	struct prefix_cacheinfo	ci;
3548 
3549 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3550 	if (nlh == NULL)
3551 		return -EMSGSIZE;
3552 
3553 	pmsg = nlmsg_data(nlh);
3554 	pmsg->prefix_family = AF_INET6;
3555 	pmsg->prefix_pad1 = 0;
3556 	pmsg->prefix_pad2 = 0;
3557 	pmsg->prefix_ifindex = idev->dev->ifindex;
3558 	pmsg->prefix_len = pinfo->prefix_len;
3559 	pmsg->prefix_type = pinfo->type;
3560 	pmsg->prefix_pad3 = 0;
3561 	pmsg->prefix_flags = 0;
3562 	if (pinfo->onlink)
3563 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3564 	if (pinfo->autoconf)
3565 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3566 
3567 	NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3568 
3569 	ci.preferred_time = ntohl(pinfo->prefered);
3570 	ci.valid_time = ntohl(pinfo->valid);
3571 	NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3572 
3573 	return nlmsg_end(skb, nlh);
3574 
3575 nla_put_failure:
3576 	nlmsg_cancel(skb, nlh);
3577 	return -EMSGSIZE;
3578 }
3579 
3580 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3581 			 struct prefix_info *pinfo)
3582 {
3583 	struct sk_buff *skb;
3584 	int err = -ENOBUFS;
3585 
3586 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
3587 	if (skb == NULL)
3588 		goto errout;
3589 
3590 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
3591 	if (err < 0) {
3592 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3593 		WARN_ON(err == -EMSGSIZE);
3594 		kfree_skb(skb);
3595 		goto errout;
3596 	}
3597 	err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3598 errout:
3599 	if (err < 0)
3600 		rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err);
3601 }
3602 
3603 static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
3604 	[RTM_GETLINK - RTM_BASE] = { .dumpit	= inet6_dump_ifinfo, },
3605 	[RTM_NEWADDR - RTM_BASE] = { .doit	= inet6_rtm_newaddr, },
3606 	[RTM_DELADDR - RTM_BASE] = { .doit	= inet6_rtm_deladdr, },
3607 	[RTM_GETADDR - RTM_BASE] = { .doit	= inet6_rtm_getaddr,
3608 				     .dumpit	= inet6_dump_ifaddr, },
3609 	[RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3610 	[RTM_GETANYCAST - RTM_BASE] = { .dumpit	= inet6_dump_ifacaddr, },
3611 	[RTM_NEWROUTE - RTM_BASE] = { .doit	= inet6_rtm_newroute, },
3612 	[RTM_DELROUTE - RTM_BASE] = { .doit	= inet6_rtm_delroute, },
3613 	[RTM_GETROUTE - RTM_BASE] = { .doit	= inet6_rtm_getroute,
3614 				      .dumpit	= inet6_dump_fib, },
3615 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3616 	[RTM_GETRULE  - RTM_BASE] = { .dumpit   = fib6_rules_dump,   },
3617 #endif
3618 };
3619 
3620 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3621 {
3622 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3623 
3624 	switch (event) {
3625 	case RTM_NEWADDR:
3626 		ip6_ins_rt(ifp->rt);
3627 		if (ifp->idev->cnf.forwarding)
3628 			addrconf_join_anycast(ifp);
3629 		break;
3630 	case RTM_DELADDR:
3631 		if (ifp->idev->cnf.forwarding)
3632 			addrconf_leave_anycast(ifp);
3633 		addrconf_leave_solict(ifp->idev, &ifp->addr);
3634 		dst_hold(&ifp->rt->u.dst);
3635 		if (ip6_del_rt(ifp->rt))
3636 			dst_free(&ifp->rt->u.dst);
3637 		break;
3638 	}
3639 }
3640 
3641 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3642 {
3643 	rcu_read_lock_bh();
3644 	if (likely(ifp->idev->dead == 0))
3645 		__ipv6_ifa_notify(event, ifp);
3646 	rcu_read_unlock_bh();
3647 }
3648 
3649 #ifdef CONFIG_SYSCTL
3650 
3651 static
3652 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3653 			   void __user *buffer, size_t *lenp, loff_t *ppos)
3654 {
3655 	int *valp = ctl->data;
3656 	int val = *valp;
3657 	int ret;
3658 
3659 	ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3660 
3661 	if (write && valp != &ipv6_devconf_dflt.forwarding) {
3662 		if (valp != &ipv6_devconf.forwarding) {
3663 			if ((!*valp) ^ (!val)) {
3664 				struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3665 				if (idev == NULL)
3666 					return ret;
3667 				dev_forward_change(idev);
3668 			}
3669 		} else {
3670 			ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3671 			addrconf_forward_change();
3672 		}
3673 		if (*valp)
3674 			rt6_purge_dflt_routers();
3675 	}
3676 
3677 	return ret;
3678 }
3679 
3680 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3681 					    int __user *name, int nlen,
3682 					    void __user *oldval,
3683 					    size_t __user *oldlenp,
3684 					    void __user *newval, size_t newlen)
3685 {
3686 	int *valp = table->data;
3687 	int new;
3688 
3689 	if (!newval || !newlen)
3690 		return 0;
3691 	if (newlen != sizeof(int))
3692 		return -EINVAL;
3693 	if (get_user(new, (int __user *)newval))
3694 		return -EFAULT;
3695 	if (new == *valp)
3696 		return 0;
3697 	if (oldval && oldlenp) {
3698 		size_t len;
3699 		if (get_user(len, oldlenp))
3700 			return -EFAULT;
3701 		if (len) {
3702 			if (len > table->maxlen)
3703 				len = table->maxlen;
3704 			if (copy_to_user(oldval, valp, len))
3705 				return -EFAULT;
3706 			if (put_user(len, oldlenp))
3707 				return -EFAULT;
3708 		}
3709 	}
3710 
3711 	if (valp != &ipv6_devconf_dflt.forwarding) {
3712 		if (valp != &ipv6_devconf.forwarding) {
3713 			struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3714 			int changed;
3715 			if (unlikely(idev == NULL))
3716 				return -ENODEV;
3717 			changed = (!*valp) ^ (!new);
3718 			*valp = new;
3719 			if (changed)
3720 				dev_forward_change(idev);
3721 		} else {
3722 			*valp = new;
3723 			addrconf_forward_change();
3724 		}
3725 
3726 		if (*valp)
3727 			rt6_purge_dflt_routers();
3728 	} else
3729 		*valp = new;
3730 
3731 	return 1;
3732 }
3733 
3734 static struct addrconf_sysctl_table
3735 {
3736 	struct ctl_table_header *sysctl_header;
3737 	ctl_table addrconf_vars[__NET_IPV6_MAX];
3738 	ctl_table addrconf_dev[2];
3739 	ctl_table addrconf_conf_dir[2];
3740 	ctl_table addrconf_proto_dir[2];
3741 	ctl_table addrconf_root_dir[2];
3742 } addrconf_sysctl __read_mostly = {
3743 	.sysctl_header = NULL,
3744 	.addrconf_vars = {
3745 		{
3746 			.ctl_name	=	NET_IPV6_FORWARDING,
3747 			.procname	=	"forwarding",
3748 			.data		=	&ipv6_devconf.forwarding,
3749 			.maxlen		=	sizeof(int),
3750 			.mode		=	0644,
3751 			.proc_handler	=	&addrconf_sysctl_forward,
3752 			.strategy	=	&addrconf_sysctl_forward_strategy,
3753 		},
3754 		{
3755 			.ctl_name	=	NET_IPV6_HOP_LIMIT,
3756 			.procname	=	"hop_limit",
3757 			.data		=	&ipv6_devconf.hop_limit,
3758 			.maxlen		=	sizeof(int),
3759 			.mode		=	0644,
3760 			.proc_handler	=	proc_dointvec,
3761 		},
3762 		{
3763 			.ctl_name	=	NET_IPV6_MTU,
3764 			.procname	=	"mtu",
3765 			.data		=	&ipv6_devconf.mtu6,
3766 			.maxlen		=	sizeof(int),
3767 			.mode		=	0644,
3768 			.proc_handler	=	&proc_dointvec,
3769 		},
3770 		{
3771 			.ctl_name	=	NET_IPV6_ACCEPT_RA,
3772 			.procname	=	"accept_ra",
3773 			.data		=	&ipv6_devconf.accept_ra,
3774 			.maxlen		=	sizeof(int),
3775 			.mode		=	0644,
3776 			.proc_handler	=	&proc_dointvec,
3777 		},
3778 		{
3779 			.ctl_name	=	NET_IPV6_ACCEPT_REDIRECTS,
3780 			.procname	=	"accept_redirects",
3781 			.data		=	&ipv6_devconf.accept_redirects,
3782 			.maxlen		=	sizeof(int),
3783 			.mode		=	0644,
3784 			.proc_handler	=	&proc_dointvec,
3785 		},
3786 		{
3787 			.ctl_name	=	NET_IPV6_AUTOCONF,
3788 			.procname	=	"autoconf",
3789 			.data		=	&ipv6_devconf.autoconf,
3790 			.maxlen		=	sizeof(int),
3791 			.mode		=	0644,
3792 			.proc_handler	=	&proc_dointvec,
3793 		},
3794 		{
3795 			.ctl_name	=	NET_IPV6_DAD_TRANSMITS,
3796 			.procname	=	"dad_transmits",
3797 			.data		=	&ipv6_devconf.dad_transmits,
3798 			.maxlen		=	sizeof(int),
3799 			.mode		=	0644,
3800 			.proc_handler	=	&proc_dointvec,
3801 		},
3802 		{
3803 			.ctl_name	=	NET_IPV6_RTR_SOLICITS,
3804 			.procname	=	"router_solicitations",
3805 			.data		=	&ipv6_devconf.rtr_solicits,
3806 			.maxlen		=	sizeof(int),
3807 			.mode		=	0644,
3808 			.proc_handler	=	&proc_dointvec,
3809 		},
3810 		{
3811 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_INTERVAL,
3812 			.procname	=	"router_solicitation_interval",
3813 			.data		=	&ipv6_devconf.rtr_solicit_interval,
3814 			.maxlen		=	sizeof(int),
3815 			.mode		=	0644,
3816 			.proc_handler	=	&proc_dointvec_jiffies,
3817 			.strategy	=	&sysctl_jiffies,
3818 		},
3819 		{
3820 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_DELAY,
3821 			.procname	=	"router_solicitation_delay",
3822 			.data		=	&ipv6_devconf.rtr_solicit_delay,
3823 			.maxlen		=	sizeof(int),
3824 			.mode		=	0644,
3825 			.proc_handler	=	&proc_dointvec_jiffies,
3826 			.strategy	=	&sysctl_jiffies,
3827 		},
3828 		{
3829 			.ctl_name	=	NET_IPV6_FORCE_MLD_VERSION,
3830 			.procname	=	"force_mld_version",
3831 			.data		=	&ipv6_devconf.force_mld_version,
3832 			.maxlen		=	sizeof(int),
3833 			.mode		=	0644,
3834 			.proc_handler	=	&proc_dointvec,
3835 		},
3836 #ifdef CONFIG_IPV6_PRIVACY
3837 		{
3838 			.ctl_name	=	NET_IPV6_USE_TEMPADDR,
3839 			.procname	=	"use_tempaddr",
3840 			.data		=	&ipv6_devconf.use_tempaddr,
3841 			.maxlen		=	sizeof(int),
3842 			.mode		=	0644,
3843 			.proc_handler	=	&proc_dointvec,
3844 		},
3845 		{
3846 			.ctl_name	=	NET_IPV6_TEMP_VALID_LFT,
3847 			.procname	=	"temp_valid_lft",
3848 			.data		=	&ipv6_devconf.temp_valid_lft,
3849 			.maxlen		=	sizeof(int),
3850 			.mode		=	0644,
3851 			.proc_handler	=	&proc_dointvec,
3852 		},
3853 		{
3854 			.ctl_name	=	NET_IPV6_TEMP_PREFERED_LFT,
3855 			.procname	=	"temp_prefered_lft",
3856 			.data		=	&ipv6_devconf.temp_prefered_lft,
3857 			.maxlen		=	sizeof(int),
3858 			.mode		=	0644,
3859 			.proc_handler	=	&proc_dointvec,
3860 		},
3861 		{
3862 			.ctl_name	=	NET_IPV6_REGEN_MAX_RETRY,
3863 			.procname	=	"regen_max_retry",
3864 			.data		=	&ipv6_devconf.regen_max_retry,
3865 			.maxlen		=	sizeof(int),
3866 			.mode		=	0644,
3867 			.proc_handler	=	&proc_dointvec,
3868 		},
3869 		{
3870 			.ctl_name	=	NET_IPV6_MAX_DESYNC_FACTOR,
3871 			.procname	=	"max_desync_factor",
3872 			.data		=	&ipv6_devconf.max_desync_factor,
3873 			.maxlen		=	sizeof(int),
3874 			.mode		=	0644,
3875 			.proc_handler	=	&proc_dointvec,
3876 		},
3877 #endif
3878 		{
3879 			.ctl_name	=	NET_IPV6_MAX_ADDRESSES,
3880 			.procname	=	"max_addresses",
3881 			.data		=	&ipv6_devconf.max_addresses,
3882 			.maxlen		=	sizeof(int),
3883 			.mode		=	0644,
3884 			.proc_handler	=	&proc_dointvec,
3885 		},
3886 		{
3887 			.ctl_name	=	NET_IPV6_ACCEPT_RA_DEFRTR,
3888 			.procname	=	"accept_ra_defrtr",
3889 			.data		=	&ipv6_devconf.accept_ra_defrtr,
3890 			.maxlen		=	sizeof(int),
3891 			.mode		=	0644,
3892 			.proc_handler	=	&proc_dointvec,
3893 		},
3894 		{
3895 			.ctl_name	=	NET_IPV6_ACCEPT_RA_PINFO,
3896 			.procname	=	"accept_ra_pinfo",
3897 			.data		=	&ipv6_devconf.accept_ra_pinfo,
3898 			.maxlen		=	sizeof(int),
3899 			.mode		=	0644,
3900 			.proc_handler	=	&proc_dointvec,
3901 		},
3902 #ifdef CONFIG_IPV6_ROUTER_PREF
3903 		{
3904 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RTR_PREF,
3905 			.procname	=	"accept_ra_rtr_pref",
3906 			.data		=	&ipv6_devconf.accept_ra_rtr_pref,
3907 			.maxlen		=	sizeof(int),
3908 			.mode		=	0644,
3909 			.proc_handler	=	&proc_dointvec,
3910 		},
3911 		{
3912 			.ctl_name	=	NET_IPV6_RTR_PROBE_INTERVAL,
3913 			.procname	=	"router_probe_interval",
3914 			.data		=	&ipv6_devconf.rtr_probe_interval,
3915 			.maxlen		=	sizeof(int),
3916 			.mode		=	0644,
3917 			.proc_handler	=	&proc_dointvec_jiffies,
3918 			.strategy	=	&sysctl_jiffies,
3919 		},
3920 #ifdef CONFIG_IPV6_ROUTE_INFO
3921 		{
3922 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
3923 			.procname	=	"accept_ra_rt_info_max_plen",
3924 			.data		=	&ipv6_devconf.accept_ra_rt_info_max_plen,
3925 			.maxlen		=	sizeof(int),
3926 			.mode		=	0644,
3927 			.proc_handler	=	&proc_dointvec,
3928 		},
3929 #endif
3930 #endif
3931 		{
3932 			.ctl_name	=	NET_IPV6_PROXY_NDP,
3933 			.procname	=	"proxy_ndp",
3934 			.data		=	&ipv6_devconf.proxy_ndp,
3935 			.maxlen		=	sizeof(int),
3936 			.mode		=	0644,
3937 			.proc_handler	=	&proc_dointvec,
3938 		},
3939 		{
3940 			.ctl_name	=	0,	/* sentinel */
3941 		}
3942 	},
3943 	.addrconf_dev = {
3944 		{
3945 			.ctl_name	=	NET_PROTO_CONF_ALL,
3946 			.procname	=	"all",
3947 			.mode		=	0555,
3948 			.child		=	addrconf_sysctl.addrconf_vars,
3949 		},
3950 		{
3951 			.ctl_name	=	0,	/* sentinel */
3952 		}
3953 	},
3954 	.addrconf_conf_dir = {
3955 		{
3956 			.ctl_name	=	NET_IPV6_CONF,
3957 			.procname	=	"conf",
3958 			.mode		=	0555,
3959 			.child		=	addrconf_sysctl.addrconf_dev,
3960 		},
3961 		{
3962 			.ctl_name	=	0,	/* sentinel */
3963 		}
3964 	},
3965 	.addrconf_proto_dir = {
3966 		{
3967 			.ctl_name	=	NET_IPV6,
3968 			.procname	=	"ipv6",
3969 			.mode		=	0555,
3970 			.child		=	addrconf_sysctl.addrconf_conf_dir,
3971 		},
3972 		{
3973 			.ctl_name	=	0,	/* sentinel */
3974 		}
3975 	},
3976 	.addrconf_root_dir = {
3977 		{
3978 			.ctl_name	=	CTL_NET,
3979 			.procname	=	"net",
3980 			.mode		=	0555,
3981 			.child		=	addrconf_sysctl.addrconf_proto_dir,
3982 		},
3983 		{
3984 			.ctl_name	=	0,	/* sentinel */
3985 		}
3986 	},
3987 };
3988 
3989 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3990 {
3991 	int i;
3992 	struct net_device *dev = idev ? idev->dev : NULL;
3993 	struct addrconf_sysctl_table *t;
3994 	char *dev_name = NULL;
3995 
3996 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
3997 	if (t == NULL)
3998 		return;
3999 	for (i=0; t->addrconf_vars[i].data; i++) {
4000 		t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
4001 		t->addrconf_vars[i].de = NULL;
4002 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4003 	}
4004 	if (dev) {
4005 		dev_name = dev->name;
4006 		t->addrconf_dev[0].ctl_name = dev->ifindex;
4007 	} else {
4008 		dev_name = "default";
4009 		t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
4010 	}
4011 
4012 	/*
4013 	 * Make a copy of dev_name, because '.procname' is regarded as const
4014 	 * by sysctl and we wouldn't want anyone to change it under our feet
4015 	 * (see SIOCSIFNAME).
4016 	 */
4017 	dev_name = kstrdup(dev_name, GFP_KERNEL);
4018 	if (!dev_name)
4019 	    goto free;
4020 
4021 	t->addrconf_dev[0].procname = dev_name;
4022 
4023 	t->addrconf_dev[0].child = t->addrconf_vars;
4024 	t->addrconf_dev[0].de = NULL;
4025 	t->addrconf_conf_dir[0].child = t->addrconf_dev;
4026 	t->addrconf_conf_dir[0].de = NULL;
4027 	t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
4028 	t->addrconf_proto_dir[0].de = NULL;
4029 	t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
4030 	t->addrconf_root_dir[0].de = NULL;
4031 
4032 	t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
4033 	if (t->sysctl_header == NULL)
4034 		goto free_procname;
4035 	else
4036 		p->sysctl = t;
4037 	return;
4038 
4039 	/* error path */
4040  free_procname:
4041 	kfree(dev_name);
4042  free:
4043 	kfree(t);
4044 
4045 	return;
4046 }
4047 
4048 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
4049 {
4050 	if (p->sysctl) {
4051 		struct addrconf_sysctl_table *t = p->sysctl;
4052 		p->sysctl = NULL;
4053 		unregister_sysctl_table(t->sysctl_header);
4054 		kfree(t->addrconf_dev[0].procname);
4055 		kfree(t);
4056 	}
4057 }
4058 
4059 
4060 #endif
4061 
4062 /*
4063  *      Device notifier
4064  */
4065 
4066 int register_inet6addr_notifier(struct notifier_block *nb)
4067 {
4068 	return atomic_notifier_chain_register(&inet6addr_chain, nb);
4069 }
4070 
4071 int unregister_inet6addr_notifier(struct notifier_block *nb)
4072 {
4073 	return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
4074 }
4075 
4076 /*
4077  *	Init / cleanup code
4078  */
4079 
4080 int __init addrconf_init(void)
4081 {
4082 	int err = 0;
4083 
4084 	/* The addrconf netdev notifier requires that loopback_dev
4085 	 * has it's ipv6 private information allocated and setup
4086 	 * before it can bring up and give link-local addresses
4087 	 * to other devices which are up.
4088 	 *
4089 	 * Unfortunately, loopback_dev is not necessarily the first
4090 	 * entry in the global dev_base list of net devices.  In fact,
4091 	 * it is likely to be the very last entry on that list.
4092 	 * So this causes the notifier registry below to try and
4093 	 * give link-local addresses to all devices besides loopback_dev
4094 	 * first, then loopback_dev, which cases all the non-loopback_dev
4095 	 * devices to fail to get a link-local address.
4096 	 *
4097 	 * So, as a temporary fix, allocate the ipv6 structure for
4098 	 * loopback_dev first by hand.
4099 	 * Longer term, all of the dependencies ipv6 has upon the loopback
4100 	 * device and it being up should be removed.
4101 	 */
4102 	rtnl_lock();
4103 	if (!ipv6_add_dev(&loopback_dev))
4104 		err = -ENOMEM;
4105 	rtnl_unlock();
4106 	if (err)
4107 		return err;
4108 
4109 	ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
4110 
4111 	register_netdevice_notifier(&ipv6_dev_notf);
4112 
4113 	addrconf_verify(0);
4114 	rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
4115 #ifdef CONFIG_SYSCTL
4116 	addrconf_sysctl.sysctl_header =
4117 		register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
4118 	addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
4119 #endif
4120 
4121 	return 0;
4122 }
4123 
4124 void __exit addrconf_cleanup(void)
4125 {
4126 	struct net_device *dev;
4127 	struct inet6_dev *idev;
4128 	struct inet6_ifaddr *ifa;
4129 	int i;
4130 
4131 	unregister_netdevice_notifier(&ipv6_dev_notf);
4132 
4133 	rtnetlink_links[PF_INET6] = NULL;
4134 #ifdef CONFIG_SYSCTL
4135 	addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4136 	addrconf_sysctl_unregister(&ipv6_devconf);
4137 #endif
4138 
4139 	rtnl_lock();
4140 
4141 	/*
4142 	 *	clean dev list.
4143 	 */
4144 
4145 	for (dev=dev_base; dev; dev=dev->next) {
4146 		if ((idev = __in6_dev_get(dev)) == NULL)
4147 			continue;
4148 		addrconf_ifdown(dev, 1);
4149 	}
4150 	addrconf_ifdown(&loopback_dev, 2);
4151 
4152 	/*
4153 	 *	Check hash table.
4154 	 */
4155 
4156 	write_lock_bh(&addrconf_hash_lock);
4157 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
4158 		for (ifa=inet6_addr_lst[i]; ifa; ) {
4159 			struct inet6_ifaddr *bifa;
4160 
4161 			bifa = ifa;
4162 			ifa = ifa->lst_next;
4163 			printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4164 			/* Do not free it; something is wrong.
4165 			   Now we can investigate it with debugger.
4166 			 */
4167 		}
4168 	}
4169 	write_unlock_bh(&addrconf_hash_lock);
4170 
4171 	del_timer(&addr_chk_timer);
4172 
4173 	rtnl_unlock();
4174 
4175 #ifdef CONFIG_PROC_FS
4176 	proc_net_remove("if_inet6");
4177 #endif
4178 }
4179