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