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