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