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