FreeBSD/Linux Kernel Cross Reference
sys/netinet6/in6.c
1 /* $FreeBSD$ */
2 /* $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)in.c 8.2 (Berkeley) 11/15/93
66 */
67
68 #include "opt_inet.h"
69 #include "opt_inet6.h"
70
71 #include <sys/param.h>
72 #include <sys/errno.h>
73 #include <sys/malloc.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sockio.h>
77 #include <sys/systm.h>
78 #include <sys/proc.h>
79 #include <sys/time.h>
80 #include <sys/kernel.h>
81 #include <sys/syslog.h>
82
83 #include <net/if.h>
84 #include <net/if_types.h>
85 #include <net/route.h>
86 #include <net/if_dl.h>
87
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/if_ether.h>
91 #ifndef SCOPEDROUTING
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
95 #endif
96
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet6/nd6.h>
100 #include <netinet6/mld6_var.h>
101 #include <netinet6/ip6_mroute.h>
102 #include <netinet6/in6_ifattach.h>
103 #include <netinet6/scope6_var.h>
104 #ifndef SCOPEDROUTING
105 #include <netinet6/in6_pcb.h>
106 #endif
107
108 #include <net/net_osdep.h>
109
110 MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address");
111
112 /*
113 * Definitions of some costant IP6 addresses.
114 */
115 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
116 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
117 const struct in6_addr in6addr_nodelocal_allnodes =
118 IN6ADDR_NODELOCAL_ALLNODES_INIT;
119 const struct in6_addr in6addr_linklocal_allnodes =
120 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
121 const struct in6_addr in6addr_linklocal_allrouters =
122 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
123
124 const struct in6_addr in6mask0 = IN6MASK0;
125 const struct in6_addr in6mask32 = IN6MASK32;
126 const struct in6_addr in6mask64 = IN6MASK64;
127 const struct in6_addr in6mask96 = IN6MASK96;
128 const struct in6_addr in6mask128 = IN6MASK128;
129
130 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
131 0, 0, IN6ADDR_ANY_INIT, 0};
132
133 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
134 struct ifnet *, struct proc *));
135 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
136 struct sockaddr_in6 *, int));
137 static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
138
139 struct in6_multihead in6_multihead; /* XXX BSS initialization */
140
141 int (*faithprefix_p)(struct in6_addr *);
142
143 /*
144 * Subroutine for in6_ifaddloop() and in6_ifremloop().
145 * This routine does actual work.
146 */
147 static void
148 in6_ifloop_request(int cmd, struct ifaddr *ifa)
149 {
150 struct sockaddr_in6 all1_sa;
151 struct rtentry *nrt = NULL;
152 int e;
153
154 bzero(&all1_sa, sizeof(all1_sa));
155 all1_sa.sin6_family = AF_INET6;
156 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
157 all1_sa.sin6_addr = in6mask128;
158
159 /*
160 * We specify the address itself as the gateway, and set the
161 * RTF_LLINFO flag, so that the corresponding host route would have
162 * the flag, and thus applications that assume traditional behavior
163 * would be happy. Note that we assume the caller of the function
164 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
165 * which changes the outgoing interface to the loopback interface.
166 */
167 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
168 (struct sockaddr *)&all1_sa,
169 RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
170 if (e != 0) {
171 log(LOG_ERR, "in6_ifloop_request: "
172 "%s operation failed for %s (errno=%d)\n",
173 cmd == RTM_ADD ? "ADD" : "DELETE",
174 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
175 e);
176 }
177
178 /*
179 * Make sure rt_ifa be equal to IFA, the second argument of the
180 * function.
181 * We need this because when we refer to rt_ifa->ia6_flags in
182 * ip6_input, we assume that the rt_ifa points to the address instead
183 * of the loopback address.
184 */
185 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
186 IFAFREE(nrt->rt_ifa);
187 IFAREF(ifa);
188 nrt->rt_ifa = ifa;
189 }
190
191 /*
192 * Report the addition/removal of the address to the routing socket.
193 * XXX: since we called rtinit for a p2p interface with a destination,
194 * we end up reporting twice in such a case. Should we rather
195 * omit the second report?
196 */
197 if (nrt) {
198 rt_newaddrmsg(cmd, ifa, e, nrt);
199 if (cmd == RTM_DELETE) {
200 if (nrt->rt_refcnt <= 0) {
201 /* XXX: we should free the entry ourselves. */
202 nrt->rt_refcnt++;
203 rtfree(nrt);
204 }
205 } else {
206 /* the cmd must be RTM_ADD here */
207 nrt->rt_refcnt--;
208 }
209 }
210 }
211
212 /*
213 * Add ownaddr as loopback rtentry. We previously add the route only if
214 * necessary (ex. on a p2p link). However, since we now manage addresses
215 * separately from prefixes, we should always add the route. We can't
216 * rely on the cloning mechanism from the corresponding interface route
217 * any more.
218 */
219 static void
220 in6_ifaddloop(struct ifaddr *ifa)
221 {
222 struct rtentry *rt;
223
224 /* If there is no loopback entry, allocate one. */
225 rt = rtalloc1(ifa->ifa_addr, 0, 0);
226 if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
227 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
228 in6_ifloop_request(RTM_ADD, ifa);
229 if (rt)
230 rt->rt_refcnt--;
231 }
232
233 /*
234 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
235 * if it exists.
236 */
237 static void
238 in6_ifremloop(struct ifaddr *ifa)
239 {
240 struct in6_ifaddr *ia;
241 struct rtentry *rt;
242 int ia_count = 0;
243
244 /*
245 * Some of BSD variants do not remove cloned routes
246 * from an interface direct route, when removing the direct route
247 * (see comments in net/net_osdep.h). Even for variants that do remove
248 * cloned routes, they could fail to remove the cloned routes when
249 * we handle multple addresses that share a common prefix.
250 * So, we should remove the route corresponding to the deleted address
251 * regardless of the result of in6_is_ifloop_auto().
252 */
253
254 /*
255 * Delete the entry only if exact one ifa exists. More than one ifa
256 * can exist if we assign a same single address to multiple
257 * (probably p2p) interfaces.
258 * XXX: we should avoid such a configuration in IPv6...
259 */
260 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
261 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
262 ia_count++;
263 if (ia_count > 1)
264 break;
265 }
266 }
267
268 if (ia_count == 1) {
269 /*
270 * Before deleting, check if a corresponding loopbacked host
271 * route surely exists. With this check, we can avoid to
272 * delete an interface direct route whose destination is same
273 * as the address being removed. This can happen when remofing
274 * a subnet-router anycast address on an interface attahced
275 * to a shared medium.
276 */
277 rt = rtalloc1(ifa->ifa_addr, 0, 0);
278 if (rt != NULL && (rt->rt_flags & RTF_HOST) != 0 &&
279 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
280 rt->rt_refcnt--;
281 in6_ifloop_request(RTM_DELETE, ifa);
282 }
283 }
284 }
285
286 int
287 in6_ifindex2scopeid(idx)
288 int idx;
289 {
290 struct ifnet *ifp;
291 struct ifaddr *ifa;
292 struct sockaddr_in6 *sin6;
293
294 if (idx < 0 || if_index < idx)
295 return -1;
296 ifp = ifindex2ifnet[idx];
297
298 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
299 {
300 if (ifa->ifa_addr->sa_family != AF_INET6)
301 continue;
302 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
303 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
304 return sin6->sin6_scope_id & 0xffff;
305 }
306
307 return -1;
308 }
309
310 int
311 in6_mask2len(mask, lim0)
312 struct in6_addr *mask;
313 u_char *lim0;
314 {
315 int x = 0, y;
316 u_char *lim = lim0, *p;
317
318 if (lim0 == NULL ||
319 lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
320 lim = (u_char *)mask + sizeof(*mask);
321 for (p = (u_char *)mask; p < lim; x++, p++) {
322 if (*p != 0xff)
323 break;
324 }
325 y = 0;
326 if (p < lim) {
327 for (y = 0; y < 8; y++) {
328 if ((*p & (0x80 >> y)) == 0)
329 break;
330 }
331 }
332
333 /*
334 * when the limit pointer is given, do a stricter check on the
335 * remaining bits.
336 */
337 if (p < lim) {
338 if (y != 0 && (*p & (0x00ff >> y)) != 0)
339 return(-1);
340 for (p = p + 1; p < lim; p++)
341 if (*p != 0)
342 return(-1);
343 }
344
345 return x * 8 + y;
346 }
347
348 void
349 in6_len2mask(mask, len)
350 struct in6_addr *mask;
351 int len;
352 {
353 int i;
354
355 bzero(mask, sizeof(*mask));
356 for (i = 0; i < len / 8; i++)
357 mask->s6_addr8[i] = 0xff;
358 if (len % 8)
359 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
360 }
361
362 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
363 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
364
365 int
366 in6_control(so, cmd, data, ifp, p)
367 struct socket *so;
368 u_long cmd;
369 caddr_t data;
370 struct ifnet *ifp;
371 struct proc *p;
372 {
373 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
374 struct in6_ifaddr *ia = NULL;
375 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
376 int privileged;
377
378 privileged = 0;
379 if (p == NULL || !suser(p))
380 privileged++;
381
382 switch (cmd) {
383 case SIOCGETSGCNT_IN6:
384 case SIOCGETMIFCNT_IN6:
385 return (mrt6_ioctl(cmd, data));
386 }
387
388 if (ifp == NULL)
389 return(EOPNOTSUPP);
390
391 switch (cmd) {
392 case SIOCSNDFLUSH_IN6:
393 case SIOCSPFXFLUSH_IN6:
394 case SIOCSRTRFLUSH_IN6:
395 case SIOCSDEFIFACE_IN6:
396 case SIOCSIFINFO_FLAGS:
397 if (!privileged)
398 return(EPERM);
399 /* fall through */
400 case OSIOCGIFINFO_IN6:
401 case SIOCGIFINFO_IN6:
402 case SIOCGDRLST_IN6:
403 case SIOCGPRLST_IN6:
404 case SIOCGNBRINFO_IN6:
405 case SIOCGDEFIFACE_IN6:
406 return(nd6_ioctl(cmd, data, ifp));
407 }
408
409 switch (cmd) {
410 case SIOCSIFPREFIX_IN6:
411 case SIOCDIFPREFIX_IN6:
412 case SIOCAIFPREFIX_IN6:
413 case SIOCCIFPREFIX_IN6:
414 case SIOCSGIFPREFIX_IN6:
415 case SIOCGIFPREFIX_IN6:
416 log(LOG_NOTICE,
417 "prefix ioctls are now invalidated. "
418 "please use ifconfig.\n");
419 return(EOPNOTSUPP);
420 }
421
422 switch (cmd) {
423 case SIOCSSCOPE6:
424 if (!privileged)
425 return(EPERM);
426 return(scope6_set(ifp, ifr->ifr_ifru.ifru_scope_id));
427 break;
428 case SIOCGSCOPE6:
429 return(scope6_get(ifp, ifr->ifr_ifru.ifru_scope_id));
430 break;
431 case SIOCGSCOPE6DEF:
432 return(scope6_get_default(ifr->ifr_ifru.ifru_scope_id));
433 break;
434 }
435
436 switch (cmd) {
437 case SIOCALIFADDR:
438 case SIOCDLIFADDR:
439 if (!privileged)
440 return(EPERM);
441 /* fall through */
442 case SIOCGLIFADDR:
443 return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
444 }
445
446 /*
447 * Find address for this interface, if it exists.
448 */
449 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
450 struct sockaddr_in6 *sa6 =
451 (struct sockaddr_in6 *)&ifra->ifra_addr;
452
453 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
454 if (sa6->sin6_addr.s6_addr16[1] == 0) {
455 /* link ID is not embedded by the user */
456 sa6->sin6_addr.s6_addr16[1] =
457 htons(ifp->if_index);
458 } else if (sa6->sin6_addr.s6_addr16[1] !=
459 htons(ifp->if_index)) {
460 return(EINVAL); /* link ID contradicts */
461 }
462 if (sa6->sin6_scope_id) {
463 if (sa6->sin6_scope_id !=
464 (u_int32_t)ifp->if_index)
465 return(EINVAL);
466 sa6->sin6_scope_id = 0; /* XXX: good way? */
467 }
468 }
469 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
470 }
471
472 switch (cmd) {
473 case SIOCSIFADDR_IN6:
474 case SIOCSIFDSTADDR_IN6:
475 case SIOCSIFNETMASK_IN6:
476 /*
477 * Since IPv6 allows a node to assign multiple addresses
478 * on a single interface, SIOCSIFxxx ioctls are not suitable
479 * and should be unused.
480 */
481 /* we decided to obsolete this command (20000704) */
482 return(EINVAL);
483
484 case SIOCDIFADDR_IN6:
485 /*
486 * for IPv4, we look for existing in_ifaddr here to allow
487 * "ifconfig if0 delete" to remove first IPv4 address on the
488 * interface. For IPv6, as the spec allow multiple interface
489 * address from the day one, we consider "remove the first one"
490 * semantics to be not preferable.
491 */
492 if (ia == NULL)
493 return(EADDRNOTAVAIL);
494 /* FALLTHROUGH */
495 case SIOCAIFADDR_IN6:
496 /*
497 * We always require users to specify a valid IPv6 address for
498 * the corresponding operation.
499 */
500 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
501 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
502 return(EAFNOSUPPORT);
503 if (!privileged)
504 return(EPERM);
505
506 break;
507
508 case SIOCGIFADDR_IN6:
509 /* This interface is basically deprecated. use SIOCGIFCONF. */
510 /* fall through */
511 case SIOCGIFAFLAG_IN6:
512 case SIOCGIFNETMASK_IN6:
513 case SIOCGIFDSTADDR_IN6:
514 case SIOCGIFALIFETIME_IN6:
515 /* must think again about its semantics */
516 if (ia == NULL)
517 return(EADDRNOTAVAIL);
518 break;
519 case SIOCSIFALIFETIME_IN6:
520 {
521 struct in6_addrlifetime *lt;
522
523 if (!privileged)
524 return(EPERM);
525 if (ia == NULL)
526 return(EADDRNOTAVAIL);
527 /* sanity for overflow - beware unsigned */
528 lt = &ifr->ifr_ifru.ifru_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 break;
538 }
539 }
540
541 switch (cmd) {
542
543 case SIOCGIFADDR_IN6:
544 ifr->ifr_addr = ia->ia_addr;
545 break;
546
547 case SIOCGIFDSTADDR_IN6:
548 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
549 return(EINVAL);
550 /*
551 * XXX: should we check if ifa_dstaddr is NULL and return
552 * an error?
553 */
554 ifr->ifr_dstaddr = ia->ia_dstaddr;
555 break;
556
557 case SIOCGIFNETMASK_IN6:
558 ifr->ifr_addr = ia->ia_prefixmask;
559 break;
560
561 case SIOCGIFAFLAG_IN6:
562 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
563 break;
564
565 case SIOCGIFSTAT_IN6:
566 if (ifp == NULL)
567 return EINVAL;
568 if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax
569 || in6_ifstat[ifp->if_index] == NULL) {
570 /* return EAFNOSUPPORT? */
571 bzero(&ifr->ifr_ifru.ifru_stat,
572 sizeof(ifr->ifr_ifru.ifru_stat));
573 } else
574 ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index];
575 break;
576
577 case SIOCGIFSTAT_ICMP6:
578 if (ifp == NULL)
579 return EINVAL;
580 if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax ||
581 icmp6_ifstat[ifp->if_index] == NULL) {
582 /* return EAFNOSUPPORT? */
583 bzero(&ifr->ifr_ifru.ifru_stat,
584 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
585 } else
586 ifr->ifr_ifru.ifru_icmp6stat =
587 *icmp6_ifstat[ifp->if_index];
588 break;
589
590 case SIOCGIFALIFETIME_IN6:
591 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
592 break;
593
594 case SIOCSIFALIFETIME_IN6:
595 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
596 /* for sanity */
597 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
598 ia->ia6_lifetime.ia6t_expire =
599 time_second + ia->ia6_lifetime.ia6t_vltime;
600 } else
601 ia->ia6_lifetime.ia6t_expire = 0;
602 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
603 ia->ia6_lifetime.ia6t_preferred =
604 time_second + ia->ia6_lifetime.ia6t_pltime;
605 } else
606 ia->ia6_lifetime.ia6t_preferred = 0;
607 break;
608
609 case SIOCAIFADDR_IN6:
610 {
611 int i, error = 0;
612 struct nd_prefix pr0, *pr;
613
614 /*
615 * first, make or update the interface address structure,
616 * and link it to the list.
617 */
618 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
619 return(error);
620
621 /*
622 * then, make the prefix on-link on the interface.
623 * XXX: we'd rather create the prefix before the address, but
624 * we need at least one address to install the corresponding
625 * interface route, so we configure the address first.
626 */
627
628 /*
629 * convert mask to prefix length (prefixmask has already
630 * been validated in in6_update_ifa().
631 */
632 bzero(&pr0, sizeof(pr0));
633 pr0.ndpr_ifp = ifp;
634 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
635 NULL);
636 if (pr0.ndpr_plen == 128)
637 break; /* we don't need to install a host route. */
638 pr0.ndpr_prefix = ifra->ifra_addr;
639 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
640 /* apply the mask for safety. */
641 for (i = 0; i < 4; i++) {
642 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
643 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
644 }
645 /*
646 * XXX: since we don't have an API to set prefix (not address)
647 * lifetimes, we just use the same lifetimes as addresses.
648 * The (temporarily) installed lifetimes can be overridden by
649 * later advertised RAs (when accept_rtadv is non 0), which is
650 * an intended behavior.
651 */
652 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
653 pr0.ndpr_raf_auto =
654 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
655 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
656 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
657
658 /* add the prefix if there's one. */
659 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
660 /*
661 * nd6_prelist_add will install the corresponding
662 * interface route.
663 */
664 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
665 return(error);
666 if (pr == NULL) {
667 log(LOG_ERR, "nd6_prelist_add succedded but "
668 "no prefix\n");
669 return(EINVAL); /* XXX panic here? */
670 }
671 }
672 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
673 == NULL) {
674 /* XXX: this should not happen! */
675 log(LOG_ERR, "in6_control: addition succeeded, but"
676 " no ifaddr\n");
677 } else {
678 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
679 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
680 ia->ia6_ndpr = pr;
681 pr->ndpr_refcnt++;
682
683 /*
684 * If this is the first autoconf address from
685 * the prefix, create a temporary address
686 * as well (when specified).
687 */
688 if (ip6_use_tempaddr &&
689 pr->ndpr_refcnt == 1) {
690 int e;
691 if ((e = in6_tmpifadd(ia, 1)) != 0) {
692 log(LOG_NOTICE, "in6_control: "
693 "failed to create a "
694 "temporary address, "
695 "errno=%d\n",
696 e);
697 }
698 }
699 }
700
701 /*
702 * this might affect the status of autoconfigured
703 * addresses, that is, this address might make
704 * other addresses detached.
705 */
706 pfxlist_onlink_check();
707 }
708 break;
709 }
710
711 case SIOCDIFADDR_IN6:
712 {
713 int i = 0;
714 struct nd_prefix pr0, *pr;
715
716 /*
717 * If the address being deleted is the only one that owns
718 * the corresponding prefix, expire the prefix as well.
719 * XXX: theoretically, we don't have to warry about such
720 * relationship, since we separate the address management
721 * and the prefix management. We do this, however, to provide
722 * as much backward compatibility as possible in terms of
723 * the ioctl operation.
724 */
725 bzero(&pr0, sizeof(pr0));
726 pr0.ndpr_ifp = ifp;
727 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
728 NULL);
729 if (pr0.ndpr_plen == 128)
730 goto purgeaddr;
731 pr0.ndpr_prefix = ia->ia_addr;
732 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
733 for (i = 0; i < 4; i++) {
734 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
735 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
736 }
737 /*
738 * The logic of the following condition is a bit complicated.
739 * We expire the prefix when
740 * 1. the address obeys autoconfiguration and it is the
741 * only owner of the associated prefix, or
742 * 2. the address does not obey autoconf and there is no
743 * other owner of the prefix.
744 */
745 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
746 (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
747 pr->ndpr_refcnt == 1) ||
748 ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
749 pr->ndpr_refcnt == 0))) {
750 pr->ndpr_expire = 1; /* XXX: just for expiration */
751 }
752
753 purgeaddr:
754 in6_purgeaddr(&ia->ia_ifa);
755 break;
756 }
757
758 default:
759 if (ifp == NULL || ifp->if_ioctl == 0)
760 return(EOPNOTSUPP);
761 return((*ifp->if_ioctl)(ifp, cmd, data));
762 }
763
764 return(0);
765 }
766
767 /*
768 * Update parameters of an IPv6 interface address.
769 * If necessary, a new entry is created and linked into address chains.
770 * This function is separated from in6_control().
771 * XXX: should this be performed under splnet()?
772 */
773 int
774 in6_update_ifa(ifp, ifra, ia)
775 struct ifnet *ifp;
776 struct in6_aliasreq *ifra;
777 struct in6_ifaddr *ia;
778 {
779 int error = 0, hostIsNew = 0, plen = -1;
780 struct in6_ifaddr *oia;
781 struct sockaddr_in6 dst6;
782 struct in6_addrlifetime *lt;
783
784 /* Validate parameters */
785 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
786 return(EINVAL);
787
788 /*
789 * The destination address for a p2p link must have a family
790 * of AF_UNSPEC or AF_INET6.
791 */
792 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
793 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
794 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
795 return(EAFNOSUPPORT);
796 /*
797 * validate ifra_prefixmask. don't check sin6_family, netmask
798 * does not carry fields other than sin6_len.
799 */
800 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
801 return(EINVAL);
802 /*
803 * Because the IPv6 address architecture is classless, we require
804 * users to specify a (non 0) prefix length (mask) for a new address.
805 * We also require the prefix (when specified) mask is valid, and thus
806 * reject a non-consecutive mask.
807 */
808 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
809 return(EINVAL);
810 if (ifra->ifra_prefixmask.sin6_len != 0) {
811 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
812 (u_char *)&ifra->ifra_prefixmask +
813 ifra->ifra_prefixmask.sin6_len);
814 if (plen <= 0)
815 return(EINVAL);
816 }
817 else {
818 /*
819 * In this case, ia must not be NULL. We just use its prefix
820 * length.
821 */
822 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
823 }
824 /*
825 * If the destination address on a p2p interface is specified,
826 * and the address is a scoped one, validate/set the scope
827 * zone identifier.
828 */
829 dst6 = ifra->ifra_dstaddr;
830 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
831 (dst6.sin6_family == AF_INET6)) {
832 int scopeid;
833
834 #ifndef SCOPEDROUTING
835 if ((error = in6_recoverscope(&dst6,
836 &ifra->ifra_dstaddr.sin6_addr,
837 ifp)) != 0)
838 return(error);
839 #endif
840 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
841 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
842 dst6.sin6_scope_id = scopeid;
843 else if (dst6.sin6_scope_id != scopeid)
844 return(EINVAL); /* scope ID mismatch. */
845 #ifndef SCOPEDROUTING
846 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
847 != 0)
848 return(error);
849 dst6.sin6_scope_id = 0; /* XXX */
850 #endif
851 }
852 /*
853 * The destination address can be specified only for a p2p or a
854 * loopback interface. If specified, the corresponding prefix length
855 * must be 128.
856 */
857 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
858 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
859 /* XXX: noisy message */
860 log(LOG_INFO, "in6_update_ifa: a destination can be "
861 "specified for a p2p or a loopback IF only\n");
862 return(EINVAL);
863 }
864 if (plen != 128) {
865 /*
866 * The following message seems noisy, but we dare to
867 * add it for diagnosis.
868 */
869 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
870 "when dstaddr is specified\n");
871 return(EINVAL);
872 }
873 }
874 /* lifetime consistency check */
875 lt = &ifra->ifra_lifetime;
876 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
877 && lt->ia6t_vltime + time_second < time_second) {
878 return EINVAL;
879 }
880 if (lt->ia6t_vltime == 0) {
881 /*
882 * the following log might be noisy, but this is a typical
883 * configuration mistake or a tool's bug.
884 */
885 log(LOG_INFO,
886 "in6_update_ifa: valid lifetime is 0 for %s\n",
887 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
888 }
889 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
890 && lt->ia6t_pltime + time_second < time_second) {
891 return EINVAL;
892 }
893
894 /*
895 * If this is a new address, allocate a new ifaddr and link it
896 * into chains.
897 */
898 if (ia == NULL) {
899 hostIsNew = 1;
900 /*
901 * When in6_update_ifa() is called in a process of a received
902 * RA, it is called under splnet(). So, we should call malloc
903 * with M_NOWAIT.
904 */
905 ia = (struct in6_ifaddr *)
906 malloc(sizeof(*ia), M_IFADDR, M_NOWAIT);
907 if (ia == NULL)
908 return (ENOBUFS);
909 bzero((caddr_t)ia, sizeof(*ia));
910 /* Initialize the address and masks */
911 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
912 ia->ia_addr.sin6_family = AF_INET6;
913 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
914 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
915 /*
916 * XXX: some functions expect that ifa_dstaddr is not
917 * NULL for p2p interfaces.
918 */
919 ia->ia_ifa.ifa_dstaddr
920 = (struct sockaddr *)&ia->ia_dstaddr;
921 } else {
922 ia->ia_ifa.ifa_dstaddr = NULL;
923 }
924 ia->ia_ifa.ifa_netmask
925 = (struct sockaddr *)&ia->ia_prefixmask;
926
927 ia->ia_ifp = ifp;
928 if ((oia = in6_ifaddr) != NULL) {
929 for ( ; oia->ia_next; oia = oia->ia_next)
930 continue;
931 oia->ia_next = ia;
932 } else
933 in6_ifaddr = ia;
934
935 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
936 ifa_list);
937 }
938
939 /* set prefix mask */
940 if (ifra->ifra_prefixmask.sin6_len) {
941 /*
942 * We prohibit changing the prefix length of an existing
943 * address, because
944 * + such an operation should be rare in IPv6, and
945 * + the operation would confuse prefix management.
946 */
947 if (ia->ia_prefixmask.sin6_len &&
948 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
949 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
950 " existing (%s) address should not be changed\n",
951 ip6_sprintf(&ia->ia_addr.sin6_addr));
952 error = EINVAL;
953 goto unlink;
954 }
955 ia->ia_prefixmask = ifra->ifra_prefixmask;
956 }
957
958 /*
959 * If a new destination address is specified, scrub the old one and
960 * install the new destination. Note that the interface must be
961 * p2p or loopback (see the check above.)
962 */
963 if (dst6.sin6_family == AF_INET6 &&
964 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
965 &ia->ia_dstaddr.sin6_addr)) {
966 int e;
967
968 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
969 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
970 != 0) {
971 log(LOG_ERR, "in6_update_ifa: failed to remove "
972 "a route to the old destination: %s\n",
973 ip6_sprintf(&ia->ia_addr.sin6_addr));
974 /* proceed anyway... */
975 }
976 else
977 ia->ia_flags &= ~IFA_ROUTE;
978 ia->ia_dstaddr = dst6;
979 }
980
981 /* reset the interface and routing table appropriately. */
982 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
983 goto unlink;
984
985 /*
986 * Beyond this point, we should call in6_purgeaddr upon an error,
987 * not just go to unlink.
988 */
989
990 #if 0 /* disable this mechanism for now */
991 /* update prefix list */
992 if (hostIsNew &&
993 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
994 int iilen;
995
996 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
997 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
998 in6_purgeaddr((struct ifaddr *)ia);
999 return(error);
1000 }
1001 }
1002 #endif
1003
1004 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1005 struct sockaddr_in6 mltaddr, mltmask;
1006 struct in6_multi *in6m;
1007
1008 if (hostIsNew) {
1009 /*
1010 * join solicited multicast addr for new host id
1011 */
1012 struct in6_addr llsol;
1013 bzero(&llsol, sizeof(struct in6_addr));
1014 llsol.s6_addr16[0] = htons(0xff02);
1015 llsol.s6_addr16[1] = htons(ifp->if_index);
1016 llsol.s6_addr32[1] = 0;
1017 llsol.s6_addr32[2] = htonl(1);
1018 llsol.s6_addr32[3] =
1019 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1020 llsol.s6_addr8[12] = 0xff;
1021 (void)in6_addmulti(&llsol, ifp, &error);
1022 if (error != 0) {
1023 log(LOG_WARNING,
1024 "in6_update_ifa: addmulti failed for "
1025 "%s on %s (errno=%d)\n",
1026 ip6_sprintf(&llsol), if_name(ifp),
1027 error);
1028 in6_purgeaddr((struct ifaddr *)ia);
1029 return(error);
1030 }
1031 }
1032
1033 bzero(&mltmask, sizeof(mltmask));
1034 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1035 mltmask.sin6_family = AF_INET6;
1036 mltmask.sin6_addr = in6mask32;
1037
1038 /*
1039 * join link-local all-nodes address
1040 */
1041 bzero(&mltaddr, sizeof(mltaddr));
1042 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1043 mltaddr.sin6_family = AF_INET6;
1044 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1045 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1046
1047 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1048 if (in6m == NULL) {
1049 rtrequest(RTM_ADD,
1050 (struct sockaddr *)&mltaddr,
1051 (struct sockaddr *)&ia->ia_addr,
1052 (struct sockaddr *)&mltmask,
1053 RTF_UP|RTF_CLONING, /* xxx */
1054 (struct rtentry **)0);
1055 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1056 if (error != 0) {
1057 log(LOG_WARNING,
1058 "in6_update_ifa: addmulti failed for "
1059 "%s on %s (errno=%d)\n",
1060 ip6_sprintf(&mltaddr.sin6_addr),
1061 if_name(ifp), error);
1062 }
1063 }
1064
1065 /*
1066 * join node information group address
1067 */
1068 #define hostnamelen strlen(hostname)
1069 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1070 == 0) {
1071 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1072 if (in6m == NULL && ia != NULL) {
1073 (void)in6_addmulti(&mltaddr.sin6_addr,
1074 ifp, &error);
1075 if (error != 0) {
1076 log(LOG_WARNING, "in6_update_ifa: "
1077 "addmulti failed for "
1078 "%s on %s (errno=%d)\n",
1079 ip6_sprintf(&mltaddr.sin6_addr),
1080 if_name(ifp), error);
1081 }
1082 }
1083 }
1084 #undef hostnamelen
1085
1086 /*
1087 * join node-local all-nodes address, on loopback.
1088 * XXX: since "node-local" is obsoleted by interface-local,
1089 * we have to join the group on every interface with
1090 * some interface-boundary restriction.
1091 */
1092 if (ifp->if_flags & IFF_LOOPBACK) {
1093 struct in6_ifaddr *ia_loop;
1094
1095 struct in6_addr loop6 = in6addr_loopback;
1096 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1097
1098 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1099
1100 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1101 if (in6m == NULL && ia_loop != NULL) {
1102 rtrequest(RTM_ADD,
1103 (struct sockaddr *)&mltaddr,
1104 (struct sockaddr *)&ia_loop->ia_addr,
1105 (struct sockaddr *)&mltmask,
1106 RTF_UP,
1107 (struct rtentry **)0);
1108 (void)in6_addmulti(&mltaddr.sin6_addr, ifp,
1109 &error);
1110 if (error != 0) {
1111 log(LOG_WARNING, "in6_update_ifa: "
1112 "addmulti failed for %s on %s "
1113 "(errno=%d)\n",
1114 ip6_sprintf(&mltaddr.sin6_addr),
1115 if_name(ifp), error);
1116 }
1117 }
1118 }
1119 }
1120
1121 ia->ia6_flags = ifra->ifra_flags;
1122 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1123 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1124
1125 ia->ia6_lifetime = ifra->ifra_lifetime;
1126 /* for sanity */
1127 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1128 ia->ia6_lifetime.ia6t_expire =
1129 time_second + ia->ia6_lifetime.ia6t_vltime;
1130 } else
1131 ia->ia6_lifetime.ia6t_expire = 0;
1132 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1133 ia->ia6_lifetime.ia6t_preferred =
1134 time_second + ia->ia6_lifetime.ia6t_pltime;
1135 } else
1136 ia->ia6_lifetime.ia6t_preferred = 0;
1137
1138 /*
1139 * make sure to initialize ND6 information. this is to workaround
1140 * issues with interfaces with IPv6 addresses, which have never brought
1141 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1142 */
1143 nd6_ifattach(ifp);
1144
1145 /*
1146 * Perform DAD, if needed.
1147 * XXX It may be of use, if we can administratively
1148 * disable DAD.
1149 */
1150 if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1151 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1152 nd6_dad_start((struct ifaddr *)ia, NULL);
1153 }
1154
1155 return(error);
1156
1157 unlink:
1158 /*
1159 * XXX: if a change of an existing address failed, keep the entry
1160 * anyway.
1161 */
1162 if (hostIsNew)
1163 in6_unlink_ifa(ia, ifp);
1164 return(error);
1165 }
1166
1167 void
1168 in6_purgeaddr(ifa)
1169 struct ifaddr *ifa;
1170 {
1171 struct ifnet *ifp = ifa->ifa_ifp;
1172 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1173
1174 /* stop DAD processing */
1175 nd6_dad_stop(ifa);
1176
1177 /*
1178 * delete route to the destination of the address being purged.
1179 * The interface must be p2p or loopback in this case.
1180 */
1181 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1182 int e;
1183
1184 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1185 != 0) {
1186 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1187 "a route to the p2p destination: %s on %s, "
1188 "errno=%d\n",
1189 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1190 e);
1191 /* proceed anyway... */
1192 }
1193 else
1194 ia->ia_flags &= ~IFA_ROUTE;
1195 }
1196
1197 /* Remove ownaddr's loopback rtentry, if it exists. */
1198 in6_ifremloop(&(ia->ia_ifa));
1199
1200 if (ifp->if_flags & IFF_MULTICAST) {
1201 /*
1202 * delete solicited multicast addr for deleting host id
1203 */
1204 struct in6_multi *in6m;
1205 struct in6_addr llsol;
1206 bzero(&llsol, sizeof(struct in6_addr));
1207 llsol.s6_addr16[0] = htons(0xff02);
1208 llsol.s6_addr16[1] = htons(ifp->if_index);
1209 llsol.s6_addr32[1] = 0;
1210 llsol.s6_addr32[2] = htonl(1);
1211 llsol.s6_addr32[3] =
1212 ia->ia_addr.sin6_addr.s6_addr32[3];
1213 llsol.s6_addr8[12] = 0xff;
1214
1215 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1216 if (in6m)
1217 in6_delmulti(in6m);
1218 }
1219
1220 in6_unlink_ifa(ia, ifp);
1221 }
1222
1223 static void
1224 in6_unlink_ifa(ia, ifp)
1225 struct in6_ifaddr *ia;
1226 struct ifnet *ifp;
1227 {
1228 int plen, iilen;
1229 struct in6_ifaddr *oia;
1230 int s = splnet();
1231
1232 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1233
1234 oia = ia;
1235 if (oia == (ia = in6_ifaddr))
1236 in6_ifaddr = ia->ia_next;
1237 else {
1238 while (ia->ia_next && (ia->ia_next != oia))
1239 ia = ia->ia_next;
1240 if (ia->ia_next)
1241 ia->ia_next = oia->ia_next;
1242 else {
1243 /* search failed */
1244 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1245 }
1246 }
1247
1248 if (oia->ia6_ifpr) { /* check for safety */
1249 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1250 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1251 in6_prefix_remove_ifid(iilen, oia);
1252 }
1253
1254 /*
1255 * When an autoconfigured address is being removed, release the
1256 * reference to the base prefix. Also, since the release might
1257 * affect the status of other (detached) addresses, call
1258 * pfxlist_onlink_check().
1259 */
1260 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1261 if (oia->ia6_ndpr == NULL) {
1262 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1263 "%p has no prefix\n", oia);
1264 } else {
1265 oia->ia6_ndpr->ndpr_refcnt--;
1266 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1267 oia->ia6_ndpr = NULL;
1268 }
1269
1270 pfxlist_onlink_check();
1271 }
1272
1273 /*
1274 * release another refcnt for the link from in6_ifaddr.
1275 * Note that we should decrement the refcnt at least once for all *BSD.
1276 */
1277 IFAFREE(&oia->ia_ifa);
1278
1279 splx(s);
1280 }
1281
1282 void
1283 in6_purgeif(ifp)
1284 struct ifnet *ifp;
1285 {
1286 struct ifaddr *ifa, *nifa;
1287
1288 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
1289 {
1290 nifa = TAILQ_NEXT(ifa, ifa_list);
1291 if (ifa->ifa_addr->sa_family != AF_INET6)
1292 continue;
1293 in6_purgeaddr(ifa);
1294 }
1295
1296 in6_ifdetach(ifp);
1297 }
1298
1299 /*
1300 * SIOC[GAD]LIFADDR.
1301 * SIOCGLIFADDR: get first address. (?)
1302 * SIOCGLIFADDR with IFLR_PREFIX:
1303 * get first address that matches the specified prefix.
1304 * SIOCALIFADDR: add the specified address.
1305 * SIOCALIFADDR with IFLR_PREFIX:
1306 * add the specified prefix, filling hostid part from
1307 * the first link-local address. prefixlen must be <= 64.
1308 * SIOCDLIFADDR: delete the specified address.
1309 * SIOCDLIFADDR with IFLR_PREFIX:
1310 * delete the first address that matches the specified prefix.
1311 * return values:
1312 * EINVAL on invalid parameters
1313 * EADDRNOTAVAIL on prefix match failed/specified address not found
1314 * other values may be returned from in6_ioctl()
1315 *
1316 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1317 * this is to accomodate address naming scheme other than RFC2374,
1318 * in the future.
1319 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1320 * address encoding scheme. (see figure on page 8)
1321 */
1322 static int
1323 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
1324 struct socket *so;
1325 u_long cmd;
1326 caddr_t data;
1327 struct ifnet *ifp;
1328 struct proc *p;
1329 {
1330 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1331 struct ifaddr *ifa;
1332 struct sockaddr *sa;
1333
1334 /* sanity checks */
1335 if (!data || !ifp) {
1336 panic("invalid argument to in6_lifaddr_ioctl");
1337 /*NOTRECHED*/
1338 }
1339
1340 switch (cmd) {
1341 case SIOCGLIFADDR:
1342 /* address must be specified on GET with IFLR_PREFIX */
1343 if ((iflr->flags & IFLR_PREFIX) == 0)
1344 break;
1345 /* FALLTHROUGH */
1346 case SIOCALIFADDR:
1347 case SIOCDLIFADDR:
1348 /* address must be specified on ADD and DELETE */
1349 sa = (struct sockaddr *)&iflr->addr;
1350 if (sa->sa_family != AF_INET6)
1351 return EINVAL;
1352 if (sa->sa_len != sizeof(struct sockaddr_in6))
1353 return EINVAL;
1354 /* XXX need improvement */
1355 sa = (struct sockaddr *)&iflr->dstaddr;
1356 if (sa->sa_family && sa->sa_family != AF_INET6)
1357 return EINVAL;
1358 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1359 return EINVAL;
1360 break;
1361 default: /* shouldn't happen */
1362 #if 0
1363 panic("invalid cmd to in6_lifaddr_ioctl");
1364 /* NOTREACHED */
1365 #else
1366 return EOPNOTSUPP;
1367 #endif
1368 }
1369 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1370 return EINVAL;
1371
1372 switch (cmd) {
1373 case SIOCALIFADDR:
1374 {
1375 struct in6_aliasreq ifra;
1376 struct in6_addr *hostid = NULL;
1377 int prefixlen;
1378
1379 if ((iflr->flags & IFLR_PREFIX) != 0) {
1380 struct sockaddr_in6 *sin6;
1381
1382 /*
1383 * hostid is to fill in the hostid part of the
1384 * address. hostid points to the first link-local
1385 * address attached to the interface.
1386 */
1387 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1388 if (!ifa)
1389 return EADDRNOTAVAIL;
1390 hostid = IFA_IN6(ifa);
1391
1392 /* prefixlen must be <= 64. */
1393 if (64 < iflr->prefixlen)
1394 return EINVAL;
1395 prefixlen = iflr->prefixlen;
1396
1397 /* hostid part must be zero. */
1398 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1399 if (sin6->sin6_addr.s6_addr32[2] != 0
1400 || sin6->sin6_addr.s6_addr32[3] != 0) {
1401 return EINVAL;
1402 }
1403 } else
1404 prefixlen = iflr->prefixlen;
1405
1406 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1407 bzero(&ifra, sizeof(ifra));
1408 bcopy(iflr->iflr_name, ifra.ifra_name,
1409 sizeof(ifra.ifra_name));
1410
1411 bcopy(&iflr->addr, &ifra.ifra_addr,
1412 ((struct sockaddr *)&iflr->addr)->sa_len);
1413 if (hostid) {
1414 /* fill in hostid part */
1415 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1416 hostid->s6_addr32[2];
1417 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1418 hostid->s6_addr32[3];
1419 }
1420
1421 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1422 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1423 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1424 if (hostid) {
1425 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1426 hostid->s6_addr32[2];
1427 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1428 hostid->s6_addr32[3];
1429 }
1430 }
1431
1432 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1433 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1434
1435 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1436 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1437 }
1438 case SIOCGLIFADDR:
1439 case SIOCDLIFADDR:
1440 {
1441 struct in6_ifaddr *ia;
1442 struct in6_addr mask, candidate, match;
1443 struct sockaddr_in6 *sin6;
1444 int cmp;
1445
1446 bzero(&mask, sizeof(mask));
1447 if (iflr->flags & IFLR_PREFIX) {
1448 /* lookup a prefix rather than address. */
1449 in6_len2mask(&mask, iflr->prefixlen);
1450
1451 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1452 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1453 match.s6_addr32[0] &= mask.s6_addr32[0];
1454 match.s6_addr32[1] &= mask.s6_addr32[1];
1455 match.s6_addr32[2] &= mask.s6_addr32[2];
1456 match.s6_addr32[3] &= mask.s6_addr32[3];
1457
1458 /* if you set extra bits, that's wrong */
1459 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1460 return EINVAL;
1461
1462 cmp = 1;
1463 } else {
1464 if (cmd == SIOCGLIFADDR) {
1465 /* on getting an address, take the 1st match */
1466 cmp = 0; /* XXX */
1467 } else {
1468 /* on deleting an address, do exact match */
1469 in6_len2mask(&mask, 128);
1470 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1471 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1472
1473 cmp = 1;
1474 }
1475 }
1476
1477 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1478 {
1479 if (ifa->ifa_addr->sa_family != AF_INET6)
1480 continue;
1481 if (!cmp)
1482 break;
1483
1484 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1485 #ifndef SCOPEDROUTING
1486 /*
1487 * XXX: this is adhoc, but is necessary to allow
1488 * a user to specify fe80::/64 (not /10) for a
1489 * link-local address.
1490 */
1491 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1492 candidate.s6_addr16[1] = 0;
1493 #endif
1494 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1495 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1496 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1497 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1498 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1499 break;
1500 }
1501 if (!ifa)
1502 return EADDRNOTAVAIL;
1503 ia = ifa2ia6(ifa);
1504
1505 if (cmd == SIOCGLIFADDR) {
1506 #ifndef SCOPEDROUTING
1507 struct sockaddr_in6 *s6;
1508 #endif
1509
1510 /* fill in the if_laddrreq structure */
1511 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1512 #ifndef SCOPEDROUTING /* XXX see above */
1513 s6 = (struct sockaddr_in6 *)&iflr->addr;
1514 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1515 s6->sin6_addr.s6_addr16[1] = 0;
1516 s6->sin6_scope_id =
1517 in6_addr2scopeid(ifp, &s6->sin6_addr);
1518 }
1519 #endif
1520 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1521 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1522 ia->ia_dstaddr.sin6_len);
1523 #ifndef SCOPEDROUTING /* XXX see above */
1524 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1525 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1526 s6->sin6_addr.s6_addr16[1] = 0;
1527 s6->sin6_scope_id =
1528 in6_addr2scopeid(ifp,
1529 &s6->sin6_addr);
1530 }
1531 #endif
1532 } else
1533 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1534
1535 iflr->prefixlen =
1536 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1537 NULL);
1538
1539 iflr->flags = ia->ia6_flags; /* XXX */
1540
1541 return 0;
1542 } else {
1543 struct in6_aliasreq ifra;
1544
1545 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1546 bzero(&ifra, sizeof(ifra));
1547 bcopy(iflr->iflr_name, ifra.ifra_name,
1548 sizeof(ifra.ifra_name));
1549
1550 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1551 ia->ia_addr.sin6_len);
1552 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1553 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1554 ia->ia_dstaddr.sin6_len);
1555 } else {
1556 bzero(&ifra.ifra_dstaddr,
1557 sizeof(ifra.ifra_dstaddr));
1558 }
1559 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1560 ia->ia_prefixmask.sin6_len);
1561
1562 ifra.ifra_flags = ia->ia6_flags;
1563 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1564 ifp, p);
1565 }
1566 }
1567 }
1568
1569 return EOPNOTSUPP; /* just for safety */
1570 }
1571
1572 /*
1573 * Initialize an interface's intetnet6 address
1574 * and routing table entry.
1575 */
1576 static int
1577 in6_ifinit(ifp, ia, sin6, newhost)
1578 struct ifnet *ifp;
1579 struct in6_ifaddr *ia;
1580 struct sockaddr_in6 *sin6;
1581 int newhost;
1582 {
1583 int error = 0, plen, ifacount = 0;
1584 int s = splimp();
1585 struct ifaddr *ifa;
1586
1587 /*
1588 * Give the interface a chance to initialize
1589 * if this is its first address,
1590 * and to validate the address if necessary.
1591 */
1592 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1593 {
1594 if (ifa->ifa_addr == NULL)
1595 continue; /* just for safety */
1596 if (ifa->ifa_addr->sa_family != AF_INET6)
1597 continue;
1598 ifacount++;
1599 }
1600
1601 ia->ia_addr = *sin6;
1602
1603 if (ifacount <= 1 && ifp->if_ioctl &&
1604 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1605 splx(s);
1606 return(error);
1607 }
1608 splx(s);
1609
1610 ia->ia_ifa.ifa_metric = ifp->if_metric;
1611
1612 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1613
1614 /*
1615 * Special case:
1616 * If a new destination address is specified for a point-to-point
1617 * interface, install a route to the destination as an interface
1618 * direct route.
1619 * XXX: the logic below rejects assigning multiple addresses on a p2p
1620 * interface that share a same destination.
1621 */
1622 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1623 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1624 ia->ia_dstaddr.sin6_family == AF_INET6) {
1625 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1626 RTF_UP | RTF_HOST)) != 0)
1627 return(error);
1628 ia->ia_flags |= IFA_ROUTE;
1629 }
1630 if (plen < 128) {
1631 /*
1632 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1633 */
1634 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1635 }
1636
1637 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1638 if (newhost) {
1639 /* set the rtrequest function to create llinfo */
1640 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1641 in6_ifaddloop(&(ia->ia_ifa));
1642 }
1643
1644 return(error);
1645 }
1646
1647 /*
1648 * Add an address to the list of IP6 multicast addresses for a
1649 * given interface.
1650 */
1651 struct in6_multi *
1652 in6_addmulti(maddr6, ifp, errorp)
1653 struct in6_addr *maddr6;
1654 struct ifnet *ifp;
1655 int *errorp;
1656 {
1657 struct in6_multi *in6m;
1658 struct sockaddr_in6 sin6;
1659 struct ifmultiaddr *ifma;
1660 int s = splnet();
1661
1662 *errorp = 0;
1663
1664 /*
1665 * Call generic routine to add membership or increment
1666 * refcount. It wants addresses in the form of a sockaddr,
1667 * so we build one here (being careful to zero the unused bytes).
1668 */
1669 bzero(&sin6, sizeof sin6);
1670 sin6.sin6_family = AF_INET6;
1671 sin6.sin6_len = sizeof sin6;
1672 sin6.sin6_addr = *maddr6;
1673 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1674 if (*errorp) {
1675 splx(s);
1676 return 0;
1677 }
1678
1679 /*
1680 * If ifma->ifma_protospec is null, then if_addmulti() created
1681 * a new record. Otherwise, we are done.
1682 */
1683 if (ifma->ifma_protospec != 0) {
1684 splx(s);
1685 return ifma->ifma_protospec;
1686 }
1687
1688 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1689 at interrupt time? If so, need to fix if_addmulti. XXX */
1690 in6m = (struct in6_multi *)malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1691 if (in6m == NULL) {
1692 splx(s);
1693 return (NULL);
1694 }
1695
1696 bzero(in6m, sizeof *in6m);
1697 in6m->in6m_addr = *maddr6;
1698 in6m->in6m_ifp = ifp;
1699 in6m->in6m_ifma = ifma;
1700 ifma->ifma_protospec = in6m;
1701 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1702
1703 /*
1704 * Let MLD6 know that we have joined a new IP6 multicast
1705 * group.
1706 */
1707 mld6_start_listening(in6m);
1708 splx(s);
1709 return(in6m);
1710 }
1711
1712 /*
1713 * Delete a multicast address record.
1714 */
1715 void
1716 in6_delmulti(in6m)
1717 struct in6_multi *in6m;
1718 {
1719 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1720 int s = splnet();
1721
1722 if (ifma->ifma_refcount == 1) {
1723 /*
1724 * No remaining claims to this record; let MLD6 know
1725 * that we are leaving the multicast group.
1726 */
1727 mld6_stop_listening(in6m);
1728 ifma->ifma_protospec = 0;
1729 LIST_REMOVE(in6m, in6m_entry);
1730 free(in6m, M_IPMADDR);
1731 }
1732 /* XXX - should be separate API for when we have an ifma? */
1733 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1734 splx(s);
1735 }
1736
1737 /*
1738 * Find an IPv6 interface link-local address specific to an interface.
1739 */
1740 struct in6_ifaddr *
1741 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1742 struct ifnet *ifp;
1743 int ignoreflags;
1744 {
1745 struct ifaddr *ifa;
1746
1747 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1748 {
1749 if (ifa->ifa_addr == NULL)
1750 continue; /* just for safety */
1751 if (ifa->ifa_addr->sa_family != AF_INET6)
1752 continue;
1753 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1754 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1755 ignoreflags) != 0)
1756 continue;
1757 break;
1758 }
1759 }
1760
1761 return((struct in6_ifaddr *)ifa);
1762 }
1763
1764
1765 /*
1766 * find the internet address corresponding to a given interface and address.
1767 */
1768 struct in6_ifaddr *
1769 in6ifa_ifpwithaddr(ifp, addr)
1770 struct ifnet *ifp;
1771 struct in6_addr *addr;
1772 {
1773 struct ifaddr *ifa;
1774
1775 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1776 {
1777 if (ifa->ifa_addr == NULL)
1778 continue; /* just for safety */
1779 if (ifa->ifa_addr->sa_family != AF_INET6)
1780 continue;
1781 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1782 break;
1783 }
1784
1785 return((struct in6_ifaddr *)ifa);
1786 }
1787
1788 /*
1789 * Convert IP6 address to printable (loggable) representation.
1790 */
1791 static char digits[] = "0123456789abcdef";
1792 static int ip6round = 0;
1793 char *
1794 ip6_sprintf(addr)
1795 const struct in6_addr *addr;
1796 {
1797 static char ip6buf[8][48];
1798 int i;
1799 char *cp;
1800 u_short *a = (u_short *)addr;
1801 u_char *d;
1802 int dcolon = 0;
1803
1804 ip6round = (ip6round + 1) & 7;
1805 cp = ip6buf[ip6round];
1806
1807 for (i = 0; i < 8; i++) {
1808 if (dcolon == 1) {
1809 if (*a == 0) {
1810 if (i == 7)
1811 *cp++ = ':';
1812 a++;
1813 continue;
1814 } else
1815 dcolon = 2;
1816 }
1817 if (*a == 0) {
1818 if (dcolon == 0 && *(a + 1) == 0) {
1819 if (i == 0)
1820 *cp++ = ':';
1821 *cp++ = ':';
1822 dcolon = 1;
1823 } else {
1824 *cp++ = '';
1825 *cp++ = ':';
1826 }
1827 a++;
1828 continue;
1829 }
1830 d = (u_char *)a;
1831 *cp++ = digits[*d >> 4];
1832 *cp++ = digits[*d++ & 0xf];
1833 *cp++ = digits[*d >> 4];
1834 *cp++ = digits[*d & 0xf];
1835 *cp++ = ':';
1836 a++;
1837 }
1838 *--cp = 0;
1839 return(ip6buf[ip6round]);
1840 }
1841
1842 int
1843 in6_localaddr(in6)
1844 struct in6_addr *in6;
1845 {
1846 struct in6_ifaddr *ia;
1847
1848 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1849 return 1;
1850
1851 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1852 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1853 &ia->ia_prefixmask.sin6_addr))
1854 return 1;
1855
1856 return (0);
1857 }
1858
1859 int
1860 in6_is_addr_deprecated(sa6)
1861 struct sockaddr_in6 *sa6;
1862 {
1863 struct in6_ifaddr *ia;
1864
1865 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1866 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1867 &sa6->sin6_addr) &&
1868 #ifdef SCOPEDROUTING
1869 ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
1870 #endif
1871 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1872 return(1); /* true */
1873
1874 /* XXX: do we still have to go thru the rest of the list? */
1875 }
1876
1877 return(0); /* false */
1878 }
1879
1880 /*
1881 * return length of part which dst and src are equal
1882 * hard coding...
1883 */
1884 int
1885 in6_matchlen(src, dst)
1886 struct in6_addr *src, *dst;
1887 {
1888 int match = 0;
1889 u_char *s = (u_char *)src, *d = (u_char *)dst;
1890 u_char *lim = s + 16, r;
1891
1892 while (s < lim)
1893 if ((r = (*d++ ^ *s++)) != 0) {
1894 while (r < 128) {
1895 match++;
1896 r <<= 1;
1897 }
1898 break;
1899 } else
1900 match += 8;
1901 return match;
1902 }
1903
1904 /* XXX: to be scope conscious */
1905 int
1906 in6_are_prefix_equal(p1, p2, len)
1907 struct in6_addr *p1, *p2;
1908 int len;
1909 {
1910 int bytelen, bitlen;
1911
1912 /* sanity check */
1913 if (0 > len || len > 128) {
1914 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1915 len);
1916 return(0);
1917 }
1918
1919 bytelen = len / 8;
1920 bitlen = len % 8;
1921
1922 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1923 return(0);
1924 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1925 p2->s6_addr[bytelen] >> (8 - bitlen))
1926 return(0);
1927
1928 return(1);
1929 }
1930
1931 void
1932 in6_prefixlen2mask(maskp, len)
1933 struct in6_addr *maskp;
1934 int len;
1935 {
1936 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1937 int bytelen, bitlen, i;
1938
1939 /* sanity check */
1940 if (0 > len || len > 128) {
1941 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1942 len);
1943 return;
1944 }
1945
1946 bzero(maskp, sizeof(*maskp));
1947 bytelen = len / 8;
1948 bitlen = len % 8;
1949 for (i = 0; i < bytelen; i++)
1950 maskp->s6_addr[i] = 0xff;
1951 if (bitlen)
1952 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1953 }
1954
1955 /*
1956 * return the best address out of the same scope
1957 */
1958 struct in6_ifaddr *
1959 in6_ifawithscope(oifp, dst)
1960 struct ifnet *oifp;
1961 struct in6_addr *dst;
1962 {
1963 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
1964 int blen = -1;
1965 struct ifaddr *ifa;
1966 struct ifnet *ifp;
1967 struct in6_ifaddr *ifa_best = NULL;
1968
1969 if (oifp == NULL) {
1970 #if 0
1971 printf("in6_ifawithscope: output interface is not specified\n");
1972 #endif
1973 return(NULL);
1974 }
1975
1976 /*
1977 * We search for all addresses on all interfaces from the beginning.
1978 * Comparing an interface with the outgoing interface will be done
1979 * only at the final stage of tiebreaking.
1980 */
1981 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1982 {
1983 /*
1984 * We can never take an address that breaks the scope zone
1985 * of the destination.
1986 */
1987 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
1988 continue;
1989
1990 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1991 {
1992 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
1993
1994 if (ifa->ifa_addr->sa_family != AF_INET6)
1995 continue;
1996
1997 src_scope = in6_addrscope(IFA_IN6(ifa));
1998
1999 /*
2000 * Don't use an address before completing DAD
2001 * nor a duplicated address.
2002 */
2003 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2004 IN6_IFF_NOTREADY)
2005 continue;
2006
2007 /* XXX: is there any case to allow anycasts? */
2008 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2009 IN6_IFF_ANYCAST)
2010 continue;
2011
2012 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2013 IN6_IFF_DETACHED)
2014 continue;
2015
2016 /*
2017 * If this is the first address we find,
2018 * keep it anyway.
2019 */
2020 if (ifa_best == NULL)
2021 goto replace;
2022
2023 /*
2024 * ifa_best is never NULL beyond this line except
2025 * within the block labeled "replace".
2026 */
2027
2028 /*
2029 * If ifa_best has a smaller scope than dst and
2030 * the current address has a larger one than
2031 * (or equal to) dst, always replace ifa_best.
2032 * Also, if the current address has a smaller scope
2033 * than dst, ignore it unless ifa_best also has a
2034 * smaller scope.
2035 * Consequently, after the two if-clause below,
2036 * the followings must be satisfied:
2037 * (scope(src) < scope(dst) &&
2038 * scope(best) < scope(dst))
2039 * OR
2040 * (scope(best) >= scope(dst) &&
2041 * scope(src) >= scope(dst))
2042 */
2043 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2044 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2045 goto replace; /* (A) */
2046 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2047 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2048 continue; /* (B) */
2049
2050 /*
2051 * A deprecated address SHOULD NOT be used in new
2052 * communications if an alternate (non-deprecated)
2053 * address is available and has sufficient scope.
2054 * RFC 2462, Section 5.5.4.
2055 */
2056 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2057 IN6_IFF_DEPRECATED) {
2058 /*
2059 * Ignore any deprecated addresses if
2060 * specified by configuration.
2061 */
2062 if (!ip6_use_deprecated)
2063 continue;
2064
2065 /*
2066 * If we have already found a non-deprecated
2067 * candidate, just ignore deprecated addresses.
2068 */
2069 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2070 == 0)
2071 continue;
2072 }
2073
2074 /*
2075 * A non-deprecated address is always preferred
2076 * to a deprecated one regardless of scopes and
2077 * address matching (Note invariants ensured by the
2078 * conditions (A) and (B) above.)
2079 */
2080 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2081 (((struct in6_ifaddr *)ifa)->ia6_flags &
2082 IN6_IFF_DEPRECATED) == 0)
2083 goto replace;
2084
2085 /*
2086 * When we use temporary addresses described in
2087 * RFC 3041, we prefer temporary addresses to
2088 * public autoconf addresses. Again, note the
2089 * invariants from (A) and (B). Also note that we
2090 * don't have any preference between static addresses
2091 * and autoconf addresses (despite of whether or not
2092 * the latter is temporary or public.)
2093 */
2094 if (ip6_use_tempaddr) {
2095 struct in6_ifaddr *ifat;
2096
2097 ifat = (struct in6_ifaddr *)ifa;
2098 if ((ifa_best->ia6_flags &
2099 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2100 == IN6_IFF_AUTOCONF &&
2101 (ifat->ia6_flags &
2102 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2103 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2104 goto replace;
2105 }
2106 if ((ifa_best->ia6_flags &
2107 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2108 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2109 (ifat->ia6_flags &
2110 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2111 == IN6_IFF_AUTOCONF) {
2112 continue;
2113 }
2114 }
2115
2116 /*
2117 * At this point, we have two cases:
2118 * 1. we are looking at a non-deprecated address,
2119 * and ifa_best is also non-deprecated.
2120 * 2. we are looking at a deprecated address,
2121 * and ifa_best is also deprecated.
2122 * Also, we do not have to consider a case where
2123 * the scope of if_best is larger(smaller) than dst and
2124 * the scope of the current address is smaller(larger)
2125 * than dst. Such a case has already been covered.
2126 * Tiebreaking is done according to the following
2127 * items:
2128 * - the scope comparison between the address and
2129 * dst (dscopecmp)
2130 * - the scope comparison between the address and
2131 * ifa_best (bscopecmp)
2132 * - if the address match dst longer than ifa_best
2133 * (matchcmp)
2134 * - if the address is on the outgoing I/F (outI/F)
2135 *
2136 * Roughly speaking, the selection policy is
2137 * - the most important item is scope. The same scope
2138 * is best. Then search for a larger scope.
2139 * Smaller scopes are the last resort.
2140 * - A deprecated address is chosen only when we have
2141 * no address that has an enough scope, but is
2142 * prefered to any addresses of smaller scopes
2143 * (this must be already done above.)
2144 * - addresses on the outgoing I/F are preferred to
2145 * ones on other interfaces if none of above
2146 * tiebreaks. In the table below, the column "bI"
2147 * means if the best_ifa is on the outgoing
2148 * interface, and the column "sI" means if the ifa
2149 * is on the outgoing interface.
2150 * - If there is no other reasons to choose one,
2151 * longest address match against dst is considered.
2152 *
2153 * The precise decision table is as follows:
2154 * dscopecmp bscopecmp match bI oI | replace?
2155 * N/A equal N/A Y N | No (1)
2156 * N/A equal N/A N Y | Yes (2)
2157 * N/A equal larger N/A | Yes (3)
2158 * N/A equal !larger N/A | No (4)
2159 * larger larger N/A N/A | No (5)
2160 * larger smaller N/A N/A | Yes (6)
2161 * smaller larger N/A N/A | Yes (7)
2162 * smaller smaller N/A N/A | No (8)
2163 * equal smaller N/A N/A | Yes (9)
2164 * equal larger (already done at A above)
2165 */
2166 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2167 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2168
2169 if (bscopecmp == 0) {
2170 struct ifnet *bifp = ifa_best->ia_ifp;
2171
2172 if (bifp == oifp && ifp != oifp) /* (1) */
2173 continue;
2174 if (bifp != oifp && ifp == oifp) /* (2) */
2175 goto replace;
2176
2177 /*
2178 * Both bifp and ifp are on the outgoing
2179 * interface, or both two are on a different
2180 * interface from the outgoing I/F.
2181 * now we need address matching against dst
2182 * for tiebreaking.
2183 */
2184 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2185 matchcmp = tlen - blen;
2186 if (matchcmp > 0) /* (3) */
2187 goto replace;
2188 continue; /* (4) */
2189 }
2190 if (dscopecmp > 0) {
2191 if (bscopecmp > 0) /* (5) */
2192 continue;
2193 goto replace; /* (6) */
2194 }
2195 if (dscopecmp < 0) {
2196 if (bscopecmp > 0) /* (7) */
2197 goto replace;
2198 continue; /* (8) */
2199 }
2200
2201 /* now dscopecmp must be 0 */
2202 if (bscopecmp < 0)
2203 goto replace; /* (9) */
2204
2205 replace:
2206 ifa_best = (struct in6_ifaddr *)ifa;
2207 blen = tlen >= 0 ? tlen :
2208 in6_matchlen(IFA_IN6(ifa), dst);
2209 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2210 }
2211 }
2212
2213 /* count statistics for future improvements */
2214 if (ifa_best == NULL)
2215 ip6stat.ip6s_sources_none++;
2216 else {
2217 if (oifp == ifa_best->ia_ifp)
2218 ip6stat.ip6s_sources_sameif[best_scope]++;
2219 else
2220 ip6stat.ip6s_sources_otherif[best_scope]++;
2221
2222 if (best_scope == dst_scope)
2223 ip6stat.ip6s_sources_samescope[best_scope]++;
2224 else
2225 ip6stat.ip6s_sources_otherscope[best_scope]++;
2226
2227 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2228 ip6stat.ip6s_sources_deprecated[best_scope]++;
2229 }
2230
2231 return(ifa_best);
2232 }
2233
2234 /*
2235 * return the best address out of the same scope. if no address was
2236 * found, return the first valid address from designated IF.
2237 */
2238 struct in6_ifaddr *
2239 in6_ifawithifp(ifp, dst)
2240 struct ifnet *ifp;
2241 struct in6_addr *dst;
2242 {
2243 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2244 struct ifaddr *ifa;
2245 struct in6_ifaddr *besta = 0;
2246 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2247
2248 dep[0] = dep[1] = NULL;
2249
2250 /*
2251 * We first look for addresses in the same scope.
2252 * If there is one, return it.
2253 * If two or more, return one which matches the dst longest.
2254 * If none, return one of global addresses assigned other ifs.
2255 */
2256 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2257 {
2258 if (ifa->ifa_addr->sa_family != AF_INET6)
2259 continue;
2260 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2261 continue; /* XXX: is there any case to allow anycast? */
2262 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2263 continue; /* don't use this interface */
2264 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2265 continue;
2266 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2267 if (ip6_use_deprecated)
2268 dep[0] = (struct in6_ifaddr *)ifa;
2269 continue;
2270 }
2271
2272 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2273 /*
2274 * call in6_matchlen() as few as possible
2275 */
2276 if (besta) {
2277 if (blen == -1)
2278 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2279 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2280 if (tlen > blen) {
2281 blen = tlen;
2282 besta = (struct in6_ifaddr *)ifa;
2283 }
2284 } else
2285 besta = (struct in6_ifaddr *)ifa;
2286 }
2287 }
2288 if (besta)
2289 return(besta);
2290
2291 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2292 {
2293 if (ifa->ifa_addr->sa_family != AF_INET6)
2294 continue;
2295 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2296 continue; /* XXX: is there any case to allow anycast? */
2297 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2298 continue; /* don't use this interface */
2299 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2300 continue;
2301 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2302 if (ip6_use_deprecated)
2303 dep[1] = (struct in6_ifaddr *)ifa;
2304 continue;
2305 }
2306
2307 return (struct in6_ifaddr *)ifa;
2308 }
2309
2310 /* use the last-resort values, that are, deprecated addresses */
2311 if (dep[0])
2312 return dep[0];
2313 if (dep[1])
2314 return dep[1];
2315
2316 return NULL;
2317 }
2318
2319 /*
2320 * perform DAD when interface becomes IFF_UP.
2321 */
2322 void
2323 in6_if_up(ifp)
2324 struct ifnet *ifp;
2325 {
2326 struct ifaddr *ifa;
2327 struct in6_ifaddr *ia;
2328 int dad_delay; /* delay ticks before DAD output */
2329
2330 /*
2331 * special cases, like 6to4, are handled in in6_ifattach
2332 */
2333 in6_ifattach(ifp, NULL);
2334
2335 dad_delay = 0;
2336 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2337 {
2338 if (ifa->ifa_addr->sa_family != AF_INET6)
2339 continue;
2340 ia = (struct in6_ifaddr *)ifa;
2341 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2342 nd6_dad_start(ifa, &dad_delay);
2343 }
2344 }
2345
2346 int
2347 in6if_do_dad(ifp)
2348 struct ifnet *ifp;
2349 {
2350 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2351 return(0);
2352
2353 switch (ifp->if_type) {
2354 #ifdef IFT_DUMMY
2355 case IFT_DUMMY:
2356 #endif
2357 case IFT_FAITH:
2358 /*
2359 * These interfaces do not have the IFF_LOOPBACK flag,
2360 * but loop packets back. We do not have to do DAD on such
2361 * interfaces. We should even omit it, because loop-backed
2362 * NS would confuse the DAD procedure.
2363 */
2364 return(0);
2365 default:
2366 /*
2367 * Our DAD routine requires the interface up and running.
2368 * However, some interfaces can be up before the RUNNING
2369 * status. Additionaly, users may try to assign addresses
2370 * before the interface becomes up (or running).
2371 * We simply skip DAD in such a case as a work around.
2372 * XXX: we should rather mark "tentative" on such addresses,
2373 * and do DAD after the interface becomes ready.
2374 */
2375 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2376 (IFF_UP|IFF_RUNNING))
2377 return(0);
2378
2379 return(1);
2380 }
2381 }
2382
2383 /*
2384 * Calculate max IPv6 MTU through all the interfaces and store it
2385 * to in6_maxmtu.
2386 */
2387 void
2388 in6_setmaxmtu()
2389 {
2390 unsigned long maxmtu = 0;
2391 struct ifnet *ifp;
2392
2393 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2394 {
2395 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2396 nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
2397 maxmtu = nd_ifinfo[ifp->if_index].linkmtu;
2398 }
2399 if (maxmtu) /* update only when maxmtu is positive */
2400 in6_maxmtu = maxmtu;
2401 }
2402
2403 /*
2404 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2405 * v4 mapped addr or v4 compat addr
2406 */
2407 void
2408 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2409 {
2410 bzero(sin, sizeof(*sin));
2411 sin->sin_len = sizeof(struct sockaddr_in);
2412 sin->sin_family = AF_INET;
2413 sin->sin_port = sin6->sin6_port;
2414 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2415 }
2416
2417 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2418 void
2419 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2420 {
2421 bzero(sin6, sizeof(*sin6));
2422 sin6->sin6_len = sizeof(struct sockaddr_in6);
2423 sin6->sin6_family = AF_INET6;
2424 sin6->sin6_port = sin->sin_port;
2425 sin6->sin6_addr.s6_addr32[0] = 0;
2426 sin6->sin6_addr.s6_addr32[1] = 0;
2427 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2428 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2429 }
2430
2431 /* Convert sockaddr_in6 into sockaddr_in. */
2432 void
2433 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2434 {
2435 struct sockaddr_in *sin_p;
2436 struct sockaddr_in6 sin6;
2437
2438 /*
2439 * Save original sockaddr_in6 addr and convert it
2440 * to sockaddr_in.
2441 */
2442 sin6 = *(struct sockaddr_in6 *)nam;
2443 sin_p = (struct sockaddr_in *)nam;
2444 in6_sin6_2_sin(sin_p, &sin6);
2445 }
2446
2447 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2448 void
2449 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2450 {
2451 struct sockaddr_in *sin_p;
2452 struct sockaddr_in6 *sin6_p;
2453
2454 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2455 M_WAITOK);
2456 sin_p = (struct sockaddr_in *)*nam;
2457 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2458 FREE(*nam, M_SONAME);
2459 *nam = (struct sockaddr *)sin6_p;
2460 }
Cache object: 98d04c0756dacbab92875c9e3e846134
|