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