in6.c revision 1.18 1 /* $NetBSD: in6.c,v 1.18 2000/02/24 12:43:49 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 <netinet/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) (&(((ia6)->ia_ifa))
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 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
524
525 switch (cmd) {
526
527 case SIOCDIFADDR_IN6:
528 if (ia == 0)
529 return(EADDRNOTAVAIL);
530 /* FALLTHROUGH */
531 case SIOCAIFADDR_IN6:
532 case SIOCSIFADDR_IN6:
533 case SIOCSIFNETMASK_IN6:
534 case SIOCSIFDSTADDR_IN6:
535 if (!privileged)
536 return(EPERM);
537 if (ia == 0) {
538 ia = (struct in6_ifaddr *)
539 malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
540 if (ia == NULL)
541 return (ENOBUFS);
542 bzero((caddr_t)ia, sizeof(*ia));
543 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
544 ia->ia_ifa.ifa_dstaddr
545 = (struct sockaddr *)&ia->ia_dstaddr;
546 ia->ia_ifa.ifa_netmask
547 = (struct sockaddr *)&ia->ia_prefixmask;
548
549 ia->ia_ifp = ifp;
550 if ((oia = in6_ifaddr) != NULL) {
551 for ( ; oia->ia_next; oia = oia->ia_next)
552 continue;
553 oia->ia_next = ia;
554 } else
555 in6_ifaddr = ia;
556 IFAREF(&ia->ia_ifa);
557
558 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
559 ifa_list);
560 IFAREF(&ia->ia_ifa);
561
562 if ((ifp->if_flags & IFF_LOOPBACK) == 0)
563 in6_interfaces++; /*XXX*/
564 }
565
566 if (cmd == SIOCAIFADDR_IN6) {
567 /* sanity for overflow - beware unsigned */
568 struct in6_addrlifetime *lt;
569 lt = &ifra->ifra_lifetime;
570 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
571 && lt->ia6t_vltime + time_second < time_second) {
572 return EINVAL;
573 }
574 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
575 && lt->ia6t_pltime + time_second < time_second) {
576 return EINVAL;
577 }
578 }
579 break;
580
581 case SIOCGIFADDR_IN6:
582 /* This interface is basically deprecated. use SIOCGIFCONF. */
583 /* fall through */
584 case SIOCGIFAFLAG_IN6:
585 case SIOCGIFNETMASK_IN6:
586 case SIOCGIFDSTADDR_IN6:
587 case SIOCGIFALIFETIME_IN6:
588 /* must think again about its semantics */
589 if (ia == 0)
590 return(EADDRNOTAVAIL);
591 break;
592 case SIOCSIFALIFETIME_IN6:
593 {
594 struct in6_addrlifetime *lt;
595
596 if (!privileged)
597 return(EPERM);
598 if (ia == 0)
599 return(EADDRNOTAVAIL);
600 /* sanity for overflow - beware unsigned */
601 lt = &ifr->ifr_ifru.ifru_lifetime;
602 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
603 && lt->ia6t_vltime + time_second < time_second) {
604 return EINVAL;
605 }
606 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
607 && lt->ia6t_pltime + time_second < time_second) {
608 return EINVAL;
609 }
610 break;
611 }
612 }
613
614 switch (cmd) {
615
616 case SIOCGIFADDR_IN6:
617 ifr->ifr_addr = ia->ia_addr;
618 break;
619
620 case SIOCGIFDSTADDR_IN6:
621 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
622 return(EINVAL);
623 ifr->ifr_dstaddr = ia->ia_dstaddr;
624 break;
625
626 case SIOCGIFNETMASK_IN6:
627 ifr->ifr_addr = ia->ia_prefixmask;
628 break;
629
630 case SIOCGIFAFLAG_IN6:
631 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
632 break;
633
634 case SIOCGIFSTAT_IN6:
635 if (ifp == NULL)
636 return EINVAL;
637 if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax
638 || in6_ifstat[ifp->if_index] == NULL) {
639 /* return EAFNOSUPPORT? */
640 bzero(&ifr->ifr_ifru.ifru_stat,
641 sizeof(ifr->ifr_ifru.ifru_stat));
642 } else
643 ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index];
644 break;
645
646 case SIOCGIFSTAT_ICMP6:
647 if (ifp == NULL)
648 return EINVAL;
649 if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax ||
650 icmp6_ifstat[ifp->if_index] == NULL) {
651 /* return EAFNOSUPPORT? */
652 bzero(&ifr->ifr_ifru.ifru_stat,
653 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
654 } else
655 ifr->ifr_ifru.ifru_icmp6stat =
656 *icmp6_ifstat[ifp->if_index];
657 break;
658
659 case SIOCSIFDSTADDR_IN6:
660 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
661 return(EINVAL);
662 oldaddr = ia->ia_dstaddr;
663 ia->ia_dstaddr = ifr->ifr_dstaddr;
664
665 /* link-local index check */
666 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
667 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
668 /* interface ID is not embedded by the user */
669 ia->ia_dstaddr.sin6_addr.s6_addr16[1]
670 = htons(ifp->if_index);
671 }
672 else
673 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
674 htons(ifp->if_index)) {
675 ia->ia_dstaddr = oldaddr;
676 return(EINVAL); /* ifid is contradict */
677 }
678 }
679
680 if (ifp->if_ioctl && (error = (ifp->if_ioctl)
681 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
682 ia->ia_dstaddr = oldaddr;
683 return(error);
684 }
685 if (ia->ia_flags & IFA_ROUTE) {
686 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
687 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
688 ia->ia_ifa.ifa_dstaddr =
689 (struct sockaddr *)&ia->ia_dstaddr;
690 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
691 }
692 break;
693
694 case SIOCGIFALIFETIME_IN6:
695 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
696 break;
697
698 case SIOCSIFALIFETIME_IN6:
699 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
700 /* for sanity */
701 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
702 ia->ia6_lifetime.ia6t_expire =
703 time_second + ia->ia6_lifetime.ia6t_vltime;
704 } else
705 ia->ia6_lifetime.ia6t_expire = 0;
706 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
707 ia->ia6_lifetime.ia6t_preferred =
708 time_second + ia->ia6_lifetime.ia6t_pltime;
709 } else
710 ia->ia6_lifetime.ia6t_preferred = 0;
711 break;
712
713 case SIOCSIFADDR_IN6:
714 return(in6_ifinit(ifp, ia, &ifr->ifr_addr, 1));
715
716 case SIOCSIFNETMASK_IN6:
717 ia->ia_prefixmask = ifr->ifr_addr;
718 bzero(&net, sizeof(net));
719 net.sin6_len = sizeof(struct sockaddr_in6);
720 net.sin6_family = AF_INET6;
721 net.sin6_port = htons(0);
722 net.sin6_flowinfo = htonl(0);
723 net.sin6_addr.s6_addr32[0]
724 = ia->ia_addr.sin6_addr.s6_addr32[0] &
725 ia->ia_prefixmask.sin6_addr.s6_addr32[0];
726 net.sin6_addr.s6_addr32[1]
727 = ia->ia_addr.sin6_addr.s6_addr32[1] &
728 ia->ia_prefixmask.sin6_addr.s6_addr32[1];
729 net.sin6_addr.s6_addr32[2]
730 = ia->ia_addr.sin6_addr.s6_addr32[2] &
731 ia->ia_prefixmask.sin6_addr.s6_addr32[2];
732 net.sin6_addr.s6_addr32[3]
733 = ia->ia_addr.sin6_addr.s6_addr32[3] &
734 ia->ia_prefixmask.sin6_addr.s6_addr32[3];
735 ia->ia_net = net;
736 break;
737
738 case SIOCAIFADDR_IN6:
739 prefixIsNew = 0;
740 hostIsNew = 1;
741
742 if (ifra->ifra_addr.sin6_len == 0) {
743 ifra->ifra_addr = ia->ia_addr;
744 hostIsNew = 0;
745 } else if (IN6_ARE_ADDR_EQUAL(&ifra->ifra_addr.sin6_addr,
746 &ia->ia_addr.sin6_addr))
747 hostIsNew = 0;
748
749 if (ifra->ifra_prefixmask.sin6_len) {
750 in6_ifscrub(ifp, ia);
751 ia->ia_prefixmask = ifra->ifra_prefixmask;
752 prefixIsNew = 1;
753 }
754 if ((ifp->if_flags & IFF_POINTOPOINT) &&
755 (ifra->ifra_dstaddr.sin6_family == AF_INET6)) {
756 in6_ifscrub(ifp, ia);
757 oldaddr = ia->ia_dstaddr;
758 ia->ia_dstaddr = ifra->ifra_dstaddr;
759 /* link-local index check: should be a separate function? */
760 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
761 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
762 /*
763 * interface ID is not embedded by
764 * the user
765 */
766 ia->ia_dstaddr.sin6_addr.s6_addr16[1]
767 = htons(ifp->if_index);
768 }
769 else
770 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
771 htons(ifp->if_index)) {
772 ia->ia_dstaddr = oldaddr;
773 return(EINVAL); /* ifid is contradict */
774 }
775 }
776 prefixIsNew = 1; /* We lie; but effect's the same */
777 }
778 if (ifra->ifra_addr.sin6_family == AF_INET6 &&
779 (hostIsNew || prefixIsNew))
780 error = in6_ifinit(ifp, ia, &ifra->ifra_addr, 0);
781 if (ifra->ifra_addr.sin6_family == AF_INET6
782 && hostIsNew && (ifp->if_flags & IFF_MULTICAST)) {
783 int error_local = 0;
784
785 /*
786 * join solicited multicast addr for new host id
787 */
788 struct in6_addr llsol;
789 bzero(&llsol, sizeof(struct in6_addr));
790 llsol.s6_addr16[0] = htons(0xff02);
791 llsol.s6_addr16[1] = htons(ifp->if_index);
792 llsol.s6_addr32[1] = 0;
793 llsol.s6_addr32[2] = htonl(1);
794 llsol.s6_addr32[3] =
795 ifra->ifra_addr.sin6_addr.s6_addr32[3];
796 llsol.s6_addr8[12] = 0xff;
797 (void)in6_addmulti(&llsol, ifp, &error_local);
798 if (error == 0)
799 error = error_local;
800 }
801 /* Join dstaddr's solicited multicast if necessary. */
802 if (nd6_proxyall && hostIsNew) {
803 int error_local;
804
805 error_local = in6_ifaddproxy(ia);
806 if (error == 0)
807 error = error_local;
808 }
809
810 ia->ia6_flags = ifra->ifra_flags;
811 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
812
813 ia->ia6_lifetime = ifra->ifra_lifetime;
814 /* for sanity */
815 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
816 ia->ia6_lifetime.ia6t_expire =
817 time_second + ia->ia6_lifetime.ia6t_vltime;
818 } else
819 ia->ia6_lifetime.ia6t_expire = 0;
820 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
821 ia->ia6_lifetime.ia6t_preferred =
822 time_second + ia->ia6_lifetime.ia6t_pltime;
823 } else
824 ia->ia6_lifetime.ia6t_preferred = 0;
825
826 /*
827 * Perform DAD, if needed.
828 * XXX It may be of use, if we can administratively
829 * disable DAD.
830 */
831 switch (ifp->if_type) {
832 case IFT_ARCNET:
833 case IFT_ETHER:
834 case IFT_FDDI:
835 #if 0
836 case IFT_ATM:
837 case IFT_SLIP:
838 case IFT_PPP:
839 #endif
840 ia->ia6_flags |= IN6_IFF_TENTATIVE;
841 nd6_dad_start(&ia->ia_ifa, NULL);
842 break;
843 case IFT_FAITH:
844 case IFT_GIF:
845 case IFT_LOOP:
846 default:
847 break;
848 }
849
850 if (hostIsNew) {
851 int iilen;
852 int error_local = 0;
853
854 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) -
855 in6_mask2len(&ia->ia_prefixmask.sin6_addr);
856 error_local = in6_prefix_add_ifid(iilen, ia);
857 if (error == 0)
858 error = error_local;
859 }
860
861 return(error);
862
863 case SIOCDIFADDR_IN6:
864 in6_purgeaddr(&ia->ia_ifa, ifp);
865 break;
866
867 default:
868 if (ifp == 0 || ifp->if_ioctl == 0)
869 return(EOPNOTSUPP);
870 return((*ifp->if_ioctl)(ifp, cmd, data));
871 }
872 return(0);
873 }
874
875 void
876 in6_purgeaddr(ifa, ifp)
877 struct ifaddr *ifa;
878 struct ifnet *ifp;
879 {
880 struct in6_ifaddr *oia, *ia = (void *) ifa;
881
882 in6_ifscrub(ifp, ia);
883
884 if (ifp->if_flags & IFF_MULTICAST) {
885 /*
886 * delete solicited multicast addr for deleting host id
887 */
888 struct in6_multi *in6m;
889 struct in6_addr llsol;
890 bzero(&llsol, sizeof(struct in6_addr));
891 llsol.s6_addr16[0] = htons(0xff02);
892 llsol.s6_addr16[1] = htons(ifp->if_index);
893 llsol.s6_addr32[1] = 0;
894 llsol.s6_addr32[2] = htonl(1);
895 llsol.s6_addr32[3] =
896 ia->ia_addr.sin6_addr.s6_addr32[3];
897 llsol.s6_addr8[12] = 0xff;
898
899 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
900 if (in6m)
901 in6_delmulti(in6m);
902 }
903 /* Leave dstaddr's solicited multicast if necessary. */
904 if (nd6_proxyall)
905 in6_ifremproxy(ia);
906
907 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
908 IFAFREE(&ia->ia_ifa);
909
910 oia = ia;
911 if (oia == (ia = in6_ifaddr))
912 in6_ifaddr = ia->ia_next;
913 else {
914 while (ia->ia_next && (ia->ia_next != oia))
915 ia = ia->ia_next;
916 if (ia->ia_next)
917 ia->ia_next = oia->ia_next;
918 else
919 printf("Didn't unlink in6_ifaddr from list\n");
920 }
921 {
922 int iilen;
923
924 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) -
925 in6_mask2len(&oia->ia_prefixmask.sin6_addr);
926 in6_prefix_remove_ifid(iilen, oia);
927 }
928 if (oia->ia6_multiaddrs.lh_first != NULL) {
929 /*
930 * XXX thorpej (at) netbsd.org -- if the interface is going
931 * XXX away, don't save the multicast entries, delete them!
932 */
933 if (oia->ia_ifa.ifa_ifp->if_output == if_nulloutput) {
934 struct in6_multi *in6m;
935
936 while ((in6m =
937 LIST_FIRST(&oia->ia6_multiaddrs)) != NULL)
938 in6_delmulti(in6m);
939 } else
940 in6_savemkludge(oia);
941 }
942
943 IFAFREE(&oia->ia_ifa);
944 }
945
946 void
947 in6_purgeif(ifp)
948 struct ifnet *ifp;
949 {
950 struct ifaddr *ifa, *nifa;
951
952 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
953 nifa = TAILQ_NEXT(ifa, ifa_list);
954 if (ifa->ifa_addr->sa_family != AF_INET6)
955 continue;
956 in6_purgeaddr(ifa, ifp);
957 }
958
959 in6_ifdetach(ifp);
960 }
961
962 /*
963 * SIOC[GAD]LIFADDR.
964 * SIOCGLIFADDR: get first address. (???)
965 * SIOCGLIFADDR with IFLR_PREFIX:
966 * get first address that matches the specified prefix.
967 * SIOCALIFADDR: add the specified address.
968 * SIOCALIFADDR with IFLR_PREFIX:
969 * add the specified prefix, filling hostid part from
970 * the first link-local address. prefixlen must be <= 64.
971 * SIOCDLIFADDR: delete the specified address.
972 * SIOCDLIFADDR with IFLR_PREFIX:
973 * delete the first address that matches the specified prefix.
974 * return values:
975 * EINVAL on invalid parameters
976 * EADDRNOTAVAIL on prefix match failed/specified address not found
977 * other values may be returned from in6_ioctl()
978 *
979 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
980 * this is to accomodate address naming scheme other than RFC2374,
981 * in the future.
982 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
983 * address encoding scheme. (see figure on page 8)
984 */
985 static int
986 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
987 struct socket *so;
988 u_long cmd;
989 caddr_t data;
990 struct ifnet *ifp;
991 struct proc *p;
992 {
993 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
994 struct ifaddr *ifa;
995 struct sockaddr *sa;
996
997 /* sanity checks */
998 if (!data || !ifp) {
999 panic("invalid argument to in6_lifaddr_ioctl");
1000 /*NOTRECHED*/
1001 }
1002
1003 switch (cmd) {
1004 case SIOCGLIFADDR:
1005 /* address must be specified on GET with IFLR_PREFIX */
1006 if ((iflr->flags & IFLR_PREFIX) == 0)
1007 break;
1008 /*FALLTHROUGH*/
1009 case SIOCALIFADDR:
1010 case SIOCDLIFADDR:
1011 /* address must be specified on ADD and DELETE */
1012 sa = (struct sockaddr *)&iflr->addr;
1013 if (sa->sa_family != AF_INET6)
1014 return EINVAL;
1015 if (sa->sa_len != sizeof(struct sockaddr_in6))
1016 return EINVAL;
1017 /* XXX need improvement */
1018 sa = (struct sockaddr *)&iflr->dstaddr;
1019 if (sa->sa_family && sa->sa_family != AF_INET6)
1020 return EINVAL;
1021 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1022 return EINVAL;
1023 break;
1024 default: /*shouldn't happen*/
1025 #if 0
1026 panic("invalid cmd to in6_lifaddr_ioctl");
1027 /*NOTREACHED*/
1028 #else
1029 return EOPNOTSUPP;
1030 #endif
1031 }
1032 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1033 return EINVAL;
1034
1035 switch (cmd) {
1036 case SIOCALIFADDR:
1037 {
1038 struct in6_aliasreq ifra;
1039 struct in6_addr *hostid = NULL;
1040 int prefixlen;
1041
1042 if ((iflr->flags & IFLR_PREFIX) != 0) {
1043 struct sockaddr_in6 *sin6;
1044
1045 /*
1046 * hostid is to fill in the hostid part of the
1047 * address. hostid points to the first link-local
1048 * address attached to the interface.
1049 */
1050 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp);
1051 if (!ifa)
1052 return EADDRNOTAVAIL;
1053 hostid = IFA_IN6(ifa);
1054
1055 /* prefixlen must be <= 64. */
1056 if (64 < iflr->prefixlen)
1057 return EINVAL;
1058 prefixlen = iflr->prefixlen;
1059
1060 /* hostid part must be zero. */
1061 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1062 if (sin6->sin6_addr.s6_addr32[2] != 0
1063 || sin6->sin6_addr.s6_addr32[3] != 0) {
1064 return EINVAL;
1065 }
1066 } else
1067 prefixlen = iflr->prefixlen;
1068
1069 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1070 bzero(&ifra, sizeof(ifra));
1071 bcopy(iflr->iflr_name, ifra.ifra_name,
1072 sizeof(ifra.ifra_name));
1073
1074 bcopy(&iflr->addr, &ifra.ifra_addr,
1075 ((struct sockaddr *)&iflr->addr)->sa_len);
1076 if (hostid) {
1077 /* fill in hostid part */
1078 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1079 hostid->s6_addr32[2];
1080 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1081 hostid->s6_addr32[3];
1082 }
1083
1084 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1085 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1086 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1087 if (hostid) {
1088 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1089 hostid->s6_addr32[2];
1090 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1091 hostid->s6_addr32[3];
1092 }
1093 }
1094
1095 ifra.ifra_prefixmask.sin6_family = AF_INET6;
1096 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1097 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1098
1099 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1100 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1101 }
1102 case SIOCGLIFADDR:
1103 case SIOCDLIFADDR:
1104 {
1105 struct in6_ifaddr *ia;
1106 struct in6_addr mask, candidate, match;
1107 struct sockaddr_in6 *sin6;
1108 int cmp;
1109
1110 bzero(&mask, sizeof(mask));
1111 if (iflr->flags & IFLR_PREFIX) {
1112 /* lookup a prefix rather than address. */
1113 in6_len2mask(&mask, iflr->prefixlen);
1114
1115 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1116 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1117 match.s6_addr32[0] &= mask.s6_addr32[0];
1118 match.s6_addr32[1] &= mask.s6_addr32[1];
1119 match.s6_addr32[2] &= mask.s6_addr32[2];
1120 match.s6_addr32[3] &= mask.s6_addr32[3];
1121
1122 /* if you set extra bits, that's wrong */
1123 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1124 return EINVAL;
1125
1126 cmp = 1;
1127 } else {
1128 if (cmd == SIOCGLIFADDR) {
1129 /* on getting an address, take the 1st match */
1130 cmp = 0; /*XXX*/
1131 } else {
1132 /* on deleting an address, do exact match */
1133 in6_len2mask(&mask, 128);
1134 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1135 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1136
1137 cmp = 1;
1138 }
1139 }
1140
1141 for (ifa = ifp->if_addrlist.tqh_first;
1142 ifa;
1143 ifa = ifa->ifa_list.tqe_next)
1144 {
1145 if (ifa->ifa_addr->sa_family != AF_INET6)
1146 continue;
1147 if (!cmp)
1148 break;
1149 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1150 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1151 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1152 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1153 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1154 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1155 break;
1156 }
1157 if (!ifa)
1158 return EADDRNOTAVAIL;
1159 ia = ifa2ia6(ifa);
1160
1161 if (cmd == SIOCGLIFADDR) {
1162 /* fill in the if_laddrreq structure */
1163 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1164
1165 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1166 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1167 ia->ia_dstaddr.sin6_len);
1168 } else
1169 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1170
1171 iflr->prefixlen =
1172 in6_mask2len(&ia->ia_prefixmask.sin6_addr);
1173
1174 iflr->flags = ia->ia6_flags; /*XXX*/
1175
1176 return 0;
1177 } else {
1178 struct in6_aliasreq ifra;
1179
1180 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1181 bzero(&ifra, sizeof(ifra));
1182 bcopy(iflr->iflr_name, ifra.ifra_name,
1183 sizeof(ifra.ifra_name));
1184
1185 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1186 ia->ia_addr.sin6_len);
1187 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1188 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1189 ia->ia_dstaddr.sin6_len);
1190 }
1191 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1192 ia->ia_prefixmask.sin6_len);
1193
1194 ifra.ifra_flags = ia->ia6_flags;
1195 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1196 ifp, p);
1197 }
1198 }
1199 }
1200
1201 return EOPNOTSUPP; /*just for safety*/
1202 }
1203
1204 /*
1205 * Delete any existing route for an interface.
1206 */
1207 void
1208 in6_ifscrub(ifp, ia)
1209 register struct ifnet *ifp;
1210 register struct in6_ifaddr *ia;
1211 {
1212 if ((ia->ia_flags & IFA_ROUTE) == 0)
1213 return;
1214 if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
1215 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
1216 else
1217 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
1218 ia->ia_flags &= ~IFA_ROUTE;
1219
1220 /* Remove ownaddr's loopback rtentry, if it exists. */
1221 in6_ifremloop(&(ia->ia_ifa));
1222 }
1223
1224 /*
1225 * Initialize an interface's intetnet6 address
1226 * and routing table entry.
1227 */
1228 int
1229 in6_ifinit(ifp, ia, sin6, scrub)
1230 struct ifnet *ifp;
1231 struct in6_ifaddr *ia;
1232 struct sockaddr_in6 *sin6;
1233 int scrub;
1234 {
1235 struct sockaddr_in6 oldaddr;
1236 int error, flags = RTF_UP;
1237 int s = splimp();
1238
1239 oldaddr = ia->ia_addr;
1240 ia->ia_addr = *sin6;
1241 /*
1242 * Give the interface a chance to initialize
1243 * if this is its first address,
1244 * and to validate the address if necessary.
1245 */
1246 if (ifp->if_ioctl &&
1247 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1248 splx(s);
1249 ia->ia_addr = oldaddr;
1250 return(error);
1251 }
1252
1253 switch (ifp->if_type) {
1254 case IFT_ARCNET:
1255 case IFT_ETHER:
1256 case IFT_FDDI:
1257 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1258 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1259 break;
1260 case IFT_PPP:
1261 ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest;
1262 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1263 break;
1264 }
1265
1266 splx(s);
1267 if (scrub) {
1268 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1269 in6_ifscrub(ifp, ia);
1270 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1271 }
1272 /* xxx
1273 * in_socktrim
1274 */
1275 /*
1276 * Add route for the network.
1277 */
1278 ia->ia_ifa.ifa_metric = ifp->if_metric;
1279 if (ifp->if_flags & IFF_LOOPBACK) {
1280 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
1281 flags |= RTF_HOST;
1282 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1283 if (ia->ia_dstaddr.sin6_family != AF_INET6)
1284 return(0);
1285 flags |= RTF_HOST;
1286 }
1287 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
1288 ia->ia_flags |= IFA_ROUTE;
1289
1290 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
1291 in6_ifaddloop(&(ia->ia_ifa));
1292
1293 if (ifp->if_flags & IFF_MULTICAST)
1294 in6_restoremkludge(ia, ifp);
1295
1296 return(error);
1297 }
1298
1299 /*
1300 * Multicast address kludge:
1301 * If there were any multicast addresses attached to this interface address,
1302 * either move them to another address on this interface, or save them until
1303 * such time as this interface is reconfigured for IPv6.
1304 */
1305 void
1306 in6_savemkludge(oia)
1307 struct in6_ifaddr *oia;
1308 {
1309 struct in6_ifaddr *ia;
1310 struct in6_multi *in6m, *next;
1311
1312 IFP_TO_IA6(oia->ia_ifp, ia);
1313 if (ia) { /* there is another address */
1314 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1315 next = in6m->in6m_entry.le_next;
1316 IFAFREE(&in6m->in6m_ia->ia_ifa);
1317 IFAREF(&ia->ia_ifa);
1318 in6m->in6m_ia = ia;
1319 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1320 }
1321 } else { /* last address on this if deleted, save */
1322 struct multi6_kludge *mk;
1323
1324 mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
1325
1326 LIST_INIT(&mk->mk_head);
1327 mk->mk_ifp = oia->ia_ifp;
1328
1329 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1330 next = in6m->in6m_entry.le_next;
1331 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1332 in6m->in6m_ia = NULL;
1333 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
1334 }
1335
1336 if (mk->mk_head.lh_first != NULL) {
1337 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
1338 }
1339 else {
1340 FREE(mk, M_IPMADDR);
1341 }
1342 }
1343 }
1344
1345 /*
1346 * Continuation of multicast address hack:
1347 * If there was a multicast group list previously saved for this interface,
1348 * then we re-attach it to the first address configured on the i/f.
1349 */
1350 void
1351 in6_restoremkludge(ia, ifp)
1352 struct in6_ifaddr *ia;
1353 struct ifnet *ifp;
1354 {
1355 struct multi6_kludge *mk;
1356
1357 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1358 if (mk->mk_ifp == ifp) {
1359 struct in6_multi *in6m, *next;
1360
1361 for (in6m = mk->mk_head.lh_first; in6m; in6m = next){
1362 next = in6m->in6m_entry.le_next;
1363 in6m->in6m_ia = ia;
1364 IFAREF(&ia->ia_ifa); /* gain a reference */
1365 LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
1366 in6m, in6m_entry);
1367 }
1368 LIST_REMOVE(mk, mk_entry);
1369 free(mk, M_IPMADDR);
1370 break;
1371 }
1372 }
1373 }
1374
1375 void
1376 in6_purgemkludge(ifp)
1377 struct ifnet *ifp;
1378 {
1379 struct multi6_kludge *mk;
1380 struct in6_multi *in6m;
1381
1382 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1383 if (mk->mk_ifp != ifp)
1384 continue;
1385
1386 /* leave from all multicast groups joined */
1387 while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL)
1388 in6_delmulti(in6m);
1389 LIST_REMOVE(mk, mk_entry);
1390 free(mk, M_IPMADDR);
1391 break;
1392 }
1393 }
1394
1395 /*
1396 * Add an address to the list of IP6 multicast addresses for a
1397 * given interface.
1398 */
1399 struct in6_multi *
1400 in6_addmulti(maddr6, ifp, errorp)
1401 register struct in6_addr *maddr6;
1402 register struct ifnet *ifp;
1403 int *errorp;
1404 {
1405 struct in6_ifaddr *ia;
1406 struct in6_ifreq ifr;
1407 struct in6_multi *in6m;
1408 int s = splsoftnet();
1409
1410 *errorp = 0;
1411 /*
1412 * See if address already in list.
1413 */
1414 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
1415 if (in6m != NULL) {
1416 /*
1417 * Found it; just increment the refrence count.
1418 */
1419 in6m->in6m_refcount++;
1420 } else {
1421 /*
1422 * New address; allocate a new multicast record
1423 * and link it into the interface's multicast list.
1424 */
1425 in6m = (struct in6_multi *)
1426 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1427 if (in6m == NULL) {
1428 splx(s);
1429 *errorp = ENOBUFS;
1430 return(NULL);
1431 }
1432 in6m->in6m_addr = *maddr6;
1433 in6m->in6m_ifp = ifp;
1434 in6m->in6m_refcount = 1;
1435 IFP_TO_IA6(ifp, ia);
1436 if (ia == NULL) {
1437 free(in6m, M_IPMADDR);
1438 splx(s);
1439 *errorp = EADDRNOTAVAIL; /* appropriate? */
1440 return(NULL);
1441 }
1442 in6m->in6m_ia = ia;
1443 IFAREF(&ia->ia_ifa); /* gain a reference */
1444 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1445
1446 /*
1447 * Ask the network driver to update its multicast reception
1448 * filter appropriately for the new address.
1449 */
1450 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1451 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1452 ifr.ifr_addr.sin6_family = AF_INET6;
1453 ifr.ifr_addr.sin6_addr = *maddr6;
1454 if (ifp->if_ioctl == NULL)
1455 *errorp = ENXIO; /* XXX: appropriate? */
1456 else
1457 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
1458 (caddr_t)&ifr);
1459 if (*errorp) {
1460 LIST_REMOVE(in6m, in6m_entry);
1461 free(in6m, M_IPMADDR);
1462 splx(s);
1463 return(NULL);
1464 }
1465 /*
1466 * Let MLD6 know that we have joined a new IP6 multicast
1467 * group.
1468 */
1469 mld6_start_listening(in6m);
1470 }
1471 splx(s);
1472 return(in6m);
1473 }
1474
1475 /*
1476 * Delete a multicast address record.
1477 */
1478 void
1479 in6_delmulti(in6m)
1480 struct in6_multi *in6m;
1481 {
1482 struct in6_ifreq ifr;
1483 int s = splsoftnet();
1484
1485 if (--in6m->in6m_refcount == 0) {
1486 /*
1487 * No remaining claims to this record; let MLD6 know
1488 * that we are leaving the multicast group.
1489 */
1490 mld6_stop_listening(in6m);
1491
1492 /*
1493 * Unlink from list.
1494 */
1495 LIST_REMOVE(in6m, in6m_entry);
1496 if (in6m->in6m_ia)
1497 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1498
1499 /*
1500 * Notify the network driver to update its multicast
1501 * reception filter.
1502 */
1503 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1504 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1505 ifr.ifr_addr.sin6_family = AF_INET6;
1506 ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
1507 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
1508 SIOCDELMULTI, (caddr_t)&ifr);
1509 free(in6m, M_IPMADDR);
1510 }
1511 splx(s);
1512 }
1513
1514 /*
1515 * Find an IPv6 interface link-local address specific to an interface.
1516 */
1517 struct in6_ifaddr *
1518 in6ifa_ifpforlinklocal(ifp)
1519 struct ifnet *ifp;
1520 {
1521 register struct ifaddr *ifa;
1522
1523 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1524 {
1525 if (ifa->ifa_addr == NULL)
1526 continue; /* just for safety */
1527 if (ifa->ifa_addr->sa_family != AF_INET6)
1528 continue;
1529 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)))
1530 break;
1531 }
1532
1533 return((struct in6_ifaddr *)ifa);
1534 }
1535
1536
1537 /*
1538 * find the internet address corresponding to a given interface and address.
1539 */
1540 struct in6_ifaddr *
1541 in6ifa_ifpwithaddr(ifp, addr)
1542 struct ifnet *ifp;
1543 struct in6_addr *addr;
1544 {
1545 register struct ifaddr *ifa;
1546
1547 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1548 {
1549 if (ifa->ifa_addr == NULL)
1550 continue; /* just for safety */
1551 if (ifa->ifa_addr->sa_family != AF_INET6)
1552 continue;
1553 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1554 break;
1555 }
1556
1557 return((struct in6_ifaddr *)ifa);
1558 }
1559
1560 /*
1561 * Convert IP6 address to printable (loggable) representation.
1562 */
1563 static char digits[] = "0123456789abcdef";
1564 static int ip6round = 0;
1565 char *
1566 ip6_sprintf(addr)
1567 register struct in6_addr *addr;
1568 {
1569 static char ip6buf[8][48];
1570 register int i;
1571 register char *cp;
1572 register u_short *a = (u_short *)addr;
1573 register u_char *d;
1574 int dcolon = 0;
1575
1576 ip6round = (ip6round + 1) & 7;
1577 cp = ip6buf[ip6round];
1578
1579 for (i = 0; i < 8; i++) {
1580 if (dcolon == 1) {
1581 if (*a == 0) {
1582 if (i == 7)
1583 *cp++ = ':';
1584 a++;
1585 continue;
1586 } else
1587 dcolon = 2;
1588 }
1589 if (*a == 0) {
1590 if (dcolon == 0 && *(a + 1) == 0) {
1591 if (i == 0)
1592 *cp++ = ':';
1593 *cp++ = ':';
1594 dcolon = 1;
1595 } else {
1596 *cp++ = '0';
1597 *cp++ = ':';
1598 }
1599 a++;
1600 continue;
1601 }
1602 d = (u_char *)a;
1603 *cp++ = digits[*d >> 4];
1604 *cp++ = digits[*d++ & 0xf];
1605 *cp++ = digits[*d >> 4];
1606 *cp++ = digits[*d & 0xf];
1607 *cp++ = ':';
1608 a++;
1609 }
1610 *--cp = 0;
1611 return(ip6buf[ip6round]);
1612 }
1613
1614 int
1615 in6_localaddr(in6)
1616 struct in6_addr *in6;
1617 {
1618 struct in6_ifaddr *ia;
1619
1620 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1621 return 1;
1622
1623 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1624 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1625 &ia->ia_prefixmask.sin6_addr))
1626 return 1;
1627
1628 return (0);
1629 }
1630
1631 /*
1632 * Get a scope of the address. Node-local, link-local, site-local or global.
1633 */
1634 int
1635 in6_addrscope (addr)
1636 struct in6_addr *addr;
1637 {
1638 int scope;
1639
1640 if (addr->s6_addr8[0] == 0xfe) {
1641 scope = addr->s6_addr8[1] & 0xc0;
1642
1643 switch (scope) {
1644 case 0x80:
1645 return IPV6_ADDR_SCOPE_LINKLOCAL;
1646 break;
1647 case 0xc0:
1648 return IPV6_ADDR_SCOPE_SITELOCAL;
1649 break;
1650 default:
1651 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
1652 break;
1653 }
1654 }
1655
1656
1657 if (addr->s6_addr8[0] == 0xff) {
1658 scope = addr->s6_addr8[1] & 0x0f;
1659
1660 /*
1661 * due to other scope such as reserved,
1662 * return scope doesn't work.
1663 */
1664 switch (scope) {
1665 case IPV6_ADDR_SCOPE_NODELOCAL:
1666 return IPV6_ADDR_SCOPE_NODELOCAL;
1667 break;
1668 case IPV6_ADDR_SCOPE_LINKLOCAL:
1669 return IPV6_ADDR_SCOPE_LINKLOCAL;
1670 break;
1671 case IPV6_ADDR_SCOPE_SITELOCAL:
1672 return IPV6_ADDR_SCOPE_SITELOCAL;
1673 break;
1674 default:
1675 return IPV6_ADDR_SCOPE_GLOBAL;
1676 break;
1677 }
1678 }
1679
1680 if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) {
1681 if (addr->s6_addr8[15] == 1) /* loopback */
1682 return IPV6_ADDR_SCOPE_NODELOCAL;
1683 if (addr->s6_addr8[15] == 0) /* unspecified */
1684 return IPV6_ADDR_SCOPE_LINKLOCAL;
1685 }
1686
1687 return IPV6_ADDR_SCOPE_GLOBAL;
1688 }
1689
1690 /*
1691 * return length of part which dst and src are equal
1692 * hard coding...
1693 */
1694
1695 int
1696 in6_matchlen(src, dst)
1697 struct in6_addr *src, *dst;
1698 {
1699 int match = 0;
1700 u_char *s = (u_char *)src, *d = (u_char *)dst;
1701 u_char *lim = s + 16, r;
1702
1703 while (s < lim)
1704 if ((r = (*d++ ^ *s++)) != 0) {
1705 while (r < 128) {
1706 match++;
1707 r <<= 1;
1708 }
1709 break;
1710 } else
1711 match += 8;
1712 return match;
1713 }
1714
1715 int
1716 in6_are_prefix_equal(p1, p2, len)
1717 struct in6_addr *p1, *p2;
1718 int len;
1719 {
1720 int bytelen, bitlen;
1721
1722 /* sanity check */
1723 if (0 > len || len > 128) {
1724 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1725 len);
1726 return(0);
1727 }
1728
1729 bytelen = len / 8;
1730 bitlen = len % 8;
1731
1732 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1733 return(0);
1734 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1735 p2->s6_addr[bytelen] >> (8 - bitlen))
1736 return(0);
1737
1738 return(1);
1739 }
1740
1741 void
1742 in6_prefixlen2mask(maskp, len)
1743 struct in6_addr *maskp;
1744 int len;
1745 {
1746 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1747 int bytelen, bitlen, i;
1748
1749 /* sanity check */
1750 if (0 > len || len > 128) {
1751 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1752 len);
1753 return;
1754 }
1755
1756 bzero(maskp, sizeof(*maskp));
1757 bytelen = len / 8;
1758 bitlen = len % 8;
1759 for (i = 0; i < bytelen; i++)
1760 maskp->s6_addr[i] = 0xff;
1761 if (bitlen)
1762 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1763 }
1764
1765 /*
1766 * return the best address out of the same scope
1767 */
1768
1769 struct in6_ifaddr *
1770 in6_ifawithscope(ifp, dst)
1771 register struct ifnet *ifp;
1772 register struct in6_addr *dst;
1773 {
1774 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1775 struct ifaddr *ifa;
1776 struct in6_ifaddr *besta = NULL, *ia;
1777 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
1778
1779 dep[0] = dep[1] = NULL;
1780
1781 /*
1782 * We first look for addresses in the same scope.
1783 * If there is one, return it.
1784 * If two or more, return one which matches the dst longest.
1785 * If none, return one of global addresses assigned other ifs.
1786 */
1787 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1788 {
1789 if (ifa->ifa_addr->sa_family != AF_INET6)
1790 continue;
1791 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1792 continue; /* XXX: is there any case to allow anycast? */
1793 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1794 continue; /* don't use this interface */
1795 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1796 continue;
1797 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1798 if (ip6_use_deprecated)
1799 dep[0] = (struct in6_ifaddr *)ifa;
1800 continue;
1801 }
1802
1803 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1804 /*
1805 * call in6_matchlen() as few as possible
1806 */
1807 if (besta) {
1808 if (blen == -1)
1809 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1810 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1811 if (tlen > blen) {
1812 blen = tlen;
1813 besta = (struct in6_ifaddr *)ifa;
1814 }
1815 } else
1816 besta = (struct in6_ifaddr *)ifa;
1817 }
1818 }
1819 if (besta)
1820 return besta;
1821
1822 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1823 if (IPV6_ADDR_SCOPE_GLOBAL !=
1824 in6_addrscope(&(ia->ia_addr.sin6_addr)))
1825 continue;
1826 /* XXX: is there any case to allow anycast? */
1827 if ((ia->ia6_flags & IN6_IFF_ANYCAST) != 0)
1828 continue;
1829 if ((ia->ia6_flags & IN6_IFF_NOTREADY) != 0)
1830 continue;
1831 if ((ia->ia6_flags & IN6_IFF_DETACHED) != 0)
1832 continue;
1833 if ((ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
1834 if (ip6_use_deprecated)
1835 dep[1] = (struct in6_ifaddr *)ifa;
1836 continue;
1837 }
1838 return ia;
1839 }
1840
1841 /* use the last-resort values, that are, deprecated addresses */
1842 if (dep[0])
1843 return dep[0];
1844 if (dep[1])
1845 return dep[1];
1846
1847 return NULL;
1848 }
1849
1850 /*
1851 * return the best address out of the same scope. if no address was
1852 * found, return the first valid address from designated IF.
1853 */
1854
1855 struct in6_ifaddr *
1856 in6_ifawithifp(ifp, dst)
1857 register struct ifnet *ifp;
1858 register struct in6_addr *dst;
1859 {
1860 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1861 struct ifaddr *ifa;
1862 struct in6_ifaddr *besta = 0;
1863 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
1864
1865 dep[0] = dep[1] = NULL;
1866
1867 /*
1868 * We first look for addresses in the same scope.
1869 * If there is one, return it.
1870 * If two or more, return one which matches the dst longest.
1871 * If none, return one of global addresses assigned other ifs.
1872 */
1873 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1874 {
1875 if (ifa->ifa_addr->sa_family != AF_INET6)
1876 continue;
1877 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1878 continue; /* XXX: is there any case to allow anycast? */
1879 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1880 continue; /* don't use this interface */
1881 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1882 continue;
1883 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1884 if (ip6_use_deprecated)
1885 dep[0] = (struct in6_ifaddr *)ifa;
1886 continue;
1887 }
1888
1889 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1890 /*
1891 * call in6_matchlen() as few as possible
1892 */
1893 if (besta) {
1894 if (blen == -1)
1895 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1896 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1897 if (tlen > blen) {
1898 blen = tlen;
1899 besta = (struct in6_ifaddr *)ifa;
1900 }
1901 } else
1902 besta = (struct in6_ifaddr *)ifa;
1903 }
1904 }
1905 if (besta)
1906 return(besta);
1907
1908 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1909 {
1910 if (ifa->ifa_addr->sa_family != AF_INET6)
1911 continue;
1912 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1913 continue; /* XXX: is there any case to allow anycast? */
1914 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1915 continue; /* don't use this interface */
1916 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1917 continue;
1918 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1919 if (ip6_use_deprecated)
1920 dep[1] = (struct in6_ifaddr *)ifa;
1921 continue;
1922 }
1923
1924 return (struct in6_ifaddr *)ifa;
1925 }
1926
1927 /* use the last-resort values, that are, deprecated addresses */
1928 if (dep[0])
1929 return dep[0];
1930 if (dep[1])
1931 return dep[1];
1932
1933 return NULL;
1934 }
1935
1936 /*
1937 * perform DAD when interface becomes IFF_UP.
1938 */
1939 void
1940 in6_if_up(ifp)
1941 struct ifnet *ifp;
1942 {
1943 struct ifaddr *ifa;
1944 struct in6_ifaddr *ia;
1945 struct sockaddr_dl *sdl;
1946 int type;
1947 struct ether_addr ea;
1948 int off;
1949 int dad_delay; /* delay ticks before DAD output */
1950
1951 bzero(&ea, sizeof(ea));
1952 sdl = NULL;
1953
1954 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1955 {
1956 if (ifa->ifa_addr->sa_family == AF_INET6
1957 && IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
1958 goto dad;
1959 }
1960 if (ifa->ifa_addr->sa_family != AF_LINK)
1961 continue;
1962 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1963 break;
1964 }
1965
1966 switch (ifp->if_type) {
1967 case IFT_LOOP:
1968 in6_ifattach(ifp, IN6_IFT_LOOP, NULL, 1);
1969 break;
1970 case IFT_SLIP:
1971 case IFT_PPP:
1972 case IFT_GIF:
1973 case IFT_FAITH:
1974 type = IN6_IFT_P2P;
1975 in6_ifattach(ifp, type, 0, 1);
1976 break;
1977 case IFT_ETHER:
1978 case IFT_FDDI:
1979 case IFT_ATM:
1980 type = IN6_IFT_802;
1981 if (sdl == NULL)
1982 break;
1983 off = sdl->sdl_nlen;
1984 if (bcmp(&sdl->sdl_data[off], &ea, sizeof(ea)) != 0)
1985 in6_ifattach(ifp, type, LLADDR(sdl), 0);
1986 break;
1987 case IFT_ARCNET:
1988 type = IN6_IFT_ARCNET;
1989 if (sdl == NULL)
1990 break;
1991 off = sdl->sdl_nlen;
1992 if (sdl->sdl_data[off] != 0) /* XXX ?: */
1993 in6_ifattach(ifp, type, LLADDR(sdl), 0);
1994 break;
1995 default:
1996 break;
1997 }
1998
1999 dad:
2000 dad_delay = 0;
2001 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
2002 {
2003 if (ifa->ifa_addr->sa_family != AF_INET6)
2004 continue;
2005 ia = (struct in6_ifaddr *)ifa;
2006 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2007 nd6_dad_start(ifa, &dad_delay);
2008 }
2009 }
2010
2011 /*
2012 * Calculate max IPv6 MTU through all the interfaces and store it
2013 * to in6_maxmtu.
2014 */
2015 void
2016 in6_setmaxmtu()
2017 {
2018 unsigned long maxmtu = 0;
2019 struct ifnet *ifp;
2020
2021 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2022 {
2023 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2024 nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
2025 maxmtu = nd_ifinfo[ifp->if_index].linkmtu;
2026 }
2027 if (maxmtu) /* update only when maxmtu is positive */
2028 in6_maxmtu = maxmtu;
2029 }
2030