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: releng/8.3/sys/netinet6/in6_ifattach.c 231307 2012-02-09 21:33:36Z bz $");
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_dl.h>
48 #include <net/if_types.h>
49 #include <net/route.h>
50 #include <net/vnet.h>
51
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/if_ether.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/udp.h>
58 #include <netinet/udp_var.h>
59
60 #include <netinet/ip6.h>
61 #include <netinet6/ip6_var.h>
62 #include <netinet6/in6_var.h>
63 #include <netinet6/in6_pcb.h>
64 #include <netinet6/in6_ifattach.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet6/nd6.h>
67 #include <netinet6/mld6_var.h>
68 #include <netinet6/scope6_var.h>
69
70 VNET_DEFINE(unsigned long, in6_maxmtu) = 0;
71
72 #ifdef IP6_AUTO_LINKLOCAL
73 VNET_DEFINE(int, ip6_auto_linklocal) = IP6_AUTO_LINKLOCAL;
74 #else
75 VNET_DEFINE(int, ip6_auto_linklocal) = 1; /* enabled by default */
76 #endif
77
78 VNET_DEFINE(struct callout, in6_tmpaddrtimer_ch);
79 #define V_in6_tmpaddrtimer_ch VNET(in6_tmpaddrtimer_ch)
80
81 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
82 #define V_ripcbinfo VNET(ripcbinfo)
83
84 static int get_rand_ifid(struct ifnet *, struct in6_addr *);
85 static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
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
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 static int
151 generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
152 {
153 MD5_CTX ctxt;
154 u_int8_t seed[16], digest[16], nullbuf[8];
155 u_int32_t val32;
156
157 /* If there's no history, start with a random seed. */
158 bzero(nullbuf, sizeof(nullbuf));
159 if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
160 int i;
161
162 for (i = 0; i < 2; i++) {
163 val32 = arc4random();
164 bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
165 }
166 } else
167 bcopy(seed0, seed, 8);
168
169 /* copy the right-most 64-bits of the given address */
170 /* XXX assumption on the size of IFID */
171 bcopy(seed1, &seed[8], 8);
172
173 if (0) { /* for debugging purposes only */
174 int i;
175
176 printf("generate_tmp_ifid: new randomized ID from: ");
177 for (i = 0; i < 16; i++)
178 printf("%02x", seed[i]);
179 printf(" ");
180 }
181
182 /* generate 16 bytes of pseudo-random value. */
183 bzero(&ctxt, sizeof(ctxt));
184 MD5Init(&ctxt);
185 MD5Update(&ctxt, seed, sizeof(seed));
186 MD5Final(digest, &ctxt);
187
188 /*
189 * RFC 3041 3.2.1. (3)
190 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
191 * left-most bit is numbered 0) to zero.
192 */
193 bcopy(digest, ret, 8);
194 ret[0] &= ~EUI64_UBIT;
195
196 /*
197 * XXX: we'd like to ensure that the generated value is not zero
198 * for simplicity. If the caclculated digest happens to be zero,
199 * use a random non-zero value as the last resort.
200 */
201 if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
202 nd6log((LOG_INFO,
203 "generate_tmp_ifid: computed MD5 value is zero.\n"));
204
205 val32 = arc4random();
206 val32 = 1 + (val32 % (0xffffffff - 1));
207 }
208
209 /*
210 * RFC 3041 3.2.1. (4)
211 * Take the rightmost 64-bits of the MD5 digest and save them in
212 * stable storage as the history value to be used in the next
213 * iteration of the algorithm.
214 */
215 bcopy(&digest[8], seed0, 8);
216
217 if (0) { /* for debugging purposes only */
218 int i;
219
220 printf("to: ");
221 for (i = 0; i < 16; i++)
222 printf("%02x", digest[i]);
223 printf("\n");
224 }
225
226 return 0;
227 }
228
229 /*
230 * Get interface identifier for the specified interface.
231 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
232 *
233 * in6 - upper 64bits are preserved
234 */
235 int
236 in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
237 {
238 struct ifaddr *ifa;
239 struct sockaddr_dl *sdl;
240 u_int8_t *addr;
241 size_t addrlen;
242 static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
243 static u_int8_t allone[8] =
244 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
245
246 IF_ADDR_LOCK(ifp);
247 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
248 if (ifa->ifa_addr->sa_family != AF_LINK)
249 continue;
250 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
251 if (sdl == NULL)
252 continue;
253 if (sdl->sdl_alen == 0)
254 continue;
255
256 goto found;
257 }
258 IF_ADDR_UNLOCK(ifp);
259
260 return -1;
261
262 found:
263 IF_ADDR_LOCK_ASSERT(ifp);
264 addr = LLADDR(sdl);
265 addrlen = sdl->sdl_alen;
266
267 /* get EUI64 */
268 switch (ifp->if_type) {
269 case IFT_ETHER:
270 case IFT_FDDI:
271 case IFT_ISO88025:
272 case IFT_ATM:
273 case IFT_IEEE1394:
274 #ifdef IFT_IEEE80211
275 case IFT_IEEE80211:
276 #endif
277 /* IEEE802/EUI64 cases - what others? */
278 /* IEEE1394 uses 16byte length address starting with EUI64 */
279 if (addrlen > 8)
280 addrlen = 8;
281
282 /* look at IEEE802/EUI64 only */
283 if (addrlen != 8 && addrlen != 6) {
284 IF_ADDR_UNLOCK(ifp);
285 return -1;
286 }
287
288 /*
289 * check for invalid MAC address - on bsdi, we see it a lot
290 * since wildboar configures all-zero MAC on pccard before
291 * card insertion.
292 */
293 if (bcmp(addr, allzero, addrlen) == 0) {
294 IF_ADDR_UNLOCK(ifp);
295 return -1;
296 }
297 if (bcmp(addr, allone, addrlen) == 0) {
298 IF_ADDR_UNLOCK(ifp);
299 return -1;
300 }
301
302 /* make EUI64 address */
303 if (addrlen == 8)
304 bcopy(addr, &in6->s6_addr[8], 8);
305 else if (addrlen == 6) {
306 in6->s6_addr[8] = addr[0];
307 in6->s6_addr[9] = addr[1];
308 in6->s6_addr[10] = addr[2];
309 in6->s6_addr[11] = 0xff;
310 in6->s6_addr[12] = 0xfe;
311 in6->s6_addr[13] = addr[3];
312 in6->s6_addr[14] = addr[4];
313 in6->s6_addr[15] = addr[5];
314 }
315 break;
316
317 case IFT_ARCNET:
318 if (addrlen != 1) {
319 IF_ADDR_UNLOCK(ifp);
320 return -1;
321 }
322 if (!addr[0]) {
323 IF_ADDR_UNLOCK(ifp);
324 return -1;
325 }
326
327 bzero(&in6->s6_addr[8], 8);
328 in6->s6_addr[15] = addr[0];
329
330 /*
331 * due to insufficient bitwidth, we mark it local.
332 */
333 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
334 in6->s6_addr[8] |= EUI64_UBIT; /* u bit to "local" */
335 break;
336
337 case IFT_GIF:
338 #ifdef IFT_STF
339 case IFT_STF:
340 #endif
341 /*
342 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
343 * however, IPv4 address is not very suitable as unique
344 * identifier source (can be renumbered).
345 * we don't do this.
346 */
347 IF_ADDR_UNLOCK(ifp);
348 return -1;
349
350 default:
351 IF_ADDR_UNLOCK(ifp);
352 return -1;
353 }
354
355 /* sanity check: g bit must not indicate "group" */
356 if (EUI64_GROUP(in6)) {
357 IF_ADDR_UNLOCK(ifp);
358 return -1;
359 }
360
361 /* convert EUI64 into IPv6 interface identifier */
362 EUI64_TO_IFID(in6);
363
364 /*
365 * sanity check: ifid must not be all zero, avoid conflict with
366 * subnet router anycast
367 */
368 if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
369 bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
370 IF_ADDR_UNLOCK(ifp);
371 return -1;
372 }
373
374 IF_ADDR_UNLOCK(ifp);
375 return 0;
376 }
377
378 /*
379 * Get interface identifier for the specified interface. If it is not
380 * available on ifp0, borrow interface identifier from other information
381 * sources.
382 *
383 * altifp - secondary EUI64 source
384 */
385 static int
386 get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
387 struct in6_addr *in6)
388 {
389 struct ifnet *ifp;
390
391 /* first, try to get it from the interface itself */
392 if (in6_get_hw_ifid(ifp0, in6) == 0) {
393 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
394 if_name(ifp0)));
395 goto success;
396 }
397
398 /* try secondary EUI64 source. this basically is for ATM PVC */
399 if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
400 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
401 if_name(ifp0), if_name(altifp)));
402 goto success;
403 }
404
405 /* next, try to get it from some other hardware interface */
406 IFNET_RLOCK_NOSLEEP();
407 for (ifp = V_ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
408 if (ifp == ifp0)
409 continue;
410 if (in6_get_hw_ifid(ifp, in6) != 0)
411 continue;
412
413 /*
414 * to borrow ifid from other interface, ifid needs to be
415 * globally unique
416 */
417 if (IFID_UNIVERSAL(in6)) {
418 nd6log((LOG_DEBUG,
419 "%s: borrow interface identifier from %s\n",
420 if_name(ifp0), if_name(ifp)));
421 IFNET_RUNLOCK_NOSLEEP();
422 goto success;
423 }
424 }
425 IFNET_RUNLOCK_NOSLEEP();
426
427 /* last resort: get from random number source */
428 if (get_rand_ifid(ifp, in6) == 0) {
429 nd6log((LOG_DEBUG,
430 "%s: interface identifier generated by random number\n",
431 if_name(ifp0)));
432 goto success;
433 }
434
435 printf("%s: failed to get interface identifier\n", if_name(ifp0));
436 return -1;
437
438 success:
439 nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
440 if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
441 in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
442 in6->s6_addr[14], in6->s6_addr[15]));
443 return 0;
444 }
445
446 /*
447 * altifp - secondary EUI64 source
448 */
449 static int
450 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
451 {
452 struct in6_ifaddr *ia;
453 struct in6_aliasreq ifra;
454 struct nd_prefixctl pr0;
455 int i, error;
456
457 /*
458 * configure link-local address.
459 */
460 bzero(&ifra, sizeof(ifra));
461
462 /*
463 * in6_update_ifa() does not use ifra_name, but we accurately set it
464 * for safety.
465 */
466 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
467
468 ifra.ifra_addr.sin6_family = AF_INET6;
469 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
470 ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
471 ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
472 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
473 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
474 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
475 } else {
476 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
477 nd6log((LOG_ERR,
478 "%s: no ifid available\n", if_name(ifp)));
479 return (-1);
480 }
481 }
482 if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
483 return (-1);
484
485 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
486 ifra.ifra_prefixmask.sin6_family = AF_INET6;
487 ifra.ifra_prefixmask.sin6_addr = in6mask64;
488 /* link-local addresses should NEVER expire. */
489 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
490 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
491
492 /*
493 * Now call in6_update_ifa() to do a bunch of procedures to configure
494 * a link-local address. We can set the 3rd argument to NULL, because
495 * we know there's no other link-local address on the interface
496 * and therefore we are adding one (instead of updating one).
497 */
498 if ((error = in6_update_ifa(ifp, &ifra, NULL,
499 IN6_IFAUPDATE_DADDELAY)) != 0) {
500 /*
501 * XXX: When the interface does not support IPv6, this call
502 * would fail in the SIOCSIFADDR ioctl. I believe the
503 * notification is rather confusing in this case, so just
504 * suppress it. (jinmei@kame.net 20010130)
505 */
506 if (error != EAFNOSUPPORT)
507 nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
508 "configure a link-local address on %s "
509 "(errno=%d)\n",
510 if_name(ifp), error));
511 return (-1);
512 }
513
514 ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
515 KASSERT(ia != NULL, ("%s: ia == NULL, ifp=%p", __func__, ifp));
516
517 ifa_free(&ia->ia_ifa);
518
519 /*
520 * Make the link-local prefix (fe80::%link/64) as on-link.
521 * Since we'd like to manage prefixes separately from addresses,
522 * we make an ND6 prefix structure for the link-local prefix,
523 * and add it to the prefix list as a never-expire prefix.
524 * XXX: this change might affect some existing code base...
525 */
526 bzero(&pr0, sizeof(pr0));
527 pr0.ndpr_ifp = ifp;
528 /* this should be 64 at this moment. */
529 pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
530 pr0.ndpr_prefix = ifra.ifra_addr;
531 /* apply the mask for safety. (nd6_prelist_add will apply it again) */
532 for (i = 0; i < 4; i++) {
533 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
534 in6mask64.s6_addr32[i];
535 }
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 draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
619 *
620 * when ifp == NULL, the caller is responsible for filling scopeid.
621 */
622 int
623 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
624 struct in6_addr *in6)
625 {
626 struct prison *pr;
627 const char *p;
628 u_char *q;
629 MD5_CTX ctxt;
630 u_int8_t digest[16];
631 char l;
632 char n[64]; /* a single label must not exceed 63 chars */
633
634 /*
635 * If no name is given and namelen is -1,
636 * we try to do the hostname lookup ourselves.
637 */
638 if (!name && namelen == -1) {
639 pr = curthread->td_ucred->cr_prison;
640 mtx_lock(&pr->pr_mtx);
641 name = pr->pr_hostname;
642 namelen = strlen(name);
643 } else
644 pr = NULL;
645 if (!name || !namelen) {
646 if (pr != NULL)
647 mtx_unlock(&pr->pr_mtx);
648 return -1;
649 }
650
651 p = name;
652 while (p && *p && *p != '.' && p - name < namelen)
653 p++;
654 if (p == name || p - name > sizeof(n) - 1) {
655 if (pr != NULL)
656 mtx_unlock(&pr->pr_mtx);
657 return -1; /* label too long */
658 }
659 l = p - name;
660 strncpy(n, name, l);
661 if (pr != NULL)
662 mtx_unlock(&pr->pr_mtx);
663 n[(int)l] = '\0';
664 for (q = n; *q; q++) {
665 if ('A' <= *q && *q <= 'Z')
666 *q = *q - 'A' + 'a';
667 }
668
669 /* generate 8 bytes of pseudo-random value. */
670 bzero(&ctxt, sizeof(ctxt));
671 MD5Init(&ctxt);
672 MD5Update(&ctxt, &l, sizeof(l));
673 MD5Update(&ctxt, n, l);
674 MD5Final(digest, &ctxt);
675
676 bzero(in6, sizeof(*in6));
677 in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
678 in6->s6_addr8[11] = 2;
679 bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
680 if (in6_setscope(in6, ifp, NULL))
681 return (-1); /* XXX: should not fail */
682
683 return 0;
684 }
685
686 /*
687 * XXX multiple loopback interface needs more care. for instance,
688 * nodelocal address needs to be configured onto only one of them.
689 * XXX multiple link-local address case
690 *
691 * altifp - secondary EUI64 source
692 */
693 void
694 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
695 {
696 struct in6_ifaddr *ia;
697 struct in6_addr in6;
698
699 /* some of the interfaces are inherently not IPv6 capable */
700 switch (ifp->if_type) {
701 case IFT_PFLOG:
702 case IFT_PFSYNC:
703 case IFT_CARP:
704 return;
705 }
706
707 /*
708 * quirks based on interface type
709 */
710 switch (ifp->if_type) {
711 #ifdef IFT_STF
712 case IFT_STF:
713 /*
714 * 6to4 interface is a very special kind of beast.
715 * no multicast, no linklocal. RFC2529 specifies how to make
716 * linklocals for 6to4 interface, but there's no use and
717 * it is rather harmful to have one.
718 */
719 goto statinit;
720 #endif
721 default:
722 break;
723 }
724
725 /*
726 * usually, we require multicast capability to the interface
727 */
728 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
729 nd6log((LOG_INFO, "in6_ifattach: "
730 "%s is not multicast capable, IPv6 not enabled\n",
731 if_name(ifp)));
732 return;
733 }
734
735 /*
736 * assign loopback address for loopback interface.
737 * XXX multiple loopback interface case.
738 */
739 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
740 struct ifaddr *ifa;
741
742 in6 = in6addr_loopback;
743 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &in6);
744 if (ifa == NULL) {
745 if (in6_ifattach_loopback(ifp) != 0)
746 return;
747 } else
748 ifa_free(ifa);
749 }
750
751 /*
752 * assign a link-local address, if there's none.
753 */
754 if (V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) {
755 ia = in6ifa_ifpforlinklocal(ifp, 0);
756 if (ia == NULL) {
757 if (in6_ifattach_linklocal(ifp, altifp) == 0) {
758 /* linklocal address assigned */
759 } else {
760 /* failed to assign linklocal address. bark? */
761 }
762 } else
763 ifa_free(&ia->ia_ifa);
764 }
765
766 #ifdef IFT_STF /* XXX */
767 statinit:
768 #endif
769
770 /* update dynamically. */
771 if (V_in6_maxmtu < ifp->if_mtu)
772 V_in6_maxmtu = ifp->if_mtu;
773 }
774
775 /*
776 * NOTE: in6_ifdetach() does not support loopback if at this moment.
777 * We don't need this function in bsdi, because interfaces are never removed
778 * from the ifnet list in bsdi.
779 */
780 void
781 in6_ifdetach(struct ifnet *ifp)
782 {
783 struct in6_ifaddr *ia;
784 struct ifaddr *ifa, *next;
785 struct radix_node_head *rnh;
786 struct rtentry *rt;
787 short rtflags;
788 struct sockaddr_in6 sin6;
789 struct in6_multi_mship *imm;
790
791 /* remove neighbor management table */
792 nd6_purge(ifp);
793
794 /* nuke any of IPv6 addresses we have */
795 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
796 if (ifa->ifa_addr->sa_family != AF_INET6)
797 continue;
798 in6_purgeaddr(ifa);
799 }
800
801 /* undo everything done by in6_ifattach(), just in case */
802 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
803 if (ifa->ifa_addr->sa_family != AF_INET6
804 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
805 continue;
806 }
807
808 ia = (struct in6_ifaddr *)ifa;
809
810 /*
811 * leave from multicast groups we have joined for the interface
812 */
813 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
814 LIST_REMOVE(imm, i6mm_chain);
815 in6_leavegroup(imm);
816 }
817
818 /* remove from the routing table */
819 if ((ia->ia_flags & IFA_ROUTE) &&
820 (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
821 rtflags = rt->rt_flags;
822 RTFREE_LOCKED(rt);
823 rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
824 (struct sockaddr *)&ia->ia_addr,
825 (struct sockaddr *)&ia->ia_prefixmask,
826 rtflags, (struct rtentry **)0);
827 }
828
829 /* remove from the linked list */
830 IF_ADDR_LOCK(ifp);
831 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
832 IF_ADDR_UNLOCK(ifp);
833 ifa_free(ifa); /* if_addrhead */
834
835 IN6_IFADDR_WLOCK();
836 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
837 IN6_IFADDR_WUNLOCK();
838 ifa_free(ifa);
839 }
840
841 in6_pcbpurgeif0(&V_udbinfo, ifp);
842 in6_pcbpurgeif0(&V_ripcbinfo, ifp);
843 /* leave from all multicast groups joined */
844 in6_purgemaddrs(ifp);
845
846 /*
847 * remove neighbor management table. we call it twice just to make
848 * sure we nuke everything. maybe we need just one call.
849 * XXX: since the first call did not release addresses, some prefixes
850 * might remain. We should call nd6_purge() again to release the
851 * prefixes after removing all addresses above.
852 * (Or can we just delay calling nd6_purge until at this point?)
853 */
854 nd6_purge(ifp);
855
856 /* remove route to link-local allnodes multicast (ff02::1) */
857 bzero(&sin6, sizeof(sin6));
858 sin6.sin6_len = sizeof(struct sockaddr_in6);
859 sin6.sin6_family = AF_INET6;
860 sin6.sin6_addr = in6addr_linklocal_allnodes;
861 if (in6_setscope(&sin6.sin6_addr, ifp, NULL))
862 /* XXX: should not fail */
863 return;
864 /* XXX grab lock first to avoid LOR */
865 rnh = rt_tables_get_rnh(0, AF_INET6);
866 if (rnh != NULL) {
867 RADIX_NODE_HEAD_LOCK(rnh);
868 rt = rtalloc1((struct sockaddr *)&sin6, 0, RTF_RNH_LOCKED);
869 if (rt) {
870 if (rt->rt_ifp == ifp)
871 rtexpunge(rt);
872 RTFREE_LOCKED(rt);
873 }
874 RADIX_NODE_HEAD_UNLOCK(rnh);
875 }
876 }
877
878 int
879 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
880 const u_int8_t *baseid, int generate)
881 {
882 u_int8_t nullbuf[8];
883 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
884
885 bzero(nullbuf, sizeof(nullbuf));
886 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
887 /* we've never created a random ID. Create a new one. */
888 generate = 1;
889 }
890
891 if (generate) {
892 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
893
894 /* generate_tmp_ifid will update seedn and buf */
895 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
896 ndi->randomid);
897 }
898 bcopy(ndi->randomid, retbuf, 8);
899
900 return (0);
901 }
902
903 void
904 in6_tmpaddrtimer(void *arg)
905 {
906 CURVNET_SET((struct vnet *) arg);
907 struct nd_ifinfo *ndi;
908 u_int8_t nullbuf[8];
909 struct ifnet *ifp;
910
911 callout_reset(&V_in6_tmpaddrtimer_ch,
912 (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
913 V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
914
915 bzero(nullbuf, sizeof(nullbuf));
916 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
917 ifp = TAILQ_NEXT(ifp, if_list)) {
918 ndi = ND_IFINFO(ifp);
919 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
920 /*
921 * We've been generating a random ID on this interface.
922 * Create a new one.
923 */
924 (void)generate_tmp_ifid(ndi->randomseed0,
925 ndi->randomseed1, ndi->randomid);
926 }
927 }
928
929 CURVNET_RESTORE();
930 }
931
932 static void
933 in6_purgemaddrs(struct ifnet *ifp)
934 {
935 LIST_HEAD(,in6_multi) purgeinms;
936 struct in6_multi *inm, *tinm;
937 struct ifmultiaddr *ifma;
938
939 LIST_INIT(&purgeinms);
940 IN6_MULTI_LOCK();
941
942 /*
943 * Extract list of in6_multi associated with the detaching ifp
944 * which the PF_INET6 layer is about to release.
945 * We need to do this as IF_ADDR_LOCK() may be re-acquired
946 * by code further down.
947 */
948 IF_ADDR_LOCK(ifp);
949 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
950 if (ifma->ifma_addr->sa_family != AF_INET6 ||
951 ifma->ifma_protospec == NULL)
952 continue;
953 inm = (struct in6_multi *)ifma->ifma_protospec;
954 LIST_INSERT_HEAD(&purgeinms, inm, in6m_entry);
955 }
956 IF_ADDR_UNLOCK(ifp);
957
958 LIST_FOREACH_SAFE(inm, &purgeinms, in6m_entry, tinm) {
959 LIST_REMOVE(inm, in6m_entry);
960 in6m_release_locked(inm);
961 }
962 mld_ifdetach(ifp);
963
964 IN6_MULTI_UNLOCK();
965 }
Cache object: 2762f515d80ad881d9ccc332ed6e617f
|