in6.c revision 1.216 1 /* $NetBSD: in6.c,v 1.216 2016/08/16 10:31:57 roy Exp $ */
2 /* $KAME: in6.c,v 1.198 2001/07/18 09:12:38 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)in.c 8.2 (Berkeley) 11/15/93
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: in6.c,v 1.216 2016/08/16 10:31:57 roy Exp $");
66
67 #ifdef _KERNEL_OPT
68 #include "opt_inet.h"
69 #include "opt_compat_netbsd.h"
70 #endif
71
72 #include <sys/param.h>
73 #include <sys/ioctl.h>
74 #include <sys/errno.h>
75 #include <sys/malloc.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/sockio.h>
79 #include <sys/systm.h>
80 #include <sys/proc.h>
81 #include <sys/time.h>
82 #include <sys/kernel.h>
83 #include <sys/syslog.h>
84 #include <sys/kauth.h>
85 #include <sys/cprng.h>
86 #include <sys/kmem.h>
87
88 #include <net/if.h>
89 #include <net/if_types.h>
90 #include <net/if_llatbl.h>
91 #include <net/if_ether.h>
92 #include <net/if_dl.h>
93 #include <net/pfil.h>
94 #include <net/route.h>
95
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet6/nd6.h>
102 #include <netinet6/mld6_var.h>
103 #include <netinet6/ip6_mroute.h>
104 #include <netinet6/in6_ifattach.h>
105 #include <netinet6/scope6_var.h>
106
107 #include <net/net_osdep.h>
108
109 #ifdef COMPAT_50
110 #include <compat/netinet6/in6_var.h>
111 #endif
112
113 MALLOC_DEFINE(M_IP6OPT, "ip6_options", "IPv6 options");
114
115 /* enable backward compatibility code for obsoleted ioctls */
116 #define COMPAT_IN6IFIOCTL
117
118 #ifdef IN6_DEBUG
119 #define IN6_DPRINTF(__fmt, ...) printf(__fmt, __VA_ARGS__)
120 #else
121 #define IN6_DPRINTF(__fmt, ...) do { } while (/*CONSTCOND*/0)
122 #endif /* IN6_DEBUG */
123
124 /*
125 * Definitions of some constant IP6 addresses.
126 */
127 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
128 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
129 const struct in6_addr in6addr_nodelocal_allnodes =
130 IN6ADDR_NODELOCAL_ALLNODES_INIT;
131 const struct in6_addr in6addr_linklocal_allnodes =
132 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
133 const struct in6_addr in6addr_linklocal_allrouters =
134 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
135
136 const struct in6_addr in6mask0 = IN6MASK0;
137 const struct in6_addr in6mask32 = IN6MASK32;
138 const struct in6_addr in6mask64 = IN6MASK64;
139 const struct in6_addr in6mask96 = IN6MASK96;
140 const struct in6_addr in6mask128 = IN6MASK128;
141
142 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
143 0, 0, IN6ADDR_ANY_INIT, 0};
144
145 struct pslist_head in6_ifaddr_list;
146 kmutex_t in6_ifaddr_lock;
147
148 static int in6_lifaddr_ioctl(struct socket *, u_long, void *,
149 struct ifnet *);
150 static int in6_ifaddprefix(struct in6_ifaddr *);
151 static int in6_ifremprefix(struct in6_ifaddr *);
152 static int in6_ifinit(struct ifnet *, struct in6_ifaddr *,
153 const struct sockaddr_in6 *, int);
154 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
155
156 void
157 in6_init(void)
158 {
159
160 PSLIST_INIT(&in6_ifaddr_list);
161 mutex_init(&in6_ifaddr_lock, MUTEX_DEFAULT, IPL_NONE);
162 }
163
164 /*
165 * Add ownaddr as loopback rtentry. We previously add the route only if
166 * necessary (ex. on a p2p link). However, since we now manage addresses
167 * separately from prefixes, we should always add the route. We can't
168 * rely on the cloning mechanism from the corresponding interface route
169 * any more.
170 */
171 void
172 in6_ifaddlocal(struct ifaddr *ifa)
173 {
174
175 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &in6addr_any) ||
176 (ifa->ifa_ifp->if_flags & IFF_POINTOPOINT &&
177 IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), IFA_DSTIN6(ifa))))
178 {
179 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
180 return;
181 }
182
183 rt_ifa_addlocal(ifa);
184 }
185
186 /*
187 * Remove loopback rtentry of ownaddr generated by in6_ifaddlocal(),
188 * if it exists.
189 */
190 void
191 in6_ifremlocal(struct ifaddr *ifa)
192 {
193 struct in6_ifaddr *ia;
194 struct ifaddr *alt_ifa = NULL;
195 int ia_count = 0;
196 struct psref psref;
197 int s;
198
199 /*
200 * Some of BSD variants do not remove cloned routes
201 * from an interface direct route, when removing the direct route
202 * (see comments in net/net_osdep.h). Even for variants that do remove
203 * cloned routes, they could fail to remove the cloned routes when
204 * we handle multple addresses that share a common prefix.
205 * So, we should remove the route corresponding to the deleted address.
206 */
207
208 /*
209 * Delete the entry only if exactly one ifaddr matches the
210 * address, ifa->ifa_addr.
211 *
212 * If more than one ifaddr matches, replace the ifaddr in
213 * the routing table, rt_ifa, with a different ifaddr than
214 * the one we are purging, ifa. It is important to do
215 * this, or else the routing table can accumulate dangling
216 * pointers rt->rt_ifa->ifa_ifp to destroyed interfaces,
217 * which will lead to crashes, later. (More than one ifaddr
218 * can match if we assign the same address to multiple---probably
219 * p2p---interfaces.)
220 *
221 * XXX An old comment at this place said, "we should avoid
222 * XXX such a configuration [i.e., interfaces with the same
223 * XXX addressed assigned --ed.] in IPv6...". I do not
224 * XXX agree, especially now that I have fixed the dangling
225 * XXX ifp-pointers bug.
226 */
227 s = pserialize_read_enter();
228 IN6_ADDRLIST_READER_FOREACH(ia) {
229 if (!IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr))
230 continue;
231 if (ia->ia_ifp != ifa->ifa_ifp)
232 alt_ifa = &ia->ia_ifa;
233 if (++ia_count > 1 && alt_ifa != NULL)
234 break;
235 }
236 if (ia_count > 1 && alt_ifa != NULL)
237 ifa_acquire(alt_ifa, &psref);
238 pserialize_read_exit(s);
239
240 if (ia_count == 0)
241 return;
242
243 rt_ifa_remlocal(ifa, ia_count == 1 ? NULL : alt_ifa);
244
245 if (ia_count > 1 && alt_ifa != NULL)
246 ifa_release(alt_ifa, &psref);
247 }
248
249 /* Add prefix route for the network. */
250 static int
251 in6_ifaddprefix(struct in6_ifaddr *ia)
252 {
253 int error, flags = 0;
254
255 if (in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) == 128) {
256 if (ia->ia_dstaddr.sin6_family != AF_INET6)
257 /* We don't need to install a host route. */
258 return 0;
259 flags |= RTF_HOST;
260 } else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) {
261 /* Loopback prefix routes are always host routes. */
262 flags |= RTF_HOST;
263 }
264
265 /* Is this a connected route for neighbour discovery? */
266 if (nd6_need_cache(ia->ia_ifp))
267 flags |= RTF_CONNECTED;
268
269 if ((error = rtinit(&ia->ia_ifa, RTM_ADD, RTF_UP | flags)) == 0)
270 ia->ia_flags |= IFA_ROUTE;
271 else if (error == EEXIST)
272 /* Existance of the route is not an error. */
273 error = 0;
274
275 return error;
276 }
277
278 /* Delete network prefix route if present.
279 * Re-add it to another address if the prefix matches. */
280 static int
281 in6_ifremprefix(struct in6_ifaddr *target)
282 {
283 int error, s;
284 struct in6_ifaddr *ia;
285
286 if ((target->ia_flags & IFA_ROUTE) == 0)
287 return 0;
288
289 s = pserialize_read_enter();
290 IN6_ADDRLIST_READER_FOREACH(ia) {
291 if (target->ia_dstaddr.sin6_len) {
292 if (ia->ia_dstaddr.sin6_len == 0 ||
293 !IN6_ARE_ADDR_EQUAL(&ia->ia_dstaddr.sin6_addr,
294 &target->ia_dstaddr.sin6_addr))
295 continue;
296 } else {
297 if (!IN6_ARE_MASKED_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
298 &target->ia_addr.sin6_addr,
299 &target->ia_prefixmask.sin6_addr))
300 continue;
301 }
302
303 /*
304 * if we got a matching prefix route, move IFA_ROUTE to him
305 */
306 if ((ia->ia_flags & IFA_ROUTE) == 0) {
307 struct psref psref;
308 int bound = curlwp_bind();
309
310 ia6_acquire(ia, &psref);
311 pserialize_read_exit(s);
312
313 rtinit(&target->ia_ifa, RTM_DELETE, 0);
314 target->ia_flags &= ~IFA_ROUTE;
315
316 error = in6_ifaddprefix(ia);
317
318 ia6_release(ia, &psref);
319 curlwp_bindx(bound);
320
321 return error;
322 }
323 }
324 pserialize_read_exit(s);
325
326 /*
327 * noone seem to have prefix route. remove it.
328 */
329 rtinit(&target->ia_ifa, RTM_DELETE, 0);
330 target->ia_flags &= ~IFA_ROUTE;
331 return 0;
332 }
333
334 int
335 in6_mask2len(struct in6_addr *mask, u_char *lim0)
336 {
337 int x = 0, y;
338 u_char *lim = lim0, *p;
339
340 /* ignore the scope_id part */
341 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
342 lim = (u_char *)mask + sizeof(*mask);
343 for (p = (u_char *)mask; p < lim; x++, p++) {
344 if (*p != 0xff)
345 break;
346 }
347 y = 0;
348 if (p < lim) {
349 for (y = 0; y < NBBY; y++) {
350 if ((*p & (0x80 >> y)) == 0)
351 break;
352 }
353 }
354
355 /*
356 * when the limit pointer is given, do a stricter check on the
357 * remaining bits.
358 */
359 if (p < lim) {
360 if (y != 0 && (*p & (0x00ff >> y)) != 0)
361 return -1;
362 for (p = p + 1; p < lim; p++)
363 if (*p != 0)
364 return -1;
365 }
366
367 return x * NBBY + y;
368 }
369
370 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
371 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
372
373 static int
374 in6_control1(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
375 {
376 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
377 struct in6_ifaddr *ia = NULL;
378 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
379 struct sockaddr_in6 *sa6;
380 int error, bound;
381 struct psref psref;
382 bool run_hooks = false;
383
384 switch (cmd) {
385 case SIOCAADDRCTL_POLICY:
386 case SIOCDADDRCTL_POLICY:
387 /* Privileged. */
388 return in6_src_ioctl(cmd, data);
389 /*
390 * XXX: Fix me, once we fix SIOCSIFADDR, SIOCIFDSTADDR, etc.
391 */
392 case SIOCSIFADDR:
393 case SIOCSIFDSTADDR:
394 case SIOCSIFBRDADDR:
395 case SIOCSIFNETMASK:
396 return EOPNOTSUPP;
397 case SIOCGETSGCNT_IN6:
398 case SIOCGETMIFCNT_IN6:
399 return mrt6_ioctl(cmd, data);
400 case SIOCGIFADDRPREF:
401 case SIOCSIFADDRPREF:
402 if (ifp == NULL)
403 return EINVAL;
404 return ifaddrpref_ioctl(so, cmd, data, ifp);
405 }
406
407 if (ifp == NULL)
408 return EOPNOTSUPP;
409
410 switch (cmd) {
411 case SIOCSNDFLUSH_IN6:
412 case SIOCSPFXFLUSH_IN6:
413 case SIOCSRTRFLUSH_IN6:
414 case SIOCSDEFIFACE_IN6:
415 case SIOCSIFINFO_FLAGS:
416 case SIOCSIFINFO_IN6:
417 /* Privileged. */
418 /* FALLTHROUGH */
419 case OSIOCGIFINFO_IN6:
420 case SIOCGIFINFO_IN6:
421 case SIOCGDRLST_IN6:
422 case SIOCGPRLST_IN6:
423 case SIOCGNBRINFO_IN6:
424 case SIOCGDEFIFACE_IN6:
425 return nd6_ioctl(cmd, data, ifp);
426 }
427
428 switch (cmd) {
429 case SIOCSIFPREFIX_IN6:
430 case SIOCDIFPREFIX_IN6:
431 case SIOCAIFPREFIX_IN6:
432 case SIOCCIFPREFIX_IN6:
433 case SIOCSGIFPREFIX_IN6:
434 case SIOCGIFPREFIX_IN6:
435 log(LOG_NOTICE,
436 "prefix ioctls are now invalidated. "
437 "please use ifconfig.\n");
438 return EOPNOTSUPP;
439 }
440
441 switch (cmd) {
442 case SIOCALIFADDR:
443 case SIOCDLIFADDR:
444 /* Privileged. */
445 /* FALLTHROUGH */
446 case SIOCGLIFADDR:
447 return in6_lifaddr_ioctl(so, cmd, data, ifp);
448 }
449
450 /*
451 * Find address for this interface, if it exists.
452 *
453 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
454 * only, and used the first interface address as the target of other
455 * operations (without checking ifra_addr). This was because netinet
456 * code/API assumed at most 1 interface address per interface.
457 * Since IPv6 allows a node to assign multiple addresses
458 * on a single interface, we almost always look and check the
459 * presence of ifra_addr, and reject invalid ones here.
460 * It also decreases duplicated code among SIOC*_IN6 operations.
461 */
462 switch (cmd) {
463 case SIOCAIFADDR_IN6:
464 #ifdef OSIOCAIFADDR_IN6
465 case OSIOCAIFADDR_IN6:
466 #endif
467 #ifdef OSIOCSIFPHYADDR_IN6
468 case OSIOCSIFPHYADDR_IN6:
469 #endif
470 case SIOCSIFPHYADDR_IN6:
471 sa6 = &ifra->ifra_addr;
472 break;
473 case SIOCSIFADDR_IN6:
474 case SIOCGIFADDR_IN6:
475 case SIOCSIFDSTADDR_IN6:
476 case SIOCSIFNETMASK_IN6:
477 case SIOCGIFDSTADDR_IN6:
478 case SIOCGIFNETMASK_IN6:
479 case SIOCDIFADDR_IN6:
480 case SIOCGIFPSRCADDR_IN6:
481 case SIOCGIFPDSTADDR_IN6:
482 case SIOCGIFAFLAG_IN6:
483 case SIOCSNDFLUSH_IN6:
484 case SIOCSPFXFLUSH_IN6:
485 case SIOCSRTRFLUSH_IN6:
486 case SIOCGIFALIFETIME_IN6:
487 #ifdef OSIOCGIFALIFETIME_IN6
488 case OSIOCGIFALIFETIME_IN6:
489 #endif
490 case SIOCGIFSTAT_IN6:
491 case SIOCGIFSTAT_ICMP6:
492 sa6 = &ifr->ifr_addr;
493 break;
494 default:
495 sa6 = NULL;
496 break;
497 }
498
499 error = 0;
500 bound = curlwp_bind();
501 if (sa6 && sa6->sin6_family == AF_INET6) {
502 if (sa6->sin6_scope_id != 0)
503 error = sa6_embedscope(sa6, 0);
504 else
505 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
506 if (error != 0)
507 goto out;
508 ia = in6ifa_ifpwithaddr_psref(ifp, &sa6->sin6_addr, &psref);
509 } else
510 ia = NULL;
511
512 switch (cmd) {
513 case SIOCSIFADDR_IN6:
514 case SIOCSIFDSTADDR_IN6:
515 case SIOCSIFNETMASK_IN6:
516 /*
517 * Since IPv6 allows a node to assign multiple addresses
518 * on a single interface, SIOCSIFxxx ioctls are deprecated.
519 */
520 error = EINVAL;
521 goto release;
522
523 case SIOCDIFADDR_IN6:
524 /*
525 * for IPv4, we look for existing in_ifaddr here to allow
526 * "ifconfig if0 delete" to remove the first IPv4 address on
527 * the interface. For IPv6, as the spec allows multiple
528 * interface address from the day one, we consider "remove the
529 * first one" semantics to be not preferable.
530 */
531 if (ia == NULL) {
532 error = EADDRNOTAVAIL;
533 goto out;
534 }
535 /* FALLTHROUGH */
536 #ifdef OSIOCAIFADDR_IN6
537 case OSIOCAIFADDR_IN6:
538 #endif
539 case SIOCAIFADDR_IN6:
540 /*
541 * We always require users to specify a valid IPv6 address for
542 * the corresponding operation.
543 */
544 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
545 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
546 error = EAFNOSUPPORT;
547 goto release;
548 }
549 /* Privileged. */
550
551 break;
552
553 case SIOCGIFADDR_IN6:
554 /* This interface is basically deprecated. use SIOCGIFCONF. */
555 /* FALLTHROUGH */
556 case SIOCGIFAFLAG_IN6:
557 case SIOCGIFNETMASK_IN6:
558 case SIOCGIFDSTADDR_IN6:
559 case SIOCGIFALIFETIME_IN6:
560 #ifdef OSIOCGIFALIFETIME_IN6
561 case OSIOCGIFALIFETIME_IN6:
562 #endif
563 /* must think again about its semantics */
564 if (ia == NULL) {
565 error = EADDRNOTAVAIL;
566 goto out;
567 }
568 break;
569 }
570
571 switch (cmd) {
572
573 case SIOCGIFADDR_IN6:
574 ifr->ifr_addr = ia->ia_addr;
575 error = sa6_recoverscope(&ifr->ifr_addr);
576 break;
577
578 case SIOCGIFDSTADDR_IN6:
579 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
580 error = EINVAL;
581 break;
582 }
583 /*
584 * XXX: should we check if ifa_dstaddr is NULL and return
585 * an error?
586 */
587 ifr->ifr_dstaddr = ia->ia_dstaddr;
588 error = sa6_recoverscope(&ifr->ifr_dstaddr);
589 break;
590
591 case SIOCGIFNETMASK_IN6:
592 ifr->ifr_addr = ia->ia_prefixmask;
593 break;
594
595 case SIOCGIFAFLAG_IN6:
596 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
597 break;
598
599 case SIOCGIFSTAT_IN6:
600 if (ifp == NULL) {
601 error = EINVAL;
602 break;
603 }
604 memset(&ifr->ifr_ifru.ifru_stat, 0,
605 sizeof(ifr->ifr_ifru.ifru_stat));
606 ifr->ifr_ifru.ifru_stat =
607 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
608 break;
609
610 case SIOCGIFSTAT_ICMP6:
611 if (ifp == NULL) {
612 error = EINVAL;
613 break;
614 }
615 memset(&ifr->ifr_ifru.ifru_icmp6stat, 0,
616 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
617 ifr->ifr_ifru.ifru_icmp6stat =
618 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
619 break;
620
621 #ifdef OSIOCGIFALIFETIME_IN6
622 case OSIOCGIFALIFETIME_IN6:
623 #endif
624 case SIOCGIFALIFETIME_IN6:
625 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
626 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
627 time_t maxexpire;
628 struct in6_addrlifetime *retlt =
629 &ifr->ifr_ifru.ifru_lifetime;
630
631 /*
632 * XXX: adjust expiration time assuming time_t is
633 * signed.
634 */
635 maxexpire = ((time_t)~0) &
636 ~((time_t)1 << ((sizeof(maxexpire) * NBBY) - 1));
637 if (ia->ia6_lifetime.ia6t_vltime <
638 maxexpire - ia->ia6_updatetime) {
639 retlt->ia6t_expire = ia->ia6_updatetime +
640 ia->ia6_lifetime.ia6t_vltime;
641 retlt->ia6t_expire = retlt->ia6t_expire ?
642 time_mono_to_wall(retlt->ia6t_expire) :
643 0;
644 } else
645 retlt->ia6t_expire = maxexpire;
646 }
647 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
648 time_t maxexpire;
649 struct in6_addrlifetime *retlt =
650 &ifr->ifr_ifru.ifru_lifetime;
651
652 /*
653 * XXX: adjust expiration time assuming time_t is
654 * signed.
655 */
656 maxexpire = ((time_t)~0) &
657 ~((time_t)1 << ((sizeof(maxexpire) * NBBY) - 1));
658 if (ia->ia6_lifetime.ia6t_pltime <
659 maxexpire - ia->ia6_updatetime) {
660 retlt->ia6t_preferred = ia->ia6_updatetime +
661 ia->ia6_lifetime.ia6t_pltime;
662 retlt->ia6t_preferred = retlt->ia6t_preferred ?
663 time_mono_to_wall(retlt->ia6t_preferred) :
664 0;
665 } else
666 retlt->ia6t_preferred = maxexpire;
667 }
668 #ifdef OSIOCFIFALIFETIME_IN6
669 if (cmd == OSIOCFIFALIFETIME_IN6)
670 in6_addrlifetime_to_in6_addrlifetime50(
671 &ifr->ifru.ifru_lifetime);
672 #endif
673 break;
674
675 #ifdef OSIOCAIFADDR_IN6
676 case OSIOCAIFADDR_IN6:
677 in6_aliasreq50_to_in6_aliasreq(ifra);
678 /*FALLTHROUGH*/
679 #endif
680 case SIOCAIFADDR_IN6:
681 {
682 struct in6_addrlifetime *lt;
683
684 /* reject read-only flags */
685 if ((ifra->ifra_flags & IN6_IFF_DUPLICATED) != 0 ||
686 (ifra->ifra_flags & IN6_IFF_DETACHED) != 0 ||
687 (ifra->ifra_flags & IN6_IFF_TENTATIVE) != 0 ||
688 (ifra->ifra_flags & IN6_IFF_NODAD) != 0 ||
689 (ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0) {
690 error = EINVAL;
691 break;
692 }
693 /*
694 * ia6t_expire and ia6t_preferred won't be used for now,
695 * so just in case.
696 */
697 lt = &ifra->ifra_lifetime;
698 if (lt->ia6t_expire != 0)
699 lt->ia6t_expire = time_wall_to_mono(lt->ia6t_expire);
700 if (lt->ia6t_preferred != 0)
701 lt->ia6t_preferred =
702 time_wall_to_mono(lt->ia6t_preferred);
703 /*
704 * make (ia == NULL) or update (ia != NULL) the interface
705 * address structure, and link it to the list.
706 */
707 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
708 break;
709 run_hooks = true;
710 break;
711 }
712
713 case SIOCDIFADDR_IN6:
714 {
715 struct nd_prefix *pr;
716
717 /*
718 * If the address being deleted is the only one that owns
719 * the corresponding prefix, expire the prefix as well.
720 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
721 */
722 pr = ia->ia6_ndpr;
723 ia6_release(ia, &psref);
724 in6_purgeaddr(&ia->ia_ifa);
725 ia = NULL;
726 if (pr && pr->ndpr_refcnt == 0)
727 prelist_remove(pr);
728 run_hooks = true;
729 break;
730 }
731
732 default:
733 error = ENOTTY;
734 }
735 release:
736 ia6_release(ia, &psref);
737
738 if (run_hooks)
739 pfil_run_hooks(if_pfil, (struct mbuf **)cmd, ifp, PFIL_IFADDR);
740 out:
741 curlwp_bindx(bound);
742 return error;
743 }
744
745 int
746 in6_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
747 {
748 int error, s;
749
750 switch (cmd) {
751 case SIOCSNDFLUSH_IN6:
752 case SIOCSPFXFLUSH_IN6:
753 case SIOCSRTRFLUSH_IN6:
754 case SIOCSDEFIFACE_IN6:
755 case SIOCSIFINFO_FLAGS:
756 case SIOCSIFINFO_IN6:
757
758 case SIOCALIFADDR:
759 case SIOCDLIFADDR:
760
761 case SIOCDIFADDR_IN6:
762 #ifdef OSIOCAIFADDR_IN6
763 case OSIOCAIFADDR_IN6:
764 #endif
765 case SIOCAIFADDR_IN6:
766
767 case SIOCAADDRCTL_POLICY:
768 case SIOCDADDRCTL_POLICY:
769
770 if (kauth_authorize_network(curlwp->l_cred,
771 KAUTH_NETWORK_SOCKET,
772 KAUTH_REQ_NETWORK_SOCKET_SETPRIV,
773 so, NULL, NULL))
774 return EPERM;
775 break;
776 }
777
778 s = splnet();
779 mutex_enter(softnet_lock);
780 error = in6_control1(so , cmd, data, ifp);
781 mutex_exit(softnet_lock);
782 splx(s);
783 return error;
784 }
785
786 /*
787 * Update parameters of an IPv6 interface address.
788 * If necessary, a new entry is created and linked into address chains.
789 * This function is separated from in6_control().
790 * XXX: should this be performed under splnet()?
791 */
792 static int
793 in6_update_ifa1(struct ifnet *ifp, struct in6_aliasreq *ifra,
794 struct in6_ifaddr *ia, int flags)
795 {
796 int error = 0, hostIsNew = 0, plen = -1;
797 struct sockaddr_in6 dst6;
798 struct in6_addrlifetime *lt;
799 struct in6_multi_mship *imm;
800 struct in6_multi *in6m_sol;
801 struct rtentry *rt;
802 int dad_delay, was_tentative;
803
804 in6m_sol = NULL;
805
806 /* Validate parameters */
807 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
808 return EINVAL;
809
810 /*
811 * The destination address for a p2p link must have a family
812 * of AF_UNSPEC or AF_INET6.
813 */
814 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
815 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
816 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
817 return EAFNOSUPPORT;
818 /*
819 * validate ifra_prefixmask. don't check sin6_family, netmask
820 * does not carry fields other than sin6_len.
821 */
822 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
823 return EINVAL;
824 /*
825 * Because the IPv6 address architecture is classless, we require
826 * users to specify a (non 0) prefix length (mask) for a new address.
827 * We also require the prefix (when specified) mask is valid, and thus
828 * reject a non-consecutive mask.
829 */
830 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
831 return EINVAL;
832 if (ifra->ifra_prefixmask.sin6_len != 0) {
833 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
834 (u_char *)&ifra->ifra_prefixmask +
835 ifra->ifra_prefixmask.sin6_len);
836 if (plen <= 0)
837 return EINVAL;
838 } else {
839 /*
840 * In this case, ia must not be NULL. We just use its prefix
841 * length.
842 */
843 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
844 }
845 /*
846 * If the destination address on a p2p interface is specified,
847 * and the address is a scoped one, validate/set the scope
848 * zone identifier.
849 */
850 dst6 = ifra->ifra_dstaddr;
851 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
852 (dst6.sin6_family == AF_INET6)) {
853 struct in6_addr in6_tmp;
854 u_int32_t zoneid;
855
856 in6_tmp = dst6.sin6_addr;
857 if (in6_setscope(&in6_tmp, ifp, &zoneid))
858 return EINVAL; /* XXX: should be impossible */
859
860 if (dst6.sin6_scope_id != 0) {
861 if (dst6.sin6_scope_id != zoneid)
862 return EINVAL;
863 } else /* user omit to specify the ID. */
864 dst6.sin6_scope_id = zoneid;
865
866 /* convert into the internal form */
867 if (sa6_embedscope(&dst6, 0))
868 return EINVAL; /* XXX: should be impossible */
869 }
870 /*
871 * The destination address can be specified only for a p2p or a
872 * loopback interface. If specified, the corresponding prefix length
873 * must be 128.
874 */
875 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
876 #ifdef FORCE_P2PPLEN
877 int i;
878 #endif
879
880 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
881 /* XXX: noisy message */
882 nd6log(LOG_INFO, "a destination can "
883 "be specified for a p2p or a loopback IF only\n");
884 return EINVAL;
885 }
886 if (plen != 128) {
887 nd6log(LOG_INFO, "prefixlen should "
888 "be 128 when dstaddr is specified\n");
889 #ifdef FORCE_P2PPLEN
890 /*
891 * To be compatible with old configurations,
892 * such as ifconfig gif0 inet6 2001::1 2001::2
893 * prefixlen 126, we override the specified
894 * prefixmask as if the prefix length was 128.
895 */
896 ifra->ifra_prefixmask.sin6_len =
897 sizeof(struct sockaddr_in6);
898 for (i = 0; i < 4; i++)
899 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i] =
900 0xffffffff;
901 plen = 128;
902 #else
903 return EINVAL;
904 #endif
905 }
906 }
907 /* lifetime consistency check */
908 lt = &ifra->ifra_lifetime;
909 if (lt->ia6t_pltime > lt->ia6t_vltime)
910 return EINVAL;
911 if (lt->ia6t_vltime == 0) {
912 /*
913 * the following log might be noisy, but this is a typical
914 * configuration mistake or a tool's bug.
915 */
916 nd6log(LOG_INFO, "valid lifetime is 0 for %s\n",
917 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
918
919 if (ia == NULL)
920 return 0; /* there's nothing to do */
921 }
922
923 /*
924 * If this is a new address, allocate a new ifaddr and link it
925 * into chains.
926 */
927 if (ia == NULL) {
928 hostIsNew = 1;
929 /*
930 * When in6_update_ifa() is called in a process of a received
931 * RA, it is called under an interrupt context. So, we should
932 * call malloc with M_NOWAIT.
933 */
934 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
935 M_NOWAIT);
936 if (ia == NULL)
937 return ENOBUFS;
938 memset(ia, 0, sizeof(*ia));
939 LIST_INIT(&ia->ia6_memberships);
940 /* Initialize the address and masks, and put time stamp */
941 ia->ia_ifa.ifa_addr = sin6tosa(&ia->ia_addr);
942 ia->ia_addr.sin6_family = AF_INET6;
943 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
944 ia->ia6_createtime = time_uptime;
945 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
946 /*
947 * XXX: some functions expect that ifa_dstaddr is not
948 * NULL for p2p interfaces.
949 */
950 ia->ia_ifa.ifa_dstaddr = sin6tosa(&ia->ia_dstaddr);
951 } else {
952 ia->ia_ifa.ifa_dstaddr = NULL;
953 }
954 ia->ia_ifa.ifa_netmask = sin6tosa(&ia->ia_prefixmask);
955
956 ia->ia_ifp = ifp;
957 IN6_ADDRLIST_ENTRY_INIT(ia);
958 ifa_psref_init(&ia->ia_ifa);
959 }
960
961 /* update timestamp */
962 ia->ia6_updatetime = time_uptime;
963
964 /* set prefix mask */
965 if (ifra->ifra_prefixmask.sin6_len) {
966 if (ia->ia_prefixmask.sin6_len) {
967 /*
968 * We prohibit changing the prefix length of an
969 * existing autoconf address, because the operation
970 * would confuse prefix management.
971 */
972 if (ia->ia6_ndpr != NULL &&
973 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) !=
974 plen)
975 {
976 nd6log(LOG_INFO, "the prefix length of an"
977 " existing (%s) autoconf address should"
978 " not be changed\n",
979 ip6_sprintf(&ia->ia_addr.sin6_addr));
980 error = EINVAL;
981 if (hostIsNew)
982 free(ia, M_IFADDR);
983 goto exit;
984 }
985
986 if (!IN6_ARE_ADDR_EQUAL(&ia->ia_prefixmask.sin6_addr,
987 &ifra->ifra_prefixmask.sin6_addr))
988 in6_ifremprefix(ia);
989 }
990 ia->ia_prefixmask = ifra->ifra_prefixmask;
991 }
992
993 /* Set destination address. */
994 if (dst6.sin6_family == AF_INET6) {
995 if (!IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
996 &ia->ia_dstaddr.sin6_addr))
997 in6_ifremprefix(ia);
998 ia->ia_dstaddr = dst6;
999 }
1000
1001 /*
1002 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1003 * to see if the address is deprecated or invalidated, but initialize
1004 * these members for applications.
1005 */
1006 ia->ia6_lifetime = ifra->ifra_lifetime;
1007 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1008 ia->ia6_lifetime.ia6t_expire =
1009 time_uptime + ia->ia6_lifetime.ia6t_vltime;
1010 } else
1011 ia->ia6_lifetime.ia6t_expire = 0;
1012 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1013 ia->ia6_lifetime.ia6t_preferred =
1014 time_uptime + ia->ia6_lifetime.ia6t_pltime;
1015 } else
1016 ia->ia6_lifetime.ia6t_preferred = 0;
1017
1018 /*
1019 * configure address flags.
1020 * We need to preserve tentative state so DAD works if
1021 * something adds the same address before DAD finishes.
1022 */
1023 was_tentative = ia->ia6_flags & (IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED);
1024 ia->ia6_flags = ifra->ifra_flags;
1025
1026 /*
1027 * Make the address tentative before joining multicast addresses,
1028 * so that corresponding MLD responses would not have a tentative
1029 * source address.
1030 */
1031 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1032 if (ifp->if_link_state == LINK_STATE_DOWN) {
1033 ia->ia6_flags |= IN6_IFF_DETACHED;
1034 ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1035 } else if ((hostIsNew || was_tentative) && if_do_dad(ifp))
1036 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1037
1038 /*
1039 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1040 * userland, make it deprecated.
1041 */
1042 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1043 ia->ia6_lifetime.ia6t_pltime = 0;
1044 ia->ia6_lifetime.ia6t_preferred = time_uptime;
1045 }
1046
1047 /* reset the interface and routing table appropriately. */
1048 error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew);
1049 if (error != 0) {
1050 if (hostIsNew)
1051 free(ia, M_IFADDR);
1052 goto exit;
1053 }
1054
1055 /*
1056 * We are done if we have simply modified an existing address.
1057 */
1058 if (!hostIsNew)
1059 return error;
1060
1061 /*
1062 * Insert ia to the global list and ifa to the interface's list.
1063 */
1064 mutex_enter(&in6_ifaddr_lock);
1065 IN6_ADDRLIST_WRITER_INSERT_TAIL(ia);
1066 mutex_exit(&in6_ifaddr_lock);
1067
1068 /* gain a refcnt for the link from in6_ifaddr */
1069 ifaref(&ia->ia_ifa);
1070 ifa_insert(ifp, &ia->ia_ifa);
1071
1072 /*
1073 * Beyond this point, we should call in6_purgeaddr upon an error,
1074 * not just go to unlink.
1075 */
1076
1077 /* join necessary multicast groups */
1078 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1079 struct sockaddr_in6 mltaddr, mltmask;
1080 struct in6_addr llsol;
1081
1082 /* join solicited multicast addr for new host id */
1083 memset(&llsol, 0, sizeof(struct in6_addr));
1084 llsol.s6_addr16[0] = htons(0xff02);
1085 llsol.s6_addr32[1] = 0;
1086 llsol.s6_addr32[2] = htonl(1);
1087 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
1088 llsol.s6_addr8[12] = 0xff;
1089 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
1090 /* XXX: should not happen */
1091 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
1092 goto cleanup;
1093 }
1094 dad_delay = 0;
1095 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1096 /*
1097 * We need a random delay for DAD on the address
1098 * being configured. It also means delaying
1099 * transmission of the corresponding MLD report to
1100 * avoid report collision.
1101 * [draft-ietf-ipv6-rfc2462bis-02.txt]
1102 */
1103 dad_delay = cprng_fast32() %
1104 (MAX_RTR_SOLICITATION_DELAY * hz);
1105 }
1106
1107 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1108 /* join solicited multicast addr for new host id */
1109 imm = in6_joingroup(ifp, &llsol, &error, dad_delay);
1110 if (!imm) {
1111 nd6log(LOG_ERR,
1112 "addmulti failed for %s on %s (errno=%d)\n",
1113 ip6_sprintf(&llsol), if_name(ifp), error);
1114 goto cleanup;
1115 }
1116 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1117 in6m_sol = imm->i6mm_maddr;
1118
1119 sockaddr_in6_init(&mltmask, &in6mask32, 0, 0, 0);
1120
1121 /*
1122 * join link-local all-nodes address
1123 */
1124 sockaddr_in6_init(&mltaddr, &in6addr_linklocal_allnodes,
1125 0, 0, 0);
1126 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1127 goto cleanup; /* XXX: should not fail */
1128
1129 /*
1130 * XXX: do we really need this automatic routes?
1131 * We should probably reconsider this stuff. Most applications
1132 * actually do not need the routes, since they usually specify
1133 * the outgoing interface.
1134 */
1135 rt = rtalloc1(sin6tosa(&mltaddr), 0);
1136 if (rt) {
1137 if (memcmp(&mltaddr.sin6_addr,
1138 &satocsin6(rt_getkey(rt))->sin6_addr,
1139 MLTMASK_LEN)) {
1140 rtfree(rt);
1141 rt = NULL;
1142 } else if (rt->rt_ifp != ifp) {
1143 IN6_DPRINTF("%s: rt_ifp %p -> %p (%s) "
1144 "network %04x:%04x::/32 = %04x:%04x::/32\n",
1145 __func__, rt->rt_ifp, ifp, ifp->if_xname,
1146 ntohs(mltaddr.sin6_addr.s6_addr16[0]),
1147 ntohs(mltaddr.sin6_addr.s6_addr16[1]),
1148 satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[0],
1149 satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[1]);
1150 rt_replace_ifa(rt, &ia->ia_ifa);
1151 rt->rt_ifp = ifp;
1152 }
1153 }
1154 if (!rt) {
1155 struct rt_addrinfo info;
1156
1157 memset(&info, 0, sizeof(info));
1158 info.rti_info[RTAX_DST] = sin6tosa(&mltaddr);
1159 info.rti_info[RTAX_GATEWAY] = sin6tosa(&ia->ia_addr);
1160 info.rti_info[RTAX_NETMASK] = sin6tosa(&mltmask);
1161 info.rti_info[RTAX_IFA] = sin6tosa(&ia->ia_addr);
1162 /* XXX: we need RTF_CONNECTED to fake nd6_rtrequest */
1163 info.rti_flags = RTF_UP | RTF_CONNECTED;
1164 error = rtrequest1(RTM_ADD, &info, NULL);
1165 if (error)
1166 goto cleanup;
1167 } else {
1168 rtfree(rt);
1169 }
1170 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1171 if (!imm) {
1172 nd6log(LOG_WARNING,
1173 "addmulti failed for %s on %s (errno=%d)\n",
1174 ip6_sprintf(&mltaddr.sin6_addr),
1175 if_name(ifp), error);
1176 goto cleanup;
1177 }
1178 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1179
1180 /*
1181 * join node information group address
1182 */
1183 dad_delay = 0;
1184 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1185 /*
1186 * The spec doesn't say anything about delay for this
1187 * group, but the same logic should apply.
1188 */
1189 dad_delay = cprng_fast32() %
1190 (MAX_RTR_SOLICITATION_DELAY * hz);
1191 }
1192 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr) != 0)
1193 ;
1194 else if ((imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1195 dad_delay)) == NULL) { /* XXX jinmei */
1196 nd6log(LOG_WARNING,
1197 "addmulti failed for %s on %s (errno=%d)\n",
1198 ip6_sprintf(&mltaddr.sin6_addr),
1199 if_name(ifp), error);
1200 /* XXX not very fatal, go on... */
1201 } else {
1202 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1203 }
1204
1205
1206 /*
1207 * join interface-local all-nodes address.
1208 * (ff01::1%ifN, and ff01::%ifN/32)
1209 */
1210 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1211 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1212 goto cleanup; /* XXX: should not fail */
1213
1214 /* XXX: again, do we really need the route? */
1215 rt = rtalloc1(sin6tosa(&mltaddr), 0);
1216 if (rt) {
1217 /* 32bit came from "mltmask" */
1218 if (memcmp(&mltaddr.sin6_addr,
1219 &satocsin6(rt_getkey(rt))->sin6_addr,
1220 32 / NBBY)) {
1221 rtfree(rt);
1222 rt = NULL;
1223 } else if (rt->rt_ifp != ifp) {
1224 IN6_DPRINTF("%s: rt_ifp %p -> %p (%s) "
1225 "network %04x:%04x::/32 = %04x:%04x::/32\n",
1226 __func__, rt->rt_ifp, ifp, ifp->if_xname,
1227 ntohs(mltaddr.sin6_addr.s6_addr16[0]),
1228 ntohs(mltaddr.sin6_addr.s6_addr16[1]),
1229 satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[0],
1230 satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[1]);
1231 rt_replace_ifa(rt, &ia->ia_ifa);
1232 rt->rt_ifp = ifp;
1233 }
1234 }
1235 if (!rt) {
1236 struct rt_addrinfo info;
1237
1238 memset(&info, 0, sizeof(info));
1239 info.rti_info[RTAX_DST] = sin6tosa(&mltaddr);
1240 info.rti_info[RTAX_GATEWAY] = sin6tosa(&ia->ia_addr);
1241 info.rti_info[RTAX_NETMASK] = sin6tosa(&mltmask);
1242 info.rti_info[RTAX_IFA] = sin6tosa(&ia->ia_addr);
1243 info.rti_flags = RTF_UP | RTF_CONNECTED;
1244 error = rtrequest1(RTM_ADD, &info, NULL);
1245 if (error)
1246 goto cleanup;
1247 #undef MLTMASK_LEN
1248 } else {
1249 rtfree(rt);
1250 }
1251 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1252 if (!imm) {
1253 nd6log(LOG_WARNING,
1254 "addmulti failed for %s on %s (errno=%d)\n",
1255 ip6_sprintf(&mltaddr.sin6_addr),
1256 if_name(ifp), error);
1257 goto cleanup;
1258 } else {
1259 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1260 }
1261 }
1262
1263 /* Add local address to lltable, if necessary (ex. on p2p link). */
1264 error = nd6_add_ifa_lle(ia);
1265 if (error != 0)
1266 goto cleanup;
1267
1268 /*
1269 * Perform DAD, if needed.
1270 * XXX It may be of use, if we can administratively
1271 * disable DAD.
1272 */
1273 if (hostIsNew && if_do_dad(ifp) &&
1274 ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1275 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1276 {
1277 int mindelay, maxdelay;
1278
1279 dad_delay = 0;
1280 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1281 /*
1282 * We need to impose a delay before sending an NS
1283 * for DAD. Check if we also needed a delay for the
1284 * corresponding MLD message. If we did, the delay
1285 * should be larger than the MLD delay (this could be
1286 * relaxed a bit, but this simple logic is at least
1287 * safe).
1288 */
1289 mindelay = 0;
1290 if (in6m_sol != NULL &&
1291 in6m_sol->in6m_state == MLD_REPORTPENDING) {
1292 mindelay = in6m_sol->in6m_timer;
1293 }
1294 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1295 if (maxdelay - mindelay == 0)
1296 dad_delay = 0;
1297 else {
1298 dad_delay =
1299 (cprng_fast32() % (maxdelay - mindelay)) +
1300 mindelay;
1301 }
1302 }
1303 /* +1 ensures callout is always used */
1304 nd6_dad_start(&ia->ia_ifa, dad_delay + 1);
1305 }
1306
1307 return 0;
1308
1309 cleanup:
1310 in6_purgeaddr(&ia->ia_ifa);
1311 exit:
1312 return error;
1313 }
1314
1315 int
1316 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
1317 struct in6_ifaddr *ia, int flags)
1318 {
1319 int rc, s;
1320
1321 s = splnet();
1322 rc = in6_update_ifa1(ifp, ifra, ia, flags);
1323 splx(s);
1324 return rc;
1325 }
1326
1327 void
1328 in6_purgeaddr(struct ifaddr *ifa)
1329 {
1330 struct ifnet *ifp = ifa->ifa_ifp;
1331 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1332 struct in6_multi_mship *imm;
1333
1334 /* stop DAD processing */
1335 nd6_dad_stop(ifa);
1336
1337 /* Delete any network route. */
1338 in6_ifremprefix(ia);
1339
1340 /* Remove ownaddr's loopback rtentry, if it exists. */
1341 in6_ifremlocal(&(ia->ia_ifa));
1342
1343 /*
1344 * leave from multicast groups we have joined for the interface
1345 */
1346 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1347 LIST_REMOVE(imm, i6mm_chain);
1348 in6_leavegroup(imm);
1349 }
1350
1351 in6_unlink_ifa(ia, ifp);
1352 }
1353
1354 static void
1355 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1356 {
1357 int s = splnet();
1358
1359 mutex_enter(&in6_ifaddr_lock);
1360 IN6_ADDRLIST_WRITER_REMOVE(ia);
1361 ifa_remove(ifp, &ia->ia_ifa);
1362 mutex_exit(&in6_ifaddr_lock);
1363
1364 /*
1365 * XXX thorpej (at) NetBSD.org -- if the interface is going
1366 * XXX away, don't save the multicast entries, delete them!
1367 */
1368 if (LIST_EMPTY(&ia->ia6_multiaddrs))
1369 ;
1370 else if (if_is_deactivated(ia->ia_ifa.ifa_ifp)) {
1371 struct in6_multi *in6m, *next;
1372
1373 for (in6m = LIST_FIRST(&ia->ia6_multiaddrs); in6m != NULL;
1374 in6m = next) {
1375 next = LIST_NEXT(in6m, in6m_entry);
1376 in6_delmulti(in6m);
1377 }
1378 } else
1379 in6_savemkludge(ia);
1380
1381 /*
1382 * Release the reference to the base prefix. There should be a
1383 * positive reference.
1384 */
1385 if (ia->ia6_ndpr == NULL) {
1386 nd6log(LOG_NOTICE, "autoconf'ed address %p has no prefix\n",
1387 ia);
1388 } else {
1389 ia->ia6_ndpr->ndpr_refcnt--;
1390 ia->ia6_ndpr = NULL;
1391 }
1392
1393 /*
1394 * Also, if the address being removed is autoconf'ed, call
1395 * pfxlist_onlink_check() since the release might affect the status of
1396 * other (detached) addresses.
1397 */
1398 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0)
1399 pfxlist_onlink_check();
1400
1401 IN6_ADDRLIST_ENTRY_DESTROY(ia);
1402
1403 /*
1404 * release another refcnt for the link from in6_ifaddr.
1405 * Note that we should decrement the refcnt at least once for all *BSD.
1406 */
1407 ifafree(&ia->ia_ifa);
1408
1409 splx(s);
1410 }
1411
1412 void
1413 in6_purgeif(struct ifnet *ifp)
1414 {
1415
1416 in6_ifdetach(ifp);
1417 }
1418
1419 /*
1420 * SIOC[GAD]LIFADDR.
1421 * SIOCGLIFADDR: get first address. (?)
1422 * SIOCGLIFADDR with IFLR_PREFIX:
1423 * get first address that matches the specified prefix.
1424 * SIOCALIFADDR: add the specified address.
1425 * SIOCALIFADDR with IFLR_PREFIX:
1426 * add the specified prefix, filling hostid part from
1427 * the first link-local address. prefixlen must be <= 64.
1428 * SIOCDLIFADDR: delete the specified address.
1429 * SIOCDLIFADDR with IFLR_PREFIX:
1430 * delete the first address that matches the specified prefix.
1431 * return values:
1432 * EINVAL on invalid parameters
1433 * EADDRNOTAVAIL on prefix match failed/specified address not found
1434 * other values may be returned from in6_ioctl()
1435 *
1436 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1437 * this is to accommodate address naming scheme other than RFC2374,
1438 * in the future.
1439 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1440 * address encoding scheme. (see figure on page 8)
1441 */
1442 static int
1443 in6_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
1444 struct ifnet *ifp)
1445 {
1446 struct in6_ifaddr *ia = NULL; /* XXX gcc 4.8 maybe-uninitialized */
1447 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1448 struct ifaddr *ifa;
1449 struct sockaddr *sa;
1450
1451 /* sanity checks */
1452 if (!data || !ifp) {
1453 panic("invalid argument to in6_lifaddr_ioctl");
1454 /* NOTREACHED */
1455 }
1456
1457 switch (cmd) {
1458 case SIOCGLIFADDR:
1459 /* address must be specified on GET with IFLR_PREFIX */
1460 if ((iflr->flags & IFLR_PREFIX) == 0)
1461 break;
1462 /* FALLTHROUGH */
1463 case SIOCALIFADDR:
1464 case SIOCDLIFADDR:
1465 /* address must be specified on ADD and DELETE */
1466 sa = (struct sockaddr *)&iflr->addr;
1467 if (sa->sa_family != AF_INET6)
1468 return EINVAL;
1469 if (sa->sa_len != sizeof(struct sockaddr_in6))
1470 return EINVAL;
1471 /* XXX need improvement */
1472 sa = (struct sockaddr *)&iflr->dstaddr;
1473 if (sa->sa_family && sa->sa_family != AF_INET6)
1474 return EINVAL;
1475 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1476 return EINVAL;
1477 break;
1478 default: /* shouldn't happen */
1479 #if 0
1480 panic("invalid cmd to in6_lifaddr_ioctl");
1481 /* NOTREACHED */
1482 #else
1483 return EOPNOTSUPP;
1484 #endif
1485 }
1486 if (sizeof(struct in6_addr) * NBBY < iflr->prefixlen)
1487 return EINVAL;
1488
1489 switch (cmd) {
1490 case SIOCALIFADDR:
1491 {
1492 struct in6_aliasreq ifra;
1493 struct in6_addr *xhostid = NULL;
1494 int prefixlen;
1495 int bound = curlwp_bind();
1496 struct psref psref;
1497
1498 if ((iflr->flags & IFLR_PREFIX) != 0) {
1499 struct sockaddr_in6 *sin6;
1500
1501 /*
1502 * xhostid is to fill in the hostid part of the
1503 * address. xhostid points to the first link-local
1504 * address attached to the interface.
1505 */
1506 ia = in6ifa_ifpforlinklocal_psref(ifp, 0, &psref);
1507 if (ia == NULL) {
1508 curlwp_bindx(bound);
1509 return EADDRNOTAVAIL;
1510 }
1511 xhostid = IFA_IN6(&ia->ia_ifa);
1512
1513 /* prefixlen must be <= 64. */
1514 if (64 < iflr->prefixlen) {
1515 ia6_release(ia, &psref);
1516 curlwp_bindx(bound);
1517 return EINVAL;
1518 }
1519 prefixlen = iflr->prefixlen;
1520
1521 /* hostid part must be zero. */
1522 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1523 if (sin6->sin6_addr.s6_addr32[2] != 0
1524 || sin6->sin6_addr.s6_addr32[3] != 0) {
1525 ia6_release(ia, &psref);
1526 curlwp_bindx(bound);
1527 return EINVAL;
1528 }
1529 } else
1530 prefixlen = iflr->prefixlen;
1531
1532 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1533 memset(&ifra, 0, sizeof(ifra));
1534 memcpy(ifra.ifra_name, iflr->iflr_name, sizeof(ifra.ifra_name));
1535
1536 memcpy(&ifra.ifra_addr, &iflr->addr,
1537 ((struct sockaddr *)&iflr->addr)->sa_len);
1538 if (xhostid) {
1539 /* fill in hostid part */
1540 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1541 xhostid->s6_addr32[2];
1542 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1543 xhostid->s6_addr32[3];
1544 }
1545
1546 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1547 memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
1548 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1549 if (xhostid) {
1550 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1551 xhostid->s6_addr32[2];
1552 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1553 xhostid->s6_addr32[3];
1554 }
1555 }
1556 if (xhostid) {
1557 ia6_release(ia, &psref);
1558 ia = NULL;
1559 }
1560 curlwp_bindx(bound);
1561
1562 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1563 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1564
1565 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
1566 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
1567 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1568 return in6_control(so, SIOCAIFADDR_IN6, &ifra, ifp);
1569 }
1570 case SIOCGLIFADDR:
1571 case SIOCDLIFADDR:
1572 {
1573 struct in6_addr mask, candidate, match;
1574 struct sockaddr_in6 *sin6;
1575 int cmp;
1576 int error, s;
1577
1578 memset(&mask, 0, sizeof(mask));
1579 if (iflr->flags & IFLR_PREFIX) {
1580 /* lookup a prefix rather than address. */
1581 in6_prefixlen2mask(&mask, iflr->prefixlen);
1582
1583 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1584 memcpy(&match, &sin6->sin6_addr, sizeof(match));
1585 match.s6_addr32[0] &= mask.s6_addr32[0];
1586 match.s6_addr32[1] &= mask.s6_addr32[1];
1587 match.s6_addr32[2] &= mask.s6_addr32[2];
1588 match.s6_addr32[3] &= mask.s6_addr32[3];
1589
1590 /* if you set extra bits, that's wrong */
1591 if (memcmp(&match, &sin6->sin6_addr, sizeof(match)))
1592 return EINVAL;
1593
1594 cmp = 1;
1595 } else {
1596 if (cmd == SIOCGLIFADDR) {
1597 /* on getting an address, take the 1st match */
1598 cmp = 0; /* XXX */
1599 } else {
1600 /* on deleting an address, do exact match */
1601 in6_prefixlen2mask(&mask, 128);
1602 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1603 memcpy(&match, &sin6->sin6_addr, sizeof(match));
1604
1605 cmp = 1;
1606 }
1607 }
1608
1609 s = pserialize_read_enter();
1610 IFADDR_READER_FOREACH(ifa, ifp) {
1611 if (ifa->ifa_addr->sa_family != AF_INET6)
1612 continue;
1613 if (!cmp)
1614 break;
1615
1616 /*
1617 * XXX: this is adhoc, but is necessary to allow
1618 * a user to specify fe80::/64 (not /10) for a
1619 * link-local address.
1620 */
1621 memcpy(&candidate, IFA_IN6(ifa), sizeof(candidate));
1622 in6_clearscope(&candidate);
1623 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1624 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1625 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1626 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1627 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1628 break;
1629 }
1630 if (!ifa) {
1631 error = EADDRNOTAVAIL;
1632 goto error;
1633 }
1634 ia = ifa2ia6(ifa);
1635
1636 if (cmd == SIOCGLIFADDR) {
1637 /* fill in the if_laddrreq structure */
1638 memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin6_len);
1639 error = sa6_recoverscope(
1640 (struct sockaddr_in6 *)&iflr->addr);
1641 if (error != 0)
1642 goto error;
1643
1644 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1645 memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
1646 ia->ia_dstaddr.sin6_len);
1647 error = sa6_recoverscope(
1648 (struct sockaddr_in6 *)&iflr->dstaddr);
1649 if (error != 0)
1650 goto error;
1651 } else
1652 memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
1653
1654 iflr->prefixlen =
1655 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1656
1657 iflr->flags = ia->ia6_flags; /* XXX */
1658
1659 error = 0;
1660 } else {
1661 struct in6_aliasreq ifra;
1662
1663 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1664 memset(&ifra, 0, sizeof(ifra));
1665 memcpy(ifra.ifra_name, iflr->iflr_name,
1666 sizeof(ifra.ifra_name));
1667
1668 memcpy(&ifra.ifra_addr, &ia->ia_addr,
1669 ia->ia_addr.sin6_len);
1670 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1671 memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
1672 ia->ia_dstaddr.sin6_len);
1673 } else {
1674 memset(&ifra.ifra_dstaddr, 0,
1675 sizeof(ifra.ifra_dstaddr));
1676 }
1677 memcpy(&ifra.ifra_dstaddr, &ia->ia_prefixmask,
1678 ia->ia_prefixmask.sin6_len);
1679
1680 ifra.ifra_flags = ia->ia6_flags;
1681 pserialize_read_exit(s);
1682
1683 return in6_control(so, SIOCDIFADDR_IN6, &ifra, ifp);
1684 }
1685 error:
1686 pserialize_read_exit(s);
1687 return error;
1688 }
1689 }
1690
1691 return EOPNOTSUPP; /* just for safety */
1692 }
1693
1694 /*
1695 * Initialize an interface's internet6 address
1696 * and routing table entry.
1697 */
1698 static int
1699 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1700 const struct sockaddr_in6 *sin6, int newhost)
1701 {
1702 int error = 0, ifacount = 0;
1703 int s = splnet();
1704 struct ifaddr *ifa;
1705
1706 /*
1707 * Give the interface a chance to initialize
1708 * if this is its first address,
1709 * and to validate the address if necessary.
1710 */
1711 IFADDR_READER_FOREACH(ifa, ifp) {
1712 if (ifa->ifa_addr->sa_family != AF_INET6)
1713 continue;
1714 ifacount++;
1715 }
1716
1717 ia->ia_addr = *sin6;
1718
1719 if (ifacount <= 0 &&
1720 (error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0) {
1721 splx(s);
1722 return error;
1723 }
1724 splx(s);
1725
1726 ia->ia_ifa.ifa_metric = ifp->if_metric;
1727
1728 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1729
1730 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1731 if (newhost) {
1732 /* set the rtrequest function to create llinfo */
1733 if (ifp->if_flags & IFF_POINTOPOINT)
1734 ia->ia_ifa.ifa_rtrequest = p2p_rtrequest;
1735 else if ((ifp->if_flags & IFF_LOOPBACK) == 0)
1736 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1737 in6_ifaddlocal(&ia->ia_ifa);
1738 } else {
1739 /* Inform the routing socket of new flags/timings */
1740 rt_newaddrmsg(RTM_NEWADDR, &ia->ia_ifa, 0, NULL);
1741 }
1742
1743 /* Add the network prefix route. */
1744 if (ia->ia_ifp->if_flags & IFF_LOOPBACK)
1745 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
1746 if ((error = in6_ifaddprefix(ia)) != 0) {
1747 if (newhost)
1748 in6_ifremlocal(&ia->ia_ifa);
1749 return error;
1750 }
1751
1752 if (ifp->if_flags & IFF_MULTICAST)
1753 in6_restoremkludge(ia, ifp);
1754
1755 return error;
1756 }
1757
1758 static struct ifaddr *
1759 bestifa(struct ifaddr *best_ifa, struct ifaddr *ifa)
1760 {
1761 if (best_ifa == NULL || best_ifa->ifa_preference < ifa->ifa_preference)
1762 return ifa;
1763 return best_ifa;
1764 }
1765
1766 /*
1767 * Find an IPv6 interface link-local address specific to an interface.
1768 */
1769 struct in6_ifaddr *
1770 in6ifa_ifpforlinklocal(const struct ifnet *ifp, const int ignoreflags)
1771 {
1772 struct ifaddr *best_ifa = NULL, *ifa;
1773
1774 IFADDR_READER_FOREACH(ifa, ifp) {
1775 if (ifa->ifa_addr->sa_family != AF_INET6)
1776 continue;
1777 if (!IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)))
1778 continue;
1779 if ((((struct in6_ifaddr *)ifa)->ia6_flags & ignoreflags) != 0)
1780 continue;
1781 best_ifa = bestifa(best_ifa, ifa);
1782 }
1783
1784 return (struct in6_ifaddr *)best_ifa;
1785 }
1786
1787 struct in6_ifaddr *
1788 in6ifa_ifpforlinklocal_psref(const struct ifnet *ifp, const int ignoreflags,
1789 struct psref *psref)
1790 {
1791 struct in6_ifaddr *ia;
1792 int s = pserialize_read_enter();
1793
1794 ia = in6ifa_ifpforlinklocal(ifp, ignoreflags);
1795 if (ia != NULL)
1796 ia6_acquire(ia, psref);
1797 pserialize_read_exit(s);
1798
1799 return ia;
1800 }
1801
1802 /*
1803 * find the internet address corresponding to a given address.
1804 * ifaddr is returned referenced.
1805 */
1806 struct in6_ifaddr *
1807 in6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid)
1808 {
1809 struct in6_ifaddr *ia;
1810 int s;
1811
1812 s = pserialize_read_enter();
1813 IN6_ADDRLIST_READER_FOREACH(ia) {
1814 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) {
1815 if (zoneid != 0 &&
1816 zoneid != ia->ia_addr.sin6_scope_id)
1817 continue;
1818 ifaref(&ia->ia_ifa);
1819 break;
1820 }
1821 }
1822 pserialize_read_exit(s);
1823
1824 return ia;
1825 }
1826
1827 /*
1828 * find the internet address corresponding to a given interface and address.
1829 */
1830 struct in6_ifaddr *
1831 in6ifa_ifpwithaddr(const struct ifnet *ifp, const struct in6_addr *addr)
1832 {
1833 struct ifaddr *best_ifa = NULL, *ifa;
1834
1835 IFADDR_READER_FOREACH(ifa, ifp) {
1836 if (ifa->ifa_addr->sa_family != AF_INET6)
1837 continue;
1838 if (!IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1839 continue;
1840 best_ifa = bestifa(best_ifa, ifa);
1841 }
1842
1843 return (struct in6_ifaddr *)best_ifa;
1844 }
1845
1846 struct in6_ifaddr *
1847 in6ifa_ifpwithaddr_psref(const struct ifnet *ifp, const struct in6_addr *addr,
1848 struct psref *psref)
1849 {
1850 struct in6_ifaddr *ia;
1851 int s = pserialize_read_enter();
1852
1853 ia = in6ifa_ifpwithaddr(ifp, addr);
1854 if (ia != NULL)
1855 ia6_acquire(ia, psref);
1856 pserialize_read_exit(s);
1857
1858 return ia;
1859 }
1860
1861 static struct in6_ifaddr *
1862 bestia(struct in6_ifaddr *best_ia, struct in6_ifaddr *ia)
1863 {
1864 if (best_ia == NULL ||
1865 best_ia->ia_ifa.ifa_preference < ia->ia_ifa.ifa_preference)
1866 return ia;
1867 return best_ia;
1868 }
1869
1870 /*
1871 * Convert IP6 address to printable (loggable) representation.
1872 */
1873 char *
1874 ip6_sprintf(const struct in6_addr *addr)
1875 {
1876 static int ip6round = 0;
1877 static char ip6buf[8][INET6_ADDRSTRLEN];
1878 char *cp = ip6buf[ip6round++ & 7];
1879
1880 in6_print(cp, INET6_ADDRSTRLEN, addr);
1881 return cp;
1882 }
1883
1884 /*
1885 * Determine if an address is on a local network.
1886 */
1887 int
1888 in6_localaddr(const struct in6_addr *in6)
1889 {
1890 struct in6_ifaddr *ia;
1891 int s;
1892
1893 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1894 return 1;
1895
1896 s = pserialize_read_enter();
1897 IN6_ADDRLIST_READER_FOREACH(ia) {
1898 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1899 &ia->ia_prefixmask.sin6_addr)) {
1900 pserialize_read_exit(s);
1901 return 1;
1902 }
1903 }
1904 pserialize_read_exit(s);
1905
1906 return 0;
1907 }
1908
1909 int
1910 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1911 {
1912 struct in6_ifaddr *ia;
1913 int s;
1914
1915 s = pserialize_read_enter();
1916 IN6_ADDRLIST_READER_FOREACH(ia) {
1917 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1918 &sa6->sin6_addr) &&
1919 #ifdef SCOPEDROUTING
1920 ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
1921 #endif
1922 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
1923 pserialize_read_exit(s);
1924 return 1; /* true */
1925 }
1926
1927 /* XXX: do we still have to go thru the rest of the list? */
1928 }
1929 pserialize_read_exit(s);
1930
1931 return 0; /* false */
1932 }
1933
1934 /*
1935 * return length of part which dst and src are equal
1936 * hard coding...
1937 */
1938 int
1939 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1940 {
1941 int match = 0;
1942 u_char *s = (u_char *)src, *d = (u_char *)dst;
1943 u_char *lim = s + 16, r;
1944
1945 while (s < lim)
1946 if ((r = (*d++ ^ *s++)) != 0) {
1947 while (r < 128) {
1948 match++;
1949 r <<= 1;
1950 }
1951 break;
1952 } else
1953 match += NBBY;
1954 return match;
1955 }
1956
1957 /* XXX: to be scope conscious */
1958 int
1959 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1960 {
1961 int bytelen, bitlen;
1962
1963 /* sanity check */
1964 if (len < 0 || len > 128) {
1965 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1966 len);
1967 return 0;
1968 }
1969
1970 bytelen = len / NBBY;
1971 bitlen = len % NBBY;
1972
1973 if (memcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1974 return 0;
1975 if (bitlen != 0 &&
1976 p1->s6_addr[bytelen] >> (NBBY - bitlen) !=
1977 p2->s6_addr[bytelen] >> (NBBY - bitlen))
1978 return 0;
1979
1980 return 1;
1981 }
1982
1983 void
1984 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1985 {
1986 static const u_char maskarray[NBBY] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1987 int bytelen, bitlen, i;
1988
1989 /* sanity check */
1990 if (len < 0 || len > 128) {
1991 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1992 len);
1993 return;
1994 }
1995
1996 memset(maskp, 0, sizeof(*maskp));
1997 bytelen = len / NBBY;
1998 bitlen = len % NBBY;
1999 for (i = 0; i < bytelen; i++)
2000 maskp->s6_addr[i] = 0xff;
2001 if (bitlen)
2002 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2003 }
2004
2005 /*
2006 * return the best address out of the same scope. if no address was
2007 * found, return the first valid address from designated IF.
2008 */
2009 struct in6_ifaddr *
2010 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2011 {
2012 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2013 struct ifaddr *ifa;
2014 struct in6_ifaddr *best_ia = NULL, *ia;
2015 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2016
2017 dep[0] = dep[1] = NULL;
2018
2019 /*
2020 * We first look for addresses in the same scope.
2021 * If there is one, return it.
2022 * If two or more, return one which matches the dst longest.
2023 * If none, return one of global addresses assigned other ifs.
2024 */
2025 IFADDR_READER_FOREACH(ifa, ifp) {
2026 if (ifa->ifa_addr->sa_family != AF_INET6)
2027 continue;
2028 ia = (struct in6_ifaddr *)ifa;
2029 if (ia->ia6_flags & IN6_IFF_ANYCAST)
2030 continue; /* XXX: is there any case to allow anycast? */
2031 if (ia->ia6_flags & IN6_IFF_NOTREADY)
2032 continue; /* don't use this interface */
2033 if (ia->ia6_flags & IN6_IFF_DETACHED)
2034 continue;
2035 if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
2036 if (ip6_use_deprecated)
2037 dep[0] = ia;
2038 continue;
2039 }
2040
2041 if (dst_scope != in6_addrscope(IFA_IN6(ifa)))
2042 continue;
2043 /*
2044 * call in6_matchlen() as few as possible
2045 */
2046 if (best_ia == NULL) {
2047 best_ia = ia;
2048 continue;
2049 }
2050 if (blen == -1)
2051 blen = in6_matchlen(&best_ia->ia_addr.sin6_addr, dst);
2052 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2053 if (tlen > blen) {
2054 blen = tlen;
2055 best_ia = ia;
2056 } else if (tlen == blen)
2057 best_ia = bestia(best_ia, ia);
2058 }
2059 if (best_ia != NULL)
2060 return best_ia;
2061
2062 IFADDR_READER_FOREACH(ifa, ifp) {
2063 if (ifa->ifa_addr->sa_family != AF_INET6)
2064 continue;
2065 ia = (struct in6_ifaddr *)ifa;
2066 if (ia->ia6_flags & IN6_IFF_ANYCAST)
2067 continue; /* XXX: is there any case to allow anycast? */
2068 if (ia->ia6_flags & IN6_IFF_NOTREADY)
2069 continue; /* don't use this interface */
2070 if (ia->ia6_flags & IN6_IFF_DETACHED)
2071 continue;
2072 if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
2073 if (ip6_use_deprecated)
2074 dep[1] = (struct in6_ifaddr *)ifa;
2075 continue;
2076 }
2077
2078 best_ia = bestia(best_ia, ia);
2079 }
2080 if (best_ia != NULL)
2081 return best_ia;
2082
2083 /* use the last-resort values, that are, deprecated addresses */
2084 if (dep[0])
2085 return dep[0];
2086 if (dep[1])
2087 return dep[1];
2088
2089 return NULL;
2090 }
2091
2092 /*
2093 * perform DAD when interface becomes IFF_UP.
2094 */
2095 void
2096 in6_if_link_up(struct ifnet *ifp)
2097 {
2098 struct ifaddr *ifa;
2099 struct in6_ifaddr *ia;
2100 int s, bound;
2101
2102 /* Ensure it's sane to run DAD */
2103 if (ifp->if_link_state == LINK_STATE_DOWN)
2104 return;
2105 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
2106 return;
2107
2108 bound = curlwp_bind();
2109 s = pserialize_read_enter();
2110 IFADDR_READER_FOREACH(ifa, ifp) {
2111 struct psref psref;
2112
2113 if (ifa->ifa_addr->sa_family != AF_INET6)
2114 continue;
2115
2116 ifa_acquire(ifa, &psref);
2117 pserialize_read_exit(s);
2118 ia = (struct in6_ifaddr *)ifa;
2119
2120 /* If detached then mark as tentative */
2121 if (ia->ia6_flags & IN6_IFF_DETACHED) {
2122 ia->ia6_flags &= ~IN6_IFF_DETACHED;
2123 if (if_do_dad(ifp)) {
2124 ia->ia6_flags |= IN6_IFF_TENTATIVE;
2125 nd6log(LOG_ERR, "%s marked tentative\n",
2126 ip6_sprintf(&ia->ia_addr.sin6_addr));
2127 } else if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0)
2128 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
2129 }
2130
2131 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2132 int rand_delay;
2133
2134 /* Clear the duplicated flag as we're starting DAD. */
2135 ia->ia6_flags &= ~IN6_IFF_DUPLICATED;
2136
2137 /*
2138 * The TENTATIVE flag was likely set by hand
2139 * beforehand, implicitly indicating the need for DAD.
2140 * We may be able to skip the random delay in this
2141 * case, but we impose delays just in case.
2142 */
2143 rand_delay = cprng_fast32() %
2144 (MAX_RTR_SOLICITATION_DELAY * hz);
2145 /* +1 ensures callout is always used */
2146 nd6_dad_start(ifa, rand_delay + 1);
2147 }
2148
2149 s = pserialize_read_enter();
2150 ifa_release(ifa, &psref);
2151 }
2152 pserialize_read_exit(s);
2153 curlwp_bindx(bound);
2154
2155 /* Restore any detached prefixes */
2156 pfxlist_onlink_check();
2157 }
2158
2159 void
2160 in6_if_up(struct ifnet *ifp)
2161 {
2162
2163 /*
2164 * special cases, like 6to4, are handled in in6_ifattach
2165 */
2166 in6_ifattach(ifp, NULL);
2167
2168 /* interface may not support link state, so bring it up also */
2169 in6_if_link_up(ifp);
2170 }
2171
2172 /*
2173 * Mark all addresses as detached.
2174 */
2175 void
2176 in6_if_link_down(struct ifnet *ifp)
2177 {
2178 struct ifaddr *ifa;
2179 struct in6_ifaddr *ia;
2180 int s, bound;
2181
2182 /* Any prefixes on this interface should be detached as well */
2183 pfxlist_onlink_check();
2184
2185 bound = curlwp_bind();
2186 s = pserialize_read_enter();
2187 IFADDR_READER_FOREACH(ifa, ifp) {
2188 struct psref psref;
2189
2190 if (ifa->ifa_addr->sa_family != AF_INET6)
2191 continue;
2192
2193 ifa_acquire(ifa, &psref);
2194 pserialize_read_exit(s);
2195 ia = (struct in6_ifaddr *)ifa;
2196
2197 /* Stop DAD processing */
2198 nd6_dad_stop(ifa);
2199
2200 /*
2201 * Mark the address as detached.
2202 * This satisfies RFC4862 Section 5.3, but we should apply
2203 * this logic to all addresses to be a good citizen and
2204 * avoid potential duplicated addresses.
2205 * When the interface comes up again, detached addresses
2206 * are marked tentative and DAD commences.
2207 */
2208 if (!(ia->ia6_flags & IN6_IFF_DETACHED)) {
2209 nd6log(LOG_DEBUG, "%s marked detached\n",
2210 ip6_sprintf(&ia->ia_addr.sin6_addr));
2211 ia->ia6_flags |= IN6_IFF_DETACHED;
2212 ia->ia6_flags &=
2213 ~(IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED);
2214 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
2215 }
2216
2217 s = pserialize_read_enter();
2218 ifa_release(ifa, &psref);
2219 }
2220 pserialize_read_exit(s);
2221 curlwp_bindx(bound);
2222 }
2223
2224 void
2225 in6_if_down(struct ifnet *ifp)
2226 {
2227
2228 in6_if_link_down(ifp);
2229 }
2230
2231 void
2232 in6_if_link_state_change(struct ifnet *ifp, int link_state)
2233 {
2234
2235 switch (link_state) {
2236 case LINK_STATE_DOWN:
2237 in6_if_link_down(ifp);
2238 break;
2239 case LINK_STATE_UP:
2240 in6_if_link_up(ifp);
2241 break;
2242 }
2243 }
2244
2245 /*
2246 * Calculate max IPv6 MTU through all the interfaces and store it
2247 * to in6_maxmtu.
2248 */
2249 void
2250 in6_setmaxmtu(void)
2251 {
2252 unsigned long maxmtu = 0;
2253 struct ifnet *ifp;
2254 int s;
2255
2256 s = pserialize_read_enter();
2257 IFNET_READER_FOREACH(ifp) {
2258 /* this function can be called during ifnet initialization */
2259 if (!ifp->if_afdata[AF_INET6])
2260 continue;
2261 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2262 IN6_LINKMTU(ifp) > maxmtu)
2263 maxmtu = IN6_LINKMTU(ifp);
2264 }
2265 pserialize_read_exit(s);
2266 if (maxmtu) /* update only when maxmtu is positive */
2267 in6_maxmtu = maxmtu;
2268 }
2269
2270 /*
2271 * Provide the length of interface identifiers to be used for the link attached
2272 * to the given interface. The length should be defined in "IPv6 over
2273 * xxx-link" document. Note that address architecture might also define
2274 * the length for a particular set of address prefixes, regardless of the
2275 * link type. As clarified in rfc2462bis, those two definitions should be
2276 * consistent, and those really are as of August 2004.
2277 */
2278 int
2279 in6_if2idlen(struct ifnet *ifp)
2280 {
2281 switch (ifp->if_type) {
2282 case IFT_ETHER: /* RFC2464 */
2283 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2284 case IFT_L2VLAN: /* ditto */
2285 case IFT_IEEE80211: /* ditto */
2286 case IFT_FDDI: /* RFC2467 */
2287 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2288 case IFT_PPP: /* RFC2472 */
2289 case IFT_ARCNET: /* RFC2497 */
2290 case IFT_FRELAY: /* RFC2590 */
2291 case IFT_IEEE1394: /* RFC3146 */
2292 case IFT_GIF: /* draft-ietf-v6ops-mech-v2-07 */
2293 case IFT_LOOP: /* XXX: is this really correct? */
2294 return 64;
2295 default:
2296 /*
2297 * Unknown link type:
2298 * It might be controversial to use the today's common constant
2299 * of 64 for these cases unconditionally. For full compliance,
2300 * we should return an error in this case. On the other hand,
2301 * if we simply miss the standard for the link type or a new
2302 * standard is defined for a new link type, the IFID length
2303 * is very likely to be the common constant. As a compromise,
2304 * we always use the constant, but make an explicit notice
2305 * indicating the "unknown" case.
2306 */
2307 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2308 return 64;
2309 }
2310 }
2311
2312 struct in6_llentry {
2313 struct llentry base;
2314 };
2315
2316 #define IN6_LLTBL_DEFAULT_HSIZE 32
2317 #define IN6_LLTBL_HASH(k, h) \
2318 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
2319
2320 /*
2321 * Do actual deallocation of @lle.
2322 * Called by LLE_FREE_LOCKED when number of references
2323 * drops to zero.
2324 */
2325 static void
2326 in6_lltable_destroy_lle(struct llentry *lle)
2327 {
2328
2329 LLE_WUNLOCK(lle);
2330 LLE_LOCK_DESTROY(lle);
2331 kmem_intr_free(lle, sizeof(struct in6_llentry));
2332 }
2333
2334 static struct llentry *
2335 in6_lltable_new(const struct in6_addr *addr6, u_int flags)
2336 {
2337 struct in6_llentry *lle;
2338
2339 lle = kmem_intr_zalloc(sizeof(struct in6_llentry), KM_NOSLEEP);
2340 if (lle == NULL) /* NB: caller generates msg */
2341 return NULL;
2342
2343 lle->base.r_l3addr.addr6 = *addr6;
2344 lle->base.lle_refcnt = 1;
2345 lle->base.lle_free = in6_lltable_destroy_lle;
2346 LLE_LOCK_INIT(&lle->base);
2347 callout_init(&lle->base.lle_timer, CALLOUT_MPSAFE);
2348
2349 return &lle->base;
2350 }
2351
2352 static int
2353 in6_lltable_match_prefix(const struct sockaddr *prefix,
2354 const struct sockaddr *mask, u_int flags, struct llentry *lle)
2355 {
2356 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2357 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2358
2359 if (IN6_ARE_MASKED_ADDR_EQUAL(&lle->r_l3addr.addr6,
2360 &pfx->sin6_addr, &msk->sin6_addr) &&
2361 ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)))
2362 return 1;
2363
2364 return 0;
2365 }
2366
2367 static void
2368 in6_lltable_free_entry(struct lltable *llt, struct llentry *lle)
2369 {
2370 struct ifnet *ifp __diagused;
2371
2372 LLE_WLOCK_ASSERT(lle);
2373 KASSERT(llt != NULL);
2374
2375 /* Unlink entry from table */
2376 if ((lle->la_flags & LLE_LINKED) != 0) {
2377
2378 ifp = llt->llt_ifp;
2379 IF_AFDATA_WLOCK_ASSERT(ifp);
2380 lltable_unlink_entry(llt, lle);
2381 }
2382
2383 KASSERT(mutex_owned(softnet_lock));
2384 callout_halt(&lle->lle_timer, softnet_lock);
2385 LLE_REMREF(lle);
2386
2387 llentry_free(lle);
2388 }
2389
2390 static int
2391 in6_lltable_rtcheck(struct ifnet *ifp,
2392 u_int flags,
2393 const struct sockaddr *l3addr)
2394 {
2395 struct rtentry *rt;
2396
2397 KASSERTMSG(l3addr->sa_family == AF_INET6,
2398 "sin_family %d", l3addr->sa_family);
2399
2400 rt = rtalloc1(l3addr, 0);
2401 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2402 int s;
2403 struct ifaddr *ifa;
2404 /*
2405 * Create an ND6 cache for an IPv6 neighbor
2406 * that is not covered by our own prefix.
2407 */
2408 /* XXX ifaof_ifpforaddr should take a const param */
2409 s = pserialize_read_enter();
2410 ifa = ifaof_ifpforaddr(l3addr, ifp);
2411 if (ifa != NULL) {
2412 pserialize_read_exit(s);
2413 if (rt != NULL)
2414 rtfree(rt);
2415 return 0;
2416 }
2417 pserialize_read_exit(s);
2418 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2419 ip6_sprintf(&((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2420 if (rt != NULL)
2421 rtfree(rt);
2422 return EINVAL;
2423 }
2424 rtfree(rt);
2425 return 0;
2426 }
2427
2428 static inline uint32_t
2429 in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize)
2430 {
2431
2432 return IN6_LLTBL_HASH(dst->s6_addr32[3], hsize);
2433 }
2434
2435 static uint32_t
2436 in6_lltable_hash(const struct llentry *lle, uint32_t hsize)
2437 {
2438
2439 return in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize);
2440 }
2441
2442 static void
2443 in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
2444 {
2445 struct sockaddr_in6 *sin6;
2446
2447 sin6 = (struct sockaddr_in6 *)sa;
2448 bzero(sin6, sizeof(*sin6));
2449 sin6->sin6_family = AF_INET6;
2450 sin6->sin6_len = sizeof(*sin6);
2451 sin6->sin6_addr = lle->r_l3addr.addr6;
2452 }
2453
2454 static inline struct llentry *
2455 in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst)
2456 {
2457 struct llentry *lle;
2458 struct llentries *lleh;
2459 u_int hashidx;
2460
2461 hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize);
2462 lleh = &llt->lle_head[hashidx];
2463 LIST_FOREACH(lle, lleh, lle_next) {
2464 if (lle->la_flags & LLE_DELETED)
2465 continue;
2466 if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst))
2467 break;
2468 }
2469
2470 return lle;
2471 }
2472
2473 static int
2474 in6_lltable_delete(struct lltable *llt, u_int flags,
2475 const struct sockaddr *l3addr)
2476 {
2477 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2478 struct llentry *lle;
2479
2480 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
2481 KASSERTMSG(l3addr->sa_family == AF_INET6,
2482 "sin_family %d", l3addr->sa_family);
2483
2484 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
2485
2486 if (lle == NULL)
2487 return ENOENT;
2488
2489 LLE_WLOCK(lle);
2490 lle->la_flags |= LLE_DELETED;
2491 #ifdef DIAGNOSTIC
2492 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2493 #endif
2494 if ((lle->la_flags & (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
2495 llentry_free(lle);
2496 else
2497 LLE_WUNLOCK(lle);
2498
2499 return 0;
2500 }
2501
2502 static struct llentry *
2503 in6_lltable_create(struct lltable *llt, u_int flags,
2504 const struct sockaddr *l3addr)
2505 {
2506 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2507 struct ifnet *ifp = llt->llt_ifp;
2508 struct llentry *lle;
2509
2510 IF_AFDATA_WLOCK_ASSERT(ifp);
2511 KASSERTMSG(l3addr->sa_family == AF_INET6,
2512 "sin_family %d", l3addr->sa_family);
2513
2514 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
2515
2516 if (lle != NULL) {
2517 LLE_WLOCK(lle);
2518 return lle;
2519 }
2520
2521 /*
2522 * A route that covers the given address must have
2523 * been installed 1st because we are doing a resolution,
2524 * verify this.
2525 */
2526 if (!(flags & LLE_IFADDR) &&
2527 in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2528 return NULL;
2529
2530 lle = in6_lltable_new(&sin6->sin6_addr, flags);
2531 if (lle == NULL) {
2532 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2533 return NULL;
2534 }
2535 lle->la_flags = flags;
2536 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
2537 memcpy(&lle->ll_addr, CLLADDR(ifp->if_sadl), ifp->if_addrlen);
2538 lle->la_flags |= LLE_VALID;
2539 }
2540
2541 lltable_link_entry(llt, lle);
2542 LLE_WLOCK(lle);
2543
2544 return lle;
2545 }
2546
2547 static struct llentry *
2548 in6_lltable_lookup(struct lltable *llt, u_int flags,
2549 const struct sockaddr *l3addr)
2550 {
2551 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2552 struct llentry *lle;
2553
2554 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
2555 KASSERTMSG(l3addr->sa_family == AF_INET6,
2556 "sin_family %d", l3addr->sa_family);
2557
2558 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
2559
2560 if (lle == NULL)
2561 return NULL;
2562
2563 if (flags & LLE_EXCLUSIVE)
2564 LLE_WLOCK(lle);
2565 else
2566 LLE_RLOCK(lle);
2567 return lle;
2568 }
2569
2570 static int
2571 in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
2572 struct rt_walkarg *w)
2573 {
2574 struct sockaddr_in6 sin6;
2575
2576 LLTABLE_LOCK_ASSERT();
2577
2578 /* skip deleted entries */
2579 if (lle->la_flags & LLE_DELETED)
2580 return 0;
2581
2582 sockaddr_in6_init(&sin6, &lle->r_l3addr.addr6, 0, 0, 0);
2583
2584 return lltable_dump_entry(llt, lle, w, sin6tosa(&sin6));
2585 }
2586
2587 static struct lltable *
2588 in6_lltattach(struct ifnet *ifp)
2589 {
2590 struct lltable *llt;
2591
2592 llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE);
2593 llt->llt_af = AF_INET6;
2594 llt->llt_ifp = ifp;
2595
2596 llt->llt_lookup = in6_lltable_lookup;
2597 llt->llt_create = in6_lltable_create;
2598 llt->llt_delete = in6_lltable_delete;
2599 llt->llt_dump_entry = in6_lltable_dump_entry;
2600 llt->llt_hash = in6_lltable_hash;
2601 llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry;
2602 llt->llt_free_entry = in6_lltable_free_entry;
2603 llt->llt_match_prefix = in6_lltable_match_prefix;
2604 lltable_link(llt);
2605
2606 return llt;
2607 }
2608
2609 void *
2610 in6_domifattach(struct ifnet *ifp)
2611 {
2612 struct in6_ifextra *ext;
2613
2614 ext = malloc(sizeof(*ext), M_IFADDR, M_WAITOK|M_ZERO);
2615
2616 ext->in6_ifstat = malloc(sizeof(struct in6_ifstat),
2617 M_IFADDR, M_WAITOK|M_ZERO);
2618
2619 ext->icmp6_ifstat = malloc(sizeof(struct icmp6_ifstat),
2620 M_IFADDR, M_WAITOK|M_ZERO);
2621
2622 ext->nd_ifinfo = nd6_ifattach(ifp);
2623 ext->scope6_id = scope6_ifattach(ifp);
2624 ext->nprefixes = 0;
2625 ext->ndefrouters = 0;
2626
2627 ext->lltable = in6_lltattach(ifp);
2628
2629 return ext;
2630 }
2631
2632 void
2633 in6_domifdetach(struct ifnet *ifp, void *aux)
2634 {
2635 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2636
2637 lltable_free(ext->lltable);
2638 ext->lltable = NULL;
2639 nd6_ifdetach(ifp, ext);
2640 free(ext->in6_ifstat, M_IFADDR);
2641 free(ext->icmp6_ifstat, M_IFADDR);
2642 scope6_ifdetach(ext->scope6_id);
2643 free(ext, M_IFADDR);
2644 }
2645
2646 /*
2647 * Convert IPv4 address stored in struct in_addr to IPv4-Mapped IPv6 address
2648 * stored in struct in6_addr as defined in RFC 4921 section 2.5.5.2.
2649 */
2650 void
2651 in6_in_2_v4mapin6(const struct in_addr *in, struct in6_addr *in6)
2652 {
2653 in6->s6_addr32[0] = 0;
2654 in6->s6_addr32[1] = 0;
2655 in6->s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2656 in6->s6_addr32[3] = in->s_addr;
2657 }
2658
2659 /*
2660 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2661 * v4 mapped addr or v4 compat addr
2662 */
2663 void
2664 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2665 {
2666 memset(sin, 0, sizeof(*sin));
2667 sin->sin_len = sizeof(struct sockaddr_in);
2668 sin->sin_family = AF_INET;
2669 sin->sin_port = sin6->sin6_port;
2670 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2671 }
2672
2673 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2674 void
2675 in6_sin_2_v4mapsin6(const struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2676 {
2677 memset(sin6, 0, sizeof(*sin6));
2678 sin6->sin6_len = sizeof(struct sockaddr_in6);
2679 sin6->sin6_family = AF_INET6;
2680 sin6->sin6_port = sin->sin_port;
2681 in6_in_2_v4mapin6(&sin->sin_addr, &sin6->sin6_addr);
2682 }
2683
2684 /* Convert sockaddr_in6 into sockaddr_in. */
2685 void
2686 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2687 {
2688 struct sockaddr_in *sin_p;
2689 struct sockaddr_in6 sin6;
2690
2691 /*
2692 * Save original sockaddr_in6 addr and convert it
2693 * to sockaddr_in.
2694 */
2695 sin6 = *(struct sockaddr_in6 *)nam;
2696 sin_p = (struct sockaddr_in *)nam;
2697 in6_sin6_2_sin(sin_p, &sin6);
2698 }
2699
2700 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2701 void
2702 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2703 {
2704 struct sockaddr_in *sin_p;
2705 struct sockaddr_in6 *sin6_p;
2706
2707 sin6_p = malloc(sizeof(*sin6_p), M_SONAME, M_WAITOK);
2708 sin_p = (struct sockaddr_in *)*nam;
2709 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2710 free(*nam, M_SONAME);
2711 *nam = sin6tosa(sin6_p);
2712 }
2713