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