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