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