in6.c revision 1.79 1 /* $NetBSD: in6.c,v 1.79 2003/09/05 23:20:50 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.79 2003/09/05 23:20:50 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_index < 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 int e;
1005
1006 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1007 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
1008 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
1009 "a route to the old destination: %s\n",
1010 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1011 /* proceed anyway... */
1012 } else
1013 ia->ia_flags &= ~IFA_ROUTE;
1014 ia->ia_dstaddr = dst6;
1015 }
1016
1017 /*
1018 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1019 * to see if the address is deprecated or invalidated, but initialize
1020 * these members for applications.
1021 */
1022 ia->ia6_lifetime = ifra->ifra_lifetime;
1023 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1024 ia->ia6_lifetime.ia6t_expire =
1025 time.tv_sec + ia->ia6_lifetime.ia6t_vltime;
1026 } else
1027 ia->ia6_lifetime.ia6t_expire = 0;
1028 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1029 ia->ia6_lifetime.ia6t_preferred =
1030 time.tv_sec + ia->ia6_lifetime.ia6t_pltime;
1031 } else
1032 ia->ia6_lifetime.ia6t_preferred = 0;
1033
1034 /* reset the interface and routing table appropriately. */
1035 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1036 goto unlink;
1037
1038 /*
1039 * configure address flags.
1040 */
1041 ia->ia6_flags = ifra->ifra_flags;
1042 /*
1043 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1044 * userland, make it deprecated.
1045 */
1046 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1047 ia->ia6_lifetime.ia6t_pltime = 0;
1048 ia->ia6_lifetime.ia6t_preferred = time.tv_sec;
1049 }
1050 /*
1051 * Make the address tentative before joining multicast addresses,
1052 * so that corresponding MLD responses would not have a tentative
1053 * source address.
1054 */
1055 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1056 if (hostIsNew && in6if_do_dad(ifp))
1057 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1058
1059 /*
1060 * Beyond this point, we should call in6_purgeaddr upon an error,
1061 * not just go to unlink.
1062 */
1063
1064 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1065 struct sockaddr_in6 mltaddr, mltmask;
1066 #ifndef SCOPEDROUTING
1067 u_int32_t zoneid = 0;
1068 #endif
1069
1070 if (hostIsNew) {
1071 /* join solicited multicast addr for new host id */
1072 struct sockaddr_in6 llsol;
1073
1074 bzero(&llsol, sizeof(llsol));
1075 llsol.sin6_family = AF_INET6;
1076 llsol.sin6_len = sizeof(llsol);
1077 llsol.sin6_addr.s6_addr16[0] = htons(0xff02);
1078 llsol.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1079 llsol.sin6_addr.s6_addr32[1] = 0;
1080 llsol.sin6_addr.s6_addr32[2] = htonl(1);
1081 llsol.sin6_addr.s6_addr32[3] =
1082 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1083 llsol.sin6_addr.s6_addr8[12] = 0xff;
1084 imm = in6_joingroup(ifp, &llsol.sin6_addr, &error);
1085 if (imm) {
1086 LIST_INSERT_HEAD(&ia->ia6_memberships, imm,
1087 i6mm_chain);
1088 } else {
1089 nd6log((LOG_ERR, "in6_update_ifa: addmulti "
1090 "failed for %s on %s (errno=%d)\n",
1091 ip6_sprintf(&llsol.sin6_addr),
1092 if_name(ifp), error));
1093 goto cleanup;
1094 }
1095 }
1096
1097 bzero(&mltmask, sizeof(mltmask));
1098 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1099 mltmask.sin6_family = AF_INET6;
1100 mltmask.sin6_addr = in6mask32;
1101
1102 /*
1103 * join link-local all-nodes address
1104 */
1105 bzero(&mltaddr, sizeof(mltaddr));
1106 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1107 mltaddr.sin6_family = AF_INET6;
1108 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1109 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1110
1111 /*
1112 * XXX: do we really need this automatic routes?
1113 * We should probably reconsider this stuff. Most applications
1114 * actually do not need the routes, since they usually specify
1115 * the outgoing interface.
1116 */
1117 rt = rtalloc1((struct sockaddr *)&mltaddr, 0);
1118 if (rt) {
1119 /*
1120 * 32bit came from "mltmask"
1121 * XXX: only works in !SCOPEDROUTING case.
1122 */
1123 if (memcmp(&mltaddr.sin6_addr,
1124 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1125 32 / 8)) {
1126 RTFREE(rt);
1127 rt = NULL;
1128 }
1129 }
1130 if (!rt) {
1131 struct rt_addrinfo info;
1132
1133 bzero(&info, sizeof(info));
1134 info.rti_info[RTAX_DST] = (struct sockaddr *)&mltaddr;
1135 info.rti_info[RTAX_GATEWAY] =
1136 (struct sockaddr *)&ia->ia_addr;
1137 info.rti_info[RTAX_NETMASK] =
1138 (struct sockaddr *)&mltmask;
1139 info.rti_info[RTAX_IFA] =
1140 (struct sockaddr *)&ia->ia_addr;
1141 /* XXX: we need RTF_CLONING to fake nd6_rtrequest */
1142 info.rti_flags = RTF_UP | RTF_CLONING;
1143 error = rtrequest1(RTM_ADD, &info, NULL);
1144 if (error)
1145 goto cleanup;
1146 } else {
1147 RTFREE(rt);
1148 }
1149 #ifndef SCOPEDROUTING
1150 mltaddr.sin6_scope_id = zoneid; /* XXX */
1151 #endif
1152 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
1153 if (imm) {
1154 LIST_INSERT_HEAD(&ia->ia6_memberships, imm,
1155 i6mm_chain);
1156 } else {
1157 nd6log((LOG_WARNING,
1158 "in6_update_ifa: addmulti failed for "
1159 "%s on %s (errno=%d)\n",
1160 ip6_sprintf(&mltaddr.sin6_addr),
1161 if_name(ifp), error));
1162 goto cleanup;
1163 }
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 LIST_INSERT_HEAD(&ia->ia6_memberships, imm,
1172 i6mm_chain);
1173 } else {
1174 nd6log((LOG_WARNING, "in6_update_ifa: "
1175 "addmulti failed for %s on %s (errno=%d)\n",
1176 ip6_sprintf(&mltaddr.sin6_addr),
1177 if_name(ifp), error));
1178 /* XXX not very fatal, go on... */
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 LIST_INSERT_HEAD(&ia->ia6_memberships, imm,
1221 i6mm_chain);
1222 } else {
1223 nd6log((LOG_WARNING, "in6_update_ifa: "
1224 "addmulti failed for %s on %s "
1225 "(errno=%d)\n",
1226 ip6_sprintf(&mltaddr.sin6_addr),
1227 if_name(ifp), error));
1228 goto cleanup;
1229 }
1230 }
1231 }
1232
1233 /*
1234 * Perform DAD, if needed.
1235 * XXX It may be of use, if we can administratively
1236 * disable DAD.
1237 */
1238 if (hostIsNew && in6if_do_dad(ifp) &&
1239 (ifra->ifra_flags & IN6_IFF_NODAD) == 0)
1240 {
1241 nd6_dad_start((struct ifaddr *)ia, NULL);
1242 }
1243
1244 return (error);
1245
1246 unlink:
1247 /*
1248 * XXX: if a change of an existing address failed, keep the entry
1249 * anyway.
1250 */
1251 if (hostIsNew)
1252 in6_unlink_ifa(ia, ifp);
1253 return (error);
1254
1255 cleanup:
1256 in6_purgeaddr(&ia->ia_ifa);
1257 return error;
1258 }
1259
1260 void
1261 in6_purgeaddr(ifa)
1262 struct ifaddr *ifa;
1263 {
1264 struct ifnet *ifp = ifa->ifa_ifp;
1265 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1266 struct in6_multi_mship *imm;
1267
1268 /* stop DAD processing */
1269 nd6_dad_stop(ifa);
1270
1271 /*
1272 * delete route to the destination of the address being purged.
1273 * The interface must be p2p or loopback in this case.
1274 */
1275 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1276 int e;
1277
1278 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1279 != 0) {
1280 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1281 "a route to the p2p destination: %s on %s, "
1282 "errno=%d\n",
1283 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1284 e);
1285 /* proceed anyway... */
1286 } else
1287 ia->ia_flags &= ~IFA_ROUTE;
1288 }
1289
1290 /* Remove ownaddr's loopback rtentry, if it exists. */
1291 in6_ifremloop(&(ia->ia_ifa));
1292
1293 /*
1294 * leave from multicast groups we have joined for the interface
1295 */
1296 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1297 LIST_REMOVE(imm, i6mm_chain);
1298 in6_leavegroup(imm);
1299 }
1300
1301 in6_unlink_ifa(ia, ifp);
1302 }
1303
1304 static void
1305 in6_unlink_ifa(ia, ifp)
1306 struct in6_ifaddr *ia;
1307 struct ifnet *ifp;
1308 {
1309 struct in6_ifaddr *oia;
1310 int s = splnet();
1311
1312 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1313 /* release a refcnt for the link from if_addrlist */
1314 IFAFREE(&ia->ia_ifa);
1315
1316 oia = ia;
1317 if (oia == (ia = in6_ifaddr))
1318 in6_ifaddr = ia->ia_next;
1319 else {
1320 while (ia->ia_next && (ia->ia_next != oia))
1321 ia = ia->ia_next;
1322 if (ia->ia_next)
1323 ia->ia_next = oia->ia_next;
1324 else {
1325 /* search failed */
1326 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1327 }
1328 }
1329
1330 if (oia->ia6_multiaddrs.lh_first != NULL) {
1331 /*
1332 * XXX thorpej (at) netbsd.org -- if the interface is going
1333 * XXX away, don't save the multicast entries, delete them!
1334 */
1335 if (oia->ia_ifa.ifa_ifp->if_output == if_nulloutput) {
1336 struct in6_multi *in6m;
1337
1338 while ((in6m =
1339 LIST_FIRST(&oia->ia6_multiaddrs)) != NULL)
1340 in6_delmulti(in6m);
1341 } else
1342 in6_savemkludge(oia);
1343 }
1344
1345 /*
1346 * When an autoconfigured address is being removed, release the
1347 * reference to the base prefix. Also, since the release might
1348 * affect the status of other (detached) addresses, call
1349 * pfxlist_onlink_check().
1350 */
1351 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1352 if (oia->ia6_ndpr == NULL) {
1353 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1354 "%p has no prefix\n", oia);
1355 } else {
1356 oia->ia6_ndpr->ndpr_refcnt--;
1357 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1358 oia->ia6_ndpr = NULL;
1359 }
1360
1361 pfxlist_onlink_check();
1362 }
1363
1364 /*
1365 * release another refcnt for the link from in6_ifaddr.
1366 * Note that we should decrement the refcnt at least once for all *BSD.
1367 */
1368 IFAFREE(&oia->ia_ifa);
1369
1370 splx(s);
1371 }
1372
1373 void
1374 in6_purgeif(ifp)
1375 struct ifnet *ifp;
1376 {
1377 struct ifaddr *ifa, *nifa;
1378
1379 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
1380 {
1381 nifa = TAILQ_NEXT(ifa, ifa_list);
1382 if (ifa->ifa_addr->sa_family != AF_INET6)
1383 continue;
1384 in6_purgeaddr(ifa);
1385 }
1386
1387 in6_ifdetach(ifp);
1388 }
1389
1390 /*
1391 * SIOC[GAD]LIFADDR.
1392 * SIOCGLIFADDR: get first address. (?)
1393 * SIOCGLIFADDR with IFLR_PREFIX:
1394 * get first address that matches the specified prefix.
1395 * SIOCALIFADDR: add the specified address.
1396 * SIOCALIFADDR with IFLR_PREFIX:
1397 * add the specified prefix, filling hostid part from
1398 * the first link-local address. prefixlen must be <= 64.
1399 * SIOCDLIFADDR: delete the specified address.
1400 * SIOCDLIFADDR with IFLR_PREFIX:
1401 * delete the first address that matches the specified prefix.
1402 * return values:
1403 * EINVAL on invalid parameters
1404 * EADDRNOTAVAIL on prefix match failed/specified address not found
1405 * other values may be returned from in6_ioctl()
1406 *
1407 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1408 * this is to accomodate address naming scheme other than RFC2374,
1409 * in the future.
1410 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1411 * address encoding scheme. (see figure on page 8)
1412 */
1413 static int
1414 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
1415 struct socket *so;
1416 u_long cmd;
1417 caddr_t data;
1418 struct ifnet *ifp;
1419 struct proc *p;
1420 {
1421 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1422 struct ifaddr *ifa;
1423 struct sockaddr *sa;
1424
1425 /* sanity checks */
1426 if (!data || !ifp) {
1427 panic("invalid argument to in6_lifaddr_ioctl");
1428 /* NOTREACHED */
1429 }
1430
1431 switch (cmd) {
1432 case SIOCGLIFADDR:
1433 /* address must be specified on GET with IFLR_PREFIX */
1434 if ((iflr->flags & IFLR_PREFIX) == 0)
1435 break;
1436 /* FALLTHROUGH */
1437 case SIOCALIFADDR:
1438 case SIOCDLIFADDR:
1439 /* address must be specified on ADD and DELETE */
1440 sa = (struct sockaddr *)&iflr->addr;
1441 if (sa->sa_family != AF_INET6)
1442 return EINVAL;
1443 if (sa->sa_len != sizeof(struct sockaddr_in6))
1444 return EINVAL;
1445 /* XXX need improvement */
1446 sa = (struct sockaddr *)&iflr->dstaddr;
1447 if (sa->sa_family && sa->sa_family != AF_INET6)
1448 return EINVAL;
1449 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1450 return EINVAL;
1451 break;
1452 default: /* shouldn't happen */
1453 #if 0
1454 panic("invalid cmd to in6_lifaddr_ioctl");
1455 /* NOTREACHED */
1456 #else
1457 return EOPNOTSUPP;
1458 #endif
1459 }
1460 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1461 return EINVAL;
1462
1463 switch (cmd) {
1464 case SIOCALIFADDR:
1465 {
1466 struct in6_aliasreq ifra;
1467 struct in6_addr *hostid = NULL;
1468 int prefixlen;
1469
1470 if ((iflr->flags & IFLR_PREFIX) != 0) {
1471 struct sockaddr_in6 *sin6;
1472
1473 /*
1474 * hostid is to fill in the hostid part of the
1475 * address. hostid points to the first link-local
1476 * address attached to the interface.
1477 */
1478 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1479 if (!ifa)
1480 return EADDRNOTAVAIL;
1481 hostid = IFA_IN6(ifa);
1482
1483 /* prefixlen must be <= 64. */
1484 if (64 < iflr->prefixlen)
1485 return EINVAL;
1486 prefixlen = iflr->prefixlen;
1487
1488 /* hostid part must be zero. */
1489 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1490 if (sin6->sin6_addr.s6_addr32[2] != 0
1491 || sin6->sin6_addr.s6_addr32[3] != 0) {
1492 return EINVAL;
1493 }
1494 } else
1495 prefixlen = iflr->prefixlen;
1496
1497 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1498 bzero(&ifra, sizeof(ifra));
1499 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1500
1501 bcopy(&iflr->addr, &ifra.ifra_addr,
1502 ((struct sockaddr *)&iflr->addr)->sa_len);
1503 if (hostid) {
1504 /* fill in hostid part */
1505 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1506 hostid->s6_addr32[2];
1507 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1508 hostid->s6_addr32[3];
1509 }
1510
1511 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1512 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1513 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1514 if (hostid) {
1515 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1516 hostid->s6_addr32[2];
1517 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1518 hostid->s6_addr32[3];
1519 }
1520 }
1521
1522 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1523 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1524
1525 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1526 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1527 }
1528 case SIOCGLIFADDR:
1529 case SIOCDLIFADDR:
1530 {
1531 struct in6_ifaddr *ia;
1532 struct in6_addr mask, candidate, match;
1533 struct sockaddr_in6 *sin6;
1534 int cmp;
1535
1536 bzero(&mask, sizeof(mask));
1537 if (iflr->flags & IFLR_PREFIX) {
1538 /* lookup a prefix rather than address. */
1539 in6_prefixlen2mask(&mask, iflr->prefixlen);
1540
1541 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1542 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1543 match.s6_addr32[0] &= mask.s6_addr32[0];
1544 match.s6_addr32[1] &= mask.s6_addr32[1];
1545 match.s6_addr32[2] &= mask.s6_addr32[2];
1546 match.s6_addr32[3] &= mask.s6_addr32[3];
1547
1548 /* if you set extra bits, that's wrong */
1549 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1550 return EINVAL;
1551
1552 cmp = 1;
1553 } else {
1554 if (cmd == SIOCGLIFADDR) {
1555 /* on getting an address, take the 1st match */
1556 cmp = 0; /* XXX */
1557 } else {
1558 /* on deleting an address, do exact match */
1559 in6_prefixlen2mask(&mask, 128);
1560 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1561 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1562
1563 cmp = 1;
1564 }
1565 }
1566
1567 for (ifa = ifp->if_addrlist.tqh_first;
1568 ifa;
1569 ifa = ifa->ifa_list.tqe_next)
1570 {
1571 if (ifa->ifa_addr->sa_family != AF_INET6)
1572 continue;
1573 if (!cmp)
1574 break;
1575
1576 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1577 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1578 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1579 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1580 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1581 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1582 break;
1583 }
1584 if (!ifa)
1585 return EADDRNOTAVAIL;
1586 ia = ifa2ia6(ifa);
1587
1588 if (cmd == SIOCGLIFADDR) {
1589 /* fill in the if_laddrreq structure */
1590 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1591 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1592 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1593 ia->ia_dstaddr.sin6_len);
1594 } else
1595 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1596
1597 iflr->prefixlen =
1598 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1599
1600 iflr->flags = ia->ia6_flags; /* XXX */
1601
1602 return 0;
1603 } else {
1604 struct in6_aliasreq ifra;
1605
1606 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1607 bzero(&ifra, sizeof(ifra));
1608 bcopy(iflr->iflr_name, ifra.ifra_name,
1609 sizeof(ifra.ifra_name));
1610
1611 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1612 ia->ia_addr.sin6_len);
1613 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1614 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1615 ia->ia_dstaddr.sin6_len);
1616 } else {
1617 bzero(&ifra.ifra_dstaddr,
1618 sizeof(ifra.ifra_dstaddr));
1619 }
1620 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1621 ia->ia_prefixmask.sin6_len);
1622
1623 ifra.ifra_flags = ia->ia6_flags;
1624 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1625 ifp, p);
1626 }
1627 }
1628 }
1629
1630 return EOPNOTSUPP; /* just for safety */
1631 }
1632
1633 /*
1634 * Initialize an interface's intetnet6 address
1635 * and routing table entry.
1636 */
1637 static int
1638 in6_ifinit(ifp, ia, sin6, newhost)
1639 struct ifnet *ifp;
1640 struct in6_ifaddr *ia;
1641 struct sockaddr_in6 *sin6;
1642 int newhost;
1643 {
1644 int error = 0, plen, ifacount = 0;
1645 int s = splnet();
1646 struct ifaddr *ifa;
1647
1648 /*
1649 * Give the interface a chance to initialize
1650 * if this is its first address,
1651 * and to validate the address if necessary.
1652 */
1653 for (ifa = ifp->if_addrlist.tqh_first; ifa;
1654 ifa = ifa->ifa_list.tqe_next)
1655 {
1656 if (ifa->ifa_addr == NULL)
1657 continue; /* just for safety */
1658 if (ifa->ifa_addr->sa_family != AF_INET6)
1659 continue;
1660 ifacount++;
1661 }
1662
1663 ia->ia_addr = *sin6;
1664
1665 if (ifacount <= 1 && ifp->if_ioctl &&
1666 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1667 splx(s);
1668 return (error);
1669 }
1670 splx(s);
1671
1672 ia->ia_ifa.ifa_metric = ifp->if_metric;
1673
1674 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1675
1676 /*
1677 * Special case:
1678 * If the destination address is specified for a point-to-point
1679 * interface, install a route to the destination as an interface
1680 * direct route.
1681 */
1682 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1683 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1684 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1685 RTF_UP | RTF_HOST)) != 0)
1686 return (error);
1687 ia->ia_flags |= IFA_ROUTE;
1688 }
1689
1690 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1691 if (newhost) {
1692 /* set the rtrequest function to create llinfo */
1693 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1694 in6_ifaddloop(&(ia->ia_ifa));
1695 }
1696
1697 if (ifp->if_flags & IFF_MULTICAST)
1698 in6_restoremkludge(ia, ifp);
1699
1700 return (error);
1701 }
1702
1703 /*
1704 * Multicast address kludge:
1705 * If there were any multicast addresses attached to this interface address,
1706 * either move them to another address on this interface, or save them until
1707 * such time as this interface is reconfigured for IPv6.
1708 */
1709 void
1710 in6_savemkludge(oia)
1711 struct in6_ifaddr *oia;
1712 {
1713 struct in6_ifaddr *ia;
1714 struct in6_multi *in6m, *next;
1715
1716 IFP_TO_IA6(oia->ia_ifp, ia);
1717 if (ia) { /* there is another address */
1718 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1719 next = in6m->in6m_entry.le_next;
1720 IFAFREE(&in6m->in6m_ia->ia_ifa);
1721 IFAREF(&ia->ia_ifa);
1722 in6m->in6m_ia = ia;
1723 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1724 }
1725 } else { /* last address on this if deleted, save */
1726 struct multi6_kludge *mk;
1727
1728 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1729 if (mk->mk_ifp == oia->ia_ifp)
1730 break;
1731 }
1732 if (mk == NULL) /* this should not happen! */
1733 panic("in6_savemkludge: no kludge space");
1734
1735 for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1736 next = in6m->in6m_entry.le_next;
1737 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1738 in6m->in6m_ia = NULL;
1739 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
1740 }
1741 }
1742 }
1743
1744 /*
1745 * Continuation of multicast address hack:
1746 * If there was a multicast group list previously saved for this interface,
1747 * then we re-attach it to the first address configured on the i/f.
1748 */
1749 void
1750 in6_restoremkludge(ia, ifp)
1751 struct in6_ifaddr *ia;
1752 struct ifnet *ifp;
1753 {
1754 struct multi6_kludge *mk;
1755
1756 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1757 if (mk->mk_ifp == ifp) {
1758 struct in6_multi *in6m, *next;
1759
1760 for (in6m = mk->mk_head.lh_first; in6m; in6m = next) {
1761 next = in6m->in6m_entry.le_next;
1762 in6m->in6m_ia = ia;
1763 IFAREF(&ia->ia_ifa);
1764 LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
1765 in6m, in6m_entry);
1766 }
1767 LIST_INIT(&mk->mk_head);
1768 break;
1769 }
1770 }
1771 }
1772
1773 /*
1774 * Allocate space for the kludge at interface initialization time.
1775 * Formerly, we dynamically allocated the space in in6_savemkludge() with
1776 * malloc(M_WAITOK). However, it was wrong since the function could be called
1777 * under an interrupt context (software timer on address lifetime expiration).
1778 * Also, we cannot just give up allocating the strucutre, since the group
1779 * membership structure is very complex and we need to keep it anyway.
1780 * Of course, this function MUST NOT be called under an interrupt context.
1781 * Specifically, it is expected to be called only from in6_ifattach(), though
1782 * it is a global function.
1783 */
1784 void
1785 in6_createmkludge(ifp)
1786 struct ifnet *ifp;
1787 {
1788 struct multi6_kludge *mk;
1789
1790 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1791 /* If we've already had one, do not allocate. */
1792 if (mk->mk_ifp == ifp)
1793 return;
1794 }
1795
1796 mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
1797
1798 bzero(mk, sizeof(*mk));
1799 LIST_INIT(&mk->mk_head);
1800 mk->mk_ifp = ifp;
1801 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
1802 }
1803
1804 void
1805 in6_purgemkludge(ifp)
1806 struct ifnet *ifp;
1807 {
1808 struct multi6_kludge *mk;
1809 struct in6_multi *in6m;
1810
1811 for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1812 if (mk->mk_ifp != ifp)
1813 continue;
1814
1815 /* leave from all multicast groups joined */
1816 while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL)
1817 in6_delmulti(in6m);
1818 LIST_REMOVE(mk, mk_entry);
1819 free(mk, M_IPMADDR);
1820 break;
1821 }
1822 }
1823
1824 /*
1825 * Add an address to the list of IP6 multicast addresses for a
1826 * given interface.
1827 */
1828 struct in6_multi *
1829 in6_addmulti(maddr6, ifp, errorp)
1830 struct in6_addr *maddr6;
1831 struct ifnet *ifp;
1832 int *errorp;
1833 {
1834 struct in6_ifaddr *ia;
1835 struct in6_ifreq ifr;
1836 struct in6_multi *in6m;
1837 int s = splsoftnet();
1838
1839 *errorp = 0;
1840 /*
1841 * See if address already in list.
1842 */
1843 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
1844 if (in6m != NULL) {
1845 /*
1846 * Found it; just increment the refrence count.
1847 */
1848 in6m->in6m_refcount++;
1849 } else {
1850 /*
1851 * New address; allocate a new multicast record
1852 * and link it into the interface's multicast list.
1853 */
1854 in6m = (struct in6_multi *)
1855 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1856 if (in6m == NULL) {
1857 splx(s);
1858 *errorp = ENOBUFS;
1859 return (NULL);
1860 }
1861 in6m->in6m_addr = *maddr6;
1862 in6m->in6m_ifp = ifp;
1863 in6m->in6m_refcount = 1;
1864 IFP_TO_IA6(ifp, ia);
1865 if (ia == NULL) {
1866 free(in6m, M_IPMADDR);
1867 splx(s);
1868 *errorp = EADDRNOTAVAIL; /* appropriate? */
1869 return (NULL);
1870 }
1871 in6m->in6m_ia = ia;
1872 IFAREF(&ia->ia_ifa); /* gain a reference */
1873 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1874
1875 /*
1876 * Ask the network driver to update its multicast reception
1877 * filter appropriately for the new address.
1878 */
1879 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1880 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1881 ifr.ifr_addr.sin6_family = AF_INET6;
1882 ifr.ifr_addr.sin6_addr = *maddr6;
1883 if (ifp->if_ioctl == NULL)
1884 *errorp = ENXIO; /* XXX: appropriate? */
1885 else
1886 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
1887 (caddr_t)&ifr);
1888 if (*errorp) {
1889 LIST_REMOVE(in6m, in6m_entry);
1890 free(in6m, M_IPMADDR);
1891 IFAFREE(&ia->ia_ifa);
1892 splx(s);
1893 return (NULL);
1894 }
1895 /*
1896 * Let MLD6 know that we have joined a new IP6 multicast
1897 * group.
1898 */
1899 mld6_start_listening(in6m);
1900 }
1901 splx(s);
1902 return (in6m);
1903 }
1904
1905 /*
1906 * Delete a multicast address record.
1907 */
1908 void
1909 in6_delmulti(in6m)
1910 struct in6_multi *in6m;
1911 {
1912 struct in6_ifreq ifr;
1913 int s = splsoftnet();
1914
1915 if (--in6m->in6m_refcount == 0) {
1916 /*
1917 * No remaining claims to this record; let MLD6 know
1918 * that we are leaving the multicast group.
1919 */
1920 mld6_stop_listening(in6m);
1921
1922 /*
1923 * Unlink from list.
1924 */
1925 LIST_REMOVE(in6m, in6m_entry);
1926 if (in6m->in6m_ia) {
1927 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1928 }
1929
1930 /*
1931 * Notify the network driver to update its multicast
1932 * reception filter.
1933 */
1934 bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1935 ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1936 ifr.ifr_addr.sin6_family = AF_INET6;
1937 ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
1938 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
1939 SIOCDELMULTI, (caddr_t)&ifr);
1940 free(in6m, M_IPMADDR);
1941 }
1942 splx(s);
1943 }
1944
1945 struct in6_multi_mship *
1946 in6_joingroup(ifp, addr, errorp)
1947 struct ifnet *ifp;
1948 struct in6_addr *addr;
1949 int *errorp;
1950 {
1951 struct in6_multi_mship *imm;
1952
1953 imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1954 if (!imm) {
1955 *errorp = ENOBUFS;
1956 return NULL;
1957 }
1958 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1959 if (!imm->i6mm_maddr) {
1960 /* *errorp is alrady set */
1961 free(imm, M_IPMADDR);
1962 return NULL;
1963 }
1964 return imm;
1965 }
1966
1967 int
1968 in6_leavegroup(imm)
1969 struct in6_multi_mship *imm;
1970 {
1971
1972 if (imm->i6mm_maddr)
1973 in6_delmulti(imm->i6mm_maddr);
1974 free(imm, M_IPMADDR);
1975 return 0;
1976 }
1977
1978 /*
1979 * Find an IPv6 interface link-local address specific to an interface.
1980 */
1981 struct in6_ifaddr *
1982 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1983 struct ifnet *ifp;
1984 int ignoreflags;
1985 {
1986 struct ifaddr *ifa;
1987
1988 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1989 {
1990 if (ifa->ifa_addr == NULL)
1991 continue; /* just for safety */
1992 if (ifa->ifa_addr->sa_family != AF_INET6)
1993 continue;
1994 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1995 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1996 ignoreflags) != 0)
1997 continue;
1998 break;
1999 }
2000 }
2001
2002 return ((struct in6_ifaddr *)ifa);
2003 }
2004
2005
2006 /*
2007 * find the internet address corresponding to a given interface and address.
2008 */
2009 struct in6_ifaddr *
2010 in6ifa_ifpwithaddr(ifp, addr)
2011 struct ifnet *ifp;
2012 struct in6_addr *addr;
2013 {
2014 struct ifaddr *ifa;
2015
2016 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
2017 {
2018 if (ifa->ifa_addr == NULL)
2019 continue; /* just for safety */
2020 if (ifa->ifa_addr->sa_family != AF_INET6)
2021 continue;
2022 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
2023 break;
2024 }
2025
2026 return ((struct in6_ifaddr *)ifa);
2027 }
2028
2029 /*
2030 * Convert IP6 address to printable (loggable) representation.
2031 */
2032 static char digits[] = "0123456789abcdef";
2033 static int ip6round = 0;
2034 char *
2035 ip6_sprintf(addr)
2036 const struct in6_addr *addr;
2037 {
2038 static char ip6buf[8][48];
2039 int i;
2040 char *cp;
2041 const u_int16_t *a = (const u_int16_t *)addr;
2042 const u_int8_t *d;
2043 int dcolon = 0;
2044
2045 ip6round = (ip6round + 1) & 7;
2046 cp = ip6buf[ip6round];
2047
2048 for (i = 0; i < 8; i++) {
2049 if (dcolon == 1) {
2050 if (*a == 0) {
2051 if (i == 7)
2052 *cp++ = ':';
2053 a++;
2054 continue;
2055 } else
2056 dcolon = 2;
2057 }
2058 if (*a == 0) {
2059 if (dcolon == 0 && *(a + 1) == 0) {
2060 if (i == 0)
2061 *cp++ = ':';
2062 *cp++ = ':';
2063 dcolon = 1;
2064 } else {
2065 *cp++ = '0';
2066 *cp++ = ':';
2067 }
2068 a++;
2069 continue;
2070 }
2071 d = (const u_char *)a;
2072 *cp++ = digits[*d >> 4];
2073 *cp++ = digits[*d++ & 0xf];
2074 *cp++ = digits[*d >> 4];
2075 *cp++ = digits[*d & 0xf];
2076 *cp++ = ':';
2077 a++;
2078 }
2079 *--cp = 0;
2080 return (ip6buf[ip6round]);
2081 }
2082
2083 /*
2084 * Determine if an address is on a local network.
2085 */
2086 int
2087 in6_localaddr(in6)
2088 struct in6_addr *in6;
2089 {
2090 struct in6_ifaddr *ia;
2091
2092 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
2093 return (1);
2094
2095 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
2096 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
2097 &ia->ia_prefixmask.sin6_addr))
2098 return (1);
2099
2100 return (0);
2101 }
2102
2103 /*
2104 * Get a scope of the address. Node-local, link-local, site-local or global.
2105 */
2106 int
2107 in6_addrscope (addr)
2108 struct in6_addr *addr;
2109 {
2110 int scope;
2111
2112 if (addr->s6_addr8[0] == 0xfe) {
2113 scope = addr->s6_addr8[1] & 0xc0;
2114
2115 switch (scope) {
2116 case 0x80:
2117 return IPV6_ADDR_SCOPE_LINKLOCAL;
2118 case 0xc0:
2119 return IPV6_ADDR_SCOPE_SITELOCAL;
2120 default:
2121 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
2122 }
2123 }
2124
2125
2126 if (addr->s6_addr8[0] == 0xff) {
2127 scope = addr->s6_addr8[1] & 0x0f;
2128
2129 /*
2130 * due to other scope such as reserved,
2131 * return scope doesn't work.
2132 */
2133 switch (scope) {
2134 case IPV6_ADDR_SCOPE_NODELOCAL:
2135 return IPV6_ADDR_SCOPE_NODELOCAL;
2136 case IPV6_ADDR_SCOPE_LINKLOCAL:
2137 return IPV6_ADDR_SCOPE_LINKLOCAL;
2138 case IPV6_ADDR_SCOPE_SITELOCAL:
2139 return IPV6_ADDR_SCOPE_SITELOCAL;
2140 default:
2141 return IPV6_ADDR_SCOPE_GLOBAL;
2142 }
2143 }
2144
2145 if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
2146 if (addr->s6_addr8[15] == 1) /* loopback */
2147 return IPV6_ADDR_SCOPE_NODELOCAL;
2148 if (addr->s6_addr8[15] == 0) /* unspecified */
2149 return IPV6_ADDR_SCOPE_LINKLOCAL;
2150 }
2151
2152 return IPV6_ADDR_SCOPE_GLOBAL;
2153 }
2154
2155 int
2156 in6_addr2scopeid(ifp, addr)
2157 struct ifnet *ifp; /* must not be NULL */
2158 struct in6_addr *addr; /* must not be NULL */
2159 {
2160 int scope = in6_addrscope(addr);
2161
2162 switch (scope) {
2163 case IPV6_ADDR_SCOPE_NODELOCAL:
2164 return (-1); /* XXX: is this an appropriate value? */
2165
2166 case IPV6_ADDR_SCOPE_LINKLOCAL:
2167 /* XXX: we do not distinguish between a link and an I/F. */
2168 return (ifp->if_index);
2169
2170 case IPV6_ADDR_SCOPE_SITELOCAL:
2171 return (0); /* XXX: invalid. */
2172
2173 default:
2174 return (0); /* XXX: treat as global. */
2175 }
2176 }
2177
2178 int
2179 in6_is_addr_deprecated(sa6)
2180 struct sockaddr_in6 *sa6;
2181 {
2182 struct in6_ifaddr *ia;
2183
2184 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
2185 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
2186 &sa6->sin6_addr) &&
2187 #ifdef SCOPEDROUTING
2188 ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
2189 #endif
2190 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2191 return (1); /* true */
2192
2193 /* XXX: do we still have to go thru the rest of the list? */
2194 }
2195
2196 return (0); /* false */
2197 }
2198
2199 /*
2200 * return length of part which dst and src are equal
2201 * hard coding...
2202 */
2203 int
2204 in6_matchlen(src, dst)
2205 struct in6_addr *src, *dst;
2206 {
2207 int match = 0;
2208 u_char *s = (u_char *)src, *d = (u_char *)dst;
2209 u_char *lim = s + 16, r;
2210
2211 while (s < lim)
2212 if ((r = (*d++ ^ *s++)) != 0) {
2213 while (r < 128) {
2214 match++;
2215 r <<= 1;
2216 }
2217 break;
2218 } else
2219 match += 8;
2220 return match;
2221 }
2222
2223 /* XXX: to be scope conscious */
2224 int
2225 in6_are_prefix_equal(p1, p2, len)
2226 struct in6_addr *p1, *p2;
2227 int len;
2228 {
2229 int bytelen, bitlen;
2230
2231 /* sanity check */
2232 if (0 > len || len > 128) {
2233 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2234 len);
2235 return (0);
2236 }
2237
2238 bytelen = len / 8;
2239 bitlen = len % 8;
2240
2241 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2242 return (0);
2243 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
2244 p2->s6_addr[bytelen] >> (8 - bitlen))
2245 return (0);
2246
2247 return (1);
2248 }
2249
2250 void
2251 in6_prefixlen2mask(maskp, len)
2252 struct in6_addr *maskp;
2253 int len;
2254 {
2255 static const u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2256 int bytelen, bitlen, i;
2257
2258 /* sanity check */
2259 if (0 > len || len > 128) {
2260 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2261 len);
2262 return;
2263 }
2264
2265 bzero(maskp, sizeof(*maskp));
2266 bytelen = len / 8;
2267 bitlen = len % 8;
2268 for (i = 0; i < bytelen; i++)
2269 maskp->s6_addr[i] = 0xff;
2270 if (bitlen)
2271 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2272 }
2273
2274 /*
2275 * return the best address out of the same scope
2276 */
2277 struct in6_ifaddr *
2278 in6_ifawithscope(oifp, dst)
2279 struct ifnet *oifp;
2280 struct in6_addr *dst;
2281 {
2282 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
2283 int blen = -1;
2284 struct ifaddr *ifa;
2285 struct ifnet *ifp;
2286 struct in6_ifaddr *ifa_best = NULL;
2287
2288 if (oifp == NULL) {
2289 printf("in6_ifawithscope: output interface is not specified\n");
2290 return (NULL);
2291 }
2292
2293 /*
2294 * We search for all addresses on all interfaces from the beginning.
2295 * Comparing an interface with the outgoing interface will be done
2296 * only at the final stage of tiebreaking.
2297 */
2298 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2299 {
2300 /*
2301 * We can never take an address that breaks the scope zone
2302 * of the destination.
2303 */
2304 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
2305 continue;
2306
2307 for (ifa = ifp->if_addrlist.tqh_first; ifa;
2308 ifa = ifa->ifa_list.tqe_next)
2309 {
2310 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
2311
2312 if (ifa->ifa_addr->sa_family != AF_INET6)
2313 continue;
2314
2315 src_scope = in6_addrscope(IFA_IN6(ifa));
2316
2317 #ifdef ADDRSELECT_DEBUG /* should be removed after stabilization */
2318 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2319 printf("in6_ifawithscope: dst=%s bestaddr=%s, "
2320 "newaddr=%s, scope=%x, dcmp=%d, bcmp=%d, "
2321 "matchlen=%d, flgs=%x\n",
2322 ip6_sprintf(dst),
2323 ifa_best ? ip6_sprintf(&ifa_best->ia_addr.sin6_addr) : "none",
2324 ip6_sprintf(IFA_IN6(ifa)), src_scope,
2325 dscopecmp,
2326 ifa_best ? IN6_ARE_SCOPE_CMP(src_scope, best_scope) : -1,
2327 in6_matchlen(IFA_IN6(ifa), dst),
2328 ((struct in6_ifaddr *)ifa)->ia6_flags);
2329 #endif
2330
2331 /*
2332 * Don't use an address before completing DAD
2333 * nor a duplicated address.
2334 */
2335 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2336 IN6_IFF_NOTREADY)
2337 continue;
2338
2339 /* XXX: is there any case to allow anycasts? */
2340 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2341 IN6_IFF_ANYCAST)
2342 continue;
2343
2344 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2345 IN6_IFF_DETACHED)
2346 continue;
2347
2348 /*
2349 * If this is the first address we find,
2350 * keep it anyway.
2351 */
2352 if (ifa_best == NULL)
2353 goto replace;
2354
2355 /*
2356 * ifa_best is never NULL beyond this line except
2357 * within the block labeled "replace".
2358 */
2359
2360 /*
2361 * If ifa_best has a smaller scope than dst and
2362 * the current address has a larger one than
2363 * (or equal to) dst, always replace ifa_best.
2364 * Also, if the current address has a smaller scope
2365 * than dst, ignore it unless ifa_best also has a
2366 * smaller scope.
2367 */
2368 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2369 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2370 goto replace;
2371 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2372 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2373 continue;
2374
2375 /*
2376 * A deprecated address SHOULD NOT be used in new
2377 * communications if an alternate (non-deprecated)
2378 * address is available and has sufficient scope.
2379 * RFC 2462, Section 5.5.4.
2380 */
2381 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2382 IN6_IFF_DEPRECATED) {
2383 /*
2384 * Ignore any deprecated addresses if
2385 * specified by configuration.
2386 */
2387 if (!ip6_use_deprecated)
2388 continue;
2389
2390 /*
2391 * If we have already found a non-deprecated
2392 * candidate, just ignore deprecated addresses.
2393 */
2394 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2395 == 0)
2396 continue;
2397 }
2398
2399 /*
2400 * A non-deprecated address is always preferred
2401 * to a deprecated one regardless of scopes and
2402 * address matching.
2403 */
2404 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2405 (((struct in6_ifaddr *)ifa)->ia6_flags &
2406 IN6_IFF_DEPRECATED) == 0)
2407 goto replace;
2408
2409 /*
2410 * At this point, we have two cases:
2411 * 1. we are looking at a non-deprecated address,
2412 * and ifa_best is also non-deprecated.
2413 * 2. we are looking at a deprecated address,
2414 * and ifa_best is also deprecated.
2415 * Also, we do not have to consider a case where
2416 * the scope of if_best is larger(smaller) than dst and
2417 * the scope of the current address is smaller(larger)
2418 * than dst. Such a case has already been covered.
2419 * Tiebreaking is done according to the following
2420 * items:
2421 * - the scope comparison between the address and
2422 * dst (dscopecmp)
2423 * - the scope comparison between the address and
2424 * ifa_best (bscopecmp)
2425 * - if the address match dst longer than ifa_best
2426 * (matchcmp)
2427 * - if the address is on the outgoing I/F (outI/F)
2428 *
2429 * Roughly speaking, the selection policy is
2430 * - the most important item is scope. The same scope
2431 * is best. Then search for a larger scope.
2432 * Smaller scopes are the last resort.
2433 * - A deprecated address is chosen only when we have
2434 * no address that has an enough scope, but is
2435 * prefered to any addresses of smaller scopes.
2436 * - Longest address match against dst is considered
2437 * only for addresses that has the same scope of dst.
2438 * - If there is no other reasons to choose one,
2439 * addresses on the outgoing I/F are preferred.
2440 *
2441 * The precise decision table is as follows:
2442 * dscopecmp bscopecmp matchcmp outI/F | replace?
2443 * !equal equal N/A Yes | Yes (1)
2444 * !equal equal N/A No | No (2)
2445 * larger larger N/A N/A | No (3)
2446 * larger smaller N/A N/A | Yes (4)
2447 * smaller larger N/A N/A | Yes (5)
2448 * smaller smaller N/A N/A | No (6)
2449 * equal smaller N/A N/A | Yes (7)
2450 * equal larger (already done)
2451 * equal equal larger N/A | Yes (8)
2452 * equal equal smaller N/A | No (9)
2453 * equal equal equal Yes | Yes (a)
2454 * eaual eqaul equal No | No (b)
2455 */
2456 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2457 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2458
2459 if (dscopecmp && bscopecmp == 0) {
2460 if (oifp == ifp) /* (1) */
2461 goto replace;
2462 continue; /* (2) */
2463 }
2464 if (dscopecmp > 0) {
2465 if (bscopecmp > 0) /* (3) */
2466 continue;
2467 goto replace; /* (4) */
2468 }
2469 if (dscopecmp < 0) {
2470 if (bscopecmp > 0) /* (5) */
2471 goto replace;
2472 continue; /* (6) */
2473 }
2474
2475 /* now dscopecmp must be 0 */
2476 if (bscopecmp < 0)
2477 goto replace; /* (7) */
2478
2479 /*
2480 * At last both dscopecmp and bscopecmp must be 0.
2481 * We need address matching against dst for
2482 * tiebreaking.
2483 */
2484 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2485 matchcmp = tlen - blen;
2486 if (matchcmp > 0) /* (8) */
2487 goto replace;
2488 if (matchcmp < 0) /* (9) */
2489 continue;
2490 if (oifp == ifp) /* (a) */
2491 goto replace;
2492 continue; /* (b) */
2493
2494 replace:
2495 ifa_best = (struct in6_ifaddr *)ifa;
2496 blen = tlen >= 0 ? tlen :
2497 in6_matchlen(IFA_IN6(ifa), dst);
2498 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2499 }
2500 }
2501
2502 /* count statistics for future improvements */
2503 if (ifa_best == NULL)
2504 ip6stat.ip6s_sources_none++;
2505 else {
2506 if (oifp == ifa_best->ia_ifp)
2507 ip6stat.ip6s_sources_sameif[best_scope]++;
2508 else
2509 ip6stat.ip6s_sources_otherif[best_scope]++;
2510
2511 if (best_scope == dst_scope)
2512 ip6stat.ip6s_sources_samescope[best_scope]++;
2513 else
2514 ip6stat.ip6s_sources_otherscope[best_scope]++;
2515
2516 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2517 ip6stat.ip6s_sources_deprecated[best_scope]++;
2518 }
2519
2520 return (ifa_best);
2521 }
2522
2523 /*
2524 * return the best address out of the same scope. if no address was
2525 * found, return the first valid address from designated IF.
2526 */
2527 struct in6_ifaddr *
2528 in6_ifawithifp(ifp, dst)
2529 struct ifnet *ifp;
2530 struct in6_addr *dst;
2531 {
2532 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2533 struct ifaddr *ifa;
2534 struct in6_ifaddr *besta = 0;
2535 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2536
2537 dep[0] = dep[1] = NULL;
2538
2539 /*
2540 * We first look for addresses in the same scope.
2541 * If there is one, return it.
2542 * If two or more, return one which matches the dst longest.
2543 * If none, return one of global addresses assigned other ifs.
2544 */
2545 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
2546 {
2547 if (ifa->ifa_addr->sa_family != AF_INET6)
2548 continue;
2549 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2550 continue; /* XXX: is there any case to allow anycast? */
2551 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2552 continue; /* don't use this interface */
2553 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2554 continue;
2555 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2556 if (ip6_use_deprecated)
2557 dep[0] = (struct in6_ifaddr *)ifa;
2558 continue;
2559 }
2560
2561 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2562 /*
2563 * call in6_matchlen() as few as possible
2564 */
2565 if (besta) {
2566 if (blen == -1)
2567 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2568 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2569 if (tlen > blen) {
2570 blen = tlen;
2571 besta = (struct in6_ifaddr *)ifa;
2572 }
2573 } else
2574 besta = (struct in6_ifaddr *)ifa;
2575 }
2576 }
2577 if (besta)
2578 return (besta);
2579
2580 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
2581 {
2582 if (ifa->ifa_addr->sa_family != AF_INET6)
2583 continue;
2584 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2585 continue; /* XXX: is there any case to allow anycast? */
2586 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2587 continue; /* don't use this interface */
2588 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2589 continue;
2590 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2591 if (ip6_use_deprecated)
2592 dep[1] = (struct in6_ifaddr *)ifa;
2593 continue;
2594 }
2595
2596 return (struct in6_ifaddr *)ifa;
2597 }
2598
2599 /* use the last-resort values, that are, deprecated addresses */
2600 if (dep[0])
2601 return dep[0];
2602 if (dep[1])
2603 return dep[1];
2604
2605 return NULL;
2606 }
2607
2608 /*
2609 * perform DAD when interface becomes IFF_UP.
2610 */
2611 void
2612 in6_if_up(ifp)
2613 struct ifnet *ifp;
2614 {
2615 struct ifaddr *ifa;
2616 struct in6_ifaddr *ia;
2617 int dad_delay; /* delay ticks before DAD output */
2618
2619 /*
2620 * special cases, like 6to4, are handled in in6_ifattach
2621 */
2622 in6_ifattach(ifp, NULL);
2623
2624 dad_delay = 0;
2625 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
2626 {
2627 if (ifa->ifa_addr->sa_family != AF_INET6)
2628 continue;
2629 ia = (struct in6_ifaddr *)ifa;
2630 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2631 nd6_dad_start(ifa, &dad_delay);
2632 }
2633 }
2634
2635 int
2636 in6if_do_dad(ifp)
2637 struct ifnet *ifp;
2638 {
2639 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2640 return (0);
2641
2642 switch (ifp->if_type) {
2643 case IFT_FAITH:
2644 /*
2645 * These interfaces do not have the IFF_LOOPBACK flag,
2646 * but loop packets back. We do not have to do DAD on such
2647 * interfaces. We should even omit it, because loop-backed
2648 * NS would confuse the DAD procedure.
2649 */
2650 return (0);
2651 default:
2652 /*
2653 * Our DAD routine requires the interface up and running.
2654 * However, some interfaces can be up before the RUNNING
2655 * status. Additionaly, users may try to assign addresses
2656 * before the interface becomes up (or running).
2657 * We simply skip DAD in such a case as a work around.
2658 * XXX: we should rather mark "tentative" on such addresses,
2659 * and do DAD after the interface becomes ready.
2660 */
2661 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2662 (IFF_UP|IFF_RUNNING))
2663 return (0);
2664
2665 return (1);
2666 }
2667 }
2668
2669 /*
2670 * Calculate max IPv6 MTU through all the interfaces and store it
2671 * to in6_maxmtu.
2672 */
2673 void
2674 in6_setmaxmtu()
2675 {
2676 unsigned long maxmtu = 0;
2677 struct ifnet *ifp;
2678
2679 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2680 {
2681 /* this function can be called during ifnet initialization */
2682 if (!ifp->if_afdata[AF_INET6])
2683 continue;
2684 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2685 IN6_LINKMTU(ifp) > maxmtu)
2686 maxmtu = IN6_LINKMTU(ifp);
2687 }
2688 if (maxmtu) /* update only when maxmtu is positive */
2689 in6_maxmtu = maxmtu;
2690 }
2691
2692 void *
2693 in6_domifattach(ifp)
2694 struct ifnet *ifp;
2695 {
2696 struct in6_ifextra *ext;
2697
2698 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2699 bzero(ext, sizeof(*ext));
2700
2701 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2702 M_IFADDR, M_WAITOK);
2703 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2704
2705 ext->icmp6_ifstat =
2706 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2707 M_IFADDR, M_WAITOK);
2708 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2709
2710 ext->nd_ifinfo = nd6_ifattach(ifp);
2711 return ext;
2712 }
2713
2714 void
2715 in6_domifdetach(ifp, aux)
2716 struct ifnet *ifp;
2717 void *aux;
2718 {
2719 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2720
2721 nd6_ifdetach(ext->nd_ifinfo);
2722 free(ext->in6_ifstat, M_IFADDR);
2723 free(ext->icmp6_ifstat, M_IFADDR);
2724 free(ext, M_IFADDR);
2725 }
2726