in6.c revision 1.12 1 /* $NetBSD: in6.c,v 1.12 2000/02/02 16:58:10 itojun 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 * if the interface is going away, and this was the last IPv6
952 * address on the interface, remove route to link-local
953 * allnodes multicast address
954 */
955 if (ifp->if_output == if_nulloutput) {
956 /* was it the last IPv6 address for the interface? */
957 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
958 if (ifa->ifa_addr->sa_family == AF_INET6)
959 return;
960
961 in6_ifdetach(ifp);
962 }
963 }
964
965 /*
966 * SIOC[GAD]LIFADDR.
967 * SIOCGLIFADDR: get first address. (???)
968 * SIOCGLIFADDR with IFLR_PREFIX:
969 * get first address that matches the specified prefix.
970 * SIOCALIFADDR: add the specified address.
971 * SIOCALIFADDR with IFLR_PREFIX:
972 * add the specified prefix, filling hostid part from
973 * the first link-local address. prefixlen must be <= 64.
974 * SIOCDLIFADDR: delete the specified address.
975 * SIOCDLIFADDR with IFLR_PREFIX:
976 * delete the first address that matches the specified prefix.
977 * return values:
978 * EINVAL on invalid parameters
979 * EADDRNOTAVAIL on prefix match failed/specified address not found
980 * other values may be returned from in6_ioctl()
981 *
982 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
983 * this is to accomodate address naming scheme other than RFC2374,
984 * in the future.
985 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
986 * address encoding scheme. (see figure on page 8)
987 */
988 static int
989 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
990 struct socket *so;
991 u_long cmd;
992 caddr_t data;
993 struct ifnet *ifp;
994 struct proc *p;
995 {
996 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
997 struct ifaddr *ifa;
998 struct sockaddr *sa;
999
1000 /* sanity checks */
1001 if (!data || !ifp) {
1002 panic("invalid argument to in6_lifaddr_ioctl");
1003 /*NOTRECHED*/
1004 }
1005
1006 switch (cmd) {
1007 case SIOCGLIFADDR:
1008 /* address must be specified on GET with IFLR_PREFIX */
1009 if ((iflr->flags & IFLR_PREFIX) == 0)
1010 break;
1011 /*FALLTHROUGH*/
1012 case SIOCALIFADDR:
1013 case SIOCDLIFADDR:
1014 /* address must be specified on ADD and DELETE */
1015 sa = (struct sockaddr *)&iflr->addr;
1016 if (sa->sa_family != AF_INET6)
1017 return EINVAL;
1018 if (sa->sa_len != sizeof(struct sockaddr_in6))
1019 return EINVAL;
1020 /* XXX need improvement */
1021 sa = (struct sockaddr *)&iflr->dstaddr;
1022 if (sa->sa_family && sa->sa_family != AF_INET6)
1023 return EINVAL;
1024 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1025 return EINVAL;
1026 break;
1027 default: /*shouldn't happen*/
1028 #if 0
1029 panic("invalid cmd to in6_lifaddr_ioctl");
1030 /*NOTREACHED*/
1031 #else
1032 return EOPNOTSUPP;
1033 #endif
1034 }
1035 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1036 return EINVAL;
1037
1038 switch (cmd) {
1039 case SIOCALIFADDR:
1040 {
1041 struct in6_aliasreq ifra;
1042 struct in6_addr *hostid = NULL;
1043 int prefixlen;
1044
1045 if ((iflr->flags & IFLR_PREFIX) != 0) {
1046 struct sockaddr_in6 *sin6;
1047
1048 /*
1049 * hostid is to fill in the hostid part of the
1050 * address. hostid points to the first link-local
1051 * address attached to the interface.
1052 */
1053 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp);
1054 if (!ifa)
1055 return EADDRNOTAVAIL;
1056 hostid = IFA_IN6(ifa);
1057
1058 /* prefixlen must be <= 64. */
1059 if (64 < iflr->prefixlen)
1060 return EINVAL;
1061 prefixlen = iflr->prefixlen;
1062
1063 /* hostid part must be zero. */
1064 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1065 if (sin6->sin6_addr.s6_addr32[2] != 0
1066 || sin6->sin6_addr.s6_addr32[3] != 0) {
1067 return EINVAL;
1068 }
1069 } else
1070 prefixlen = iflr->prefixlen;
1071
1072 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1073 bzero(&ifra, sizeof(ifra));
1074 bcopy(iflr->iflr_name, ifra.ifra_name,
1075 sizeof(ifra.ifra_name));
1076
1077 bcopy(&iflr->addr, &ifra.ifra_addr,
1078 ((struct sockaddr *)&iflr->addr)->sa_len);
1079 if (hostid) {
1080 /* fill in hostid part */
1081 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1082 hostid->s6_addr32[2];
1083 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1084 hostid->s6_addr32[3];
1085 }
1086
1087 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1088 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1089 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1090 if (hostid) {
1091 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1092 hostid->s6_addr32[2];
1093 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1094 hostid->s6_addr32[3];
1095 }
1096 }
1097
1098 ifra.ifra_prefixmask.sin6_family = AF_INET6;
1099 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1100 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1101
1102 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1103 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1104 }
1105 case SIOCGLIFADDR:
1106 case SIOCDLIFADDR:
1107 {
1108 struct in6_ifaddr *ia;
1109 struct in6_addr mask, candidate, match;
1110 struct sockaddr_in6 *sin6;
1111 int cmp;
1112
1113 bzero(&mask, sizeof(mask));
1114 if (iflr->flags & IFLR_PREFIX) {
1115 /* lookup a prefix rather than address. */
1116 in6_len2mask(&mask, iflr->prefixlen);
1117
1118 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1119 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1120 match.s6_addr32[0] &= mask.s6_addr32[0];
1121 match.s6_addr32[1] &= mask.s6_addr32[1];
1122 match.s6_addr32[2] &= mask.s6_addr32[2];
1123 match.s6_addr32[3] &= mask.s6_addr32[3];
1124
1125 /* if you set extra bits, that's wrong */
1126 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1127 return EINVAL;
1128
1129 cmp = 1;
1130 } else {
1131 if (cmd == SIOCGLIFADDR) {
1132 /* on getting an address, take the 1st match */
1133 cmp = 0; /*XXX*/
1134 } else {
1135 /* on deleting an address, do exact match */
1136 in6_len2mask(&mask, 128);
1137 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1138 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1139
1140 cmp = 1;
1141 }
1142 }
1143
1144 for (ifa = ifp->if_addrlist.tqh_first;
1145 ifa;
1146 ifa = ifa->ifa_list.tqe_next)
1147 {
1148 if (ifa->ifa_addr->sa_family != AF_INET6)
1149 continue;
1150 if (!cmp)
1151 break;
1152 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1153 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1154 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1155 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1156 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1157 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1158 break;
1159 }
1160 if (!ifa)
1161 return EADDRNOTAVAIL;
1162 ia = ifa2ia6(ifa);
1163
1164 if (cmd == SIOCGLIFADDR) {
1165 /* fill in the if_laddrreq structure */
1166 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1167
1168 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1169 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1170 ia->ia_dstaddr.sin6_len);
1171 } else
1172 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1173
1174 iflr->prefixlen =
1175 in6_mask2len(&ia->ia_prefixmask.sin6_addr);
1176
1177 iflr->flags = ia->ia6_flags; /*XXX*/
1178
1179 return 0;
1180 } else {
1181 struct in6_aliasreq ifra;
1182
1183 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1184 bzero(&ifra, sizeof(ifra));
1185 bcopy(iflr->iflr_name, ifra.ifra_name,
1186 sizeof(ifra.ifra_name));
1187
1188 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1189 ia->ia_addr.sin6_len);
1190 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1191 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1192 ia->ia_dstaddr.sin6_len);
1193 }
1194 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1195 ia->ia_prefixmask.sin6_len);
1196
1197 ifra.ifra_flags = ia->ia6_flags;
1198 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1199 ifp, p);
1200 }
1201 }
1202 }
1203
1204 return EOPNOTSUPP; /*just for safety*/
1205 }
1206
1207 /*
1208 * Delete any existing route for an interface.
1209 */
1210 void
1211 in6_ifscrub(ifp, ia)
1212 register struct ifnet *ifp;
1213 register struct in6_ifaddr *ia;
1214 {
1215 if ((ia->ia_flags & IFA_ROUTE) == 0)
1216 return;
1217 if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
1218 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
1219 else
1220 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
1221 ia->ia_flags &= ~IFA_ROUTE;
1222
1223 /* Remove ownaddr's loopback rtentry, if it exists. */
1224 in6_ifremloop(&(ia->ia_ifa));
1225 }
1226
1227 /*
1228 * Initialize an interface's intetnet6 address
1229 * and routing table entry.
1230 */
1231 int
1232 in6_ifinit(ifp, ia, sin6, scrub)
1233 struct ifnet *ifp;
1234 struct in6_ifaddr *ia;
1235 struct sockaddr_in6 *sin6;
1236 int scrub;
1237 {
1238 struct sockaddr_in6 oldaddr;
1239 int error, flags = RTF_UP;
1240 int s = splimp();
1241
1242 oldaddr = ia->ia_addr;
1243 ia->ia_addr = *sin6;
1244 /*
1245 * Give the interface a chance to initialize
1246 * if this is its first address,
1247 * and to validate the address if necessary.
1248 */
1249 if (ifp->if_ioctl &&
1250 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1251 splx(s);
1252 ia->ia_addr = oldaddr;
1253 return(error);
1254 }
1255
1256 switch (ifp->if_type) {
1257 case IFT_ARCNET:
1258 case IFT_ETHER:
1259 case IFT_FDDI:
1260 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1261 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1262 break;
1263 case IFT_PPP:
1264 ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest;
1265 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1266 break;
1267 }
1268
1269 splx(s);
1270 if (scrub) {
1271 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1272 in6_ifscrub(ifp, ia);
1273 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1274 }
1275 /* xxx
1276 * in_socktrim
1277 */
1278 /*
1279 * Add route for the network.
1280 */
1281 ia->ia_ifa.ifa_metric = ifp->if_metric;
1282 if (ifp->if_flags & IFF_LOOPBACK) {
1283 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
1284 flags |= RTF_HOST;
1285 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1286 if (ia->ia_dstaddr.sin6_family != AF_INET6)
1287 return(0);
1288 flags |= RTF_HOST;
1289 }
1290 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
1291 ia->ia_flags |= IFA_ROUTE;
1292
1293 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
1294 in6_ifaddloop(&(ia->ia_ifa));
1295
1296 if (ifp->if_flags & IFF_MULTICAST)
1297 in6_restoremkludge(ia, ifp);
1298
1299 return(error);
1300 }
1301
1302 /*
1303 * Multicast address kludge:
1304 * If there were any multicast addresses attached to this interface address,
1305 * either move them to another address on this interface, or save them until
1306 * such time as this interface is reconfigured for IPv6.
1307 */
1308 void
1309 in6_savemkludge(oia)
1310 struct in6_ifaddr *oia;
1311 {
1312 struct in6_ifaddr *ia;
1313 struct in6_multi *in6m, *next;
1314
1315 IFP_TO_IA6(oia->ia_ifp, ia);
1316 if (ia) { /* there is another address */
1317 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1318 next = in6m->in6m_entry.le_next;
1319 IFAFREE(&in6m->in6m_ia->ia_ifa);
1320 IFAREF(&ia->ia_ifa);
1321 in6m->in6m_ia = ia;
1322 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1323 }
1324 } else { /* last address on this if deleted, save */
1325 struct multi6_kludge *mk;
1326
1327 mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
1328
1329 LIST_INIT(&mk->mk_head);
1330 mk->mk_ifp = oia->ia_ifp;
1331
1332 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1333 next = in6m->in6m_entry.le_next;
1334 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1335 in6m->in6m_ia = NULL;
1336 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
1337 }
1338
1339 if (mk->mk_head.lh_first != NULL) {
1340 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
1341 }
1342 else {
1343 FREE(mk, M_IPMADDR);
1344 }
1345 }
1346 }
1347
1348 /*
1349 * Continuation of multicast address hack:
1350 * If there was a multicast group list previously saved for this interface,
1351 * then we re-attach it to the first address configured on the i/f.
1352 */
1353 void
1354 in6_restoremkludge(ia, ifp)
1355 struct in6_ifaddr *ia;
1356 struct ifnet *ifp;
1357 {
1358 struct multi6_kludge *mk;
1359
1360 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1361 if (mk->mk_ifp == ifp) {
1362 struct in6_multi *in6m, *next;
1363
1364 for (in6m = mk->mk_head.lh_first; in6m; in6m = next){
1365 next = in6m->in6m_entry.le_next;
1366 in6m->in6m_ia = ia;
1367 IFAREF(&ia->ia_ifa); /* gain a reference */
1368 LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
1369 in6m, in6m_entry);
1370 }
1371 LIST_REMOVE(mk, mk_entry);
1372 free(mk, M_IPMADDR);
1373 break;
1374 }
1375 }
1376 }
1377
1378 void
1379 in6_purgemkludge(ifp)
1380 struct ifnet *ifp;
1381 {
1382 struct multi6_kludge *mk;
1383 struct in6_multi *in6m, *next;
1384
1385 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1386 if (mk->mk_ifp != ifp)
1387 continue;
1388
1389 for (in6m = mk->mk_head.lh_first; in6m; in6m = next) {
1390 next = in6m->in6m_entry.le_next;
1391 LIST_REMOVE(in6m, in6m_entry);
1392 in6_delmulti(in6m);
1393 in6m = NULL;
1394 }
1395 LIST_REMOVE(mk, mk_entry);
1396 free(mk, M_IPMADDR);
1397 break;
1398 }
1399 }
1400
1401 /*
1402 * Add an address to the list of IP6 multicast addresses for a
1403 * given interface.
1404 */
1405 struct in6_multi *
1406 in6_addmulti(maddr6, ifp, errorp)
1407 register struct in6_addr *maddr6;
1408 register struct ifnet *ifp;
1409 int *errorp;
1410 {
1411 struct in6_ifaddr *ia;
1412 struct in6_ifreq ifr;
1413 struct in6_multi *in6m;
1414 int s = splsoftnet();
1415
1416 *errorp = 0;
1417 /*
1418 * See if address already in list.
1419 */
1420 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
1421 if (in6m != NULL) {
1422 /*
1423 * Found it; just increment the refrence count.
1424 */
1425 in6m->in6m_refcount++;
1426 } else {
1427 /*
1428 * New address; allocate a new multicast record
1429 * and link it into the interface's multicast list.
1430 */
1431 in6m = (struct in6_multi *)
1432 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1433 if (in6m == NULL) {
1434 splx(s);
1435 *errorp = ENOBUFS;
1436 return(NULL);
1437 }
1438 in6m->in6m_addr = *maddr6;
1439 in6m->in6m_ifp = ifp;
1440 in6m->in6m_refcount = 1;
1441 IFP_TO_IA6(ifp, ia);
1442 if (ia == NULL) {
1443 free(in6m, M_IPMADDR);
1444 splx(s);
1445 *errorp = EADDRNOTAVAIL; /* appropriate? */
1446 return(NULL);
1447 }
1448 in6m->in6m_ia = ia;
1449 IFAREF(&ia->ia_ifa); /* gain a reference */
1450 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1451
1452 /*
1453 * Ask the network driver to update its multicast reception
1454 * filter appropriately for the new address.
1455 */
1456 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1457 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1458 ifr.ifr_addr.sin6_family = AF_INET6;
1459 ifr.ifr_addr.sin6_addr = *maddr6;
1460 if (ifp->if_ioctl == NULL)
1461 *errorp = ENXIO; /* XXX: appropriate? */
1462 else
1463 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
1464 (caddr_t)&ifr);
1465 if (*errorp) {
1466 LIST_REMOVE(in6m, in6m_entry);
1467 free(in6m, M_IPMADDR);
1468 splx(s);
1469 return(NULL);
1470 }
1471 /*
1472 * Let MLD6 know that we have joined a new IP6 multicast
1473 * group.
1474 */
1475 mld6_start_listening(in6m);
1476 }
1477 splx(s);
1478 return(in6m);
1479 }
1480
1481 /*
1482 * Delete a multicast address record.
1483 */
1484 void
1485 in6_delmulti(in6m)
1486 struct in6_multi *in6m;
1487 {
1488 struct in6_ifreq ifr;
1489 int s = splsoftnet();
1490
1491 if (--in6m->in6m_refcount == 0) {
1492 /*
1493 * No remaining claims to this record; let MLD6 know
1494 * that we are leaving the multicast group.
1495 */
1496 mld6_stop_listening(in6m);
1497
1498 /*
1499 * Unlink from list.
1500 */
1501 LIST_REMOVE(in6m, in6m_entry);
1502 if (in6m->in6m_ia)
1503 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1504
1505 /*
1506 * Notify the network driver to update its multicast
1507 * reception filter.
1508 */
1509 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1510 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1511 ifr.ifr_addr.sin6_family = AF_INET6;
1512 ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
1513 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
1514 SIOCDELMULTI, (caddr_t)&ifr);
1515 free(in6m, M_IPMADDR);
1516 }
1517 splx(s);
1518 }
1519
1520 /*
1521 * Find an IPv6 interface link-local address specific to an interface.
1522 */
1523 struct in6_ifaddr *
1524 in6ifa_ifpforlinklocal(ifp)
1525 struct ifnet *ifp;
1526 {
1527 register struct ifaddr *ifa;
1528
1529 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1530 {
1531 if (ifa->ifa_addr == NULL)
1532 continue; /* just for safety */
1533 if (ifa->ifa_addr->sa_family != AF_INET6)
1534 continue;
1535 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)))
1536 break;
1537 }
1538
1539 return((struct in6_ifaddr *)ifa);
1540 }
1541
1542
1543 /*
1544 * find the internet address corresponding to a given interface and address.
1545 */
1546 struct in6_ifaddr *
1547 in6ifa_ifpwithaddr(ifp, addr)
1548 struct ifnet *ifp;
1549 struct in6_addr *addr;
1550 {
1551 register struct ifaddr *ifa;
1552
1553 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1554 {
1555 if (ifa->ifa_addr == NULL)
1556 continue; /* just for safety */
1557 if (ifa->ifa_addr->sa_family != AF_INET6)
1558 continue;
1559 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1560 break;
1561 }
1562
1563 return((struct in6_ifaddr *)ifa);
1564 }
1565
1566 /*
1567 * Convert IP6 address to printable (loggable) representation.
1568 */
1569 static char digits[] = "0123456789abcdef";
1570 static int ip6round = 0;
1571 char *
1572 ip6_sprintf(addr)
1573 register struct in6_addr *addr;
1574 {
1575 static char ip6buf[8][48];
1576 register int i;
1577 register char *cp;
1578 register u_short *a = (u_short *)addr;
1579 register u_char *d;
1580 int dcolon = 0;
1581
1582 ip6round = (ip6round + 1) & 7;
1583 cp = ip6buf[ip6round];
1584
1585 for (i = 0; i < 8; i++) {
1586 if (dcolon == 1) {
1587 if (*a == 0) {
1588 if (i == 7)
1589 *cp++ = ':';
1590 a++;
1591 continue;
1592 } else
1593 dcolon = 2;
1594 }
1595 if (*a == 0) {
1596 if (dcolon == 0 && *(a + 1) == 0) {
1597 if (i == 0)
1598 *cp++ = ':';
1599 *cp++ = ':';
1600 dcolon = 1;
1601 } else {
1602 *cp++ = '0';
1603 *cp++ = ':';
1604 }
1605 a++;
1606 continue;
1607 }
1608 d = (u_char *)a;
1609 *cp++ = digits[*d >> 4];
1610 *cp++ = digits[*d++ & 0xf];
1611 *cp++ = digits[*d >> 4];
1612 *cp++ = digits[*d & 0xf];
1613 *cp++ = ':';
1614 a++;
1615 }
1616 *--cp = 0;
1617 return(ip6buf[ip6round]);
1618 }
1619
1620 int
1621 in6_localaddr(in6)
1622 struct in6_addr *in6;
1623 {
1624 struct in6_ifaddr *ia;
1625
1626 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1627 return 1;
1628
1629 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1630 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1631 &ia->ia_prefixmask.sin6_addr))
1632 return 1;
1633
1634 return (0);
1635 }
1636
1637 /*
1638 * Get a scope of the address. Node-local, link-local, site-local or global.
1639 */
1640 int
1641 in6_addrscope (addr)
1642 struct in6_addr *addr;
1643 {
1644 int scope;
1645
1646 if (addr->s6_addr8[0] == 0xfe) {
1647 scope = addr->s6_addr8[1] & 0xc0;
1648
1649 switch (scope) {
1650 case 0x80:
1651 return IPV6_ADDR_SCOPE_LINKLOCAL;
1652 break;
1653 case 0xc0:
1654 return IPV6_ADDR_SCOPE_SITELOCAL;
1655 break;
1656 default:
1657 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
1658 break;
1659 }
1660 }
1661
1662
1663 if (addr->s6_addr8[0] == 0xff) {
1664 scope = addr->s6_addr8[1] & 0x0f;
1665
1666 /*
1667 * due to other scope such as reserved,
1668 * return scope doesn't work.
1669 */
1670 switch (scope) {
1671 case IPV6_ADDR_SCOPE_NODELOCAL:
1672 return IPV6_ADDR_SCOPE_NODELOCAL;
1673 break;
1674 case IPV6_ADDR_SCOPE_LINKLOCAL:
1675 return IPV6_ADDR_SCOPE_LINKLOCAL;
1676 break;
1677 case IPV6_ADDR_SCOPE_SITELOCAL:
1678 return IPV6_ADDR_SCOPE_SITELOCAL;
1679 break;
1680 default:
1681 return IPV6_ADDR_SCOPE_GLOBAL;
1682 break;
1683 }
1684 }
1685
1686 if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) {
1687 if (addr->s6_addr8[15] == 1) /* loopback */
1688 return IPV6_ADDR_SCOPE_NODELOCAL;
1689 if (addr->s6_addr8[15] == 0) /* unspecified */
1690 return IPV6_ADDR_SCOPE_LINKLOCAL;
1691 }
1692
1693 return IPV6_ADDR_SCOPE_GLOBAL;
1694 }
1695
1696 /*
1697 * return length of part which dst and src are equal
1698 * hard coding...
1699 */
1700
1701 int
1702 in6_matchlen(src, dst)
1703 struct in6_addr *src, *dst;
1704 {
1705 int match = 0;
1706 u_char *s = (u_char *)src, *d = (u_char *)dst;
1707 u_char *lim = s + 16, r;
1708
1709 while (s < lim)
1710 if ((r = (*d++ ^ *s++)) != 0) {
1711 while (r < 128) {
1712 match++;
1713 r <<= 1;
1714 }
1715 break;
1716 } else
1717 match += 8;
1718 return match;
1719 }
1720
1721 int
1722 in6_are_prefix_equal(p1, p2, len)
1723 struct in6_addr *p1, *p2;
1724 int len;
1725 {
1726 int bytelen, bitlen;
1727
1728 /* sanity check */
1729 if (0 > len || len > 128) {
1730 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1731 len);
1732 return(0);
1733 }
1734
1735 bytelen = len / 8;
1736 bitlen = len % 8;
1737
1738 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1739 return(0);
1740 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1741 p2->s6_addr[bytelen] >> (8 - bitlen))
1742 return(0);
1743
1744 return(1);
1745 }
1746
1747 void
1748 in6_prefixlen2mask(maskp, len)
1749 struct in6_addr *maskp;
1750 int len;
1751 {
1752 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1753 int bytelen, bitlen, i;
1754
1755 /* sanity check */
1756 if (0 > len || len > 128) {
1757 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1758 len);
1759 return;
1760 }
1761
1762 bzero(maskp, sizeof(*maskp));
1763 bytelen = len / 8;
1764 bitlen = len % 8;
1765 for (i = 0; i < bytelen; i++)
1766 maskp->s6_addr[i] = 0xff;
1767 if (bitlen)
1768 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1769 }
1770
1771 /*
1772 * return the best address out of the same scope
1773 */
1774
1775 struct in6_ifaddr *
1776 in6_ifawithscope(ifp, dst)
1777 register struct ifnet *ifp;
1778 register struct in6_addr *dst;
1779 {
1780 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1781 struct ifaddr *ifa;
1782 struct in6_ifaddr *besta = NULL, *ia;
1783 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
1784
1785 dep[0] = dep[1] = NULL;
1786
1787 /*
1788 * We first look for addresses in the same scope.
1789 * If there is one, return it.
1790 * If two or more, return one which matches the dst longest.
1791 * If none, return one of global addresses assigned other ifs.
1792 */
1793 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1794 {
1795 if (ifa->ifa_addr->sa_family != AF_INET6)
1796 continue;
1797 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1798 continue; /* XXX: is there any case to allow anycast? */
1799 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1800 continue; /* don't use this interface */
1801 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1802 continue;
1803 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1804 if (ip6_use_deprecated)
1805 dep[0] = (struct in6_ifaddr *)ifa;
1806 continue;
1807 }
1808
1809 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1810 /*
1811 * call in6_matchlen() as few as possible
1812 */
1813 if (besta) {
1814 if (blen == -1)
1815 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1816 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1817 if (tlen > blen) {
1818 blen = tlen;
1819 besta = (struct in6_ifaddr *)ifa;
1820 }
1821 } else
1822 besta = (struct in6_ifaddr *)ifa;
1823 }
1824 }
1825 if (besta)
1826 return besta;
1827
1828 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1829 if (IPV6_ADDR_SCOPE_GLOBAL !=
1830 in6_addrscope(&(ia->ia_addr.sin6_addr)))
1831 continue;
1832 /* XXX: is there any case to allow anycast? */
1833 if ((ia->ia6_flags & IN6_IFF_ANYCAST) != 0)
1834 continue;
1835 if ((ia->ia6_flags & IN6_IFF_NOTREADY) != 0)
1836 continue;
1837 if ((ia->ia6_flags & IN6_IFF_DETACHED) != 0)
1838 continue;
1839 if ((ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
1840 if (ip6_use_deprecated)
1841 dep[1] = (struct in6_ifaddr *)ifa;
1842 continue;
1843 }
1844 return ia;
1845 }
1846
1847 /* use the last-resort values, that are, deprecated addresses */
1848 if (dep[0])
1849 return dep[0];
1850 if (dep[1])
1851 return dep[1];
1852
1853 return NULL;
1854 }
1855
1856 /*
1857 * return the best address out of the same scope. if no address was
1858 * found, return the first valid address from designated IF.
1859 */
1860
1861 struct in6_ifaddr *
1862 in6_ifawithifp(ifp, dst)
1863 register struct ifnet *ifp;
1864 register struct in6_addr *dst;
1865 {
1866 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1867 struct ifaddr *ifa;
1868 struct in6_ifaddr *besta = 0;
1869 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
1870
1871 dep[0] = dep[1] = NULL;
1872
1873 /*
1874 * We first look for addresses in the same scope.
1875 * If there is one, return it.
1876 * If two or more, return one which matches the dst longest.
1877 * If none, return one of global addresses assigned other ifs.
1878 */
1879 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1880 {
1881 if (ifa->ifa_addr->sa_family != AF_INET6)
1882 continue;
1883 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1884 continue; /* XXX: is there any case to allow anycast? */
1885 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1886 continue; /* don't use this interface */
1887 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1888 continue;
1889 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1890 if (ip6_use_deprecated)
1891 dep[0] = (struct in6_ifaddr *)ifa;
1892 continue;
1893 }
1894
1895 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1896 /*
1897 * call in6_matchlen() as few as possible
1898 */
1899 if (besta) {
1900 if (blen == -1)
1901 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1902 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1903 if (tlen > blen) {
1904 blen = tlen;
1905 besta = (struct in6_ifaddr *)ifa;
1906 }
1907 } else
1908 besta = (struct in6_ifaddr *)ifa;
1909 }
1910 }
1911 if (besta)
1912 return(besta);
1913
1914 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1915 {
1916 if (ifa->ifa_addr->sa_family != AF_INET6)
1917 continue;
1918 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1919 continue; /* XXX: is there any case to allow anycast? */
1920 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1921 continue; /* don't use this interface */
1922 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1923 continue;
1924 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1925 if (ip6_use_deprecated)
1926 dep[1] = (struct in6_ifaddr *)ifa;
1927 continue;
1928 }
1929
1930 return (struct in6_ifaddr *)ifa;
1931 }
1932
1933 /* use the last-resort values, that are, deprecated addresses */
1934 if (dep[0])
1935 return dep[0];
1936 if (dep[1])
1937 return dep[1];
1938
1939 return NULL;
1940 }
1941
1942 /*
1943 * perform DAD when interface becomes IFF_UP.
1944 */
1945 void
1946 in6_if_up(ifp)
1947 struct ifnet *ifp;
1948 {
1949 struct ifaddr *ifa;
1950 struct in6_ifaddr *ia;
1951 struct sockaddr_dl *sdl;
1952 int type;
1953 struct ether_addr ea;
1954 int off;
1955 int dad_delay; /* delay ticks before DAD output */
1956
1957 bzero(&ea, sizeof(ea));
1958 sdl = NULL;
1959
1960 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1961 {
1962 if (ifa->ifa_addr->sa_family == AF_INET6
1963 && IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
1964 goto dad;
1965 }
1966 if (ifa->ifa_addr->sa_family != AF_LINK)
1967 continue;
1968 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1969 break;
1970 }
1971
1972 switch (ifp->if_type) {
1973 case IFT_LOOP:
1974 in6_ifattach(ifp, IN6_IFT_LOOP, NULL, 1);
1975 break;
1976 case IFT_SLIP:
1977 case IFT_PPP:
1978 case IFT_GIF:
1979 case IFT_FAITH:
1980 type = IN6_IFT_P2P;
1981 in6_ifattach(ifp, type, 0, 1);
1982 break;
1983 case IFT_ETHER:
1984 case IFT_FDDI:
1985 case IFT_ATM:
1986 type = IN6_IFT_802;
1987 if (sdl == NULL)
1988 break;
1989 off = sdl->sdl_nlen;
1990 if (bcmp(&sdl->sdl_data[off], &ea, sizeof(ea)) != 0)
1991 in6_ifattach(ifp, type, LLADDR(sdl), 0);
1992 break;
1993 case IFT_ARCNET:
1994 type = IN6_IFT_ARCNET;
1995 if (sdl == NULL)
1996 break;
1997 off = sdl->sdl_nlen;
1998 if (sdl->sdl_data[off] != 0) /* XXX ?: */
1999 in6_ifattach(ifp, type, LLADDR(sdl), 0);
2000 break;
2001 default:
2002 break;
2003 }
2004
2005 dad:
2006 dad_delay = 0;
2007 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
2008 {
2009 if (ifa->ifa_addr->sa_family != AF_INET6)
2010 continue;
2011 ia = (struct in6_ifaddr *)ifa;
2012 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2013 nd6_dad_start(ifa, &dad_delay);
2014 }
2015 }
2016
2017 /*
2018 * Calculate max IPv6 MTU through all the interfaces and store it
2019 * to in6_maxmtu.
2020 */
2021 void
2022 in6_setmaxmtu()
2023 {
2024 unsigned long maxmtu = 0;
2025 struct ifnet *ifp;
2026
2027 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2028 {
2029 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2030 nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
2031 maxmtu = nd_ifinfo[ifp->if_index].linkmtu;
2032 }
2033 if (maxmtu) /* update only when maxmtu is positive */
2034 in6_maxmtu = maxmtu;
2035 }
2036