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