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