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