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