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