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