in6.c revision 1.30 1 /* $NetBSD: in6.c,v 1.30 2000/04/16 15:00:56 itojun Exp $ */
2 /* $KAME: in6.c,v 1.75 2000/04/12 03:51:29 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 "gif.h"
87 #if NGIF > 0
88 #include <net/if_gif.h>
89 #endif
90 #include <net/if_dl.h>
91
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <net/if_ether.h>
95
96 #include <netinet6/nd6.h>
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet6/mld6_var.h>
100 #include <netinet6/ip6_mroute.h>
101 #include <netinet6/in6_ifattach.h>
102
103 #include <net/net_osdep.h>
104
105 /* enable backward compatibility code for obsoleted ioctls */
106 #define COMPAT_IN6IFIOCTL
107
108 /*
109 * Definitions of some costant IP6 addresses.
110 */
111 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
112 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
113 const struct in6_addr in6addr_nodelocal_allnodes =
114 IN6ADDR_NODELOCAL_ALLNODES_INIT;
115 const struct in6_addr in6addr_linklocal_allnodes =
116 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
117 const struct in6_addr in6addr_linklocal_allrouters =
118 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
119
120 const struct in6_addr in6mask0 = IN6MASK0;
121 const struct in6_addr in6mask32 = IN6MASK32;
122 const struct in6_addr in6mask64 = IN6MASK64;
123 const struct in6_addr in6mask96 = IN6MASK96;
124 const struct in6_addr in6mask128 = IN6MASK128;
125
126 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
127 struct ifnet *, struct proc *));
128
129 /*
130 * This structure is used to keep track of in6_multi chains which belong to
131 * deleted interface addresses.
132 */
133 static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */
134
135 struct multi6_kludge {
136 LIST_ENTRY(multi6_kludge) mk_entry;
137 struct ifnet *mk_ifp;
138 struct in6_multihead mk_head;
139 };
140
141 /*
142 * Check if the loopback entry will be automatically generated.
143 * if 0 returned, will not be automatically generated.
144 * if 1 returned, will be automatically generated.
145 */
146 static int
147 in6_is_ifloop_auto(struct ifaddr *ifa)
148 {
149 #define SIN6(s) ((struct sockaddr_in6 *)s)
150 /*
151 * If RTF_CLONING is unset, or (IFF_LOOPBACK | IFF_POINTOPOINT),
152 * or netmask is all0 or all1, then cloning will not happen,
153 * then we can't rely on its loopback entry generation.
154 */
155 if ((ifa->ifa_flags & RTF_CLONING) == 0 ||
156 (ifa->ifa_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) ||
157 (SIN6(ifa->ifa_netmask)->sin6_len == sizeof(struct sockaddr_in6)
158 &&
159 IN6_ARE_ADDR_EQUAL(&SIN6(ifa->ifa_netmask)->sin6_addr,
160 &in6mask128)) ||
161 ((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_len == 0)
162 return 0;
163 else
164 return 1;
165 #undef SIN6
166 }
167
168 /*
169 * Subroutine for in6_ifaddloop() and in6_ifremloop().
170 * This routine does actual work.
171 */
172 static void
173 in6_ifloop_request(int cmd, struct ifaddr *ifa)
174 {
175 struct sockaddr_in6 lo_sa;
176 struct sockaddr_in6 all1_sa;
177 struct rtentry *nrt = NULL;
178
179 bzero(&lo_sa, sizeof(lo_sa));
180 bzero(&all1_sa, sizeof(all1_sa));
181 lo_sa.sin6_family = AF_INET6;
182 lo_sa.sin6_len = sizeof(struct sockaddr_in6);
183 all1_sa = lo_sa;
184 lo_sa.sin6_addr = in6addr_loopback;
185 all1_sa.sin6_addr = in6mask128;
186
187 /* So we add or remove static loopback entry, here. */
188 rtrequest(cmd, ifa->ifa_addr,
189 (struct sockaddr *)&lo_sa,
190 (struct sockaddr *)&all1_sa,
191 RTF_UP|RTF_HOST, &nrt);
192
193 /*
194 * Make sure rt_ifa be equal to IFA, the second argument of the
195 * function.
196 * We need this because when we refer rt_ifa->ia6_flags in ip6_input,
197 * we assume that the rt_ifa points to the address instead of the
198 * loopback address.
199 */
200 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
201 IFAFREE(nrt->rt_ifa);
202 IFAREF(ifa);
203 nrt->rt_ifa = ifa;
204 }
205 if (nrt)
206 nrt->rt_refcnt--;
207 }
208
209 /*
210 * Add ownaddr as loopback rtentry, if necessary(ex. on p2p link).
211 * Because, KAME needs loopback rtentry for ownaddr check in
212 * ip6_input().
213 */
214 static void
215 in6_ifaddloop(struct ifaddr *ifa)
216 {
217 if (!in6_is_ifloop_auto(ifa)) {
218 struct rtentry *rt;
219
220 /* If there is no loopback entry, allocate one. */
221 rt = rtalloc1(ifa->ifa_addr, 0);
222 if (rt == 0 || (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
223 in6_ifloop_request(RTM_ADD, ifa);
224 if (rt)
225 rt->rt_refcnt--;
226 }
227 }
228
229 /*
230 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
231 * if it exists.
232 */
233 static void
234 in6_ifremloop(struct ifaddr *ifa)
235 {
236 if (!in6_is_ifloop_auto(ifa)) {
237 struct in6_ifaddr *ia;
238 int ia_count = 0;
239
240 /* If only one ifa for the loopback entry, delete it. */
241 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
242 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa),
243 &ia->ia_addr.sin6_addr)) {
244 ia_count++;
245 if (ia_count > 1)
246 break;
247 }
248 }
249 if (ia_count == 1)
250 in6_ifloop_request(RTM_DELETE, ifa);
251 }
252 }
253
254 int
255 in6_ifindex2scopeid(idx)
256 int idx;
257 {
258 struct ifnet *ifp;
259 struct ifaddr *ifa;
260 struct sockaddr_in6 *sin6;
261
262 if (idx < 0 || if_index < idx)
263 return -1;
264 ifp = ifindex2ifnet[idx];
265
266 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
267 {
268 if (ifa->ifa_addr->sa_family != AF_INET6)
269 continue;
270 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
271 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
272 return sin6->sin6_scope_id & 0xffff;
273 }
274
275 return -1;
276 }
277
278 int
279 in6_mask2len(mask)
280 struct in6_addr *mask;
281 {
282 int x, y;
283
284 for (x = 0; x < sizeof(*mask); x++) {
285 if (mask->s6_addr8[x] != 0xff)
286 break;
287 }
288 y = 0;
289 if (x < sizeof(*mask)) {
290 for (y = 0; y < 8; y++) {
291 if ((mask->s6_addr8[x] & (0x80 >> y)) == 0)
292 break;
293 }
294 }
295 return x * 8 + y;
296 }
297
298 void
299 in6_len2mask(mask, len)
300 struct in6_addr *mask;
301 int len;
302 {
303 int i;
304
305 bzero(mask, sizeof(*mask));
306 for (i = 0; i < len / 8; i++)
307 mask->s6_addr8[i] = 0xff;
308 if (len % 8)
309 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
310 }
311
312 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
313 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
314
315 int
316 in6_control(so, cmd, data, ifp, p)
317 struct socket *so;
318 u_long cmd;
319 caddr_t data;
320 struct ifnet *ifp;
321 struct proc *p;
322 {
323 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
324 struct in6_ifaddr *ia, *oia;
325 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
326 struct sockaddr_in6 oldaddr;
327 #ifdef COMPAT_IN6IFIOCTL
328 struct sockaddr_in6 net;
329 #endif
330 int error = 0, hostIsNew, prefixIsNew;
331 int newifaddr;
332 time_t time_second = (time_t)time.tv_sec;
333 int privileged;
334
335 privileged = 0;
336 if (p && !suser(p->p_ucred, &p->p_acflag))
337 privileged++;
338
339 /*
340 * xxx should prevent processes for link-local addresses?
341 */
342 #if NGIF > 0
343 if (ifp && ifp->if_type == IFT_GIF) {
344 switch (cmd) {
345 case SIOCSIFPHYADDR_IN6:
346 if (!privileged)
347 return(EPERM);
348 /*fall through*/
349 case SIOCGIFPSRCADDR_IN6:
350 case SIOCGIFPDSTADDR_IN6:
351 return gif_ioctl(ifp, cmd, data);
352 }
353 }
354 #endif
355 switch (cmd) {
356 case SIOCGETSGCNT_IN6:
357 case SIOCGETMIFCNT_IN6:
358 return (mrt6_ioctl(cmd, data));
359 }
360
361 if (ifp == NULL)
362 return(EOPNOTSUPP);
363
364 switch (cmd) {
365 case SIOCSNDFLUSH_IN6:
366 case SIOCSPFXFLUSH_IN6:
367 case SIOCSRTRFLUSH_IN6:
368 case SIOCSDEFIFACE_IN6:
369 if (!privileged)
370 return(EPERM);
371 /*fall through*/
372 case SIOCGIFINFO_IN6:
373 case SIOCGDRLST_IN6:
374 case SIOCGPRLST_IN6:
375 case SIOCGNBRINFO_IN6:
376 case SIOCGDEFIFACE_IN6:
377 return(nd6_ioctl(cmd, data, ifp));
378 }
379
380 switch (cmd) {
381 case SIOCSIFPREFIX_IN6:
382 case SIOCDIFPREFIX_IN6:
383 case SIOCAIFPREFIX_IN6:
384 case SIOCCIFPREFIX_IN6:
385 case SIOCSGIFPREFIX_IN6:
386 if (!privileged)
387 return(EPERM);
388 /*fall through*/
389 case SIOCGIFPREFIX_IN6:
390 return(in6_prefix_ioctl(so, cmd, data, ifp));
391 }
392
393 switch (cmd) {
394 case SIOCALIFADDR:
395 case SIOCDLIFADDR:
396 if (!privileged)
397 return(EPERM);
398 /*fall through*/
399 case SIOCGLIFADDR:
400 return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
401 }
402
403 /*
404 * Find address for this interface, if it exists.
405 */
406 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
407 struct sockaddr_in6 *sa6 =
408 (struct sockaddr_in6 *)&ifra->ifra_addr;
409
410 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
411 if (sa6->sin6_addr.s6_addr16[1] == 0) {
412 /* interface ID is not embedded by the user */
413 sa6->sin6_addr.s6_addr16[1] =
414 htons(ifp->if_index);
415 } else if (sa6->sin6_addr.s6_addr16[1] !=
416 htons(ifp->if_index)) {
417 return(EINVAL); /* ifid is contradict */
418 }
419 if (sa6->sin6_scope_id) {
420 if (sa6->sin6_scope_id !=
421 (u_int32_t)ifp->if_index)
422 return(EINVAL);
423 sa6->sin6_scope_id = 0; /* XXX: good way? */
424 }
425 }
426 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
427 }
428
429 switch (cmd) {
430
431 case SIOCDIFADDR_IN6:
432 /*
433 * for IPv4, we look for existing in6_ifaddr here to allow
434 * "ifconfig if0 delete" to remove first IPv4 address on the
435 * interface. For IPv6, as the spec allow multiple interface
436 * address from the day one, we consider "remove the first one"
437 * semantics to be not preferrable.
438 */
439 if (ia == NULL)
440 return(EADDRNOTAVAIL);
441 /* FALLTHROUGH */
442 case SIOCAIFADDR_IN6:
443 case SIOCSIFADDR_IN6:
444 #ifdef COMPAT_IN6IFIOCTL
445 case SIOCSIFDSTADDR_IN6:
446 case SIOCSIFNETMASK_IN6:
447 /*
448 * Since IPv6 allows a node to assign multiple addresses
449 * on a single interface, SIOCSIFxxx ioctls are not suitable
450 * and should be unused.
451 */
452 #endif
453 if (ifra->ifra_addr.sin6_family != AF_INET6)
454 return(EAFNOSUPPORT);
455 if (!privileged)
456 return(EPERM);
457 if (ia == NULL) {
458 ia = (struct in6_ifaddr *)
459 malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
460 if (ia == NULL)
461 return (ENOBUFS);
462 bzero((caddr_t)ia, sizeof(*ia));
463 /* Initialize the address and masks */
464 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
465 ia->ia_addr.sin6_family = AF_INET6;
466 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
467 if (ifp->if_flags & IFF_POINTOPOINT) {
468 ia->ia_ifa.ifa_dstaddr
469 = (struct sockaddr *)&ia->ia_dstaddr;
470 ia->ia_dstaddr.sin6_family = AF_INET6;
471 ia->ia_dstaddr.sin6_len = sizeof(ia->ia_dstaddr);
472 } else {
473 ia->ia_ifa.ifa_dstaddr = NULL;
474 bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr));
475 }
476 ia->ia_ifa.ifa_netmask
477 = (struct sockaddr *)&ia->ia_prefixmask;
478
479 ia->ia_ifp = ifp;
480 if ((oia = in6_ifaddr) != NULL) {
481 for ( ; oia->ia_next; oia = oia->ia_next)
482 continue;
483 oia->ia_next = ia;
484 } else
485 in6_ifaddr = ia;
486 IFAREF(&ia->ia_ifa);
487
488 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
489 ifa_list);
490 IFAREF(&ia->ia_ifa);
491
492 newifaddr = 1;
493 } else
494 newifaddr = 0;
495
496 if (cmd == SIOCAIFADDR_IN6) {
497 /* sanity for overflow - beware unsigned */
498 struct in6_addrlifetime *lt;
499 lt = &ifra->ifra_lifetime;
500 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
501 && lt->ia6t_vltime + time_second < time_second) {
502 return EINVAL;
503 }
504 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
505 && lt->ia6t_pltime + time_second < time_second) {
506 return EINVAL;
507 }
508 }
509 break;
510
511 case SIOCGIFADDR_IN6:
512 /* This interface is basically deprecated. use SIOCGIFCONF. */
513 /* fall through */
514 case SIOCGIFAFLAG_IN6:
515 case SIOCGIFNETMASK_IN6:
516 case SIOCGIFDSTADDR_IN6:
517 case SIOCGIFALIFETIME_IN6:
518 /* must think again about its semantics */
519 if (ia == NULL)
520 return(EADDRNOTAVAIL);
521 break;
522 case SIOCSIFALIFETIME_IN6:
523 {
524 struct in6_addrlifetime *lt;
525
526 if (!privileged)
527 return(EPERM);
528 if (ia == NULL)
529 return(EADDRNOTAVAIL);
530 /* sanity for overflow - beware unsigned */
531 lt = &ifr->ifr_ifru.ifru_lifetime;
532 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
533 && lt->ia6t_vltime + time_second < time_second) {
534 return EINVAL;
535 }
536 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
537 && lt->ia6t_pltime + time_second < time_second) {
538 return EINVAL;
539 }
540 break;
541 }
542 }
543
544 switch (cmd) {
545
546 case SIOCGIFADDR_IN6:
547 ifr->ifr_addr = ia->ia_addr;
548 break;
549
550 case SIOCGIFDSTADDR_IN6:
551 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
552 return(EINVAL);
553 ifr->ifr_dstaddr = ia->ia_dstaddr;
554 break;
555
556 case SIOCGIFNETMASK_IN6:
557 ifr->ifr_addr = ia->ia_prefixmask;
558 break;
559
560 case SIOCGIFAFLAG_IN6:
561 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
562 break;
563
564 case SIOCGIFSTAT_IN6:
565 if (ifp == NULL)
566 return EINVAL;
567 if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax
568 || in6_ifstat[ifp->if_index] == NULL) {
569 /* return EAFNOSUPPORT? */
570 bzero(&ifr->ifr_ifru.ifru_stat,
571 sizeof(ifr->ifr_ifru.ifru_stat));
572 } else
573 ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index];
574 break;
575
576 case SIOCGIFSTAT_ICMP6:
577 if (ifp == NULL)
578 return EINVAL;
579 if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax ||
580 icmp6_ifstat[ifp->if_index] == NULL) {
581 /* return EAFNOSUPPORT? */
582 bzero(&ifr->ifr_ifru.ifru_stat,
583 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
584 } else
585 ifr->ifr_ifru.ifru_icmp6stat =
586 *icmp6_ifstat[ifp->if_index];
587 break;
588
589 #ifdef COMPAT_IN6IFIOCTL /* should be unused */
590 case SIOCSIFDSTADDR_IN6:
591 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
592 return(EINVAL);
593 oldaddr = ia->ia_dstaddr;
594 ia->ia_dstaddr = ifr->ifr_dstaddr;
595
596 /* link-local index check */
597 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
598 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
599 /* interface ID is not embedded by the user */
600 ia->ia_dstaddr.sin6_addr.s6_addr16[1]
601 = htons(ifp->if_index);
602 } else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
603 htons(ifp->if_index)) {
604 ia->ia_dstaddr = oldaddr;
605 return(EINVAL); /* ifid is contradict */
606 }
607 }
608
609 if (ifp->if_ioctl && (error = (ifp->if_ioctl)
610 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
611 ia->ia_dstaddr = oldaddr;
612 return(error);
613 }
614 if (ia->ia_flags & IFA_ROUTE) {
615 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
616 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
617 ia->ia_ifa.ifa_dstaddr =
618 (struct sockaddr *)&ia->ia_dstaddr;
619 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
620 }
621 break;
622
623 #endif
624 case SIOCGIFALIFETIME_IN6:
625 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
626 break;
627
628 case SIOCSIFALIFETIME_IN6:
629 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
630 /* for sanity */
631 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
632 ia->ia6_lifetime.ia6t_expire =
633 time_second + ia->ia6_lifetime.ia6t_vltime;
634 } else
635 ia->ia6_lifetime.ia6t_expire = 0;
636 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
637 ia->ia6_lifetime.ia6t_preferred =
638 time_second + ia->ia6_lifetime.ia6t_pltime;
639 } else
640 ia->ia6_lifetime.ia6t_preferred = 0;
641 break;
642
643 case SIOCSIFADDR_IN6:
644 error = in6_ifinit(ifp, ia, &ifr->ifr_addr, 1);
645 #if 0
646 /*
647 * the code chokes if we are to assign multiple addresses with
648 * the same address prefix (rtinit() will return EEXIST, which
649 * is not fatal actually). we will get memory leak if we
650 * don't do it.
651 * -> we may want to hide EEXIST from rtinit().
652 */
653 undo:
654 if (error && newifaddr) {
655 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
656 IFAFREE(&ia->ia_ifa);
657
658 oia = ia;
659 if (oia == (ia = in6_ifaddr))
660 in6_ifaddr = ia->ia_next;
661 else {
662 while (ia->ia_next && (ia->ia_next != oia))
663 ia = ia->ia_next;
664 if (ia->ia_next)
665 ia->ia_next = oia->ia_next;
666 else {
667 printf("Didn't unlink in6_ifaddr "
668 "from list\n");
669 }
670 }
671 IFAFREE(&ia->ia_ifa);
672 }
673 #endif
674 return error;
675
676 #ifdef COMPAT_IN6IFIOCTL /* XXX should be unused */
677 case SIOCSIFNETMASK_IN6:
678 ia->ia_prefixmask = ifr->ifr_addr;
679 bzero(&net, sizeof(net));
680 net.sin6_len = sizeof(struct sockaddr_in6);
681 net.sin6_family = AF_INET6;
682 net.sin6_port = htons(0);
683 net.sin6_flowinfo = htonl(0);
684 net.sin6_addr.s6_addr32[0]
685 = ia->ia_addr.sin6_addr.s6_addr32[0] &
686 ia->ia_prefixmask.sin6_addr.s6_addr32[0];
687 net.sin6_addr.s6_addr32[1]
688 = ia->ia_addr.sin6_addr.s6_addr32[1] &
689 ia->ia_prefixmask.sin6_addr.s6_addr32[1];
690 net.sin6_addr.s6_addr32[2]
691 = ia->ia_addr.sin6_addr.s6_addr32[2] &
692 ia->ia_prefixmask.sin6_addr.s6_addr32[2];
693 net.sin6_addr.s6_addr32[3]
694 = ia->ia_addr.sin6_addr.s6_addr32[3] &
695 ia->ia_prefixmask.sin6_addr.s6_addr32[3];
696 ia->ia_net = net;
697 break;
698 #endif
699
700 case SIOCAIFADDR_IN6:
701 prefixIsNew = 0;
702 hostIsNew = 1;
703
704 if (ifra->ifra_addr.sin6_len == 0) {
705 ifra->ifra_addr = ia->ia_addr;
706 hostIsNew = 0;
707 } else if (IN6_ARE_ADDR_EQUAL(&ifra->ifra_addr.sin6_addr,
708 &ia->ia_addr.sin6_addr))
709 hostIsNew = 0;
710
711 /* Validate address families: */
712 /*
713 * The destination address for a p2p link must have a family
714 * of AF_UNSPEC or AF_INET6.
715 */
716 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
717 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
718 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
719 return(EAFNOSUPPORT);
720 /*
721 * The prefixmask must have a family of AF_UNSPEC or AF_INET6.
722 */
723 if (ifra->ifra_prefixmask.sin6_family != AF_INET6 &&
724 ifra->ifra_prefixmask.sin6_family != AF_UNSPEC)
725 return(EAFNOSUPPORT);
726
727 if (ifra->ifra_prefixmask.sin6_len) {
728 in6_ifscrub(ifp, ia);
729 ia->ia_prefixmask = ifra->ifra_prefixmask;
730 prefixIsNew = 1;
731 }
732 if ((ifp->if_flags & IFF_POINTOPOINT) &&
733 (ifra->ifra_dstaddr.sin6_family == AF_INET6)) {
734 in6_ifscrub(ifp, ia);
735 oldaddr = ia->ia_dstaddr;
736 ia->ia_dstaddr = ifra->ifra_dstaddr;
737 /* link-local index check: should be a separate function? */
738 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
739 if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
740 /*
741 * interface ID is not embedded by
742 * the user
743 */
744 ia->ia_dstaddr.sin6_addr.s6_addr16[1]
745 = htons(ifp->if_index);
746 } else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
747 htons(ifp->if_index)) {
748 ia->ia_dstaddr = oldaddr;
749 return(EINVAL); /* ifid is contradict */
750 }
751 }
752 prefixIsNew = 1; /* We lie; but effect's the same */
753 }
754 if (hostIsNew || prefixIsNew) {
755 error = in6_ifinit(ifp, ia, &ifra->ifra_addr, 0);
756 #if 0
757 if (error)
758 goto undo;
759 #endif
760 }
761 if (hostIsNew && (ifp->if_flags & IFF_MULTICAST)) {
762 int error_local = 0;
763
764 /*
765 * join solicited multicast addr for new host id
766 */
767 struct in6_addr llsol;
768 bzero(&llsol, sizeof(struct in6_addr));
769 llsol.s6_addr16[0] = htons(0xff02);
770 llsol.s6_addr16[1] = htons(ifp->if_index);
771 llsol.s6_addr32[1] = 0;
772 llsol.s6_addr32[2] = htonl(1);
773 llsol.s6_addr32[3] =
774 ifra->ifra_addr.sin6_addr.s6_addr32[3];
775 llsol.s6_addr8[12] = 0xff;
776 (void)in6_addmulti(&llsol, ifp, &error_local);
777 if (error == 0)
778 error = error_local;
779 }
780
781 ia->ia6_flags = ifra->ifra_flags;
782 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
783
784 ia->ia6_lifetime = ifra->ifra_lifetime;
785 /* for sanity */
786 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
787 ia->ia6_lifetime.ia6t_expire =
788 time_second + ia->ia6_lifetime.ia6t_vltime;
789 } else
790 ia->ia6_lifetime.ia6t_expire = 0;
791 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
792 ia->ia6_lifetime.ia6t_preferred =
793 time_second + ia->ia6_lifetime.ia6t_pltime;
794 } else
795 ia->ia6_lifetime.ia6t_preferred = 0;
796
797 /*
798 * Perform DAD, if needed.
799 * XXX It may be of use, if we can administratively
800 * disable DAD.
801 */
802 switch (ifp->if_type) {
803 case IFT_ARCNET:
804 case IFT_ETHER:
805 case IFT_FDDI:
806 #if 0
807 case IFT_ATM:
808 case IFT_SLIP:
809 case IFT_PPP:
810 #endif
811 ia->ia6_flags |= IN6_IFF_TENTATIVE;
812 nd6_dad_start(&ia->ia_ifa, NULL);
813 break;
814 case IFT_FAITH:
815 case IFT_GIF:
816 case IFT_LOOP:
817 default:
818 break;
819 }
820
821 if (hostIsNew) {
822 int iilen;
823 int error_local = 0;
824
825 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) -
826 in6_mask2len(&ia->ia_prefixmask.sin6_addr);
827 error_local = in6_prefix_add_ifid(iilen, ia);
828 if (error == 0)
829 error = error_local;
830 }
831
832 return(error);
833
834 case SIOCDIFADDR_IN6:
835 in6_purgeaddr(&ia->ia_ifa, ifp);
836 break;
837
838 default:
839 if (ifp == NULL || ifp->if_ioctl == 0)
840 return(EOPNOTSUPP);
841 return((*ifp->if_ioctl)(ifp, cmd, data));
842 }
843 return(0);
844 }
845
846 void
847 in6_purgeaddr(ifa, ifp)
848 struct ifaddr *ifa;
849 struct ifnet *ifp;
850 {
851 struct in6_ifaddr *oia, *ia = (void *) 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 if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
1186 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
1187 else
1188 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
1189 ia->ia_flags &= ~IFA_ROUTE;
1190
1191 /* Remove ownaddr's loopback rtentry, if it exists. */
1192 in6_ifremloop(&(ia->ia_ifa));
1193 }
1194
1195 /*
1196 * Initialize an interface's intetnet6 address
1197 * and routing table entry.
1198 */
1199 int
1200 in6_ifinit(ifp, ia, sin6, scrub)
1201 struct ifnet *ifp;
1202 struct in6_ifaddr *ia;
1203 struct sockaddr_in6 *sin6;
1204 int scrub;
1205 {
1206 struct sockaddr_in6 oldaddr;
1207 int error, flags = RTF_UP;
1208 int s = splimp();
1209
1210 oldaddr = ia->ia_addr;
1211 ia->ia_addr = *sin6;
1212 /*
1213 * Give the interface a chance to initialize
1214 * if this is its first address,
1215 * and to validate the address if necessary.
1216 */
1217 if (ifp->if_ioctl &&
1218 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1219 splx(s);
1220 ia->ia_addr = oldaddr;
1221 return(error);
1222 }
1223
1224 switch (ifp->if_type) {
1225 case IFT_ARCNET:
1226 case IFT_ETHER:
1227 case IFT_FDDI:
1228 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1229 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1230 break;
1231 case IFT_PPP:
1232 ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest;
1233 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1234 break;
1235 }
1236
1237 splx(s);
1238 if (scrub) {
1239 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1240 in6_ifscrub(ifp, ia);
1241 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1242 }
1243 /* xxx
1244 * in_socktrim
1245 */
1246 /*
1247 * Add route for the network.
1248 */
1249 ia->ia_ifa.ifa_metric = ifp->if_metric;
1250 if (ifp->if_flags & IFF_LOOPBACK) {
1251 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
1252 flags |= RTF_HOST;
1253 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1254 if (ia->ia_dstaddr.sin6_family != AF_INET6)
1255 return(0);
1256 flags |= RTF_HOST;
1257 }
1258 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
1259 ia->ia_flags |= IFA_ROUTE;
1260
1261 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
1262 in6_ifaddloop(&(ia->ia_ifa));
1263
1264 if (ifp->if_flags & IFF_MULTICAST)
1265 in6_restoremkludge(ia, ifp);
1266
1267 return(error);
1268 }
1269
1270 /*
1271 * Multicast address kludge:
1272 * If there were any multicast addresses attached to this interface address,
1273 * either move them to another address on this interface, or save them until
1274 * such time as this interface is reconfigured for IPv6.
1275 */
1276 void
1277 in6_savemkludge(oia)
1278 struct in6_ifaddr *oia;
1279 {
1280 struct in6_ifaddr *ia;
1281 struct in6_multi *in6m, *next;
1282
1283 IFP_TO_IA6(oia->ia_ifp, ia);
1284 if (ia) { /* there is another address */
1285 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1286 next = in6m->in6m_entry.le_next;
1287 IFAFREE(&in6m->in6m_ia->ia_ifa);
1288 IFAREF(&ia->ia_ifa);
1289 in6m->in6m_ia = ia;
1290 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1291 }
1292 } else { /* last address on this if deleted, save */
1293 struct multi6_kludge *mk;
1294
1295 mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
1296
1297 LIST_INIT(&mk->mk_head);
1298 mk->mk_ifp = oia->ia_ifp;
1299
1300 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1301 next = in6m->in6m_entry.le_next;
1302 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1303 in6m->in6m_ia = NULL;
1304 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
1305 }
1306
1307 if (mk->mk_head.lh_first != NULL) {
1308 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
1309 } else {
1310 FREE(mk, M_IPMADDR);
1311 }
1312 }
1313 }
1314
1315 /*
1316 * Continuation of multicast address hack:
1317 * If there was a multicast group list previously saved for this interface,
1318 * then we re-attach it to the first address configured on the i/f.
1319 */
1320 void
1321 in6_restoremkludge(ia, ifp)
1322 struct in6_ifaddr *ia;
1323 struct ifnet *ifp;
1324 {
1325 struct multi6_kludge *mk;
1326
1327 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1328 if (mk->mk_ifp == ifp) {
1329 struct in6_multi *in6m, *next;
1330
1331 for (in6m = mk->mk_head.lh_first; in6m; in6m = next){
1332 next = in6m->in6m_entry.le_next;
1333 in6m->in6m_ia = ia;
1334 IFAREF(&ia->ia_ifa);
1335 LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
1336 in6m, in6m_entry);
1337 }
1338 LIST_REMOVE(mk, mk_entry);
1339 free(mk, M_IPMADDR);
1340 break;
1341 }
1342 }
1343 }
1344
1345 void
1346 in6_purgemkludge(ifp)
1347 struct ifnet *ifp;
1348 {
1349 struct multi6_kludge *mk;
1350 struct in6_multi *in6m;
1351
1352 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1353 if (mk->mk_ifp != ifp)
1354 continue;
1355
1356 /* leave from all multicast groups joined */
1357 while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL)
1358 in6_delmulti(in6m);
1359 LIST_REMOVE(mk, mk_entry);
1360 free(mk, M_IPMADDR);
1361 break;
1362 }
1363 }
1364
1365 /*
1366 * Add an address to the list of IP6 multicast addresses for a
1367 * given interface.
1368 */
1369 struct in6_multi *
1370 in6_addmulti(maddr6, ifp, errorp)
1371 register struct in6_addr *maddr6;
1372 register struct ifnet *ifp;
1373 int *errorp;
1374 {
1375 struct in6_ifaddr *ia;
1376 struct in6_ifreq ifr;
1377 struct in6_multi *in6m;
1378 int s = splsoftnet();
1379
1380 *errorp = 0;
1381 /*
1382 * See if address already in list.
1383 */
1384 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
1385 if (in6m != NULL) {
1386 /*
1387 * Found it; just increment the refrence count.
1388 */
1389 in6m->in6m_refcount++;
1390 } else {
1391 /*
1392 * New address; allocate a new multicast record
1393 * and link it into the interface's multicast list.
1394 */
1395 in6m = (struct in6_multi *)
1396 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1397 if (in6m == NULL) {
1398 splx(s);
1399 *errorp = ENOBUFS;
1400 return(NULL);
1401 }
1402 in6m->in6m_addr = *maddr6;
1403 in6m->in6m_ifp = ifp;
1404 in6m->in6m_refcount = 1;
1405 IFP_TO_IA6(ifp, ia);
1406 if (ia == NULL) {
1407 free(in6m, M_IPMADDR);
1408 splx(s);
1409 *errorp = EADDRNOTAVAIL; /* appropriate? */
1410 return(NULL);
1411 }
1412 in6m->in6m_ia = ia;
1413 IFAREF(&ia->ia_ifa); /* gain a reference */
1414 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1415
1416 /*
1417 * Ask the network driver to update its multicast reception
1418 * filter appropriately for the new address.
1419 */
1420 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1421 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1422 ifr.ifr_addr.sin6_family = AF_INET6;
1423 ifr.ifr_addr.sin6_addr = *maddr6;
1424 if (ifp->if_ioctl == NULL)
1425 *errorp = ENXIO; /* XXX: appropriate? */
1426 else
1427 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
1428 (caddr_t)&ifr);
1429 if (*errorp) {
1430 LIST_REMOVE(in6m, in6m_entry);
1431 free(in6m, M_IPMADDR);
1432 splx(s);
1433 return(NULL);
1434 }
1435 /*
1436 * Let MLD6 know that we have joined a new IP6 multicast
1437 * group.
1438 */
1439 mld6_start_listening(in6m);
1440 }
1441 splx(s);
1442 return(in6m);
1443 }
1444
1445 /*
1446 * Delete a multicast address record.
1447 */
1448 void
1449 in6_delmulti(in6m)
1450 struct in6_multi *in6m;
1451 {
1452 struct in6_ifreq ifr;
1453 int s = splsoftnet();
1454
1455 if (--in6m->in6m_refcount == 0) {
1456 /*
1457 * No remaining claims to this record; let MLD6 know
1458 * that we are leaving the multicast group.
1459 */
1460 mld6_stop_listening(in6m);
1461
1462 /*
1463 * Unlink from list.
1464 */
1465 LIST_REMOVE(in6m, in6m_entry);
1466 if (in6m->in6m_ia)
1467 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1468
1469 /*
1470 * Notify the network driver to update its multicast
1471 * reception filter.
1472 */
1473 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1474 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1475 ifr.ifr_addr.sin6_family = AF_INET6;
1476 ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
1477 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
1478 SIOCDELMULTI, (caddr_t)&ifr);
1479 free(in6m, M_IPMADDR);
1480 }
1481 splx(s);
1482 }
1483
1484 /*
1485 * Find an IPv6 interface link-local address specific to an interface.
1486 */
1487 struct in6_ifaddr *
1488 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1489 struct ifnet *ifp;
1490 int ignoreflags;
1491 {
1492 register struct ifaddr *ifa;
1493
1494 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1495 {
1496 if (ifa->ifa_addr == NULL)
1497 continue; /* just for safety */
1498 if (ifa->ifa_addr->sa_family != AF_INET6)
1499 continue;
1500 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1501 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1502 ignoreflags) != 0)
1503 continue;
1504 break;
1505 }
1506 }
1507
1508 return((struct in6_ifaddr *)ifa);
1509 }
1510
1511
1512 /*
1513 * find the internet address corresponding to a given interface and address.
1514 */
1515 struct in6_ifaddr *
1516 in6ifa_ifpwithaddr(ifp, addr)
1517 struct ifnet *ifp;
1518 struct in6_addr *addr;
1519 {
1520 register struct ifaddr *ifa;
1521
1522 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1523 {
1524 if (ifa->ifa_addr == NULL)
1525 continue; /* just for safety */
1526 if (ifa->ifa_addr->sa_family != AF_INET6)
1527 continue;
1528 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1529 break;
1530 }
1531
1532 return((struct in6_ifaddr *)ifa);
1533 }
1534
1535 /*
1536 * Convert IP6 address to printable (loggable) representation.
1537 */
1538 static char digits[] = "0123456789abcdef";
1539 static int ip6round = 0;
1540 char *
1541 ip6_sprintf(addr)
1542 register struct in6_addr *addr;
1543 {
1544 static char ip6buf[8][48];
1545 register int i;
1546 register char *cp;
1547 register u_short *a = (u_short *)addr;
1548 register u_char *d;
1549 int dcolon = 0;
1550
1551 ip6round = (ip6round + 1) & 7;
1552 cp = ip6buf[ip6round];
1553
1554 for (i = 0; i < 8; i++) {
1555 if (dcolon == 1) {
1556 if (*a == 0) {
1557 if (i == 7)
1558 *cp++ = ':';
1559 a++;
1560 continue;
1561 } else
1562 dcolon = 2;
1563 }
1564 if (*a == 0) {
1565 if (dcolon == 0 && *(a + 1) == 0) {
1566 if (i == 0)
1567 *cp++ = ':';
1568 *cp++ = ':';
1569 dcolon = 1;
1570 } else {
1571 *cp++ = '0';
1572 *cp++ = ':';
1573 }
1574 a++;
1575 continue;
1576 }
1577 d = (u_char *)a;
1578 *cp++ = digits[*d >> 4];
1579 *cp++ = digits[*d++ & 0xf];
1580 *cp++ = digits[*d >> 4];
1581 *cp++ = digits[*d & 0xf];
1582 *cp++ = ':';
1583 a++;
1584 }
1585 *--cp = 0;
1586 return(ip6buf[ip6round]);
1587 }
1588
1589 int
1590 in6_localaddr(in6)
1591 struct in6_addr *in6;
1592 {
1593 struct in6_ifaddr *ia;
1594
1595 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1596 return 1;
1597
1598 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1599 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1600 &ia->ia_prefixmask.sin6_addr))
1601 return 1;
1602
1603 return (0);
1604 }
1605
1606 /*
1607 * Get a scope of the address. Node-local, link-local, site-local or global.
1608 */
1609 int
1610 in6_addrscope (addr)
1611 struct in6_addr *addr;
1612 {
1613 int scope;
1614
1615 if (addr->s6_addr8[0] == 0xfe) {
1616 scope = addr->s6_addr8[1] & 0xc0;
1617
1618 switch (scope) {
1619 case 0x80:
1620 return IPV6_ADDR_SCOPE_LINKLOCAL;
1621 break;
1622 case 0xc0:
1623 return IPV6_ADDR_SCOPE_SITELOCAL;
1624 break;
1625 default:
1626 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
1627 break;
1628 }
1629 }
1630
1631
1632 if (addr->s6_addr8[0] == 0xff) {
1633 scope = addr->s6_addr8[1] & 0x0f;
1634
1635 /*
1636 * due to other scope such as reserved,
1637 * return scope doesn't work.
1638 */
1639 switch (scope) {
1640 case IPV6_ADDR_SCOPE_NODELOCAL:
1641 return IPV6_ADDR_SCOPE_NODELOCAL;
1642 break;
1643 case IPV6_ADDR_SCOPE_LINKLOCAL:
1644 return IPV6_ADDR_SCOPE_LINKLOCAL;
1645 break;
1646 case IPV6_ADDR_SCOPE_SITELOCAL:
1647 return IPV6_ADDR_SCOPE_SITELOCAL;
1648 break;
1649 default:
1650 return IPV6_ADDR_SCOPE_GLOBAL;
1651 break;
1652 }
1653 }
1654
1655 if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) {
1656 if (addr->s6_addr8[15] == 1) /* loopback */
1657 return IPV6_ADDR_SCOPE_NODELOCAL;
1658 if (addr->s6_addr8[15] == 0) /* unspecified */
1659 return IPV6_ADDR_SCOPE_LINKLOCAL;
1660 }
1661
1662 return IPV6_ADDR_SCOPE_GLOBAL;
1663 }
1664
1665 int
1666 in6_addr2scopeid(ifp, addr)
1667 struct ifnet *ifp; /* must not be NULL */
1668 struct in6_addr *addr; /* must not be NULL */
1669 {
1670 int scope = in6_addrscope(addr);
1671
1672 switch(scope) {
1673 case IPV6_ADDR_SCOPE_NODELOCAL:
1674 return(-1); /* XXX: is this an appropriate value? */
1675
1676 case IPV6_ADDR_SCOPE_LINKLOCAL:
1677 /* XXX: we do not distinguish between a link and an I/F. */
1678 return(ifp->if_index);
1679
1680 case IPV6_ADDR_SCOPE_SITELOCAL:
1681 return(0); /* XXX: invalid. */
1682
1683 default:
1684 return(0); /* XXX: treat as global. */
1685 }
1686 }
1687
1688 /*
1689 * return length of part which dst and src are equal
1690 * hard coding...
1691 */
1692
1693 int
1694 in6_matchlen(src, dst)
1695 struct in6_addr *src, *dst;
1696 {
1697 int match = 0;
1698 u_char *s = (u_char *)src, *d = (u_char *)dst;
1699 u_char *lim = s + 16, r;
1700
1701 while (s < lim)
1702 if ((r = (*d++ ^ *s++)) != 0) {
1703 while (r < 128) {
1704 match++;
1705 r <<= 1;
1706 }
1707 break;
1708 } else
1709 match += 8;
1710 return match;
1711 }
1712
1713 int
1714 in6_are_prefix_equal(p1, p2, len)
1715 struct in6_addr *p1, *p2;
1716 int len;
1717 {
1718 int bytelen, bitlen;
1719
1720 /* sanity check */
1721 if (0 > len || len > 128) {
1722 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1723 len);
1724 return(0);
1725 }
1726
1727 bytelen = len / 8;
1728 bitlen = len % 8;
1729
1730 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1731 return(0);
1732 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1733 p2->s6_addr[bytelen] >> (8 - bitlen))
1734 return(0);
1735
1736 return(1);
1737 }
1738
1739 void
1740 in6_prefixlen2mask(maskp, len)
1741 struct in6_addr *maskp;
1742 int len;
1743 {
1744 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1745 int bytelen, bitlen, i;
1746
1747 /* sanity check */
1748 if (0 > len || len > 128) {
1749 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1750 len);
1751 return;
1752 }
1753
1754 bzero(maskp, sizeof(*maskp));
1755 bytelen = len / 8;
1756 bitlen = len % 8;
1757 for (i = 0; i < bytelen; i++)
1758 maskp->s6_addr[i] = 0xff;
1759 if (bitlen)
1760 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1761 }
1762
1763 /*
1764 * return the best address out of the same scope
1765 */
1766 struct in6_ifaddr *
1767 in6_ifawithscope(oifp, dst)
1768 register struct ifnet *oifp;
1769 register struct in6_addr *dst;
1770 {
1771 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
1772 int blen = -1;
1773 struct ifaddr *ifa;
1774 struct ifnet *ifp;
1775 struct in6_ifaddr *ifa_best = NULL;
1776
1777 if (oifp == NULL) {
1778 printf("in6_ifawithscope: output interface is not specified\n");
1779 return(NULL);
1780 }
1781
1782 /*
1783 * We search for all addresses on all interfaces from the beginning.
1784 * Comparing an interface with the outgoing interface will be done
1785 * only at the final stage of tiebreaking.
1786 */
1787 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1788 {
1789 /*
1790 * We can never take an address that breaks the scope zone
1791 * of the destination.
1792 */
1793 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
1794 continue;
1795
1796 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1797 ifa = ifa->ifa_list.tqe_next)
1798 {
1799 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
1800
1801 if (ifa->ifa_addr->sa_family != AF_INET6)
1802 continue;
1803
1804 src_scope = in6_addrscope(IFA_IN6(ifa));
1805
1806 #ifdef ADDRSELECT_DEBUG /* should be removed after stabilization */
1807 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
1808 printf("in6_ifawithscope: dst=%s bestaddr=%s, "
1809 "newaddr=%s, scope=%x, dcmp=%d, bcmp=%d, "
1810 "matchlen=%d, flgs=%x\n",
1811 ip6_sprintf(dst),
1812 ifa_best ? ip6_sprintf(&ifa_best->ia_addr.sin6_addr) : "none",
1813 ip6_sprintf(IFA_IN6(ifa)), src_scope,
1814 dscopecmp,
1815 ifa_best ? IN6_ARE_SCOPE_CMP(src_scope, best_scope) : -1,
1816 in6_matchlen(IFA_IN6(ifa), dst),
1817 ((struct in6_ifaddr *)ifa)->ia6_flags);
1818 #endif
1819
1820 /*
1821 * Don't use an address before completing DAD
1822 * nor a duplicated address.
1823 */
1824 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1825 IN6_IFF_NOTREADY)
1826 continue;
1827
1828 /* XXX: is there any case to allow anycasts? */
1829 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1830 IN6_IFF_ANYCAST)
1831 continue;
1832
1833 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1834 IN6_IFF_DETACHED)
1835 continue;
1836
1837 /*
1838 * If this is the first address we find,
1839 * keep it anyway.
1840 */
1841 if (ifa_best == NULL)
1842 goto replace;
1843
1844 /*
1845 * ifa_best is never NULL beyond this line except
1846 * within the block labeled "replace".
1847 */
1848
1849 /*
1850 * If ifa_best has a smaller scope than dst and
1851 * the current address has a larger one than
1852 * (or equal to) dst, always replace ifa_best.
1853 * Also, if the current address has a smaller scope
1854 * than dst, ignore it unless ifa_best also has a
1855 * smaller scope.
1856 */
1857 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
1858 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
1859 goto replace;
1860 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
1861 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
1862 continue;
1863
1864 /*
1865 * A deprecated address SHOULD NOT be used in new
1866 * communications if an alternate (non-deprecated)
1867 * address is available and has sufficient scope.
1868 * RFC 2462, Section 5.5.4.
1869 */
1870 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1871 IN6_IFF_DEPRECATED) {
1872 /*
1873 * Ignore any deprecated addresses if
1874 * specified by configuration.
1875 */
1876 if (!ip6_use_deprecated)
1877 continue;
1878
1879 /*
1880 * If we have already found a non-deprecated
1881 * candidate, just ignore deprecated addresses.
1882 */
1883 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
1884 == 0)
1885 continue;
1886 }
1887
1888 /*
1889 * A non-deprecated address is always preferred
1890 * to a deprecated one regardless of scopes and
1891 * address matching.
1892 */
1893 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
1894 (((struct in6_ifaddr *)ifa)->ia6_flags &
1895 IN6_IFF_DEPRECATED) == 0)
1896 goto replace;
1897
1898 /*
1899 * At this point, we have two cases:
1900 * 1. we are looking at a non-deprecated address,
1901 * and ifa_best is also non-deprecated.
1902 * 2. we are looking at a deprecated address,
1903 * and ifa_best is also deprecated.
1904 * Also, we do not have to consider a case where
1905 * the scope of if_best is larger(smaller) than dst and
1906 * the scope of the current address is smaller(larger)
1907 * than dst. Such a case has already been covered.
1908 * Tiebreaking is done according to the following
1909 * items:
1910 * - the scope comparison between the address and
1911 * dst (dscopecmp)
1912 * - the scope comparison between the address and
1913 * ifa_best (bscopecmp)
1914 * - if the address match dst longer than ifa_best
1915 * (matchcmp)
1916 * - if the address is on the outgoing I/F (outI/F)
1917 *
1918 * Roughly speaking, the selection policy is
1919 * - the most important item is scope. The same scope
1920 * is best. Then search for a larger scope.
1921 * Smaller scopes are the last resort.
1922 * - A deprecated address is chosen only when we have
1923 * no address that has an enough scope, but is
1924 * prefered to any addresses of smaller scopes.
1925 * - Longest address match against dst is considered
1926 * only for addresses that has the same scope of dst.
1927 * - If there is no other reasons to choose one,
1928 * addresses on the outgoing I/F are preferred.
1929 *
1930 * The precise decision table is as follows:
1931 * dscopecmp bscopecmp matchcmp outI/F | replace?
1932 * !equal equal N/A Yes | Yes (1)
1933 * !equal equal N/A No | No (2)
1934 * larger larger N/A N/A | No (3)
1935 * larger smaller N/A N/A | Yes (4)
1936 * smaller larger N/A N/A | Yes (5)
1937 * smaller smaller N/A N/A | No (6)
1938 * equal smaller N/A N/A | Yes (7)
1939 * equal larger (already done)
1940 * equal equal larger N/A | Yes (8)
1941 * equal equal smaller N/A | No (9)
1942 * equal equal equal Yes | Yes (a)
1943 * eaual eqaul equal No | No (b)
1944 */
1945 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
1946 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
1947
1948 if (dscopecmp && bscopecmp == 0) {
1949 if (oifp == ifp) /* (1) */
1950 goto replace;
1951 continue; /* (2) */
1952 }
1953 if (dscopecmp > 0) {
1954 if (bscopecmp > 0) /* (3) */
1955 continue;
1956 goto replace; /* (4) */
1957 }
1958 if (dscopecmp < 0) {
1959 if (bscopecmp > 0) /* (5) */
1960 goto replace;
1961 continue; /* (6) */
1962 }
1963
1964 /* now dscopecmp must be 0 */
1965 if (bscopecmp < 0)
1966 goto replace; /* (7) */
1967
1968 /*
1969 * At last both dscopecmp and bscopecmp must be 0.
1970 * We need address matching against dst for
1971 * tiebreaking.
1972 */
1973 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1974 matchcmp = tlen - blen;
1975 if (matchcmp > 0) /* (8) */
1976 goto replace;
1977 if (matchcmp < 0) /* (9) */
1978 continue;
1979 if (oifp == ifp) /* (a) */
1980 goto replace;
1981 continue; /* (b) */
1982
1983 replace:
1984 ifa_best = (struct in6_ifaddr *)ifa;
1985 blen = tlen >= 0 ? tlen :
1986 in6_matchlen(IFA_IN6(ifa), dst);
1987 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
1988 }
1989 }
1990
1991 /* count statistics for future improvements */
1992 if (ifa_best == NULL)
1993 ip6stat.ip6s_sources_none++;
1994 else {
1995 if (oifp == ifa_best->ia_ifp)
1996 ip6stat.ip6s_sources_sameif[best_scope]++;
1997 else
1998 ip6stat.ip6s_sources_otherif[best_scope]++;
1999
2000 if (best_scope == dst_scope)
2001 ip6stat.ip6s_sources_samescope[best_scope]++;
2002 else
2003 ip6stat.ip6s_sources_otherscope[best_scope]++;
2004
2005 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2006 ip6stat.ip6s_sources_deprecated[best_scope]++;
2007 }
2008
2009 return(ifa_best);
2010 }
2011
2012 /*
2013 * return the best address out of the same scope. if no address was
2014 * found, return the first valid address from designated IF.
2015 */
2016
2017 struct in6_ifaddr *
2018 in6_ifawithifp(ifp, dst)
2019 register struct ifnet *ifp;
2020 register struct in6_addr *dst;
2021 {
2022 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2023 struct ifaddr *ifa;
2024 struct in6_ifaddr *besta = 0;
2025 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
2026
2027 dep[0] = dep[1] = NULL;
2028
2029 /*
2030 * We first look for addresses in the same scope.
2031 * If there is one, return it.
2032 * If two or more, return one which matches the dst longest.
2033 * If none, return one of global addresses assigned other ifs.
2034 */
2035 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
2036 {
2037 if (ifa->ifa_addr->sa_family != AF_INET6)
2038 continue;
2039 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2040 continue; /* XXX: is there any case to allow anycast? */
2041 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2042 continue; /* don't use this interface */
2043 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2044 continue;
2045 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2046 if (ip6_use_deprecated)
2047 dep[0] = (struct in6_ifaddr *)ifa;
2048 continue;
2049 }
2050
2051 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2052 /*
2053 * call in6_matchlen() as few as possible
2054 */
2055 if (besta) {
2056 if (blen == -1)
2057 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2058 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2059 if (tlen > blen) {
2060 blen = tlen;
2061 besta = (struct in6_ifaddr *)ifa;
2062 }
2063 } else
2064 besta = (struct in6_ifaddr *)ifa;
2065 }
2066 }
2067 if (besta)
2068 return(besta);
2069
2070 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
2071 {
2072 if (ifa->ifa_addr->sa_family != AF_INET6)
2073 continue;
2074 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2075 continue; /* XXX: is there any case to allow anycast? */
2076 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2077 continue; /* don't use this interface */
2078 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2079 continue;
2080 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2081 if (ip6_use_deprecated)
2082 dep[1] = (struct in6_ifaddr *)ifa;
2083 continue;
2084 }
2085
2086 return (struct in6_ifaddr *)ifa;
2087 }
2088
2089 /* use the last-resort values, that are, deprecated addresses */
2090 if (dep[0])
2091 return dep[0];
2092 if (dep[1])
2093 return dep[1];
2094
2095 return NULL;
2096 }
2097
2098 /*
2099 * perform DAD when interface becomes IFF_UP.
2100 */
2101 void
2102 in6_if_up(ifp)
2103 struct ifnet *ifp;
2104 {
2105 struct ifaddr *ifa;
2106 struct in6_ifaddr *ia;
2107 int dad_delay; /* delay ticks before DAD output */
2108
2109 /*
2110 * special cases, like 6to4, are handled in in6_ifattach
2111 */
2112 in6_ifattach(ifp, NULL);
2113
2114 dad_delay = 0;
2115 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
2116 {
2117 if (ifa->ifa_addr->sa_family != AF_INET6)
2118 continue;
2119 ia = (struct in6_ifaddr *)ifa;
2120 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2121 nd6_dad_start(ifa, &dad_delay);
2122 }
2123 }
2124
2125 /*
2126 * Calculate max IPv6 MTU through all the interfaces and store it
2127 * to in6_maxmtu.
2128 */
2129 void
2130 in6_setmaxmtu()
2131 {
2132 unsigned long maxmtu = 0;
2133 struct ifnet *ifp;
2134
2135 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2136 {
2137 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2138 nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
2139 maxmtu = nd_ifinfo[ifp->if_index].linkmtu;
2140 }
2141 if (maxmtu) /* update only when maxmtu is positive */
2142 in6_maxmtu = maxmtu;
2143 }
2144