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