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