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