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