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