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/malloc.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <sys/jail.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/proc.h>
43 #include <sys/rmlock.h>
44 #include <sys/syslog.h>
45 #include <sys/md5.h>
46
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/if_dl.h>
50 #include <net/if_private.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 #ifdef IP6_AUTO_LINKLOCAL
74 VNET_DEFINE(int, ip6_auto_linklocal) = IP6_AUTO_LINKLOCAL;
75 #else
76 VNET_DEFINE(int, ip6_auto_linklocal) = 1; /* enabled by default */
77 #endif
78
79 VNET_DEFINE(struct callout, in6_tmpaddrtimer_ch);
80 #define V_in6_tmpaddrtimer_ch VNET(in6_tmpaddrtimer_ch)
81
82 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
83 #define V_ripcbinfo VNET(ripcbinfo)
84
85 static int get_rand_ifid(struct ifnet *, struct in6_addr *);
86 static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
87 static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
88 static int in6_ifattach_loopback(struct ifnet *);
89 static void in6_purgemaddrs(struct ifnet *);
90
91 #define EUI64_GBIT 0x01
92 #define EUI64_UBIT 0x02
93 #define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
94 #define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT)
95 #define EUI64_INDIVIDUAL(in6) (!EUI64_GROUP(in6))
96 #define EUI64_LOCAL(in6) ((in6)->s6_addr[8] & EUI64_UBIT)
97 #define EUI64_UNIVERSAL(in6) (!EUI64_LOCAL(in6))
98
99 #define IFID_LOCAL(in6) (!EUI64_LOCAL(in6))
100 #define IFID_UNIVERSAL(in6) (!EUI64_UNIVERSAL(in6))
101
102 /*
103 * Generate a last-resort interface identifier, when the machine has no
104 * IEEE802/EUI64 address sources.
105 * The goal here is to get an interface identifier that is
106 * (1) random enough and (2) does not change across reboot.
107 * We currently use MD5(hostname) for it.
108 *
109 * in6 - upper 64bits are preserved
110 */
111 static int
get_rand_ifid(struct ifnet * ifp,struct in6_addr * in6)112 get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
113 {
114 MD5_CTX ctxt;
115 struct prison *pr;
116 u_int8_t digest[16];
117 int hostnamelen;
118
119 pr = curthread->td_ucred->cr_prison;
120 mtx_lock(&pr->pr_mtx);
121 hostnamelen = strlen(pr->pr_hostname);
122 #if 0
123 /* we need at least several letters as seed for ifid */
124 if (hostnamelen < 3) {
125 mtx_unlock(&pr->pr_mtx);
126 return -1;
127 }
128 #endif
129
130 /* generate 8 bytes of pseudo-random value. */
131 bzero(&ctxt, sizeof(ctxt));
132 MD5Init(&ctxt);
133 MD5Update(&ctxt, pr->pr_hostname, hostnamelen);
134 mtx_unlock(&pr->pr_mtx);
135 MD5Final(digest, &ctxt);
136
137 /* assumes sizeof(digest) > sizeof(ifid) */
138 bcopy(digest, &in6->s6_addr[8], 8);
139
140 /* make sure to set "u" bit to local, and "g" bit to individual. */
141 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
142 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
143
144 /* convert EUI64 into IPv6 interface identifier */
145 EUI64_TO_IFID(in6);
146
147 return 0;
148 }
149
150
151 /*
152 * Get interface identifier for the specified interface.
153 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
154 *
155 * in6 - upper 64bits are preserved
156 */
157 int
in6_get_hw_ifid(struct ifnet * ifp,struct in6_addr * in6)158 in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
159 {
160 struct ifaddr *ifa;
161 struct sockaddr_dl *sdl;
162 u_int8_t *addr;
163 size_t addrlen;
164 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
165 static u_int8_t allone[8] =
166 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
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 goto found;
180 }
181
182 return -1;
183
184 found:
185 addr = LLADDR(sdl);
186 addrlen = sdl->sdl_alen;
187
188 /* get EUI64 */
189 switch (ifp->if_type) {
190 case IFT_BRIDGE:
191 case IFT_ETHER:
192 case IFT_L2VLAN:
193 case IFT_ATM:
194 case IFT_IEEE1394:
195 /* IEEE802/EUI64 cases - what others? */
196 /* IEEE1394 uses 16byte length address starting with EUI64 */
197 if (addrlen > 8)
198 addrlen = 8;
199
200 /* look at IEEE802/EUI64 only */
201 if (addrlen != 8 && addrlen != 6)
202 return -1;
203
204 /*
205 * check for invalid MAC address - on bsdi, we see it a lot
206 * since wildboar configures all-zero MAC on pccard before
207 * card insertion.
208 */
209 if (bcmp(addr, allzero, addrlen) == 0)
210 return -1;
211 if (bcmp(addr, allone, addrlen) == 0)
212 return -1;
213
214 /* make EUI64 address */
215 if (addrlen == 8)
216 bcopy(addr, &in6->s6_addr[8], 8);
217 else if (addrlen == 6) {
218 in6->s6_addr[8] = addr[0];
219 in6->s6_addr[9] = addr[1];
220 in6->s6_addr[10] = addr[2];
221 in6->s6_addr[11] = 0xff;
222 in6->s6_addr[12] = 0xfe;
223 in6->s6_addr[13] = addr[3];
224 in6->s6_addr[14] = addr[4];
225 in6->s6_addr[15] = addr[5];
226 }
227 break;
228
229 case IFT_GIF:
230 case IFT_STF:
231 /*
232 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
233 * however, IPv4 address is not very suitable as unique
234 * identifier source (can be renumbered).
235 * we don't do this.
236 */
237 return -1;
238
239 case IFT_INFINIBAND:
240 if (addrlen != 20)
241 return -1;
242 bcopy(addr + 12, &in6->s6_addr[8], 8);
243 break;
244
245 default:
246 return -1;
247 }
248
249 /* sanity check: g bit must not indicate "group" */
250 if (EUI64_GROUP(in6))
251 return -1;
252
253 /* convert EUI64 into IPv6 interface identifier */
254 EUI64_TO_IFID(in6);
255
256 /*
257 * sanity check: ifid must not be all zero, avoid conflict with
258 * subnet router anycast
259 */
260 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
261 bcmp(&in6->s6_addr[9], allzero, 7) == 0)
262 return -1;
263
264 return 0;
265 }
266
267 /*
268 * Get interface identifier for the specified interface. If it is not
269 * available on ifp0, borrow interface identifier from other information
270 * sources.
271 *
272 * altifp - secondary EUI64 source
273 */
274 static int
get_ifid(struct ifnet * ifp0,struct ifnet * altifp,struct in6_addr * in6)275 get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
276 struct in6_addr *in6)
277 {
278 struct ifnet *ifp;
279
280 NET_EPOCH_ASSERT();
281
282 /* first, try to get it from the interface itself */
283 if (in6_get_hw_ifid(ifp0, in6) == 0) {
284 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
285 if_name(ifp0)));
286 goto success;
287 }
288
289 /* try secondary EUI64 source. this basically is for ATM PVC */
290 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
291 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
292 if_name(ifp0), if_name(altifp)));
293 goto success;
294 }
295
296 /* next, try to get it from some other hardware interface */
297 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
298 if (ifp == ifp0)
299 continue;
300 if (in6_get_hw_ifid(ifp, in6) != 0)
301 continue;
302
303 /*
304 * to borrow ifid from other interface, ifid needs to be
305 * globally unique
306 */
307 if (IFID_UNIVERSAL(in6)) {
308 nd6log((LOG_DEBUG,
309 "%s: borrow interface identifier from %s\n",
310 if_name(ifp0), if_name(ifp)));
311 goto success;
312 }
313 }
314
315 /* last resort: get from random number source */
316 if (get_rand_ifid(ifp, in6) == 0) {
317 nd6log((LOG_DEBUG,
318 "%s: interface identifier generated by random number\n",
319 if_name(ifp0)));
320 goto success;
321 }
322
323 printf("%s: failed to get interface identifier\n", if_name(ifp0));
324 return -1;
325
326 success:
327 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
328 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
329 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
330 in6->s6_addr[14], in6->s6_addr[15]));
331 return 0;
332 }
333
334 /*
335 * altifp - secondary EUI64 source
336 */
337 static int
in6_ifattach_linklocal(struct ifnet * ifp,struct ifnet * altifp)338 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
339 {
340 struct in6_ifaddr *ia;
341 struct in6_aliasreq ifra;
342 struct nd_prefixctl pr0;
343 struct epoch_tracker et;
344 struct nd_prefix *pr;
345 int error;
346
347 /*
348 * configure link-local address.
349 */
350 in6_prepare_ifra(&ifra, NULL, &in6mask64);
351
352 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
353 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
354 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
355 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
356 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
357 } else {
358 NET_EPOCH_ENTER(et);
359 error = get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr);
360 NET_EPOCH_EXIT(et);
361 if (error != 0) {
362 nd6log((LOG_ERR,
363 "%s: no ifid available\n", if_name(ifp)));
364 return (-1);
365 }
366 }
367 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
368 return (-1);
369
370 /* link-local addresses should NEVER expire. */
371 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
372 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
373
374 /*
375 * Now call in6_update_ifa() to do a bunch of procedures to configure
376 * a link-local address. We can set the 3rd argument to NULL, because
377 * we know there's no other link-local address on the interface
378 * and therefore we are adding one (instead of updating one).
379 */
380 if ((error = in6_update_ifa(ifp, &ifra, NULL,
381 IN6_IFAUPDATE_DADDELAY)) != 0) {
382 /*
383 * XXX: When the interface does not support IPv6, this call
384 * would fail in the SIOCSIFADDR ioctl. I believe the
385 * notification is rather confusing in this case, so just
386 * suppress it. (jinmei@kame.net 20010130)
387 */
388 if (error != EAFNOSUPPORT)
389 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
390 "configure a link-local address on %s "
391 "(errno=%d)\n",
392 if_name(ifp), error));
393 return (-1);
394 }
395
396 NET_EPOCH_ENTER(et);
397 ia = in6ifa_ifpforlinklocal(ifp, 0);
398 NET_EPOCH_EXIT(et);
399 if (ia == NULL) {
400 /*
401 * Another thread removed the address that we just added.
402 * This should be rare, but it happens.
403 */
404 nd6log((LOG_NOTICE, "%s: %s: new link-local address "
405 "disappeared\n", __func__, if_name(ifp)));
406 return (-1);
407 }
408 ifa_free(&ia->ia_ifa);
409
410 /*
411 * Make the link-local prefix (fe80::%link/64) as on-link.
412 * Since we'd like to manage prefixes separately from addresses,
413 * we make an ND6 prefix structure for the link-local prefix,
414 * and add it to the prefix list as a never-expire prefix.
415 * XXX: this change might affect some existing code base...
416 */
417 bzero(&pr0, sizeof(pr0));
418 pr0.ndpr_ifp = ifp;
419 /* this should be 64 at this moment. */
420 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
421 pr0.ndpr_prefix = ifra.ifra_addr;
422 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
423 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, &in6mask64);
424 /*
425 * Initialize parameters. The link-local prefix must always be
426 * on-link, and its lifetimes never expire.
427 */
428 pr0.ndpr_raf_onlink = 1;
429 pr0.ndpr_raf_auto = 1; /* probably meaningless */
430 pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
431 pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
432 /*
433 * Since there is no other link-local addresses, nd6_prefix_lookup()
434 * probably returns NULL. However, we cannot always expect the result.
435 * For example, if we first remove the (only) existing link-local
436 * address, and then reconfigure another one, the prefix is still
437 * valid with referring to the old link-local address.
438 */
439 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
440 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
441 return (error);
442 /* Reference prefix */
443 ia->ia6_ndpr = pr;
444 pr->ndpr_addrcnt++;
445 } else
446 nd6_prefix_rele(pr);
447
448 return 0;
449 }
450
451 /*
452 * ifp - must be IFT_LOOP
453 */
454 static int
in6_ifattach_loopback(struct ifnet * ifp)455 in6_ifattach_loopback(struct ifnet *ifp)
456 {
457 struct in6_aliasreq ifra;
458 int error;
459
460 in6_prepare_ifra(&ifra, &in6addr_loopback, &in6mask128);
461
462 /*
463 * Always initialize ia_dstaddr (= broadcast address) to loopback
464 * address. Follows IPv4 practice - see in_ifinit().
465 */
466 ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
467 ifra.ifra_dstaddr.sin6_family = AF_INET6;
468 ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
469
470 /* the loopback address should NEVER expire. */
471 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
472 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
473
474 /*
475 * We are sure that this is a newly assigned address, so we can set
476 * NULL to the 3rd arg.
477 */
478 if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
479 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
480 "the loopback address on %s (errno=%d)\n",
481 if_name(ifp), error));
482 return (-1);
483 }
484
485 return 0;
486 }
487
488 /*
489 * compute NI group address, based on the current hostname setting.
490 * see RFC 4620.
491 *
492 * when ifp == NULL, the caller is responsible for filling scopeid.
493 *
494 * If oldmcprefix == 1, FF02:0:0:0:0:2::/96 is used for NI group address
495 * while it is FF02:0:0:0:0:2:FF00::/104 in RFC 4620.
496 */
497 static int
in6_nigroup0(struct ifnet * ifp,const char * name,int namelen,struct in6_addr * in6,int oldmcprefix)498 in6_nigroup0(struct ifnet *ifp, const char *name, int namelen,
499 struct in6_addr *in6, int oldmcprefix)
500 {
501 struct prison *pr;
502 const char *p;
503 u_char *q;
504 MD5_CTX ctxt;
505 u_int8_t digest[16];
506 char l;
507 char n[64]; /* a single label must not exceed 63 chars */
508
509 /*
510 * If no name is given and namelen is -1,
511 * we try to do the hostname lookup ourselves.
512 */
513 if (!name && namelen == -1) {
514 pr = curthread->td_ucred->cr_prison;
515 mtx_lock(&pr->pr_mtx);
516 name = pr->pr_hostname;
517 namelen = strlen(name);
518 } else
519 pr = NULL;
520 if (!name || !namelen) {
521 if (pr != NULL)
522 mtx_unlock(&pr->pr_mtx);
523 return -1;
524 }
525
526 p = name;
527 while (p && *p && *p != '.' && p - name < namelen)
528 p++;
529 if (p == name || p - name > sizeof(n) - 1) {
530 if (pr != NULL)
531 mtx_unlock(&pr->pr_mtx);
532 return -1; /* label too long */
533 }
534 l = p - name;
535 strncpy(n, name, l);
536 if (pr != NULL)
537 mtx_unlock(&pr->pr_mtx);
538 n[(int)l] = '\0';
539 for (q = n; *q; q++) {
540 if ('A' <= *q && *q <= 'Z')
541 *q = *q - 'A' + 'a';
542 }
543
544 /* generate 16 bytes of pseudo-random value. */
545 bzero(&ctxt, sizeof(ctxt));
546 MD5Init(&ctxt);
547 MD5Update(&ctxt, &l, sizeof(l));
548 MD5Update(&ctxt, n, l);
549 MD5Final(digest, &ctxt);
550
551 bzero(in6, sizeof(*in6));
552 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
553 in6->s6_addr8[11] = 2;
554 if (oldmcprefix == 0) {
555 in6->s6_addr8[12] = 0xff;
556 /* Copy the first 24 bits of 128-bit hash into the address. */
557 bcopy(digest, &in6->s6_addr8[13], 3);
558 } else {
559 /* Copy the first 32 bits of 128-bit hash into the address. */
560 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
561 }
562 if (in6_setscope(in6, ifp, NULL))
563 return (-1); /* XXX: should not fail */
564
565 return 0;
566 }
567
568 int
in6_nigroup(struct ifnet * ifp,const char * name,int namelen,struct in6_addr * in6)569 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
570 struct in6_addr *in6)
571 {
572
573 return (in6_nigroup0(ifp, name, namelen, in6, 0));
574 }
575
576 int
in6_nigroup_oldmcprefix(struct ifnet * ifp,const char * name,int namelen,struct in6_addr * in6)577 in6_nigroup_oldmcprefix(struct ifnet *ifp, const char *name, int namelen,
578 struct in6_addr *in6)
579 {
580
581 return (in6_nigroup0(ifp, name, namelen, in6, 1));
582 }
583
584 /*
585 * XXX multiple loopback interface needs more care. for instance,
586 * nodelocal address needs to be configured onto only one of them.
587 * XXX multiple link-local address case
588 *
589 * altifp - secondary EUI64 source
590 */
591 void
in6_ifattach(struct ifnet * ifp,struct ifnet * altifp)592 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
593 {
594 struct in6_ifaddr *ia;
595
596 if (ifp->if_afdata[AF_INET6] == NULL)
597 return;
598 /*
599 * quirks based on interface type
600 */
601 switch (ifp->if_type) {
602 case IFT_STF:
603 /*
604 * 6to4 interface is a very special kind of beast.
605 * no multicast, no linklocal. RFC2529 specifies how to make
606 * linklocals for 6to4 interface, but there's no use and
607 * it is rather harmful to have one.
608 */
609 ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
610 ND_IFINFO(ifp)->flags |= ND6_IFF_NO_DAD;
611 break;
612 default:
613 break;
614 }
615
616 /*
617 * usually, we require multicast capability to the interface
618 */
619 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
620 nd6log((LOG_INFO, "in6_ifattach: "
621 "%s is not multicast capable, IPv6 not enabled\n",
622 if_name(ifp)));
623 return;
624 }
625
626 /*
627 * assign loopback address for loopback interface.
628 */
629 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
630 /*
631 * check that loopback address doesn't exist yet.
632 */
633 ia = in6ifa_ifwithaddr(&in6addr_loopback, 0, false);
634 if (ia == NULL)
635 in6_ifattach_loopback(ifp);
636 }
637
638 /*
639 * assign a link-local address, if there's none.
640 */
641 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
642 ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
643 struct epoch_tracker et;
644
645 NET_EPOCH_ENTER(et);
646 ia = in6ifa_ifpforlinklocal(ifp, 0);
647 NET_EPOCH_EXIT(et);
648 if (ia == NULL)
649 in6_ifattach_linklocal(ifp, altifp);
650 else
651 ifa_free(&ia->ia_ifa);
652 }
653 }
654
655 /*
656 * NOTE: in6_ifdetach() does not support loopback if at this moment.
657 *
658 * When shutting down a VNET we clean up layers top-down. In that case
659 * upper layer protocols (ulp) are cleaned up already and locks are destroyed
660 * and we must not call into these cleanup functions anymore, thus purgeulp
661 * is set to 0 in that case by in6_ifdetach_destroy().
662 * The normal case of destroying a (cloned) interface still needs to cleanup
663 * everything related to the interface and will have purgeulp set to 1.
664 */
665 static void
_in6_ifdetach(struct ifnet * ifp,int purgeulp)666 _in6_ifdetach(struct ifnet *ifp, int purgeulp)
667 {
668 struct ifaddr *ifa, *next;
669
670 if (ifp->if_afdata[AF_INET6] == NULL)
671 return;
672
673 /*
674 * nuke any of IPv6 addresses we have
675 */
676 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
677 if (ifa->ifa_addr->sa_family != AF_INET6)
678 continue;
679 in6_purgeaddr(ifa);
680 }
681 if (purgeulp) {
682 IN6_MULTI_LOCK();
683 in6_pcbpurgeif0(&V_udbinfo, ifp);
684 in6_pcbpurgeif0(&V_ulitecbinfo, ifp);
685 in6_pcbpurgeif0(&V_ripcbinfo, ifp);
686 IN6_MULTI_UNLOCK();
687 }
688 /* leave from all multicast groups joined */
689 in6_purgemaddrs(ifp);
690
691 /*
692 * Remove neighbor management table.
693 * Enabling the nd6_purge will panic on vmove for interfaces on VNET
694 * teardown as the IPv6 layer is cleaned up already and the locks
695 * are destroyed.
696 */
697 if (purgeulp)
698 nd6_purge(ifp);
699 }
700
701 void
in6_ifdetach(struct ifnet * ifp)702 in6_ifdetach(struct ifnet *ifp)
703 {
704
705 _in6_ifdetach(ifp, 1);
706 }
707
708 void
in6_ifdetach_destroy(struct ifnet * ifp)709 in6_ifdetach_destroy(struct ifnet *ifp)
710 {
711
712 _in6_ifdetach(ifp, 0);
713 }
714
715 void
in6_tmpaddrtimer(void * arg)716 in6_tmpaddrtimer(void *arg)
717 {
718 CURVNET_SET((struct vnet *) arg);
719
720 callout_reset(&V_in6_tmpaddrtimer_ch,
721 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
722 V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
723
724 CURVNET_RESTORE();
725 }
726
727 static void
in6_purgemaddrs(struct ifnet * ifp)728 in6_purgemaddrs(struct ifnet *ifp)
729 {
730 struct in6_multi_head inmh;
731
732 SLIST_INIT(&inmh);
733 IN6_MULTI_LOCK();
734 IN6_MULTI_LIST_LOCK();
735 mld_ifdetach(ifp, &inmh);
736 IN6_MULTI_LIST_UNLOCK();
737 IN6_MULTI_UNLOCK();
738 in6m_release_list_deferred(&inmh);
739
740 /*
741 * Make sure all multicast deletions invoking if_ioctl() are
742 * completed before returning. Else we risk accessing a freed
743 * ifnet structure pointer.
744 */
745 in6m_release_wait(NULL);
746 }
747
748 void
in6_ifattach_destroy(void)749 in6_ifattach_destroy(void)
750 {
751
752 callout_drain(&V_in6_tmpaddrtimer_ch);
753 }
754
755 static void
in6_ifattach_init(void * dummy)756 in6_ifattach_init(void *dummy)
757 {
758
759 /* Timer for regeneranation of temporary addresses randomize ID. */
760 callout_init(&V_in6_tmpaddrtimer_ch, 1);
761 callout_reset(&V_in6_tmpaddrtimer_ch,
762 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
763 V_ip6_temp_regen_advance) * hz,
764 in6_tmpaddrtimer, curvnet);
765 }
766
767 /*
768 * Cheat.
769 * This must be after route_init(), which is now SI_ORDER_THIRD.
770 */
771 SYSINIT(in6_ifattach_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
772 in6_ifattach_init, NULL);
773