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