in6_ifattach.c revision 1.4
1/*	$OpenBSD: in6_ifattach.c,v 1.4 2000/02/02 17:16:52 itojun Exp $	*/
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/malloc.h>
35#include <sys/socket.h>
36#include <sys/sockio.h>
37#include <sys/kernel.h>
38#include <sys/md5k.h>
39
40#include <net/if.h>
41#include <net/if_dl.h>
42#include <net/if_types.h>
43#include <net/route.h>
44
45#include <netinet/in.h>
46#include <netinet/in_var.h>
47#include <netinet/if_ether.h>
48
49#include <netinet6/ip6.h>
50#include <netinet6/ip6_var.h>
51#include <netinet6/in6_ifattach.h>
52#include <netinet6/ip6.h>
53#include <netinet6/ip6_var.h>
54#include <netinet6/nd6.h>
55
56#include <net/net_osdep.h>
57
58static	struct in6_addr llsol;
59
60struct in6_ifstat **in6_ifstat = NULL;
61struct icmp6_ifstat **icmp6_ifstat = NULL;
62size_t in6_ifstatmax = 0;
63size_t icmp6_ifstatmax = 0;
64unsigned long in6_maxmtu = 0;
65
66int found_first_ifid = 0;
67#define IFID_LEN 8
68static u_int8_t first_ifid[IFID_LEN];
69
70static int laddr_to_eui64 __P((u_int8_t *, u_int8_t *, size_t));
71static int gen_rand_eui64 __P((u_int8_t *));
72
73static int
74laddr_to_eui64(dst, src, len)
75	u_int8_t *dst;
76	u_int8_t *src;
77	size_t len;
78{
79	static u_int8_t zero[8];
80
81	bzero(zero, sizeof(zero));
82
83	switch (len) {
84	case 6:
85		if (bcmp(zero, src, 6) == 0)
86			return EINVAL;
87		dst[0] = src[0];
88		dst[1] = src[1];
89		dst[2] = src[2];
90		dst[3] = 0xff;
91		dst[4] = 0xfe;
92		dst[5] = src[3];
93		dst[6] = src[4];
94		dst[7] = src[5];
95		break;
96	case 8:
97		if (bcmp(zero, src, 8) == 0)
98			return EINVAL;
99		bcopy(src, dst, len);
100		break;
101	default:
102		return EINVAL;
103	}
104
105	return 0;
106}
107
108/*
109 * Generate a last-resort interface identifier, when the machine has no
110 * IEEE802/EUI64 address sources.
111 * The address should be random, and should not change across reboot.
112 */
113static int
114gen_rand_eui64(dst)
115	u_int8_t *dst;
116{
117	MD5_CTX ctxt;
118	u_int8_t digest[16];
119
120	/* generate 8bytes of pseudo-random value. */
121	bzero(&ctxt, sizeof(ctxt));
122	MD5Init(&ctxt);
123	MD5Update(&ctxt, hostname, hostnamelen);
124	MD5Final(digest, &ctxt);
125
126	/* assumes sizeof(digest) > sizeof(first_ifid) */
127	bcopy(digest, dst, 8);
128
129	/* make sure to set "u" bit to local, and "g" bit to individual. */
130	dst[0] &= 0xfe;
131	dst[0] |= 0x02;		/* EUI64 "local" */
132
133	return 0;
134}
135
136/*
137 * Find first ifid on list of interfaces.
138 * This is assumed that ifp0's interface token (for example, IEEE802 MAC)
139 * is globally unique.  We may need to have a flag parameter in the future.
140 */
141int
142in6_ifattach_getifid(ifp0)
143	struct ifnet *ifp0;
144{
145	struct ifnet *ifp;
146	struct ifaddr *ifa;
147	u_int8_t *addr = NULL;
148	int addrlen = 0;
149	struct sockaddr_dl *sdl;
150
151	if (found_first_ifid)
152		return 0;
153
154	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
155	{
156		if (ifp0 != NULL && ifp0 != ifp)
157			continue;
158		for (ifa = ifp->if_addrlist.tqh_first;
159		     ifa;
160		     ifa = ifa->ifa_list.tqe_next)
161		{
162			if (ifa->ifa_addr->sa_family != AF_LINK)
163				continue;
164			sdl = (struct sockaddr_dl *)ifa->ifa_addr;
165			if (sdl == NULL)
166				continue;
167			if (sdl->sdl_alen == 0)
168				continue;
169			switch (ifp->if_type) {
170			case IFT_ETHER:
171			case IFT_FDDI:
172			case IFT_ATM:
173				/* IEEE802/EUI64 cases - what others? */
174				addr = LLADDR(sdl);
175				addrlen = sdl->sdl_alen;
176				/*
177				 * to copy ifid from IEEE802/EUI64 interface,
178				 * u bit of the source needs to be 0.
179				 */
180				if ((addr[0] & 0x02) != 0)
181					break;
182				goto found;
183			case IFT_ARCNET:
184				/*
185				 * ARCnet interface token cannot be used as
186				 * globally unique identifier due to its
187				 * small bitwidth.
188				 */
189				break;
190			default:
191				break;
192			}
193		}
194	}
195#ifdef DEBUG
196	printf("in6_ifattach_getifid: failed to get EUI64");
197#endif
198	return EADDRNOTAVAIL;
199
200found:
201	if (laddr_to_eui64(first_ifid, addr, addrlen) == 0)
202		found_first_ifid = 1;
203
204	if (found_first_ifid) {
205		printf("%s: supplying EUI64: "
206			"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
207			if_name(ifp),
208			first_ifid[0], first_ifid[1],
209			first_ifid[2], first_ifid[3],
210			first_ifid[4], first_ifid[5],
211			first_ifid[6], first_ifid[7]);
212
213		/* invert u bit to convert EUI64 to RFC2373 interface ID. */
214		first_ifid[0] ^= 0x02;
215
216		return 0;
217	} else {
218#ifdef DEBUG
219		printf("in6_ifattach_getifid: failed to get EUI64");
220#endif
221		return EADDRNOTAVAIL;
222	}
223}
224
225/*
226 * XXX multiple loopback interface needs more care.  for instance,
227 * nodelocal address needs to be configured onto only one of them.
228 */
229void
230in6_ifattach(ifp, type, laddr, noloop)
231	struct ifnet *ifp;
232	u_int type;
233	caddr_t laddr;
234	/* size_t laddrlen; */
235	int noloop;
236{
237	static size_t if_indexlim = 8;
238	struct sockaddr_in6 mltaddr;
239	struct sockaddr_in6 mltmask;
240	struct sockaddr_in6 gate;
241	struct sockaddr_in6 mask;
242	struct in6_ifaddr *ia, *ib, *oia;
243	struct ifaddr *ifa;
244	int rtflag = 0;
245
246	if (type == IN6_IFT_P2P && found_first_ifid == 0) {
247		printf("%s: no ifid available for IPv6 link-local address\n",
248			if_name(ifp));
249#if 0
250		return;
251#else
252		/* last resort */
253		if (gen_rand_eui64(first_ifid) == 0) {
254			printf("%s: using random value as EUI64: "
255				"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
256				if_name(ifp),
257				first_ifid[0], first_ifid[1],
258				first_ifid[2], first_ifid[3],
259				first_ifid[4], first_ifid[5],
260				first_ifid[6], first_ifid[7]);
261			/*
262			 * invert u bit to convert EUI64 to RFC2373 interface
263			 * ID.
264			 */
265			first_ifid[0] ^= 0x02;
266
267			found_first_ifid = 1;
268		}
269#endif
270	}
271
272	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
273		printf("%s: not multicast capable, IPv6 not enabled\n",
274			if_name(ifp));
275		return;
276	}
277
278	/*
279	 * We have some arrays that should be indexed by if_index.
280	 * since if_index will grow dynamically, they should grow too.
281	 *	struct in6_ifstat **in6_ifstat
282	 *	struct icmp6_ifstat **icmp6_ifstat
283	 */
284	if (in6_ifstat == NULL || icmp6_ifstat == NULL
285	 || if_index >= if_indexlim) {
286		size_t n;
287		caddr_t q;
288		size_t olim;
289
290		olim = if_indexlim;
291		while (if_index >= if_indexlim)
292			if_indexlim <<= 1;
293
294		/* grow in6_ifstat */
295		n = if_indexlim * sizeof(struct in6_ifstat *);
296		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
297		bzero(q, n);
298		if (in6_ifstat) {
299			bcopy((caddr_t)in6_ifstat, q,
300				olim * sizeof(struct in6_ifstat *));
301			free((caddr_t)in6_ifstat, M_IFADDR);
302		}
303		in6_ifstat = (struct in6_ifstat **)q;
304		in6_ifstatmax = if_indexlim;
305
306		/* grow icmp6_ifstat */
307		n = if_indexlim * sizeof(struct icmp6_ifstat *);
308		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
309		bzero(q, n);
310		if (icmp6_ifstat) {
311			bcopy((caddr_t)icmp6_ifstat, q,
312				olim * sizeof(struct icmp6_ifstat *));
313			free((caddr_t)icmp6_ifstat, M_IFADDR);
314		}
315		icmp6_ifstat = (struct icmp6_ifstat **)q;
316		icmp6_ifstatmax = if_indexlim;
317	}
318
319	/*
320	 * To prevent to assign link-local address to PnP network
321	 * cards multiple times.
322	 * This is lengthy for P2P and LOOP but works.
323	 */
324	ifa = TAILQ_FIRST(&ifp->if_addrlist);
325	if (ifa != NULL) {
326		for ( ; ifa; ifa = TAILQ_NEXT(ifa, ifa_list)) {
327			if (ifa->ifa_addr->sa_family != AF_INET6)
328				continue;
329			if (IN6_IS_ADDR_LINKLOCAL(&satosin6(ifa->ifa_addr)->sin6_addr))
330				return;
331		}
332	} else {
333		TAILQ_INIT(&ifp->if_addrlist);
334	}
335
336	/*
337	 * link-local address
338	 */
339	ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
340	bzero((caddr_t)ia, sizeof(*ia));
341	ia->ia_ifa.ifa_addr =    (struct sockaddr *)&ia->ia_addr;
342	ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
343	ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
344	ia->ia_ifp = ifp;
345
346	TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
347
348	/*
349	 * Also link into the IPv6 address chain beginning with in6_ifaddr.
350	 * kazu opposed it, but itojun & jinmei wanted.
351	 */
352	if ((oia = in6_ifaddr) != NULL) {
353		for (; oia->ia_next; oia = oia->ia_next)
354			continue;
355		oia->ia_next = ia;
356	} else
357		in6_ifaddr = ia;
358
359	ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
360	ia->ia_prefixmask.sin6_family = AF_INET6;
361	ia->ia_prefixmask.sin6_addr = in6mask64;
362
363	bzero(&ia->ia_addr, sizeof(struct sockaddr_in6));
364	ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
365	ia->ia_addr.sin6_family = AF_INET6;
366	ia->ia_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
367	ia->ia_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
368	ia->ia_addr.sin6_addr.s6_addr32[1] = 0;
369
370	switch (type) {
371	case IN6_IFT_LOOP:
372		ia->ia_addr.sin6_addr.s6_addr32[2] = 0;
373		ia->ia_addr.sin6_addr.s6_addr32[3] = htonl(1);
374		break;
375	case IN6_IFT_802:
376		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
377		ia->ia_ifa.ifa_flags |= RTF_CLONING;
378		rtflag = RTF_CLONING;
379		/* fall through */
380	case IN6_IFT_P2P802:
381		if (laddr == NULL)
382			break;
383		/* XXX use laddrlen */
384		if (laddr_to_eui64(&ia->ia_addr.sin6_addr.s6_addr8[8],
385				laddr, 6) != 0) {
386			break;
387		}
388		/* invert u bit to convert EUI64 to RFC2373 interface ID. */
389		ia->ia_addr.sin6_addr.s6_addr8[8] ^= 0x02;
390		if (found_first_ifid == 0)
391			in6_ifattach_getifid(ifp);
392		bzero(&ia->ia_dstaddr, sizeof(struct sockaddr_in6));
393		ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
394		ia->ia_dstaddr.sin6_family = AF_INET6;
395		break;
396	case IN6_IFT_P2P:
397		bcopy((caddr_t)first_ifid,
398		      (caddr_t)&ia->ia_addr.sin6_addr.s6_addr8[8],
399		      IFID_LEN);
400		bzero(&ia->ia_dstaddr, sizeof(struct sockaddr_in6));
401		ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
402		ia->ia_dstaddr.sin6_family = AF_INET6;
403		break;
404	case IN6_IFT_ARCNET:
405		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
406		ia->ia_ifa.ifa_flags |= RTF_CLONING;
407		rtflag = RTF_CLONING;
408		if (laddr == NULL)
409			break;
410
411		/* make non-global IF id out of link-level address */
412		bzero(&ia->ia_addr.sin6_addr.s6_addr8[8], 7);
413		ia->ia_addr.sin6_addr.s6_addr8[15] = *laddr;
414	}
415
416	ia->ia_ifa.ifa_metric = ifp->if_metric;
417
418	if (ifp->if_ioctl != NULL) {
419		int s;
420		int error;
421
422		/*
423		 * give the interface a chance to initialize, in case this
424		 * is the first address to be added.
425		 */
426		s = splimp();
427		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
428		splx(s);
429
430		if (error) {
431			switch (error) {
432			case EAFNOSUPPORT:
433				printf("%s: IPv6 not supported\n",
434					if_name(ifp));
435				break;
436			default:
437				printf("%s: SIOCSIFADDR error %d\n",
438					if_name(ifp), error);
439				break;
440			}
441
442			/* undo changes */
443			TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
444			if (oia)
445				oia->ia_next = ia->ia_next;
446			else
447				in6_ifaddr = ia->ia_next;
448			free(ia, M_IFADDR);
449			return;
450		}
451	}
452
453	/* add route to the interface. */
454	rtrequest(RTM_ADD,
455		  (struct sockaddr *)&ia->ia_addr,
456		  (struct sockaddr *)&ia->ia_addr,
457		  (struct sockaddr *)&ia->ia_prefixmask,
458		  RTF_UP|rtflag,
459		  (struct rtentry **)0);
460	ia->ia_flags |= IFA_ROUTE;
461
462	if (type == IN6_IFT_P2P || type == IN6_IFT_P2P802) {
463		/*
464		 * route local address to loopback
465		 */
466		bzero(&gate, sizeof(gate));
467		gate.sin6_len = sizeof(struct sockaddr_in6);
468		gate.sin6_family = AF_INET6;
469		gate.sin6_addr = in6addr_loopback;
470		bzero(&mask, sizeof(mask));
471		mask.sin6_len = sizeof(struct sockaddr_in6);
472		mask.sin6_family = AF_INET6;
473		mask.sin6_addr = in6mask64;
474		rtrequest(RTM_ADD,
475			  (struct sockaddr *)&ia->ia_addr,
476			  (struct sockaddr *)&gate,
477			  (struct sockaddr *)&mask,
478			  RTF_UP|RTF_HOST,
479			  (struct rtentry **)0);
480	}
481
482	/*
483	 * loopback address
484	 */
485	ib = (struct in6_ifaddr *)NULL;
486	if (type == IN6_IFT_LOOP) {
487		ib = (struct in6_ifaddr *)
488			malloc(sizeof(*ib), M_IFADDR, M_WAITOK);
489		bzero((caddr_t)ib, sizeof(*ib));
490		ib->ia_ifa.ifa_addr = (struct sockaddr *)&ib->ia_addr;
491		ib->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ib->ia_dstaddr;
492		ib->ia_ifa.ifa_netmask = (struct sockaddr *)&ib->ia_prefixmask;
493		ib->ia_ifp = ifp;
494
495		ia->ia_next = ib;
496		TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ib,
497			ifa_list);
498
499		ib->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
500		ib->ia_prefixmask.sin6_family = AF_INET6;
501		ib->ia_prefixmask.sin6_addr = in6mask128;
502		ib->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
503		ib->ia_addr.sin6_family = AF_INET6;
504		ib->ia_addr.sin6_addr = in6addr_loopback;
505		ib->ia_ifa.ifa_metric = ifp->if_metric;
506
507		rtrequest(RTM_ADD,
508			  (struct sockaddr *)&ib->ia_addr,
509			  (struct sockaddr *)&ib->ia_addr,
510			  (struct sockaddr *)&ib->ia_prefixmask,
511			  RTF_UP|RTF_HOST,
512			  (struct rtentry **)0);
513
514		ib->ia_flags |= IFA_ROUTE;
515	}
516
517	/*
518	 * join multicast
519	 */
520	if (ifp->if_flags & IFF_MULTICAST) {
521		int error;	/* not used */
522
523		/* Restore saved multicast addresses(if any). */
524		in6_restoremkludge(ia, ifp);
525
526		bzero(&mltmask, sizeof(mltmask));
527		mltmask.sin6_len = sizeof(struct sockaddr_in6);
528		mltmask.sin6_family = AF_INET6;
529		mltmask.sin6_addr = in6mask32;
530
531		/*
532		 * join link-local all-nodes address
533		 */
534		bzero(&mltaddr, sizeof(mltaddr));
535		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
536		mltaddr.sin6_family = AF_INET6;
537		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
538		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
539		rtrequest(RTM_ADD,
540			  (struct sockaddr *)&mltaddr,
541			  (struct sockaddr *)&ia->ia_addr,
542			  (struct sockaddr *)&mltmask,
543			  RTF_UP|RTF_CLONING,  /* xxx */
544			  (struct rtentry **)0);
545		(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
546
547		if (type == IN6_IFT_LOOP) {
548			/*
549			 * join node-local all-nodes address
550			 */
551			mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
552			rtrequest(RTM_ADD,
553				  (struct sockaddr *)&mltaddr,
554				  (struct sockaddr *)&ib->ia_addr,
555				  (struct sockaddr *)&mltmask,
556				  RTF_UP,
557				  (struct rtentry **)0);
558			(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
559		} else {
560			/*
561			 * join solicited multicast address
562			 */
563			bzero(&llsol, sizeof(llsol));
564			llsol.s6_addr16[0] = htons(0xff02);
565			llsol.s6_addr16[1] = htons(ifp->if_index);
566			llsol.s6_addr32[1] = 0;
567			llsol.s6_addr32[2] = htonl(1);
568			llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
569			llsol.s6_addr8[12] = 0xff;
570			(void)in6_addmulti(&llsol, ifp, &error);
571		}
572	}
573
574	/* update dynamically. */
575	if (in6_maxmtu < ifp->if_mtu)
576		in6_maxmtu = ifp->if_mtu;
577
578	if (in6_ifstat[ifp->if_index] == NULL) {
579		in6_ifstat[ifp->if_index] = (struct in6_ifstat *)
580			malloc(sizeof(struct in6_ifstat), M_IFADDR, M_WAITOK);
581		bzero(in6_ifstat[ifp->if_index], sizeof(struct in6_ifstat));
582	}
583	if (icmp6_ifstat[ifp->if_index] == NULL) {
584		icmp6_ifstat[ifp->if_index] = (struct icmp6_ifstat *)
585			malloc(sizeof(struct icmp6_ifstat), M_IFADDR, M_WAITOK);
586		bzero(icmp6_ifstat[ifp->if_index], sizeof(struct icmp6_ifstat));
587	}
588
589	/* initialize NDP variables */
590	nd6_ifattach(ifp);
591
592	/* mark the address TENTATIVE, if needed. */
593	switch (ifp->if_type) {
594	case IFT_ARCNET:
595	case IFT_ETHER:
596	case IFT_FDDI:
597#if 0
598	case IFT_ATM:
599	case IFT_SLIP:
600	case IFT_PPP:
601#endif
602		ia->ia6_flags |= IN6_IFF_TENTATIVE;
603		/* nd6_dad_start() will be called in in6_if_up */
604		break;
605	case IFT_DUMMY:
606	case IFT_GIF:	/*XXX*/
607	case IFT_LOOP:
608	case IFT_FAITH:
609	default:
610		break;
611	}
612
613	return;
614}
615
616/*
617 * NOTE: in6_ifdetach() does not support loopback if at this moment.
618 */
619void
620in6_ifdetach(ifp)
621	struct ifnet *ifp;
622{
623	struct in6_ifaddr *ia, *oia;
624	struct ifaddr *ifa;
625	struct rtentry *rt;
626	short rtflags;
627	struct sockaddr_in6 sin6;
628
629	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
630	{
631		if (ifa->ifa_addr->sa_family != AF_INET6
632		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
633			continue;
634		}
635
636		ia = (struct in6_ifaddr *)ifa;
637
638		/* remove from the routing table */
639		if ((ia->ia_flags & IFA_ROUTE)
640		 && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
641			rtflags = rt->rt_flags;
642			rtfree(rt);
643			rtrequest(RTM_DELETE,
644				(struct sockaddr *)&ia->ia_addr,
645				(struct sockaddr *)&ia->ia_addr,
646				(struct sockaddr *)&ia->ia_prefixmask,
647				rtflags, (struct rtentry **)0);
648		}
649
650		/* remove from the linked list */
651		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
652
653		/* also remove from the IPv6 address chain(itojun&jinmei) */
654		oia = ia;
655		if (oia == (ia = in6_ifaddr))
656			in6_ifaddr = ia->ia_next;
657		else {
658			while (ia->ia_next && (ia->ia_next != oia))
659				ia = ia->ia_next;
660			if (ia->ia_next)
661				ia->ia_next = oia->ia_next;
662#ifdef DEBUG
663			else
664				printf("%s: didn't unlink in6ifaddr from "
665				    "list\n", if_name(ifp));
666#endif
667		}
668
669		free(ia, M_IFADDR);
670	}
671
672	/* cleanup multicast address kludge table, if there is any */
673	in6_purgemkludge(ifp);
674
675	/* remove route to link-local allnodes multicast (ff02::1) */
676	bzero(&sin6, sizeof(sin6));
677	sin6.sin6_len = sizeof(struct sockaddr_in6);
678	sin6.sin6_family = AF_INET6;
679	sin6.sin6_addr = in6addr_linklocal_allnodes;
680	sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
681	if ((rt = rtalloc1((struct sockaddr *)&sin6, 0)) != NULL) {
682		rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
683			rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
684		rtfree(rt);
685	}
686}
687