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