FreeBSD/Linux Kernel Cross Reference
sys/netinet6/in6.c
1 /* $FreeBSD: releng/6.0/sys/netinet6/in6.c 151947 2005-11-01 22:42:05Z suz $ */
2 /* $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ */
3
4 /*-
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*-
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 4. 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 "opt_inet.h"
65 #include "opt_inet6.h"
66
67 #include <sys/param.h>
68 #include <sys/errno.h>
69 #include <sys/malloc.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/sockio.h>
73 #include <sys/systm.h>
74 #include <sys/proc.h>
75 #include <sys/time.h>
76 #include <sys/kernel.h>
77 #include <sys/syslog.h>
78
79 #include <net/if.h>
80 #include <net/if_types.h>
81 #include <net/route.h>
82 #include <net/if_dl.h>
83
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/if_ether.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/in_pcb.h>
90
91 #include <netinet/ip6.h>
92 #include <netinet6/ip6_var.h>
93 #include <netinet6/nd6.h>
94 #include <netinet6/mld6_var.h>
95 #include <netinet6/ip6_mroute.h>
96 #include <netinet6/in6_ifattach.h>
97 #include <netinet6/scope6_var.h>
98 #include <netinet6/in6_pcb.h>
99
100 #include <net/net_osdep.h>
101
102 MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address");
103
104 /*
105 * Definitions of some costant IP6 addresses.
106 */
107 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
108 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
109 const struct in6_addr in6addr_nodelocal_allnodes =
110 IN6ADDR_NODELOCAL_ALLNODES_INIT;
111 const struct in6_addr in6addr_linklocal_allnodes =
112 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
113 const struct in6_addr in6addr_linklocal_allrouters =
114 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
115
116 const struct in6_addr in6mask0 = IN6MASK0;
117 const struct in6_addr in6mask32 = IN6MASK32;
118 const struct in6_addr in6mask64 = IN6MASK64;
119 const struct in6_addr in6mask96 = IN6MASK96;
120 const struct in6_addr in6mask128 = IN6MASK128;
121
122 const struct sockaddr_in6 sa6_any =
123 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
124
125 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
126 struct ifnet *, struct thread *));
127 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
128 struct sockaddr_in6 *, int));
129 static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
130
131 struct in6_multihead in6_multihead; /* XXX BSS initialization */
132 int (*faithprefix_p)(struct in6_addr *);
133
134 /*
135 * Subroutine for in6_ifaddloop() and in6_ifremloop().
136 * This routine does actual work.
137 */
138 static void
139 in6_ifloop_request(int cmd, struct ifaddr *ifa)
140 {
141 struct sockaddr_in6 all1_sa;
142 struct rtentry *nrt = NULL;
143 int e;
144
145 bzero(&all1_sa, sizeof(all1_sa));
146 all1_sa.sin6_family = AF_INET6;
147 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
148 all1_sa.sin6_addr = in6mask128;
149
150 /*
151 * We specify the address itself as the gateway, and set the
152 * RTF_LLINFO flag, so that the corresponding host route would have
153 * the flag, and thus applications that assume traditional behavior
154 * would be happy. Note that we assume the caller of the function
155 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
156 * which changes the outgoing interface to the loopback interface.
157 */
158 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
159 (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
160 if (e != 0) {
161 /* XXX need more descriptive message */
162 log(LOG_ERR, "in6_ifloop_request: "
163 "%s operation failed for %s (errno=%d)\n",
164 cmd == RTM_ADD ? "ADD" : "DELETE",
165 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
166 e);
167 }
168
169 if (nrt) {
170 RT_LOCK(nrt);
171 /*
172 * Make sure rt_ifa be equal to IFA, the second argument of
173 * the function. We need this because when we refer to
174 * rt_ifa->ia6_flags in ip6_input, we assume that the rt_ifa
175 * points to the address instead of the loopback address.
176 */
177 if (cmd == RTM_ADD && ifa != nrt->rt_ifa) {
178 IFAFREE(nrt->rt_ifa);
179 IFAREF(ifa);
180 nrt->rt_ifa = ifa;
181 }
182
183 /*
184 * Report the addition/removal of the address to the routing
185 * socket.
186 *
187 * XXX: since we called rtinit for a p2p interface with a
188 * destination, we end up reporting twice in such a case.
189 * Should we rather omit the second report?
190 */
191 rt_newaddrmsg(cmd, ifa, e, nrt);
192 if (cmd == RTM_DELETE) {
193 rtfree(nrt);
194 } else {
195 /* the cmd must be RTM_ADD here */
196 RT_REMREF(nrt);
197 RT_UNLOCK(nrt);
198 }
199 }
200 }
201
202 /*
203 * Add ownaddr as loopback rtentry. We previously add the route only if
204 * necessary (ex. on a p2p link). However, since we now manage addresses
205 * separately from prefixes, we should always add the route. We can't
206 * rely on the cloning mechanism from the corresponding interface route
207 * any more.
208 */
209 void
210 in6_ifaddloop(struct ifaddr *ifa)
211 {
212 struct rtentry *rt;
213 int need_loop;
214
215 /* If there is no loopback entry, allocate one. */
216 rt = rtalloc1(ifa->ifa_addr, 0, 0);
217 need_loop = (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
218 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0);
219 if (rt)
220 rtfree(rt);
221 if (need_loop)
222 in6_ifloop_request(RTM_ADD, ifa);
223 }
224
225 /*
226 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
227 * if it exists.
228 */
229 void
230 in6_ifremloop(struct ifaddr *ifa)
231 {
232 struct in6_ifaddr *ia;
233 struct rtentry *rt;
234 int ia_count = 0;
235
236 /*
237 * Some of BSD variants do not remove cloned routes
238 * from an interface direct route, when removing the direct route
239 * (see comments in net/net_osdep.h). Even for variants that do remove
240 * cloned routes, they could fail to remove the cloned routes when
241 * we handle multple addresses that share a common prefix.
242 * So, we should remove the route corresponding to the deleted address
243 * regardless of the result of in6_is_ifloop_auto().
244 */
245
246 /*
247 * Delete the entry only if exact one ifa exists. More than one ifa
248 * can exist if we assign a same single address to multiple
249 * (probably p2p) interfaces.
250 * XXX: we should avoid such a configuration in IPv6...
251 */
252 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
253 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
254 ia_count++;
255 if (ia_count > 1)
256 break;
257 }
258 }
259
260 if (ia_count == 1) {
261 /*
262 * Before deleting, check if a corresponding loopbacked host
263 * route surely exists. With this check, we can avoid to
264 * delete an interface direct route whose destination is same
265 * as the address being removed. This can happen when removing
266 * a subnet-router anycast address on an interface attahced
267 * to a shared medium.
268 */
269 rt = rtalloc1(ifa->ifa_addr, 0, 0);
270 if (rt != NULL) {
271 if ((rt->rt_flags & RTF_HOST) != 0 &&
272 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
273 rtfree(rt);
274 in6_ifloop_request(RTM_DELETE, ifa);
275 } else
276 RT_UNLOCK(rt);
277 }
278 }
279 }
280
281 int
282 in6_mask2len(mask, lim0)
283 struct in6_addr *mask;
284 u_char *lim0;
285 {
286 int x = 0, y;
287 u_char *lim = lim0, *p;
288
289 /* ignore the scope_id part */
290 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
291 lim = (u_char *)mask + sizeof(*mask);
292 for (p = (u_char *)mask; p < lim; x++, p++) {
293 if (*p != 0xff)
294 break;
295 }
296 y = 0;
297 if (p < lim) {
298 for (y = 0; y < 8; y++) {
299 if ((*p & (0x80 >> y)) == 0)
300 break;
301 }
302 }
303
304 /*
305 * when the limit pointer is given, do a stricter check on the
306 * remaining bits.
307 */
308 if (p < lim) {
309 if (y != 0 && (*p & (0x00ff >> y)) != 0)
310 return (-1);
311 for (p = p + 1; p < lim; p++)
312 if (*p != 0)
313 return (-1);
314 }
315
316 return x * 8 + y;
317 }
318
319 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
320 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
321
322 int
323 in6_control(so, cmd, data, ifp, td)
324 struct socket *so;
325 u_long cmd;
326 caddr_t data;
327 struct ifnet *ifp;
328 struct thread *td;
329 {
330 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
331 struct in6_ifaddr *ia = NULL;
332 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
333 int privileged;
334
335 privileged = 0;
336 if (td == NULL || !suser(td))
337 privileged++;
338
339 switch (cmd) {
340 case SIOCGETSGCNT_IN6:
341 case SIOCGETMIFCNT_IN6:
342 return (mrt6_ioctl(cmd, data));
343 }
344
345 switch(cmd) {
346 case SIOCAADDRCTL_POLICY:
347 case SIOCDADDRCTL_POLICY:
348 if (!privileged)
349 return (EPERM);
350 return (in6_src_ioctl(cmd, data));
351 }
352
353 if (ifp == NULL)
354 return (EOPNOTSUPP);
355
356 switch (cmd) {
357 case SIOCSNDFLUSH_IN6:
358 case SIOCSPFXFLUSH_IN6:
359 case SIOCSRTRFLUSH_IN6:
360 case SIOCSDEFIFACE_IN6:
361 case SIOCSIFINFO_FLAGS:
362 if (!privileged)
363 return (EPERM);
364 /* FALLTHROUGH */
365 case OSIOCGIFINFO_IN6:
366 case SIOCGIFINFO_IN6:
367 case SIOCGDRLST_IN6:
368 case SIOCGPRLST_IN6:
369 case SIOCGNBRINFO_IN6:
370 case SIOCGDEFIFACE_IN6:
371 return (nd6_ioctl(cmd, data, ifp));
372 }
373
374 switch (cmd) {
375 case SIOCSIFPREFIX_IN6:
376 case SIOCDIFPREFIX_IN6:
377 case SIOCAIFPREFIX_IN6:
378 case SIOCCIFPREFIX_IN6:
379 case SIOCSGIFPREFIX_IN6:
380 case SIOCGIFPREFIX_IN6:
381 log(LOG_NOTICE,
382 "prefix ioctls are now invalidated. "
383 "please use ifconfig.\n");
384 return (EOPNOTSUPP);
385 }
386
387 switch (cmd) {
388 case SIOCSSCOPE6:
389 if (!privileged)
390 return (EPERM);
391 return (scope6_set(ifp,
392 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
393 case SIOCGSCOPE6:
394 return (scope6_get(ifp,
395 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
396 case SIOCGSCOPE6DEF:
397 return (scope6_get_default((struct scope6_id *)
398 ifr->ifr_ifru.ifru_scope_id));
399 }
400
401 switch (cmd) {
402 case SIOCALIFADDR:
403 case SIOCDLIFADDR:
404 if (!privileged)
405 return (EPERM);
406 /* FALLTHROUGH */
407 case SIOCGLIFADDR:
408 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
409 }
410
411 /*
412 * Find address for this interface, if it exists.
413 */
414 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
415 struct sockaddr_in6 *sa6 =
416 (struct sockaddr_in6 *)&ifra->ifra_addr;
417
418 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
419 if (sa6->sin6_addr.s6_addr16[1] == 0) {
420 /* link ID is not embedded by the user */
421 sa6->sin6_addr.s6_addr16[1] =
422 htons(ifp->if_index);
423 } else if (sa6->sin6_addr.s6_addr16[1] !=
424 htons(ifp->if_index)) {
425 return (EINVAL); /* link ID contradicts */
426 }
427 if (sa6->sin6_scope_id) {
428 if (sa6->sin6_scope_id !=
429 (u_int32_t)ifp->if_index)
430 return (EINVAL);
431 sa6->sin6_scope_id = 0; /* XXX: good way? */
432 }
433 }
434 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
435 }
436
437 switch (cmd) {
438 case SIOCSIFADDR_IN6:
439 case SIOCSIFDSTADDR_IN6:
440 case SIOCSIFNETMASK_IN6:
441 /*
442 * Since IPv6 allows a node to assign multiple addresses
443 * on a single interface, SIOCSIFxxx ioctls are not suitable
444 * and should be unused.
445 */
446 /* we decided to obsolete this command (20000704) */
447 return (EINVAL);
448
449 case SIOCDIFADDR_IN6:
450 /*
451 * for IPv4, we look for existing in_ifaddr here to allow
452 * "ifconfig if0 delete" to remove first IPv4 address on the
453 * interface. For IPv6, as the spec allow multiple interface
454 * address from the day one, we consider "remove the first one"
455 * semantics to be not preferable.
456 */
457 if (ia == NULL)
458 return (EADDRNOTAVAIL);
459 /* FALLTHROUGH */
460 case SIOCAIFADDR_IN6:
461 /*
462 * We always require users to specify a valid IPv6 address for
463 * the corresponding operation.
464 */
465 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
466 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
467 return (EAFNOSUPPORT);
468 if (!privileged)
469 return (EPERM);
470
471 break;
472
473 case SIOCGIFADDR_IN6:
474 /* This interface is basically deprecated. use SIOCGIFCONF. */
475 /* FALLTHROUGH */
476 case SIOCGIFAFLAG_IN6:
477 case SIOCGIFNETMASK_IN6:
478 case SIOCGIFDSTADDR_IN6:
479 case SIOCGIFALIFETIME_IN6:
480 /* must think again about its semantics */
481 if (ia == NULL)
482 return (EADDRNOTAVAIL);
483 break;
484 case SIOCSIFALIFETIME_IN6:
485 {
486 struct in6_addrlifetime *lt;
487
488 if (!privileged)
489 return (EPERM);
490 if (ia == NULL)
491 return (EADDRNOTAVAIL);
492 /* sanity for overflow - beware unsigned */
493 lt = &ifr->ifr_ifru.ifru_lifetime;
494 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
495 lt->ia6t_vltime + time_second < time_second) {
496 return EINVAL;
497 }
498 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
499 lt->ia6t_pltime + time_second < time_second) {
500 return EINVAL;
501 }
502 break;
503 }
504 }
505
506 switch (cmd) {
507
508 case SIOCGIFADDR_IN6:
509 ifr->ifr_addr = ia->ia_addr;
510 break;
511
512 case SIOCGIFDSTADDR_IN6:
513 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
514 return (EINVAL);
515 /*
516 * XXX: should we check if ifa_dstaddr is NULL and return
517 * an error?
518 */
519 ifr->ifr_dstaddr = ia->ia_dstaddr;
520 break;
521
522 case SIOCGIFNETMASK_IN6:
523 ifr->ifr_addr = ia->ia_prefixmask;
524 break;
525
526 case SIOCGIFAFLAG_IN6:
527 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
528 break;
529
530 case SIOCGIFSTAT_IN6:
531 if (ifp == NULL)
532 return EINVAL;
533 bzero(&ifr->ifr_ifru.ifru_stat,
534 sizeof(ifr->ifr_ifru.ifru_stat));
535 ifr->ifr_ifru.ifru_stat =
536 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
537 break;
538
539 case SIOCGIFSTAT_ICMP6:
540 if (ifp == NULL)
541 return EINVAL;
542 bzero(&ifr->ifr_ifru.ifru_stat,
543 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
544 ifr->ifr_ifru.ifru_icmp6stat =
545 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
546 break;
547
548 case SIOCGIFALIFETIME_IN6:
549 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
550 break;
551
552 case SIOCSIFALIFETIME_IN6:
553 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
554 /* for sanity */
555 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
556 ia->ia6_lifetime.ia6t_expire =
557 time_second + ia->ia6_lifetime.ia6t_vltime;
558 } else
559 ia->ia6_lifetime.ia6t_expire = 0;
560 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
561 ia->ia6_lifetime.ia6t_preferred =
562 time_second + ia->ia6_lifetime.ia6t_pltime;
563 } else
564 ia->ia6_lifetime.ia6t_preferred = 0;
565 break;
566
567 case SIOCAIFADDR_IN6:
568 {
569 int i, error = 0;
570 struct nd_prefix pr0, *pr;
571
572 /*
573 * first, make or update the interface address structure,
574 * and link it to the list.
575 */
576 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
577 return (error);
578 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
579 == NULL) {
580 /*
581 * this can happen when the user specify the 0 valid
582 * lifetime.
583 */
584 break;
585 }
586
587 /*
588 * then, make the prefix on-link on the interface.
589 * XXX: we'd rather create the prefix before the address, but
590 * we need at least one address to install the corresponding
591 * interface route, so we configure the address first.
592 */
593
594 /*
595 * convert mask to prefix length (prefixmask has already
596 * been validated in in6_update_ifa().
597 */
598 bzero(&pr0, sizeof(pr0));
599 pr0.ndpr_ifp = ifp;
600 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
601 NULL);
602 if (pr0.ndpr_plen == 128) {
603 break; /* we don't need to install a host route. */
604 }
605 pr0.ndpr_prefix = ifra->ifra_addr;
606 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
607 /* apply the mask for safety. */
608 for (i = 0; i < 4; i++) {
609 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
610 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
611 }
612 /*
613 * XXX: since we don't have an API to set prefix (not address)
614 * lifetimes, we just use the same lifetimes as addresses.
615 * The (temporarily) installed lifetimes can be overridden by
616 * later advertised RAs (when accept_rtadv is non 0), which is
617 * an intended behavior.
618 */
619 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
620 pr0.ndpr_raf_auto =
621 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
622 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
623 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
624
625 /* add the prefix if not yet. */
626 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
627 /*
628 * nd6_prelist_add will install the corresponding
629 * interface route.
630 */
631 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
632 return (error);
633 if (pr == NULL) {
634 log(LOG_ERR, "nd6_prelist_add succeeded but "
635 "no prefix\n");
636 return (EINVAL); /* XXX panic here? */
637 }
638 }
639 /* relate the address to the prefix */
640 if (ia->ia6_ndpr == NULL) {
641 ia->ia6_ndpr = pr;
642 pr->ndpr_refcnt++;
643
644 /*
645 * If this is the first autoconf address from the
646 * prefix, create a temporary address as well
647 * (when required).
648 */
649 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
650 ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
651 int e;
652 if ((e = in6_tmpifadd(ia, 1)) != 0) {
653 log(LOG_NOTICE, "in6_control: failed "
654 "to create a temporary address, "
655 "errno=%d\n", e);
656 }
657 }
658 }
659
660 /*
661 * this might affect the status of autoconfigured addresses,
662 * that is, this address might make other addresses detached.
663 */
664 pfxlist_onlink_check();
665 if (error == 0 && ia)
666 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
667 break;
668 }
669
670 case SIOCDIFADDR_IN6:
671 {
672 struct nd_prefix *pr;
673
674 /*
675 * If the address being deleted is the only one that owns
676 * the corresponding prefix, expire the prefix as well.
677 * XXX: theoretically, we don't have to worry about such
678 * relationship, since we separate the address management
679 * and the prefix management. We do this, however, to provide
680 * as much backward compatibility as possible in terms of
681 * the ioctl operation.
682 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
683 */
684 pr = ia->ia6_ndpr;
685 in6_purgeaddr(&ia->ia_ifa);
686 if (pr && pr->ndpr_refcnt == 0)
687 prelist_remove(pr);
688 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
689 break;
690 }
691
692 default:
693 if (ifp == NULL || ifp->if_ioctl == 0)
694 return (EOPNOTSUPP);
695 return ((*ifp->if_ioctl)(ifp, cmd, data));
696 }
697
698 return (0);
699 }
700
701 /*
702 * Update parameters of an IPv6 interface address.
703 * If necessary, a new entry is created and linked into address chains.
704 * This function is separated from in6_control().
705 * XXX: should this be performed under splnet()?
706 */
707 int
708 in6_update_ifa(ifp, ifra, ia)
709 struct ifnet *ifp;
710 struct in6_aliasreq *ifra;
711 struct in6_ifaddr *ia;
712 {
713 int error = 0, hostIsNew = 0, plen = -1;
714 struct in6_ifaddr *oia;
715 struct sockaddr_in6 dst6;
716 struct in6_addrlifetime *lt;
717
718 /* Validate parameters */
719 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
720 return (EINVAL);
721
722 /*
723 * The destination address for a p2p link must have a family
724 * of AF_UNSPEC or AF_INET6.
725 */
726 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
727 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
728 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
729 return (EAFNOSUPPORT);
730 /*
731 * validate ifra_prefixmask. don't check sin6_family, netmask
732 * does not carry fields other than sin6_len.
733 */
734 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
735 return (EINVAL);
736 /*
737 * Because the IPv6 address architecture is classless, we require
738 * users to specify a (non 0) prefix length (mask) for a new address.
739 * We also require the prefix (when specified) mask is valid, and thus
740 * reject a non-consecutive mask.
741 */
742 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
743 return (EINVAL);
744 if (ifra->ifra_prefixmask.sin6_len != 0) {
745 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
746 (u_char *)&ifra->ifra_prefixmask +
747 ifra->ifra_prefixmask.sin6_len);
748 if (plen <= 0)
749 return (EINVAL);
750 } else {
751 /*
752 * In this case, ia must not be NULL. We just use its prefix
753 * length.
754 */
755 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
756 }
757 /*
758 * If the destination address on a p2p interface is specified,
759 * and the address is a scoped one, validate/set the scope
760 * zone identifier.
761 */
762 dst6 = ifra->ifra_dstaddr;
763 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
764 (dst6.sin6_family == AF_INET6)) {
765 u_int32_t zoneid;
766
767 if ((error = in6_recoverscope(&dst6,
768 &ifra->ifra_dstaddr.sin6_addr, ifp)) != 0)
769 return (error);
770 if (in6_addr2zoneid(ifp, &dst6.sin6_addr, &zoneid))
771 return (EINVAL);
772 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
773 dst6.sin6_scope_id = zoneid;
774 else if (dst6.sin6_scope_id != zoneid)
775 return (EINVAL); /* scope ID mismatch. */
776 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
777 != 0)
778 return (error);
779 dst6.sin6_scope_id = 0; /* XXX */
780 }
781 /*
782 * The destination address can be specified only for a p2p or a
783 * loopback interface. If specified, the corresponding prefix length
784 * must be 128.
785 */
786 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
787 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
788 /* XXX: noisy message */
789 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
790 "be specified for a p2p or a loopback IF only\n"));
791 return (EINVAL);
792 }
793 if (plen != 128) {
794 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
795 "be 128 when dstaddr is specified\n"));
796 return (EINVAL);
797 }
798 }
799 /* lifetime consistency check */
800 lt = &ifra->ifra_lifetime;
801 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
802 && lt->ia6t_vltime + time_second < time_second) {
803 return EINVAL;
804 }
805 if (lt->ia6t_vltime == 0) {
806 /*
807 * the following log might be noisy, but this is a typical
808 * configuration mistake or a tool's bug.
809 */
810 nd6log((LOG_INFO,
811 "in6_update_ifa: valid lifetime is 0 for %s\n",
812 ip6_sprintf(&ifra->ifra_addr.sin6_addr)));
813 }
814 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
815 && lt->ia6t_pltime + time_second < time_second) {
816 return EINVAL;
817 }
818
819 /*
820 * If this is a new address, allocate a new ifaddr and link it
821 * into chains.
822 */
823 if (ia == NULL) {
824 hostIsNew = 1;
825 /*
826 * When in6_update_ifa() is called in a process of a received
827 * RA, it is called under an interrupt context. So, we should
828 * call malloc with M_NOWAIT.
829 */
830 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
831 M_NOWAIT);
832 if (ia == NULL)
833 return (ENOBUFS);
834 bzero((caddr_t)ia, sizeof(*ia));
835 /* Initialize the address and masks */
836 IFA_LOCK_INIT(&ia->ia_ifa);
837 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
838 ia->ia_addr.sin6_family = AF_INET6;
839 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
840 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
841 /*
842 * XXX: some functions expect that ifa_dstaddr is not
843 * NULL for p2p interfaces.
844 */
845 ia->ia_ifa.ifa_dstaddr =
846 (struct sockaddr *)&ia->ia_dstaddr;
847 } else {
848 ia->ia_ifa.ifa_dstaddr = NULL;
849 }
850 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
851
852 ia->ia_ifp = ifp;
853 if ((oia = in6_ifaddr) != NULL) {
854 for ( ; oia->ia_next; oia = oia->ia_next)
855 continue;
856 oia->ia_next = ia;
857 } else
858 in6_ifaddr = ia;
859
860 ia->ia_ifa.ifa_refcnt = 1;
861 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
862 }
863
864 /* set prefix mask */
865 if (ifra->ifra_prefixmask.sin6_len) {
866 /*
867 * We prohibit changing the prefix length of an existing
868 * address, because
869 * + such an operation should be rare in IPv6, and
870 * + the operation would confuse prefix management.
871 */
872 if (ia->ia_prefixmask.sin6_len &&
873 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
874 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
875 " existing (%s) address should not be changed\n",
876 ip6_sprintf(&ia->ia_addr.sin6_addr)));
877 error = EINVAL;
878 goto unlink;
879 }
880 ia->ia_prefixmask = ifra->ifra_prefixmask;
881 }
882
883 /*
884 * If a new destination address is specified, scrub the old one and
885 * install the new destination. Note that the interface must be
886 * p2p or loopback (see the check above.)
887 */
888 if (dst6.sin6_family == AF_INET6 &&
889 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
890 int e;
891
892 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
893 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
894 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
895 "a route to the old destination: %s\n",
896 ip6_sprintf(&ia->ia_addr.sin6_addr)));
897 /* proceed anyway... */
898 } else
899 ia->ia_flags &= ~IFA_ROUTE;
900 ia->ia_dstaddr = dst6;
901 }
902
903 /* reset the interface and routing table appropriately. */
904 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
905 goto unlink;
906
907 /*
908 * Beyond this point, we should call in6_purgeaddr upon an error,
909 * not just go to unlink.
910 */
911
912 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
913 struct sockaddr_in6 mltaddr, mltmask;
914 struct in6_multi *in6m;
915
916 if (hostIsNew) {
917 /* join solicited multicast addr for new host id */
918 struct in6_addr llsol;
919
920 bzero(&llsol, sizeof(struct in6_addr));
921 llsol.s6_addr16[0] = htons(0xff02);
922 llsol.s6_addr16[1] = htons(ifp->if_index);
923 llsol.s6_addr32[1] = 0;
924 llsol.s6_addr32[2] = htonl(1);
925 llsol.s6_addr32[3] =
926 ifra->ifra_addr.sin6_addr.s6_addr32[3];
927 llsol.s6_addr8[12] = 0xff;
928 (void)in6_addmulti(&llsol, ifp, &error);
929 if (error != 0) {
930 nd6log((LOG_WARNING,
931 "in6_update_ifa: addmulti failed for "
932 "%s on %s (errno=%d)\n",
933 ip6_sprintf(&llsol), if_name(ifp),
934 error));
935 in6_purgeaddr((struct ifaddr *)ia);
936 return (error);
937 }
938 }
939
940 bzero(&mltmask, sizeof(mltmask));
941 mltmask.sin6_len = sizeof(struct sockaddr_in6);
942 mltmask.sin6_family = AF_INET6;
943 mltmask.sin6_addr = in6mask32;
944
945 /*
946 * join link-local all-nodes address
947 */
948 bzero(&mltaddr, sizeof(mltaddr));
949 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
950 mltaddr.sin6_family = AF_INET6;
951 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
952 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
953
954 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
955 if (in6m == NULL) {
956 rtrequest(RTM_ADD,
957 (struct sockaddr *)&mltaddr,
958 (struct sockaddr *)&ia->ia_addr,
959 (struct sockaddr *)&mltmask,
960 RTF_UP|RTF_CLONING, /* xxx */
961 (struct rtentry **)0);
962 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
963 if (error != 0) {
964 nd6log((LOG_WARNING,
965 "in6_update_ifa: addmulti failed for "
966 "%s on %s (errno=%d)\n",
967 ip6_sprintf(&mltaddr.sin6_addr),
968 if_name(ifp), error));
969 }
970 }
971
972 /*
973 * join node information group address
974 */
975 #define hostnamelen strlen(hostname)
976 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
977 == 0) {
978 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
979 if (in6m == NULL && ia != NULL) {
980 (void)in6_addmulti(&mltaddr.sin6_addr,
981 ifp, &error);
982 if (error != 0) {
983 nd6log((LOG_WARNING, "in6_update_ifa: "
984 "addmulti failed for "
985 "%s on %s (errno=%d)\n",
986 ip6_sprintf(&mltaddr.sin6_addr),
987 if_name(ifp), error));
988 }
989 }
990 }
991 #undef hostnamelen
992
993 /*
994 * join node-local all-nodes address, on loopback.
995 * XXX: since "node-local" is obsoleted by interface-local,
996 * we have to join the group on every interface with
997 * some interface-boundary restriction.
998 */
999 if (ifp->if_flags & IFF_LOOPBACK) {
1000 struct in6_ifaddr *ia_loop;
1001
1002 struct in6_addr loop6 = in6addr_loopback;
1003 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1004
1005 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1006
1007 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1008 if (in6m == NULL && ia_loop != NULL) {
1009 rtrequest(RTM_ADD,
1010 (struct sockaddr *)&mltaddr,
1011 (struct sockaddr *)&ia_loop->ia_addr,
1012 (struct sockaddr *)&mltmask,
1013 RTF_UP,
1014 (struct rtentry **)0);
1015 (void)in6_addmulti(&mltaddr.sin6_addr, ifp,
1016 &error);
1017 if (error != 0) {
1018 nd6log((LOG_WARNING, "in6_update_ifa: "
1019 "addmulti failed for %s on %s "
1020 "(errno=%d)\n",
1021 ip6_sprintf(&mltaddr.sin6_addr),
1022 if_name(ifp), error));
1023 }
1024 }
1025 }
1026 }
1027
1028 ia->ia6_flags = ifra->ifra_flags;
1029 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1030 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1031
1032 ia->ia6_lifetime = ifra->ifra_lifetime;
1033 /* for sanity */
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 /*
1046 * Perform DAD, if needed.
1047 * XXX It may be of use, if we can administratively
1048 * disable DAD.
1049 */
1050 if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1051 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1052 nd6_dad_start((struct ifaddr *)ia, NULL);
1053 }
1054
1055 return (error);
1056
1057 unlink:
1058 /*
1059 * XXX: if a change of an existing address failed, keep the entry
1060 * anyway.
1061 */
1062 if (hostIsNew)
1063 in6_unlink_ifa(ia, ifp);
1064 return (error);
1065 }
1066
1067 void
1068 in6_purgeaddr(ifa)
1069 struct ifaddr *ifa;
1070 {
1071 struct ifnet *ifp = ifa->ifa_ifp;
1072 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1073
1074 /* stop DAD processing */
1075 nd6_dad_stop(ifa);
1076
1077 /*
1078 * delete route to the destination of the address being purged.
1079 * The interface must be p2p or loopback in this case.
1080 */
1081 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1082 int e;
1083
1084 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1085 != 0) {
1086 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1087 "a route to the p2p destination: %s on %s, "
1088 "errno=%d\n",
1089 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1090 e);
1091 /* proceed anyway... */
1092 } else
1093 ia->ia_flags &= ~IFA_ROUTE;
1094 }
1095
1096 /* Remove ownaddr's loopback rtentry, if it exists. */
1097 in6_ifremloop(&(ia->ia_ifa));
1098
1099 if (ifp->if_flags & IFF_MULTICAST) {
1100 /*
1101 * delete solicited multicast addr for deleting host id
1102 */
1103 struct in6_multi *in6m;
1104 struct in6_addr llsol;
1105 bzero(&llsol, sizeof(struct in6_addr));
1106 llsol.s6_addr16[0] = htons(0xff02);
1107 llsol.s6_addr16[1] = htons(ifp->if_index);
1108 llsol.s6_addr32[1] = 0;
1109 llsol.s6_addr32[2] = htonl(1);
1110 llsol.s6_addr32[3] =
1111 ia->ia_addr.sin6_addr.s6_addr32[3];
1112 llsol.s6_addr8[12] = 0xff;
1113
1114 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1115 if (in6m)
1116 in6_delmulti(in6m);
1117 }
1118
1119 in6_unlink_ifa(ia, ifp);
1120 }
1121
1122 static void
1123 in6_unlink_ifa(ia, ifp)
1124 struct in6_ifaddr *ia;
1125 struct ifnet *ifp;
1126 {
1127 struct in6_ifaddr *oia;
1128 int s = splnet();
1129
1130 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1131
1132 oia = ia;
1133 if (oia == (ia = in6_ifaddr))
1134 in6_ifaddr = ia->ia_next;
1135 else {
1136 while (ia->ia_next && (ia->ia_next != oia))
1137 ia = ia->ia_next;
1138 if (ia->ia_next)
1139 ia->ia_next = oia->ia_next;
1140 else {
1141 /* search failed */
1142 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1143 }
1144 }
1145
1146 /*
1147 * Release the reference to the base prefix. There should be a
1148 * positive reference.
1149 */
1150 if (oia->ia6_ndpr == NULL) {
1151 nd6log((LOG_NOTICE,
1152 "in6_unlink_ifa: autoconf'ed address "
1153 "%p has no prefix\n", oia));
1154 } else {
1155 oia->ia6_ndpr->ndpr_refcnt--;
1156 oia->ia6_ndpr = NULL;
1157 }
1158
1159 /*
1160 * Also, if the address being removed is autoconf'ed, call
1161 * pfxlist_onlink_check() since the release might affect the status of
1162 * other (detached) addresses.
1163 */
1164 if ((oia->ia6_flags & IN6_IFF_AUTOCONF)) {
1165 pfxlist_onlink_check();
1166 }
1167
1168 /*
1169 * release another refcnt for the link from in6_ifaddr.
1170 * Note that we should decrement the refcnt at least once for all *BSD.
1171 */
1172 IFAFREE(&oia->ia_ifa);
1173
1174 splx(s);
1175 }
1176
1177 void
1178 in6_purgeif(ifp)
1179 struct ifnet *ifp;
1180 {
1181 struct ifaddr *ifa, *nifa;
1182
1183 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
1184 nifa = TAILQ_NEXT(ifa, ifa_list);
1185 if (ifa->ifa_addr->sa_family != AF_INET6)
1186 continue;
1187 in6_purgeaddr(ifa);
1188 }
1189
1190 in6_ifdetach(ifp);
1191 }
1192
1193 /*
1194 * SIOC[GAD]LIFADDR.
1195 * SIOCGLIFADDR: get first address. (?)
1196 * SIOCGLIFADDR with IFLR_PREFIX:
1197 * get first address that matches the specified prefix.
1198 * SIOCALIFADDR: add the specified address.
1199 * SIOCALIFADDR with IFLR_PREFIX:
1200 * add the specified prefix, filling hostid part from
1201 * the first link-local address. prefixlen must be <= 64.
1202 * SIOCDLIFADDR: delete the specified address.
1203 * SIOCDLIFADDR with IFLR_PREFIX:
1204 * delete the first address that matches the specified prefix.
1205 * return values:
1206 * EINVAL on invalid parameters
1207 * EADDRNOTAVAIL on prefix match failed/specified address not found
1208 * other values may be returned from in6_ioctl()
1209 *
1210 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1211 * this is to accomodate address naming scheme other than RFC2374,
1212 * in the future.
1213 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1214 * address encoding scheme. (see figure on page 8)
1215 */
1216 static int
1217 in6_lifaddr_ioctl(so, cmd, data, ifp, td)
1218 struct socket *so;
1219 u_long cmd;
1220 caddr_t data;
1221 struct ifnet *ifp;
1222 struct thread *td;
1223 {
1224 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1225 struct ifaddr *ifa;
1226 struct sockaddr *sa;
1227
1228 /* sanity checks */
1229 if (!data || !ifp) {
1230 panic("invalid argument to in6_lifaddr_ioctl");
1231 /* NOTREACHED */
1232 }
1233
1234 switch (cmd) {
1235 case SIOCGLIFADDR:
1236 /* address must be specified on GET with IFLR_PREFIX */
1237 if ((iflr->flags & IFLR_PREFIX) == 0)
1238 break;
1239 /* FALLTHROUGH */
1240 case SIOCALIFADDR:
1241 case SIOCDLIFADDR:
1242 /* address must be specified on ADD and DELETE */
1243 sa = (struct sockaddr *)&iflr->addr;
1244 if (sa->sa_family != AF_INET6)
1245 return EINVAL;
1246 if (sa->sa_len != sizeof(struct sockaddr_in6))
1247 return EINVAL;
1248 /* XXX need improvement */
1249 sa = (struct sockaddr *)&iflr->dstaddr;
1250 if (sa->sa_family && sa->sa_family != AF_INET6)
1251 return EINVAL;
1252 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1253 return EINVAL;
1254 break;
1255 default: /* shouldn't happen */
1256 #if 0
1257 panic("invalid cmd to in6_lifaddr_ioctl");
1258 /* NOTREACHED */
1259 #else
1260 return EOPNOTSUPP;
1261 #endif
1262 }
1263 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1264 return EINVAL;
1265
1266 switch (cmd) {
1267 case SIOCALIFADDR:
1268 {
1269 struct in6_aliasreq ifra;
1270 struct in6_addr *hostid = NULL;
1271 int prefixlen;
1272
1273 if ((iflr->flags & IFLR_PREFIX) != 0) {
1274 struct sockaddr_in6 *sin6;
1275
1276 /*
1277 * hostid is to fill in the hostid part of the
1278 * address. hostid points to the first link-local
1279 * address attached to the interface.
1280 */
1281 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1282 if (!ifa)
1283 return EADDRNOTAVAIL;
1284 hostid = IFA_IN6(ifa);
1285
1286 /* prefixlen must be <= 64. */
1287 if (64 < iflr->prefixlen)
1288 return EINVAL;
1289 prefixlen = iflr->prefixlen;
1290
1291 /* hostid part must be zero. */
1292 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1293 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1294 sin6->sin6_addr.s6_addr32[3] != 0) {
1295 return EINVAL;
1296 }
1297 } else
1298 prefixlen = iflr->prefixlen;
1299
1300 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1301 bzero(&ifra, sizeof(ifra));
1302 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1303
1304 bcopy(&iflr->addr, &ifra.ifra_addr,
1305 ((struct sockaddr *)&iflr->addr)->sa_len);
1306 if (hostid) {
1307 /* fill in hostid part */
1308 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1309 hostid->s6_addr32[2];
1310 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1311 hostid->s6_addr32[3];
1312 }
1313
1314 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1315 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1316 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1317 if (hostid) {
1318 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1319 hostid->s6_addr32[2];
1320 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1321 hostid->s6_addr32[3];
1322 }
1323 }
1324
1325 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1326 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1327
1328 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1329 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1330 }
1331 case SIOCGLIFADDR:
1332 case SIOCDLIFADDR:
1333 {
1334 struct in6_ifaddr *ia;
1335 struct in6_addr mask, candidate, match;
1336 struct sockaddr_in6 *sin6;
1337 int cmp;
1338
1339 bzero(&mask, sizeof(mask));
1340 if (iflr->flags & IFLR_PREFIX) {
1341 /* lookup a prefix rather than address. */
1342 in6_prefixlen2mask(&mask, iflr->prefixlen);
1343
1344 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1345 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1346 match.s6_addr32[0] &= mask.s6_addr32[0];
1347 match.s6_addr32[1] &= mask.s6_addr32[1];
1348 match.s6_addr32[2] &= mask.s6_addr32[2];
1349 match.s6_addr32[3] &= mask.s6_addr32[3];
1350
1351 /* if you set extra bits, that's wrong */
1352 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1353 return EINVAL;
1354
1355 cmp = 1;
1356 } else {
1357 if (cmd == SIOCGLIFADDR) {
1358 /* on getting an address, take the 1st match */
1359 cmp = 0; /* XXX */
1360 } else {
1361 /* on deleting an address, do exact match */
1362 in6_prefixlen2mask(&mask, 128);
1363 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1364 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1365
1366 cmp = 1;
1367 }
1368 }
1369
1370 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1371 if (ifa->ifa_addr->sa_family != AF_INET6)
1372 continue;
1373 if (!cmp)
1374 break;
1375
1376 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1377 /*
1378 * XXX: this is adhoc, but is necessary to allow
1379 * a user to specify fe80::/64 (not /10) for a
1380 * link-local address.
1381 */
1382 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1383 candidate.s6_addr16[1] = 0;
1384 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1385 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1386 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1387 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1388 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1389 break;
1390 }
1391 if (!ifa)
1392 return EADDRNOTAVAIL;
1393 ia = ifa2ia6(ifa);
1394
1395 if (cmd == SIOCGLIFADDR) {
1396 struct sockaddr_in6 *s6;
1397
1398 /* fill in the if_laddrreq structure */
1399 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1400 s6 = (struct sockaddr_in6 *)&iflr->addr;
1401 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1402 s6->sin6_addr.s6_addr16[1] = 0;
1403 if (in6_addr2zoneid(ifp, &s6->sin6_addr,
1404 &s6->sin6_scope_id))
1405 return (EINVAL); /* XXX */
1406 }
1407 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1408 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1409 ia->ia_dstaddr.sin6_len);
1410 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1411 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1412 s6->sin6_addr.s6_addr16[1] = 0;
1413 if (in6_addr2zoneid(ifp,
1414 &s6->sin6_addr, &s6->sin6_scope_id))
1415 return (EINVAL); /* EINVAL */
1416 }
1417 } else
1418 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1419
1420 iflr->prefixlen =
1421 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1422
1423 iflr->flags = ia->ia6_flags; /* XXX */
1424
1425 return 0;
1426 } else {
1427 struct in6_aliasreq ifra;
1428
1429 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1430 bzero(&ifra, sizeof(ifra));
1431 bcopy(iflr->iflr_name, ifra.ifra_name,
1432 sizeof(ifra.ifra_name));
1433
1434 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1435 ia->ia_addr.sin6_len);
1436 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1437 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1438 ia->ia_dstaddr.sin6_len);
1439 } else {
1440 bzero(&ifra.ifra_dstaddr,
1441 sizeof(ifra.ifra_dstaddr));
1442 }
1443 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1444 ia->ia_prefixmask.sin6_len);
1445
1446 ifra.ifra_flags = ia->ia6_flags;
1447 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1448 ifp, td);
1449 }
1450 }
1451 }
1452
1453 return EOPNOTSUPP; /* just for safety */
1454 }
1455
1456 /*
1457 * Initialize an interface's intetnet6 address
1458 * and routing table entry.
1459 */
1460 static int
1461 in6_ifinit(ifp, ia, sin6, newhost)
1462 struct ifnet *ifp;
1463 struct in6_ifaddr *ia;
1464 struct sockaddr_in6 *sin6;
1465 int newhost;
1466 {
1467 int error = 0, plen, ifacount = 0;
1468 int s = splimp();
1469 struct ifaddr *ifa;
1470
1471 /*
1472 * Give the interface a chance to initialize
1473 * if this is its first address,
1474 * and to validate the address if necessary.
1475 */
1476 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1477 if (ifa->ifa_addr == NULL)
1478 continue; /* just for safety */
1479 if (ifa->ifa_addr->sa_family != AF_INET6)
1480 continue;
1481 ifacount++;
1482 }
1483
1484 ia->ia_addr = *sin6;
1485
1486 if (ifacount <= 1 && ifp->if_ioctl) {
1487 IFF_LOCKGIANT(ifp);
1488 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1489 IFF_UNLOCKGIANT(ifp);
1490 if (error) {
1491 splx(s);
1492 return (error);
1493 }
1494 }
1495 splx(s);
1496
1497 ia->ia_ifa.ifa_metric = ifp->if_metric;
1498
1499 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1500
1501 /*
1502 * Special case:
1503 * If a new destination address is specified for a point-to-point
1504 * interface, install a route to the destination as an interface
1505 * direct route.
1506 * XXX: the logic below rejects assigning multiple addresses on a p2p
1507 * interface that share a same destination.
1508 */
1509 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1510 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1511 ia->ia_dstaddr.sin6_family == AF_INET6) {
1512 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1513 RTF_UP | RTF_HOST)) != 0)
1514 return (error);
1515 ia->ia_flags |= IFA_ROUTE;
1516 }
1517 if (plen < 128) {
1518 /*
1519 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1520 */
1521 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1522 }
1523
1524 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1525 if (newhost) {
1526 /* set the rtrequest function to create llinfo */
1527 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1528 in6_ifaddloop(&(ia->ia_ifa));
1529 }
1530
1531 return (error);
1532 }
1533
1534 struct in6_multi_mship *
1535 in6_joingroup(ifp, addr, errorp)
1536 struct ifnet *ifp;
1537 struct in6_addr *addr;
1538 int *errorp;
1539 {
1540 struct in6_multi_mship *imm;
1541
1542 imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1543 if (!imm) {
1544 *errorp = ENOBUFS;
1545 return NULL;
1546 }
1547 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1548 if (!imm->i6mm_maddr) {
1549 /* *errorp is alrady set */
1550 free(imm, M_IPMADDR);
1551 return NULL;
1552 }
1553 return imm;
1554 }
1555
1556 int
1557 in6_leavegroup(imm)
1558 struct in6_multi_mship *imm;
1559 {
1560
1561 if (imm->i6mm_maddr)
1562 in6_delmulti(imm->i6mm_maddr);
1563 free(imm, M_IPMADDR);
1564 return 0;
1565 }
1566
1567 /*
1568 * Find an IPv6 interface link-local address specific to an interface.
1569 */
1570 struct in6_ifaddr *
1571 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1572 struct ifnet *ifp;
1573 int ignoreflags;
1574 {
1575 struct ifaddr *ifa;
1576
1577 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1578 if (ifa->ifa_addr == NULL)
1579 continue; /* just for safety */
1580 if (ifa->ifa_addr->sa_family != AF_INET6)
1581 continue;
1582 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1583 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1584 ignoreflags) != 0)
1585 continue;
1586 break;
1587 }
1588 }
1589
1590 return ((struct in6_ifaddr *)ifa);
1591 }
1592
1593
1594 /*
1595 * find the internet address corresponding to a given interface and address.
1596 */
1597 struct in6_ifaddr *
1598 in6ifa_ifpwithaddr(ifp, addr)
1599 struct ifnet *ifp;
1600 struct in6_addr *addr;
1601 {
1602 struct ifaddr *ifa;
1603
1604 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1605 if (ifa->ifa_addr == NULL)
1606 continue; /* just for safety */
1607 if (ifa->ifa_addr->sa_family != AF_INET6)
1608 continue;
1609 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1610 break;
1611 }
1612
1613 return ((struct in6_ifaddr *)ifa);
1614 }
1615
1616 /*
1617 * Convert IP6 address to printable (loggable) representation.
1618 */
1619 static char digits[] = "0123456789abcdef";
1620 static int ip6round = 0;
1621 char *
1622 ip6_sprintf(addr)
1623 const struct in6_addr *addr;
1624 {
1625 static char ip6buf[8][48];
1626 int i;
1627 char *cp;
1628 const u_int16_t *a = (const u_int16_t *)addr;
1629 const u_int8_t *d;
1630 int dcolon = 0;
1631
1632 ip6round = (ip6round + 1) & 7;
1633 cp = ip6buf[ip6round];
1634
1635 for (i = 0; i < 8; i++) {
1636 if (dcolon == 1) {
1637 if (*a == 0) {
1638 if (i == 7)
1639 *cp++ = ':';
1640 a++;
1641 continue;
1642 } else
1643 dcolon = 2;
1644 }
1645 if (*a == 0) {
1646 if (dcolon == 0 && *(a + 1) == 0) {
1647 if (i == 0)
1648 *cp++ = ':';
1649 *cp++ = ':';
1650 dcolon = 1;
1651 } else {
1652 *cp++ = '';
1653 *cp++ = ':';
1654 }
1655 a++;
1656 continue;
1657 }
1658 d = (const u_char *)a;
1659 *cp++ = digits[*d >> 4];
1660 *cp++ = digits[*d++ & 0xf];
1661 *cp++ = digits[*d >> 4];
1662 *cp++ = digits[*d & 0xf];
1663 *cp++ = ':';
1664 a++;
1665 }
1666 *--cp = 0;
1667 return (ip6buf[ip6round]);
1668 }
1669
1670 int
1671 in6_localaddr(in6)
1672 struct in6_addr *in6;
1673 {
1674 struct in6_ifaddr *ia;
1675
1676 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1677 return 1;
1678
1679 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1680 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1681 &ia->ia_prefixmask.sin6_addr)) {
1682 return 1;
1683 }
1684 }
1685
1686 return (0);
1687 }
1688
1689 int
1690 in6_is_addr_deprecated(sa6)
1691 struct sockaddr_in6 *sa6;
1692 {
1693 struct in6_ifaddr *ia;
1694
1695 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1696 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1697 &sa6->sin6_addr) &&
1698 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1699 return (1); /* true */
1700
1701 /* XXX: do we still have to go thru the rest of the list? */
1702 }
1703
1704 return (0); /* false */
1705 }
1706
1707 /*
1708 * return length of part which dst and src are equal
1709 * hard coding...
1710 */
1711 int
1712 in6_matchlen(src, dst)
1713 struct in6_addr *src, *dst;
1714 {
1715 int match = 0;
1716 u_char *s = (u_char *)src, *d = (u_char *)dst;
1717 u_char *lim = s + 16, r;
1718
1719 while (s < lim)
1720 if ((r = (*d++ ^ *s++)) != 0) {
1721 while (r < 128) {
1722 match++;
1723 r <<= 1;
1724 }
1725 break;
1726 } else
1727 match += 8;
1728 return match;
1729 }
1730
1731 /* XXX: to be scope conscious */
1732 int
1733 in6_are_prefix_equal(p1, p2, len)
1734 struct in6_addr *p1, *p2;
1735 int len;
1736 {
1737 int bytelen, bitlen;
1738
1739 /* sanity check */
1740 if (0 > len || len > 128) {
1741 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1742 len);
1743 return (0);
1744 }
1745
1746 bytelen = len / 8;
1747 bitlen = len % 8;
1748
1749 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1750 return (0);
1751 if (bitlen != 0 &&
1752 p1->s6_addr[bytelen] >> (8 - bitlen) !=
1753 p2->s6_addr[bytelen] >> (8 - bitlen))
1754 return (0);
1755
1756 return (1);
1757 }
1758
1759 void
1760 in6_prefixlen2mask(maskp, len)
1761 struct in6_addr *maskp;
1762 int len;
1763 {
1764 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1765 int bytelen, bitlen, i;
1766
1767 /* sanity check */
1768 if (0 > len || len > 128) {
1769 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1770 len);
1771 return;
1772 }
1773
1774 bzero(maskp, sizeof(*maskp));
1775 bytelen = len / 8;
1776 bitlen = len % 8;
1777 for (i = 0; i < bytelen; i++)
1778 maskp->s6_addr[i] = 0xff;
1779 if (bitlen)
1780 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1781 }
1782
1783 /*
1784 * return the best address out of the same scope. if no address was
1785 * found, return the first valid address from designated IF.
1786 */
1787 struct in6_ifaddr *
1788 in6_ifawithifp(ifp, dst)
1789 struct ifnet *ifp;
1790 struct in6_addr *dst;
1791 {
1792 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1793 struct ifaddr *ifa;
1794 struct in6_ifaddr *besta = 0;
1795 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
1796
1797 dep[0] = dep[1] = NULL;
1798
1799 /*
1800 * We first look for addresses in the same scope.
1801 * If there is one, return it.
1802 * If two or more, return one which matches the dst longest.
1803 * If none, return one of global addresses assigned other ifs.
1804 */
1805 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1806 if (ifa->ifa_addr->sa_family != AF_INET6)
1807 continue;
1808 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1809 continue; /* XXX: is there any case to allow anycast? */
1810 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1811 continue; /* don't use this interface */
1812 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1813 continue;
1814 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1815 if (ip6_use_deprecated)
1816 dep[0] = (struct in6_ifaddr *)ifa;
1817 continue;
1818 }
1819
1820 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1821 /*
1822 * call in6_matchlen() as few as possible
1823 */
1824 if (besta) {
1825 if (blen == -1)
1826 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1827 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1828 if (tlen > blen) {
1829 blen = tlen;
1830 besta = (struct in6_ifaddr *)ifa;
1831 }
1832 } else
1833 besta = (struct in6_ifaddr *)ifa;
1834 }
1835 }
1836 if (besta)
1837 return (besta);
1838
1839 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1840 if (ifa->ifa_addr->sa_family != AF_INET6)
1841 continue;
1842 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1843 continue; /* XXX: is there any case to allow anycast? */
1844 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1845 continue; /* don't use this interface */
1846 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1847 continue;
1848 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1849 if (ip6_use_deprecated)
1850 dep[1] = (struct in6_ifaddr *)ifa;
1851 continue;
1852 }
1853
1854 return (struct in6_ifaddr *)ifa;
1855 }
1856
1857 /* use the last-resort values, that are, deprecated addresses */
1858 if (dep[0])
1859 return dep[0];
1860 if (dep[1])
1861 return dep[1];
1862
1863 return NULL;
1864 }
1865
1866 /*
1867 * perform DAD when interface becomes IFF_UP.
1868 */
1869 void
1870 in6_if_up(ifp)
1871 struct ifnet *ifp;
1872 {
1873 struct ifaddr *ifa;
1874 struct in6_ifaddr *ia;
1875 int dad_delay; /* delay ticks before DAD output */
1876
1877 /*
1878 * special cases, like 6to4, are handled in in6_ifattach
1879 */
1880 in6_ifattach(ifp, NULL);
1881
1882 dad_delay = 0;
1883 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1884 if (ifa->ifa_addr->sa_family != AF_INET6)
1885 continue;
1886 ia = (struct in6_ifaddr *)ifa;
1887 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
1888 nd6_dad_start(ifa, &dad_delay);
1889 }
1890 }
1891
1892 int
1893 in6if_do_dad(ifp)
1894 struct ifnet *ifp;
1895 {
1896 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
1897 return (0);
1898
1899 switch (ifp->if_type) {
1900 #ifdef IFT_DUMMY
1901 case IFT_DUMMY:
1902 #endif
1903 case IFT_FAITH:
1904 /*
1905 * These interfaces do not have the IFF_LOOPBACK flag,
1906 * but loop packets back. We do not have to do DAD on such
1907 * interfaces. We should even omit it, because loop-backed
1908 * NS would confuse the DAD procedure.
1909 */
1910 return (0);
1911 default:
1912 /*
1913 * Our DAD routine requires the interface up and running.
1914 * However, some interfaces can be up before the RUNNING
1915 * status. Additionaly, users may try to assign addresses
1916 * before the interface becomes up (or running).
1917 * We simply skip DAD in such a case as a work around.
1918 * XXX: we should rather mark "tentative" on such addresses,
1919 * and do DAD after the interface becomes ready.
1920 */
1921 if (!((ifp->if_flags & IFF_UP) &&
1922 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
1923 return (0);
1924
1925 return (1);
1926 }
1927 }
1928
1929 /*
1930 * Calculate max IPv6 MTU through all the interfaces and store it
1931 * to in6_maxmtu.
1932 */
1933 void
1934 in6_setmaxmtu()
1935 {
1936 unsigned long maxmtu = 0;
1937 struct ifnet *ifp;
1938
1939 IFNET_RLOCK();
1940 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1941 /* this function can be called during ifnet initialization */
1942 if (!ifp->if_afdata[AF_INET6])
1943 continue;
1944 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
1945 IN6_LINKMTU(ifp) > maxmtu)
1946 maxmtu = IN6_LINKMTU(ifp);
1947 }
1948 IFNET_RUNLOCK();
1949 if (maxmtu) /* update only when maxmtu is positive */
1950 in6_maxmtu = maxmtu;
1951 }
1952
1953 void *
1954 in6_domifattach(ifp)
1955 struct ifnet *ifp;
1956 {
1957 struct in6_ifextra *ext;
1958
1959 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
1960 bzero(ext, sizeof(*ext));
1961
1962 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
1963 M_IFADDR, M_WAITOK);
1964 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
1965
1966 ext->icmp6_ifstat =
1967 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
1968 M_IFADDR, M_WAITOK);
1969 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
1970
1971 ext->nd_ifinfo = nd6_ifattach(ifp);
1972 ext->scope6_id = scope6_ifattach(ifp);
1973 return ext;
1974 }
1975
1976 void
1977 in6_domifdetach(ifp, aux)
1978 struct ifnet *ifp;
1979 void *aux;
1980 {
1981 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
1982
1983 scope6_ifdetach(ext->scope6_id);
1984 nd6_ifdetach(ext->nd_ifinfo);
1985 free(ext->in6_ifstat, M_IFADDR);
1986 free(ext->icmp6_ifstat, M_IFADDR);
1987 free(ext, M_IFADDR);
1988 }
1989
1990 /*
1991 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
1992 * v4 mapped addr or v4 compat addr
1993 */
1994 void
1995 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
1996 {
1997 bzero(sin, sizeof(*sin));
1998 sin->sin_len = sizeof(struct sockaddr_in);
1999 sin->sin_family = AF_INET;
2000 sin->sin_port = sin6->sin6_port;
2001 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2002 }
2003
2004 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2005 void
2006 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2007 {
2008 bzero(sin6, sizeof(*sin6));
2009 sin6->sin6_len = sizeof(struct sockaddr_in6);
2010 sin6->sin6_family = AF_INET6;
2011 sin6->sin6_port = sin->sin_port;
2012 sin6->sin6_addr.s6_addr32[0] = 0;
2013 sin6->sin6_addr.s6_addr32[1] = 0;
2014 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2015 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2016 }
2017
2018 /* Convert sockaddr_in6 into sockaddr_in. */
2019 void
2020 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2021 {
2022 struct sockaddr_in *sin_p;
2023 struct sockaddr_in6 sin6;
2024
2025 /*
2026 * Save original sockaddr_in6 addr and convert it
2027 * to sockaddr_in.
2028 */
2029 sin6 = *(struct sockaddr_in6 *)nam;
2030 sin_p = (struct sockaddr_in *)nam;
2031 in6_sin6_2_sin(sin_p, &sin6);
2032 }
2033
2034 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2035 void
2036 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2037 {
2038 struct sockaddr_in *sin_p;
2039 struct sockaddr_in6 *sin6_p;
2040
2041 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2042 M_WAITOK);
2043 sin_p = (struct sockaddr_in *)*nam;
2044 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2045 FREE(*nam, M_SONAME);
2046 *nam = (struct sockaddr *)sin6_p;
2047 }
Cache object: aec89fa679edd07db99e52376165a90b
|