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