Deleted Added
sdiff udiff text old ( 151465 ) new ( 151468 )
full compact
1/* $FreeBSD: head/sys/netinet6/in6.c 151468 2005-10-19 15:05:42Z suz $ */
2/* $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi 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 * 4. 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 "opt_inet.h"
65#include "opt_inet6.h"
66
67#include <sys/param.h>
68#include <sys/errno.h>
69#include <sys/malloc.h>
70#include <sys/socket.h>
71#include <sys/socketvar.h>
72#include <sys/sockio.h>
73#include <sys/systm.h>
74#include <sys/proc.h>
75#include <sys/time.h>
76#include <sys/kernel.h>
77#include <sys/syslog.h>
78
79#include <net/if.h>
80#include <net/if_types.h>
81#include <net/route.h>
82#include <net/if_dl.h>
83
84#include <netinet/in.h>
85#include <netinet/in_var.h>
86#include <netinet/if_ether.h>
87#include <netinet/in_systm.h>
88#include <netinet/ip.h>
89#include <netinet/in_pcb.h>
90
91#include <netinet/ip6.h>
92#include <netinet6/ip6_var.h>
93#include <netinet6/nd6.h>
94#include <netinet6/mld6_var.h>
95#include <netinet6/ip6_mroute.h>
96#include <netinet6/in6_ifattach.h>
97#include <netinet6/scope6_var.h>
98#include <netinet6/in6_pcb.h>
99
100#include <net/net_osdep.h>
101
102MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "internet multicast address");
103
104/*
105 * Definitions of some costant IP6 addresses.
106 */
107const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
108const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
109const struct in6_addr in6addr_nodelocal_allnodes =
110 IN6ADDR_NODELOCAL_ALLNODES_INIT;
111const struct in6_addr in6addr_linklocal_allnodes =
112 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
113const struct in6_addr in6addr_linklocal_allrouters =
114 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
115
116const struct in6_addr in6mask0 = IN6MASK0;
117const struct in6_addr in6mask32 = IN6MASK32;
118const struct in6_addr in6mask64 = IN6MASK64;
119const struct in6_addr in6mask96 = IN6MASK96;
120const struct in6_addr in6mask128 = IN6MASK128;
121
122const struct sockaddr_in6 sa6_any =
123 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
124
125static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
126 struct ifnet *, struct thread *));
127static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
128 struct sockaddr_in6 *, int));
129static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
130
131struct in6_multihead in6_multihead; /* XXX BSS initialization */
132int (*faithprefix_p)(struct in6_addr *);
133
134/*
135 * Subroutine for in6_ifaddloop() and in6_ifremloop().
136 * This routine does actual work.
137 */
138static void
139in6_ifloop_request(int cmd, struct ifaddr *ifa)
140{
141 struct sockaddr_in6 all1_sa;
142 struct rtentry *nrt = NULL;
143 int e;
144
145 bzero(&all1_sa, sizeof(all1_sa));
146 all1_sa.sin6_family = AF_INET6;
147 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
148 all1_sa.sin6_addr = in6mask128;
149
150 /*
151 * We specify the address itself as the gateway, and set the
152 * RTF_LLINFO flag, so that the corresponding host route would have
153 * the flag, and thus applications that assume traditional behavior
154 * would be happy. Note that we assume the caller of the function
155 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
156 * which changes the outgoing interface to the loopback interface.
157 */
158 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
159 (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
160 if (e != 0) {
161 /* XXX need more descriptive message */
162 log(LOG_ERR, "in6_ifloop_request: "
163 "%s operation failed for %s (errno=%d)\n",
164 cmd == RTM_ADD ? "ADD" : "DELETE",
165 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
166 e);
167 }
168
169 /*
170 * Report the addition/removal of the address to the routing socket.
171 * XXX: since we called rtinit for a p2p interface with a destination,
172 * we end up reporting twice in such a case. Should we rather
173 * omit the second report?
174 */
175 if (nrt) {
176 RT_LOCK(nrt);
177 /*
178 * Make sure rt_ifa be equal to IFA, the second argument of
179 * the function. We need this because when we refer to
180 * rt_ifa->ia6_flags in ip6_input, we assume that the rt_ifa
181 * points to the address instead of the loopback address.
182 */
183 if (cmd == RTM_ADD && ifa != nrt->rt_ifa) {
184 IFAFREE(nrt->rt_ifa);
185 IFAREF(ifa);
186 nrt->rt_ifa = ifa;
187 }
188
189 rt_newaddrmsg(cmd, ifa, e, nrt);
190 if (cmd == RTM_DELETE) {
191 rtfree(nrt);
192 } else {
193 /* the cmd must be RTM_ADD here */
194 RT_REMREF(nrt);
195 RT_UNLOCK(nrt);
196 }
197 }
198}
199
200/*
201 * Add ownaddr as loopback rtentry. We previously add the route only if
202 * necessary (ex. on a p2p link). However, since we now manage addresses
203 * separately from prefixes, we should always add the route. We can't
204 * rely on the cloning mechanism from the corresponding interface route
205 * any more.
206 */
207void
208in6_ifaddloop(struct ifaddr *ifa)
209{
210 struct rtentry *rt;
211 int need_loop;
212
213 /* If there is no loopback entry, allocate one. */
214 rt = rtalloc1(ifa->ifa_addr, 0, 0);
215 need_loop = (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
216 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0);
217 if (rt)
218 rtfree(rt);
219 if (need_loop)
220 in6_ifloop_request(RTM_ADD, ifa);
221}
222
223/*
224 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
225 * if it exists.
226 */
227void
228in6_ifremloop(struct ifaddr *ifa)
229{
230 struct in6_ifaddr *ia;
231 struct rtentry *rt;
232 int ia_count = 0;
233
234 /*
235 * Some of BSD variants do not remove cloned routes
236 * from an interface direct route, when removing the direct route
237 * (see comments in net/net_osdep.h). Even for variants that do remove
238 * cloned routes, they could fail to remove the cloned routes when
239 * we handle multple addresses that share a common prefix.
240 * So, we should remove the route corresponding to the deleted address.
241 */
242
243 /*
244 * Delete the entry only if exact one ifa exists. More than one ifa
245 * can exist if we assign a same single address to multiple
246 * (probably p2p) interfaces.
247 * XXX: we should avoid such a configuration in IPv6...
248 */
249 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
250 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
251 ia_count++;
252 if (ia_count > 1)
253 break;
254 }
255 }
256
257 if (ia_count == 1) {
258 /*
259 * Before deleting, check if a corresponding loopbacked host
260 * route surely exists. With this check, we can avoid to
261 * delete an interface direct route whose destination is same
262 * as the address being removed. This can happen when removing
263 * a subnet-router anycast address on an interface attahced
264 * to a shared medium.
265 */
266 rt = rtalloc1(ifa->ifa_addr, 0, 0);
267 if (rt != NULL) {
268 if ((rt->rt_flags & RTF_HOST) != 0 &&
269 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
270 rtfree(rt);
271 in6_ifloop_request(RTM_DELETE, ifa);
272 } else
273 RT_UNLOCK(rt);
274 }
275 }
276}
277
278int
279in6_mask2len(mask, lim0)
280 struct in6_addr *mask;
281 u_char *lim0;
282{
283 int x = 0, y;
284 u_char *lim = lim0, *p;
285
286 /* ignore the scope_id part */
287 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
288 lim = (u_char *)mask + sizeof(*mask);
289 for (p = (u_char *)mask; p < lim; x++, p++) {
290 if (*p != 0xff)
291 break;
292 }
293 y = 0;
294 if (p < lim) {
295 for (y = 0; y < 8; y++) {
296 if ((*p & (0x80 >> y)) == 0)
297 break;
298 }
299 }
300
301 /*
302 * when the limit pointer is given, do a stricter check on the
303 * remaining bits.
304 */
305 if (p < lim) {
306 if (y != 0 && (*p & (0x00ff >> y)) != 0)
307 return (-1);
308 for (p = p + 1; p < lim; p++)
309 if (*p != 0)
310 return (-1);
311 }
312
313 return x * 8 + y;
314}
315
316#define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
317#define ia62ifa(ia6) (&((ia6)->ia_ifa))
318
319int
320in6_control(so, cmd, data, ifp, td)
321 struct socket *so;
322 u_long cmd;
323 caddr_t data;
324 struct ifnet *ifp;
325 struct thread *td;
326{
327 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
328 struct in6_ifaddr *ia = NULL;
329 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
330 int error, privileged;
331
332 privileged = 0;
333 if (td == NULL || !suser(td))
334 privileged++;
335
336 switch (cmd) {
337 case SIOCGETSGCNT_IN6:
338 case SIOCGETMIFCNT_IN6:
339 return (mrt6_ioctl(cmd, data));
340 }
341
342 switch(cmd) {
343 case SIOCAADDRCTL_POLICY:
344 case SIOCDADDRCTL_POLICY:
345 if (!privileged)
346 return (EPERM);
347 return (in6_src_ioctl(cmd, data));
348 }
349
350 if (ifp == NULL)
351 return (EOPNOTSUPP);
352
353 switch (cmd) {
354 case SIOCSNDFLUSH_IN6:
355 case SIOCSPFXFLUSH_IN6:
356 case SIOCSRTRFLUSH_IN6:
357 case SIOCSDEFIFACE_IN6:
358 case SIOCSIFINFO_FLAGS:
359 if (!privileged)
360 return (EPERM);
361 /* FALLTHROUGH */
362 case OSIOCGIFINFO_IN6:
363 case SIOCGIFINFO_IN6:
364 case SIOCSIFINFO_IN6:
365 case SIOCGDRLST_IN6:
366 case SIOCGPRLST_IN6:
367 case SIOCGNBRINFO_IN6:
368 case SIOCGDEFIFACE_IN6:
369 return (nd6_ioctl(cmd, data, ifp));
370 }
371
372 switch (cmd) {
373 case SIOCSIFPREFIX_IN6:
374 case SIOCDIFPREFIX_IN6:
375 case SIOCAIFPREFIX_IN6:
376 case SIOCCIFPREFIX_IN6:
377 case SIOCSGIFPREFIX_IN6:
378 case SIOCGIFPREFIX_IN6:
379 log(LOG_NOTICE,
380 "prefix ioctls are now invalidated. "
381 "please use ifconfig.\n");
382 return (EOPNOTSUPP);
383 }
384
385 switch (cmd) {
386 case SIOCSSCOPE6:
387 if (!privileged)
388 return (EPERM);
389 return (scope6_set(ifp,
390 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
391 case SIOCGSCOPE6:
392 return (scope6_get(ifp,
393 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
394 case SIOCGSCOPE6DEF:
395 return (scope6_get_default((struct scope6_id *)
396 ifr->ifr_ifru.ifru_scope_id));
397 }
398
399 switch (cmd) {
400 case SIOCALIFADDR:
401 case SIOCDLIFADDR:
402 if (!privileged)
403 return (EPERM);
404 /* FALLTHROUGH */
405 case SIOCGLIFADDR:
406 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
407 }
408
409 /*
410 * Find address for this interface, if it exists.
411 */
412 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
413 int error = 0;
414
415 if (ifra->ifra_addr.sin6_scope_id != 0)
416 error = sa6_embedscope(&ifra->ifra_addr, 0);
417 else
418 error = in6_setscope(&ifra->ifra_addr.sin6_addr,
419 ifp, NULL);
420 if (error != 0)
421 return (error);
422 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
423 }
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 /* we decided to obsolete this command (20000704) */
434 return (EINVAL);
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 return (EADDRNOTAVAIL);
446 /* FALLTHROUGH */
447 case SIOCAIFADDR_IN6:
448 /*
449 * We always require users to specify a valid IPv6 address for
450 * the corresponding operation.
451 */
452 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
453 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
454 return (EAFNOSUPPORT);
455 if (!privileged)
456 return (EPERM);
457
458 break;
459
460 case SIOCGIFADDR_IN6:
461 /* This interface is basically deprecated. use SIOCGIFCONF. */
462 /* FALLTHROUGH */
463 case SIOCGIFAFLAG_IN6:
464 case SIOCGIFNETMASK_IN6:
465 case SIOCGIFDSTADDR_IN6:
466 case SIOCGIFALIFETIME_IN6:
467 /* must think again about its semantics */
468 if (ia == NULL)
469 return (EADDRNOTAVAIL);
470 break;
471 case SIOCSIFALIFETIME_IN6:
472 {
473 struct in6_addrlifetime *lt;
474
475 if (!privileged)
476 return (EPERM);
477 if (ia == NULL)
478 return (EADDRNOTAVAIL);
479 /* sanity for overflow - beware unsigned */
480 lt = &ifr->ifr_ifru.ifru_lifetime;
481 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
482 lt->ia6t_vltime + time_second < time_second) {
483 return EINVAL;
484 }
485 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
486 lt->ia6t_pltime + time_second < time_second) {
487 return EINVAL;
488 }
489 break;
490 }
491 }
492
493 switch (cmd) {
494
495 case SIOCGIFADDR_IN6:
496 ifr->ifr_addr = ia->ia_addr;
497 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
498 return (error);
499 break;
500
501 case SIOCGIFDSTADDR_IN6:
502 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
503 return (EINVAL);
504 /*
505 * XXX: should we check if ifa_dstaddr is NULL and return
506 * an error?
507 */
508 ifr->ifr_dstaddr = ia->ia_dstaddr;
509 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
510 return (error);
511 break;
512
513 case SIOCGIFNETMASK_IN6:
514 ifr->ifr_addr = ia->ia_prefixmask;
515 break;
516
517 case SIOCGIFAFLAG_IN6:
518 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
519 break;
520
521 case SIOCGIFSTAT_IN6:
522 if (ifp == NULL)
523 return EINVAL;
524 bzero(&ifr->ifr_ifru.ifru_stat,
525 sizeof(ifr->ifr_ifru.ifru_stat));
526 ifr->ifr_ifru.ifru_stat =
527 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
528 break;
529
530 case SIOCGIFSTAT_ICMP6:
531 if (ifp == NULL)
532 return EINVAL;
533 bzero(&ifr->ifr_ifru.ifru_stat,
534 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
535 ifr->ifr_ifru.ifru_icmp6stat =
536 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
537 break;
538
539 case SIOCGIFALIFETIME_IN6:
540 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
541 break;
542
543 case SIOCSIFALIFETIME_IN6:
544 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
545 /* for sanity */
546 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
547 ia->ia6_lifetime.ia6t_expire =
548 time_second + ia->ia6_lifetime.ia6t_vltime;
549 } else
550 ia->ia6_lifetime.ia6t_expire = 0;
551 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
552 ia->ia6_lifetime.ia6t_preferred =
553 time_second + ia->ia6_lifetime.ia6t_pltime;
554 } else
555 ia->ia6_lifetime.ia6t_preferred = 0;
556 break;
557
558 case SIOCAIFADDR_IN6:
559 {
560 int i, error = 0;
561 struct nd_prefix pr0, *pr;
562
563 /*
564 * first, make or update the interface address structure,
565 * and link it to the list.
566 */
567 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
568 return (error);
569
570 /*
571 * then, make the prefix on-link on the interface.
572 * XXX: we'd rather create the prefix before the address, but
573 * we need at least one address to install the corresponding
574 * interface route, so we configure the address first.
575 */
576
577 /*
578 * convert mask to prefix length (prefixmask has already
579 * been validated in in6_update_ifa().
580 */
581 bzero(&pr0, sizeof(pr0));
582 pr0.ndpr_ifp = ifp;
583 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
584 NULL);
585 if (pr0.ndpr_plen == 128) {
586 break; /* we don't need to install a host route. */
587 }
588 pr0.ndpr_prefix = ifra->ifra_addr;
589 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
590 /* apply the mask for safety. */
591 for (i = 0; i < 4; i++) {
592 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
593 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
594 }
595 /*
596 * XXX: since we don't have an API to set prefix (not address)
597 * lifetimes, we just use the same lifetimes as addresses.
598 * The (temporarily) installed lifetimes can be overridden by
599 * later advertised RAs (when accept_rtadv is non 0), which is
600 * an intended behavior.
601 */
602 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
603 pr0.ndpr_raf_auto =
604 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
605 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
606 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
607
608 /* add the prefix if not yet. */
609 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
610 /*
611 * nd6_prelist_add will install the corresponding
612 * interface route.
613 */
614 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
615 return (error);
616 if (pr == NULL) {
617 log(LOG_ERR, "nd6_prelist_add succeeded but "
618 "no prefix\n");
619 return (EINVAL); /* XXX panic here? */
620 }
621 }
622 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
623 == NULL) {
624 /* XXX: this should not happen! */
625 log(LOG_ERR, "in6_control: addition succeeded, but"
626 " no ifaddr\n");
627 } else {
628 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
629 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
630 ia->ia6_ndpr = pr;
631 pr->ndpr_refcnt++;
632
633 /*
634 * If this is the first autoconf address from
635 * the prefix, create a temporary address
636 * as well (when specified).
637 */
638 if (ip6_use_tempaddr &&
639 pr->ndpr_refcnt == 1) {
640 int e;
641 if ((e = in6_tmpifadd(ia, 1)) != 0) {
642 log(LOG_NOTICE, "in6_control: "
643 "failed to create a "
644 "temporary address, "
645 "errno=%d\n", e);
646 }
647 }
648 }
649
650 /*
651 * this might affect the status of autoconfigured
652 * addresses, that is, this address might make
653 * other addresses detached.
654 */
655 pfxlist_onlink_check();
656 }
657 if (error == 0 && ia)
658 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
659 break;
660 }
661
662 case SIOCDIFADDR_IN6:
663 {
664 int i = 0;
665 struct nd_prefix pr0, *pr;
666
667 /*
668 * If the address being deleted is the only one that owns
669 * the corresponding prefix, expire the prefix as well.
670 * XXX: theoretically, we don't have to worry about such
671 * relationship, since we separate the address management
672 * and the prefix management. We do this, however, to provide
673 * as much backward compatibility as possible in terms of
674 * the ioctl operation.
675 */
676 bzero(&pr0, sizeof(pr0));
677 pr0.ndpr_ifp = ifp;
678 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
679 NULL);
680 if (pr0.ndpr_plen == 128)
681 goto purgeaddr;
682 pr0.ndpr_prefix = ia->ia_addr;
683 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
684 for (i = 0; i < 4; i++) {
685 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
686 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
687 }
688 /*
689 * The logic of the following condition is a bit complicated.
690 * We expire the prefix when
691 * 1. the address obeys autoconfiguration and it is the
692 * only owner of the associated prefix, or
693 * 2. the address does not obey autoconf and there is no
694 * other owner of the prefix.
695 */
696 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
697 (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
698 pr->ndpr_refcnt == 1) ||
699 ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
700 pr->ndpr_refcnt == 0))) {
701 pr->ndpr_expire = 1; /* XXX: just for expiration */
702 }
703
704 purgeaddr:
705 in6_purgeaddr(&ia->ia_ifa);
706 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
707 break;
708 }
709
710 default:
711 if (ifp == NULL || ifp->if_ioctl == 0)
712 return (EOPNOTSUPP);
713 return ((*ifp->if_ioctl)(ifp, cmd, data));
714 }
715
716 return (0);
717}
718
719/*
720 * Update parameters of an IPv6 interface address.
721 * If necessary, a new entry is created and linked into address chains.
722 * This function is separated from in6_control().
723 * XXX: should this be performed under splnet()?
724 */
725int
726in6_update_ifa(ifp, ifra, ia)
727 struct ifnet *ifp;
728 struct in6_aliasreq *ifra;
729 struct in6_ifaddr *ia;
730{
731 int error = 0, hostIsNew = 0, plen = -1;
732 struct in6_ifaddr *oia;
733 struct sockaddr_in6 dst6;
734 struct in6_addrlifetime *lt;
735 struct rtentry *rt;
736
737 /* Validate parameters */
738 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
739 return (EINVAL);
740
741 /*
742 * The destination address for a p2p link must have a family
743 * of AF_UNSPEC or AF_INET6.
744 */
745 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
746 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
747 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
748 return (EAFNOSUPPORT);
749 /*
750 * validate ifra_prefixmask. don't check sin6_family, netmask
751 * does not carry fields other than sin6_len.
752 */
753 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
754 return (EINVAL);
755 /*
756 * Because the IPv6 address architecture is classless, we require
757 * users to specify a (non 0) prefix length (mask) for a new address.
758 * We also require the prefix (when specified) mask is valid, and thus
759 * reject a non-consecutive mask.
760 */
761 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
762 return (EINVAL);
763 if (ifra->ifra_prefixmask.sin6_len != 0) {
764 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
765 (u_char *)&ifra->ifra_prefixmask +
766 ifra->ifra_prefixmask.sin6_len);
767 if (plen <= 0)
768 return (EINVAL);
769 } else {
770 /*
771 * In this case, ia must not be NULL. We just use its prefix
772 * length.
773 */
774 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
775 }
776 /*
777 * If the destination address on a p2p interface is specified,
778 * and the address is a scoped one, validate/set the scope
779 * zone identifier.
780 */
781 dst6 = ifra->ifra_dstaddr;
782 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
783 (dst6.sin6_family == AF_INET6)) {
784 struct in6_addr in6_tmp;
785 u_int32_t zoneid;
786
787 in6_tmp = dst6.sin6_addr;
788 if (in6_setscope(&in6_tmp, ifp, &zoneid))
789 return (EINVAL); /* XXX: should be impossible */
790
791 if (dst6.sin6_scope_id != 0) {
792 if (dst6.sin6_scope_id != zoneid)
793 return (EINVAL);
794 } else /* user omit to specify the ID. */
795 dst6.sin6_scope_id = zoneid;
796
797 /* convert into the internal form */
798 if (sa6_embedscope(&dst6, 0))
799 return (EINVAL); /* XXX: should be impossible */
800 }
801 /*
802 * The destination address can be specified only for a p2p or a
803 * loopback interface. If specified, the corresponding prefix length
804 * must be 128.
805 */
806 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
807 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
808 /* XXX: noisy message */
809 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
810 "be specified for a p2p or a loopback IF only\n"));
811 return (EINVAL);
812 }
813 if (plen != 128) {
814 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
815 "be 128 when dstaddr is specified\n"));
816 return (EINVAL);
817 }
818 }
819 /* lifetime consistency check */
820 lt = &ifra->ifra_lifetime;
821 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
822 && lt->ia6t_vltime + time_second < time_second) {
823 return EINVAL;
824 }
825 if (lt->ia6t_vltime == 0) {
826 /*
827 * the following log might be noisy, but this is a typical
828 * configuration mistake or a tool's bug.
829 */
830 nd6log((LOG_INFO,
831 "in6_update_ifa: valid lifetime is 0 for %s\n",
832 ip6_sprintf(&ifra->ifra_addr.sin6_addr)));
833 }
834 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
835 && lt->ia6t_pltime + time_second < time_second) {
836 return EINVAL;
837 }
838
839 /*
840 * If this is a new address, allocate a new ifaddr and link it
841 * into chains.
842 */
843 if (ia == NULL) {
844 hostIsNew = 1;
845 /*
846 * When in6_update_ifa() is called in a process of a received
847 * RA, it is called under an interrupt context. So, we should
848 * call malloc with M_NOWAIT.
849 */
850 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
851 M_NOWAIT);
852 if (ia == NULL)
853 return (ENOBUFS);
854 bzero((caddr_t)ia, sizeof(*ia));
855 /* Initialize the address and masks */
856 IFA_LOCK_INIT(&ia->ia_ifa);
857 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
858 ia->ia_addr.sin6_family = AF_INET6;
859 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
860 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
861 /*
862 * XXX: some functions expect that ifa_dstaddr is not
863 * NULL for p2p interfaces.
864 */
865 ia->ia_ifa.ifa_dstaddr =
866 (struct sockaddr *)&ia->ia_dstaddr;
867 } else {
868 ia->ia_ifa.ifa_dstaddr = NULL;
869 }
870 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
871
872 ia->ia_ifp = ifp;
873 if ((oia = in6_ifaddr) != NULL) {
874 for ( ; oia->ia_next; oia = oia->ia_next)
875 continue;
876 oia->ia_next = ia;
877 } else
878 in6_ifaddr = ia;
879
880 ia->ia_ifa.ifa_refcnt = 1;
881 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
882 }
883
884 /* set prefix mask */
885 if (ifra->ifra_prefixmask.sin6_len) {
886 /*
887 * We prohibit changing the prefix length of an existing
888 * address, because
889 * + such an operation should be rare in IPv6, and
890 * + the operation would confuse prefix management.
891 */
892 if (ia->ia_prefixmask.sin6_len &&
893 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
894 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
895 " existing (%s) address should not be changed\n",
896 ip6_sprintf(&ia->ia_addr.sin6_addr)));
897 error = EINVAL;
898 goto unlink;
899 }
900 ia->ia_prefixmask = ifra->ifra_prefixmask;
901 }
902
903 /*
904 * If a new destination address is specified, scrub the old one and
905 * install the new destination. Note that the interface must be
906 * p2p or loopback (see the check above.)
907 */
908 if (dst6.sin6_family == AF_INET6 &&
909 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
910 int e;
911
912 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
913 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
914 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
915 "a route to the old destination: %s\n",
916 ip6_sprintf(&ia->ia_addr.sin6_addr)));
917 /* proceed anyway... */
918 } else
919 ia->ia_flags &= ~IFA_ROUTE;
920 ia->ia_dstaddr = dst6;
921 }
922
923 /*
924 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
925 * to see if the address is deprecated or invalidated, but initialize
926 * these members for applications.
927 */
928 ia->ia6_lifetime = ifra->ifra_lifetime;
929 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
930 ia->ia6_lifetime.ia6t_expire =
931 time_second + ia->ia6_lifetime.ia6t_vltime;
932 } else
933 ia->ia6_lifetime.ia6t_expire = 0;
934 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
935 ia->ia6_lifetime.ia6t_preferred =
936 time_second + ia->ia6_lifetime.ia6t_pltime;
937 } else
938 ia->ia6_lifetime.ia6t_preferred = 0;
939
940 /* reset the interface and routing table appropriately. */
941 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
942 goto unlink;
943
944 /*
945 * configure address flags.
946 */
947 ia->ia6_flags = ifra->ifra_flags;
948 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
949 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
950 /*
951 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
952 * userland, make it deprecated.
953 */
954 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
955 ia->ia6_lifetime.ia6t_pltime = 0;
956 ia->ia6_lifetime.ia6t_preferred = time_second;
957 }
958
959 /*
960 * Perform DAD, if needed.
961 * XXX It may be of use, if we can administratively
962 * disable DAD.
963 */
964 if (in6if_do_dad(ifp) && hostIsNew &&
965 (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
966 ia->ia6_flags |= IN6_IFF_TENTATIVE;
967 nd6_dad_start((struct ifaddr *)ia, NULL);
968 }
969
970 /*
971 * We are done if we have simply modified an existing address.
972 */
973 if (!hostIsNew)
974 return (error);
975
976 /*
977 * Beyond this point, we should call in6_purgeaddr upon an error,
978 * not just go to unlink.
979 */
980
981 /* Join necessary multicast groups */
982 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
983 struct sockaddr_in6 mltaddr, mltmask;
984 struct in6_multi *in6m;
985 struct in6_addr llsol;
986
987 /* join solicited multicast addr for new host id */
988 bzero(&llsol, sizeof(struct in6_addr));
989 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
990 llsol.s6_addr32[1] = 0;
991 llsol.s6_addr32[2] = htonl(1);
992 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
993 llsol.s6_addr8[12] = 0xff;
994 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
995 /* XXX: should not happen */
996 log(LOG_ERR, "in6_update_ifa: "
997 "in6_setscope failed\n");
998 goto cleanup;
999 }
1000 (void)in6_addmulti(&llsol, ifp, &error);
1001 if (error != 0) {
1002 nd6log((LOG_WARNING,
1003 "in6_update_ifa: addmulti failed for "
1004 "%s on %s (errno=%d)\n",
1005 ip6_sprintf(&llsol), if_name(ifp),
1006 error));
1007 goto cleanup;
1008 }
1009
1010 bzero(&mltmask, sizeof(mltmask));
1011 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1012 mltmask.sin6_family = AF_INET6;
1013 mltmask.sin6_addr = in6mask32;
1014#define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1015
1016 /*
1017 * join link-local all-nodes address
1018 */
1019 bzero(&mltaddr, sizeof(mltaddr));
1020 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1021 mltaddr.sin6_family = AF_INET6;
1022 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1023 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1024 0)
1025 goto cleanup; /* XXX: should not fail */
1026
1027 /*
1028 * XXX: do we really need this automatic routes?
1029 * We should probably reconsider this stuff. Most applications
1030 * actually do not need the routes, since they usually specify
1031 * the outgoing interface.
1032 */
1033 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1034 if (rt) {
1035 if (memcmp(&mltaddr.sin6_addr,
1036 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1037 MLTMASK_LEN)) {
1038 RTFREE_LOCKED(rt);
1039 rt = NULL;
1040 }
1041 }
1042 if (!rt) {
1043 /* XXX: we need RTF_CLONING to fake nd6_rtrequest */
1044 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1045 (struct sockaddr *)&ia->ia_addr,
1046 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1047 (struct rtentry **)0);
1048 if (error)
1049 goto cleanup;
1050 } else
1051 RTFREE_LOCKED(rt);
1052
1053 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1054 if (in6m == NULL) {
1055 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1056 if (error != 0) {
1057 nd6log((LOG_WARNING,
1058 "in6_update_ifa: addmulti failed for "
1059 "%s on %s (errno=%d)\n",
1060 ip6_sprintf(&mltaddr.sin6_addr),
1061 if_name(ifp), error));
1062 goto cleanup;
1063 }
1064 }
1065
1066 /*
1067 * join node information group address
1068 */
1069#define hostnamelen strlen(hostname)
1070 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1071 == 0) {
1072 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1073 if (in6m == NULL) {
1074 (void)in6_addmulti(&mltaddr.sin6_addr,
1075 ifp, &error);
1076 if (error != 0) {
1077 nd6log((LOG_WARNING, "in6_update_ifa: "
1078 "addmulti failed for "
1079 "%s on %s (errno=%d)\n",
1080 ip6_sprintf(&mltaddr.sin6_addr),
1081 if_name(ifp), error));
1082 goto cleanup;
1083 }
1084 }
1085 }
1086#undef hostnamelen
1087
1088 /*
1089 * join interface-local all-nodes address.
1090 * (ff01::1%ifN, and ff01::%ifN/32)
1091 */
1092 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1093 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
1094 != 0)
1095 goto cleanup; /* XXX: should not fail */
1096 /* XXX: again, do we really need the route? */
1097 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1098 if (rt) {
1099 if (memcmp(&mltaddr.sin6_addr,
1100 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1101 MLTMASK_LEN)) {
1102 RTFREE_LOCKED(rt);
1103 rt = NULL;
1104 }
1105 }
1106 if (!rt) {
1107 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1108 (struct sockaddr *)&ia->ia_addr,
1109 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1110 (struct rtentry **)0);
1111 if (error)
1112 goto cleanup;
1113 } else
1114 RTFREE_LOCKED(rt);
1115
1116 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1117 if (in6m == NULL) {
1118 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1119 if (error != 0) {
1120 nd6log((LOG_WARNING, "in6_update_ifa: "
1121 "addmulti failed for %s on %s "
1122 "(errno=%d)\n",
1123 ip6_sprintf(&mltaddr.sin6_addr),
1124 if_name(ifp), error));
1125 goto cleanup;
1126 }
1127 }
1128#undef MLTMASK_LEN
1129 }
1130
1131 return (error);
1132
1133 unlink:
1134 /*
1135 * XXX: if a change of an existing address failed, keep the entry
1136 * anyway.
1137 */
1138 if (hostIsNew)
1139 in6_unlink_ifa(ia, ifp);
1140 return (error);
1141
1142 cleanup:
1143 in6_purgeaddr(&ia->ia_ifa);
1144 return error;
1145}
1146
1147void
1148in6_purgeaddr(ifa)
1149 struct ifaddr *ifa;
1150{
1151 struct ifnet *ifp = ifa->ifa_ifp;
1152 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1153
1154 /* stop DAD processing */
1155 nd6_dad_stop(ifa);
1156
1157 /*
1158 * delete route to the destination of the address being purged.
1159 * The interface must be p2p or loopback in this case.
1160 */
1161 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1162 int e;
1163
1164 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1165 != 0) {
1166 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1167 "a route to the p2p destination: %s on %s, "
1168 "errno=%d\n",
1169 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1170 e);
1171 /* proceed anyway... */
1172 } else
1173 ia->ia_flags &= ~IFA_ROUTE;
1174 }
1175
1176 /* Remove ownaddr's loopback rtentry, if it exists. */
1177 in6_ifremloop(&(ia->ia_ifa));
1178
1179 if (ifp->if_flags & IFF_MULTICAST) {
1180 /*
1181 * delete solicited multicast addr for deleting host id
1182 */
1183 struct in6_multi *in6m;
1184 struct in6_addr llsol;
1185 bzero(&llsol, sizeof(struct in6_addr));
1186 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1187 llsol.s6_addr32[1] = 0;
1188 llsol.s6_addr32[2] = htonl(1);
1189 llsol.s6_addr32[3] =
1190 ia->ia_addr.sin6_addr.s6_addr32[3];
1191 llsol.s6_addr8[12] = 0xff;
1192 (void)in6_setscope(&llsol, ifp, NULL); /* XXX proceed anyway */
1193
1194 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1195 if (in6m)
1196 in6_delmulti(in6m);
1197 }
1198
1199 in6_unlink_ifa(ia, ifp);
1200}
1201
1202static void
1203in6_unlink_ifa(ia, ifp)
1204 struct in6_ifaddr *ia;
1205 struct ifnet *ifp;
1206{
1207 struct in6_ifaddr *oia;
1208 int s = splnet();
1209
1210 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1211
1212 oia = ia;
1213 if (oia == (ia = in6_ifaddr))
1214 in6_ifaddr = ia->ia_next;
1215 else {
1216 while (ia->ia_next && (ia->ia_next != oia))
1217 ia = ia->ia_next;
1218 if (ia->ia_next)
1219 ia->ia_next = oia->ia_next;
1220 else {
1221 /* search failed */
1222 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1223 }
1224 }
1225
1226 /*
1227 * When an autoconfigured address is being removed, release the
1228 * reference to the base prefix. Also, since the release might
1229 * affect the status of other (detached) addresses, call
1230 * pfxlist_onlink_check().
1231 */
1232 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1233 if (oia->ia6_ndpr == NULL) {
1234 nd6log((LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1235 "%p has no prefix\n", oia));
1236 } else {
1237 oia->ia6_ndpr->ndpr_refcnt--;
1238 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1239 oia->ia6_ndpr = NULL;
1240 }
1241
1242 pfxlist_onlink_check();
1243 }
1244
1245 /*
1246 * release another refcnt for the link from in6_ifaddr.
1247 * Note that we should decrement the refcnt at least once for all *BSD.
1248 */
1249 IFAFREE(&oia->ia_ifa);
1250
1251 splx(s);
1252}
1253
1254void
1255in6_purgeif(ifp)
1256 struct ifnet *ifp;
1257{
1258 struct ifaddr *ifa, *nifa;
1259
1260 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
1261 nifa = TAILQ_NEXT(ifa, ifa_list);
1262 if (ifa->ifa_addr->sa_family != AF_INET6)
1263 continue;
1264 in6_purgeaddr(ifa);
1265 }
1266
1267 in6_ifdetach(ifp);
1268}
1269
1270/*
1271 * SIOC[GAD]LIFADDR.
1272 * SIOCGLIFADDR: get first address. (?)
1273 * SIOCGLIFADDR with IFLR_PREFIX:
1274 * get first address that matches the specified prefix.
1275 * SIOCALIFADDR: add the specified address.
1276 * SIOCALIFADDR with IFLR_PREFIX:
1277 * add the specified prefix, filling hostid part from
1278 * the first link-local address. prefixlen must be <= 64.
1279 * SIOCDLIFADDR: delete the specified address.
1280 * SIOCDLIFADDR with IFLR_PREFIX:
1281 * delete the first address that matches the specified prefix.
1282 * return values:
1283 * EINVAL on invalid parameters
1284 * EADDRNOTAVAIL on prefix match failed/specified address not found
1285 * other values may be returned from in6_ioctl()
1286 *
1287 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1288 * this is to accomodate address naming scheme other than RFC2374,
1289 * in the future.
1290 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1291 * address encoding scheme. (see figure on page 8)
1292 */
1293static int
1294in6_lifaddr_ioctl(so, cmd, data, ifp, td)
1295 struct socket *so;
1296 u_long cmd;
1297 caddr_t data;
1298 struct ifnet *ifp;
1299 struct thread *td;
1300{
1301 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1302 struct ifaddr *ifa;
1303 struct sockaddr *sa;
1304
1305 /* sanity checks */
1306 if (!data || !ifp) {
1307 panic("invalid argument to in6_lifaddr_ioctl");
1308 /* NOTREACHED */
1309 }
1310
1311 switch (cmd) {
1312 case SIOCGLIFADDR:
1313 /* address must be specified on GET with IFLR_PREFIX */
1314 if ((iflr->flags & IFLR_PREFIX) == 0)
1315 break;
1316 /* FALLTHROUGH */
1317 case SIOCALIFADDR:
1318 case SIOCDLIFADDR:
1319 /* address must be specified on ADD and DELETE */
1320 sa = (struct sockaddr *)&iflr->addr;
1321 if (sa->sa_family != AF_INET6)
1322 return EINVAL;
1323 if (sa->sa_len != sizeof(struct sockaddr_in6))
1324 return EINVAL;
1325 /* XXX need improvement */
1326 sa = (struct sockaddr *)&iflr->dstaddr;
1327 if (sa->sa_family && sa->sa_family != AF_INET6)
1328 return EINVAL;
1329 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1330 return EINVAL;
1331 break;
1332 default: /* shouldn't happen */
1333#if 0
1334 panic("invalid cmd to in6_lifaddr_ioctl");
1335 /* NOTREACHED */
1336#else
1337 return EOPNOTSUPP;
1338#endif
1339 }
1340 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1341 return EINVAL;
1342
1343 switch (cmd) {
1344 case SIOCALIFADDR:
1345 {
1346 struct in6_aliasreq ifra;
1347 struct in6_addr *hostid = NULL;
1348 int prefixlen;
1349
1350 if ((iflr->flags & IFLR_PREFIX) != 0) {
1351 struct sockaddr_in6 *sin6;
1352
1353 /*
1354 * hostid is to fill in the hostid part of the
1355 * address. hostid points to the first link-local
1356 * address attached to the interface.
1357 */
1358 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1359 if (!ifa)
1360 return EADDRNOTAVAIL;
1361 hostid = IFA_IN6(ifa);
1362
1363 /* prefixlen must be <= 64. */
1364 if (64 < iflr->prefixlen)
1365 return EINVAL;
1366 prefixlen = iflr->prefixlen;
1367
1368 /* hostid part must be zero. */
1369 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1370 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1371 sin6->sin6_addr.s6_addr32[3] != 0) {
1372 return EINVAL;
1373 }
1374 } else
1375 prefixlen = iflr->prefixlen;
1376
1377 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1378 bzero(&ifra, sizeof(ifra));
1379 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1380
1381 bcopy(&iflr->addr, &ifra.ifra_addr,
1382 ((struct sockaddr *)&iflr->addr)->sa_len);
1383 if (hostid) {
1384 /* fill in hostid part */
1385 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1386 hostid->s6_addr32[2];
1387 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1388 hostid->s6_addr32[3];
1389 }
1390
1391 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1392 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1393 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1394 if (hostid) {
1395 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1396 hostid->s6_addr32[2];
1397 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1398 hostid->s6_addr32[3];
1399 }
1400 }
1401
1402 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1403 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1404
1405 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1406 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1407 }
1408 case SIOCGLIFADDR:
1409 case SIOCDLIFADDR:
1410 {
1411 struct in6_ifaddr *ia;
1412 struct in6_addr mask, candidate, match;
1413 struct sockaddr_in6 *sin6;
1414 int cmp;
1415
1416 bzero(&mask, sizeof(mask));
1417 if (iflr->flags & IFLR_PREFIX) {
1418 /* lookup a prefix rather than address. */
1419 in6_prefixlen2mask(&mask, iflr->prefixlen);
1420
1421 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1422 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1423 match.s6_addr32[0] &= mask.s6_addr32[0];
1424 match.s6_addr32[1] &= mask.s6_addr32[1];
1425 match.s6_addr32[2] &= mask.s6_addr32[2];
1426 match.s6_addr32[3] &= mask.s6_addr32[3];
1427
1428 /* if you set extra bits, that's wrong */
1429 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1430 return EINVAL;
1431
1432 cmp = 1;
1433 } else {
1434 if (cmd == SIOCGLIFADDR) {
1435 /* on getting an address, take the 1st match */
1436 cmp = 0; /* XXX */
1437 } else {
1438 /* on deleting an address, do exact match */
1439 in6_prefixlen2mask(&mask, 128);
1440 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1441 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1442
1443 cmp = 1;
1444 }
1445 }
1446
1447 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1448 if (ifa->ifa_addr->sa_family != AF_INET6)
1449 continue;
1450 if (!cmp)
1451 break;
1452
1453 /*
1454 * XXX: this is adhoc, but is necessary to allow
1455 * a user to specify fe80::/64 (not /10) for a
1456 * link-local address.
1457 */
1458 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1459 in6_clearscope(&candidate);
1460 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1461 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1462 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1463 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1464 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1465 break;
1466 }
1467 if (!ifa)
1468 return EADDRNOTAVAIL;
1469 ia = ifa2ia6(ifa);
1470
1471 if (cmd == SIOCGLIFADDR) {
1472 int error;
1473
1474 /* fill in the if_laddrreq structure */
1475 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1476 error = sa6_recoverscope(
1477 (struct sockaddr_in6 *)&iflr->addr);
1478 if (error != 0)
1479 return (error);
1480
1481 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1482 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1483 ia->ia_dstaddr.sin6_len);
1484 error = sa6_recoverscope(
1485 (struct sockaddr_in6 *)&iflr->dstaddr);
1486 if (error != 0)
1487 return (error);
1488 } else
1489 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1490
1491 iflr->prefixlen =
1492 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1493
1494 iflr->flags = ia->ia6_flags; /* XXX */
1495
1496 return 0;
1497 } else {
1498 struct in6_aliasreq ifra;
1499
1500 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1501 bzero(&ifra, sizeof(ifra));
1502 bcopy(iflr->iflr_name, ifra.ifra_name,
1503 sizeof(ifra.ifra_name));
1504
1505 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1506 ia->ia_addr.sin6_len);
1507 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1508 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1509 ia->ia_dstaddr.sin6_len);
1510 } else {
1511 bzero(&ifra.ifra_dstaddr,
1512 sizeof(ifra.ifra_dstaddr));
1513 }
1514 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1515 ia->ia_prefixmask.sin6_len);
1516
1517 ifra.ifra_flags = ia->ia6_flags;
1518 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1519 ifp, td);
1520 }
1521 }
1522 }
1523
1524 return EOPNOTSUPP; /* just for safety */
1525}
1526
1527/*
1528 * Initialize an interface's intetnet6 address
1529 * and routing table entry.
1530 */
1531static int
1532in6_ifinit(ifp, ia, sin6, newhost)
1533 struct ifnet *ifp;
1534 struct in6_ifaddr *ia;
1535 struct sockaddr_in6 *sin6;
1536 int newhost;
1537{
1538 int error = 0, plen, ifacount = 0;
1539 int s = splimp();
1540 struct ifaddr *ifa;
1541
1542 /*
1543 * Give the interface a chance to initialize
1544 * if this is its first address,
1545 * and to validate the address if necessary.
1546 */
1547 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1548 if (ifa->ifa_addr == NULL)
1549 continue; /* just for safety */
1550 if (ifa->ifa_addr->sa_family != AF_INET6)
1551 continue;
1552 ifacount++;
1553 }
1554
1555 ia->ia_addr = *sin6;
1556
1557 if (ifacount <= 1 && ifp->if_ioctl) {
1558 IFF_LOCKGIANT(ifp);
1559 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1560 IFF_UNLOCKGIANT(ifp);
1561 if (error) {
1562 splx(s);
1563 return (error);
1564 }
1565 }
1566 splx(s);
1567
1568 ia->ia_ifa.ifa_metric = ifp->if_metric;
1569
1570 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1571
1572 /*
1573 * Special case:
1574 * If a new destination address is specified for a point-to-point
1575 * interface, install a route to the destination as an interface
1576 * direct route.
1577 * XXX: the logic below rejects assigning multiple addresses on a p2p
1578 * interface that share a same destination.
1579 */
1580 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1581 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1582 ia->ia_dstaddr.sin6_family == AF_INET6) {
1583 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1584 RTF_UP | RTF_HOST)) != 0)
1585 return (error);
1586 ia->ia_flags |= IFA_ROUTE;
1587 }
1588 if (plen < 128) {
1589 /*
1590 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1591 */
1592 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1593 }
1594
1595 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1596 if (newhost) {
1597 /* set the rtrequest function to create llinfo */
1598 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1599 in6_ifaddloop(&(ia->ia_ifa));
1600 }
1601
1602 return (error);
1603}
1604
1605struct in6_multi_mship *
1606in6_joingroup(ifp, addr, errorp)
1607 struct ifnet *ifp;
1608 struct in6_addr *addr;
1609 int *errorp;
1610{
1611 struct in6_multi_mship *imm;
1612
1613 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
1614 if (!imm) {
1615 *errorp = ENOBUFS;
1616 return NULL;
1617 }
1618 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1619 if (!imm->i6mm_maddr) {
1620 /* *errorp is alrady set */
1621 free(imm, M_IP6MADDR);
1622 return NULL;
1623 }
1624 return imm;
1625}
1626
1627int
1628in6_leavegroup(imm)
1629 struct in6_multi_mship *imm;
1630{
1631
1632 if (imm->i6mm_maddr)
1633 in6_delmulti(imm->i6mm_maddr);
1634 free(imm, M_IP6MADDR);
1635 return 0;
1636}
1637
1638/*
1639 * Find an IPv6 interface link-local address specific to an interface.
1640 */
1641struct in6_ifaddr *
1642in6ifa_ifpforlinklocal(ifp, ignoreflags)
1643 struct ifnet *ifp;
1644 int ignoreflags;
1645{
1646 struct ifaddr *ifa;
1647
1648 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1649 if (ifa->ifa_addr == NULL)
1650 continue; /* just for safety */
1651 if (ifa->ifa_addr->sa_family != AF_INET6)
1652 continue;
1653 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1654 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1655 ignoreflags) != 0)
1656 continue;
1657 break;
1658 }
1659 }
1660
1661 return ((struct in6_ifaddr *)ifa);
1662}
1663
1664
1665/*
1666 * find the internet address corresponding to a given interface and address.
1667 */
1668struct in6_ifaddr *
1669in6ifa_ifpwithaddr(ifp, addr)
1670 struct ifnet *ifp;
1671 struct in6_addr *addr;
1672{
1673 struct ifaddr *ifa;
1674
1675 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1676 if (ifa->ifa_addr == NULL)
1677 continue; /* just for safety */
1678 if (ifa->ifa_addr->sa_family != AF_INET6)
1679 continue;
1680 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1681 break;
1682 }
1683
1684 return ((struct in6_ifaddr *)ifa);
1685}
1686
1687/*
1688 * Convert IP6 address to printable (loggable) representation.
1689 */
1690static char digits[] = "0123456789abcdef";
1691static int ip6round = 0;
1692char *
1693ip6_sprintf(addr)
1694 const struct in6_addr *addr;
1695{
1696 static char ip6buf[8][48];
1697 int i;
1698 char *cp;
1699 const u_int16_t *a = (const u_int16_t *)addr;
1700 const u_int8_t *d;
1701 int dcolon = 0;
1702
1703 ip6round = (ip6round + 1) & 7;
1704 cp = ip6buf[ip6round];
1705
1706 for (i = 0; i < 8; i++) {
1707 if (dcolon == 1) {
1708 if (*a == 0) {
1709 if (i == 7)
1710 *cp++ = ':';
1711 a++;
1712 continue;
1713 } else
1714 dcolon = 2;
1715 }
1716 if (*a == 0) {
1717 if (dcolon == 0 && *(a + 1) == 0) {
1718 if (i == 0)
1719 *cp++ = ':';
1720 *cp++ = ':';
1721 dcolon = 1;
1722 } else {
1723 *cp++ = '0';
1724 *cp++ = ':';
1725 }
1726 a++;
1727 continue;
1728 }
1729 d = (const u_char *)a;
1730 *cp++ = digits[*d >> 4];
1731 *cp++ = digits[*d++ & 0xf];
1732 *cp++ = digits[*d >> 4];
1733 *cp++ = digits[*d & 0xf];
1734 *cp++ = ':';
1735 a++;
1736 }
1737 *--cp = 0;
1738 return (ip6buf[ip6round]);
1739}
1740
1741int
1742in6_localaddr(in6)
1743 struct in6_addr *in6;
1744{
1745 struct in6_ifaddr *ia;
1746
1747 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1748 return 1;
1749
1750 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1751 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1752 &ia->ia_prefixmask.sin6_addr)) {
1753 return 1;
1754 }
1755 }
1756
1757 return (0);
1758}
1759
1760int
1761in6_is_addr_deprecated(sa6)
1762 struct sockaddr_in6 *sa6;
1763{
1764 struct in6_ifaddr *ia;
1765
1766 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1767 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1768 &sa6->sin6_addr) &&
1769 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1770 return (1); /* true */
1771
1772 /* XXX: do we still have to go thru the rest of the list? */
1773 }
1774
1775 return (0); /* false */
1776}
1777
1778/*
1779 * return length of part which dst and src are equal
1780 * hard coding...
1781 */
1782int
1783in6_matchlen(src, dst)
1784struct in6_addr *src, *dst;
1785{
1786 int match = 0;
1787 u_char *s = (u_char *)src, *d = (u_char *)dst;
1788 u_char *lim = s + 16, r;
1789
1790 while (s < lim)
1791 if ((r = (*d++ ^ *s++)) != 0) {
1792 while (r < 128) {
1793 match++;
1794 r <<= 1;
1795 }
1796 break;
1797 } else
1798 match += 8;
1799 return match;
1800}
1801
1802/* XXX: to be scope conscious */
1803int
1804in6_are_prefix_equal(p1, p2, len)
1805 struct in6_addr *p1, *p2;
1806 int len;
1807{
1808 int bytelen, bitlen;
1809
1810 /* sanity check */
1811 if (0 > len || len > 128) {
1812 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1813 len);
1814 return (0);
1815 }
1816
1817 bytelen = len / 8;
1818 bitlen = len % 8;
1819
1820 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1821 return (0);
1822 if (bitlen != 0 &&
1823 p1->s6_addr[bytelen] >> (8 - bitlen) !=
1824 p2->s6_addr[bytelen] >> (8 - bitlen))
1825 return (0);
1826
1827 return (1);
1828}
1829
1830void
1831in6_prefixlen2mask(maskp, len)
1832 struct in6_addr *maskp;
1833 int len;
1834{
1835 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1836 int bytelen, bitlen, i;
1837
1838 /* sanity check */
1839 if (0 > len || len > 128) {
1840 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1841 len);
1842 return;
1843 }
1844
1845 bzero(maskp, sizeof(*maskp));
1846 bytelen = len / 8;
1847 bitlen = len % 8;
1848 for (i = 0; i < bytelen; i++)
1849 maskp->s6_addr[i] = 0xff;
1850 if (bitlen)
1851 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1852}
1853
1854/*
1855 * return the best address out of the same scope. if no address was
1856 * found, return the first valid address from designated IF.
1857 */
1858struct in6_ifaddr *
1859in6_ifawithifp(ifp, dst)
1860 struct ifnet *ifp;
1861 struct in6_addr *dst;
1862{
1863 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1864 struct ifaddr *ifa;
1865 struct in6_ifaddr *besta = 0;
1866 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
1867
1868 dep[0] = dep[1] = NULL;
1869
1870 /*
1871 * We first look for addresses in the same scope.
1872 * If there is one, return it.
1873 * If two or more, return one which matches the dst longest.
1874 * If none, return one of global addresses assigned other ifs.
1875 */
1876 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1877 if (ifa->ifa_addr->sa_family != AF_INET6)
1878 continue;
1879 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1880 continue; /* XXX: is there any case to allow anycast? */
1881 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1882 continue; /* don't use this interface */
1883 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1884 continue;
1885 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1886 if (ip6_use_deprecated)
1887 dep[0] = (struct in6_ifaddr *)ifa;
1888 continue;
1889 }
1890
1891 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1892 /*
1893 * call in6_matchlen() as few as possible
1894 */
1895 if (besta) {
1896 if (blen == -1)
1897 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1898 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1899 if (tlen > blen) {
1900 blen = tlen;
1901 besta = (struct in6_ifaddr *)ifa;
1902 }
1903 } else
1904 besta = (struct in6_ifaddr *)ifa;
1905 }
1906 }
1907 if (besta)
1908 return (besta);
1909
1910 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1911 if (ifa->ifa_addr->sa_family != AF_INET6)
1912 continue;
1913 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1914 continue; /* XXX: is there any case to allow anycast? */
1915 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1916 continue; /* don't use this interface */
1917 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1918 continue;
1919 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1920 if (ip6_use_deprecated)
1921 dep[1] = (struct in6_ifaddr *)ifa;
1922 continue;
1923 }
1924
1925 return (struct in6_ifaddr *)ifa;
1926 }
1927
1928 /* use the last-resort values, that are, deprecated addresses */
1929 if (dep[0])
1930 return dep[0];
1931 if (dep[1])
1932 return dep[1];
1933
1934 return NULL;
1935}
1936
1937/*
1938 * perform DAD when interface becomes IFF_UP.
1939 */
1940void
1941in6_if_up(ifp)
1942 struct ifnet *ifp;
1943{
1944 struct ifaddr *ifa;
1945 struct in6_ifaddr *ia;
1946 int dad_delay; /* delay ticks before DAD output */
1947
1948 /*
1949 * special cases, like 6to4, are handled in in6_ifattach
1950 */
1951 in6_ifattach(ifp, NULL);
1952
1953 dad_delay = 0;
1954 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1955 if (ifa->ifa_addr->sa_family != AF_INET6)
1956 continue;
1957 ia = (struct in6_ifaddr *)ifa;
1958 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
1959 nd6_dad_start(ifa, &dad_delay);
1960 }
1961}
1962
1963int
1964in6if_do_dad(ifp)
1965 struct ifnet *ifp;
1966{
1967 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
1968 return (0);
1969
1970 switch (ifp->if_type) {
1971#ifdef IFT_DUMMY
1972 case IFT_DUMMY:
1973#endif
1974 case IFT_FAITH:
1975 /*
1976 * These interfaces do not have the IFF_LOOPBACK flag,
1977 * but loop packets back. We do not have to do DAD on such
1978 * interfaces. We should even omit it, because loop-backed
1979 * NS would confuse the DAD procedure.
1980 */
1981 return (0);
1982 default:
1983 /*
1984 * Our DAD routine requires the interface up and running.
1985 * However, some interfaces can be up before the RUNNING
1986 * status. Additionaly, users may try to assign addresses
1987 * before the interface becomes up (or running).
1988 * We simply skip DAD in such a case as a work around.
1989 * XXX: we should rather mark "tentative" on such addresses,
1990 * and do DAD after the interface becomes ready.
1991 */
1992 if (!((ifp->if_flags & IFF_UP) &&
1993 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
1994 return (0);
1995
1996 return (1);
1997 }
1998}
1999
2000/*
2001 * Calculate max IPv6 MTU through all the interfaces and store it
2002 * to in6_maxmtu.
2003 */
2004void
2005in6_setmaxmtu()
2006{
2007 unsigned long maxmtu = 0;
2008 struct ifnet *ifp;
2009
2010 IFNET_RLOCK();
2011 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
2012 /* this function can be called during ifnet initialization */
2013 if (!ifp->if_afdata[AF_INET6])
2014 continue;
2015 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2016 IN6_LINKMTU(ifp) > maxmtu)
2017 maxmtu = IN6_LINKMTU(ifp);
2018 }
2019 IFNET_RUNLOCK();
2020 if (maxmtu) /* update only when maxmtu is positive */
2021 in6_maxmtu = maxmtu;
2022}
2023
2024void *
2025in6_domifattach(ifp)
2026 struct ifnet *ifp;
2027{
2028 struct in6_ifextra *ext;
2029
2030 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2031 bzero(ext, sizeof(*ext));
2032
2033 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2034 M_IFADDR, M_WAITOK);
2035 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2036
2037 ext->icmp6_ifstat =
2038 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2039 M_IFADDR, M_WAITOK);
2040 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2041
2042 ext->nd_ifinfo = nd6_ifattach(ifp);
2043 ext->scope6_id = scope6_ifattach(ifp);
2044 return ext;
2045}
2046
2047void
2048in6_domifdetach(ifp, aux)
2049 struct ifnet *ifp;
2050 void *aux;
2051{
2052 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2053
2054 scope6_ifdetach(ext->scope6_id);
2055 nd6_ifdetach(ext->nd_ifinfo);
2056 free(ext->in6_ifstat, M_IFADDR);
2057 free(ext->icmp6_ifstat, M_IFADDR);
2058 free(ext, M_IFADDR);
2059}
2060
2061/*
2062 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2063 * v4 mapped addr or v4 compat addr
2064 */
2065void
2066in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2067{
2068 bzero(sin, sizeof(*sin));
2069 sin->sin_len = sizeof(struct sockaddr_in);
2070 sin->sin_family = AF_INET;
2071 sin->sin_port = sin6->sin6_port;
2072 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2073}
2074
2075/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2076void
2077in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2078{
2079 bzero(sin6, sizeof(*sin6));
2080 sin6->sin6_len = sizeof(struct sockaddr_in6);
2081 sin6->sin6_family = AF_INET6;
2082 sin6->sin6_port = sin->sin_port;
2083 sin6->sin6_addr.s6_addr32[0] = 0;
2084 sin6->sin6_addr.s6_addr32[1] = 0;
2085 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2086 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2087}
2088
2089/* Convert sockaddr_in6 into sockaddr_in. */
2090void
2091in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2092{
2093 struct sockaddr_in *sin_p;
2094 struct sockaddr_in6 sin6;
2095
2096 /*
2097 * Save original sockaddr_in6 addr and convert it
2098 * to sockaddr_in.
2099 */
2100 sin6 = *(struct sockaddr_in6 *)nam;
2101 sin_p = (struct sockaddr_in *)nam;
2102 in6_sin6_2_sin(sin_p, &sin6);
2103}
2104
2105/* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2106void
2107in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2108{
2109 struct sockaddr_in *sin_p;
2110 struct sockaddr_in6 *sin6_p;
2111
2112 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2113 M_WAITOK);
2114 sin_p = (struct sockaddr_in *)*nam;
2115 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2116 FREE(*nam, M_SONAME);
2117 *nam = (struct sockaddr *)sin6_p;
2118}