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