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