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