Deleted Added
full compact
in6_ifattach.c (193066) in6_ifattach.c (193232)
1/*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $
30 */
31
32#include <sys/cdefs.h>
1/*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/netinet6/in6_ifattach.c 193066 2009-05-29 21:27:12Z jamie $");
33__FBSDID("$FreeBSD: head/sys/netinet6/in6_ifattach.c 193232 2009-06-01 15:49:42Z bz $");
34
35#include "opt_route.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/malloc.h>
40#include <sys/socket.h>
41#include <sys/sockio.h>
42#include <sys/jail.h>
43#include <sys/kernel.h>
44#include <sys/syslog.h>
45#include <sys/md5.h>
46#include <sys/vimage.h>
47
48#include <net/if.h>
49#include <net/if_dl.h>
50#include <net/if_types.h>
51#include <net/route.h>
52#include <net/vnet.h>
53
54#include <netinet/in.h>
55#include <netinet/in_var.h>
56#include <netinet/if_ether.h>
57#include <netinet/in_pcb.h>
58#include <netinet/vinet.h>
59
60#include <netinet/ip6.h>
61#include <netinet6/ip6_var.h>
62#include <netinet6/in6_var.h>
63#include <netinet6/in6_pcb.h>
64#include <netinet6/in6_ifattach.h>
65#include <netinet6/ip6_var.h>
66#include <netinet6/nd6.h>
67#include <netinet6/mld6_var.h>
68#include <netinet6/scope6_var.h>
69#include <netinet6/vinet6.h>
70
71#ifdef VIMAGE_GLOBALS
72unsigned long in6_maxmtu;
73int ip6_auto_linklocal;
74struct callout in6_tmpaddrtimer_ch;
75extern struct inpcbinfo ripcbinfo;
76#endif
77
78static int get_rand_ifid(struct ifnet *, struct in6_addr *);
79static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
80static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
81static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
82static int in6_ifattach_loopback(struct ifnet *);
83static void in6_purgemaddrs(struct ifnet *);
84
85#define EUI64_GBIT 0x01
86#define EUI64_UBIT 0x02
87#define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
88#define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
89#define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
90#define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
91#define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
92
93#define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
94#define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
95
96/*
97 * Generate a last-resort interface identifier, when the machine has no
98 * IEEE802/EUI64 address sources.
99 * The goal here is to get an interface identifier that is
100 * (1) random enough and (2) does not change across reboot.
101 * We currently use MD5(hostname) for it.
102 *
103 * in6 - upper 64bits are preserved
104 */
105static int
106get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
107{
108 MD5_CTX ctxt;
109 struct prison *pr;
110 u_int8_t digest[16];
111 int hostnamelen;
112
113 pr = curthread->td_ucred->cr_prison;
114 mtx_lock(&pr->pr_mtx);
115 hostnamelen = strlen(pr->pr_host);
116#if 0
117 /* we need at least several letters as seed for ifid */
118 if (hostnamelen < 3) {
119 mtx_unlock(&pr->pr_mtx);
120 return -1;
121 }
122#endif
123
124 /* generate 8 bytes of pseudo-random value. */
125 bzero(&ctxt, sizeof(ctxt));
126 MD5Init(&ctxt);
127 MD5Update(&ctxt, pr->pr_host, hostnamelen);
128 mtx_unlock(&pr->pr_mtx);
129 MD5Final(digest, &ctxt);
130
131 /* assumes sizeof(digest) > sizeof(ifid) */
132 bcopy(digest, &in6->s6_addr[8], 8);
133
134 /* make sure to set "u" bit to local, and "g" bit to individual. */
135 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
136 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
137
138 /* convert EUI64 into IPv6 interface identifier */
139 EUI64_TO_IFID(in6);
140
141 return 0;
142}
143
144static int
145generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
146{
147 INIT_VNET_INET6(curvnet);
148 MD5_CTX ctxt;
149 u_int8_t seed[16], digest[16], nullbuf[8];
150 u_int32_t val32;
151
152 /* If there's no history, start with a random seed. */
153 bzero(nullbuf, sizeof(nullbuf));
154 if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
155 int i;
156
157 for (i = 0; i < 2; i++) {
158 val32 = arc4random();
159 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
160 }
161 } else
162 bcopy(seed0, seed, 8);
163
164 /* copy the right-most 64-bits of the given address */
165 /* XXX assumption on the size of IFID */
166 bcopy(seed1, &seed[8], 8);
167
168 if (0) { /* for debugging purposes only */
169 int i;
170
171 printf("generate_tmp_ifid: new randomized ID from: ");
172 for (i = 0; i < 16; i++)
173 printf("%02x", seed[i]);
174 printf(" ");
175 }
176
177 /* generate 16 bytes of pseudo-random value. */
178 bzero(&ctxt, sizeof(ctxt));
179 MD5Init(&ctxt);
180 MD5Update(&ctxt, seed, sizeof(seed));
181 MD5Final(digest, &ctxt);
182
183 /*
184 * RFC 3041 3.2.1. (3)
185 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
186 * left-most bit is numbered 0) to zero.
187 */
188 bcopy(digest, ret, 8);
189 ret[0] &= ~EUI64_UBIT;
190
191 /*
192 * XXX: we'd like to ensure that the generated value is not zero
193 * for simplicity. If the caclculated digest happens to be zero,
194 * use a random non-zero value as the last resort.
195 */
196 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
197 nd6log((LOG_INFO,
198 "generate_tmp_ifid: computed MD5 value is zero.\n"));
199
200 val32 = arc4random();
201 val32 = 1 + (val32 % (0xffffffff - 1));
202 }
203
204 /*
205 * RFC 3041 3.2.1. (4)
206 * Take the rightmost 64-bits of the MD5 digest and save them in
207 * stable storage as the history value to be used in the next
208 * iteration of the algorithm.
209 */
210 bcopy(&digest[8], seed0, 8);
211
212 if (0) { /* for debugging purposes only */
213 int i;
214
215 printf("to: ");
216 for (i = 0; i < 16; i++)
217 printf("%02x", digest[i]);
218 printf("\n");
219 }
220
221 return 0;
222}
223
224/*
225 * Get interface identifier for the specified interface.
226 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
227 *
228 * in6 - upper 64bits are preserved
229 */
230int
231in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
232{
233 struct ifaddr *ifa;
234 struct sockaddr_dl *sdl;
235 u_int8_t *addr;
236 size_t addrlen;
237 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
238 static u_int8_t allone[8] =
239 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
240
241 IF_ADDR_LOCK(ifp);
242 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
243 if (ifa->ifa_addr->sa_family != AF_LINK)
244 continue;
245 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
246 if (sdl == NULL)
247 continue;
248 if (sdl->sdl_alen == 0)
249 continue;
250
251 goto found;
252 }
253 IF_ADDR_UNLOCK(ifp);
254
255 return -1;
256
257found:
258 addr = LLADDR(sdl);
259 addrlen = sdl->sdl_alen;
260
261 /* get EUI64 */
262 switch (ifp->if_type) {
263 case IFT_ETHER:
264 case IFT_FDDI:
265 case IFT_ISO88025:
266 case IFT_ATM:
267 case IFT_IEEE1394:
268#ifdef IFT_IEEE80211
269 case IFT_IEEE80211:
270#endif
271 /* IEEE802/EUI64 cases - what others? */
272 /* IEEE1394 uses 16byte length address starting with EUI64 */
273 if (addrlen > 8)
274 addrlen = 8;
275
276 /* look at IEEE802/EUI64 only */
277 if (addrlen != 8 && addrlen != 6) {
278 IF_ADDR_UNLOCK(ifp);
279 return -1;
280 }
281
282 /*
283 * check for invalid MAC address - on bsdi, we see it a lot
284 * since wildboar configures all-zero MAC on pccard before
285 * card insertion.
286 */
287 if (bcmp(addr, allzero, addrlen) == 0) {
288 IF_ADDR_UNLOCK(ifp);
289 return -1;
290 }
291 if (bcmp(addr, allone, addrlen) == 0) {
292 IF_ADDR_UNLOCK(ifp);
293 return -1;
294 }
295
296 /* make EUI64 address */
297 if (addrlen == 8)
298 bcopy(addr, &in6->s6_addr[8], 8);
299 else if (addrlen == 6) {
300 in6->s6_addr[8] = addr[0];
301 in6->s6_addr[9] = addr[1];
302 in6->s6_addr[10] = addr[2];
303 in6->s6_addr[11] = 0xff;
304 in6->s6_addr[12] = 0xfe;
305 in6->s6_addr[13] = addr[3];
306 in6->s6_addr[14] = addr[4];
307 in6->s6_addr[15] = addr[5];
308 }
309 break;
310
311 case IFT_ARCNET:
312 if (addrlen != 1) {
313 IF_ADDR_UNLOCK(ifp);
314 return -1;
315 }
316 if (!addr[0]) {
317 IF_ADDR_UNLOCK(ifp);
318 return -1;
319 }
320
321 bzero(&in6->s6_addr[8], 8);
322 in6->s6_addr[15] = addr[0];
323
324 /*
325 * due to insufficient bitwidth, we mark it local.
326 */
327 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
328 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
329 break;
330
331 case IFT_GIF:
332#ifdef IFT_STF
333 case IFT_STF:
334#endif
335 /*
336 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
337 * however, IPv4 address is not very suitable as unique
338 * identifier source (can be renumbered).
339 * we don't do this.
340 */
341 IF_ADDR_UNLOCK(ifp);
342 return -1;
343
344 default:
345 IF_ADDR_UNLOCK(ifp);
346 return -1;
347 }
348
349 /* sanity check: g bit must not indicate "group" */
350 if (EUI64_GROUP(in6)) {
351 IF_ADDR_UNLOCK(ifp);
352 return -1;
353 }
354
355 /* convert EUI64 into IPv6 interface identifier */
356 EUI64_TO_IFID(in6);
357
358 /*
359 * sanity check: ifid must not be all zero, avoid conflict with
360 * subnet router anycast
361 */
362 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
363 bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
364 IF_ADDR_UNLOCK(ifp);
365 return -1;
366 }
367
368 IF_ADDR_UNLOCK(ifp);
369 return 0;
370}
371
372/*
373 * Get interface identifier for the specified interface. If it is not
374 * available on ifp0, borrow interface identifier from other information
375 * sources.
376 *
377 * altifp - secondary EUI64 source
378 */
379static int
380get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
381 struct in6_addr *in6)
382{
383 INIT_VNET_NET(ifp0->if_vnet);
384 INIT_VNET_INET6(ifp0->if_vnet);
385 struct ifnet *ifp;
386
387 /* first, try to get it from the interface itself */
388 if (in6_get_hw_ifid(ifp0, in6) == 0) {
389 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
390 if_name(ifp0)));
391 goto success;
392 }
393
394 /* try secondary EUI64 source. this basically is for ATM PVC */
395 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
396 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
397 if_name(ifp0), if_name(altifp)));
398 goto success;
399 }
400
401 /* next, try to get it from some other hardware interface */
402 IFNET_RLOCK();
403 for (ifp = V_ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
404 if (ifp == ifp0)
405 continue;
406 if (in6_get_hw_ifid(ifp, in6) != 0)
407 continue;
408
409 /*
410 * to borrow ifid from other interface, ifid needs to be
411 * globally unique
412 */
413 if (IFID_UNIVERSAL(in6)) {
414 nd6log((LOG_DEBUG,
415 "%s: borrow interface identifier from %s\n",
416 if_name(ifp0), if_name(ifp)));
417 IFNET_RUNLOCK();
418 goto success;
419 }
420 }
421 IFNET_RUNLOCK();
422
423 /* last resort: get from random number source */
424 if (get_rand_ifid(ifp, in6) == 0) {
425 nd6log((LOG_DEBUG,
426 "%s: interface identifier generated by random number\n",
427 if_name(ifp0)));
428 goto success;
429 }
430
431 printf("%s: failed to get interface identifier\n", if_name(ifp0));
432 return -1;
433
434success:
435 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
436 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
437 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
438 in6->s6_addr[14], in6->s6_addr[15]));
439 return 0;
440}
441
442/*
443 * altifp - secondary EUI64 source
444 */
445static int
446in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
447{
448 INIT_VNET_INET6(curvnet);
449 struct in6_ifaddr *ia;
450 struct in6_aliasreq ifra;
451 struct nd_prefixctl pr0;
452 int i, error;
453
454 /*
455 * configure link-local address.
456 */
457 bzero(&ifra, sizeof(ifra));
458
459 /*
460 * in6_update_ifa() does not use ifra_name, but we accurately set it
461 * for safety.
462 */
463 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
464
465 ifra.ifra_addr.sin6_family = AF_INET6;
466 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
467 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
468 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
469 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
470 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
471 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
472 } else {
473 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
474 nd6log((LOG_ERR,
475 "%s: no ifid available\n", if_name(ifp)));
476 return (-1);
477 }
478 }
479 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
480 return (-1);
481
482 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
483 ifra.ifra_prefixmask.sin6_family = AF_INET6;
484 ifra.ifra_prefixmask.sin6_addr = in6mask64;
485 /* link-local addresses should NEVER expire. */
486 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
487 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
488
489 /*
490 * Now call in6_update_ifa() to do a bunch of procedures to configure
491 * a link-local address. We can set the 3rd argument to NULL, because
492 * we know there's no other link-local address on the interface
493 * and therefore we are adding one (instead of updating one).
494 */
495 if ((error = in6_update_ifa(ifp, &ifra, NULL,
496 IN6_IFAUPDATE_DADDELAY)) != 0) {
497 /*
498 * XXX: When the interface does not support IPv6, this call
499 * would fail in the SIOCSIFADDR ioctl. I believe the
500 * notification is rather confusing in this case, so just
501 * suppress it. (jinmei@kame.net 20010130)
502 */
503 if (error != EAFNOSUPPORT)
504 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
505 "configure a link-local address on %s "
506 "(errno=%d)\n",
507 if_name(ifp), error));
508 return (-1);
509 }
510
511 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
512#ifdef DIAGNOSTIC
513 if (!ia) {
514 panic("ia == NULL in in6_ifattach_linklocal");
515 /* NOTREACHED */
516 }
517#endif
518
519 /*
520 * Make the link-local prefix (fe80::%link/64) as on-link.
521 * Since we'd like to manage prefixes separately from addresses,
522 * we make an ND6 prefix structure for the link-local prefix,
523 * and add it to the prefix list as a never-expire prefix.
524 * XXX: this change might affect some existing code base...
525 */
526 bzero(&pr0, sizeof(pr0));
527 pr0.ndpr_ifp = ifp;
528 /* this should be 64 at this moment. */
529 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
530 pr0.ndpr_prefix = ifra.ifra_addr;
531 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
532 for (i = 0; i < 4; i++) {
533 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
534 in6mask64.s6_addr32[i];
535 }
536 /*
537 * Initialize parameters. The link-local prefix must always be
538 * on-link, and its lifetimes never expire.
539 */
540 pr0.ndpr_raf_onlink = 1;
541 pr0.ndpr_raf_auto = 1; /* probably meaningless */
542 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
543 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
544 /*
545 * Since there is no other link-local addresses, nd6_prefix_lookup()
546 * probably returns NULL. However, we cannot always expect the result.
547 * For example, if we first remove the (only) existing link-local
548 * address, and then reconfigure another one, the prefix is still
549 * valid with referring to the old link-local address.
550 */
551 if (nd6_prefix_lookup(&pr0) == NULL) {
552 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
553 return (error);
554 }
555
556 return 0;
557}
558
559/*
560 * ifp - must be IFT_LOOP
561 */
562static int
563in6_ifattach_loopback(struct ifnet *ifp)
564{
565 INIT_VNET_INET6(curvnet);
566 struct in6_aliasreq ifra;
567 int error;
568
569 bzero(&ifra, sizeof(ifra));
570
571 /*
572 * in6_update_ifa() does not use ifra_name, but we accurately set it
573 * for safety.
574 */
575 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
576
577 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
578 ifra.ifra_prefixmask.sin6_family = AF_INET6;
579 ifra.ifra_prefixmask.sin6_addr = in6mask128;
580
581 /*
582 * Always initialize ia_dstaddr (= broadcast address) to loopback
583 * address. Follows IPv4 practice - see in_ifinit().
584 */
585 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
586 ifra.ifra_dstaddr.sin6_family = AF_INET6;
587 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
588
589 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
590 ifra.ifra_addr.sin6_family = AF_INET6;
591 ifra.ifra_addr.sin6_addr = in6addr_loopback;
592
593 /* the loopback address should NEVER expire. */
594 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
595 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
596
597 /* we don't need to perform DAD on loopback interfaces. */
598 ifra.ifra_flags |= IN6_IFF_NODAD;
599
600 /* skip registration to the prefix list. XXX should be temporary. */
601 ifra.ifra_flags |= IN6_IFF_NOPFX;
602
603 /*
604 * We are sure that this is a newly assigned address, so we can set
605 * NULL to the 3rd arg.
606 */
607 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
608 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
609 "the loopback address on %s (errno=%d)\n",
610 if_name(ifp), error));
611 return (-1);
612 }
613
614 return 0;
615}
616
617/*
618 * compute NI group address, based on the current hostname setting.
619 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
620 *
621 * when ifp == NULL, the caller is responsible for filling scopeid.
622 */
623int
624in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
625 struct in6_addr *in6)
626{
627 struct prison *pr;
628 const char *p;
629 u_char *q;
630 MD5_CTX ctxt;
631 u_int8_t digest[16];
632 char l;
633 char n[64]; /* a single label must not exceed 63 chars */
634
635 /*
636 * If no name is given and namelen is -1,
637 * we try to do the hostname lookup ourselves.
638 */
639 if (!name && namelen == -1) {
640 pr = curthread->td_ucred->cr_prison;
641 mtx_lock(&pr->pr_mtx);
642 name = pr->pr_host;
643 namelen = strlen(name);
644 } else
645 pr = NULL;
646 if (!name || !namelen) {
647 if (pr != NULL)
648 mtx_unlock(&pr->pr_mtx);
649 return -1;
650 }
651
652 p = name;
653 while (p && *p && *p != '.' && p - name < namelen)
654 p++;
655 if (p == name || p - name > sizeof(n) - 1) {
656 if (pr != NULL)
657 mtx_unlock(&pr->pr_mtx);
658 return -1; /* label too long */
659 }
660 l = p - name;
661 strncpy(n, name, l);
662 if (pr != NULL)
663 mtx_unlock(&pr->pr_mtx);
664 n[(int)l] = '\0';
665 for (q = n; *q; q++) {
666 if ('A' <= *q && *q <= 'Z')
667 *q = *q - 'A' + 'a';
668 }
669
670 /* generate 8 bytes of pseudo-random value. */
671 bzero(&ctxt, sizeof(ctxt));
672 MD5Init(&ctxt);
673 MD5Update(&ctxt, &l, sizeof(l));
674 MD5Update(&ctxt, n, l);
675 MD5Final(digest, &ctxt);
676
677 bzero(in6, sizeof(*in6));
678 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
679 in6->s6_addr8[11] = 2;
680 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
681 if (in6_setscope(in6, ifp, NULL))
682 return (-1); /* XXX: should not fail */
683
684 return 0;
685}
686
687/*
688 * XXX multiple loopback interface needs more care. for instance,
689 * nodelocal address needs to be configured onto only one of them.
690 * XXX multiple link-local address case
691 *
692 * altifp - secondary EUI64 source
693 */
694void
695in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
696{
697 INIT_VNET_INET6(ifp->if_vnet);
698 struct in6_ifaddr *ia;
699 struct in6_addr in6;
700
701 /* some of the interfaces are inherently not IPv6 capable */
702 switch (ifp->if_type) {
703 case IFT_PFLOG:
704 case IFT_PFSYNC:
705 case IFT_CARP:
706 return;
707 }
708
709 /*
710 * quirks based on interface type
711 */
712 switch (ifp->if_type) {
713#ifdef IFT_STF
714 case IFT_STF:
715 /*
716 * 6to4 interface is a very special kind of beast.
717 * no multicast, no linklocal. RFC2529 specifies how to make
718 * linklocals for 6to4 interface, but there's no use and
719 * it is rather harmful to have one.
720 */
721 goto statinit;
722#endif
723 default:
724 break;
725 }
726
727 /*
728 * usually, we require multicast capability to the interface
729 */
730 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
731 nd6log((LOG_INFO, "in6_ifattach: "
732 "%s is not multicast capable, IPv6 not enabled\n",
733 if_name(ifp)));
734 return;
735 }
736
737 /*
738 * assign loopback address for loopback interface.
739 * XXX multiple loopback interface case.
740 */
741 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
742 in6 = in6addr_loopback;
743 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
744 if (in6_ifattach_loopback(ifp) != 0)
745 return;
746 }
747 }
748
749 /*
750 * assign a link-local address, if there's none.
751 */
752 if (V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) {
753 ia = in6ifa_ifpforlinklocal(ifp, 0);
754 if (ia == NULL) {
755 if (in6_ifattach_linklocal(ifp, altifp) == 0) {
756 /* linklocal address assigned */
757 } else {
758 /* failed to assign linklocal address. bark? */
759 }
760 }
761 }
762
763#ifdef IFT_STF /* XXX */
764statinit:
765#endif
766
767 /* update dynamically. */
768 if (V_in6_maxmtu < ifp->if_mtu)
769 V_in6_maxmtu = ifp->if_mtu;
770}
771
772/*
773 * NOTE: in6_ifdetach() does not support loopback if at this moment.
774 * We don't need this function in bsdi, because interfaces are never removed
775 * from the ifnet list in bsdi.
776 */
777void
778in6_ifdetach(struct ifnet *ifp)
779{
34
35#include "opt_route.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/malloc.h>
40#include <sys/socket.h>
41#include <sys/sockio.h>
42#include <sys/jail.h>
43#include <sys/kernel.h>
44#include <sys/syslog.h>
45#include <sys/md5.h>
46#include <sys/vimage.h>
47
48#include <net/if.h>
49#include <net/if_dl.h>
50#include <net/if_types.h>
51#include <net/route.h>
52#include <net/vnet.h>
53
54#include <netinet/in.h>
55#include <netinet/in_var.h>
56#include <netinet/if_ether.h>
57#include <netinet/in_pcb.h>
58#include <netinet/vinet.h>
59
60#include <netinet/ip6.h>
61#include <netinet6/ip6_var.h>
62#include <netinet6/in6_var.h>
63#include <netinet6/in6_pcb.h>
64#include <netinet6/in6_ifattach.h>
65#include <netinet6/ip6_var.h>
66#include <netinet6/nd6.h>
67#include <netinet6/mld6_var.h>
68#include <netinet6/scope6_var.h>
69#include <netinet6/vinet6.h>
70
71#ifdef VIMAGE_GLOBALS
72unsigned long in6_maxmtu;
73int ip6_auto_linklocal;
74struct callout in6_tmpaddrtimer_ch;
75extern struct inpcbinfo ripcbinfo;
76#endif
77
78static int get_rand_ifid(struct ifnet *, struct in6_addr *);
79static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
80static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
81static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
82static int in6_ifattach_loopback(struct ifnet *);
83static void in6_purgemaddrs(struct ifnet *);
84
85#define EUI64_GBIT 0x01
86#define EUI64_UBIT 0x02
87#define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
88#define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
89#define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
90#define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
91#define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
92
93#define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
94#define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
95
96/*
97 * Generate a last-resort interface identifier, when the machine has no
98 * IEEE802/EUI64 address sources.
99 * The goal here is to get an interface identifier that is
100 * (1) random enough and (2) does not change across reboot.
101 * We currently use MD5(hostname) for it.
102 *
103 * in6 - upper 64bits are preserved
104 */
105static int
106get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
107{
108 MD5_CTX ctxt;
109 struct prison *pr;
110 u_int8_t digest[16];
111 int hostnamelen;
112
113 pr = curthread->td_ucred->cr_prison;
114 mtx_lock(&pr->pr_mtx);
115 hostnamelen = strlen(pr->pr_host);
116#if 0
117 /* we need at least several letters as seed for ifid */
118 if (hostnamelen < 3) {
119 mtx_unlock(&pr->pr_mtx);
120 return -1;
121 }
122#endif
123
124 /* generate 8 bytes of pseudo-random value. */
125 bzero(&ctxt, sizeof(ctxt));
126 MD5Init(&ctxt);
127 MD5Update(&ctxt, pr->pr_host, hostnamelen);
128 mtx_unlock(&pr->pr_mtx);
129 MD5Final(digest, &ctxt);
130
131 /* assumes sizeof(digest) > sizeof(ifid) */
132 bcopy(digest, &in6->s6_addr[8], 8);
133
134 /* make sure to set "u" bit to local, and "g" bit to individual. */
135 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
136 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
137
138 /* convert EUI64 into IPv6 interface identifier */
139 EUI64_TO_IFID(in6);
140
141 return 0;
142}
143
144static int
145generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
146{
147 INIT_VNET_INET6(curvnet);
148 MD5_CTX ctxt;
149 u_int8_t seed[16], digest[16], nullbuf[8];
150 u_int32_t val32;
151
152 /* If there's no history, start with a random seed. */
153 bzero(nullbuf, sizeof(nullbuf));
154 if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
155 int i;
156
157 for (i = 0; i < 2; i++) {
158 val32 = arc4random();
159 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
160 }
161 } else
162 bcopy(seed0, seed, 8);
163
164 /* copy the right-most 64-bits of the given address */
165 /* XXX assumption on the size of IFID */
166 bcopy(seed1, &seed[8], 8);
167
168 if (0) { /* for debugging purposes only */
169 int i;
170
171 printf("generate_tmp_ifid: new randomized ID from: ");
172 for (i = 0; i < 16; i++)
173 printf("%02x", seed[i]);
174 printf(" ");
175 }
176
177 /* generate 16 bytes of pseudo-random value. */
178 bzero(&ctxt, sizeof(ctxt));
179 MD5Init(&ctxt);
180 MD5Update(&ctxt, seed, sizeof(seed));
181 MD5Final(digest, &ctxt);
182
183 /*
184 * RFC 3041 3.2.1. (3)
185 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
186 * left-most bit is numbered 0) to zero.
187 */
188 bcopy(digest, ret, 8);
189 ret[0] &= ~EUI64_UBIT;
190
191 /*
192 * XXX: we'd like to ensure that the generated value is not zero
193 * for simplicity. If the caclculated digest happens to be zero,
194 * use a random non-zero value as the last resort.
195 */
196 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
197 nd6log((LOG_INFO,
198 "generate_tmp_ifid: computed MD5 value is zero.\n"));
199
200 val32 = arc4random();
201 val32 = 1 + (val32 % (0xffffffff - 1));
202 }
203
204 /*
205 * RFC 3041 3.2.1. (4)
206 * Take the rightmost 64-bits of the MD5 digest and save them in
207 * stable storage as the history value to be used in the next
208 * iteration of the algorithm.
209 */
210 bcopy(&digest[8], seed0, 8);
211
212 if (0) { /* for debugging purposes only */
213 int i;
214
215 printf("to: ");
216 for (i = 0; i < 16; i++)
217 printf("%02x", digest[i]);
218 printf("\n");
219 }
220
221 return 0;
222}
223
224/*
225 * Get interface identifier for the specified interface.
226 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
227 *
228 * in6 - upper 64bits are preserved
229 */
230int
231in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
232{
233 struct ifaddr *ifa;
234 struct sockaddr_dl *sdl;
235 u_int8_t *addr;
236 size_t addrlen;
237 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
238 static u_int8_t allone[8] =
239 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
240
241 IF_ADDR_LOCK(ifp);
242 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
243 if (ifa->ifa_addr->sa_family != AF_LINK)
244 continue;
245 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
246 if (sdl == NULL)
247 continue;
248 if (sdl->sdl_alen == 0)
249 continue;
250
251 goto found;
252 }
253 IF_ADDR_UNLOCK(ifp);
254
255 return -1;
256
257found:
258 addr = LLADDR(sdl);
259 addrlen = sdl->sdl_alen;
260
261 /* get EUI64 */
262 switch (ifp->if_type) {
263 case IFT_ETHER:
264 case IFT_FDDI:
265 case IFT_ISO88025:
266 case IFT_ATM:
267 case IFT_IEEE1394:
268#ifdef IFT_IEEE80211
269 case IFT_IEEE80211:
270#endif
271 /* IEEE802/EUI64 cases - what others? */
272 /* IEEE1394 uses 16byte length address starting with EUI64 */
273 if (addrlen > 8)
274 addrlen = 8;
275
276 /* look at IEEE802/EUI64 only */
277 if (addrlen != 8 && addrlen != 6) {
278 IF_ADDR_UNLOCK(ifp);
279 return -1;
280 }
281
282 /*
283 * check for invalid MAC address - on bsdi, we see it a lot
284 * since wildboar configures all-zero MAC on pccard before
285 * card insertion.
286 */
287 if (bcmp(addr, allzero, addrlen) == 0) {
288 IF_ADDR_UNLOCK(ifp);
289 return -1;
290 }
291 if (bcmp(addr, allone, addrlen) == 0) {
292 IF_ADDR_UNLOCK(ifp);
293 return -1;
294 }
295
296 /* make EUI64 address */
297 if (addrlen == 8)
298 bcopy(addr, &in6->s6_addr[8], 8);
299 else if (addrlen == 6) {
300 in6->s6_addr[8] = addr[0];
301 in6->s6_addr[9] = addr[1];
302 in6->s6_addr[10] = addr[2];
303 in6->s6_addr[11] = 0xff;
304 in6->s6_addr[12] = 0xfe;
305 in6->s6_addr[13] = addr[3];
306 in6->s6_addr[14] = addr[4];
307 in6->s6_addr[15] = addr[5];
308 }
309 break;
310
311 case IFT_ARCNET:
312 if (addrlen != 1) {
313 IF_ADDR_UNLOCK(ifp);
314 return -1;
315 }
316 if (!addr[0]) {
317 IF_ADDR_UNLOCK(ifp);
318 return -1;
319 }
320
321 bzero(&in6->s6_addr[8], 8);
322 in6->s6_addr[15] = addr[0];
323
324 /*
325 * due to insufficient bitwidth, we mark it local.
326 */
327 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
328 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
329 break;
330
331 case IFT_GIF:
332#ifdef IFT_STF
333 case IFT_STF:
334#endif
335 /*
336 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
337 * however, IPv4 address is not very suitable as unique
338 * identifier source (can be renumbered).
339 * we don't do this.
340 */
341 IF_ADDR_UNLOCK(ifp);
342 return -1;
343
344 default:
345 IF_ADDR_UNLOCK(ifp);
346 return -1;
347 }
348
349 /* sanity check: g bit must not indicate "group" */
350 if (EUI64_GROUP(in6)) {
351 IF_ADDR_UNLOCK(ifp);
352 return -1;
353 }
354
355 /* convert EUI64 into IPv6 interface identifier */
356 EUI64_TO_IFID(in6);
357
358 /*
359 * sanity check: ifid must not be all zero, avoid conflict with
360 * subnet router anycast
361 */
362 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
363 bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
364 IF_ADDR_UNLOCK(ifp);
365 return -1;
366 }
367
368 IF_ADDR_UNLOCK(ifp);
369 return 0;
370}
371
372/*
373 * Get interface identifier for the specified interface. If it is not
374 * available on ifp0, borrow interface identifier from other information
375 * sources.
376 *
377 * altifp - secondary EUI64 source
378 */
379static int
380get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
381 struct in6_addr *in6)
382{
383 INIT_VNET_NET(ifp0->if_vnet);
384 INIT_VNET_INET6(ifp0->if_vnet);
385 struct ifnet *ifp;
386
387 /* first, try to get it from the interface itself */
388 if (in6_get_hw_ifid(ifp0, in6) == 0) {
389 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
390 if_name(ifp0)));
391 goto success;
392 }
393
394 /* try secondary EUI64 source. this basically is for ATM PVC */
395 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
396 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
397 if_name(ifp0), if_name(altifp)));
398 goto success;
399 }
400
401 /* next, try to get it from some other hardware interface */
402 IFNET_RLOCK();
403 for (ifp = V_ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
404 if (ifp == ifp0)
405 continue;
406 if (in6_get_hw_ifid(ifp, in6) != 0)
407 continue;
408
409 /*
410 * to borrow ifid from other interface, ifid needs to be
411 * globally unique
412 */
413 if (IFID_UNIVERSAL(in6)) {
414 nd6log((LOG_DEBUG,
415 "%s: borrow interface identifier from %s\n",
416 if_name(ifp0), if_name(ifp)));
417 IFNET_RUNLOCK();
418 goto success;
419 }
420 }
421 IFNET_RUNLOCK();
422
423 /* last resort: get from random number source */
424 if (get_rand_ifid(ifp, in6) == 0) {
425 nd6log((LOG_DEBUG,
426 "%s: interface identifier generated by random number\n",
427 if_name(ifp0)));
428 goto success;
429 }
430
431 printf("%s: failed to get interface identifier\n", if_name(ifp0));
432 return -1;
433
434success:
435 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
436 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
437 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
438 in6->s6_addr[14], in6->s6_addr[15]));
439 return 0;
440}
441
442/*
443 * altifp - secondary EUI64 source
444 */
445static int
446in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
447{
448 INIT_VNET_INET6(curvnet);
449 struct in6_ifaddr *ia;
450 struct in6_aliasreq ifra;
451 struct nd_prefixctl pr0;
452 int i, error;
453
454 /*
455 * configure link-local address.
456 */
457 bzero(&ifra, sizeof(ifra));
458
459 /*
460 * in6_update_ifa() does not use ifra_name, but we accurately set it
461 * for safety.
462 */
463 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
464
465 ifra.ifra_addr.sin6_family = AF_INET6;
466 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
467 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
468 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
469 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
470 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
471 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
472 } else {
473 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
474 nd6log((LOG_ERR,
475 "%s: no ifid available\n", if_name(ifp)));
476 return (-1);
477 }
478 }
479 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
480 return (-1);
481
482 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
483 ifra.ifra_prefixmask.sin6_family = AF_INET6;
484 ifra.ifra_prefixmask.sin6_addr = in6mask64;
485 /* link-local addresses should NEVER expire. */
486 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
487 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
488
489 /*
490 * Now call in6_update_ifa() to do a bunch of procedures to configure
491 * a link-local address. We can set the 3rd argument to NULL, because
492 * we know there's no other link-local address on the interface
493 * and therefore we are adding one (instead of updating one).
494 */
495 if ((error = in6_update_ifa(ifp, &ifra, NULL,
496 IN6_IFAUPDATE_DADDELAY)) != 0) {
497 /*
498 * XXX: When the interface does not support IPv6, this call
499 * would fail in the SIOCSIFADDR ioctl. I believe the
500 * notification is rather confusing in this case, so just
501 * suppress it. (jinmei@kame.net 20010130)
502 */
503 if (error != EAFNOSUPPORT)
504 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
505 "configure a link-local address on %s "
506 "(errno=%d)\n",
507 if_name(ifp), error));
508 return (-1);
509 }
510
511 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
512#ifdef DIAGNOSTIC
513 if (!ia) {
514 panic("ia == NULL in in6_ifattach_linklocal");
515 /* NOTREACHED */
516 }
517#endif
518
519 /*
520 * Make the link-local prefix (fe80::%link/64) as on-link.
521 * Since we'd like to manage prefixes separately from addresses,
522 * we make an ND6 prefix structure for the link-local prefix,
523 * and add it to the prefix list as a never-expire prefix.
524 * XXX: this change might affect some existing code base...
525 */
526 bzero(&pr0, sizeof(pr0));
527 pr0.ndpr_ifp = ifp;
528 /* this should be 64 at this moment. */
529 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
530 pr0.ndpr_prefix = ifra.ifra_addr;
531 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
532 for (i = 0; i < 4; i++) {
533 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
534 in6mask64.s6_addr32[i];
535 }
536 /*
537 * Initialize parameters. The link-local prefix must always be
538 * on-link, and its lifetimes never expire.
539 */
540 pr0.ndpr_raf_onlink = 1;
541 pr0.ndpr_raf_auto = 1; /* probably meaningless */
542 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
543 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
544 /*
545 * Since there is no other link-local addresses, nd6_prefix_lookup()
546 * probably returns NULL. However, we cannot always expect the result.
547 * For example, if we first remove the (only) existing link-local
548 * address, and then reconfigure another one, the prefix is still
549 * valid with referring to the old link-local address.
550 */
551 if (nd6_prefix_lookup(&pr0) == NULL) {
552 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
553 return (error);
554 }
555
556 return 0;
557}
558
559/*
560 * ifp - must be IFT_LOOP
561 */
562static int
563in6_ifattach_loopback(struct ifnet *ifp)
564{
565 INIT_VNET_INET6(curvnet);
566 struct in6_aliasreq ifra;
567 int error;
568
569 bzero(&ifra, sizeof(ifra));
570
571 /*
572 * in6_update_ifa() does not use ifra_name, but we accurately set it
573 * for safety.
574 */
575 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
576
577 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
578 ifra.ifra_prefixmask.sin6_family = AF_INET6;
579 ifra.ifra_prefixmask.sin6_addr = in6mask128;
580
581 /*
582 * Always initialize ia_dstaddr (= broadcast address) to loopback
583 * address. Follows IPv4 practice - see in_ifinit().
584 */
585 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
586 ifra.ifra_dstaddr.sin6_family = AF_INET6;
587 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
588
589 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
590 ifra.ifra_addr.sin6_family = AF_INET6;
591 ifra.ifra_addr.sin6_addr = in6addr_loopback;
592
593 /* the loopback address should NEVER expire. */
594 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
595 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
596
597 /* we don't need to perform DAD on loopback interfaces. */
598 ifra.ifra_flags |= IN6_IFF_NODAD;
599
600 /* skip registration to the prefix list. XXX should be temporary. */
601 ifra.ifra_flags |= IN6_IFF_NOPFX;
602
603 /*
604 * We are sure that this is a newly assigned address, so we can set
605 * NULL to the 3rd arg.
606 */
607 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
608 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
609 "the loopback address on %s (errno=%d)\n",
610 if_name(ifp), error));
611 return (-1);
612 }
613
614 return 0;
615}
616
617/*
618 * compute NI group address, based on the current hostname setting.
619 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
620 *
621 * when ifp == NULL, the caller is responsible for filling scopeid.
622 */
623int
624in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
625 struct in6_addr *in6)
626{
627 struct prison *pr;
628 const char *p;
629 u_char *q;
630 MD5_CTX ctxt;
631 u_int8_t digest[16];
632 char l;
633 char n[64]; /* a single label must not exceed 63 chars */
634
635 /*
636 * If no name is given and namelen is -1,
637 * we try to do the hostname lookup ourselves.
638 */
639 if (!name && namelen == -1) {
640 pr = curthread->td_ucred->cr_prison;
641 mtx_lock(&pr->pr_mtx);
642 name = pr->pr_host;
643 namelen = strlen(name);
644 } else
645 pr = NULL;
646 if (!name || !namelen) {
647 if (pr != NULL)
648 mtx_unlock(&pr->pr_mtx);
649 return -1;
650 }
651
652 p = name;
653 while (p && *p && *p != '.' && p - name < namelen)
654 p++;
655 if (p == name || p - name > sizeof(n) - 1) {
656 if (pr != NULL)
657 mtx_unlock(&pr->pr_mtx);
658 return -1; /* label too long */
659 }
660 l = p - name;
661 strncpy(n, name, l);
662 if (pr != NULL)
663 mtx_unlock(&pr->pr_mtx);
664 n[(int)l] = '\0';
665 for (q = n; *q; q++) {
666 if ('A' <= *q && *q <= 'Z')
667 *q = *q - 'A' + 'a';
668 }
669
670 /* generate 8 bytes of pseudo-random value. */
671 bzero(&ctxt, sizeof(ctxt));
672 MD5Init(&ctxt);
673 MD5Update(&ctxt, &l, sizeof(l));
674 MD5Update(&ctxt, n, l);
675 MD5Final(digest, &ctxt);
676
677 bzero(in6, sizeof(*in6));
678 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
679 in6->s6_addr8[11] = 2;
680 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
681 if (in6_setscope(in6, ifp, NULL))
682 return (-1); /* XXX: should not fail */
683
684 return 0;
685}
686
687/*
688 * XXX multiple loopback interface needs more care. for instance,
689 * nodelocal address needs to be configured onto only one of them.
690 * XXX multiple link-local address case
691 *
692 * altifp - secondary EUI64 source
693 */
694void
695in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
696{
697 INIT_VNET_INET6(ifp->if_vnet);
698 struct in6_ifaddr *ia;
699 struct in6_addr in6;
700
701 /* some of the interfaces are inherently not IPv6 capable */
702 switch (ifp->if_type) {
703 case IFT_PFLOG:
704 case IFT_PFSYNC:
705 case IFT_CARP:
706 return;
707 }
708
709 /*
710 * quirks based on interface type
711 */
712 switch (ifp->if_type) {
713#ifdef IFT_STF
714 case IFT_STF:
715 /*
716 * 6to4 interface is a very special kind of beast.
717 * no multicast, no linklocal. RFC2529 specifies how to make
718 * linklocals for 6to4 interface, but there's no use and
719 * it is rather harmful to have one.
720 */
721 goto statinit;
722#endif
723 default:
724 break;
725 }
726
727 /*
728 * usually, we require multicast capability to the interface
729 */
730 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
731 nd6log((LOG_INFO, "in6_ifattach: "
732 "%s is not multicast capable, IPv6 not enabled\n",
733 if_name(ifp)));
734 return;
735 }
736
737 /*
738 * assign loopback address for loopback interface.
739 * XXX multiple loopback interface case.
740 */
741 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
742 in6 = in6addr_loopback;
743 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
744 if (in6_ifattach_loopback(ifp) != 0)
745 return;
746 }
747 }
748
749 /*
750 * assign a link-local address, if there's none.
751 */
752 if (V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) {
753 ia = in6ifa_ifpforlinklocal(ifp, 0);
754 if (ia == NULL) {
755 if (in6_ifattach_linklocal(ifp, altifp) == 0) {
756 /* linklocal address assigned */
757 } else {
758 /* failed to assign linklocal address. bark? */
759 }
760 }
761 }
762
763#ifdef IFT_STF /* XXX */
764statinit:
765#endif
766
767 /* update dynamically. */
768 if (V_in6_maxmtu < ifp->if_mtu)
769 V_in6_maxmtu = ifp->if_mtu;
770}
771
772/*
773 * NOTE: in6_ifdetach() does not support loopback if at this moment.
774 * We don't need this function in bsdi, because interfaces are never removed
775 * from the ifnet list in bsdi.
776 */
777void
778in6_ifdetach(struct ifnet *ifp)
779{
780 INIT_VNET_NET(ifp->if_vnet);
781 INIT_VNET_INET(ifp->if_vnet);
782 INIT_VNET_INET6(ifp->if_vnet);
783 struct in6_ifaddr *ia, *oia;
784 struct ifaddr *ifa, *next;
780 INIT_VNET_INET(ifp->if_vnet);
781 INIT_VNET_INET6(ifp->if_vnet);
782 struct in6_ifaddr *ia, *oia;
783 struct ifaddr *ifa, *next;
784 struct radix_node_head *rnh;
785 struct rtentry *rt;
786 short rtflags;
787 struct sockaddr_in6 sin6;
788 struct in6_multi_mship *imm;
789
790 /* remove neighbor management table */
791 nd6_purge(ifp);
792
793 /* nuke any of IPv6 addresses we have */
794 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
795 if (ifa->ifa_addr->sa_family != AF_INET6)
796 continue;
797 in6_purgeaddr(ifa);
798 }
799
800 /* undo everything done by in6_ifattach(), just in case */
801 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
802 if (ifa->ifa_addr->sa_family != AF_INET6
803 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
804 continue;
805 }
806
807 ia = (struct in6_ifaddr *)ifa;
808
809 /*
810 * leave from multicast groups we have joined for the interface
811 */
812 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
813 LIST_REMOVE(imm, i6mm_chain);
814 in6_leavegroup(imm);
815 }
816
817 /* remove from the routing table */
818 if ((ia->ia_flags & IFA_ROUTE) &&
819 (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
820 rtflags = rt->rt_flags;
821 RTFREE_LOCKED(rt);
822 rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
823 (struct sockaddr *)&ia->ia_addr,
824 (struct sockaddr *)&ia->ia_prefixmask,
825 rtflags, (struct rtentry **)0);
826 }
827
828 /* remove from the linked list */
829 IF_ADDR_LOCK(ifp);
830 TAILQ_REMOVE(&ifp->if_addrhead, (struct ifaddr *)ia, ifa_link);
831 IF_ADDR_UNLOCK(ifp);
832 IFAFREE(&ia->ia_ifa);
833
834 /* also remove from the IPv6 address chain(itojun&jinmei) */
835 oia = ia;
836 if (oia == (ia = V_in6_ifaddr))
837 V_in6_ifaddr = ia->ia_next;
838 else {
839 while (ia->ia_next && (ia->ia_next != oia))
840 ia = ia->ia_next;
841 if (ia->ia_next)
842 ia->ia_next = oia->ia_next;
843 else {
844 nd6log((LOG_ERR,
845 "%s: didn't unlink in6ifaddr from list\n",
846 if_name(ifp)));
847 }
848 }
849
850 IFAFREE(&oia->ia_ifa);
851 }
852
853 in6_pcbpurgeif0(&V_udbinfo, ifp);
854 in6_pcbpurgeif0(&V_ripcbinfo, ifp);
855 /* leave from all multicast groups joined */
856 in6_purgemaddrs(ifp);
857
858 /*
859 * remove neighbor management table. we call it twice just to make
860 * sure we nuke everything. maybe we need just one call.
861 * XXX: since the first call did not release addresses, some prefixes
862 * might remain. We should call nd6_purge() again to release the
863 * prefixes after removing all addresses above.
864 * (Or can we just delay calling nd6_purge until at this point?)
865 */
866 nd6_purge(ifp);
867
868 /* remove route to link-local allnodes multicast (ff02::1) */
869 bzero(&sin6, sizeof(sin6));
870 sin6.sin6_len = sizeof(struct sockaddr_in6);
871 sin6.sin6_family = AF_INET6;
872 sin6.sin6_addr = in6addr_linklocal_allnodes;
873 if (in6_setscope(&sin6.sin6_addr, ifp, NULL))
874 /* XXX: should not fail */
875 return;
876 /* XXX grab lock first to avoid LOR */
785 struct rtentry *rt;
786 short rtflags;
787 struct sockaddr_in6 sin6;
788 struct in6_multi_mship *imm;
789
790 /* remove neighbor management table */
791 nd6_purge(ifp);
792
793 /* nuke any of IPv6 addresses we have */
794 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
795 if (ifa->ifa_addr->sa_family != AF_INET6)
796 continue;
797 in6_purgeaddr(ifa);
798 }
799
800 /* undo everything done by in6_ifattach(), just in case */
801 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
802 if (ifa->ifa_addr->sa_family != AF_INET6
803 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
804 continue;
805 }
806
807 ia = (struct in6_ifaddr *)ifa;
808
809 /*
810 * leave from multicast groups we have joined for the interface
811 */
812 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
813 LIST_REMOVE(imm, i6mm_chain);
814 in6_leavegroup(imm);
815 }
816
817 /* remove from the routing table */
818 if ((ia->ia_flags & IFA_ROUTE) &&
819 (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
820 rtflags = rt->rt_flags;
821 RTFREE_LOCKED(rt);
822 rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
823 (struct sockaddr *)&ia->ia_addr,
824 (struct sockaddr *)&ia->ia_prefixmask,
825 rtflags, (struct rtentry **)0);
826 }
827
828 /* remove from the linked list */
829 IF_ADDR_LOCK(ifp);
830 TAILQ_REMOVE(&ifp->if_addrhead, (struct ifaddr *)ia, ifa_link);
831 IF_ADDR_UNLOCK(ifp);
832 IFAFREE(&ia->ia_ifa);
833
834 /* also remove from the IPv6 address chain(itojun&jinmei) */
835 oia = ia;
836 if (oia == (ia = V_in6_ifaddr))
837 V_in6_ifaddr = ia->ia_next;
838 else {
839 while (ia->ia_next && (ia->ia_next != oia))
840 ia = ia->ia_next;
841 if (ia->ia_next)
842 ia->ia_next = oia->ia_next;
843 else {
844 nd6log((LOG_ERR,
845 "%s: didn't unlink in6ifaddr from list\n",
846 if_name(ifp)));
847 }
848 }
849
850 IFAFREE(&oia->ia_ifa);
851 }
852
853 in6_pcbpurgeif0(&V_udbinfo, ifp);
854 in6_pcbpurgeif0(&V_ripcbinfo, ifp);
855 /* leave from all multicast groups joined */
856 in6_purgemaddrs(ifp);
857
858 /*
859 * remove neighbor management table. we call it twice just to make
860 * sure we nuke everything. maybe we need just one call.
861 * XXX: since the first call did not release addresses, some prefixes
862 * might remain. We should call nd6_purge() again to release the
863 * prefixes after removing all addresses above.
864 * (Or can we just delay calling nd6_purge until at this point?)
865 */
866 nd6_purge(ifp);
867
868 /* remove route to link-local allnodes multicast (ff02::1) */
869 bzero(&sin6, sizeof(sin6));
870 sin6.sin6_len = sizeof(struct sockaddr_in6);
871 sin6.sin6_family = AF_INET6;
872 sin6.sin6_addr = in6addr_linklocal_allnodes;
873 if (in6_setscope(&sin6.sin6_addr, ifp, NULL))
874 /* XXX: should not fail */
875 return;
876 /* XXX grab lock first to avoid LOR */
877 if (V_rt_tables[0][AF_INET6] != NULL) {
878 RADIX_NODE_HEAD_LOCK(V_rt_tables[0][AF_INET6]);
877 rnh = rt_tables_get_rnh(0, AF_INET6);
878 if (rnh != NULL) {
879 RADIX_NODE_HEAD_LOCK(rnh);
879 rt = rtalloc1((struct sockaddr *)&sin6, 0, RTF_RNH_LOCKED);
880 if (rt) {
881 if (rt->rt_ifp == ifp)
882 rtexpunge(rt);
883 RTFREE_LOCKED(rt);
884 }
880 rt = rtalloc1((struct sockaddr *)&sin6, 0, RTF_RNH_LOCKED);
881 if (rt) {
882 if (rt->rt_ifp == ifp)
883 rtexpunge(rt);
884 RTFREE_LOCKED(rt);
885 }
885 RADIX_NODE_HEAD_UNLOCK(V_rt_tables[0][AF_INET6]);
886 RADIX_NODE_HEAD_UNLOCK(rnh);
886 }
887}
888
889int
890in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
891 const u_int8_t *baseid, int generate)
892{
893 u_int8_t nullbuf[8];
894 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
895
896 bzero(nullbuf, sizeof(nullbuf));
897 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
898 /* we've never created a random ID. Create a new one. */
899 generate = 1;
900 }
901
902 if (generate) {
903 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
904
905 /* generate_tmp_ifid will update seedn and buf */
906 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
907 ndi->randomid);
908 }
909 bcopy(ndi->randomid, retbuf, 8);
910
911 return (0);
912}
913
914void
915in6_tmpaddrtimer(void *arg)
916{
917 CURVNET_SET((struct vnet *) arg);
918 INIT_VNET_NET(curvnet);
919 INIT_VNET_INET6(curvnet);
920 struct nd_ifinfo *ndi;
921 u_int8_t nullbuf[8];
922 struct ifnet *ifp;
923
924 callout_reset(&V_in6_tmpaddrtimer_ch,
925 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
926 V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
927
928 bzero(nullbuf, sizeof(nullbuf));
929 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
930 ifp = TAILQ_NEXT(ifp, if_list)) {
931 ndi = ND_IFINFO(ifp);
932 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
933 /*
934 * We've been generating a random ID on this interface.
935 * Create a new one.
936 */
937 (void)generate_tmp_ifid(ndi->randomseed0,
938 ndi->randomseed1, ndi->randomid);
939 }
940 }
941
942 CURVNET_RESTORE();
943}
944
945static void
946in6_purgemaddrs(struct ifnet *ifp)
947{
948 LIST_HEAD(,in6_multi) purgeinms;
949 struct in6_multi *inm, *tinm;
950 struct ifmultiaddr *ifma;
951
952 LIST_INIT(&purgeinms);
953 IN6_MULTI_LOCK();
954
955 /*
956 * Extract list of in6_multi associated with the detaching ifp
957 * which the PF_INET6 layer is about to release.
958 * We need to do this as IF_ADDR_LOCK() may be re-acquired
959 * by code further down.
960 */
961 IF_ADDR_LOCK(ifp);
962 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
963 if (ifma->ifma_addr->sa_family != AF_INET6 ||
964 ifma->ifma_protospec == NULL)
965 continue;
966 inm = (struct in6_multi *)ifma->ifma_protospec;
967 LIST_INSERT_HEAD(&purgeinms, inm, in6m_entry);
968 }
969 IF_ADDR_UNLOCK(ifp);
970
971 LIST_FOREACH_SAFE(inm, &purgeinms, in6m_entry, tinm) {
972 LIST_REMOVE(inm, in6m_entry);
973 in6m_release_locked(inm);
974 }
975 mld_ifdetach(ifp);
976
977 IN6_MULTI_UNLOCK();
978}
887 }
888}
889
890int
891in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
892 const u_int8_t *baseid, int generate)
893{
894 u_int8_t nullbuf[8];
895 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
896
897 bzero(nullbuf, sizeof(nullbuf));
898 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
899 /* we've never created a random ID. Create a new one. */
900 generate = 1;
901 }
902
903 if (generate) {
904 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
905
906 /* generate_tmp_ifid will update seedn and buf */
907 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
908 ndi->randomid);
909 }
910 bcopy(ndi->randomid, retbuf, 8);
911
912 return (0);
913}
914
915void
916in6_tmpaddrtimer(void *arg)
917{
918 CURVNET_SET((struct vnet *) arg);
919 INIT_VNET_NET(curvnet);
920 INIT_VNET_INET6(curvnet);
921 struct nd_ifinfo *ndi;
922 u_int8_t nullbuf[8];
923 struct ifnet *ifp;
924
925 callout_reset(&V_in6_tmpaddrtimer_ch,
926 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
927 V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
928
929 bzero(nullbuf, sizeof(nullbuf));
930 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
931 ifp = TAILQ_NEXT(ifp, if_list)) {
932 ndi = ND_IFINFO(ifp);
933 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
934 /*
935 * We've been generating a random ID on this interface.
936 * Create a new one.
937 */
938 (void)generate_tmp_ifid(ndi->randomseed0,
939 ndi->randomseed1, ndi->randomid);
940 }
941 }
942
943 CURVNET_RESTORE();
944}
945
946static void
947in6_purgemaddrs(struct ifnet *ifp)
948{
949 LIST_HEAD(,in6_multi) purgeinms;
950 struct in6_multi *inm, *tinm;
951 struct ifmultiaddr *ifma;
952
953 LIST_INIT(&purgeinms);
954 IN6_MULTI_LOCK();
955
956 /*
957 * Extract list of in6_multi associated with the detaching ifp
958 * which the PF_INET6 layer is about to release.
959 * We need to do this as IF_ADDR_LOCK() may be re-acquired
960 * by code further down.
961 */
962 IF_ADDR_LOCK(ifp);
963 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
964 if (ifma->ifma_addr->sa_family != AF_INET6 ||
965 ifma->ifma_protospec == NULL)
966 continue;
967 inm = (struct in6_multi *)ifma->ifma_protospec;
968 LIST_INSERT_HEAD(&purgeinms, inm, in6m_entry);
969 }
970 IF_ADDR_UNLOCK(ifp);
971
972 LIST_FOREACH_SAFE(inm, &purgeinms, in6m_entry, tinm) {
973 LIST_REMOVE(inm, in6m_entry);
974 in6m_release_locked(inm);
975 }
976 mld_ifdetach(ifp);
977
978 IN6_MULTI_UNLOCK();
979}