1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * $KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $
32 */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/counter.h>
37 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 #include <sys/sockio.h>
40 #include <sys/jail.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/proc.h>
44 #include <sys/rmlock.h>
45 #include <sys/syslog.h>
46 #include <sys/md5.h>
47
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/if_dl.h>
51 #include <net/if_private.h>
52 #include <net/if_types.h>
53 #include <net/route.h>
54 #include <net/vnet.h>
55
56 #include <netinet/in.h>
57 #include <netinet/in_var.h>
58 #include <netinet/if_ether.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/udp.h>
62 #include <netinet/udp_var.h>
63
64 #include <netinet/ip6.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet6/in6_pcb.h>
68 #include <netinet6/in6_ifattach.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/nd6.h>
71 #include <netinet6/mld6_var.h>
72 #include <netinet6/scope6_var.h>
73
74 #include <crypto/sha2/sha256.h>
75 #include <machine/atomic.h>
76
77 #ifdef IP6_AUTO_LINKLOCAL
78 VNET_DEFINE(int, ip6_auto_linklocal) = IP6_AUTO_LINKLOCAL;
79 #else
80 VNET_DEFINE(int, ip6_auto_linklocal) = 1; /* enabled by default */
81 #endif
82
83 VNET_DEFINE(struct callout, in6_tmpaddrtimer_ch);
84 #define V_in6_tmpaddrtimer_ch VNET(in6_tmpaddrtimer_ch)
85
86 VNET_DEFINE(int, ip6_stableaddr_netifsource) = IP6_STABLEADDR_NETIFSRC_NAME; /* Use interface name by default */
87
88 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
89 #define V_ripcbinfo VNET(ripcbinfo)
90
91 static int get_rand_ifid(struct ifnet *, struct in6_addr *);
92 static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
93 static int in6_ifattach_loopback(struct ifnet *);
94 static void in6_purgemaddrs(struct ifnet *);
95
96 #define EUI64_GBIT 0x01
97 #define EUI64_UBIT 0x02
98 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
99 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
100 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
101 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
102 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
103
104 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
105 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
106
107 #define HMAC_IPAD 0x36
108 #define HMAC_OPAD 0x5C
109
110 /*
111 * Generate a last-resort interface identifier, when the machine has no
112 * IEEE802/EUI64 address sources.
113 * The goal here is to get an interface identifier that is
114 * (1) random enough and (2) does not change across reboot.
115 * We currently use MD5(hostname) for it.
116 *
117 * in6 - upper 64bits are preserved
118 */
119 static int
get_rand_ifid(struct ifnet * ifp,struct in6_addr * in6)120 get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
121 {
122 MD5_CTX ctxt;
123 struct prison *pr;
124 u_int8_t digest[16];
125 int hostnamelen;
126
127 pr = curthread->td_ucred->cr_prison;
128 mtx_lock(&pr->pr_mtx);
129 hostnamelen = strlen(pr->pr_hostname);
130 #if 0
131 /* we need at least several letters as seed for ifid */
132 if (hostnamelen < 3) {
133 mtx_unlock(&pr->pr_mtx);
134 return -1;
135 }
136 #endif
137
138 /* generate 8 bytes of pseudo-random value. */
139 bzero(&ctxt, sizeof(ctxt));
140 MD5Init(&ctxt);
141 MD5Update(&ctxt, pr->pr_hostname, hostnamelen);
142 mtx_unlock(&pr->pr_mtx);
143 MD5Final(digest, &ctxt);
144
145 /* assumes sizeof(digest) > sizeof(ifid) */
146 bcopy(digest, &in6->s6_addr[8], 8);
147
148 /* make sure to set "u" bit to local, and "g" bit to individual. */
149 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
150 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
151
152 /* convert EUI64 into IPv6 interface identifier */
153 EUI64_TO_IFID(in6);
154
155 return 0;
156 }
157
158
159 /**
160 * Get interface link level sockaddr
161 */
162 static struct sockaddr_dl *
get_interface_link_level(struct ifnet * ifp)163 get_interface_link_level(struct ifnet *ifp)
164 {
165 struct ifaddr *ifa;
166 struct sockaddr_dl *sdl;
167
168 NET_EPOCH_ASSERT();
169
170 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
171 if (ifa->ifa_addr->sa_family != AF_LINK)
172 continue;
173 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
174 if (sdl == NULL)
175 continue;
176 if (sdl->sdl_alen == 0)
177 continue;
178
179 return sdl;
180 }
181
182 return NULL;
183 }
184
185 /*
186 * Get hwaddr from link interface
187 */
188 static uint8_t *
in6_get_interface_hwaddr(struct ifnet * ifp,size_t * len)189 in6_get_interface_hwaddr(struct ifnet *ifp, size_t *len)
190 {
191 struct sockaddr_dl *sdl;
192 u_int8_t *addr;
193 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
194 static u_int8_t allone[8] =
195 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
196
197 sdl = get_interface_link_level(ifp);
198 if (sdl == NULL)
199 return (NULL);
200
201 addr = LLADDR(sdl);
202 *len = sdl->sdl_alen;
203
204 /* get EUI64 */
205 switch (ifp->if_type) {
206 case IFT_BRIDGE:
207 case IFT_ETHER:
208 case IFT_L2VLAN:
209 case IFT_ATM:
210 case IFT_IEEE1394:
211 /* IEEE802/EUI64 cases - what others? */
212 /* IEEE1394 uses 16byte length address starting with EUI64 */
213 if (*len > 8)
214 *len = 8;
215
216 /* look at IEEE802/EUI64 only */
217 if (*len != 8 && *len != 6)
218 return (NULL);
219
220 /*
221 * check for invalid MAC address - on bsdi, we see it a lot
222 * since wildboar configures all-zero MAC on pccard before
223 * card insertion.
224 */
225 if (memcmp(addr, allzero, *len) == 0 || memcmp(addr, allone, *len) == 0)
226 return (NULL);
227
228 break;
229
230 case IFT_GIF:
231 case IFT_STF:
232 /*
233 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
234 * however, IPv4 address is not very suitable as unique
235 * identifier source (can be renumbered).
236 * we don't do this.
237 */
238 return (NULL);
239
240 case IFT_INFINIBAND:
241 if (*len != 20)
242 return (NULL);
243 *len = 8;
244 addr += 12;
245 break;
246
247 default:
248 return (NULL);
249 }
250
251 return addr;
252 }
253
254 /*
255 * Get interface identifier for the specified interface.
256 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
257 *
258 * in6 - upper 64bits are preserved
259 */
260 int
in6_get_hw_ifid(struct ifnet * ifp,struct in6_addr * in6)261 in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
262 {
263 size_t hwaddr_len;
264 uint8_t *hwaddr;
265 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
266
267 hwaddr = in6_get_interface_hwaddr(ifp, &hwaddr_len);
268 if (hwaddr == NULL || (hwaddr_len != 6 && hwaddr_len != 8))
269 return -1;
270
271 /* make EUI64 address */
272 if (hwaddr_len == 8)
273 memcpy(&in6->s6_addr[8], hwaddr, 8);
274 else if (hwaddr_len == 6) {
275 in6->s6_addr[8] = hwaddr[0];
276 in6->s6_addr[9] = hwaddr[1];
277 in6->s6_addr[10] = hwaddr[2];
278 in6->s6_addr[11] = 0xff;
279 in6->s6_addr[12] = 0xfe;
280 in6->s6_addr[13] = hwaddr[3];
281 in6->s6_addr[14] = hwaddr[4];
282 in6->s6_addr[15] = hwaddr[5];
283 }
284
285 /* sanity check: g bit must not indicate "group" */
286 if (EUI64_GROUP(in6))
287 return -1;
288
289 /* convert EUI64 into IPv6 interface identifier */
290 EUI64_TO_IFID(in6);
291
292 /*
293 * sanity check: ifid must not be all zero, avoid conflict with
294 * subnet router anycast
295 */
296 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
297 bcmp(&in6->s6_addr[9], allzero, 7) == 0)
298 return -1;
299
300 return 0;
301 }
302
303 /*
304 * Validate generated interface id to make sure it does not fall in any reserved range:
305 *
306 * https://www.iana.org/assignments/ipv6-interface-ids/ipv6-interface-ids.xhtml
307 */
308 static bool
validate_ifid(uint8_t * iid)309 validate_ifid(uint8_t *iid)
310 {
311 static uint8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
312 static uint8_t reserved_eth[5] = { 0x02, 0x00, 0x5E, 0xFF, 0xFE };
313 static uint8_t reserved_anycast[7] = { 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
314
315 /* Subnet-Router Anycast (RFC 4291)*/
316 if (memcmp(iid, allzero, 8) == 0)
317 return (false);
318
319 /*
320 * Reserved IPv6 Interface Identifiers corresponding to the IANA Ethernet Block (RFC 4291)
321 * and
322 * Proxy Mobile IPv6 (RFC 6543)
323 */
324 if (memcmp(iid, reserved_eth, 5) == 0)
325 return (false);
326
327 /* Reserved Subnet Anycast Addresses (RFC 2526) */
328 if (memcmp(iid, reserved_anycast, 7) == 0 && iid[7] >= 0x80)
329 return (false);
330
331 return (true);
332 }
333
334 /*
335 * Get interface identifier for the specified interface, according to
336 * RFC 7217 Stable and Opaque IDs with SLAAC, using HMAC-SHA256 digest.
337 *
338 * in6 - upper 64bits are preserved
339 */
340 bool
in6_get_stableifid(struct ifnet * ifp,struct in6_addr * in6,int prefixlen)341 in6_get_stableifid(struct ifnet *ifp, struct in6_addr *in6, int prefixlen)
342 {
343 struct sockaddr_dl *sdl;
344 const uint8_t *netiface;
345 size_t netiface_len, hostuuid_len;
346 uint8_t hostuuid[HOSTUUIDLEN + 1], hmac_key[SHA256_BLOCK_LENGTH],
347 hk_ipad[SHA256_BLOCK_LENGTH], hk_opad[SHA256_BLOCK_LENGTH];
348 uint64_t dad_failures;
349 SHA256_CTX ctxt;
350
351 switch (V_ip6_stableaddr_netifsource) {
352 case IP6_STABLEADDR_NETIFSRC_ID:
353 sdl = get_interface_link_level(ifp);
354 if (sdl == NULL)
355 return (false);
356 netiface = (uint8_t *)&LLINDEX(sdl);
357 netiface_len = sizeof(u_short); /* real return type of LLINDEX */
358 break;
359
360 case IP6_STABLEADDR_NETIFSRC_MAC:
361 netiface = in6_get_interface_hwaddr(ifp, &netiface_len);
362 if (netiface == NULL)
363 return (false);
364 break;
365
366 case IP6_STABLEADDR_NETIFSRC_NAME:
367 default:
368 netiface = (const uint8_t *)if_name(ifp);
369 netiface_len = strlen(netiface);
370 break;
371 }
372
373 /* Use hostuuid as constant "secret" key */
374 getcredhostuuid(curthread->td_ucred, hostuuid, sizeof(hostuuid));
375 if (strncmp(hostuuid, DEFAULT_HOSTUUID, sizeof(hostuuid)) == 0) {
376 // If hostuuid is not set, use a random value
377 arc4rand(hostuuid, HOSTUUIDLEN, 0);
378 hostuuid[HOSTUUIDLEN] = '\0';
379 }
380 hostuuid_len = strlen(hostuuid);
381
382 dad_failures = atomic_load_int(&DAD_FAILURES(ifp));
383
384 /*
385 * RFC 7217 section 7
386 *
387 * default max retries
388 */
389 if (dad_failures > V_ip6_stableaddr_maxretries)
390 return (false);
391
392 /*
393 * Use hostuuid as basis for HMAC key
394 */
395 memset(hmac_key, 0, sizeof(hmac_key));
396 if (hostuuid_len <= SHA256_BLOCK_LENGTH) {
397 /* copy to hmac key variable, zero padded */
398 memcpy(hmac_key, hostuuid, hostuuid_len);
399 } else {
400 /* if longer than block length, use hash of the value, zero padded */
401 SHA256_Init(&ctxt);
402 SHA256_Update(&ctxt, hostuuid, hostuuid_len);
403 SHA256_Final(hmac_key, &ctxt);
404 }
405 /* XOR key with ipad and opad values */
406 for (uint16_t i = 0; i < sizeof(hmac_key); i++) {
407 hk_ipad[i] = hmac_key[i] ^ HMAC_IPAD;
408 hk_opad[i] = hmac_key[i] ^ HMAC_OPAD;
409 }
410
411 /*
412 * Generate interface id in a loop, adding an offset to be factored in the hash function.
413 * This is necessary, because if the generated interface id happens to be invalid we
414 * want to force the hash function to generate a different one, otherwise we would end up
415 * in an infinite loop trying the same invalid interface id over and over again.
416 *
417 * Using an uint8 counter for the offset, so limit iteration at UINT8_MAX. This is a safety
418 * measure, this will never iterate more than once or twice in practice.
419 */
420 for(uint8_t offset = 0; offset < UINT8_MAX; offset++) {
421 uint8_t digest[SHA256_DIGEST_LENGTH];
422
423 /* Calculate inner hash */
424 SHA256_Init(&ctxt);
425 SHA256_Update(&ctxt, hk_ipad, sizeof(hk_ipad));
426 SHA256_Update(&ctxt, in6->s6_addr, prefixlen / 8);
427 SHA256_Update(&ctxt, netiface, netiface_len);
428 SHA256_Update(&ctxt, (uint8_t *)&dad_failures, 8);
429 SHA256_Update(&ctxt, hostuuid, hostuuid_len);
430 SHA256_Update(&ctxt, &offset, 1);
431 SHA256_Final(digest, &ctxt);
432
433 /* Calculate outer hash */
434 SHA256_Init(&ctxt);
435 SHA256_Update(&ctxt, hk_opad, sizeof(hk_opad));
436 SHA256_Update(&ctxt, digest, sizeof(digest));
437 SHA256_Final(digest, &ctxt);
438
439 if (validate_ifid(digest)) {
440 /* assumes sizeof(digest) > sizeof(ifid) */
441 memcpy(&in6->s6_addr[8], digest, 8);
442
443 return (true);
444 }
445 }
446
447 return (false);
448 }
449
450 /*
451 * Get interface identifier for the specified interface. If it is not
452 * available on ifp0, borrow interface identifier from other information
453 * sources.
454 *
455 * altifp - secondary EUI64 source
456 */
457 int
in6_get_ifid(struct ifnet * ifp0,struct ifnet * altifp,struct in6_addr * in6)458 in6_get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
459 struct in6_addr *in6)
460 {
461 struct ifnet *ifp;
462
463 NET_EPOCH_ASSERT();
464
465 /* first, try to get it from the interface itself, with stable algorithm, if configured */
466 if ((ND_IFINFO(ifp0)->flags & ND6_IFF_STABLEADDR) && in6_get_stableifid(ifp0, in6, 64) == 0) {
467 nd6log((LOG_DEBUG, "%s: got interface identifier from itself (stable private)\n",
468 if_name(ifp0)));
469 goto success;
470 }
471
472 /* then/otherwise try to get it from the interface itself */
473 if (in6_get_hw_ifid(ifp0, in6) == 0) {
474 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
475 if_name(ifp0)));
476 goto success;
477 }
478
479 /* try secondary EUI64 source. this basically is for ATM PVC */
480 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
481 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
482 if_name(ifp0), if_name(altifp)));
483 goto success;
484 }
485
486 /* next, try to get it from some other hardware interface */
487 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
488 if (ifp == ifp0)
489 continue;
490 if (in6_get_hw_ifid(ifp, in6) != 0)
491 continue;
492
493 /*
494 * to borrow ifid from other interface, ifid needs to be
495 * globally unique
496 */
497 if (IFID_UNIVERSAL(in6)) {
498 nd6log((LOG_DEBUG,
499 "%s: borrow interface identifier from %s\n",
500 if_name(ifp0), if_name(ifp)));
501 goto success;
502 }
503 }
504
505 /* last resort: get from random number source */
506 if (get_rand_ifid(ifp, in6) == 0) {
507 nd6log((LOG_DEBUG,
508 "%s: interface identifier generated by random number\n",
509 if_name(ifp0)));
510 goto success;
511 }
512
513 printf("%s: failed to get interface identifier\n", if_name(ifp0));
514 return -1;
515
516 success:
517 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
518 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
519 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
520 in6->s6_addr[14], in6->s6_addr[15]));
521 return 0;
522 }
523
524 /*
525 * altifp - secondary EUI64 source
526 */
527 static int
in6_ifattach_linklocal(struct ifnet * ifp,struct ifnet * altifp)528 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
529 {
530 struct in6_ifaddr *ia;
531 struct in6_aliasreq ifra;
532 struct nd_prefixctl pr0;
533 struct epoch_tracker et;
534 struct nd_prefix *pr;
535 int error;
536
537 /*
538 * configure link-local address.
539 */
540 in6_prepare_ifra(&ifra, NULL, &in6mask64);
541
542 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
543 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
544 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
545 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
546 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
547 } else {
548 NET_EPOCH_ENTER(et);
549 error = in6_get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr);
550 NET_EPOCH_EXIT(et);
551 if (error != 0) {
552 nd6log((LOG_ERR,
553 "%s: no ifid available\n", if_name(ifp)));
554 return (-1);
555 }
556 }
557 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
558 return (-1);
559
560 /* link-local addresses should NEVER expire. */
561 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
562 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
563
564 /*
565 * Now call in6_update_ifa() to do a bunch of procedures to configure
566 * a link-local address. We can set the 3rd argument to NULL, because
567 * we know there's no other link-local address on the interface
568 * and therefore we are adding one (instead of updating one).
569 */
570 if ((error = in6_update_ifa(ifp, &ifra, NULL,
571 IN6_IFAUPDATE_DADDELAY)) != 0) {
572 /*
573 * XXX: When the interface does not support IPv6, this call
574 * would fail in the SIOCSIFADDR ioctl. I believe the
575 * notification is rather confusing in this case, so just
576 * suppress it. (jinmei@kame.net 20010130)
577 */
578 if (error != EAFNOSUPPORT)
579 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
580 "configure a link-local address on %s "
581 "(errno=%d)\n",
582 if_name(ifp), error));
583 return (-1);
584 }
585
586 NET_EPOCH_ENTER(et);
587 ia = in6ifa_ifpforlinklocal(ifp, 0);
588 NET_EPOCH_EXIT(et);
589 if (ia == NULL) {
590 /*
591 * Another thread removed the address that we just added.
592 * This should be rare, but it happens.
593 */
594 nd6log((LOG_NOTICE, "%s: %s: new link-local address "
595 "disappeared\n", __func__, if_name(ifp)));
596 return (-1);
597 }
598 ifa_free(&ia->ia_ifa);
599
600 /*
601 * Make the link-local prefix (fe80::%link/64) as on-link.
602 * Since we'd like to manage prefixes separately from addresses,
603 * we make an ND6 prefix structure for the link-local prefix,
604 * and add it to the prefix list as a never-expire prefix.
605 * XXX: this change might affect some existing code base...
606 */
607 bzero(&pr0, sizeof(pr0));
608 pr0.ndpr_ifp = ifp;
609 /* this should be 64 at this moment. */
610 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
611 pr0.ndpr_prefix = ifra.ifra_addr;
612 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
613 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, &in6mask64);
614 /*
615 * Initialize parameters. The link-local prefix must always be
616 * on-link, and its lifetimes never expire.
617 */
618 pr0.ndpr_raf_onlink = 1;
619 pr0.ndpr_raf_auto = 1; /* probably meaningless */
620 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
621 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
622 /*
623 * Since there is no other link-local addresses, nd6_prefix_lookup()
624 * probably returns NULL. However, we cannot always expect the result.
625 * For example, if we first remove the (only) existing link-local
626 * address, and then reconfigure another one, the prefix is still
627 * valid with referring to the old link-local address.
628 */
629 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
630 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
631 return (error);
632 /* Reference prefix */
633 ia->ia6_ndpr = pr;
634 pr->ndpr_addrcnt++;
635 } else
636 nd6_prefix_rele(pr);
637
638 return 0;
639 }
640
641 /*
642 * ifp - must be IFT_LOOP
643 */
644 static int
in6_ifattach_loopback(struct ifnet * ifp)645 in6_ifattach_loopback(struct ifnet *ifp)
646 {
647 struct in6_aliasreq ifra;
648 int error;
649
650 in6_prepare_ifra(&ifra, &in6addr_loopback, &in6mask128);
651
652 /*
653 * Always initialize ia_dstaddr (= broadcast address) to loopback
654 * address. Follows IPv4 practice - see in_ifinit().
655 */
656 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
657 ifra.ifra_dstaddr.sin6_family = AF_INET6;
658 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
659
660 /* the loopback address should NEVER expire. */
661 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
662 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
663
664 /*
665 * We are sure that this is a newly assigned address, so we can set
666 * NULL to the 3rd arg.
667 */
668 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
669 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
670 "the loopback address on %s (errno=%d)\n",
671 if_name(ifp), error));
672 return (-1);
673 }
674
675 return 0;
676 }
677
678 /*
679 * compute NI group address, based on the current hostname setting.
680 * see RFC 4620.
681 *
682 * when ifp == NULL, the caller is responsible for filling scopeid.
683 *
684 * If oldmcprefix == 1, FF02:0:0:0:0:2::/96 is used for NI group address
685 * while it is FF02:0:0:0:0:2:FF00::/104 in RFC 4620.
686 */
687 static int
in6_nigroup0(struct ifnet * ifp,const char * name,int namelen,struct in6_addr * in6,int oldmcprefix)688 in6_nigroup0(struct ifnet *ifp, const char *name, int namelen,
689 struct in6_addr *in6, int oldmcprefix)
690 {
691 struct prison *pr;
692 const char *p;
693 u_char *q;
694 MD5_CTX ctxt;
695 u_int8_t digest[16];
696 char l;
697 char n[64]; /* a single label must not exceed 63 chars */
698
699 /*
700 * If no name is given and namelen is -1,
701 * we try to do the hostname lookup ourselves.
702 */
703 if (!name && namelen == -1) {
704 pr = curthread->td_ucred->cr_prison;
705 mtx_lock(&pr->pr_mtx);
706 name = pr->pr_hostname;
707 namelen = strlen(name);
708 } else
709 pr = NULL;
710 if (!name || !namelen) {
711 if (pr != NULL)
712 mtx_unlock(&pr->pr_mtx);
713 return -1;
714 }
715
716 p = name;
717 while (p && *p && *p != '.' && p - name < namelen)
718 p++;
719 if (p == name || p - name > sizeof(n) - 1) {
720 if (pr != NULL)
721 mtx_unlock(&pr->pr_mtx);
722 return -1; /* label too long */
723 }
724 l = p - name;
725 strncpy(n, name, l);
726 if (pr != NULL)
727 mtx_unlock(&pr->pr_mtx);
728 n[(int)l] = '\0';
729 for (q = n; *q; q++) {
730 if ('A' <= *q && *q <= 'Z')
731 *q = *q - 'A' + 'a';
732 }
733
734 /* generate 16 bytes of pseudo-random value. */
735 bzero(&ctxt, sizeof(ctxt));
736 MD5Init(&ctxt);
737 MD5Update(&ctxt, &l, sizeof(l));
738 MD5Update(&ctxt, n, l);
739 MD5Final(digest, &ctxt);
740
741 bzero(in6, sizeof(*in6));
742 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
743 in6->s6_addr8[11] = 2;
744 if (oldmcprefix == 0) {
745 in6->s6_addr8[12] = 0xff;
746 /* Copy the first 24 bits of 128-bit hash into the address. */
747 bcopy(digest, &in6->s6_addr8[13], 3);
748 } else {
749 /* Copy the first 32 bits of 128-bit hash into the address. */
750 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
751 }
752 if (in6_setscope(in6, ifp, NULL))
753 return (-1); /* XXX: should not fail */
754
755 return 0;
756 }
757
758 int
in6_nigroup(struct ifnet * ifp,const char * name,int namelen,struct in6_addr * in6)759 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
760 struct in6_addr *in6)
761 {
762
763 return (in6_nigroup0(ifp, name, namelen, in6, 0));
764 }
765
766 int
in6_nigroup_oldmcprefix(struct ifnet * ifp,const char * name,int namelen,struct in6_addr * in6)767 in6_nigroup_oldmcprefix(struct ifnet *ifp, const char *name, int namelen,
768 struct in6_addr *in6)
769 {
770
771 return (in6_nigroup0(ifp, name, namelen, in6, 1));
772 }
773
774 /*
775 * XXX multiple loopback interface needs more care. for instance,
776 * nodelocal address needs to be configured onto only one of them.
777 * XXX multiple link-local address case
778 *
779 * altifp - secondary EUI64 source
780 */
781 void
in6_ifattach(struct ifnet * ifp,struct ifnet * altifp)782 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
783 {
784 struct in6_ifaddr *ia;
785
786 if (ifp->if_afdata[AF_INET6] == NULL)
787 return;
788 /*
789 * quirks based on interface type
790 */
791 switch (ifp->if_type) {
792 case IFT_STF:
793 /*
794 * 6to4 interface is a very special kind of beast.
795 * no multicast, no linklocal. RFC2529 specifies how to make
796 * linklocals for 6to4 interface, but there's no use and
797 * it is rather harmful to have one.
798 */
799 ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
800 ND_IFINFO(ifp)->flags |= ND6_IFF_NO_DAD;
801 break;
802 default:
803 break;
804 }
805
806 /*
807 * usually, we require multicast capability to the interface
808 */
809 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
810 nd6log((LOG_INFO, "in6_ifattach: "
811 "%s is not multicast capable, IPv6 not enabled\n",
812 if_name(ifp)));
813 return;
814 }
815
816 /*
817 * assign loopback address for loopback interface.
818 */
819 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
820 /*
821 * check that loopback address doesn't exist yet.
822 */
823 ia = in6ifa_ifwithaddr(&in6addr_loopback, 0, false);
824 if (ia == NULL)
825 in6_ifattach_loopback(ifp);
826 }
827
828 /*
829 * assign a link-local address, if there's none.
830 */
831 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
832 ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
833 struct epoch_tracker et;
834
835 NET_EPOCH_ENTER(et);
836 ia = in6ifa_ifpforlinklocal(ifp, 0);
837 NET_EPOCH_EXIT(et);
838 if (ia == NULL)
839 in6_ifattach_linklocal(ifp, altifp);
840 else
841 ifa_free(&ia->ia_ifa);
842 }
843 }
844
845 /*
846 * NOTE: in6_ifdetach() does not support loopback if at this moment.
847 *
848 * When shutting down a VNET we clean up layers top-down. In that case
849 * upper layer protocols (ulp) are cleaned up already and locks are destroyed
850 * and we must not call into these cleanup functions anymore, thus purgeulp
851 * is set to 0 in that case by in6_ifdetach_destroy().
852 * The normal case of destroying a (cloned) interface still needs to cleanup
853 * everything related to the interface and will have purgeulp set to 1.
854 */
855 static void
_in6_ifdetach(struct ifnet * ifp,int purgeulp)856 _in6_ifdetach(struct ifnet *ifp, int purgeulp)
857 {
858 struct ifaddr *ifa, *next;
859
860 if (ifp->if_afdata[AF_INET6] == NULL)
861 return;
862
863 /*
864 * nuke any of IPv6 addresses we have
865 */
866 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
867 if (ifa->ifa_addr->sa_family != AF_INET6)
868 continue;
869 in6_purgeaddr(ifa);
870 }
871 if (purgeulp) {
872 IN6_MULTI_LOCK();
873 in6_pcbpurgeif0(&V_udbinfo, ifp);
874 in6_pcbpurgeif0(&V_ulitecbinfo, ifp);
875 in6_pcbpurgeif0(&V_ripcbinfo, ifp);
876 IN6_MULTI_UNLOCK();
877 }
878 /* leave from all multicast groups joined */
879 in6_purgemaddrs(ifp);
880
881 /*
882 * Remove neighbor management table.
883 * Enabling the nd6_purge will panic on vmove for interfaces on VNET
884 * teardown as the IPv6 layer is cleaned up already and the locks
885 * are destroyed.
886 */
887 if (purgeulp)
888 nd6_purge(ifp);
889 }
890
891 void
in6_ifdetach(struct ifnet * ifp)892 in6_ifdetach(struct ifnet *ifp)
893 {
894
895 _in6_ifdetach(ifp, 1);
896 }
897
898 void
in6_ifdetach_destroy(struct ifnet * ifp)899 in6_ifdetach_destroy(struct ifnet *ifp)
900 {
901
902 _in6_ifdetach(ifp, 0);
903 }
904
905 void
in6_tmpaddrtimer(void * arg)906 in6_tmpaddrtimer(void *arg)
907 {
908 CURVNET_SET((struct vnet *) arg);
909
910 callout_reset(&V_in6_tmpaddrtimer_ch,
911 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
912 V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
913
914 CURVNET_RESTORE();
915 }
916
917 static void
in6_purgemaddrs(struct ifnet * ifp)918 in6_purgemaddrs(struct ifnet *ifp)
919 {
920 struct in6_multi_head inmh;
921
922 SLIST_INIT(&inmh);
923 IN6_MULTI_LOCK();
924 IN6_MULTI_LIST_LOCK();
925 mld_ifdetach(ifp, &inmh);
926 IN6_MULTI_LIST_UNLOCK();
927 IN6_MULTI_UNLOCK();
928 in6m_release_list_deferred(&inmh);
929
930 /*
931 * Make sure all multicast deletions invoking if_ioctl() are
932 * completed before returning. Else we risk accessing a freed
933 * ifnet structure pointer.
934 */
935 in6m_release_wait(NULL);
936 }
937
938 void
in6_ifattach_destroy(void)939 in6_ifattach_destroy(void)
940 {
941
942 callout_drain(&V_in6_tmpaddrtimer_ch);
943 }
944
945 static void
in6_ifattach_init(void * dummy)946 in6_ifattach_init(void *dummy)
947 {
948
949 /* Timer for regeneranation of temporary addresses randomize ID. */
950 callout_init(&V_in6_tmpaddrtimer_ch, 1);
951 callout_reset(&V_in6_tmpaddrtimer_ch,
952 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
953 V_ip6_temp_regen_advance) * hz,
954 in6_tmpaddrtimer, curvnet);
955 }
956
957 /*
958 * Cheat.
959 * This must be after route_init(), which is now SI_ORDER_THIRD.
960 */
961 SYSINIT(in6_ifattach_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
962 in6_ifattach_init, NULL);
963