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