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/cdefs.h>
35 __FBSDID("$FreeBSD: releng/12.0/sys/netinet6/in6_ifattach.c 337866 2018-08-15 20:23:08Z mmacy $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/proc.h>
46 #include <sys/rmlock.h>
47 #include <sys/syslog.h>
48 #include <sys/md5.h>
49
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/route.h>
55 #include <net/vnet.h>
56
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netinet/if_ether.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/udp.h>
63 #include <netinet/udp_var.h>
64
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
67 #include <netinet6/in6_var.h>
68 #include <netinet6/in6_pcb.h>
69 #include <netinet6/in6_ifattach.h>
70 #include <netinet6/ip6_var.h>
71 #include <netinet6/nd6.h>
72 #include <netinet6/mld6_var.h>
73 #include <netinet6/scope6_var.h>
74
75 VNET_DEFINE(unsigned long, in6_maxmtu) = 0;
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_DECLARE(struct inpcbinfo, ripcbinfo);
87 #define V_ripcbinfo VNET(ripcbinfo)
88
89 static int get_rand_ifid(struct ifnet *, struct in6_addr *);
90 static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
91 static int get_ifid(struct ifnet *, 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 /*
108 * Generate a last-resort interface identifier, when the machine has no
109 * IEEE802/EUI64 address sources.
110 * The goal here is to get an interface identifier that is
111 * (1) random enough and (2) does not change across reboot.
112 * We currently use MD5(hostname) for it.
113 *
114 * in6 - upper 64bits are preserved
115 */
116 static int
117 get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
118 {
119 MD5_CTX ctxt;
120 struct prison *pr;
121 u_int8_t digest[16];
122 int hostnamelen;
123
124 pr = curthread->td_ucred->cr_prison;
125 mtx_lock(&pr->pr_mtx);
126 hostnamelen = strlen(pr->pr_hostname);
127 #if 0
128 /* we need at least several letters as seed for ifid */
129 if (hostnamelen < 3) {
130 mtx_unlock(&pr->pr_mtx);
131 return -1;
132 }
133 #endif
134
135 /* generate 8 bytes of pseudo-random value. */
136 bzero(&ctxt, sizeof(ctxt));
137 MD5Init(&ctxt);
138 MD5Update(&ctxt, pr->pr_hostname, hostnamelen);
139 mtx_unlock(&pr->pr_mtx);
140 MD5Final(digest, &ctxt);
141
142 /* assumes sizeof(digest) > sizeof(ifid) */
143 bcopy(digest, &in6->s6_addr[8], 8);
144
145 /* make sure to set "u" bit to local, and "g" bit to individual. */
146 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
147 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
148
149 /* convert EUI64 into IPv6 interface identifier */
150 EUI64_TO_IFID(in6);
151
152 return 0;
153 }
154
155 static int
156 generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
157 {
158 MD5_CTX ctxt;
159 u_int8_t seed[16], digest[16], nullbuf[8];
160 u_int32_t val32;
161
162 /* If there's no history, start with a random seed. */
163 bzero(nullbuf, sizeof(nullbuf));
164 if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
165 int i;
166
167 for (i = 0; i < 2; i++) {
168 val32 = arc4random();
169 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
170 }
171 } else
172 bcopy(seed0, seed, 8);
173
174 /* copy the right-most 64-bits of the given address */
175 /* XXX assumption on the size of IFID */
176 bcopy(seed1, &seed[8], 8);
177
178 if (0) { /* for debugging purposes only */
179 int i;
180
181 printf("generate_tmp_ifid: new randomized ID from: ");
182 for (i = 0; i < 16; i++)
183 printf("%02x", seed[i]);
184 printf(" ");
185 }
186
187 /* generate 16 bytes of pseudo-random value. */
188 bzero(&ctxt, sizeof(ctxt));
189 MD5Init(&ctxt);
190 MD5Update(&ctxt, seed, sizeof(seed));
191 MD5Final(digest, &ctxt);
192
193 /*
194 * RFC 3041 3.2.1. (3)
195 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
196 * left-most bit is numbered 0) to zero.
197 */
198 bcopy(digest, ret, 8);
199 ret[0] &= ~EUI64_UBIT;
200
201 /*
202 * XXX: we'd like to ensure that the generated value is not zero
203 * for simplicity. If the caclculated digest happens to be zero,
204 * use a random non-zero value as the last resort.
205 */
206 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
207 nd6log((LOG_INFO,
208 "generate_tmp_ifid: computed MD5 value is zero.\n"));
209
210 val32 = arc4random();
211 val32 = 1 + (val32 % (0xffffffff - 1));
212 }
213
214 /*
215 * RFC 3041 3.2.1. (4)
216 * Take the rightmost 64-bits of the MD5 digest and save them in
217 * stable storage as the history value to be used in the next
218 * iteration of the algorithm.
219 */
220 bcopy(&digest[8], seed0, 8);
221
222 if (0) { /* for debugging purposes only */
223 int i;
224
225 printf("to: ");
226 for (i = 0; i < 16; i++)
227 printf("%02x", digest[i]);
228 printf("\n");
229 }
230
231 return 0;
232 }
233
234 /*
235 * Get interface identifier for the specified interface.
236 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
237 *
238 * in6 - upper 64bits are preserved
239 */
240 int
241 in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
242 {
243 struct ifaddr *ifa;
244 struct sockaddr_dl *sdl;
245 u_int8_t *addr;
246 size_t addrlen;
247 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
248 static u_int8_t allone[8] =
249 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
250
251 IF_ADDR_RLOCK(ifp);
252 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
253 if (ifa->ifa_addr->sa_family != AF_LINK)
254 continue;
255 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
256 if (sdl == NULL)
257 continue;
258 if (sdl->sdl_alen == 0)
259 continue;
260
261 goto found;
262 }
263 IF_ADDR_RUNLOCK(ifp);
264
265 return -1;
266
267 found:
268 IF_ADDR_LOCK_ASSERT(ifp);
269 addr = LLADDR(sdl);
270 addrlen = sdl->sdl_alen;
271
272 /* get EUI64 */
273 switch (ifp->if_type) {
274 case IFT_BRIDGE:
275 case IFT_ETHER:
276 case IFT_L2VLAN:
277 case IFT_ATM:
278 case IFT_IEEE1394:
279 /* IEEE802/EUI64 cases - what others? */
280 /* IEEE1394 uses 16byte length address starting with EUI64 */
281 if (addrlen > 8)
282 addrlen = 8;
283
284 /* look at IEEE802/EUI64 only */
285 if (addrlen != 8 && addrlen != 6) {
286 IF_ADDR_RUNLOCK(ifp);
287 return -1;
288 }
289
290 /*
291 * check for invalid MAC address - on bsdi, we see it a lot
292 * since wildboar configures all-zero MAC on pccard before
293 * card insertion.
294 */
295 if (bcmp(addr, allzero, addrlen) == 0) {
296 IF_ADDR_RUNLOCK(ifp);
297 return -1;
298 }
299 if (bcmp(addr, allone, addrlen) == 0) {
300 IF_ADDR_RUNLOCK(ifp);
301 return -1;
302 }
303
304 /* make EUI64 address */
305 if (addrlen == 8)
306 bcopy(addr, &in6->s6_addr[8], 8);
307 else if (addrlen == 6) {
308 in6->s6_addr[8] = addr[0];
309 in6->s6_addr[9] = addr[1];
310 in6->s6_addr[10] = addr[2];
311 in6->s6_addr[11] = 0xff;
312 in6->s6_addr[12] = 0xfe;
313 in6->s6_addr[13] = addr[3];
314 in6->s6_addr[14] = addr[4];
315 in6->s6_addr[15] = addr[5];
316 }
317 break;
318
319 case IFT_GIF:
320 case IFT_STF:
321 /*
322 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
323 * however, IPv4 address is not very suitable as unique
324 * identifier source (can be renumbered).
325 * we don't do this.
326 */
327 IF_ADDR_RUNLOCK(ifp);
328 return -1;
329
330 default:
331 IF_ADDR_RUNLOCK(ifp);
332 return -1;
333 }
334
335 /* sanity check: g bit must not indicate "group" */
336 if (EUI64_GROUP(in6)) {
337 IF_ADDR_RUNLOCK(ifp);
338 return -1;
339 }
340
341 /* convert EUI64 into IPv6 interface identifier */
342 EUI64_TO_IFID(in6);
343
344 /*
345 * sanity check: ifid must not be all zero, avoid conflict with
346 * subnet router anycast
347 */
348 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
349 bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
350 IF_ADDR_RUNLOCK(ifp);
351 return -1;
352 }
353
354 IF_ADDR_RUNLOCK(ifp);
355 return 0;
356 }
357
358 /*
359 * Get interface identifier for the specified interface. If it is not
360 * available on ifp0, borrow interface identifier from other information
361 * sources.
362 *
363 * altifp - secondary EUI64 source
364 */
365 static int
366 get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
367 struct in6_addr *in6)
368 {
369 struct ifnet *ifp;
370
371 /* first, try to get it from the interface itself */
372 if (in6_get_hw_ifid(ifp0, in6) == 0) {
373 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
374 if_name(ifp0)));
375 goto success;
376 }
377
378 /* try secondary EUI64 source. this basically is for ATM PVC */
379 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
380 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
381 if_name(ifp0), if_name(altifp)));
382 goto success;
383 }
384
385 /* next, try to get it from some other hardware interface */
386 IFNET_RLOCK_NOSLEEP();
387 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
388 if (ifp == ifp0)
389 continue;
390 if (in6_get_hw_ifid(ifp, in6) != 0)
391 continue;
392
393 /*
394 * to borrow ifid from other interface, ifid needs to be
395 * globally unique
396 */
397 if (IFID_UNIVERSAL(in6)) {
398 nd6log((LOG_DEBUG,
399 "%s: borrow interface identifier from %s\n",
400 if_name(ifp0), if_name(ifp)));
401 IFNET_RUNLOCK_NOSLEEP();
402 goto success;
403 }
404 }
405 IFNET_RUNLOCK_NOSLEEP();
406
407 /* last resort: get from random number source */
408 if (get_rand_ifid(ifp, in6) == 0) {
409 nd6log((LOG_DEBUG,
410 "%s: interface identifier generated by random number\n",
411 if_name(ifp0)));
412 goto success;
413 }
414
415 printf("%s: failed to get interface identifier\n", if_name(ifp0));
416 return -1;
417
418 success:
419 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
420 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
421 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
422 in6->s6_addr[14], in6->s6_addr[15]));
423 return 0;
424 }
425
426 /*
427 * altifp - secondary EUI64 source
428 */
429 static int
430 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
431 {
432 struct in6_ifaddr *ia;
433 struct in6_aliasreq ifra;
434 struct nd_prefixctl pr0;
435 struct nd_prefix *pr;
436 int error;
437
438 /*
439 * configure link-local address.
440 */
441 in6_prepare_ifra(&ifra, NULL, &in6mask64);
442
443 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
444 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
445 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
446 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
447 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
448 } else {
449 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
450 nd6log((LOG_ERR,
451 "%s: no ifid available\n", if_name(ifp)));
452 return (-1);
453 }
454 }
455 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
456 return (-1);
457
458 /* link-local addresses should NEVER expire. */
459 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
460 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
461
462 /*
463 * Now call in6_update_ifa() to do a bunch of procedures to configure
464 * a link-local address. We can set the 3rd argument to NULL, because
465 * we know there's no other link-local address on the interface
466 * and therefore we are adding one (instead of updating one).
467 */
468 if ((error = in6_update_ifa(ifp, &ifra, NULL,
469 IN6_IFAUPDATE_DADDELAY)) != 0) {
470 /*
471 * XXX: When the interface does not support IPv6, this call
472 * would fail in the SIOCSIFADDR ioctl. I believe the
473 * notification is rather confusing in this case, so just
474 * suppress it. (jinmei@kame.net 20010130)
475 */
476 if (error != EAFNOSUPPORT)
477 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
478 "configure a link-local address on %s "
479 "(errno=%d)\n",
480 if_name(ifp), error));
481 return (-1);
482 }
483
484 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
485 KASSERT(ia != NULL, ("%s: ia == NULL, ifp=%p", __func__, ifp));
486
487 ifa_free(&ia->ia_ifa);
488
489 /*
490 * Make the link-local prefix (fe80::%link/64) as on-link.
491 * Since we'd like to manage prefixes separately from addresses,
492 * we make an ND6 prefix structure for the link-local prefix,
493 * and add it to the prefix list as a never-expire prefix.
494 * XXX: this change might affect some existing code base...
495 */
496 bzero(&pr0, sizeof(pr0));
497 pr0.ndpr_ifp = ifp;
498 /* this should be 64 at this moment. */
499 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
500 pr0.ndpr_prefix = ifra.ifra_addr;
501 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
502 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, &in6mask64);
503 /*
504 * Initialize parameters. The link-local prefix must always be
505 * on-link, and its lifetimes never expire.
506 */
507 pr0.ndpr_raf_onlink = 1;
508 pr0.ndpr_raf_auto = 1; /* probably meaningless */
509 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
510 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
511 /*
512 * Since there is no other link-local addresses, nd6_prefix_lookup()
513 * probably returns NULL. However, we cannot always expect the result.
514 * For example, if we first remove the (only) existing link-local
515 * address, and then reconfigure another one, the prefix is still
516 * valid with referring to the old link-local address.
517 */
518 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
519 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
520 return (error);
521 } else
522 nd6_prefix_rele(pr);
523
524 return 0;
525 }
526
527 /*
528 * ifp - must be IFT_LOOP
529 */
530 static int
531 in6_ifattach_loopback(struct ifnet *ifp)
532 {
533 struct in6_aliasreq ifra;
534 int error;
535
536 in6_prepare_ifra(&ifra, &in6addr_loopback, &in6mask128);
537
538 /*
539 * Always initialize ia_dstaddr (= broadcast address) to loopback
540 * address. Follows IPv4 practice - see in_ifinit().
541 */
542 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
543 ifra.ifra_dstaddr.sin6_family = AF_INET6;
544 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
545
546 /* the loopback address should NEVER expire. */
547 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
548 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
549
550 /*
551 * We are sure that this is a newly assigned address, so we can set
552 * NULL to the 3rd arg.
553 */
554 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
555 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
556 "the loopback address on %s (errno=%d)\n",
557 if_name(ifp), error));
558 return (-1);
559 }
560
561 return 0;
562 }
563
564 /*
565 * compute NI group address, based on the current hostname setting.
566 * see RFC 4620.
567 *
568 * when ifp == NULL, the caller is responsible for filling scopeid.
569 *
570 * If oldmcprefix == 1, FF02:0:0:0:0:2::/96 is used for NI group address
571 * while it is FF02:0:0:0:0:2:FF00::/104 in RFC 4620.
572 */
573 static int
574 in6_nigroup0(struct ifnet *ifp, const char *name, int namelen,
575 struct in6_addr *in6, int oldmcprefix)
576 {
577 struct prison *pr;
578 const char *p;
579 u_char *q;
580 MD5_CTX ctxt;
581 u_int8_t digest[16];
582 char l;
583 char n[64]; /* a single label must not exceed 63 chars */
584
585 /*
586 * If no name is given and namelen is -1,
587 * we try to do the hostname lookup ourselves.
588 */
589 if (!name && namelen == -1) {
590 pr = curthread->td_ucred->cr_prison;
591 mtx_lock(&pr->pr_mtx);
592 name = pr->pr_hostname;
593 namelen = strlen(name);
594 } else
595 pr = NULL;
596 if (!name || !namelen) {
597 if (pr != NULL)
598 mtx_unlock(&pr->pr_mtx);
599 return -1;
600 }
601
602 p = name;
603 while (p && *p && *p != '.' && p - name < namelen)
604 p++;
605 if (p == name || p - name > sizeof(n) - 1) {
606 if (pr != NULL)
607 mtx_unlock(&pr->pr_mtx);
608 return -1; /* label too long */
609 }
610 l = p - name;
611 strncpy(n, name, l);
612 if (pr != NULL)
613 mtx_unlock(&pr->pr_mtx);
614 n[(int)l] = '\0';
615 for (q = n; *q; q++) {
616 if ('A' <= *q && *q <= 'Z')
617 *q = *q - 'A' + 'a';
618 }
619
620 /* generate 16 bytes of pseudo-random value. */
621 bzero(&ctxt, sizeof(ctxt));
622 MD5Init(&ctxt);
623 MD5Update(&ctxt, &l, sizeof(l));
624 MD5Update(&ctxt, n, l);
625 MD5Final(digest, &ctxt);
626
627 bzero(in6, sizeof(*in6));
628 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
629 in6->s6_addr8[11] = 2;
630 if (oldmcprefix == 0) {
631 in6->s6_addr8[12] = 0xff;
632 /* Copy the first 24 bits of 128-bit hash into the address. */
633 bcopy(digest, &in6->s6_addr8[13], 3);
634 } else {
635 /* Copy the first 32 bits of 128-bit hash into the address. */
636 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
637 }
638 if (in6_setscope(in6, ifp, NULL))
639 return (-1); /* XXX: should not fail */
640
641 return 0;
642 }
643
644 int
645 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
646 struct in6_addr *in6)
647 {
648
649 return (in6_nigroup0(ifp, name, namelen, in6, 0));
650 }
651
652 int
653 in6_nigroup_oldmcprefix(struct ifnet *ifp, const char *name, int namelen,
654 struct in6_addr *in6)
655 {
656
657 return (in6_nigroup0(ifp, name, namelen, in6, 1));
658 }
659
660 /*
661 * XXX multiple loopback interface needs more care. for instance,
662 * nodelocal address needs to be configured onto only one of them.
663 * XXX multiple link-local address case
664 *
665 * altifp - secondary EUI64 source
666 */
667 void
668 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
669 {
670 struct in6_ifaddr *ia;
671
672 if (ifp->if_afdata[AF_INET6] == NULL)
673 return;
674 /*
675 * quirks based on interface type
676 */
677 switch (ifp->if_type) {
678 case IFT_STF:
679 /*
680 * 6to4 interface is a very special kind of beast.
681 * no multicast, no linklocal. RFC2529 specifies how to make
682 * linklocals for 6to4 interface, but there's no use and
683 * it is rather harmful to have one.
684 */
685 ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
686 break;
687 default:
688 break;
689 }
690
691 /*
692 * usually, we require multicast capability to the interface
693 */
694 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
695 nd6log((LOG_INFO, "in6_ifattach: "
696 "%s is not multicast capable, IPv6 not enabled\n",
697 if_name(ifp)));
698 return;
699 }
700
701 /*
702 * assign loopback address for loopback interface.
703 */
704 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
705 /*
706 * check that loopback address doesn't exist yet.
707 */
708 ia = in6ifa_ifwithaddr(&in6addr_loopback, 0);
709 if (ia == NULL)
710 in6_ifattach_loopback(ifp);
711 else
712 ifa_free(&ia->ia_ifa);
713 }
714
715 /*
716 * assign a link-local address, if there's none.
717 */
718 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
719 ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
720 ia = in6ifa_ifpforlinklocal(ifp, 0);
721 if (ia == NULL)
722 in6_ifattach_linklocal(ifp, altifp);
723 else
724 ifa_free(&ia->ia_ifa);
725 }
726
727 /* update dynamically. */
728 if (V_in6_maxmtu < ifp->if_mtu)
729 V_in6_maxmtu = ifp->if_mtu;
730 }
731
732 /*
733 * NOTE: in6_ifdetach() does not support loopback if at this moment.
734 *
735 * When shutting down a VNET we clean up layers top-down. In that case
736 * upper layer protocols (ulp) are cleaned up already and locks are destroyed
737 * and we must not call into these cleanup functions anymore, thus purgeulp
738 * is set to 0 in that case by in6_ifdetach_destroy().
739 * The normal case of destroying a (cloned) interface still needs to cleanup
740 * everything related to the interface and will have purgeulp set to 1.
741 */
742 static void
743 _in6_ifdetach(struct ifnet *ifp, int purgeulp)
744 {
745 struct ifaddr *ifa, *next;
746
747 if (ifp->if_afdata[AF_INET6] == NULL)
748 return;
749
750 /*
751 * nuke any of IPv6 addresses we have
752 */
753 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
754 if (ifa->ifa_addr->sa_family != AF_INET6)
755 continue;
756 in6_purgeaddr(ifa);
757 }
758 if (purgeulp) {
759 in6_pcbpurgeif0(&V_udbinfo, ifp);
760 in6_pcbpurgeif0(&V_ulitecbinfo, ifp);
761 in6_pcbpurgeif0(&V_ripcbinfo, ifp);
762 }
763 /* leave from all multicast groups joined */
764 in6_purgemaddrs(ifp);
765
766 /*
767 * Remove neighbor management table.
768 * Enabling the nd6_purge will panic on vmove for interfaces on VNET
769 * teardown as the IPv6 layer is cleaned up already and the locks
770 * are destroyed.
771 */
772 if (purgeulp)
773 nd6_purge(ifp);
774 }
775
776 void
777 in6_ifdetach(struct ifnet *ifp)
778 {
779
780 _in6_ifdetach(ifp, 1);
781 }
782
783 void
784 in6_ifdetach_destroy(struct ifnet *ifp)
785 {
786
787 _in6_ifdetach(ifp, 0);
788 }
789
790 int
791 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
792 const u_int8_t *baseid, int generate)
793 {
794 u_int8_t nullbuf[8];
795 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
796
797 bzero(nullbuf, sizeof(nullbuf));
798 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
799 /* we've never created a random ID. Create a new one. */
800 generate = 1;
801 }
802
803 if (generate) {
804 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
805
806 /* generate_tmp_ifid will update seedn and buf */
807 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
808 ndi->randomid);
809 }
810 bcopy(ndi->randomid, retbuf, 8);
811
812 return (0);
813 }
814
815 void
816 in6_tmpaddrtimer(void *arg)
817 {
818 CURVNET_SET((struct vnet *) arg);
819 struct nd_ifinfo *ndi;
820 u_int8_t nullbuf[8];
821 struct ifnet *ifp;
822
823 callout_reset(&V_in6_tmpaddrtimer_ch,
824 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
825 V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
826
827 bzero(nullbuf, sizeof(nullbuf));
828 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
829 if (ifp->if_afdata[AF_INET6] == NULL)
830 continue;
831 ndi = ND_IFINFO(ifp);
832 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
833 /*
834 * We've been generating a random ID on this interface.
835 * Create a new one.
836 */
837 (void)generate_tmp_ifid(ndi->randomseed0,
838 ndi->randomseed1, ndi->randomid);
839 }
840 }
841
842 CURVNET_RESTORE();
843 }
844
845 static void
846 in6_purgemaddrs(struct ifnet *ifp)
847 {
848 struct in6_multi_head purgeinms;
849 struct in6_multi *inm;
850 struct ifmultiaddr *ifma, *next;
851
852 SLIST_INIT(&purgeinms);
853 IN6_MULTI_LOCK();
854 IN6_MULTI_LIST_LOCK();
855 IF_ADDR_WLOCK(ifp);
856 /*
857 * Extract list of in6_multi associated with the detaching ifp
858 * which the PF_INET6 layer is about to release.
859 */
860 restart:
861 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
862 if (ifma->ifma_addr->sa_family != AF_INET6 ||
863 ifma->ifma_protospec == NULL)
864 continue;
865 inm = (struct in6_multi *)ifma->ifma_protospec;
866 in6m_disconnect(inm);
867 in6m_rele_locked(&purgeinms, inm);
868 if (__predict_false(ifma6_restart)) {
869 ifma6_restart = false;
870 goto restart;
871 }
872 }
873 IF_ADDR_WUNLOCK(ifp);
874 mld_ifdetach(ifp);
875 IN6_MULTI_LIST_UNLOCK();
876 IN6_MULTI_UNLOCK();
877 in6m_release_list_deferred(&purgeinms);
878 }
879
880 void
881 in6_ifattach_destroy(void)
882 {
883
884 callout_drain(&V_in6_tmpaddrtimer_ch);
885 }
886
887 static void
888 in6_ifattach_init(void *dummy)
889 {
890
891 /* Timer for regeneranation of temporary addresses randomize ID. */
892 callout_init(&V_in6_tmpaddrtimer_ch, 0);
893 callout_reset(&V_in6_tmpaddrtimer_ch,
894 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
895 V_ip6_temp_regen_advance) * hz,
896 in6_tmpaddrtimer, curvnet);
897 }
898
899 /*
900 * Cheat.
901 * This must be after route_init(), which is now SI_ORDER_THIRD.
902 */
903 SYSINIT(in6_ifattach_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
904 in6_ifattach_init, NULL);
Cache object: 288ae40f446490e4e35f905ead2ecd3a
|