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