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