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