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