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