in6.c revision 1.125 1 /* $NetBSD: in6.c,v 1.125 2007/03/04 06:03:25 christos 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.125 2007/03/04 06:03:25 christos Exp $");
66
67 #include "opt_inet.h"
68 #include "opt_pfil_hooks.h"
69
70 #include <sys/param.h>
71 #include <sys/ioctl.h>
72 #include <sys/errno.h>
73 #include <sys/malloc.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sockio.h>
77 #include <sys/systm.h>
78 #include <sys/proc.h>
79 #include <sys/time.h>
80 #include <sys/kernel.h>
81 #include <sys/syslog.h>
82 #include <sys/kauth.h>
83
84 #include <net/if.h>
85 #include <net/if_types.h>
86 #include <net/route.h>
87 #include <net/if_dl.h>
88
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <net/if_ether.h>
92
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet6/nd6.h>
96 #include <netinet6/mld6_var.h>
97 #include <netinet6/ip6_mroute.h>
98 #include <netinet6/in6_ifattach.h>
99 #include <netinet6/scope6_var.h>
100
101 #include <net/net_osdep.h>
102
103 #ifdef PFIL_HOOKS
104 #include <net/pfil.h>
105 #endif
106
107 MALLOC_DEFINE(M_IP6OPT, "ip6_options", "IPv6 options");
108
109 /* enable backward compatibility code for obsoleted ioctls */
110 #define COMPAT_IN6IFIOCTL
111
112 #ifdef IN6_DEBUG
113 #define IN6_DPRINTF(__fmt, ...) printf(__fmt, __VA_ARGS__)
114 #else
115 #define IN6_DPRINTF(__fmt, ...) do { } while (/*CONSTCOND*/0)
116 #endif /* IN6_DEBUG */
117
118 /*
119 * Definitions of some constant IP6 addresses.
120 */
121 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
122 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
123 const struct in6_addr in6addr_nodelocal_allnodes =
124 IN6ADDR_NODELOCAL_ALLNODES_INIT;
125 const struct in6_addr in6addr_linklocal_allnodes =
126 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
127 const struct in6_addr in6addr_linklocal_allrouters =
128 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
129
130 const struct in6_addr in6mask0 = IN6MASK0;
131 const struct in6_addr in6mask32 = IN6MASK32;
132 const struct in6_addr in6mask64 = IN6MASK64;
133 const struct in6_addr in6mask96 = IN6MASK96;
134 const struct in6_addr in6mask128 = IN6MASK128;
135
136 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
137 0, 0, IN6ADDR_ANY_INIT, 0};
138
139 static int in6_lifaddr_ioctl(struct socket *, u_long, void *,
140 struct ifnet *, struct lwp *);
141 static int in6_ifinit(struct ifnet *, struct in6_ifaddr *,
142 struct sockaddr_in6 *, int);
143 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
144
145 /*
146 * Subroutine for in6_ifaddloop() and in6_ifremloop().
147 * This routine does actual work.
148 */
149 static void
150 in6_ifloop_request(int cmd, struct ifaddr *ifa)
151 {
152 struct sockaddr_in6 lo_sa;
153 struct sockaddr_in6 all1_sa;
154 struct rtentry *nrt = NULL;
155 int e;
156
157 bzero(&lo_sa, sizeof(lo_sa));
158 bzero(&all1_sa, sizeof(all1_sa));
159 lo_sa.sin6_family = all1_sa.sin6_family = AF_INET6;
160 lo_sa.sin6_len = all1_sa.sin6_len = sizeof(struct sockaddr_in6);
161 lo_sa.sin6_addr = in6addr_loopback;
162 all1_sa.sin6_addr = in6mask128;
163
164 /*
165 * We specify the address itself as the gateway, and set the
166 * RTF_LLINFO flag, so that the corresponding host route would have
167 * the flag, and thus applications that assume traditional behavior
168 * would be happy. Note that we assume the caller of the function
169 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
170 * which changes the outgoing interface to the loopback interface.
171 */
172 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
173 (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
174 if (e != 0) {
175 log(LOG_ERR, "in6_ifloop_request: "
176 "%s operation failed for %s (errno=%d)\n",
177 cmd == RTM_ADD ? "ADD" : "DELETE",
178 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
179 e);
180 }
181
182 /*
183 * Make sure rt_ifa be equal to IFA, the second argument of the
184 * function.
185 * We need this because when we refer to rt_ifa->ia6_flags in
186 * ip6_input, we assume that the rt_ifa points to the address instead
187 * of the loopback address.
188 */
189 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa)
190 rt_replace_ifa(nrt, ifa);
191
192 /*
193 * Report the addition/removal of the address to the routing socket.
194 * XXX: since we called rtinit for a p2p interface with a destination,
195 * we end up reporting twice in such a case. Should we rather
196 * omit the second report?
197 */
198 if (nrt) {
199 rt_newaddrmsg(cmd, ifa, e, nrt);
200 if (cmd == RTM_DELETE) {
201 if (nrt->rt_refcnt <= 0) {
202 /* XXX: we should free the entry ourselves. */
203 nrt->rt_refcnt++;
204 rtfree(nrt);
205 }
206 } else {
207 /* the cmd must be RTM_ADD here */
208 nrt->rt_refcnt--;
209 }
210 }
211 }
212
213 /*
214 * Add ownaddr as loopback rtentry. We previously add the route only if
215 * necessary (ex. on a p2p link). However, since we now manage addresses
216 * separately from prefixes, we should always add the route. We can't
217 * rely on the cloning mechanism from the corresponding interface route
218 * any more.
219 */
220 void
221 in6_ifaddloop(struct ifaddr *ifa)
222 {
223 struct rtentry *rt;
224
225 /* If there is no loopback entry, allocate one. */
226 rt = rtalloc1(ifa->ifa_addr, 0);
227 if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
228 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
229 in6_ifloop_request(RTM_ADD, ifa);
230 if (rt)
231 rt->rt_refcnt--;
232 }
233
234 /*
235 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
236 * if it exists.
237 */
238 void
239 in6_ifremloop(struct ifaddr *ifa)
240 {
241 struct in6_ifaddr *alt_ia = NULL, *ia;
242 struct rtentry *rt;
243 int ia_count = 0;
244
245 /*
246 * Some of BSD variants do not remove cloned routes
247 * from an interface direct route, when removing the direct route
248 * (see comments in net/net_osdep.h). Even for variants that do remove
249 * cloned routes, they could fail to remove the cloned routes when
250 * we handle multple addresses that share a common prefix.
251 * So, we should remove the route corresponding to the deleted address.
252 */
253
254 /*
255 * Delete the entry only if exactly one ifaddr matches the
256 * address, ifa->ifa_addr.
257 *
258 * If more than one ifaddr matches, replace the ifaddr in
259 * the routing table, rt_ifa, with a different ifaddr than
260 * the one we are purging, ifa. It is important to do
261 * this, or else the routing table can accumulate dangling
262 * pointers rt->rt_ifa->ifa_ifp to destroyed interfaces,
263 * which will lead to crashes, later. (More than one ifaddr
264 * can match if we assign the same address to multiple---probably
265 * p2p---interfaces.)
266 *
267 * XXX An old comment at this place said, "we should avoid
268 * XXX such a configuration [i.e., interfaces with the same
269 * XXX addressed assigned --ed.] in IPv6...". I do not
270 * XXX agree, especially now that I have fixed the dangling
271 * XXX ifp-pointers bug.
272 */
273 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
274 if (!IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr))
275 continue;
276 if (ia->ia_ifp != ifa->ifa_ifp)
277 alt_ia = ia;
278 if (++ia_count > 1 && alt_ia != NULL)
279 break;
280 }
281
282 if (ia_count == 0)
283 return;
284
285 if ((rt = rtalloc1(ifa->ifa_addr, 0)) == NULL)
286 return;
287 rt->rt_refcnt--;
288
289 /*
290 * Before deleting, check if a corresponding loopbacked
291 * host route surely exists. With this check, we can avoid
292 * deleting an interface direct route whose destination is
293 * the same as the address being removed. This can happen
294 * when removing a subnet-router anycast address on an
295 * interface attached to a shared medium.
296 */
297 if ((rt->rt_flags & RTF_HOST) == 0 ||
298 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
299 return;
300
301 /* If we cannot replace the route's ifaddr with the equivalent
302 * ifaddr of another interface, I believe it is safest to
303 * delete the route.
304 */
305 if (ia_count == 1 || alt_ia == NULL)
306 in6_ifloop_request(RTM_DELETE, ifa);
307 else
308 rt_replace_ifa(rt, &alt_ia->ia_ifa);
309 }
310
311 int
312 in6_mask2len(mask, lim0)
313 struct in6_addr *mask;
314 u_char *lim0;
315 {
316 int x = 0, y;
317 u_char *lim = lim0, *p;
318
319 /* ignore the scope_id part */
320 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
321 lim = (u_char *)mask + sizeof(*mask);
322 for (p = (u_char *)mask; p < lim; x++, p++) {
323 if (*p != 0xff)
324 break;
325 }
326 y = 0;
327 if (p < lim) {
328 for (y = 0; y < NBBY; y++) {
329 if ((*p & (0x80 >> y)) == 0)
330 break;
331 }
332 }
333
334 /*
335 * when the limit pointer is given, do a stricter check on the
336 * remaining bits.
337 */
338 if (p < lim) {
339 if (y != 0 && (*p & (0x00ff >> y)) != 0)
340 return -1;
341 for (p = p + 1; p < lim; p++)
342 if (*p != 0)
343 return -1;
344 }
345
346 return x * NBBY + y;
347 }
348
349 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
350 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
351
352 static int
353 in6_control1(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
354 struct lwp *l, int privileged)
355 {
356 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
357 struct in6_ifaddr *ia = NULL;
358 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
359 struct sockaddr_in6 *sa6;
360 int error;
361 switch (cmd) {
362 /*
363 * XXX: Fix me, once we fix SIOCSIFADDR, SIOCIFDSTADDR, etc.
364 */
365 case SIOCSIFADDR:
366 case SIOCSIFDSTADDR:
367 #ifdef SIOCSIFCONF_X26
368 case SIOCSIFCONF_X25:
369 #endif
370 return EOPNOTSUPP;
371 case SIOCGETSGCNT_IN6:
372 case SIOCGETMIFCNT_IN6:
373 return mrt6_ioctl(cmd, data);
374 }
375
376 if (ifp == NULL)
377 return EOPNOTSUPP;
378
379 switch (cmd) {
380 case SIOCSNDFLUSH_IN6:
381 case SIOCSPFXFLUSH_IN6:
382 case SIOCSRTRFLUSH_IN6:
383 case SIOCSDEFIFACE_IN6:
384 case SIOCSIFINFO_FLAGS:
385 case SIOCSIFINFO_IN6:
386 if (!privileged)
387 return EPERM;
388 /* FALLTHROUGH */
389 case OSIOCGIFINFO_IN6:
390 case SIOCGIFINFO_IN6:
391 case SIOCGDRLST_IN6:
392 case SIOCGPRLST_IN6:
393 case SIOCGNBRINFO_IN6:
394 case SIOCGDEFIFACE_IN6:
395 return nd6_ioctl(cmd, data, ifp);
396 }
397
398 switch (cmd) {
399 case SIOCSIFPREFIX_IN6:
400 case SIOCDIFPREFIX_IN6:
401 case SIOCAIFPREFIX_IN6:
402 case SIOCCIFPREFIX_IN6:
403 case SIOCSGIFPREFIX_IN6:
404 case SIOCGIFPREFIX_IN6:
405 log(LOG_NOTICE,
406 "prefix ioctls are now invalidated. "
407 "please use ifconfig.\n");
408 return EOPNOTSUPP;
409 }
410
411 switch (cmd) {
412 case SIOCALIFADDR:
413 case SIOCDLIFADDR:
414 if (!privileged)
415 return EPERM;
416 /* FALLTHROUGH */
417 case SIOCGLIFADDR:
418 return in6_lifaddr_ioctl(so, cmd, data, ifp, l);
419 }
420
421 /*
422 * Find address for this interface, if it exists.
423 *
424 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
425 * only, and used the first interface address as the target of other
426 * operations (without checking ifra_addr). This was because netinet
427 * code/API assumed at most 1 interface address per interface.
428 * Since IPv6 allows a node to assign multiple addresses
429 * on a single interface, we almost always look and check the
430 * presence of ifra_addr, and reject invalid ones here.
431 * It also decreases duplicated code among SIOC*_IN6 operations.
432 */
433 switch (cmd) {
434 case SIOCAIFADDR_IN6:
435 case SIOCSIFPHYADDR_IN6:
436 sa6 = &ifra->ifra_addr;
437 break;
438 case SIOCSIFADDR_IN6:
439 case SIOCGIFADDR_IN6:
440 case SIOCSIFDSTADDR_IN6:
441 case SIOCSIFNETMASK_IN6:
442 case SIOCGIFDSTADDR_IN6:
443 case SIOCGIFNETMASK_IN6:
444 case SIOCDIFADDR_IN6:
445 case SIOCGIFPSRCADDR_IN6:
446 case SIOCGIFPDSTADDR_IN6:
447 case SIOCGIFAFLAG_IN6:
448 case SIOCSNDFLUSH_IN6:
449 case SIOCSPFXFLUSH_IN6:
450 case SIOCSRTRFLUSH_IN6:
451 case SIOCGIFALIFETIME_IN6:
452 case SIOCSIFALIFETIME_IN6:
453 case SIOCGIFSTAT_IN6:
454 case SIOCGIFSTAT_ICMP6:
455 sa6 = &ifr->ifr_addr;
456 break;
457 default:
458 sa6 = NULL;
459 break;
460 }
461 if (sa6 && sa6->sin6_family == AF_INET6) {
462 if (sa6->sin6_scope_id != 0)
463 error = sa6_embedscope(sa6, 0);
464 else
465 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
466 if (error != 0)
467 return error;
468 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
469 } else
470 ia = NULL;
471
472 switch (cmd) {
473 case SIOCSIFADDR_IN6:
474 case SIOCSIFDSTADDR_IN6:
475 case SIOCSIFNETMASK_IN6:
476 /*
477 * Since IPv6 allows a node to assign multiple addresses
478 * on a single interface, SIOCSIFxxx ioctls are deprecated.
479 */
480 return EINVAL;
481
482 case SIOCDIFADDR_IN6:
483 /*
484 * for IPv4, we look for existing in_ifaddr here to allow
485 * "ifconfig if0 delete" to remove the first IPv4 address on
486 * the interface. For IPv6, as the spec allows multiple
487 * interface address from the day one, we consider "remove the
488 * first one" semantics to be not preferable.
489 */
490 if (ia == NULL)
491 return EADDRNOTAVAIL;
492 /* FALLTHROUGH */
493 case SIOCAIFADDR_IN6:
494 /*
495 * We always require users to specify a valid IPv6 address for
496 * the corresponding operation.
497 */
498 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
499 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
500 return EAFNOSUPPORT;
501 if (!privileged)
502 return EPERM;
503
504 break;
505
506 case SIOCGIFADDR_IN6:
507 /* This interface is basically deprecated. use SIOCGIFCONF. */
508 /* FALLTHROUGH */
509 case SIOCGIFAFLAG_IN6:
510 case SIOCGIFNETMASK_IN6:
511 case SIOCGIFDSTADDR_IN6:
512 case SIOCGIFALIFETIME_IN6:
513 /* must think again about its semantics */
514 if (ia == NULL)
515 return EADDRNOTAVAIL;
516 break;
517 case SIOCSIFALIFETIME_IN6:
518 {
519 struct in6_addrlifetime *lt;
520
521 if (!privileged)
522 return EPERM;
523 if (ia == NULL)
524 return EADDRNOTAVAIL;
525 /* sanity for overflow - beware unsigned */
526 lt = &ifr->ifr_ifru.ifru_lifetime;
527 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
528 && lt->ia6t_vltime + time_second < time_second) {
529 return EINVAL;
530 }
531 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
532 && lt->ia6t_pltime + time_second < time_second) {
533 return EINVAL;
534 }
535 break;
536 }
537 }
538
539 switch (cmd) {
540
541 case SIOCGIFADDR_IN6:
542 ifr->ifr_addr = ia->ia_addr;
543 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
544 return error;
545 break;
546
547 case SIOCGIFDSTADDR_IN6:
548 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
549 return EINVAL;
550 /*
551 * XXX: should we check if ifa_dstaddr is NULL and return
552 * an error?
553 */
554 ifr->ifr_dstaddr = ia->ia_dstaddr;
555 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
556 return error;
557 break;
558
559 case SIOCGIFNETMASK_IN6:
560 ifr->ifr_addr = ia->ia_prefixmask;
561 break;
562
563 case SIOCGIFAFLAG_IN6:
564 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
565 break;
566
567 case SIOCGIFSTAT_IN6:
568 if (ifp == NULL)
569 return EINVAL;
570 bzero(&ifr->ifr_ifru.ifru_stat,
571 sizeof(ifr->ifr_ifru.ifru_stat));
572 ifr->ifr_ifru.ifru_stat =
573 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
574 break;
575
576 case SIOCGIFSTAT_ICMP6:
577 if (ifp == NULL)
578 return EINVAL;
579 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
580 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
581 ifr->ifr_ifru.ifru_icmp6stat =
582 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
583 break;
584
585 case SIOCGIFALIFETIME_IN6:
586 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
587 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
588 time_t maxexpire;
589 struct in6_addrlifetime *retlt =
590 &ifr->ifr_ifru.ifru_lifetime;
591
592 /*
593 * XXX: adjust expiration time assuming time_t is
594 * signed.
595 */
596 maxexpire = ((time_t)~0) &
597 ~((time_t)1 << ((sizeof(maxexpire) * NBBY) - 1));
598 if (ia->ia6_lifetime.ia6t_vltime <
599 maxexpire - ia->ia6_updatetime) {
600 retlt->ia6t_expire = ia->ia6_updatetime +
601 ia->ia6_lifetime.ia6t_vltime;
602 } else
603 retlt->ia6t_expire = maxexpire;
604 }
605 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
606 time_t maxexpire;
607 struct in6_addrlifetime *retlt =
608 &ifr->ifr_ifru.ifru_lifetime;
609
610 /*
611 * XXX: adjust expiration time assuming time_t is
612 * signed.
613 */
614 maxexpire = ((time_t)~0) &
615 ~((time_t)1 << ((sizeof(maxexpire) * NBBY) - 1));
616 if (ia->ia6_lifetime.ia6t_pltime <
617 maxexpire - ia->ia6_updatetime) {
618 retlt->ia6t_preferred = ia->ia6_updatetime +
619 ia->ia6_lifetime.ia6t_pltime;
620 } else
621 retlt->ia6t_preferred = maxexpire;
622 }
623 break;
624
625 case SIOCSIFALIFETIME_IN6:
626 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
627 /* for sanity */
628 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
629 ia->ia6_lifetime.ia6t_expire =
630 time_second + ia->ia6_lifetime.ia6t_vltime;
631 } else
632 ia->ia6_lifetime.ia6t_expire = 0;
633 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
634 ia->ia6_lifetime.ia6t_preferred =
635 time_second + ia->ia6_lifetime.ia6t_pltime;
636 } else
637 ia->ia6_lifetime.ia6t_preferred = 0;
638 break;
639
640 case SIOCAIFADDR_IN6:
641 {
642 int i;
643 struct nd_prefixctl pr0;
644 struct nd_prefix *pr;
645
646 /* reject read-only flags */
647 if ((ifra->ifra_flags & IN6_IFF_DUPLICATED) != 0 ||
648 (ifra->ifra_flags & IN6_IFF_DETACHED) != 0 ||
649 (ifra->ifra_flags & IN6_IFF_NODAD) != 0 ||
650 (ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0) {
651 return EINVAL;
652 }
653 /*
654 * first, make or update the interface address structure,
655 * and link it to the list.
656 */
657 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
658 return error;
659 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
660 == NULL) {
661 /*
662 * this can happen when the user specify the 0 valid
663 * lifetime.
664 */
665 break;
666 }
667
668 /*
669 * then, make the prefix on-link on the interface.
670 * XXX: we'd rather create the prefix before the address, but
671 * we need at least one address to install the corresponding
672 * interface route, so we configure the address first.
673 */
674
675 /*
676 * convert mask to prefix length (prefixmask has already
677 * been validated in in6_update_ifa().
678 */
679 bzero(&pr0, sizeof(pr0));
680 pr0.ndpr_ifp = ifp;
681 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
682 NULL);
683 if (pr0.ndpr_plen == 128) {
684 break; /* we don't need to install a host route. */
685 }
686 pr0.ndpr_prefix = ifra->ifra_addr;
687 /* apply the mask for safety. */
688 for (i = 0; i < 4; i++) {
689 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
690 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
691 }
692 /*
693 * XXX: since we don't have an API to set prefix (not address)
694 * lifetimes, we just use the same lifetimes as addresses.
695 * The (temporarily) installed lifetimes can be overridden by
696 * later advertised RAs (when accept_rtadv is non 0), which is
697 * an intended behavior.
698 */
699 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
700 pr0.ndpr_raf_auto =
701 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
702 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
703 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
704
705 /* add the prefix if not yet. */
706 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
707 /*
708 * nd6_prelist_add will install the corresponding
709 * interface route.
710 */
711 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
712 return error;
713 if (pr == NULL) {
714 log(LOG_ERR, "nd6_prelist_add succeeded but "
715 "no prefix\n");
716 return EINVAL; /* XXX panic here? */
717 }
718 }
719
720 /* relate the address to the prefix */
721 if (ia->ia6_ndpr == NULL) {
722 ia->ia6_ndpr = pr;
723 pr->ndpr_refcnt++;
724
725 /*
726 * If this is the first autoconf address from the
727 * prefix, create a temporary address as well
728 * (when required).
729 */
730 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
731 ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
732 int e;
733 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
734 log(LOG_NOTICE, "in6_control: failed "
735 "to create a temporary address, "
736 "errno=%d\n", e);
737 }
738 }
739 }
740
741 /*
742 * this might affect the status of autoconfigured addresses,
743 * that is, this address might make other addresses detached.
744 */
745 pfxlist_onlink_check();
746
747 #ifdef PFIL_HOOKS
748 (void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCAIFADDR_IN6,
749 ifp, PFIL_IFADDR);
750 #endif
751
752 break;
753 }
754
755 case SIOCDIFADDR_IN6:
756 {
757 struct nd_prefix *pr;
758
759 /*
760 * If the address being deleted is the only one that owns
761 * the corresponding prefix, expire the prefix as well.
762 * XXX: theoretically, we don't have to worry about such
763 * relationship, since we separate the address management
764 * and the prefix management. We do this, however, to provide
765 * as much backward compatibility as possible in terms of
766 * the ioctl operation.
767 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
768 */
769 pr = ia->ia6_ndpr;
770 in6_purgeaddr(&ia->ia_ifa);
771 if (pr && pr->ndpr_refcnt == 0)
772 prelist_remove(pr);
773 #ifdef PFIL_HOOKS
774 (void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCDIFADDR_IN6,
775 ifp, PFIL_IFADDR);
776 #endif
777 break;
778 }
779
780 default:
781 if (ifp == NULL || ifp->if_ioctl == 0)
782 return EOPNOTSUPP;
783 error = ((*ifp->if_ioctl)(ifp, cmd, data));
784 return error;
785 }
786
787 return 0;
788 }
789
790 int
791 in6_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
792 struct lwp *l)
793 {
794 int error, privileged, s;
795
796 privileged = 0;
797 if (l && !kauth_authorize_generic(l->l_cred,
798 KAUTH_GENERIC_ISSUSER, NULL))
799 privileged++;
800
801 s = splnet();
802 error = in6_control1(so , cmd, data, ifp, l, privileged);
803 splx(s);
804 return error;
805 }
806
807 /*
808 * Update parameters of an IPv6 interface address.
809 * If necessary, a new entry is created and linked into address chains.
810 * This function is separated from in6_control().
811 * XXX: should this be performed under splnet()?
812 */
813 static int
814 in6_update_ifa1(struct ifnet *ifp, struct in6_aliasreq *ifra,
815 struct in6_ifaddr *ia, int flags)
816 {
817 int error = 0, hostIsNew = 0, plen = -1;
818 struct in6_ifaddr *oia;
819 struct sockaddr_in6 dst6;
820 struct in6_addrlifetime *lt;
821 struct in6_multi_mship *imm;
822 struct in6_multi *in6m_sol;
823 struct rtentry *rt;
824 int dad_delay;
825
826 in6m_sol = NULL;
827
828 /* Validate parameters */
829 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
830 return EINVAL;
831
832 /*
833 * The destination address for a p2p link must have a family
834 * of AF_UNSPEC or AF_INET6.
835 */
836 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
837 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
838 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
839 return EAFNOSUPPORT;
840 /*
841 * validate ifra_prefixmask. don't check sin6_family, netmask
842 * does not carry fields other than sin6_len.
843 */
844 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
845 return EINVAL;
846 /*
847 * Because the IPv6 address architecture is classless, we require
848 * users to specify a (non 0) prefix length (mask) for a new address.
849 * We also require the prefix (when specified) mask is valid, and thus
850 * reject a non-consecutive mask.
851 */
852 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
853 return EINVAL;
854 if (ifra->ifra_prefixmask.sin6_len != 0) {
855 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
856 (u_char *)&ifra->ifra_prefixmask +
857 ifra->ifra_prefixmask.sin6_len);
858 if (plen <= 0)
859 return EINVAL;
860 } else {
861 /*
862 * In this case, ia must not be NULL. We just use its prefix
863 * length.
864 */
865 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
866 }
867 /*
868 * If the destination address on a p2p interface is specified,
869 * and the address is a scoped one, validate/set the scope
870 * zone identifier.
871 */
872 dst6 = ifra->ifra_dstaddr;
873 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
874 (dst6.sin6_family == AF_INET6)) {
875 struct in6_addr in6_tmp;
876 u_int32_t zoneid;
877
878 in6_tmp = dst6.sin6_addr;
879 if (in6_setscope(&in6_tmp, ifp, &zoneid))
880 return EINVAL; /* XXX: should be impossible */
881
882 if (dst6.sin6_scope_id != 0) {
883 if (dst6.sin6_scope_id != zoneid)
884 return EINVAL;
885 } else /* user omit to specify the ID. */
886 dst6.sin6_scope_id = zoneid;
887
888 /* convert into the internal form */
889 if (sa6_embedscope(&dst6, 0))
890 return EINVAL; /* XXX: should be impossible */
891 }
892 /*
893 * The destination address can be specified only for a p2p or a
894 * loopback interface. If specified, the corresponding prefix length
895 * must be 128.
896 */
897 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
898 #ifdef FORCE_P2PPLEN
899 int i;
900 #endif
901
902 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
903 /* XXX: noisy message */
904 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
905 "be specified for a p2p or a loopback IF only\n"));
906 return EINVAL;
907 }
908 if (plen != 128) {
909 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
910 "be 128 when dstaddr is specified\n"));
911 #ifdef FORCE_P2PPLEN
912 /*
913 * To be compatible with old configurations,
914 * such as ifconfig gif0 inet6 2001::1 2001::2
915 * prefixlen 126, we override the specified
916 * prefixmask as if the prefix length was 128.
917 */
918 ifra->ifra_prefixmask.sin6_len =
919 sizeof(struct sockaddr_in6);
920 for (i = 0; i < 4; i++)
921 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i] =
922 0xffffffff;
923 plen = 128;
924 #else
925 return EINVAL;
926 #endif
927 }
928 }
929 /* lifetime consistency check */
930 lt = &ifra->ifra_lifetime;
931 if (lt->ia6t_pltime > lt->ia6t_vltime)
932 return EINVAL;
933 if (lt->ia6t_vltime == 0) {
934 /*
935 * the following log might be noisy, but this is a typical
936 * configuration mistake or a tool's bug.
937 */
938 nd6log((LOG_INFO,
939 "in6_update_ifa: valid lifetime is 0 for %s\n",
940 ip6_sprintf(&ifra->ifra_addr.sin6_addr)));
941
942 if (ia == NULL)
943 return 0; /* there's nothing to do */
944 }
945
946 /*
947 * If this is a new address, allocate a new ifaddr and link it
948 * into chains.
949 */
950 if (ia == NULL) {
951 hostIsNew = 1;
952 /*
953 * When in6_update_ifa() is called in a process of a received
954 * RA, it is called under an interrupt context. So, we should
955 * call malloc with M_NOWAIT.
956 */
957 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
958 M_NOWAIT);
959 if (ia == NULL)
960 return ENOBUFS;
961 bzero((void *)ia, sizeof(*ia));
962 LIST_INIT(&ia->ia6_memberships);
963 /* Initialize the address and masks, and put time stamp */
964 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
965 ia->ia_addr.sin6_family = AF_INET6;
966 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
967 ia->ia6_createtime = time_second;
968 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
969 /*
970 * XXX: some functions expect that ifa_dstaddr is not
971 * NULL for p2p interfaces.
972 */
973 ia->ia_ifa.ifa_dstaddr =
974 (struct sockaddr *)&ia->ia_dstaddr;
975 } else {
976 ia->ia_ifa.ifa_dstaddr = NULL;
977 }
978 ia->ia_ifa.ifa_netmask =
979 (struct sockaddr *)&ia->ia_prefixmask;
980
981 ia->ia_ifp = ifp;
982 if ((oia = in6_ifaddr) != NULL) {
983 for ( ; oia->ia_next; oia = oia->ia_next)
984 continue;
985 oia->ia_next = ia;
986 } else
987 in6_ifaddr = ia;
988 /* gain a refcnt for the link from in6_ifaddr */
989 IFAREF(&ia->ia_ifa);
990
991 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
992 /* gain another refcnt for the link from if_addrlist */
993 IFAREF(&ia->ia_ifa);
994 }
995
996 /* update timestamp */
997 ia->ia6_updatetime = time_second;
998
999 /* set prefix mask */
1000 if (ifra->ifra_prefixmask.sin6_len) {
1001 /*
1002 * We prohibit changing the prefix length of an existing
1003 * address, because
1004 * + such an operation should be rare in IPv6, and
1005 * + the operation would confuse prefix management.
1006 */
1007 if (ia->ia_prefixmask.sin6_len &&
1008 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1009 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
1010 " existing (%s) address should not be changed\n",
1011 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1012 error = EINVAL;
1013 goto unlink;
1014 }
1015 ia->ia_prefixmask = ifra->ifra_prefixmask;
1016 }
1017
1018 /*
1019 * If a new destination address is specified, scrub the old one and
1020 * install the new destination. Note that the interface must be
1021 * p2p or loopback (see the check above.)
1022 */
1023 if (dst6.sin6_family == AF_INET6 &&
1024 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1025 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1026 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST) != 0) {
1027 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
1028 "a route to the old destination: %s\n",
1029 ip6_sprintf(&ia->ia_addr.sin6_addr)));
1030 /* proceed anyway... */
1031 } else
1032 ia->ia_flags &= ~IFA_ROUTE;
1033 ia->ia_dstaddr = dst6;
1034 }
1035
1036 /*
1037 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1038 * to see if the address is deprecated or invalidated, but initialize
1039 * these members for applications.
1040 */
1041 ia->ia6_lifetime = ifra->ifra_lifetime;
1042 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1043 ia->ia6_lifetime.ia6t_expire =
1044 time_second + ia->ia6_lifetime.ia6t_vltime;
1045 } else
1046 ia->ia6_lifetime.ia6t_expire = 0;
1047 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1048 ia->ia6_lifetime.ia6t_preferred =
1049 time_second + ia->ia6_lifetime.ia6t_pltime;
1050 } else
1051 ia->ia6_lifetime.ia6t_preferred = 0;
1052
1053 /* reset the interface and routing table appropriately. */
1054 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1055 goto unlink;
1056
1057 /*
1058 * configure address flags.
1059 */
1060 ia->ia6_flags = ifra->ifra_flags;
1061 /*
1062 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1063 * userland, make it deprecated.
1064 */
1065 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1066 ia->ia6_lifetime.ia6t_pltime = 0;
1067 ia->ia6_lifetime.ia6t_preferred = time_second;
1068 }
1069
1070 /*
1071 * Make the address tentative before joining multicast addresses,
1072 * so that corresponding MLD responses would not have a tentative
1073 * source address.
1074 */
1075 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1076 if (hostIsNew && in6if_do_dad(ifp))
1077 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1078
1079 /*
1080 * We are done if we have simply modified an existing address.
1081 */
1082 if (!hostIsNew)
1083 return error;
1084
1085 /*
1086 * Beyond this point, we should call in6_purgeaddr upon an error,
1087 * not just go to unlink.
1088 */
1089
1090 /* join necessary multicast groups */
1091 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1092 struct sockaddr_in6 mltaddr, mltmask;
1093 struct in6_addr llsol;
1094
1095 /* join solicited multicast addr for new host id */
1096 bzero(&llsol, sizeof(struct in6_addr));
1097 llsol.s6_addr16[0] = htons(0xff02);
1098 llsol.s6_addr32[1] = 0;
1099 llsol.s6_addr32[2] = htonl(1);
1100 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
1101 llsol.s6_addr8[12] = 0xff;
1102 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
1103 /* XXX: should not happen */
1104 log(LOG_ERR, "in6_update_ifa: "
1105 "in6_setscope failed\n");
1106 goto cleanup;
1107 }
1108 dad_delay = 0;
1109 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1110 /*
1111 * We need a random delay for DAD on the address
1112 * being configured. It also means delaying
1113 * transmission of the corresponding MLD report to
1114 * avoid report collision.
1115 * [draft-ietf-ipv6-rfc2462bis-02.txt]
1116 */
1117 dad_delay = arc4random() %
1118 (MAX_RTR_SOLICITATION_DELAY * hz);
1119 }
1120
1121 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1122 /* join solicited multicast addr for new host id */
1123 imm = in6_joingroup(ifp, &llsol, &error, dad_delay);
1124 if (!imm) {
1125 nd6log((LOG_ERR,
1126 "in6_update_ifa: addmulti "
1127 "failed for %s on %s (errno=%d)\n",
1128 ip6_sprintf(&llsol), if_name(ifp), error));
1129 goto cleanup;
1130 }
1131 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1132 in6m_sol = imm->i6mm_maddr;
1133
1134 bzero(&mltmask, sizeof(mltmask));
1135 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1136 mltmask.sin6_family = AF_INET6;
1137 mltmask.sin6_addr = in6mask32;
1138
1139 /*
1140 * join link-local all-nodes address
1141 */
1142 bzero(&mltaddr, sizeof(mltaddr));
1143 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1144 mltaddr.sin6_family = AF_INET6;
1145 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1146 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1147 goto cleanup; /* XXX: should not fail */
1148
1149 /*
1150 * XXX: do we really need this automatic routes?
1151 * We should probably reconsider this stuff. Most applications
1152 * actually do not need the routes, since they usually specify
1153 * the outgoing interface.
1154 */
1155 rt = rtalloc1((struct sockaddr *)&mltaddr, 0);
1156 if (rt) {
1157 if (memcmp(&mltaddr.sin6_addr,
1158 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1159 MLTMASK_LEN)) {
1160 RTFREE(rt);
1161 rt = NULL;
1162 } else if (rt->rt_ifp != ifp) {
1163 IN6_DPRINTF("%s: rt_ifp %p -> %p (%s) "
1164 "network %04x:%04x::/32 = %04x:%04x::/32\n",
1165 __func__, rt->rt_ifp, ifp, ifp->if_xname,
1166 ntohs(mltaddr.sin6_addr.s6_addr16[0]),
1167 ntohs(mltaddr.sin6_addr.s6_addr16[1]),
1168 ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr.s6_addr16[0],
1169 ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr.s6_addr16[1]);
1170 rt_replace_ifa(rt, &ia->ia_ifa);
1171 rt->rt_ifp = ifp;
1172 }
1173 }
1174 if (!rt) {
1175 struct rt_addrinfo info;
1176
1177 bzero(&info, sizeof(info));
1178 info.rti_info[RTAX_DST] = (struct sockaddr *)&mltaddr;
1179 info.rti_info[RTAX_GATEWAY] =
1180 (struct sockaddr *)&ia->ia_addr;
1181 info.rti_info[RTAX_NETMASK] =
1182 (struct sockaddr *)&mltmask;
1183 info.rti_info[RTAX_IFA] =
1184 (struct sockaddr *)&ia->ia_addr;
1185 /* XXX: we need RTF_CLONING to fake nd6_rtrequest */
1186 info.rti_flags = RTF_UP | RTF_CLONING;
1187 error = rtrequest1(RTM_ADD, &info, NULL);
1188 if (error)
1189 goto cleanup;
1190 } else {
1191 RTFREE(rt);
1192 }
1193 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1194 if (!imm) {
1195 nd6log((LOG_WARNING,
1196 "in6_update_ifa: addmulti failed for "
1197 "%s on %s (errno=%d)\n",
1198 ip6_sprintf(&mltaddr.sin6_addr),
1199 if_name(ifp), error));
1200 goto cleanup;
1201 }
1202 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1203
1204 /*
1205 * join node information group address
1206 */
1207 dad_delay = 0;
1208 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1209 /*
1210 * The spec doesn't say anything about delay for this
1211 * group, but the same logic should apply.
1212 */
1213 dad_delay = arc4random() %
1214 (MAX_RTR_SOLICITATION_DELAY * hz);
1215 }
1216 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr) != 0)
1217 ;
1218 else if ((imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1219 dad_delay)) == NULL) { /* XXX jinmei */
1220 nd6log((LOG_WARNING, "in6_update_ifa: "
1221 "addmulti failed for %s on %s (errno=%d)\n",
1222 ip6_sprintf(&mltaddr.sin6_addr),
1223 if_name(ifp), error));
1224 /* XXX not very fatal, go on... */
1225 } else {
1226 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1227 }
1228
1229
1230 /*
1231 * join interface-local all-nodes address.
1232 * (ff01::1%ifN, and ff01::%ifN/32)
1233 */
1234 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1235 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1236 goto cleanup; /* XXX: should not fail */
1237
1238 /* XXX: again, do we really need the route? */
1239 rt = rtalloc1((struct sockaddr *)&mltaddr, 0);
1240 if (rt) {
1241 /* 32bit came from "mltmask" */
1242 if (memcmp(&mltaddr.sin6_addr,
1243 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1244 32 / NBBY)) {
1245 RTFREE(rt);
1246 rt = NULL;
1247 } else if (rt->rt_ifp != ifp) {
1248 IN6_DPRINTF("%s: rt_ifp %p -> %p (%s) "
1249 "network %04x:%04x::/32 = %04x:%04x::/32\n",
1250 __func__, rt->rt_ifp, ifp, ifp->if_xname,
1251 ntohs(mltaddr.sin6_addr.s6_addr16[0]),
1252 ntohs(mltaddr.sin6_addr.s6_addr16[1]),
1253 ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr.s6_addr16[0],
1254 ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr.s6_addr16[1]);
1255 rt_replace_ifa(rt, &ia->ia_ifa);
1256 rt->rt_ifp = ifp;
1257 }
1258 }
1259 if (!rt) {
1260 struct rt_addrinfo info;
1261
1262 bzero(&info, sizeof(info));
1263 info.rti_info[RTAX_DST] = (struct sockaddr *)&mltaddr;
1264 info.rti_info[RTAX_GATEWAY] =
1265 (struct sockaddr *)&ia->ia_addr;
1266 info.rti_info[RTAX_NETMASK] =
1267 (struct sockaddr *)&mltmask;
1268 info.rti_info[RTAX_IFA] =
1269 (struct sockaddr *)&ia->ia_addr;
1270 info.rti_flags = RTF_UP | RTF_CLONING;
1271 error = rtrequest1(RTM_ADD, &info, NULL);
1272 if (error)
1273 goto cleanup;
1274 #undef MLTMASK_LEN
1275 } else {
1276 RTFREE(rt);
1277 }
1278 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1279 if (!imm) {
1280 nd6log((LOG_WARNING, "in6_update_ifa: "
1281 "addmulti failed for %s on %s (errno=%d)\n",
1282 ip6_sprintf(&mltaddr.sin6_addr),
1283 if_name(ifp), error));
1284 goto cleanup;
1285 } else {
1286 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1287 }
1288 }
1289
1290 /*
1291 * Perform DAD, if needed.
1292 * XXX It may be of use, if we can administratively
1293 * disable DAD.
1294 */
1295 if (hostIsNew && in6if_do_dad(ifp) &&
1296 ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1297 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1298 {
1299 int mindelay, maxdelay;
1300
1301 dad_delay = 0;
1302 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1303 /*
1304 * We need to impose a delay before sending an NS
1305 * for DAD. Check if we also needed a delay for the
1306 * corresponding MLD message. If we did, the delay
1307 * should be larger than the MLD delay (this could be
1308 * relaxed a bit, but this simple logic is at least
1309 * safe).
1310 */
1311 mindelay = 0;
1312 if (in6m_sol != NULL &&
1313 in6m_sol->in6m_state == MLD_REPORTPENDING) {
1314 mindelay = in6m_sol->in6m_timer;
1315 }
1316 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1317 if (maxdelay - mindelay == 0)
1318 dad_delay = 0;
1319 else {
1320 dad_delay =
1321 (arc4random() % (maxdelay - mindelay)) +
1322 mindelay;
1323 }
1324 }
1325 nd6_dad_start((struct ifaddr *)ia, dad_delay);
1326 }
1327
1328 return error;
1329
1330 unlink:
1331 /*
1332 * XXX: if a change of an existing address failed, keep the entry
1333 * anyway.
1334 */
1335 if (hostIsNew)
1336 in6_unlink_ifa(ia, ifp);
1337 return error;
1338
1339 cleanup:
1340 in6_purgeaddr(&ia->ia_ifa);
1341 return error;
1342 }
1343
1344 int
1345 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
1346 struct in6_ifaddr *ia, int flags)
1347 {
1348 int rc, s;
1349
1350 s = splnet();
1351 rc = in6_update_ifa1(ifp, ifra, ia, flags);
1352 splx(s);
1353 return rc;
1354 }
1355
1356 void
1357 in6_purgeaddr(ifa)
1358 struct ifaddr *ifa;
1359 {
1360 struct ifnet *ifp = ifa->ifa_ifp;
1361 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1362 struct in6_multi_mship *imm;
1363
1364 /* stop DAD processing */
1365 nd6_dad_stop(ifa);
1366
1367 /*
1368 * delete route to the destination of the address being purged.
1369 * The interface must be p2p or loopback in this case.
1370 */
1371 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1372 int e;
1373
1374 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1375 != 0) {
1376 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1377 "a route to the p2p destination: %s on %s, "
1378 "errno=%d\n",
1379 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1380 e);
1381 /* proceed anyway... */
1382 } else
1383 ia->ia_flags &= ~IFA_ROUTE;
1384 }
1385
1386 /* Remove ownaddr's loopback rtentry, if it exists. */
1387 in6_ifremloop(&(ia->ia_ifa));
1388
1389 /*
1390 * leave from multicast groups we have joined for the interface
1391 */
1392 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1393 LIST_REMOVE(imm, i6mm_chain);
1394 in6_leavegroup(imm);
1395 }
1396
1397 in6_unlink_ifa(ia, ifp);
1398 }
1399
1400 static void
1401 in6_unlink_ifa(ia, ifp)
1402 struct in6_ifaddr *ia;
1403 struct ifnet *ifp;
1404 {
1405 struct in6_ifaddr *oia;
1406 int s = splnet();
1407
1408 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1409 /* release a refcnt for the link from if_addrlist */
1410 IFAFREE(&ia->ia_ifa);
1411
1412 oia = ia;
1413 if (oia == (ia = in6_ifaddr))
1414 in6_ifaddr = ia->ia_next;
1415 else {
1416 while (ia->ia_next && (ia->ia_next != oia))
1417 ia = ia->ia_next;
1418 if (ia->ia_next)
1419 ia->ia_next = oia->ia_next;
1420 else {
1421 /* search failed */
1422 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1423 }
1424 }
1425
1426 if (!LIST_EMPTY(&oia->ia6_multiaddrs)) {
1427 /*
1428 * XXX thorpej (at) NetBSD.org -- if the interface is going
1429 * XXX away, don't save the multicast entries, delete them!
1430 */
1431 if (oia->ia_ifa.ifa_ifp->if_output == if_nulloutput) {
1432 struct in6_multi *in6m, *next;
1433
1434 for (in6m = LIST_FIRST(&oia->ia6_multiaddrs);
1435 in6m != NULL;
1436 in6m = next) {
1437 next = LIST_NEXT(in6m, in6m_entry);
1438 in6_delmulti(in6m);
1439 }
1440 } else
1441 in6_savemkludge(oia);
1442 }
1443
1444 /*
1445 * Release the reference to the base prefix. There should be a
1446 * positive reference.
1447 */
1448 if (oia->ia6_ndpr == NULL) {
1449 nd6log((LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1450 "%p has no prefix\n", oia));
1451 } else {
1452 oia->ia6_ndpr->ndpr_refcnt--;
1453 oia->ia6_ndpr = NULL;
1454 }
1455
1456 /*
1457 * Also, if the address being removed is autoconf'ed, call
1458 * pfxlist_onlink_check() since the release might affect the status of
1459 * other (detached) addresses.
1460 */
1461 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0)
1462 pfxlist_onlink_check();
1463
1464 /*
1465 * release another refcnt for the link from in6_ifaddr.
1466 * Note that we should decrement the refcnt at least once for all *BSD.
1467 */
1468 IFAFREE(&oia->ia_ifa);
1469
1470 splx(s);
1471 }
1472
1473 void
1474 in6_purgeif(ifp)
1475 struct ifnet *ifp;
1476 {
1477 struct ifaddr *ifa, *nifa;
1478
1479 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
1480 nifa = TAILQ_NEXT(ifa, ifa_list);
1481 if (ifa->ifa_addr->sa_family != AF_INET6)
1482 continue;
1483 in6_purgeaddr(ifa);
1484 }
1485
1486 in6_ifdetach(ifp);
1487 }
1488
1489 /*
1490 * SIOC[GAD]LIFADDR.
1491 * SIOCGLIFADDR: get first address. (?)
1492 * SIOCGLIFADDR with IFLR_PREFIX:
1493 * get first address that matches the specified prefix.
1494 * SIOCALIFADDR: add the specified address.
1495 * SIOCALIFADDR with IFLR_PREFIX:
1496 * add the specified prefix, filling hostid part from
1497 * the first link-local address. prefixlen must be <= 64.
1498 * SIOCDLIFADDR: delete the specified address.
1499 * SIOCDLIFADDR with IFLR_PREFIX:
1500 * delete the first address that matches the specified prefix.
1501 * return values:
1502 * EINVAL on invalid parameters
1503 * EADDRNOTAVAIL on prefix match failed/specified address not found
1504 * other values may be returned from in6_ioctl()
1505 *
1506 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1507 * this is to accommodate address naming scheme other than RFC2374,
1508 * in the future.
1509 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1510 * address encoding scheme. (see figure on page 8)
1511 */
1512 static int
1513 in6_lifaddr_ioctl(so, cmd, data, ifp, l)
1514 struct socket *so;
1515 u_long cmd;
1516 void * data;
1517 struct ifnet *ifp;
1518 struct lwp *l;
1519 {
1520 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1521 struct ifaddr *ifa;
1522 struct sockaddr *sa;
1523
1524 /* sanity checks */
1525 if (!data || !ifp) {
1526 panic("invalid argument to in6_lifaddr_ioctl");
1527 /* NOTREACHED */
1528 }
1529
1530 switch (cmd) {
1531 case SIOCGLIFADDR:
1532 /* address must be specified on GET with IFLR_PREFIX */
1533 if ((iflr->flags & IFLR_PREFIX) == 0)
1534 break;
1535 /* FALLTHROUGH */
1536 case SIOCALIFADDR:
1537 case SIOCDLIFADDR:
1538 /* address must be specified on ADD and DELETE */
1539 sa = (struct sockaddr *)&iflr->addr;
1540 if (sa->sa_family != AF_INET6)
1541 return EINVAL;
1542 if (sa->sa_len != sizeof(struct sockaddr_in6))
1543 return EINVAL;
1544 /* XXX need improvement */
1545 sa = (struct sockaddr *)&iflr->dstaddr;
1546 if (sa->sa_family && sa->sa_family != AF_INET6)
1547 return EINVAL;
1548 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1549 return EINVAL;
1550 break;
1551 default: /* shouldn't happen */
1552 #if 0
1553 panic("invalid cmd to in6_lifaddr_ioctl");
1554 /* NOTREACHED */
1555 #else
1556 return EOPNOTSUPP;
1557 #endif
1558 }
1559 if (sizeof(struct in6_addr) * NBBY < iflr->prefixlen)
1560 return EINVAL;
1561
1562 switch (cmd) {
1563 case SIOCALIFADDR:
1564 {
1565 struct in6_aliasreq ifra;
1566 struct in6_addr *xhostid = NULL;
1567 int prefixlen;
1568
1569 if ((iflr->flags & IFLR_PREFIX) != 0) {
1570 struct sockaddr_in6 *sin6;
1571
1572 /*
1573 * xhostid is to fill in the hostid part of the
1574 * address. xhostid points to the first link-local
1575 * address attached to the interface.
1576 */
1577 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1578 if (!ifa)
1579 return EADDRNOTAVAIL;
1580 xhostid = IFA_IN6(ifa);
1581
1582 /* prefixlen must be <= 64. */
1583 if (64 < iflr->prefixlen)
1584 return EINVAL;
1585 prefixlen = iflr->prefixlen;
1586
1587 /* hostid part must be zero. */
1588 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1589 if (sin6->sin6_addr.s6_addr32[2] != 0
1590 || sin6->sin6_addr.s6_addr32[3] != 0) {
1591 return EINVAL;
1592 }
1593 } else
1594 prefixlen = iflr->prefixlen;
1595
1596 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1597 bzero(&ifra, sizeof(ifra));
1598 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1599
1600 bcopy(&iflr->addr, &ifra.ifra_addr,
1601 ((struct sockaddr *)&iflr->addr)->sa_len);
1602 if (xhostid) {
1603 /* fill in hostid part */
1604 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1605 xhostid->s6_addr32[2];
1606 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1607 xhostid->s6_addr32[3];
1608 }
1609
1610 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1611 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1612 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1613 if (xhostid) {
1614 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1615 xhostid->s6_addr32[2];
1616 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1617 xhostid->s6_addr32[3];
1618 }
1619 }
1620
1621 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1622 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1623
1624 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
1625 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
1626 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1627 return in6_control(so, SIOCAIFADDR_IN6, (void *)&ifra, ifp, l);
1628 }
1629 case SIOCGLIFADDR:
1630 case SIOCDLIFADDR:
1631 {
1632 struct in6_ifaddr *ia;
1633 struct in6_addr mask, candidate, match;
1634 struct sockaddr_in6 *sin6;
1635 int cmp;
1636
1637 bzero(&mask, sizeof(mask));
1638 if (iflr->flags & IFLR_PREFIX) {
1639 /* lookup a prefix rather than address. */
1640 in6_prefixlen2mask(&mask, iflr->prefixlen);
1641
1642 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1643 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1644 match.s6_addr32[0] &= mask.s6_addr32[0];
1645 match.s6_addr32[1] &= mask.s6_addr32[1];
1646 match.s6_addr32[2] &= mask.s6_addr32[2];
1647 match.s6_addr32[3] &= mask.s6_addr32[3];
1648
1649 /* if you set extra bits, that's wrong */
1650 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1651 return EINVAL;
1652
1653 cmp = 1;
1654 } else {
1655 if (cmd == SIOCGLIFADDR) {
1656 /* on getting an address, take the 1st match */
1657 cmp = 0; /* XXX */
1658 } else {
1659 /* on deleting an address, do exact match */
1660 in6_prefixlen2mask(&mask, 128);
1661 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1662 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1663
1664 cmp = 1;
1665 }
1666 }
1667
1668 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1669 if (ifa->ifa_addr->sa_family != AF_INET6)
1670 continue;
1671 if (!cmp)
1672 break;
1673
1674 /*
1675 * XXX: this is adhoc, but is necessary to allow
1676 * a user to specify fe80::/64 (not /10) for a
1677 * link-local address.
1678 */
1679 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1680 in6_clearscope(&candidate);
1681 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1682 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1683 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1684 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1685 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1686 break;
1687 }
1688 if (!ifa)
1689 return EADDRNOTAVAIL;
1690 ia = ifa2ia6(ifa);
1691
1692 if (cmd == SIOCGLIFADDR) {
1693 int error;
1694
1695 /* fill in the if_laddrreq structure */
1696 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1697 error = sa6_recoverscope(
1698 (struct sockaddr_in6 *)&iflr->addr);
1699 if (error != 0)
1700 return error;
1701
1702 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1703 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1704 ia->ia_dstaddr.sin6_len);
1705 error = sa6_recoverscope(
1706 (struct sockaddr_in6 *)&iflr->dstaddr);
1707 if (error != 0)
1708 return error;
1709 } else
1710 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1711
1712 iflr->prefixlen =
1713 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1714
1715 iflr->flags = ia->ia6_flags; /* XXX */
1716
1717 return 0;
1718 } else {
1719 struct in6_aliasreq ifra;
1720
1721 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1722 bzero(&ifra, sizeof(ifra));
1723 bcopy(iflr->iflr_name, ifra.ifra_name,
1724 sizeof(ifra.ifra_name));
1725
1726 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1727 ia->ia_addr.sin6_len);
1728 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1729 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1730 ia->ia_dstaddr.sin6_len);
1731 } else {
1732 bzero(&ifra.ifra_dstaddr,
1733 sizeof(ifra.ifra_dstaddr));
1734 }
1735 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1736 ia->ia_prefixmask.sin6_len);
1737
1738 ifra.ifra_flags = ia->ia6_flags;
1739 return in6_control(so, SIOCDIFADDR_IN6, (void *)&ifra,
1740 ifp, l);
1741 }
1742 }
1743 }
1744
1745 return EOPNOTSUPP; /* just for safety */
1746 }
1747
1748 /*
1749 * Initialize an interface's internet6 address
1750 * and routing table entry.
1751 */
1752 static int
1753 in6_ifinit(ifp, ia, sin6, newhost)
1754 struct ifnet *ifp;
1755 struct in6_ifaddr *ia;
1756 struct sockaddr_in6 *sin6;
1757 int newhost;
1758 {
1759 int error = 0, plen, ifacount = 0;
1760 int s = splnet();
1761 struct ifaddr *ifa;
1762
1763 /*
1764 * Give the interface a chance to initialize
1765 * if this is its first address,
1766 * and to validate the address if necessary.
1767 */
1768 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1769 if (ifa->ifa_addr == NULL)
1770 continue; /* just for safety */
1771 if (ifa->ifa_addr->sa_family != AF_INET6)
1772 continue;
1773 ifacount++;
1774 }
1775
1776 ia->ia_addr = *sin6;
1777
1778 if (ifacount <= 1 && ifp->if_ioctl &&
1779 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (void *)ia))) {
1780 splx(s);
1781 return error;
1782 }
1783 splx(s);
1784
1785 ia->ia_ifa.ifa_metric = ifp->if_metric;
1786
1787 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1788
1789 /*
1790 * Special case:
1791 * If the destination address is specified for a point-to-point
1792 * interface, install a route to the destination as an interface
1793 * direct route.
1794 */
1795 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1796 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1797 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1798 RTF_UP | RTF_HOST)) != 0)
1799 return error;
1800 ia->ia_flags |= IFA_ROUTE;
1801 }
1802
1803 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1804 if (newhost) {
1805 /* set the rtrequest function to create llinfo */
1806 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1807 in6_ifaddloop(&(ia->ia_ifa));
1808 }
1809
1810 if (ifp->if_flags & IFF_MULTICAST)
1811 in6_restoremkludge(ia, ifp);
1812
1813 return error;
1814 }
1815
1816 /*
1817 * Find an IPv6 interface link-local address specific to an interface.
1818 */
1819 struct in6_ifaddr *
1820 in6ifa_ifpforlinklocal(const struct ifnet *ifp, const int ignoreflags)
1821 {
1822 struct ifaddr *ifa;
1823
1824 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1825 if (ifa->ifa_addr == NULL)
1826 continue; /* just for safety */
1827 if (ifa->ifa_addr->sa_family != AF_INET6)
1828 continue;
1829 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1830 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1831 ignoreflags) != 0)
1832 continue;
1833 break;
1834 }
1835 }
1836
1837 return (struct in6_ifaddr *)ifa;
1838 }
1839
1840
1841 /*
1842 * find the internet address corresponding to a given interface and address.
1843 */
1844 struct in6_ifaddr *
1845 in6ifa_ifpwithaddr(const struct ifnet *ifp, const struct in6_addr *addr)
1846 {
1847 struct ifaddr *ifa;
1848
1849 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1850 if (ifa->ifa_addr == NULL)
1851 continue; /* just for safety */
1852 if (ifa->ifa_addr->sa_family != AF_INET6)
1853 continue;
1854 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1855 break;
1856 }
1857
1858 return (struct in6_ifaddr *)ifa;
1859 }
1860
1861 /*
1862 * Convert IP6 address to printable (loggable) representation.
1863 */
1864 static int ip6round = 0;
1865 char *
1866 ip6_sprintf(addr)
1867 const struct in6_addr *addr;
1868 {
1869 static char ip6buf[8][48];
1870 int i;
1871 char *cp;
1872 const u_int16_t *a = (const u_int16_t *)addr;
1873 const u_int8_t *d;
1874 int dcolon = 0;
1875
1876 ip6round = (ip6round + 1) & 7;
1877 cp = ip6buf[ip6round];
1878
1879 for (i = 0; i < 8; i++) {
1880 if (dcolon == 1) {
1881 if (*a == 0) {
1882 if (i == 7)
1883 *cp++ = ':';
1884 a++;
1885 continue;
1886 } else
1887 dcolon = 2;
1888 }
1889 if (*a == 0) {
1890 if (dcolon == 0 && *(a + 1) == 0) {
1891 if (i == 0)
1892 *cp++ = ':';
1893 *cp++ = ':';
1894 dcolon = 1;
1895 } else {
1896 *cp++ = '0';
1897 *cp++ = ':';
1898 }
1899 a++;
1900 continue;
1901 }
1902 d = (const u_char *)a;
1903 *cp++ = hexdigits[*d >> 4];
1904 *cp++ = hexdigits[*d++ & 0xf];
1905 *cp++ = hexdigits[*d >> 4];
1906 *cp++ = hexdigits[*d & 0xf];
1907 *cp++ = ':';
1908 a++;
1909 }
1910 *--cp = 0;
1911 return ip6buf[ip6round];
1912 }
1913
1914 /*
1915 * Determine if an address is on a local network.
1916 */
1917 int
1918 in6_localaddr(in6)
1919 struct in6_addr *in6;
1920 {
1921 struct in6_ifaddr *ia;
1922
1923 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1924 return 1;
1925
1926 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1927 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1928 &ia->ia_prefixmask.sin6_addr))
1929 return 1;
1930
1931 return 0;
1932 }
1933
1934 int
1935 in6_is_addr_deprecated(sa6)
1936 struct sockaddr_in6 *sa6;
1937 {
1938 struct in6_ifaddr *ia;
1939
1940 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1941 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1942 &sa6->sin6_addr) &&
1943 #ifdef SCOPEDROUTING
1944 ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
1945 #endif
1946 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1947 return 1; /* true */
1948
1949 /* XXX: do we still have to go thru the rest of the list? */
1950 }
1951
1952 return 0; /* false */
1953 }
1954
1955 /*
1956 * return length of part which dst and src are equal
1957 * hard coding...
1958 */
1959 int
1960 in6_matchlen(src, dst)
1961 struct in6_addr *src, *dst;
1962 {
1963 int match = 0;
1964 u_char *s = (u_char *)src, *d = (u_char *)dst;
1965 u_char *lim = s + 16, r;
1966
1967 while (s < lim)
1968 if ((r = (*d++ ^ *s++)) != 0) {
1969 while (r < 128) {
1970 match++;
1971 r <<= 1;
1972 }
1973 break;
1974 } else
1975 match += NBBY;
1976 return match;
1977 }
1978
1979 /* XXX: to be scope conscious */
1980 int
1981 in6_are_prefix_equal(p1, p2, len)
1982 struct in6_addr *p1, *p2;
1983 int len;
1984 {
1985 int bytelen, bitlen;
1986
1987 /* sanity check */
1988 if (len < 0 || len > 128) {
1989 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1990 len);
1991 return 0;
1992 }
1993
1994 bytelen = len / NBBY;
1995 bitlen = len % NBBY;
1996
1997 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1998 return 0;
1999 if (bitlen != 0 &&
2000 p1->s6_addr[bytelen] >> (NBBY - bitlen) !=
2001 p2->s6_addr[bytelen] >> (NBBY - bitlen))
2002 return 0;
2003
2004 return 1;
2005 }
2006
2007 void
2008 in6_prefixlen2mask(maskp, len)
2009 struct in6_addr *maskp;
2010 int len;
2011 {
2012 static const u_char maskarray[NBBY] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2013 int bytelen, bitlen, i;
2014
2015 /* sanity check */
2016 if (len < 0 || len > 128) {
2017 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2018 len);
2019 return;
2020 }
2021
2022 bzero(maskp, sizeof(*maskp));
2023 bytelen = len / NBBY;
2024 bitlen = len % NBBY;
2025 for (i = 0; i < bytelen; i++)
2026 maskp->s6_addr[i] = 0xff;
2027 if (bitlen)
2028 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2029 }
2030
2031 /*
2032 * return the best address out of the same scope. if no address was
2033 * found, return the first valid address from designated IF.
2034 */
2035 struct in6_ifaddr *
2036 in6_ifawithifp(ifp, dst)
2037 struct ifnet *ifp;
2038 struct in6_addr *dst;
2039 {
2040 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2041 struct ifaddr *ifa;
2042 struct in6_ifaddr *besta = 0;
2043 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2044
2045 dep[0] = dep[1] = NULL;
2046
2047 /*
2048 * We first look for addresses in the same scope.
2049 * If there is one, return it.
2050 * If two or more, return one which matches the dst longest.
2051 * If none, return one of global addresses assigned other ifs.
2052 */
2053 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2054 if (ifa->ifa_addr->sa_family != AF_INET6)
2055 continue;
2056 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2057 continue; /* XXX: is there any case to allow anycast? */
2058 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2059 continue; /* don't use this interface */
2060 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2061 continue;
2062 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2063 if (ip6_use_deprecated)
2064 dep[0] = (struct in6_ifaddr *)ifa;
2065 continue;
2066 }
2067
2068 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2069 /*
2070 * call in6_matchlen() as few as possible
2071 */
2072 if (besta) {
2073 if (blen == -1)
2074 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2075 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2076 if (tlen > blen) {
2077 blen = tlen;
2078 besta = (struct in6_ifaddr *)ifa;
2079 }
2080 } else
2081 besta = (struct in6_ifaddr *)ifa;
2082 }
2083 }
2084 if (besta)
2085 return besta;
2086
2087 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2088 if (ifa->ifa_addr->sa_family != AF_INET6)
2089 continue;
2090 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2091 continue; /* XXX: is there any case to allow anycast? */
2092 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2093 continue; /* don't use this interface */
2094 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2095 continue;
2096 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2097 if (ip6_use_deprecated)
2098 dep[1] = (struct in6_ifaddr *)ifa;
2099 continue;
2100 }
2101
2102 return (struct in6_ifaddr *)ifa;
2103 }
2104
2105 /* use the last-resort values, that are, deprecated addresses */
2106 if (dep[0])
2107 return dep[0];
2108 if (dep[1])
2109 return dep[1];
2110
2111 return NULL;
2112 }
2113
2114 /*
2115 * perform DAD when interface becomes IFF_UP.
2116 */
2117 void
2118 in6_if_up(ifp)
2119 struct ifnet *ifp;
2120 {
2121 struct ifaddr *ifa;
2122 struct in6_ifaddr *ia;
2123
2124 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2125 if (ifa->ifa_addr->sa_family != AF_INET6)
2126 continue;
2127 ia = (struct in6_ifaddr *)ifa;
2128 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2129 /*
2130 * The TENTATIVE flag was likely set by hand
2131 * beforehand, implicitly indicating the need for DAD.
2132 * We may be able to skip the random delay in this
2133 * case, but we impose delays just in case.
2134 */
2135 nd6_dad_start(ifa,
2136 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2137 }
2138 }
2139
2140 /*
2141 * special cases, like 6to4, are handled in in6_ifattach
2142 */
2143 in6_ifattach(ifp, NULL);
2144 }
2145
2146 int
2147 in6if_do_dad(ifp)
2148 struct ifnet *ifp;
2149 {
2150 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2151 return 0;
2152
2153 switch (ifp->if_type) {
2154 case IFT_FAITH:
2155 /*
2156 * These interfaces do not have the IFF_LOOPBACK flag,
2157 * but loop packets back. We do not have to do DAD on such
2158 * interfaces. We should even omit it, because loop-backed
2159 * NS would confuse the DAD procedure.
2160 */
2161 return 0;
2162 default:
2163 /*
2164 * Our DAD routine requires the interface up and running.
2165 * However, some interfaces can be up before the RUNNING
2166 * status. Additionaly, users may try to assign addresses
2167 * before the interface becomes up (or running).
2168 * We simply skip DAD in such a case as a work around.
2169 * XXX: we should rather mark "tentative" on such addresses,
2170 * and do DAD after the interface becomes ready.
2171 */
2172 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2173 (IFF_UP|IFF_RUNNING))
2174 return 0;
2175
2176 return 1;
2177 }
2178 }
2179
2180 /*
2181 * Calculate max IPv6 MTU through all the interfaces and store it
2182 * to in6_maxmtu.
2183 */
2184 void
2185 in6_setmaxmtu()
2186 {
2187 unsigned long maxmtu = 0;
2188 struct ifnet *ifp;
2189
2190 TAILQ_FOREACH(ifp, &ifnet, if_list) {
2191 /* this function can be called during ifnet initialization */
2192 if (!ifp->if_afdata[AF_INET6])
2193 continue;
2194 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2195 IN6_LINKMTU(ifp) > maxmtu)
2196 maxmtu = IN6_LINKMTU(ifp);
2197 }
2198 if (maxmtu) /* update only when maxmtu is positive */
2199 in6_maxmtu = maxmtu;
2200 }
2201
2202 /*
2203 * Provide the length of interface identifiers to be used for the link attached
2204 * to the given interface. The length should be defined in "IPv6 over
2205 * xxx-link" document. Note that address architecture might also define
2206 * the length for a particular set of address prefixes, regardless of the
2207 * link type. As clarified in rfc2462bis, those two definitions should be
2208 * consistent, and those really are as of August 2004.
2209 */
2210 int
2211 in6_if2idlen(ifp)
2212 struct ifnet *ifp;
2213 {
2214 switch (ifp->if_type) {
2215 case IFT_ETHER: /* RFC2464 */
2216 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2217 case IFT_L2VLAN: /* ditto */
2218 case IFT_IEEE80211: /* ditto */
2219 case IFT_FDDI: /* RFC2467 */
2220 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2221 case IFT_PPP: /* RFC2472 */
2222 case IFT_ARCNET: /* RFC2497 */
2223 case IFT_FRELAY: /* RFC2590 */
2224 case IFT_IEEE1394: /* RFC3146 */
2225 case IFT_GIF: /* draft-ietf-v6ops-mech-v2-07 */
2226 case IFT_LOOP: /* XXX: is this really correct? */
2227 return 64;
2228 default:
2229 /*
2230 * Unknown link type:
2231 * It might be controversial to use the today's common constant
2232 * of 64 for these cases unconditionally. For full compliance,
2233 * we should return an error in this case. On the other hand,
2234 * if we simply miss the standard for the link type or a new
2235 * standard is defined for a new link type, the IFID length
2236 * is very likely to be the common constant. As a compromise,
2237 * we always use the constant, but make an explicit notice
2238 * indicating the "unknown" case.
2239 */
2240 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2241 return 64;
2242 }
2243 }
2244
2245 void *
2246 in6_domifattach(ifp)
2247 struct ifnet *ifp;
2248 {
2249 struct in6_ifextra *ext;
2250
2251 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2252 bzero(ext, sizeof(*ext));
2253
2254 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2255 M_IFADDR, M_WAITOK);
2256 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2257
2258 ext->icmp6_ifstat =
2259 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2260 M_IFADDR, M_WAITOK);
2261 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2262
2263 ext->nd_ifinfo = nd6_ifattach(ifp);
2264 ext->scope6_id = scope6_ifattach(ifp);
2265 return ext;
2266 }
2267
2268 void
2269 in6_domifdetach(struct ifnet *ifp, void *aux)
2270 {
2271 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2272
2273 nd6_ifdetach(ext->nd_ifinfo);
2274 free(ext->in6_ifstat, M_IFADDR);
2275 free(ext->icmp6_ifstat, M_IFADDR);
2276 scope6_ifdetach(ext->scope6_id);
2277 free(ext, M_IFADDR);
2278 }
2279