in6.c revision 1.11 1 /* $NetBSD: in6.c,v 1.11 2000/02/02 13:44:05 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 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
1335 }
1336
1337 if (mk->mk_head.lh_first != NULL) {
1338 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
1339 }
1340 else {
1341 FREE(mk, M_IPMADDR);
1342 }
1343 }
1344 }
1345
1346 /*
1347 * Continuation of multicast address hack:
1348 * If there was a multicast group list previously saved for this interface,
1349 * then we re-attach it to the first address configured on the i/f.
1350 */
1351 void
1352 in6_restoremkludge(ia, ifp)
1353 struct in6_ifaddr *ia;
1354 struct ifnet *ifp;
1355 {
1356 struct multi6_kludge *mk;
1357
1358 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1359 if (mk->mk_ifp == ifp) {
1360 struct in6_multi *in6m, *next;
1361
1362 for (in6m = mk->mk_head.lh_first; in6m; in6m = next){
1363 next = in6m->in6m_entry.le_next;
1364 LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
1365 in6m, in6m_entry);
1366 }
1367 LIST_REMOVE(mk, mk_entry);
1368 free(mk, M_IPMADDR);
1369 break;
1370 }
1371 }
1372 }
1373
1374 /*
1375 * Add an address to the list of IP6 multicast addresses for a
1376 * given interface.
1377 */
1378 struct in6_multi *
1379 in6_addmulti(maddr6, ifp, errorp)
1380 register struct in6_addr *maddr6;
1381 register struct ifnet *ifp;
1382 int *errorp;
1383 {
1384 struct in6_ifaddr *ia;
1385 struct in6_ifreq ifr;
1386 struct in6_multi *in6m;
1387 int s = splsoftnet();
1388
1389 *errorp = 0;
1390 /*
1391 * See if address already in list.
1392 */
1393 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
1394 if (in6m != NULL) {
1395 /*
1396 * Found it; just increment the refrence count.
1397 */
1398 in6m->in6m_refcount++;
1399 } else {
1400 /*
1401 * New address; allocate a new multicast record
1402 * and link it into the interface's multicast list.
1403 */
1404 in6m = (struct in6_multi *)
1405 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1406 if (in6m == NULL) {
1407 splx(s);
1408 *errorp = ENOBUFS;
1409 return(NULL);
1410 }
1411 in6m->in6m_addr = *maddr6;
1412 in6m->in6m_ifp = ifp;
1413 in6m->in6m_refcount = 1;
1414 IFP_TO_IA6(ifp, ia);
1415 if (ia == NULL) {
1416 free(in6m, M_IPMADDR);
1417 splx(s);
1418 *errorp = EADDRNOTAVAIL; /* appropriate? */
1419 return(NULL);
1420 }
1421 in6m->in6m_ia = ia;
1422 IFAREF(&ia->ia_ifa); /* gain a reference */
1423 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1424
1425 /*
1426 * Ask the network driver to update its multicast reception
1427 * filter appropriately for the new address.
1428 */
1429 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1430 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1431 ifr.ifr_addr.sin6_family = AF_INET6;
1432 ifr.ifr_addr.sin6_addr = *maddr6;
1433 if (ifp->if_ioctl == NULL)
1434 *errorp = ENXIO; /* XXX: appropriate? */
1435 else
1436 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
1437 (caddr_t)&ifr);
1438 if (*errorp) {
1439 LIST_REMOVE(in6m, in6m_entry);
1440 free(in6m, M_IPMADDR);
1441 splx(s);
1442 return(NULL);
1443 }
1444 /*
1445 * Let MLD6 know that we have joined a new IP6 multicast
1446 * group.
1447 */
1448 mld6_start_listening(in6m);
1449 }
1450 splx(s);
1451 return(in6m);
1452 }
1453
1454 /*
1455 * Delete a multicast address record.
1456 */
1457 void
1458 in6_delmulti(in6m)
1459 struct in6_multi *in6m;
1460 {
1461 struct in6_ifreq ifr;
1462 int s = splsoftnet();
1463
1464 if (--in6m->in6m_refcount == 0) {
1465 /*
1466 * No remaining claims to this record; let MLD6 know
1467 * that we are leaving the multicast group.
1468 */
1469 mld6_stop_listening(in6m);
1470
1471 /*
1472 * Unlink from list.
1473 */
1474 LIST_REMOVE(in6m, in6m_entry);
1475 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1476
1477 /*
1478 * Notify the network driver to update its multicast
1479 * reception filter.
1480 */
1481 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1482 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1483 ifr.ifr_addr.sin6_family = AF_INET6;
1484 ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
1485 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
1486 SIOCDELMULTI, (caddr_t)&ifr);
1487 free(in6m, M_IPMADDR);
1488 }
1489 splx(s);
1490 }
1491
1492 /*
1493 * Find an IPv6 interface link-local address specific to an interface.
1494 */
1495 struct in6_ifaddr *
1496 in6ifa_ifpforlinklocal(ifp)
1497 struct ifnet *ifp;
1498 {
1499 register struct ifaddr *ifa;
1500
1501 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1502 {
1503 if (ifa->ifa_addr == NULL)
1504 continue; /* just for safety */
1505 if (ifa->ifa_addr->sa_family != AF_INET6)
1506 continue;
1507 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)))
1508 break;
1509 }
1510
1511 return((struct in6_ifaddr *)ifa);
1512 }
1513
1514
1515 /*
1516 * find the internet address corresponding to a given interface and address.
1517 */
1518 struct in6_ifaddr *
1519 in6ifa_ifpwithaddr(ifp, addr)
1520 struct ifnet *ifp;
1521 struct in6_addr *addr;
1522 {
1523 register struct ifaddr *ifa;
1524
1525 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1526 {
1527 if (ifa->ifa_addr == NULL)
1528 continue; /* just for safety */
1529 if (ifa->ifa_addr->sa_family != AF_INET6)
1530 continue;
1531 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1532 break;
1533 }
1534
1535 return((struct in6_ifaddr *)ifa);
1536 }
1537
1538 /*
1539 * Convert IP6 address to printable (loggable) representation.
1540 */
1541 static char digits[] = "0123456789abcdef";
1542 static int ip6round = 0;
1543 char *
1544 ip6_sprintf(addr)
1545 register struct in6_addr *addr;
1546 {
1547 static char ip6buf[8][48];
1548 register int i;
1549 register char *cp;
1550 register u_short *a = (u_short *)addr;
1551 register u_char *d;
1552 int dcolon = 0;
1553
1554 ip6round = (ip6round + 1) & 7;
1555 cp = ip6buf[ip6round];
1556
1557 for (i = 0; i < 8; i++) {
1558 if (dcolon == 1) {
1559 if (*a == 0) {
1560 if (i == 7)
1561 *cp++ = ':';
1562 a++;
1563 continue;
1564 } else
1565 dcolon = 2;
1566 }
1567 if (*a == 0) {
1568 if (dcolon == 0 && *(a + 1) == 0) {
1569 if (i == 0)
1570 *cp++ = ':';
1571 *cp++ = ':';
1572 dcolon = 1;
1573 } else {
1574 *cp++ = '0';
1575 *cp++ = ':';
1576 }
1577 a++;
1578 continue;
1579 }
1580 d = (u_char *)a;
1581 *cp++ = digits[*d >> 4];
1582 *cp++ = digits[*d++ & 0xf];
1583 *cp++ = digits[*d >> 4];
1584 *cp++ = digits[*d & 0xf];
1585 *cp++ = ':';
1586 a++;
1587 }
1588 *--cp = 0;
1589 return(ip6buf[ip6round]);
1590 }
1591
1592 int
1593 in6_localaddr(in6)
1594 struct in6_addr *in6;
1595 {
1596 struct in6_ifaddr *ia;
1597
1598 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1599 return 1;
1600
1601 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1602 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1603 &ia->ia_prefixmask.sin6_addr))
1604 return 1;
1605
1606 return (0);
1607 }
1608
1609 /*
1610 * Get a scope of the address. Node-local, link-local, site-local or global.
1611 */
1612 int
1613 in6_addrscope (addr)
1614 struct in6_addr *addr;
1615 {
1616 int scope;
1617
1618 if (addr->s6_addr8[0] == 0xfe) {
1619 scope = addr->s6_addr8[1] & 0xc0;
1620
1621 switch (scope) {
1622 case 0x80:
1623 return IPV6_ADDR_SCOPE_LINKLOCAL;
1624 break;
1625 case 0xc0:
1626 return IPV6_ADDR_SCOPE_SITELOCAL;
1627 break;
1628 default:
1629 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
1630 break;
1631 }
1632 }
1633
1634
1635 if (addr->s6_addr8[0] == 0xff) {
1636 scope = addr->s6_addr8[1] & 0x0f;
1637
1638 /*
1639 * due to other scope such as reserved,
1640 * return scope doesn't work.
1641 */
1642 switch (scope) {
1643 case IPV6_ADDR_SCOPE_NODELOCAL:
1644 return IPV6_ADDR_SCOPE_NODELOCAL;
1645 break;
1646 case IPV6_ADDR_SCOPE_LINKLOCAL:
1647 return IPV6_ADDR_SCOPE_LINKLOCAL;
1648 break;
1649 case IPV6_ADDR_SCOPE_SITELOCAL:
1650 return IPV6_ADDR_SCOPE_SITELOCAL;
1651 break;
1652 default:
1653 return IPV6_ADDR_SCOPE_GLOBAL;
1654 break;
1655 }
1656 }
1657
1658 if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) {
1659 if (addr->s6_addr8[15] == 1) /* loopback */
1660 return IPV6_ADDR_SCOPE_NODELOCAL;
1661 if (addr->s6_addr8[15] == 0) /* unspecified */
1662 return IPV6_ADDR_SCOPE_LINKLOCAL;
1663 }
1664
1665 return IPV6_ADDR_SCOPE_GLOBAL;
1666 }
1667
1668 /*
1669 * return length of part which dst and src are equal
1670 * hard coding...
1671 */
1672
1673 int
1674 in6_matchlen(src, dst)
1675 struct in6_addr *src, *dst;
1676 {
1677 int match = 0;
1678 u_char *s = (u_char *)src, *d = (u_char *)dst;
1679 u_char *lim = s + 16, r;
1680
1681 while (s < lim)
1682 if ((r = (*d++ ^ *s++)) != 0) {
1683 while (r < 128) {
1684 match++;
1685 r <<= 1;
1686 }
1687 break;
1688 } else
1689 match += 8;
1690 return match;
1691 }
1692
1693 int
1694 in6_are_prefix_equal(p1, p2, len)
1695 struct in6_addr *p1, *p2;
1696 int len;
1697 {
1698 int bytelen, bitlen;
1699
1700 /* sanity check */
1701 if (0 > len || len > 128) {
1702 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1703 len);
1704 return(0);
1705 }
1706
1707 bytelen = len / 8;
1708 bitlen = len % 8;
1709
1710 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1711 return(0);
1712 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1713 p2->s6_addr[bytelen] >> (8 - bitlen))
1714 return(0);
1715
1716 return(1);
1717 }
1718
1719 void
1720 in6_prefixlen2mask(maskp, len)
1721 struct in6_addr *maskp;
1722 int len;
1723 {
1724 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1725 int bytelen, bitlen, i;
1726
1727 /* sanity check */
1728 if (0 > len || len > 128) {
1729 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1730 len);
1731 return;
1732 }
1733
1734 bzero(maskp, sizeof(*maskp));
1735 bytelen = len / 8;
1736 bitlen = len % 8;
1737 for (i = 0; i < bytelen; i++)
1738 maskp->s6_addr[i] = 0xff;
1739 if (bitlen)
1740 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1741 }
1742
1743 /*
1744 * return the best address out of the same scope
1745 */
1746
1747 struct in6_ifaddr *
1748 in6_ifawithscope(ifp, dst)
1749 register struct ifnet *ifp;
1750 register struct in6_addr *dst;
1751 {
1752 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1753 struct ifaddr *ifa;
1754 struct in6_ifaddr *besta = NULL, *ia;
1755 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
1756
1757 dep[0] = dep[1] = NULL;
1758
1759 /*
1760 * We first look for addresses in the same scope.
1761 * If there is one, return it.
1762 * If two or more, return one which matches the dst longest.
1763 * If none, return one of global addresses assigned other ifs.
1764 */
1765 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1766 {
1767 if (ifa->ifa_addr->sa_family != AF_INET6)
1768 continue;
1769 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1770 continue; /* XXX: is there any case to allow anycast? */
1771 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1772 continue; /* don't use this interface */
1773 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1774 continue;
1775 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1776 if (ip6_use_deprecated)
1777 dep[0] = (struct in6_ifaddr *)ifa;
1778 continue;
1779 }
1780
1781 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1782 /*
1783 * call in6_matchlen() as few as possible
1784 */
1785 if (besta) {
1786 if (blen == -1)
1787 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1788 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1789 if (tlen > blen) {
1790 blen = tlen;
1791 besta = (struct in6_ifaddr *)ifa;
1792 }
1793 } else
1794 besta = (struct in6_ifaddr *)ifa;
1795 }
1796 }
1797 if (besta)
1798 return besta;
1799
1800 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1801 if (IPV6_ADDR_SCOPE_GLOBAL !=
1802 in6_addrscope(&(ia->ia_addr.sin6_addr)))
1803 continue;
1804 /* XXX: is there any case to allow anycast? */
1805 if ((ia->ia6_flags & IN6_IFF_ANYCAST) != 0)
1806 continue;
1807 if ((ia->ia6_flags & IN6_IFF_NOTREADY) != 0)
1808 continue;
1809 if ((ia->ia6_flags & IN6_IFF_DETACHED) != 0)
1810 continue;
1811 if ((ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
1812 if (ip6_use_deprecated)
1813 dep[1] = (struct in6_ifaddr *)ifa;
1814 continue;
1815 }
1816 return ia;
1817 }
1818
1819 /* use the last-resort values, that are, deprecated addresses */
1820 if (dep[0])
1821 return dep[0];
1822 if (dep[1])
1823 return dep[1];
1824
1825 return NULL;
1826 }
1827
1828 /*
1829 * return the best address out of the same scope. if no address was
1830 * found, return the first valid address from designated IF.
1831 */
1832
1833 struct in6_ifaddr *
1834 in6_ifawithifp(ifp, dst)
1835 register struct ifnet *ifp;
1836 register struct in6_addr *dst;
1837 {
1838 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1839 struct ifaddr *ifa;
1840 struct in6_ifaddr *besta = 0;
1841 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
1842
1843 dep[0] = dep[1] = NULL;
1844
1845 /*
1846 * We first look for addresses in the same scope.
1847 * If there is one, return it.
1848 * If two or more, return one which matches the dst longest.
1849 * If none, return one of global addresses assigned other ifs.
1850 */
1851 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1852 {
1853 if (ifa->ifa_addr->sa_family != AF_INET6)
1854 continue;
1855 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1856 continue; /* XXX: is there any case to allow anycast? */
1857 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1858 continue; /* don't use this interface */
1859 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1860 continue;
1861 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1862 if (ip6_use_deprecated)
1863 dep[0] = (struct in6_ifaddr *)ifa;
1864 continue;
1865 }
1866
1867 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1868 /*
1869 * call in6_matchlen() as few as possible
1870 */
1871 if (besta) {
1872 if (blen == -1)
1873 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1874 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1875 if (tlen > blen) {
1876 blen = tlen;
1877 besta = (struct in6_ifaddr *)ifa;
1878 }
1879 } else
1880 besta = (struct in6_ifaddr *)ifa;
1881 }
1882 }
1883 if (besta)
1884 return(besta);
1885
1886 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1887 {
1888 if (ifa->ifa_addr->sa_family != AF_INET6)
1889 continue;
1890 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1891 continue; /* XXX: is there any case to allow anycast? */
1892 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1893 continue; /* don't use this interface */
1894 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1895 continue;
1896 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1897 if (ip6_use_deprecated)
1898 dep[1] = (struct in6_ifaddr *)ifa;
1899 continue;
1900 }
1901
1902 return (struct in6_ifaddr *)ifa;
1903 }
1904
1905 /* use the last-resort values, that are, deprecated addresses */
1906 if (dep[0])
1907 return dep[0];
1908 if (dep[1])
1909 return dep[1];
1910
1911 return NULL;
1912 }
1913
1914 /*
1915 * perform DAD when interface becomes IFF_UP.
1916 */
1917 void
1918 in6_if_up(ifp)
1919 struct ifnet *ifp;
1920 {
1921 struct ifaddr *ifa;
1922 struct in6_ifaddr *ia;
1923 struct sockaddr_dl *sdl;
1924 int type;
1925 struct ether_addr ea;
1926 int off;
1927 int dad_delay; /* delay ticks before DAD output */
1928
1929 bzero(&ea, sizeof(ea));
1930 sdl = NULL;
1931
1932 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1933 {
1934 if (ifa->ifa_addr->sa_family == AF_INET6
1935 && IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
1936 goto dad;
1937 }
1938 if (ifa->ifa_addr->sa_family != AF_LINK)
1939 continue;
1940 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1941 break;
1942 }
1943
1944 switch (ifp->if_type) {
1945 case IFT_LOOP:
1946 in6_ifattach(ifp, IN6_IFT_LOOP, NULL, 1);
1947 break;
1948 case IFT_SLIP:
1949 case IFT_PPP:
1950 case IFT_GIF:
1951 case IFT_FAITH:
1952 type = IN6_IFT_P2P;
1953 in6_ifattach(ifp, type, 0, 1);
1954 break;
1955 case IFT_ETHER:
1956 case IFT_FDDI:
1957 case IFT_ATM:
1958 type = IN6_IFT_802;
1959 if (sdl == NULL)
1960 break;
1961 off = sdl->sdl_nlen;
1962 if (bcmp(&sdl->sdl_data[off], &ea, sizeof(ea)) != 0)
1963 in6_ifattach(ifp, type, LLADDR(sdl), 0);
1964 break;
1965 case IFT_ARCNET:
1966 type = IN6_IFT_ARCNET;
1967 if (sdl == NULL)
1968 break;
1969 off = sdl->sdl_nlen;
1970 if (sdl->sdl_data[off] != 0) /* XXX ?: */
1971 in6_ifattach(ifp, type, LLADDR(sdl), 0);
1972 break;
1973 default:
1974 break;
1975 }
1976
1977 dad:
1978 dad_delay = 0;
1979 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1980 {
1981 if (ifa->ifa_addr->sa_family != AF_INET6)
1982 continue;
1983 ia = (struct in6_ifaddr *)ifa;
1984 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
1985 nd6_dad_start(ifa, &dad_delay);
1986 }
1987 }
1988
1989 /*
1990 * Calculate max IPv6 MTU through all the interfaces and store it
1991 * to in6_maxmtu.
1992 */
1993 void
1994 in6_setmaxmtu()
1995 {
1996 unsigned long maxmtu = 0;
1997 struct ifnet *ifp;
1998
1999 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2000 {
2001 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2002 nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
2003 maxmtu = nd_ifinfo[ifp->if_index].linkmtu;
2004 }
2005 if (maxmtu) /* update only when maxmtu is positive */
2006 in6_maxmtu = maxmtu;
2007 }
2008