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