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