]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/in6_ifattach.c
Implement pci_enable_msi() and pci_disable_msi() in the LinuxKPI.
[FreeBSD/FreeBSD.git] / sys / netinet6 / in6_ifattach.c
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$");
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 epoch_tracker et;
244         struct ifaddr *ifa;
245         struct sockaddr_dl *sdl;
246         u_int8_t *addr;
247         size_t addrlen;
248         static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
249         static u_int8_t allone[8] =
250                 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
251
252         NET_EPOCH_ENTER(et);
253         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
254                 if (ifa->ifa_addr->sa_family != AF_LINK)
255                         continue;
256                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
257                 if (sdl == NULL)
258                         continue;
259                 if (sdl->sdl_alen == 0)
260                         continue;
261
262                 goto found;
263         }
264         NET_EPOCH_EXIT(et);
265
266         return -1;
267
268 found:
269         IF_ADDR_LOCK_ASSERT(ifp);
270         addr = LLADDR(sdl);
271         addrlen = sdl->sdl_alen;
272
273         /* get EUI64 */
274         switch (ifp->if_type) {
275         case IFT_BRIDGE:
276         case IFT_ETHER:
277         case IFT_L2VLAN:
278         case IFT_ATM:
279         case IFT_IEEE1394:
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                         NET_EPOCH_EXIT(et);
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                         NET_EPOCH_EXIT(et);
298                         return -1;
299                 }
300                 if (bcmp(addr, allone, addrlen) == 0) {
301                         NET_EPOCH_EXIT(et);
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_GIF:
321         case IFT_STF:
322                 /*
323                  * RFC2893 says: "SHOULD use IPv4 address as ifid source".
324                  * however, IPv4 address is not very suitable as unique
325                  * identifier source (can be renumbered).
326                  * we don't do this.
327                  */
328                 NET_EPOCH_EXIT(et);
329                 return -1;
330
331         case IFT_INFINIBAND:
332                 if (addrlen != 20) {
333                         NET_EPOCH_EXIT(et);
334                         return -1;
335                 }
336                 bcopy(addr + 12, &in6->s6_addr[8], 8);
337                 break;
338
339         default:
340                 NET_EPOCH_EXIT(et);
341                 return -1;
342         }
343
344         /* sanity check: g bit must not indicate "group" */
345         if (EUI64_GROUP(in6)) {
346                 NET_EPOCH_EXIT(et);
347                 return -1;
348         }
349
350         /* convert EUI64 into IPv6 interface identifier */
351         EUI64_TO_IFID(in6);
352
353         /*
354          * sanity check: ifid must not be all zero, avoid conflict with
355          * subnet router anycast
356          */
357         if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
358             bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
359                 NET_EPOCH_EXIT(et);
360                 return -1;
361         }
362
363         NET_EPOCH_EXIT(et);
364         return 0;
365 }
366
367 /*
368  * Get interface identifier for the specified interface.  If it is not
369  * available on ifp0, borrow interface identifier from other information
370  * sources.
371  *
372  * altifp - secondary EUI64 source
373  */
374 static int
375 get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
376     struct in6_addr *in6)
377 {
378         struct epoch_tracker et;
379         struct ifnet *ifp;
380
381         /* first, try to get it from the interface itself */
382         if (in6_get_hw_ifid(ifp0, in6) == 0) {
383                 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
384                     if_name(ifp0)));
385                 goto success;
386         }
387
388         /* try secondary EUI64 source. this basically is for ATM PVC */
389         if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
390                 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
391                     if_name(ifp0), if_name(altifp)));
392                 goto success;
393         }
394
395         /* next, try to get it from some other hardware interface */
396         NET_EPOCH_ENTER(et);
397         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
398                 if (ifp == ifp0)
399                         continue;
400                 if (in6_get_hw_ifid(ifp, in6) != 0)
401                         continue;
402
403                 /*
404                  * to borrow ifid from other interface, ifid needs to be
405                  * globally unique
406                  */
407                 if (IFID_UNIVERSAL(in6)) {
408                         nd6log((LOG_DEBUG,
409                             "%s: borrow interface identifier from %s\n",
410                             if_name(ifp0), if_name(ifp)));
411                         NET_EPOCH_EXIT(et);
412                         goto success;
413                 }
414         }
415         NET_EPOCH_EXIT(et);
416
417         /* last resort: get from random number source */
418         if (get_rand_ifid(ifp, in6) == 0) {
419                 nd6log((LOG_DEBUG,
420                     "%s: interface identifier generated by random number\n",
421                     if_name(ifp0)));
422                 goto success;
423         }
424
425         printf("%s: failed to get interface identifier\n", if_name(ifp0));
426         return -1;
427
428 success:
429         nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
430             if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
431             in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
432             in6->s6_addr[14], in6->s6_addr[15]));
433         return 0;
434 }
435
436 /*
437  * altifp - secondary EUI64 source
438  */
439 static int
440 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
441 {
442         struct in6_ifaddr *ia;
443         struct in6_aliasreq ifra;
444         struct nd_prefixctl pr0;
445         struct nd_prefix *pr;
446         int error;
447
448         /*
449          * configure link-local address.
450          */
451         in6_prepare_ifra(&ifra, NULL, &in6mask64);
452
453         ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
454         ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
455         if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
456                 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
457                 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
458         } else {
459                 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
460                         nd6log((LOG_ERR,
461                             "%s: no ifid available\n", if_name(ifp)));
462                         return (-1);
463                 }
464         }
465         if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
466                 return (-1);
467
468         /* link-local addresses should NEVER expire. */
469         ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
470         ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
471
472         /*
473          * Now call in6_update_ifa() to do a bunch of procedures to configure
474          * a link-local address. We can set the 3rd argument to NULL, because
475          * we know there's no other link-local address on the interface
476          * and therefore we are adding one (instead of updating one).
477          */
478         if ((error = in6_update_ifa(ifp, &ifra, NULL,
479                                     IN6_IFAUPDATE_DADDELAY)) != 0) {
480                 /*
481                  * XXX: When the interface does not support IPv6, this call
482                  * would fail in the SIOCSIFADDR ioctl.  I believe the
483                  * notification is rather confusing in this case, so just
484                  * suppress it.  (jinmei@kame.net 20010130)
485                  */
486                 if (error != EAFNOSUPPORT)
487                         nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
488                             "configure a link-local address on %s "
489                             "(errno=%d)\n",
490                             if_name(ifp), error));
491                 return (-1);
492         }
493
494         ia = in6ifa_ifpforlinklocal(ifp, 0);
495         if (ia == NULL) {
496                 /*
497                  * Another thread removed the address that we just added.
498                  * This should be rare, but it happens.
499                  */
500                 nd6log((LOG_NOTICE, "%s: %s: new link-local address "
501                         "disappeared\n", __func__, if_name(ifp)));
502                 return (-1);
503         }
504         ifa_free(&ia->ia_ifa);
505
506         /*
507          * Make the link-local prefix (fe80::%link/64) as on-link.
508          * Since we'd like to manage prefixes separately from addresses,
509          * we make an ND6 prefix structure for the link-local prefix,
510          * and add it to the prefix list as a never-expire prefix.
511          * XXX: this change might affect some existing code base...
512          */
513         bzero(&pr0, sizeof(pr0));
514         pr0.ndpr_ifp = ifp;
515         /* this should be 64 at this moment. */
516         pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
517         pr0.ndpr_prefix = ifra.ifra_addr;
518         /* apply the mask for safety. (nd6_prelist_add will apply it again) */
519         IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, &in6mask64);
520         /*
521          * Initialize parameters.  The link-local prefix must always be
522          * on-link, and its lifetimes never expire.
523          */
524         pr0.ndpr_raf_onlink = 1;
525         pr0.ndpr_raf_auto = 1;  /* probably meaningless */
526         pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
527         pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
528         /*
529          * Since there is no other link-local addresses, nd6_prefix_lookup()
530          * probably returns NULL.  However, we cannot always expect the result.
531          * For example, if we first remove the (only) existing link-local
532          * address, and then reconfigure another one, the prefix is still
533          * valid with referring to the old link-local address.
534          */
535         if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
536                 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
537                         return (error);
538         } else
539                 nd6_prefix_rele(pr);
540
541         return 0;
542 }
543
544 /*
545  * ifp - must be IFT_LOOP
546  */
547 static int
548 in6_ifattach_loopback(struct ifnet *ifp)
549 {
550         struct in6_aliasreq ifra;
551         int error;
552
553         in6_prepare_ifra(&ifra, &in6addr_loopback, &in6mask128);
554
555         /*
556          * Always initialize ia_dstaddr (= broadcast address) to loopback
557          * address.  Follows IPv4 practice - see in_ifinit().
558          */
559         ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
560         ifra.ifra_dstaddr.sin6_family = AF_INET6;
561         ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
562
563         /* the loopback  address should NEVER expire. */
564         ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
565         ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
566
567         /*
568          * We are sure that this is a newly assigned address, so we can set
569          * NULL to the 3rd arg.
570          */
571         if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
572                 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
573                     "the loopback address on %s (errno=%d)\n",
574                     if_name(ifp), error));
575                 return (-1);
576         }
577
578         return 0;
579 }
580
581 /*
582  * compute NI group address, based on the current hostname setting.
583  * see RFC 4620.
584  *
585  * when ifp == NULL, the caller is responsible for filling scopeid.
586  *
587  * If oldmcprefix == 1, FF02:0:0:0:0:2::/96 is used for NI group address
588  * while it is FF02:0:0:0:0:2:FF00::/104 in RFC 4620. 
589  */
590 static int
591 in6_nigroup0(struct ifnet *ifp, const char *name, int namelen,
592     struct in6_addr *in6, int oldmcprefix)
593 {
594         struct prison *pr;
595         const char *p;
596         u_char *q;
597         MD5_CTX ctxt;
598         u_int8_t digest[16];
599         char l;
600         char n[64];     /* a single label must not exceed 63 chars */
601
602         /*
603          * If no name is given and namelen is -1,
604          * we try to do the hostname lookup ourselves.
605          */
606         if (!name && namelen == -1) {
607                 pr = curthread->td_ucred->cr_prison;
608                 mtx_lock(&pr->pr_mtx);
609                 name = pr->pr_hostname;
610                 namelen = strlen(name);
611         } else
612                 pr = NULL;
613         if (!name || !namelen) {
614                 if (pr != NULL)
615                         mtx_unlock(&pr->pr_mtx);
616                 return -1;
617         }
618
619         p = name;
620         while (p && *p && *p != '.' && p - name < namelen)
621                 p++;
622         if (p == name || p - name > sizeof(n) - 1) {
623                 if (pr != NULL)
624                         mtx_unlock(&pr->pr_mtx);
625                 return -1;      /* label too long */
626         }
627         l = p - name;
628         strncpy(n, name, l);
629         if (pr != NULL)
630                 mtx_unlock(&pr->pr_mtx);
631         n[(int)l] = '\0';
632         for (q = n; *q; q++) {
633                 if ('A' <= *q && *q <= 'Z')
634                         *q = *q - 'A' + 'a';
635         }
636
637         /* generate 16 bytes of pseudo-random value. */
638         bzero(&ctxt, sizeof(ctxt));
639         MD5Init(&ctxt);
640         MD5Update(&ctxt, &l, sizeof(l));
641         MD5Update(&ctxt, n, l);
642         MD5Final(digest, &ctxt);
643
644         bzero(in6, sizeof(*in6));
645         in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
646         in6->s6_addr8[11] = 2;
647         if (oldmcprefix == 0) {
648                 in6->s6_addr8[12] = 0xff;
649                 /* Copy the first 24 bits of 128-bit hash into the address. */
650                 bcopy(digest, &in6->s6_addr8[13], 3);
651         } else {
652                 /* Copy the first 32 bits of 128-bit hash into the address. */
653                 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
654         }
655         if (in6_setscope(in6, ifp, NULL))
656                 return (-1); /* XXX: should not fail */
657
658         return 0;
659 }
660
661 int
662 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
663     struct in6_addr *in6)
664 {
665
666         return (in6_nigroup0(ifp, name, namelen, in6, 0));
667 }
668
669 int
670 in6_nigroup_oldmcprefix(struct ifnet *ifp, const char *name, int namelen,
671     struct in6_addr *in6)
672 {
673
674         return (in6_nigroup0(ifp, name, namelen, in6, 1));
675 }
676
677 /*
678  * XXX multiple loopback interface needs more care.  for instance,
679  * nodelocal address needs to be configured onto only one of them.
680  * XXX multiple link-local address case
681  *
682  * altifp - secondary EUI64 source
683  */
684 void
685 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
686 {
687         struct in6_ifaddr *ia;
688
689         if (ifp->if_afdata[AF_INET6] == NULL)
690                 return;
691         /*
692          * quirks based on interface type
693          */
694         switch (ifp->if_type) {
695         case IFT_STF:
696                 /*
697                  * 6to4 interface is a very special kind of beast.
698                  * no multicast, no linklocal.  RFC2529 specifies how to make
699                  * linklocals for 6to4 interface, but there's no use and
700                  * it is rather harmful to have one.
701                  */
702                 ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
703                 ND_IFINFO(ifp)->flags |= ND6_IFF_NO_DAD;
704                 break;
705         default:
706                 break;
707         }
708
709         /*
710          * usually, we require multicast capability to the interface
711          */
712         if ((ifp->if_flags & IFF_MULTICAST) == 0) {
713                 nd6log((LOG_INFO, "in6_ifattach: "
714                     "%s is not multicast capable, IPv6 not enabled\n",
715                     if_name(ifp)));
716                 return;
717         }
718
719         /*
720          * assign loopback address for loopback interface.
721          */
722         if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
723                 /*
724                  * check that loopback address doesn't exist yet.
725                  */
726                 ia = in6ifa_ifwithaddr(&in6addr_loopback, 0);
727                 if (ia == NULL)
728                         in6_ifattach_loopback(ifp);
729                 else
730                         ifa_free(&ia->ia_ifa);
731         }
732
733         /*
734          * assign a link-local address, if there's none.
735          */
736         if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
737             ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
738                 ia = in6ifa_ifpforlinklocal(ifp, 0);
739                 if (ia == NULL)
740                         in6_ifattach_linklocal(ifp, altifp);
741                 else
742                         ifa_free(&ia->ia_ifa);
743         }
744
745         /* update dynamically. */
746         if (V_in6_maxmtu < ifp->if_mtu)
747                 V_in6_maxmtu = ifp->if_mtu;
748 }
749
750 /*
751  * NOTE: in6_ifdetach() does not support loopback if at this moment.
752  *
753  * When shutting down a VNET we clean up layers top-down.  In that case
754  * upper layer protocols (ulp) are cleaned up already and locks are destroyed
755  * and we must not call into these cleanup functions anymore, thus purgeulp
756  * is set to 0 in that case by in6_ifdetach_destroy().
757  * The normal case of destroying a (cloned) interface still needs to cleanup
758  * everything related to the interface and will have purgeulp set to 1.
759  */
760 static void
761 _in6_ifdetach(struct ifnet *ifp, int purgeulp)
762 {
763         struct ifaddr *ifa, *next;
764
765         if (ifp->if_afdata[AF_INET6] == NULL)
766                 return;
767
768         /*
769          * nuke any of IPv6 addresses we have
770          */
771         CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
772                 if (ifa->ifa_addr->sa_family != AF_INET6)
773                         continue;
774                 in6_purgeaddr(ifa);
775         }
776         if (purgeulp) {
777                 IN6_MULTI_LOCK();
778                 in6_pcbpurgeif0(&V_udbinfo, ifp);
779                 in6_pcbpurgeif0(&V_ulitecbinfo, ifp);
780                 in6_pcbpurgeif0(&V_ripcbinfo, ifp);
781                 IN6_MULTI_UNLOCK();
782         }
783         /* leave from all multicast groups joined */
784         in6_purgemaddrs(ifp);
785
786         /*
787          * Remove neighbor management table.
788          * Enabling the nd6_purge will panic on vmove for interfaces on VNET
789          * teardown as the IPv6 layer is cleaned up already and the locks
790          * are destroyed.
791          */
792         if (purgeulp)
793                 nd6_purge(ifp);
794 }
795
796 void
797 in6_ifdetach(struct ifnet *ifp)
798 {
799
800         _in6_ifdetach(ifp, 1);
801 }
802
803 void
804 in6_ifdetach_destroy(struct ifnet *ifp)
805 {
806
807         _in6_ifdetach(ifp, 0);
808 }
809
810 int
811 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
812     const u_int8_t *baseid, int generate)
813 {
814         u_int8_t nullbuf[8];
815         struct nd_ifinfo *ndi = ND_IFINFO(ifp);
816
817         bzero(nullbuf, sizeof(nullbuf));
818         if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
819                 /* we've never created a random ID.  Create a new one. */
820                 generate = 1;
821         }
822
823         if (generate) {
824                 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
825
826                 /* generate_tmp_ifid will update seedn and buf */
827                 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
828                     ndi->randomid);
829         }
830         bcopy(ndi->randomid, retbuf, 8);
831
832         return (0);
833 }
834
835 void
836 in6_tmpaddrtimer(void *arg)
837 {
838         CURVNET_SET((struct vnet *) arg);
839         struct nd_ifinfo *ndi;
840         u_int8_t nullbuf[8];
841         struct ifnet *ifp;
842
843         callout_reset(&V_in6_tmpaddrtimer_ch,
844             (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
845             V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
846
847         bzero(nullbuf, sizeof(nullbuf));
848         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
849                 if (ifp->if_afdata[AF_INET6] == NULL)
850                         continue;
851                 ndi = ND_IFINFO(ifp);
852                 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
853                         /*
854                          * We've been generating a random ID on this interface.
855                          * Create a new one.
856                          */
857                         (void)generate_tmp_ifid(ndi->randomseed0,
858                             ndi->randomseed1, ndi->randomid);
859                 }
860         }
861
862         CURVNET_RESTORE();
863 }
864
865 static void
866 in6_purgemaddrs(struct ifnet *ifp)
867 {
868         struct in6_multi_head inmh;
869
870         SLIST_INIT(&inmh);
871         IN6_MULTI_LOCK();
872         IN6_MULTI_LIST_LOCK();
873         mld_ifdetach(ifp, &inmh);
874         IN6_MULTI_LIST_UNLOCK();
875         IN6_MULTI_UNLOCK();
876         in6m_release_list_deferred(&inmh);
877
878         /*
879          * Make sure all multicast deletions invoking if_ioctl() are
880          * completed before returning. Else we risk accessing a freed
881          * ifnet structure pointer.
882          */
883         in6m_release_wait();
884 }
885
886 void
887 in6_ifattach_destroy(void)
888 {
889
890         callout_drain(&V_in6_tmpaddrtimer_ch);
891 }
892
893 static void
894 in6_ifattach_init(void *dummy)
895 {
896
897         /* Timer for regeneranation of temporary addresses randomize ID. */
898         callout_init(&V_in6_tmpaddrtimer_ch, 0);
899         callout_reset(&V_in6_tmpaddrtimer_ch,
900             (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
901             V_ip6_temp_regen_advance) * hz,
902             in6_tmpaddrtimer, curvnet);
903 }
904
905 /*
906  * Cheat.
907  * This must be after route_init(), which is now SI_ORDER_THIRD.
908  */
909 SYSINIT(in6_ifattach_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
910     in6_ifattach_init, NULL);