in6.c revision 1.10 1 /* $NetBSD: in6.c,v 1.10 2000/02/01 22:52:10 thorpej Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)in.c 8.2 (Berkeley) 11/15/93
65 */
66
67 #include "opt_inet.h"
68
69 #include <sys/param.h>
70 #include <sys/ioctl.h>
71 #include <sys/errno.h>
72 #include <sys/malloc.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/sockio.h>
76 #include <sys/systm.h>
77 #include <sys/proc.h>
78 #include <sys/time.h>
79 #include <sys/kernel.h>
80 #include <sys/syslog.h>
81
82 #include <net/if.h>
83 #include <net/if_types.h>
84 #include <net/route.h>
85 #include "gif.h"
86 #if NGIF > 0
87 #include <net/if_gif.h>
88 #endif
89 #include <net/if_dl.h>
90
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <net/if_ether.h>
94
95 #include <netinet6/nd6.h>
96 #include <netinet6/ip6.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/mld6_var.h>
99 #include <netinet6/ip6_mroute.h>
100 #include <netinet6/in6_ifattach.h>
101
102 #include <net/net_osdep.h>
103
104 /*
105 * Definitions of some costant IP6 addresses.
106 */
107 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
108 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
109 const struct in6_addr in6addr_nodelocal_allnodes =
110 IN6ADDR_NODELOCAL_ALLNODES_INIT;
111 const struct in6_addr in6addr_linklocal_allnodes =
112 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
113 const struct in6_addr in6addr_linklocal_allrouters =
114 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
115
116 const struct in6_addr in6mask0 = IN6MASK0;
117 const struct in6_addr in6mask32 = IN6MASK32;
118 const struct in6_addr in6mask64 = IN6MASK64;
119 const struct in6_addr in6mask96 = IN6MASK96;
120 const struct in6_addr in6mask128 = IN6MASK128;
121
122 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
123 struct ifnet *, struct proc *));
124
125 /*
126 * This structure is used to keep track of in6_multi chains which belong to
127 * deleted interface addresses.
128 */
129 static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */
130
131 struct multi6_kludge {
132 LIST_ENTRY(multi6_kludge) mk_entry;
133 struct ifnet *mk_ifp;
134 struct in6_multihead mk_head;
135 };
136
137 /*
138 * Determine whether an IP6 address is in a reserved set of addresses
139 * that may not be forwarded, or whether datagrams to that destination
140 * may be forwarded.
141 */
142 int
143 in6_canforward(src, dst)
144 struct in6_addr *src, *dst;
145 {
146 if (IN6_IS_ADDR_LINKLOCAL(src) ||
147 IN6_IS_ADDR_LINKLOCAL(dst) ||
148 IN6_IS_ADDR_MULTICAST(dst))
149 return(0);
150 return(1);
151 }
152
153 /*
154 * Check if the loopback entry will be automatically generated.
155 * if 0 returned, will not be automatically generated.
156 * if 1 returned, will be automatically generated.
157 */
158 static int
159 in6_is_ifloop_auto(struct ifaddr *ifa)
160 {
161 #define SIN6(s) ((struct sockaddr_in6 *)s)
162 /*
163 * If RTF_CLONING is unset, or (IFF_LOOPBACK | IFF_POINTOPOINT),
164 * or netmask is all0 or all1, then cloning will not happen,
165 * then we can't rely on its loopback entry generation.
166 */
167 if ((ifa->ifa_flags & RTF_CLONING) == 0 ||
168 (ifa->ifa_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) ||
169 (SIN6(ifa->ifa_netmask)->sin6_len == sizeof(struct sockaddr_in6)
170 &&
171 IN6_ARE_ADDR_EQUAL(&SIN6(ifa->ifa_netmask)->sin6_addr,
172 &in6mask128)) ||
173 ((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_len == 0)
174 return 0;
175 else
176 return 1;
177 #undef SIN6
178 }
179
180 /*
181 * Subroutine for in6_ifaddloop() and in6_ifremloop().
182 * This routine does actual work.
183 */
184 static void
185 in6_ifloop_request(int cmd, struct ifaddr *ifa)
186 {
187 struct sockaddr_in6 lo_sa;
188 struct sockaddr_in6 all1_sa;
189 struct rtentry *nrt = NULL;
190
191 bzero(&lo_sa, sizeof(lo_sa));
192 bzero(&all1_sa, sizeof(all1_sa));
193 lo_sa.sin6_family = AF_INET6;
194 lo_sa.sin6_len = sizeof(struct sockaddr_in6);
195 all1_sa = lo_sa;
196 lo_sa.sin6_addr = in6addr_loopback;
197 all1_sa.sin6_addr = in6mask128;
198
199 /* So we add or remove static loopback entry, here. */
200 rtrequest(cmd, ifa->ifa_addr,
201 (struct sockaddr *)&lo_sa,
202 (struct sockaddr *)&all1_sa,
203 RTF_UP|RTF_HOST, &nrt);
204
205 /*
206 * Make sure rt_ifa be equal to IFA, the second argument of the
207 * function.
208 * We need this because when we refer rt_ifa->ia6_flags in ip6_input,
209 * we assume that the rt_ifa points to the address instead of the
210 * loopback address.
211 */
212 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
213 IFAFREE(nrt->rt_ifa);
214 IFAREF(ifa);
215 nrt->rt_ifa = ifa;
216 }
217 if (nrt)
218 nrt->rt_refcnt--;
219 }
220
221 /*
222 * Add ownaddr as loopback rtentry, if necessary(ex. on p2p link).
223 * Because, KAME needs loopback rtentry for ownaddr check in
224 * ip6_input().
225 */
226 static void
227 in6_ifaddloop(struct ifaddr *ifa)
228 {
229 if (!in6_is_ifloop_auto(ifa)) {
230 struct rtentry *rt;
231
232 /* If there is no loopback entry, allocate one. */
233 rt = rtalloc1(ifa->ifa_addr, 0);
234 if (rt == 0 || (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
235 in6_ifloop_request(RTM_ADD, ifa);
236 if (rt)
237 rt->rt_refcnt--;
238 }
239 }
240
241 /*
242 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
243 * if it exists.
244 */
245 static void
246 in6_ifremloop(struct ifaddr *ifa)
247 {
248 if (!in6_is_ifloop_auto(ifa)) {
249 struct in6_ifaddr *ia;
250 int ia_count = 0;
251
252 /* If only one ifa for the loopback entry, delete it. */
253 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
254 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa),
255 &ia->ia_addr.sin6_addr)) {
256 ia_count++;
257 if (ia_count > 1)
258 break;
259 }
260 }
261 if (ia_count == 1)
262 in6_ifloop_request(RTM_DELETE, ifa);
263 }
264 }
265
266 /*
267 * Subroutine for in6_ifaddproxy() and in6_ifremproxy().
268 * This routine does actual work.
269 * call in6_addmulti() when cmd == 1.
270 * call in6_delmulti() when cmd == 2.
271 */
272 static int
273 in6_ifproxy_request(int cmd, struct in6_ifaddr *ia)
274 {
275 int error = 0;
276
277 /*
278 * If we have an IPv6 dstaddr on adding p2p interface,
279 * join dstaddr's solicited multicast on necessary interface.
280 */
281 if ((ia->ia_ifp->if_flags & IFF_POINTOPOINT) &&
282 ia->ia_dstaddr.sin6_family == AF_INET6 &&
283 !IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
284 struct in6_ifaddr *ia_lan;
285
286 /*
287 * TODO: Join only on some specified interfaces by some
288 * configuration.
289 * Unsolicited Neighbor Advertisements will be also necessary.
290 *
291 * Now, join on interfaces which meets following.
292 * -IFF_BROADCAST and IFF_MULTICAST
293 * (NBMA is out of scope)
294 * -the prefix value is same as p2p dstaddr
295 */
296 for (ia_lan = in6_ifaddr; ia_lan; ia_lan = ia_lan->ia_next) {
297 struct in6_addr llsol;
298
299 if ((ia_lan->ia_ifp->if_flags &
300 (IFF_BROADCAST|IFF_MULTICAST)) !=
301 (IFF_BROADCAST|IFF_MULTICAST))
302 continue;
303 if (!IN6_ARE_MASKED_ADDR_EQUAL(IA6_IN6(ia),
304 IA6_IN6(ia_lan),
305 IA6_MASKIN6(ia_lan)))
306 continue;
307 if (ia_lan->ia_ifp == ia->ia_ifp)
308 continue;
309
310 /* init llsol */
311 bzero(&llsol, sizeof(struct in6_addr));
312 llsol.s6_addr16[0] = htons(0xff02);
313 llsol.s6_addr16[1] = htons(ia_lan->ia_ifp->if_index);
314 llsol.s6_addr32[1] = 0;
315 llsol.s6_addr32[2] = htonl(1);
316 llsol.s6_addr32[3] =
317 ia->ia_dstaddr.sin6_addr.s6_addr32[3];
318 llsol.s6_addr8[12] = 0xff;
319
320 if (cmd == 1)
321 (void)in6_addmulti(&llsol,
322 ia_lan->ia_ifp,
323 &error);
324 else if (cmd == 2) {
325 struct in6_multi *in6m;
326
327 IN6_LOOKUP_MULTI(llsol,
328 ia_lan->ia_ifp,
329 in6m);
330 if (in6m)
331 in6_delmulti(in6m);
332 }
333 }
334 }
335 return error;
336 }
337
338 static int
339 in6_ifaddproxy(struct in6_ifaddr *ia)
340 {
341 return(in6_ifproxy_request(1, ia));
342 }
343
344 static void
345 in6_ifremproxy(struct in6_ifaddr *ia)
346 {
347 in6_ifproxy_request(2, ia);
348 }
349
350 int
351 in6_ifindex2scopeid(idx)
352 int idx;
353 {
354 struct ifnet *ifp;
355 struct ifaddr *ifa;
356 struct sockaddr_in6 *sin6;
357
358 if (idx < 0 || if_index < idx)
359 return -1;
360 ifp = ifindex2ifnet[idx];
361
362 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
363 {
364 if (ifa->ifa_addr->sa_family != AF_INET6)
365 continue;
366 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
367 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
368 return sin6->sin6_scope_id & 0xffff;
369 }
370
371 return -1;
372 }
373
374 int
375 in6_mask2len(mask)
376 struct in6_addr *mask;
377 {
378 int x, y;
379
380 for (x = 0; x < sizeof(*mask); x++) {
381 if (mask->s6_addr8[x] != 0xff)
382 break;
383 }
384 y = 0;
385 if (x < sizeof(*mask)) {
386 for (y = 0; y < 8; y++) {
387 if ((mask->s6_addr8[x] & (0x80 >> y)) == 0)
388 break;
389 }
390 }
391 return x * 8 + y;
392 }
393
394 void
395 in6_len2mask(mask, len)
396 struct in6_addr *mask;
397 int len;
398 {
399 int i;
400
401 bzero(mask, sizeof(*mask));
402 for (i = 0; i < len / 8; i++)
403 mask->s6_addr8[i] = 0xff;
404 if (len % 8)
405 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
406 }
407
408 int in6_interfaces; /* number of external internet interfaces */
409
410 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
411 #define ia62ifa(ia6) ((struct ifaddr *)(ia6))
412
413 int
414 in6_control(so, cmd, data, ifp, p)
415 struct socket *so;
416 u_long cmd;
417 caddr_t data;
418 struct ifnet *ifp;
419 struct proc *p;
420 {
421 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
422 struct in6_ifaddr *ia, *oia;
423 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
424 struct sockaddr_in6 oldaddr, net;
425 int error = 0, hostIsNew, prefixIsNew;
426 time_t time_second = (time_t)time.tv_sec;
427 int privileged;
428
429 privileged = 0;
430 if (p && !suser(p->p_ucred, &p->p_acflag))
431 privileged++;
432
433 /*
434 * xxx should prevent processes for link-local addresses?
435 */
436 #if NGIF > 0
437 if (ifp && ifp->if_type == IFT_GIF) {
438 switch (cmd) {
439 case SIOCSIFPHYADDR_IN6:
440 if (!privileged)
441 return(EPERM);
442 /*fall through*/
443 case SIOCGIFPSRCADDR_IN6:
444 case SIOCGIFPDSTADDR_IN6:
445 return gif_ioctl(ifp, cmd, data);
446 }
447 }
448 #endif
449 switch (cmd) {
450 case SIOCGETSGCNT_IN6:
451 case SIOCGETMIFCNT_IN6:
452 return (mrt6_ioctl(cmd, data));
453 }
454
455 if (ifp == 0)
456 return(EOPNOTSUPP);
457
458 switch (cmd) {
459 case SIOCSNDFLUSH_IN6:
460 case SIOCSPFXFLUSH_IN6:
461 case SIOCSRTRFLUSH_IN6:
462 case SIOCSDEFIFACE_IN6:
463 if (!privileged)
464 return(EPERM);
465 /*fall through*/
466 case SIOCGIFINFO_IN6:
467 case SIOCGDRLST_IN6:
468 case SIOCGPRLST_IN6:
469 case SIOCGNBRINFO_IN6:
470 case SIOCGDEFIFACE_IN6:
471 return(nd6_ioctl(cmd, data, ifp));
472 }
473
474 switch (cmd) {
475 case SIOCSIFPREFIX_IN6:
476 case SIOCDIFPREFIX_IN6:
477 case SIOCAIFPREFIX_IN6:
478 case SIOCCIFPREFIX_IN6:
479 case SIOCSGIFPREFIX_IN6:
480 if (!privileged)
481 return(EPERM);
482 /*fall through*/
483 case SIOCGIFPREFIX_IN6:
484 return(in6_prefix_ioctl(so, cmd, data, ifp));
485 }
486
487 switch (cmd) {
488 case SIOCALIFADDR:
489 case SIOCDLIFADDR:
490 if (!privileged)
491 return(EPERM);
492 /*fall through*/
493 case SIOCGLIFADDR:
494 return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
495 }
496
497 /*
498 * Find address for this interface, if it exists.
499 */
500 {
501
502 struct sockaddr_in6 *sa6 =
503 (struct sockaddr_in6 *)&ifra->ifra_addr;
504
505 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
506 if (sa6->sin6_addr.s6_addr16[1] == 0) {
507 /* interface ID is not embedded by the user */
508 sa6->sin6_addr.s6_addr16[1] =
509 htons(ifp->if_index);
510 }
511 else
512 if (sa6->sin6_addr.s6_addr16[1] !=
513 htons(ifp->if_index))
514 return(EINVAL); /* ifid is contradict */
515 if (sa6->sin6_scope_id) {
516 if (sa6->sin6_scope_id !=
517 (u_int32_t)ifp->if_index)
518 return(EINVAL);
519 sa6->sin6_scope_id = 0; /* XXX: good way? */
520 }
521 }
522 }
523 #if 0
524 if (ifra->ifra_addr.sin6_family == AF_INET6) {
525 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
526 }
527 #else
528 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
529 #endif
530
531 switch (cmd) {
532
533 case SIOCDIFADDR_IN6:
534 if (ia == 0)
535 return(EADDRNOTAVAIL);
536 /* FALLTHROUGH */
537 case SIOCAIFADDR_IN6:
538 case SIOCSIFADDR_IN6:
539 case SIOCSIFNETMASK_IN6:
540 case SIOCSIFDSTADDR_IN6:
541 if (!privileged)
542 return(EPERM);
543 if (ia == 0) {
544 ia = (struct in6_ifaddr *)
545 malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
546 if (ia == NULL)
547 return (ENOBUFS);
548 bzero((caddr_t)ia, sizeof(*ia));
549 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
550 ia->ia_ifa.ifa_dstaddr
551 = (struct sockaddr *)&ia->ia_dstaddr;
552 ia->ia_ifa.ifa_netmask
553 = (struct sockaddr *)&ia->ia_prefixmask;
554
555 ia->ia_ifp = ifp;
556 if ((oia = in6_ifaddr) != NULL) {
557 for ( ; oia->ia_next; oia = oia->ia_next)
558 continue;
559 oia->ia_next = ia;
560 } else
561 in6_ifaddr = ia;
562 IFAREF(&ia->ia_ifa);
563
564 TAILQ_INSERT_TAIL(&ifp->if_addrlist,
565 (struct ifaddr *)ia, ifa_list);
566 IFAREF(&ia->ia_ifa);
567
568 if ((ifp->if_flags & IFF_LOOPBACK) == 0)
569 in6_interfaces++; /*XXX*/
570 }
571
572 if (cmd == SIOCAIFADDR_IN6) {
573 /* sanity for overflow - beware unsigned */
574 struct in6_addrlifetime *lt;
575 lt = &ifra->ifra_lifetime;
576 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
577 && lt->ia6t_vltime + time_second < time_second) {
578 return EINVAL;
579 }
580 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
581 && lt->ia6t_pltime + time_second < time_second) {
582 return EINVAL;
583 }
584 }
585 break;
586
587 case SIOCGIFADDR_IN6:
588 /* This interface is basically deprecated. use SIOCGIFCONF. */
589 /* fall through */
590 case SIOCGIFAFLAG_IN6:
591 case SIOCGIFNETMASK_IN6:
592 case SIOCGIFDSTADDR_IN6:
593 case SIOCGIFALIFETIME_IN6:
594 /* must think again about its semantics */
595 if (ia == 0)
596 return(EADDRNOTAVAIL);
597 break;
598 case SIOCSIFALIFETIME_IN6:
599 {
600 struct in6_addrlifetime *lt;
601
602 if (!privileged)
603 return(EPERM);
604 if (ia == 0)
605 return(EADDRNOTAVAIL);
606 /* sanity for overflow - beware unsigned */
607 lt = &ifr->ifr_ifru.ifru_lifetime;
608 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
609 && lt->ia6t_vltime + time_second < time_second) {
610 return EINVAL;
611 }
612 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
613 && lt->ia6t_pltime + time_second < time_second) {
614 return EINVAL;
615 }
616 break;
617 }
618 }
619
620 switch (cmd) {
621
622 case SIOCGIFADDR_IN6:
623 ifr->ifr_addr = ia->ia_addr;
624 break;
625
626 case SIOCGIFDSTADDR_IN6:
627 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
628 return(EINVAL);
629 ifr->ifr_dstaddr = ia->ia_dstaddr;
630 break;
631
632 case SIOCGIFNETMASK_IN6:
633 ifr->ifr_addr = ia->ia_prefixmask;
634 break;
635
636 case SIOCGIFAFLAG_IN6:
637 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
638 break;
639
640 case SIOCGIFSTAT_IN6:
641 if (ifp == NULL)
642 return EINVAL;
643 if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax
644 || in6_ifstat[ifp->if_index] == NULL) {
645 /* return EAFNOSUPPORT? */
646 bzero(&ifr->ifr_ifru.ifru_stat,
647 sizeof(ifr->ifr_ifru.ifru_stat));
648 } else
649 ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index];
650 break;
651
652 case SIOCGIFSTAT_ICMP6:
653 if (ifp == NULL)
654 return EINVAL;
655 if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax ||
656 icmp6_ifstat[ifp->if_index] == NULL) {
657 /* return EAFNOSUPPORT? */
658 bzero(&ifr->ifr_ifru.ifru_stat,
659 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
660 } else
661 ifr->ifr_ifru.ifru_icmp6stat =
662 *icmp6_ifstat[ifp->if_index];
663 break;
664
665 case SIOCSIFDSTADDR_IN6:
666 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
667 return(EINVAL);
668 oldaddr = ia->ia_dstaddr;
669 ia->ia_dstaddr = ifr->ifr_dstaddr;
670
671 /* link-local index check */
672 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
673 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
674 /* interface ID is not embedded by the user */
675 ia->ia_dstaddr.sin6_addr.s6_addr16[1]
676 = htons(ifp->if_index);
677 }
678 else
679 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
680 htons(ifp->if_index)) {
681 ia->ia_dstaddr = oldaddr;
682 return(EINVAL); /* ifid is contradict */
683 }
684 }
685
686 if (ifp->if_ioctl && (error = (ifp->if_ioctl)
687 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
688 ia->ia_dstaddr = oldaddr;
689 return(error);
690 }
691 if (ia->ia_flags & IFA_ROUTE) {
692 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
693 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
694 ia->ia_ifa.ifa_dstaddr =
695 (struct sockaddr *)&ia->ia_dstaddr;
696 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
697 }
698 break;
699
700 case SIOCGIFALIFETIME_IN6:
701 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
702 break;
703
704 case SIOCSIFALIFETIME_IN6:
705 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
706 /* for sanity */
707 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
708 ia->ia6_lifetime.ia6t_expire =
709 time_second + ia->ia6_lifetime.ia6t_vltime;
710 } else
711 ia->ia6_lifetime.ia6t_expire = 0;
712 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
713 ia->ia6_lifetime.ia6t_preferred =
714 time_second + ia->ia6_lifetime.ia6t_pltime;
715 } else
716 ia->ia6_lifetime.ia6t_preferred = 0;
717 break;
718
719 case SIOCSIFADDR_IN6:
720 return(in6_ifinit(ifp, ia, &ifr->ifr_addr, 1));
721
722 case SIOCSIFNETMASK_IN6:
723 ia->ia_prefixmask = ifr->ifr_addr;
724 bzero(&net, sizeof(net));
725 net.sin6_len = sizeof(struct sockaddr_in6);
726 net.sin6_family = AF_INET6;
727 net.sin6_port = htons(0);
728 net.sin6_flowinfo = htonl(0);
729 net.sin6_addr.s6_addr32[0]
730 = ia->ia_addr.sin6_addr.s6_addr32[0] &
731 ia->ia_prefixmask.sin6_addr.s6_addr32[0];
732 net.sin6_addr.s6_addr32[1]
733 = ia->ia_addr.sin6_addr.s6_addr32[1] &
734 ia->ia_prefixmask.sin6_addr.s6_addr32[1];
735 net.sin6_addr.s6_addr32[2]
736 = ia->ia_addr.sin6_addr.s6_addr32[2] &
737 ia->ia_prefixmask.sin6_addr.s6_addr32[2];
738 net.sin6_addr.s6_addr32[3]
739 = ia->ia_addr.sin6_addr.s6_addr32[3] &
740 ia->ia_prefixmask.sin6_addr.s6_addr32[3];
741 ia->ia_net = net;
742 break;
743
744 case SIOCAIFADDR_IN6:
745 prefixIsNew = 0;
746 hostIsNew = 1;
747
748 if (ifra->ifra_addr.sin6_len == 0) {
749 ifra->ifra_addr = ia->ia_addr;
750 hostIsNew = 0;
751 } else if (IN6_ARE_ADDR_EQUAL(&ifra->ifra_addr.sin6_addr,
752 &ia->ia_addr.sin6_addr))
753 hostIsNew = 0;
754
755 if (ifra->ifra_prefixmask.sin6_len) {
756 in6_ifscrub(ifp, ia);
757 ia->ia_prefixmask = ifra->ifra_prefixmask;
758 prefixIsNew = 1;
759 }
760 if ((ifp->if_flags & IFF_POINTOPOINT) &&
761 (ifra->ifra_dstaddr.sin6_family == AF_INET6)) {
762 in6_ifscrub(ifp, ia);
763 ia->ia_dstaddr = ifra->ifra_dstaddr;
764 /* link-local index check: should be a separate function? */
765 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
766 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
767 /*
768 * interface ID is not embedded by
769 * the user
770 */
771 ia->ia_dstaddr.sin6_addr.s6_addr16[1]
772 = htons(ifp->if_index);
773 }
774 else
775 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
776 htons(ifp->if_index)) {
777 ia->ia_dstaddr = oldaddr;
778 return(EINVAL); /* ifid is contradict */
779 }
780 }
781 prefixIsNew = 1; /* We lie; but effect's the same */
782 }
783 if (ifra->ifra_addr.sin6_family == AF_INET6 &&
784 (hostIsNew || prefixIsNew))
785 error = in6_ifinit(ifp, ia, &ifra->ifra_addr, 0);
786 if (ifra->ifra_addr.sin6_family == AF_INET6
787 && hostIsNew && (ifp->if_flags & IFF_MULTICAST)) {
788 int error_local = 0;
789
790 /*
791 * join solicited multicast addr for new host id
792 */
793 struct in6_addr llsol;
794 bzero(&llsol, sizeof(struct in6_addr));
795 llsol.s6_addr16[0] = htons(0xff02);
796 llsol.s6_addr16[1] = htons(ifp->if_index);
797 llsol.s6_addr32[1] = 0;
798 llsol.s6_addr32[2] = htonl(1);
799 llsol.s6_addr32[3] =
800 ifra->ifra_addr.sin6_addr.s6_addr32[3];
801 llsol.s6_addr8[12] = 0xff;
802 (void)in6_addmulti(&llsol, ifp, &error_local);
803 if (error == 0)
804 error = error_local;
805 }
806 /* Join dstaddr's solicited multicast if necessary. */
807 if (nd6_proxyall && hostIsNew) {
808 int error_local;
809
810 error_local = in6_ifaddproxy(ia);
811 if (error == 0)
812 error = error_local;
813 }
814
815 ia->ia6_flags = ifra->ifra_flags;
816 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
817
818 ia->ia6_lifetime = ifra->ifra_lifetime;
819 /* for sanity */
820 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
821 ia->ia6_lifetime.ia6t_expire =
822 time_second + ia->ia6_lifetime.ia6t_vltime;
823 } else
824 ia->ia6_lifetime.ia6t_expire = 0;
825 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
826 ia->ia6_lifetime.ia6t_preferred =
827 time_second + ia->ia6_lifetime.ia6t_pltime;
828 } else
829 ia->ia6_lifetime.ia6t_preferred = 0;
830
831 /*
832 * Perform DAD, if needed.
833 * XXX It may be of use, if we can administratively
834 * disable DAD.
835 */
836 switch (ifp->if_type) {
837 case IFT_ARCNET:
838 case IFT_ETHER:
839 case IFT_FDDI:
840 #if 0
841 case IFT_ATM:
842 case IFT_SLIP:
843 case IFT_PPP:
844 #endif
845 ia->ia6_flags |= IN6_IFF_TENTATIVE;
846 nd6_dad_start((struct ifaddr *)ia, NULL);
847 break;
848 case IFT_FAITH:
849 case IFT_GIF:
850 case IFT_LOOP:
851 default:
852 break;
853 }
854
855 if (hostIsNew) {
856 int iilen;
857 int error_local = 0;
858
859 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) -
860 in6_mask2len(&ia->ia_prefixmask.sin6_addr);
861 error_local = in6_prefix_add_ifid(iilen, ia);
862 if (error == 0)
863 error = error_local;
864 }
865
866 return(error);
867
868 case SIOCDIFADDR_IN6:
869 in6_purgeaddr(&ia->ia_ifa, ifp);
870 break;
871
872 default:
873 if (ifp == 0 || ifp->if_ioctl == 0)
874 return(EOPNOTSUPP);
875 return((*ifp->if_ioctl)(ifp, cmd, data));
876 }
877 return(0);
878 }
879
880 void
881 in6_purgeaddr(ifa, ifp)
882 struct ifaddr *ifa;
883 struct ifnet *ifp;
884 {
885 struct in6_ifaddr *oia, *ia = (void *) ifa;
886
887 in6_ifscrub(ifp, ia);
888
889 if (ifp->if_flags & IFF_MULTICAST) {
890 /*
891 * delete solicited multicast addr for deleting host id
892 */
893 struct in6_multi *in6m;
894 struct in6_addr llsol;
895 bzero(&llsol, sizeof(struct in6_addr));
896 llsol.s6_addr16[0] = htons(0xff02);
897 llsol.s6_addr16[1] = htons(ifp->if_index);
898 llsol.s6_addr32[1] = 0;
899 llsol.s6_addr32[2] = htonl(1);
900 llsol.s6_addr32[3] =
901 ia->ia_addr.sin6_addr.s6_addr32[3];
902 llsol.s6_addr8[12] = 0xff;
903
904 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
905 if (in6m)
906 in6_delmulti(in6m);
907 }
908 /* Leave dstaddr's solicited multicast if necessary. */
909 if (nd6_proxyall)
910 in6_ifremproxy(ia);
911
912 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
913 IFAFREE(&ia->ia_ifa);
914
915 oia = ia;
916 if (oia == (ia = in6_ifaddr))
917 in6_ifaddr = ia->ia_next;
918 else {
919 while (ia->ia_next && (ia->ia_next != oia))
920 ia = ia->ia_next;
921 if (ia->ia_next)
922 ia->ia_next = oia->ia_next;
923 else
924 printf("Didn't unlink in6_ifaddr from list\n");
925 }
926 {
927 int iilen;
928
929 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) -
930 in6_mask2len(&oia->ia_prefixmask.sin6_addr);
931 in6_prefix_remove_ifid(iilen, oia);
932 }
933 if (oia->ia6_multiaddrs.lh_first != NULL) {
934 /*
935 * XXX thorpej (at) netbsd.org -- if the interface is going
936 * XXX away, don't save the multicast entries, delete them!
937 */
938 if (oia->ia_ifa.ifa_ifp->if_output == if_nulloutput) {
939 struct in6_multi *in6m;
940
941 while ((in6m =
942 LIST_FIRST(&oia->ia6_multiaddrs)) != NULL)
943 in6_delmulti(in6m);
944 } else
945 in6_savemkludge(oia);
946 }
947
948 IFAFREE(&oia->ia_ifa);
949 }
950
951 /*
952 * SIOC[GAD]LIFADDR.
953 * SIOCGLIFADDR: get first address. (???)
954 * SIOCGLIFADDR with IFLR_PREFIX:
955 * get first address that matches the specified prefix.
956 * SIOCALIFADDR: add the specified address.
957 * SIOCALIFADDR with IFLR_PREFIX:
958 * add the specified prefix, filling hostid part from
959 * the first link-local address. prefixlen must be <= 64.
960 * SIOCDLIFADDR: delete the specified address.
961 * SIOCDLIFADDR with IFLR_PREFIX:
962 * delete the first address that matches the specified prefix.
963 * return values:
964 * EINVAL on invalid parameters
965 * EADDRNOTAVAIL on prefix match failed/specified address not found
966 * other values may be returned from in6_ioctl()
967 *
968 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
969 * this is to accomodate address naming scheme other than RFC2374,
970 * in the future.
971 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
972 * address encoding scheme. (see figure on page 8)
973 */
974 static int
975 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
976 struct socket *so;
977 u_long cmd;
978 caddr_t data;
979 struct ifnet *ifp;
980 struct proc *p;
981 {
982 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
983 struct ifaddr *ifa;
984 struct sockaddr *sa;
985
986 /* sanity checks */
987 if (!data || !ifp) {
988 panic("invalid argument to in6_lifaddr_ioctl");
989 /*NOTRECHED*/
990 }
991
992 switch (cmd) {
993 case SIOCGLIFADDR:
994 /* address must be specified on GET with IFLR_PREFIX */
995 if ((iflr->flags & IFLR_PREFIX) == 0)
996 break;
997 /*FALLTHROUGH*/
998 case SIOCALIFADDR:
999 case SIOCDLIFADDR:
1000 /* address must be specified on ADD and DELETE */
1001 sa = (struct sockaddr *)&iflr->addr;
1002 if (sa->sa_family != AF_INET6)
1003 return EINVAL;
1004 if (sa->sa_len != sizeof(struct sockaddr_in6))
1005 return EINVAL;
1006 /* XXX need improvement */
1007 sa = (struct sockaddr *)&iflr->dstaddr;
1008 if (sa->sa_family && sa->sa_family != AF_INET6)
1009 return EINVAL;
1010 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1011 return EINVAL;
1012 break;
1013 default: /*shouldn't happen*/
1014 #if 0
1015 panic("invalid cmd to in6_lifaddr_ioctl");
1016 /*NOTREACHED*/
1017 #else
1018 return EOPNOTSUPP;
1019 #endif
1020 }
1021 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1022 return EINVAL;
1023
1024 switch (cmd) {
1025 case SIOCALIFADDR:
1026 {
1027 struct in6_aliasreq ifra;
1028 struct in6_addr *hostid = NULL;
1029 int prefixlen;
1030
1031 if ((iflr->flags & IFLR_PREFIX) != 0) {
1032 struct sockaddr_in6 *sin6;
1033
1034 /*
1035 * hostid is to fill in the hostid part of the
1036 * address. hostid points to the first link-local
1037 * address attached to the interface.
1038 */
1039 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp);
1040 if (!ifa)
1041 return EADDRNOTAVAIL;
1042 hostid = IFA_IN6(ifa);
1043
1044 /* prefixlen must be <= 64. */
1045 if (64 < iflr->prefixlen)
1046 return EINVAL;
1047 prefixlen = iflr->prefixlen;
1048
1049 /* hostid part must be zero. */
1050 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1051 if (sin6->sin6_addr.s6_addr32[2] != 0
1052 || sin6->sin6_addr.s6_addr32[3] != 0) {
1053 return EINVAL;
1054 }
1055 } else
1056 prefixlen = iflr->prefixlen;
1057
1058 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1059 bzero(&ifra, sizeof(ifra));
1060 bcopy(iflr->iflr_name, ifra.ifra_name,
1061 sizeof(ifra.ifra_name));
1062
1063 bcopy(&iflr->addr, &ifra.ifra_addr,
1064 ((struct sockaddr *)&iflr->addr)->sa_len);
1065 if (hostid) {
1066 /* fill in hostid part */
1067 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1068 hostid->s6_addr32[2];
1069 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1070 hostid->s6_addr32[3];
1071 }
1072
1073 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1074 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1075 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1076 if (hostid) {
1077 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1078 hostid->s6_addr32[2];
1079 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1080 hostid->s6_addr32[3];
1081 }
1082 }
1083
1084 ifra.ifra_prefixmask.sin6_family = AF_INET6;
1085 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1086 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1087
1088 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1089 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1090 }
1091 case SIOCGLIFADDR:
1092 case SIOCDLIFADDR:
1093 {
1094 struct in6_ifaddr *ia;
1095 struct in6_addr mask, candidate, match;
1096 struct sockaddr_in6 *sin6;
1097 int cmp;
1098
1099 bzero(&mask, sizeof(mask));
1100 if (iflr->flags & IFLR_PREFIX) {
1101 /* lookup a prefix rather than address. */
1102 in6_len2mask(&mask, iflr->prefixlen);
1103
1104 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1105 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1106 match.s6_addr32[0] &= mask.s6_addr32[0];
1107 match.s6_addr32[1] &= mask.s6_addr32[1];
1108 match.s6_addr32[2] &= mask.s6_addr32[2];
1109 match.s6_addr32[3] &= mask.s6_addr32[3];
1110
1111 /* if you set extra bits, that's wrong */
1112 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1113 return EINVAL;
1114
1115 cmp = 1;
1116 } else {
1117 if (cmd == SIOCGLIFADDR) {
1118 /* on getting an address, take the 1st match */
1119 cmp = 0; /*XXX*/
1120 } else {
1121 /* on deleting an address, do exact match */
1122 in6_len2mask(&mask, 128);
1123 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1124 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1125
1126 cmp = 1;
1127 }
1128 }
1129
1130 for (ifa = ifp->if_addrlist.tqh_first;
1131 ifa;
1132 ifa = ifa->ifa_list.tqe_next)
1133 {
1134 if (ifa->ifa_addr->sa_family != AF_INET6)
1135 continue;
1136 if (!cmp)
1137 break;
1138 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1139 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1140 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1141 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1142 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1143 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1144 break;
1145 }
1146 if (!ifa)
1147 return EADDRNOTAVAIL;
1148 ia = ifa2ia6(ifa);
1149
1150 if (cmd == SIOCGLIFADDR) {
1151 /* fill in the if_laddrreq structure */
1152 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1153
1154 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1155 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1156 ia->ia_dstaddr.sin6_len);
1157 } else
1158 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1159
1160 iflr->prefixlen =
1161 in6_mask2len(&ia->ia_prefixmask.sin6_addr);
1162
1163 iflr->flags = ia->ia6_flags; /*XXX*/
1164
1165 return 0;
1166 } else {
1167 struct in6_aliasreq ifra;
1168
1169 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1170 bzero(&ifra, sizeof(ifra));
1171 bcopy(iflr->iflr_name, ifra.ifra_name,
1172 sizeof(ifra.ifra_name));
1173
1174 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1175 ia->ia_addr.sin6_len);
1176 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1177 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1178 ia->ia_dstaddr.sin6_len);
1179 }
1180 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1181 ia->ia_prefixmask.sin6_len);
1182
1183 ifra.ifra_flags = ia->ia6_flags;
1184 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1185 ifp, p);
1186 }
1187 }
1188 }
1189
1190 return EOPNOTSUPP; /*just for safety*/
1191 }
1192
1193 /*
1194 * Delete any existing route for an interface.
1195 */
1196 void
1197 in6_ifscrub(ifp, ia)
1198 register struct ifnet *ifp;
1199 register struct in6_ifaddr *ia;
1200 {
1201 if ((ia->ia_flags & IFA_ROUTE) == 0)
1202 return;
1203 if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
1204 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
1205 else
1206 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
1207 ia->ia_flags &= ~IFA_ROUTE;
1208
1209 /* Remove ownaddr's loopback rtentry, if it exists. */
1210 in6_ifremloop(&(ia->ia_ifa));
1211 }
1212
1213 /*
1214 * Initialize an interface's intetnet6 address
1215 * and routing table entry.
1216 */
1217 int
1218 in6_ifinit(ifp, ia, sin6, scrub)
1219 struct ifnet *ifp;
1220 struct in6_ifaddr *ia;
1221 struct sockaddr_in6 *sin6;
1222 int scrub;
1223 {
1224 struct sockaddr_in6 oldaddr;
1225 int error, flags = RTF_UP;
1226 int s = splimp();
1227
1228 oldaddr = ia->ia_addr;
1229 ia->ia_addr = *sin6;
1230 /*
1231 * Give the interface a chance to initialize
1232 * if this is its first address,
1233 * and to validate the address if necessary.
1234 */
1235 if (ifp->if_ioctl &&
1236 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1237 splx(s);
1238 ia->ia_addr = oldaddr;
1239 return(error);
1240 }
1241
1242 switch (ifp->if_type) {
1243 case IFT_ARCNET:
1244 case IFT_ETHER:
1245 case IFT_FDDI:
1246 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1247 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1248 break;
1249 case IFT_PPP:
1250 ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest;
1251 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1252 break;
1253 }
1254
1255 splx(s);
1256 if (scrub) {
1257 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1258 in6_ifscrub(ifp, ia);
1259 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1260 }
1261 /* xxx
1262 * in_socktrim
1263 */
1264 /*
1265 * Add route for the network.
1266 */
1267 ia->ia_ifa.ifa_metric = ifp->if_metric;
1268 if (ifp->if_flags & IFF_LOOPBACK) {
1269 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
1270 flags |= RTF_HOST;
1271 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1272 if (ia->ia_dstaddr.sin6_family != AF_INET6)
1273 return(0);
1274 flags |= RTF_HOST;
1275 }
1276 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
1277 ia->ia_flags |= IFA_ROUTE;
1278
1279 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
1280 in6_ifaddloop(&(ia->ia_ifa));
1281
1282 if (ifp->if_flags & IFF_MULTICAST)
1283 in6_restoremkludge(ia, ifp);
1284
1285 return(error);
1286 }
1287
1288 /*
1289 * Multicast address kludge:
1290 * If there were any multicast addresses attached to this interface address,
1291 * either move them to another address on this interface, or save them until
1292 * such time as this interface is reconfigured for IPv6.
1293 */
1294 void
1295 in6_savemkludge(oia)
1296 struct in6_ifaddr *oia;
1297 {
1298 struct in6_ifaddr *ia;
1299 struct in6_multi *in6m, *next;
1300
1301 IFP_TO_IA6(oia->ia_ifp, ia);
1302 if (ia) { /* there is another address */
1303 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1304 next = in6m->in6m_entry.le_next;
1305 IFAFREE(&in6m->in6m_ia->ia_ifa);
1306 IFAREF(&ia->ia_ifa);
1307 in6m->in6m_ia = ia;
1308 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1309 }
1310 } else { /* last address on this if deleted, save */
1311 struct multi6_kludge *mk;
1312
1313 mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
1314
1315 LIST_INIT(&mk->mk_head);
1316 mk->mk_ifp = oia->ia_ifp;
1317
1318 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1319 next = in6m->in6m_entry.le_next;
1320 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
1321 }
1322
1323 if (mk->mk_head.lh_first != NULL) {
1324 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
1325 }
1326 else {
1327 FREE(mk, M_IPMADDR);
1328 }
1329 }
1330 }
1331
1332 /*
1333 * Continuation of multicast address hack:
1334 * If there was a multicast group list previously saved for this interface,
1335 * then we re-attach it to the first address configured on the i/f.
1336 */
1337 void
1338 in6_restoremkludge(ia, ifp)
1339 struct in6_ifaddr *ia;
1340 struct ifnet *ifp;
1341 {
1342 struct multi6_kludge *mk;
1343
1344 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1345 if (mk->mk_ifp == ifp) {
1346 struct in6_multi *in6m, *next;
1347
1348 for (in6m = mk->mk_head.lh_first; in6m; in6m = next){
1349 next = in6m->in6m_entry.le_next;
1350 LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
1351 in6m, in6m_entry);
1352 }
1353 LIST_REMOVE(mk, mk_entry);
1354 free(mk, M_IPMADDR);
1355 break;
1356 }
1357 }
1358 }
1359
1360 /*
1361 * Add an address to the list of IP6 multicast addresses for a
1362 * given interface.
1363 */
1364 struct in6_multi *
1365 in6_addmulti(maddr6, ifp, errorp)
1366 register struct in6_addr *maddr6;
1367 register struct ifnet *ifp;
1368 int *errorp;
1369 {
1370 struct in6_ifaddr *ia;
1371 struct in6_ifreq ifr;
1372 struct in6_multi *in6m;
1373 int s = splsoftnet();
1374
1375 *errorp = 0;
1376 /*
1377 * See if address already in list.
1378 */
1379 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
1380 if (in6m != NULL) {
1381 /*
1382 * Found it; just increment the refrence count.
1383 */
1384 in6m->in6m_refcount++;
1385 } else {
1386 /*
1387 * New address; allocate a new multicast record
1388 * and link it into the interface's multicast list.
1389 */
1390 in6m = (struct in6_multi *)
1391 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1392 if (in6m == NULL) {
1393 splx(s);
1394 *errorp = ENOBUFS;
1395 return(NULL);
1396 }
1397 in6m->in6m_addr = *maddr6;
1398 in6m->in6m_ifp = ifp;
1399 in6m->in6m_refcount = 1;
1400 IFP_TO_IA6(ifp, ia);
1401 if (ia == NULL) {
1402 free(in6m, M_IPMADDR);
1403 splx(s);
1404 *errorp = EADDRNOTAVAIL; /* appropriate? */
1405 return(NULL);
1406 }
1407 in6m->in6m_ia = ia;
1408 IFAREF(&ia->ia_ifa); /* gain a reference */
1409 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1410
1411 /*
1412 * Ask the network driver to update its multicast reception
1413 * filter appropriately for the new address.
1414 */
1415 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1416 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1417 ifr.ifr_addr.sin6_family = AF_INET6;
1418 ifr.ifr_addr.sin6_addr = *maddr6;
1419 if (ifp->if_ioctl == NULL)
1420 *errorp = ENXIO; /* XXX: appropriate? */
1421 else
1422 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
1423 (caddr_t)&ifr);
1424 if (*errorp) {
1425 LIST_REMOVE(in6m, in6m_entry);
1426 free(in6m, M_IPMADDR);
1427 splx(s);
1428 return(NULL);
1429 }
1430 /*
1431 * Let MLD6 know that we have joined a new IP6 multicast
1432 * group.
1433 */
1434 mld6_start_listening(in6m);
1435 }
1436 splx(s);
1437 return(in6m);
1438 }
1439
1440 /*
1441 * Delete a multicast address record.
1442 */
1443 void
1444 in6_delmulti(in6m)
1445 struct in6_multi *in6m;
1446 {
1447 struct in6_ifreq ifr;
1448 int s = splsoftnet();
1449
1450 if (--in6m->in6m_refcount == 0) {
1451 /*
1452 * No remaining claims to this record; let MLD6 know
1453 * that we are leaving the multicast group.
1454 */
1455 mld6_stop_listening(in6m);
1456
1457 /*
1458 * Unlink from list.
1459 */
1460 LIST_REMOVE(in6m, in6m_entry);
1461 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1462
1463 /*
1464 * Notify the network driver to update its multicast
1465 * reception filter.
1466 */
1467 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1468 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1469 ifr.ifr_addr.sin6_family = AF_INET6;
1470 ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
1471 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
1472 SIOCDELMULTI, (caddr_t)&ifr);
1473 free(in6m, M_IPMADDR);
1474 }
1475 splx(s);
1476 }
1477
1478 /*
1479 * Find an IPv6 interface link-local address specific to an interface.
1480 */
1481 struct in6_ifaddr *
1482 in6ifa_ifpforlinklocal(ifp)
1483 struct ifnet *ifp;
1484 {
1485 register struct ifaddr *ifa;
1486
1487 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1488 {
1489 if (ifa->ifa_addr == NULL)
1490 continue; /* just for safety */
1491 if (ifa->ifa_addr->sa_family != AF_INET6)
1492 continue;
1493 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)))
1494 break;
1495 }
1496
1497 return((struct in6_ifaddr *)ifa);
1498 }
1499
1500
1501 /*
1502 * find the internet address corresponding to a given interface and address.
1503 */
1504 struct in6_ifaddr *
1505 in6ifa_ifpwithaddr(ifp, addr)
1506 struct ifnet *ifp;
1507 struct in6_addr *addr;
1508 {
1509 register struct ifaddr *ifa;
1510
1511 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1512 {
1513 if (ifa->ifa_addr == NULL)
1514 continue; /* just for safety */
1515 if (ifa->ifa_addr->sa_family != AF_INET6)
1516 continue;
1517 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1518 break;
1519 }
1520
1521 return((struct in6_ifaddr *)ifa);
1522 }
1523
1524 /*
1525 * Convert IP6 address to printable (loggable) representation.
1526 */
1527 static char digits[] = "0123456789abcdef";
1528 static int ip6round = 0;
1529 char *
1530 ip6_sprintf(addr)
1531 register struct in6_addr *addr;
1532 {
1533 static char ip6buf[8][48];
1534 register int i;
1535 register char *cp;
1536 register u_short *a = (u_short *)addr;
1537 register u_char *d;
1538 int dcolon = 0;
1539
1540 ip6round = (ip6round + 1) & 7;
1541 cp = ip6buf[ip6round];
1542
1543 for (i = 0; i < 8; i++) {
1544 if (dcolon == 1) {
1545 if (*a == 0) {
1546 if (i == 7)
1547 *cp++ = ':';
1548 a++;
1549 continue;
1550 } else
1551 dcolon = 2;
1552 }
1553 if (*a == 0) {
1554 if (dcolon == 0 && *(a + 1) == 0) {
1555 if (i == 0)
1556 *cp++ = ':';
1557 *cp++ = ':';
1558 dcolon = 1;
1559 } else {
1560 *cp++ = '0';
1561 *cp++ = ':';
1562 }
1563 a++;
1564 continue;
1565 }
1566 d = (u_char *)a;
1567 *cp++ = digits[*d >> 4];
1568 *cp++ = digits[*d++ & 0xf];
1569 *cp++ = digits[*d >> 4];
1570 *cp++ = digits[*d & 0xf];
1571 *cp++ = ':';
1572 a++;
1573 }
1574 *--cp = 0;
1575 return(ip6buf[ip6round]);
1576 }
1577
1578 int
1579 in6_localaddr(in6)
1580 struct in6_addr *in6;
1581 {
1582 struct in6_ifaddr *ia;
1583
1584 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1585 return 1;
1586
1587 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1588 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1589 &ia->ia_prefixmask.sin6_addr))
1590 return 1;
1591
1592 return (0);
1593 }
1594
1595 /*
1596 * Get a scope of the address. Node-local, link-local, site-local or global.
1597 */
1598 int
1599 in6_addrscope (addr)
1600 struct in6_addr *addr;
1601 {
1602 int scope;
1603
1604 if (addr->s6_addr8[0] == 0xfe) {
1605 scope = addr->s6_addr8[1] & 0xc0;
1606
1607 switch (scope) {
1608 case 0x80:
1609 return IPV6_ADDR_SCOPE_LINKLOCAL;
1610 break;
1611 case 0xc0:
1612 return IPV6_ADDR_SCOPE_SITELOCAL;
1613 break;
1614 default:
1615 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
1616 break;
1617 }
1618 }
1619
1620
1621 if (addr->s6_addr8[0] == 0xff) {
1622 scope = addr->s6_addr8[1] & 0x0f;
1623
1624 /*
1625 * due to other scope such as reserved,
1626 * return scope doesn't work.
1627 */
1628 switch (scope) {
1629 case IPV6_ADDR_SCOPE_NODELOCAL:
1630 return IPV6_ADDR_SCOPE_NODELOCAL;
1631 break;
1632 case IPV6_ADDR_SCOPE_LINKLOCAL:
1633 return IPV6_ADDR_SCOPE_LINKLOCAL;
1634 break;
1635 case IPV6_ADDR_SCOPE_SITELOCAL:
1636 return IPV6_ADDR_SCOPE_SITELOCAL;
1637 break;
1638 default:
1639 return IPV6_ADDR_SCOPE_GLOBAL;
1640 break;
1641 }
1642 }
1643
1644 if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) {
1645 if (addr->s6_addr8[15] == 1) /* loopback */
1646 return IPV6_ADDR_SCOPE_NODELOCAL;
1647 if (addr->s6_addr8[15] == 0) /* unspecified */
1648 return IPV6_ADDR_SCOPE_LINKLOCAL;
1649 }
1650
1651 return IPV6_ADDR_SCOPE_GLOBAL;
1652 }
1653
1654 /*
1655 * return length of part which dst and src are equal
1656 * hard coding...
1657 */
1658
1659 int
1660 in6_matchlen(src, dst)
1661 struct in6_addr *src, *dst;
1662 {
1663 int match = 0;
1664 u_char *s = (u_char *)src, *d = (u_char *)dst;
1665 u_char *lim = s + 16, r;
1666
1667 while (s < lim)
1668 if ((r = (*d++ ^ *s++)) != 0) {
1669 while (r < 128) {
1670 match++;
1671 r <<= 1;
1672 }
1673 break;
1674 } else
1675 match += 8;
1676 return match;
1677 }
1678
1679 int
1680 in6_are_prefix_equal(p1, p2, len)
1681 struct in6_addr *p1, *p2;
1682 int len;
1683 {
1684 int bytelen, bitlen;
1685
1686 /* sanity check */
1687 if (0 > len || len > 128) {
1688 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1689 len);
1690 return(0);
1691 }
1692
1693 bytelen = len / 8;
1694 bitlen = len % 8;
1695
1696 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1697 return(0);
1698 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1699 p2->s6_addr[bytelen] >> (8 - bitlen))
1700 return(0);
1701
1702 return(1);
1703 }
1704
1705 void
1706 in6_prefixlen2mask(maskp, len)
1707 struct in6_addr *maskp;
1708 int len;
1709 {
1710 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1711 int bytelen, bitlen, i;
1712
1713 /* sanity check */
1714 if (0 > len || len > 128) {
1715 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1716 len);
1717 return;
1718 }
1719
1720 bzero(maskp, sizeof(*maskp));
1721 bytelen = len / 8;
1722 bitlen = len % 8;
1723 for (i = 0; i < bytelen; i++)
1724 maskp->s6_addr[i] = 0xff;
1725 if (bitlen)
1726 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1727 }
1728
1729 /*
1730 * return the best address out of the same scope
1731 */
1732
1733 struct in6_ifaddr *
1734 in6_ifawithscope(ifp, dst)
1735 register struct ifnet *ifp;
1736 register struct in6_addr *dst;
1737 {
1738 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1739 struct ifaddr *ifa;
1740 struct in6_ifaddr *besta = NULL, *ia;
1741 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
1742
1743 dep[0] = dep[1] = NULL;
1744
1745 /*
1746 * We first look for addresses in the same scope.
1747 * If there is one, return it.
1748 * If two or more, return one which matches the dst longest.
1749 * If none, return one of global addresses assigned other ifs.
1750 */
1751 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1752 {
1753 if (ifa->ifa_addr->sa_family != AF_INET6)
1754 continue;
1755 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1756 continue; /* XXX: is there any case to allow anycast? */
1757 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1758 continue; /* don't use this interface */
1759 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1760 continue;
1761 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1762 if (ip6_use_deprecated)
1763 dep[0] = (struct in6_ifaddr *)ifa;
1764 continue;
1765 }
1766
1767 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1768 /*
1769 * call in6_matchlen() as few as possible
1770 */
1771 if (besta) {
1772 if (blen == -1)
1773 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1774 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1775 if (tlen > blen) {
1776 blen = tlen;
1777 besta = (struct in6_ifaddr *)ifa;
1778 }
1779 } else
1780 besta = (struct in6_ifaddr *)ifa;
1781 }
1782 }
1783 if (besta)
1784 return besta;
1785
1786 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1787 if (IPV6_ADDR_SCOPE_GLOBAL !=
1788 in6_addrscope(&(ia->ia_addr.sin6_addr)))
1789 continue;
1790 /* XXX: is there any case to allow anycast? */
1791 if ((ia->ia6_flags & IN6_IFF_ANYCAST) != 0)
1792 continue;
1793 if ((ia->ia6_flags & IN6_IFF_NOTREADY) != 0)
1794 continue;
1795 if ((ia->ia6_flags & IN6_IFF_DETACHED) != 0)
1796 continue;
1797 if ((ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
1798 if (ip6_use_deprecated)
1799 dep[1] = (struct in6_ifaddr *)ifa;
1800 continue;
1801 }
1802 return ia;
1803 }
1804
1805 /* use the last-resort values, that are, deprecated addresses */
1806 if (dep[0])
1807 return dep[0];
1808 if (dep[1])
1809 return dep[1];
1810
1811 return NULL;
1812 }
1813
1814 /*
1815 * return the best address out of the same scope. if no address was
1816 * found, return the first valid address from designated IF.
1817 */
1818
1819 struct in6_ifaddr *
1820 in6_ifawithifp(ifp, dst)
1821 register struct ifnet *ifp;
1822 register struct in6_addr *dst;
1823 {
1824 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1825 struct ifaddr *ifa;
1826 struct in6_ifaddr *besta = 0;
1827 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
1828
1829 dep[0] = dep[1] = NULL;
1830
1831 /*
1832 * We first look for addresses in the same scope.
1833 * If there is one, return it.
1834 * If two or more, return one which matches the dst longest.
1835 * If none, return one of global addresses assigned other ifs.
1836 */
1837 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1838 {
1839 if (ifa->ifa_addr->sa_family != AF_INET6)
1840 continue;
1841 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1842 continue; /* XXX: is there any case to allow anycast? */
1843 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1844 continue; /* don't use this interface */
1845 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1846 continue;
1847 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1848 if (ip6_use_deprecated)
1849 dep[0] = (struct in6_ifaddr *)ifa;
1850 continue;
1851 }
1852
1853 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1854 /*
1855 * call in6_matchlen() as few as possible
1856 */
1857 if (besta) {
1858 if (blen == -1)
1859 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1860 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1861 if (tlen > blen) {
1862 blen = tlen;
1863 besta = (struct in6_ifaddr *)ifa;
1864 }
1865 } else
1866 besta = (struct in6_ifaddr *)ifa;
1867 }
1868 }
1869 if (besta)
1870 return(besta);
1871
1872 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1873 {
1874 if (ifa->ifa_addr->sa_family != AF_INET6)
1875 continue;
1876 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1877 continue; /* XXX: is there any case to allow anycast? */
1878 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1879 continue; /* don't use this interface */
1880 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1881 continue;
1882 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1883 if (ip6_use_deprecated)
1884 dep[1] = (struct in6_ifaddr *)ifa;
1885 continue;
1886 }
1887
1888 return (struct in6_ifaddr *)ifa;
1889 }
1890
1891 /* use the last-resort values, that are, deprecated addresses */
1892 if (dep[0])
1893 return dep[0];
1894 if (dep[1])
1895 return dep[1];
1896
1897 return NULL;
1898 }
1899
1900 /*
1901 * perform DAD when interface becomes IFF_UP.
1902 */
1903 void
1904 in6_if_up(ifp)
1905 struct ifnet *ifp;
1906 {
1907 struct ifaddr *ifa;
1908 struct in6_ifaddr *ia;
1909 struct sockaddr_dl *sdl;
1910 int type;
1911 struct ether_addr ea;
1912 int off;
1913 int dad_delay; /* delay ticks before DAD output */
1914
1915 bzero(&ea, sizeof(ea));
1916 sdl = NULL;
1917
1918 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1919 {
1920 if (ifa->ifa_addr->sa_family == AF_INET6
1921 && IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
1922 goto dad;
1923 }
1924 if (ifa->ifa_addr->sa_family != AF_LINK)
1925 continue;
1926 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1927 break;
1928 }
1929
1930 switch (ifp->if_type) {
1931 case IFT_LOOP:
1932 in6_ifattach(ifp, IN6_IFT_LOOP, NULL, 1);
1933 break;
1934 case IFT_SLIP:
1935 case IFT_PPP:
1936 case IFT_GIF:
1937 case IFT_FAITH:
1938 type = IN6_IFT_P2P;
1939 in6_ifattach(ifp, type, 0, 1);
1940 break;
1941 case IFT_ETHER:
1942 case IFT_FDDI:
1943 case IFT_ATM:
1944 type = IN6_IFT_802;
1945 if (sdl == NULL)
1946 break;
1947 off = sdl->sdl_nlen;
1948 if (bcmp(&sdl->sdl_data[off], &ea, sizeof(ea)) != 0)
1949 in6_ifattach(ifp, type, LLADDR(sdl), 0);
1950 break;
1951 case IFT_ARCNET:
1952 type = IN6_IFT_ARCNET;
1953 if (sdl == NULL)
1954 break;
1955 off = sdl->sdl_nlen;
1956 if (sdl->sdl_data[off] != 0) /* XXX ?: */
1957 in6_ifattach(ifp, type, LLADDR(sdl), 0);
1958 break;
1959 default:
1960 break;
1961 }
1962
1963 dad:
1964 dad_delay = 0;
1965 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1966 {
1967 if (ifa->ifa_addr->sa_family != AF_INET6)
1968 continue;
1969 ia = (struct in6_ifaddr *)ifa;
1970 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
1971 nd6_dad_start(ifa, &dad_delay);
1972 }
1973 }
1974
1975 /*
1976 * Calculate max IPv6 MTU through all the interfaces and store it
1977 * to in6_maxmtu.
1978 */
1979 void
1980 in6_setmaxmtu()
1981 {
1982 unsigned long maxmtu = 0;
1983 struct ifnet *ifp;
1984
1985 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1986 {
1987 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
1988 nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
1989 maxmtu = nd_ifinfo[ifp->if_index].linkmtu;
1990 }
1991 if (maxmtu) /* update only when maxmtu is positive */
1992 in6_maxmtu = maxmtu;
1993 }
1994